]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/rl78/trace.c
Copyright year update in most files of the GDB Project.
[thirdparty/binutils-gdb.git] / sim / rl78 / trace.c
CommitLineData
87326c78
DD
1/* trace.c --- tracing output for the RL78 simulator.
2
3 Copyright (C) 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Red Hat, Inc.
6
7 This file is part of the GNU simulators.
8
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.
13
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.
18
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/>.
21*/
22
23
24#include "config.h"
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
41static int
42sim_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
53static long
54remove_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
77static int
78compare_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
90static char opbuf[1000];
91
92static int
93op_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
104static bfd * current_bfd = NULL;
105static asymbol ** symtab = NULL;
106static int symcount = 0;
107static asection * code_section = NULL;
108static bfd_vma code_base = 0;
109static struct disassemble_info info;
110
111void
112sim_disasm_init (bfd *prog)
113{
114 current_bfd = prog;
115}
116
117typedef struct Files
118{
119 struct Files *next;
120 char *filename;
121 int nlines;
122 char **lines;
123 char *data;
124} Files;
125Files *files = 0;
126
127static char *
128load_file_and_line (const char *filename, int lineno)
129{
130 Files *f;
131 for (f = files; f; f = f->next)
132 if (strcmp (f->filename, filename) == 0)
133 break;
134 if (!f)
135 {
136 int i;
137 struct stat s;
138 const char *found_filename, *slash;
139
140 found_filename = filename;
141 while (1)
142 {
143 if (stat (found_filename, &s) == 0)
144 break;
145 slash = strchr (found_filename, '/');
146 if (!slash)
147 return "";
148 found_filename = slash + 1;
149 }
150
151 f = (Files *) xmalloc (sizeof (Files));
152 f->next = files;
153 files = f;
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;
159 fclose (file);
160
161 f->nlines = 1;
162 for (i = 0; i < s.st_size; i ++)
163 if (f->data[i] == '\n')
164 f->nlines ++;
165 f->lines = (char **) xmalloc (f->nlines * sizeof (char *));
166 f->lines[0] = f->data;
167 f->nlines = 1;
168 for (i = 0; i < s.st_size; i ++)
169 if (f->data[i] == '\n')
170 {
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] ++;
175 f->nlines ++;
176 f->data[i] = 0;
177 }
178 }
179 if (lineno < 1 || lineno > f->nlines)
180 return "";
181 return f->lines[lineno - 1];
182}
183
184int
185sim_get_current_source_location (const char ** pfilename,
186 const char ** pfunctionname,
187 unsigned int * plineno)
188{
189 static int initted = 0;
190 int mypc = pc;
191
192 if (current_bfd == NULL)
193 return 0;
194
195 if (!initted)
196 {
197 int storage;
198 asection * s;
199
200 initted = 1;
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);
206 if (info.mach == 0)
207 info.arch = bfd_arch_rl78;
208
209 disassemble_init_for_target (& info);
210
211 storage = bfd_get_symtab_upper_bound (current_bfd);
212 if (storage > 0)
213 {
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);
218 }
219
220 for (s = current_bfd->sections; s; s = s->next)
221 {
222 if (s->flags & SEC_CODE || code_section == 0)
223 {
224 code_section = s;
225 code_base = bfd_section_lma (current_bfd, s);
226 break;
227 }
228 }
229 }
230
231 *pfilename = *pfunctionname = NULL;
232 *plineno = 0;
233
234 bfd_find_nearest_line
235 (current_bfd, code_section, symtab, mypc - code_base,
236 pfilename, pfunctionname, plineno);
237
238 return 1;
239}
240
241void
242sim_disasm_one (void)
243{
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;
249 unsigned int lineno;
250 int sym, bestaddr;
251 int min, max, i;
252 int save_trace = trace;
253 int mypc = pc;
254
255 if (! sim_get_current_source_location (& filename, & functionname, & lineno))
256 return;
257
258 trace = 0;
259
260 if (filename && functionname && lineno)
261 {
262 if (lineno != prev_lineno || strcmp (prev_filename, filename))
263 {
264 char * the_line = load_file_and_line (filename, lineno);
265 const char * slash = strrchr (filename, '/');
266
267 if (!slash)
268 slash = filename;
269 else
270 slash ++;
271 printf
272 ("========================================"
273 "=====================================\n");
274 printf ("\033[37;41m %s:%d: \033[33;40m %s\033[K\033[0m\n",
275 slash, lineno, the_line);
276 }
277 prev_lineno = lineno;
278 prev_filename = filename;
279 }
280
281 min = -1;
282 max = symcount;
283 while (min < max - 1)
284 {
285 bfd_vma sa;
286
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])); */
291 if (sa > mypc)
292 max = sym;
293 else if (sa < mypc)
294 min = sym;
295 else
296 {
297 min = sym;
298 break;
299 }
300 }
301
302 if (min != -1 && min != last_sym)
303 {
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");
309 last_sym = min;
310#if 0
311 if (trace == 1)
312 if (strcmp (bfd_asymbol_name (symtab[min]), "abort") == 0
313 || strcmp (bfd_asymbol_name (symtab[min]), "exit") == 0)
314 trace = 0;
315#endif
316 }
317
318#define TCR0 0xf0180
319
320 opbuf[0] = 0;
321#ifdef CYCLE_ACCURATE
322 printf ("\033[33m %04u %06x: ", (int)(regs.cycle_count % 10000), mypc);
323#else
324 printf ("\033[33m %08llx %06x: ", total_clocks, mypc);
325#endif
326
327 max = print_insn_rl78 (mypc, & info);
328
329 for (i = 0; i < max; i ++)
330 printf ("%02x", mem_get_qi (mypc + i));
331
332 do
333 {
334 printf (" ");
335 i ++;
336 }
337 while (i < 6);
338
339 printf ("%-16s ", opbuf);
340
341 printf ("\033[0m\n");
342 trace = save_trace;
343}