1 /* trace.c --- tracing output for the RL78 simulator.
3 Copyright (C) 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Red Hat, Inc.
7 This file is part of the GNU simulators.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/types.h>
33 #include "libiberty.h"
42 sim_dis_read (bfd_vma memaddr, bfd_byte * ptr, unsigned int length,
43 struct disassemble_info *info)
45 mem_get_blk (memaddr, ptr, length);
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. */
54 remove_useless_symbols (asymbol ** symbols, long count)
56 register asymbol **in_ptr = symbols, **out_ptr = symbols;
60 asymbol *sym = *in_ptr ++;
62 if (strstr (sym->name, "gcc2_compiled"))
64 if (sym->name == NULL || sym->name[0] == '\0')
66 if (sym->flags & (BSF_DEBUGGING))
68 if (bfd_is_und_section (sym->section)
69 || bfd_is_com_section (sym->section))
74 return out_ptr - symbols;
78 compare_symbols (const PTR ap, const PTR bp)
80 const asymbol *a = *(const asymbol **) ap;
81 const asymbol *b = *(const asymbol **) bp;
83 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
85 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
90 static char opbuf[1000];
93 op_printf (char *buf, char *fmt, ...)
99 ret = vsprintf (opbuf + strlen (opbuf), fmt, ap);
104 static bfd * current_bfd = NULL;
105 static asymbol ** symtab = NULL;
106 static int symcount = 0;
107 static asection * code_section = NULL;
108 static bfd_vma code_base = 0;
109 static struct disassemble_info info;
112 sim_disasm_init (bfd *prog)
128 load_file_and_line (const char *filename, int lineno)
131 for (f = files; f; f = f->next)
132 if (strcmp (f->filename, filename) == 0)
138 const char *found_filename, *slash;
140 found_filename = filename;
143 if (stat (found_filename, &s) == 0)
145 slash = strchr (found_filename, '/');
148 found_filename = slash + 1;
151 f = (Files *) xmalloc (sizeof (Files));
154 f->filename = xstrdup (filename);
155 f->data = (char *) xmalloc (s.st_size + 2);
156 FILE *file = fopen (found_filename, "rb");
157 fread (f->data, 1, s.st_size, file);
158 f->data[s.st_size] = 0;
162 for (i = 0; i < s.st_size; i ++)
163 if (f->data[i] == '\n')
165 f->lines = (char **) xmalloc (f->nlines * sizeof (char *));
166 f->lines[0] = f->data;
168 for (i = 0; i < s.st_size; i ++)
169 if (f->data[i] == '\n')
171 f->lines[f->nlines] = f->data + i + 1;
172 while (*f->lines[f->nlines] == ' '
173 || *f->lines[f->nlines] == '\t')
174 f->lines[f->nlines] ++;
179 if (lineno < 1 || lineno > f->nlines)
181 return f->lines[lineno - 1];
185 sim_get_current_source_location (const char ** pfilename,
186 const char ** pfunctionname,
187 unsigned int * plineno)
189 static int initted = 0;
192 if (current_bfd == NULL)
201 memset (& info, 0, sizeof (info));
202 INIT_DISASSEMBLE_INFO (info, stdout, op_printf);
203 info.read_memory_func = sim_dis_read;
204 info.arch = bfd_get_arch (current_bfd);
205 info.mach = bfd_get_mach (current_bfd);
207 info.arch = bfd_arch_rl78;
209 disassemble_init_for_target (& info);
211 storage = bfd_get_symtab_upper_bound (current_bfd);
214 symtab = (asymbol **) xmalloc (storage);
215 symcount = bfd_canonicalize_symtab (current_bfd, symtab);
216 symcount = remove_useless_symbols (symtab, symcount);
217 qsort (symtab, symcount, sizeof (asymbol *), compare_symbols);
220 for (s = current_bfd->sections; s; s = s->next)
222 if (s->flags & SEC_CODE || code_section == 0)
225 code_base = bfd_section_lma (current_bfd, s);
231 *pfilename = *pfunctionname = NULL;
234 bfd_find_nearest_line
235 (current_bfd, code_section, symtab, mypc - code_base,
236 pfilename, pfunctionname, plineno);
242 sim_disasm_one (void)
244 static int last_sym = -1;
245 static const char * prev_filename = "";
246 static int prev_lineno = 0;
247 const char * filename;
248 const char * functionname;
252 int save_trace = trace;
255 if (! sim_get_current_source_location (& filename, & functionname, & lineno))
260 if (filename && functionname && lineno)
262 if (lineno != prev_lineno || strcmp (prev_filename, filename))
264 char * the_line = load_file_and_line (filename, lineno);
265 const char * slash = strrchr (filename, '/');
272 ("========================================"
273 "=====================================\n");
274 printf ("\033[37;41m %s:%d: \033[33;40m %s\033[K\033[0m\n",
275 slash, lineno, the_line);
277 prev_lineno = lineno;
278 prev_filename = filename;
283 while (min < max - 1)
287 sym = (min + max) / 2;
288 sa = bfd_asymbol_value (symtab[sym]);
289 /*printf ("checking %4d %08x %s\n",
290 sym, sa, bfd_asymbol_name (symtab[sym])); */
302 if (min != -1 && min != last_sym)
304 bestaddr = bfd_asymbol_value (symtab[min]);
305 printf ("\033[43;30m%s", bfd_asymbol_name (symtab[min]));
306 if (bestaddr != mypc)
307 printf ("+%d", mypc - bestaddr);
308 printf (":\t\t\t\033[0m\n");
312 if (strcmp (bfd_asymbol_name (symtab[min]), "abort") == 0
313 || strcmp (bfd_asymbol_name (symtab[min]), "exit") == 0)
321 #ifdef CYCLE_ACCURATE
322 printf ("\033[33m %04u %06x: ", (int)(regs.cycle_count % 10000), mypc);
324 printf ("\033[33m %08llx %06x: ", total_clocks, mypc);
327 max = print_insn_rl78 (mypc, & info);
329 for (i = 0; i < max; i ++)
330 printf ("%02x", mem_get_qi (mypc + i));
339 printf ("%-16s ", opbuf);
341 printf ("\033[0m\n");