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