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