]>
Commit | Line | Data |
---|---|---|
d45a4bef JB |
1 | /* trace.c --- tracing output for the M32C simulator. |
2 | ||
3666a048 | 3 | Copyright (C) 2005-2021 Free Software Foundation, Inc. |
d45a4bef JB |
4 | Contributed by Red Hat, Inc. |
5 | ||
6 | This file is part of the GNU simulators. | |
7 | ||
4744ac1b JB |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3 of the License, or | |
11 | (at your option) any later version. | |
d45a4bef | 12 | |
4744ac1b JB |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
d45a4bef JB |
17 | |
18 | You should have received a copy of the GNU General Public License | |
4744ac1b | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
d45a4bef | 20 | |
6df01ab8 MF |
21 | /* This must come before any other includes. */ |
22 | #include "defs.h" | |
23 | ||
d45a4bef JB |
24 | #include <stdio.h> |
25 | #include <stdarg.h> | |
26 | #include <string.h> | |
27 | #include <stdlib.h> | |
28 | #include <sys/types.h> | |
29 | #include <sys/stat.h> | |
30 | #include <ctype.h> | |
31 | ||
32 | #include "bfd.h" | |
33 | #include "dis-asm.h" | |
34 | #include "m32c-desc.h" | |
35 | ||
36 | #include "cpu.h" | |
37 | #include "mem.h" | |
38 | #include "load.h" | |
269e9c18 | 39 | #include "trace.h" |
d45a4bef JB |
40 | |
41 | static int | |
42 | sim_dis_read (bfd_vma memaddr, bfd_byte * ptr, unsigned int length, | |
43 | struct disassemble_info *info) | |
44 | { | |
45 | mem_get_blk (memaddr, ptr, length); | |
46 | return 0; | |
47 | } | |
48 | ||
49 | /* Filter out (in place) symbols that are useless for disassembly. | |
50 | COUNT is the number of elements in SYMBOLS. | |
51 | Return the number of useful symbols. */ | |
52 | ||
53 | static long | |
54 | remove_useless_symbols (asymbol ** symbols, long count) | |
55 | { | |
56 | register asymbol **in_ptr = symbols, **out_ptr = symbols; | |
57 | ||
58 | while (--count >= 0) | |
59 | { | |
60 | asymbol *sym = *in_ptr++; | |
61 | ||
62 | if (strstr (sym->name, "gcc2_compiled")) | |
63 | continue; | |
64 | if (sym->name == NULL || sym->name[0] == '\0') | |
65 | continue; | |
66 | if (sym->flags & (BSF_DEBUGGING)) | |
67 | continue; | |
68 | if (bfd_is_und_section (sym->section) | |
69 | || bfd_is_com_section (sym->section)) | |
70 | continue; | |
71 | ||
72 | *out_ptr++ = sym; | |
73 | } | |
74 | return out_ptr - symbols; | |
75 | } | |
76 | ||
77 | static int | |
78 | compare_symbols (const PTR ap, const PTR bp) | |
79 | { | |
80 | const asymbol *a = *(const asymbol **) ap; | |
81 | const asymbol *b = *(const asymbol **) bp; | |
82 | ||
83 | if (bfd_asymbol_value (a) > bfd_asymbol_value (b)) | |
84 | return 1; | |
85 | else if (bfd_asymbol_value (a) < bfd_asymbol_value (b)) | |
86 | return -1; | |
87 | return 0; | |
88 | } | |
89 | ||
90 | static char opbuf[1000]; | |
91 | ||
92 | static int | |
93 | op_printf (char *buf, char *fmt, ...) | |
94 | { | |
95 | int ret; | |
96 | va_list ap; | |
97 | ||
98 | va_start (ap, fmt); | |
99 | ret = vsprintf (opbuf + strlen (opbuf), fmt, ap); | |
100 | va_end (ap); | |
101 | return ret; | |
102 | } | |
103 | ||
104 | static bfd *current_bfd; | |
105 | ||
106 | void | |
3877a145 | 107 | sim_disasm_init (bfd * prog) |
d45a4bef JB |
108 | { |
109 | current_bfd = prog; | |
110 | } | |
111 | ||
112 | typedef struct Files | |
113 | { | |
114 | struct Files *next; | |
115 | char *filename; | |
116 | int nlines; | |
117 | char **lines; | |
118 | char *data; | |
119 | } Files; | |
120 | Files *files = 0; | |
121 | ||
122 | static char * | |
123 | load_file_and_line (const char *filename, int lineno) | |
124 | { | |
125 | Files *f; | |
126 | for (f = files; f; f = f->next) | |
127 | if (strcmp (f->filename, filename) == 0) | |
128 | break; | |
129 | if (!f) | |
130 | { | |
131 | int i; | |
132 | struct stat s; | |
133 | const char *found_filename, *slash; | |
269e9c18 | 134 | FILE *file; |
44056b7c | 135 | size_t ret; |
d45a4bef JB |
136 | |
137 | found_filename = filename; | |
138 | while (1) | |
139 | { | |
140 | if (stat (found_filename, &s) == 0) | |
141 | break; | |
142 | slash = strchr (found_filename, '/'); | |
143 | if (!slash) | |
144 | return ""; | |
145 | found_filename = slash + 1; | |
146 | } | |
147 | ||
148 | f = (Files *) malloc (sizeof (Files)); | |
149 | f->next = files; | |
150 | files = f; | |
151 | f->filename = strdup (filename); | |
152 | f->data = (char *) malloc (s.st_size + 2); | |
269e9c18 | 153 | file = fopen (found_filename, "rb"); |
44056b7c MF |
154 | ret = fread (f->data, 1, s.st_size, file); |
155 | f->data[ret] = 0; | |
d45a4bef JB |
156 | fclose (file); |
157 | ||
158 | f->nlines = 1; | |
159 | for (i = 0; i < s.st_size; i++) | |
160 | if (f->data[i] == '\n') | |
161 | f->nlines++; | |
162 | f->lines = (char **) malloc (f->nlines * sizeof (char *)); | |
163 | f->lines[0] = f->data; | |
164 | f->nlines = 1; | |
165 | for (i = 0; i < s.st_size; i++) | |
166 | if (f->data[i] == '\n') | |
167 | { | |
168 | f->lines[f->nlines] = f->data + i + 1; | |
169 | while (*f->lines[f->nlines] == ' ' | |
170 | || *f->lines[f->nlines] == '\t') | |
171 | f->lines[f->nlines]++; | |
172 | f->nlines++; | |
173 | f->data[i] = 0; | |
174 | } | |
175 | } | |
176 | if (lineno < 1 || lineno > f->nlines) | |
177 | return ""; | |
178 | return f->lines[lineno - 1]; | |
179 | } | |
180 | ||
181 | void | |
269e9c18 | 182 | sim_disasm_one (void) |
d45a4bef JB |
183 | { |
184 | static int initted = 0; | |
185 | static asymbol **symtab = 0; | |
186 | static int symcount = 0; | |
187 | static int last_sym = -1; | |
188 | static struct disassemble_info info; | |
189 | int storage, sym, bestaddr; | |
190 | int min, max, i; | |
191 | static asection *code_section = 0; | |
192 | static bfd_vma code_base = 0; | |
193 | asection *s; | |
194 | int save_trace = trace; | |
195 | ||
196 | static const char *prev_filename = ""; | |
197 | static int prev_lineno = 0; | |
198 | const char *filename; | |
199 | const char *functionname; | |
200 | unsigned int lineno; | |
201 | ||
202 | int mypc = get_reg (pc); | |
203 | ||
204 | if (current_bfd == 0) | |
205 | return; | |
206 | ||
207 | trace = 0; | |
208 | ||
209 | if (!initted) | |
210 | { | |
211 | initted = 1; | |
212 | memset (&info, 0, sizeof (info)); | |
213 | INIT_DISASSEMBLE_INFO (info, stdout, op_printf); | |
214 | info.read_memory_func = sim_dis_read; | |
215 | info.arch = bfd_get_arch (current_bfd); | |
216 | info.mach = bfd_get_mach (current_bfd); | |
217 | if (info.mach == 0) | |
218 | { | |
219 | info.arch = bfd_arch_m32c; | |
220 | info.mach = default_machine; | |
221 | } | |
222 | disassemble_init_for_target (&info); | |
223 | ||
224 | storage = bfd_get_symtab_upper_bound (current_bfd); | |
225 | if (storage > 0) | |
226 | { | |
227 | symtab = (asymbol **) malloc (storage); | |
228 | symcount = bfd_canonicalize_symtab (current_bfd, symtab); | |
229 | symcount = remove_useless_symbols (symtab, symcount); | |
230 | qsort (symtab, symcount, sizeof (asymbol *), compare_symbols); | |
231 | } | |
232 | for (s = current_bfd->sections; s; s = s->next) | |
233 | { | |
234 | if (s->flags & SEC_CODE || code_section == 0) | |
235 | { | |
236 | code_section = s; | |
fd361982 | 237 | code_base = bfd_section_lma (s); |
d45a4bef JB |
238 | break; |
239 | } | |
240 | } | |
241 | } | |
242 | ||
243 | filename = functionname = 0; | |
244 | lineno = 0; | |
245 | if (bfd_find_nearest_line | |
246 | (current_bfd, code_section, symtab, mypc - code_base, &filename, | |
247 | &functionname, &lineno)) | |
248 | { | |
249 | if (filename && functionname && lineno) | |
250 | { | |
251 | if (lineno != prev_lineno || strcmp (prev_filename, filename)) | |
252 | { | |
253 | char *the_line = load_file_and_line (filename, lineno); | |
254 | const char *slash = strrchr (filename, '/'); | |
255 | if (!slash) | |
256 | slash = filename; | |
257 | else | |
258 | slash++; | |
259 | printf | |
260 | ("========================================" | |
3877a145 | 261 | "=====================================\n"); |
d45a4bef JB |
262 | printf ("\033[37;41m %s:%d: \033[33;40m %s\033[K\033[0m\n", |
263 | slash, lineno, the_line); | |
264 | } | |
265 | prev_lineno = lineno; | |
266 | prev_filename = filename; | |
267 | } | |
268 | } | |
269 | ||
270 | { | |
271 | min = -1; | |
272 | max = symcount; | |
273 | while (min < max - 1) | |
274 | { | |
275 | bfd_vma sa; | |
276 | sym = (min + max) / 2; | |
277 | sa = bfd_asymbol_value (symtab[sym]); | |
278 | /*printf("checking %4d %08x %s\n", | |
3877a145 | 279 | sym, sa, bfd_asymbol_name (symtab[sym])); */ |
d45a4bef JB |
280 | if (sa > mypc) |
281 | max = sym; | |
282 | else if (sa < mypc) | |
283 | min = sym; | |
284 | else | |
285 | { | |
286 | min = sym; | |
287 | break; | |
288 | } | |
289 | } | |
290 | if (min != -1 && min != last_sym) | |
291 | { | |
292 | bestaddr = bfd_asymbol_value (symtab[min]); | |
293 | printf ("\033[43;30m%s", bfd_asymbol_name (symtab[min])); | |
294 | if (bestaddr != mypc) | |
295 | printf ("+%d", mypc - bestaddr); | |
296 | printf (":\t\t\t\033[0m\n"); | |
297 | last_sym = min; | |
298 | #if 0 | |
299 | if (trace == 1) | |
300 | if (strcmp (bfd_asymbol_name (symtab[min]), "abort") == 0 | |
301 | || strcmp (bfd_asymbol_name (symtab[min]), "exit") == 0) | |
302 | trace = 0; | |
303 | #endif | |
304 | } | |
305 | } | |
306 | ||
307 | opbuf[0] = 0; | |
308 | printf ("\033[33m%06x: ", mypc); | |
309 | max = print_insn_m32c (mypc, &info); | |
310 | for (i = 0; i < max; i++) | |
311 | printf ("%02x", mem_get_qi (mypc + i)); | |
312 | for (; i < 6; i++) | |
313 | printf (" "); | |
314 | printf ("%-16s ", opbuf); | |
315 | ||
316 | printf ("\033[0m\n"); | |
317 | trace = save_trace; | |
318 | } |