]>
Commit | Line | Data |
---|---|---|
4f8d4a38 DD |
1 | /* trace.c --- tracing output for the RX simulator. |
2 | ||
3666a048 | 3 | Copyright (C) 2005-2021 Free Software Foundation, Inc. |
4f8d4a38 DD |
4 | Contributed by Red Hat, Inc. |
5 | ||
6 | This file is part of the GNU simulators. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
20 | ||
6df01ab8 MF |
21 | /* This must come before any other includes. */ |
22 | #include "defs.h" | |
4f8d4a38 DD |
23 | |
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 | ||
35 | #include "cpu.h" | |
36 | #include "mem.h" | |
37 | #include "load.h" | |
fab2b376 | 38 | #include "trace.h" |
4f8d4a38 DD |
39 | |
40 | static int | |
41 | sim_dis_read (bfd_vma memaddr, bfd_byte * ptr, unsigned int length, | |
42 | struct disassemble_info *info) | |
43 | { | |
44 | int i; | |
45 | ||
46 | if (rx_big_endian) | |
47 | { | |
48 | /* See load.c for an explanation of this. */ | |
49 | for (i=0; i<length; i++) | |
50 | ptr[i] = mem_get_qi ((memaddr + i) ^ 3); | |
51 | } | |
52 | else | |
53 | mem_get_blk (memaddr, ptr, length); | |
54 | return 0; | |
55 | } | |
56 | ||
57 | /* Filter out (in place) symbols that are useless for disassembly. | |
58 | COUNT is the number of elements in SYMBOLS. | |
59 | Return the number of useful symbols. */ | |
60 | ||
61 | static long | |
62 | remove_useless_symbols (asymbol ** symbols, long count) | |
63 | { | |
64 | register asymbol **in_ptr = symbols, **out_ptr = symbols; | |
65 | ||
66 | while (--count >= 0) | |
67 | { | |
68 | asymbol *sym = *in_ptr++; | |
69 | ||
70 | if (strstr (sym->name, "gcc2_compiled")) | |
71 | continue; | |
72 | if (sym->name == NULL || sym->name[0] == '\0') | |
73 | continue; | |
74 | if (sym->flags & (BSF_DEBUGGING)) | |
75 | continue; | |
76 | if (bfd_is_und_section (sym->section) | |
77 | || bfd_is_com_section (sym->section)) | |
78 | continue; | |
79 | ||
80 | *out_ptr++ = sym; | |
81 | } | |
82 | return out_ptr - symbols; | |
83 | } | |
84 | ||
85 | static int | |
86 | compare_symbols (const PTR ap, const PTR bp) | |
87 | { | |
88 | const asymbol *a = *(const asymbol **) ap; | |
89 | const asymbol *b = *(const asymbol **) bp; | |
90 | ||
91 | if (bfd_asymbol_value (a) > bfd_asymbol_value (b)) | |
92 | return 1; | |
93 | else if (bfd_asymbol_value (a) < bfd_asymbol_value (b)) | |
94 | return -1; | |
95 | return 0; | |
96 | } | |
97 | ||
98 | static char opbuf[1000]; | |
99 | ||
f08708cb | 100 | static int ATTRIBUTE_PRINTF (2, 3) |
4f8d4a38 DD |
101 | op_printf (char *buf, char *fmt, ...) |
102 | { | |
103 | int ret; | |
104 | va_list ap; | |
105 | ||
106 | va_start (ap, fmt); | |
107 | ret = vsprintf (opbuf + strlen (opbuf), fmt, ap); | |
108 | va_end (ap); | |
109 | return ret; | |
110 | } | |
111 | ||
112 | static bfd * current_bfd = NULL; | |
113 | static asymbol ** symtab = NULL; | |
114 | static int symcount = 0; | |
115 | static asection * code_section = NULL; | |
116 | static bfd_vma code_base = 0; | |
117 | static struct disassemble_info info; | |
118 | ||
119 | void | |
120 | sim_disasm_init (bfd *prog) | |
121 | { | |
122 | current_bfd = prog; | |
123 | } | |
124 | ||
125 | typedef struct Files | |
126 | { | |
127 | struct Files *next; | |
128 | char *filename; | |
129 | int nlines; | |
130 | char **lines; | |
131 | char *data; | |
132 | } Files; | |
133 | Files *files = 0; | |
134 | ||
135 | static char * | |
136 | load_file_and_line (const char *filename, int lineno) | |
137 | { | |
138 | Files *f; | |
139 | for (f = files; f; f = f->next) | |
140 | if (strcmp (f->filename, filename) == 0) | |
141 | break; | |
142 | if (!f) | |
143 | { | |
783a7b12 | 144 | FILE *file; |
4f8d4a38 DD |
145 | int i; |
146 | struct stat s; | |
9ee45557 | 147 | size_t ret; |
4f8d4a38 DD |
148 | const char *found_filename, *slash; |
149 | ||
150 | found_filename = filename; | |
151 | while (1) | |
152 | { | |
153 | if (stat (found_filename, &s) == 0) | |
154 | break; | |
155 | slash = strchr (found_filename, '/'); | |
156 | if (!slash) | |
157 | return ""; | |
158 | found_filename = slash + 1; | |
159 | } | |
160 | ||
161 | f = (Files *) malloc (sizeof (Files)); | |
162 | f->next = files; | |
163 | files = f; | |
164 | f->filename = strdup (filename); | |
165 | f->data = (char *) malloc (s.st_size + 2); | |
783a7b12 | 166 | file = fopen (found_filename, "rb"); |
9ee45557 MF |
167 | ret = fread (f->data, 1, s.st_size, file); |
168 | f->data[ret] = 0; | |
4f8d4a38 DD |
169 | fclose (file); |
170 | ||
171 | f->nlines = 1; | |
172 | for (i = 0; i < s.st_size; i++) | |
173 | if (f->data[i] == '\n') | |
174 | f->nlines++; | |
175 | f->lines = (char **) malloc (f->nlines * sizeof (char *)); | |
176 | f->lines[0] = f->data; | |
177 | f->nlines = 1; | |
178 | for (i = 0; i < s.st_size; i++) | |
179 | if (f->data[i] == '\n') | |
180 | { | |
181 | f->lines[f->nlines] = f->data + i + 1; | |
182 | while (*f->lines[f->nlines] == ' ' | |
183 | || *f->lines[f->nlines] == '\t') | |
184 | f->lines[f->nlines]++; | |
185 | f->nlines++; | |
186 | f->data[i] = 0; | |
187 | } | |
188 | } | |
189 | if (lineno < 1 || lineno > f->nlines) | |
190 | return ""; | |
191 | return f->lines[lineno - 1]; | |
192 | } | |
193 | ||
194 | int | |
195 | sim_get_current_source_location (const char ** pfilename, | |
196 | const char ** pfunctionname, | |
197 | unsigned int * plineno) | |
198 | { | |
199 | static int initted = 0; | |
200 | int mypc = get_reg (pc); | |
201 | ||
202 | if (current_bfd == NULL) | |
203 | return 0; | |
204 | ||
205 | if (!initted) | |
206 | { | |
207 | int storage; | |
208 | asection * s; | |
209 | ||
210 | initted = 1; | |
211 | memset (& info, 0, sizeof (info)); | |
212 | INIT_DISASSEMBLE_INFO (info, stdout, op_printf); | |
213 | info.read_memory_func = sim_dis_read; | |
214 | info.arch = bfd_get_arch (current_bfd); | |
215 | info.mach = bfd_get_mach (current_bfd); | |
216 | if (info.mach == 0) | |
217 | info.arch = bfd_arch_rx; | |
218 | ||
219 | disassemble_init_for_target (& info); | |
220 | ||
221 | storage = bfd_get_symtab_upper_bound (current_bfd); | |
222 | if (storage > 0) | |
223 | { | |
224 | symtab = (asymbol **) malloc (storage); | |
225 | symcount = bfd_canonicalize_symtab (current_bfd, symtab); | |
226 | symcount = remove_useless_symbols (symtab, symcount); | |
227 | qsort (symtab, symcount, sizeof (asymbol *), compare_symbols); | |
228 | } | |
229 | ||
230 | for (s = current_bfd->sections; s; s = s->next) | |
231 | { | |
232 | if (s->flags & SEC_CODE || code_section == 0) | |
233 | { | |
234 | code_section = s; | |
fd361982 | 235 | code_base = bfd_section_lma (s); |
4f8d4a38 DD |
236 | break; |
237 | } | |
238 | } | |
239 | } | |
240 | ||
241 | *pfilename = *pfunctionname = NULL; | |
242 | *plineno = 0; | |
243 | ||
244 | bfd_find_nearest_line | |
245 | (current_bfd, code_section, symtab, mypc - code_base, | |
246 | pfilename, pfunctionname, plineno); | |
247 | ||
248 | return 1; | |
249 | } | |
250 | ||
251 | void | |
252 | sim_disasm_one (void) | |
253 | { | |
254 | static int last_sym = -1; | |
255 | static const char * prev_filename = ""; | |
256 | static int prev_lineno = 0; | |
257 | const char * filename; | |
258 | const char * functionname; | |
259 | unsigned int lineno; | |
260 | int sym, bestaddr; | |
261 | int min, max, i; | |
262 | int save_trace = trace; | |
263 | int mypc = get_reg (pc); | |
264 | ||
265 | if (! sim_get_current_source_location (& filename, & functionname, & lineno)) | |
266 | return; | |
267 | ||
268 | trace = 0; | |
269 | ||
270 | if (filename && functionname && lineno) | |
271 | { | |
272 | if (lineno != prev_lineno || strcmp (prev_filename, filename)) | |
273 | { | |
274 | char * the_line = load_file_and_line (filename, lineno); | |
275 | const char * slash = strrchr (filename, '/'); | |
276 | ||
277 | if (!slash) | |
278 | slash = filename; | |
279 | else | |
280 | slash++; | |
281 | printf | |
282 | ("========================================" | |
283 | "=====================================\n"); | |
284 | printf ("\033[37;41m %s:%d: \033[33;40m %s\033[K\033[0m\n", | |
285 | slash, lineno, the_line); | |
286 | } | |
287 | prev_lineno = lineno; | |
288 | prev_filename = filename; | |
289 | } | |
290 | ||
291 | min = -1; | |
292 | max = symcount; | |
293 | while (min < max - 1) | |
294 | { | |
295 | bfd_vma sa; | |
296 | ||
297 | sym = (min + max) / 2; | |
298 | sa = bfd_asymbol_value (symtab[sym]); | |
299 | /*printf("checking %4d %08x %s\n", | |
300 | sym, sa, bfd_asymbol_name (symtab[sym])); */ | |
301 | if (sa > mypc) | |
302 | max = sym; | |
303 | else if (sa < mypc) | |
304 | min = sym; | |
305 | else | |
306 | { | |
307 | min = sym; | |
308 | break; | |
309 | } | |
310 | } | |
311 | ||
312 | if (min != -1 && min != last_sym) | |
313 | { | |
314 | bestaddr = bfd_asymbol_value (symtab[min]); | |
315 | printf ("\033[43;30m%s", bfd_asymbol_name (symtab[min])); | |
316 | if (bestaddr != mypc) | |
317 | printf ("+%d", mypc - bestaddr); | |
318 | printf (":\t\t\t\033[0m\n"); | |
319 | last_sym = min; | |
320 | #if 0 | |
321 | if (trace == 1) | |
322 | if (strcmp (bfd_asymbol_name (symtab[min]), "abort") == 0 | |
323 | || strcmp (bfd_asymbol_name (symtab[min]), "exit") == 0) | |
324 | trace = 0; | |
325 | #endif | |
326 | } | |
327 | ||
328 | opbuf[0] = 0; | |
93378652 DD |
329 | #ifdef CYCLE_ACCURATE |
330 | printf ("\033[33m %04u %06x: ", (int)(regs.cycle_count % 10000), mypc); | |
331 | #else | |
332 | printf ("\033[33m %06x: ", mypc); | |
333 | ||
334 | #endif | |
335 | ||
4f8d4a38 DD |
336 | max = print_insn_rx (mypc, & info); |
337 | ||
338 | for (i = 0; i < max; i++) | |
339 | { | |
340 | if (rx_big_endian) | |
341 | printf ("%02x", mem_get_qi ((mypc + i) ^ 3)); | |
342 | else | |
343 | printf ("%02x", mem_get_qi (mypc + i)); | |
344 | } | |
345 | ||
346 | do | |
347 | { | |
348 | printf (" "); | |
349 | i ++; | |
350 | } | |
351 | while (i < 6); | |
352 | ||
353 | printf ("%-16s ", opbuf); | |
354 | ||
355 | printf ("\033[0m\n"); | |
356 | trace = save_trace; | |
357 | } |