]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/m32c/trace.c
run copyright.sh for 2011.
[thirdparty/binutils-gdb.git] / sim / m32c / trace.c
CommitLineData
d45a4bef
JB
1/* trace.c --- tracing output for the M32C simulator.
2
7b6bb8da
JB
3Copyright (C) 2005, 2007, 2008, 2009, 2010, 2011
4Free Software Foundation, Inc.
d45a4bef
JB
5Contributed by Red Hat, Inc.
6
7This file is part of the GNU simulators.
8
4744ac1b
JB
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 3 of the License, or
12(at your option) any later version.
d45a4bef 13
4744ac1b
JB
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
d45a4bef
JB
18
19You should have received a copy of the GNU General Public License
4744ac1b 20along with this program. If not, see <http://www.gnu.org/licenses/>. */
d45a4bef
JB
21
22
23#include <stdio.h>
24#include <stdarg.h>
25#include <string.h>
26#include <stdlib.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <ctype.h>
30
31#include "bfd.h"
32#include "dis-asm.h"
33#include "m32c-desc.h"
34
35#include "cpu.h"
36#include "mem.h"
37#include "load.h"
38
39static int
40sim_dis_read (bfd_vma memaddr, bfd_byte * ptr, unsigned int length,
41 struct disassemble_info *info)
42{
43 mem_get_blk (memaddr, ptr, length);
44 return 0;
45}
46
47/* Filter out (in place) symbols that are useless for disassembly.
48 COUNT is the number of elements in SYMBOLS.
49 Return the number of useful symbols. */
50
51static long
52remove_useless_symbols (asymbol ** symbols, long count)
53{
54 register asymbol **in_ptr = symbols, **out_ptr = symbols;
55
56 while (--count >= 0)
57 {
58 asymbol *sym = *in_ptr++;
59
60 if (strstr (sym->name, "gcc2_compiled"))
61 continue;
62 if (sym->name == NULL || sym->name[0] == '\0')
63 continue;
64 if (sym->flags & (BSF_DEBUGGING))
65 continue;
66 if (bfd_is_und_section (sym->section)
67 || bfd_is_com_section (sym->section))
68 continue;
69
70 *out_ptr++ = sym;
71 }
72 return out_ptr - symbols;
73}
74
75static int
76compare_symbols (const PTR ap, const PTR bp)
77{
78 const asymbol *a = *(const asymbol **) ap;
79 const asymbol *b = *(const asymbol **) bp;
80
81 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
82 return 1;
83 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
84 return -1;
85 return 0;
86}
87
88static char opbuf[1000];
89
90static int
91op_printf (char *buf, char *fmt, ...)
92{
93 int ret;
94 va_list ap;
95
96 va_start (ap, fmt);
97 ret = vsprintf (opbuf + strlen (opbuf), fmt, ap);
98 va_end (ap);
99 return ret;
100}
101
102static bfd *current_bfd;
103
104void
3877a145 105sim_disasm_init (bfd * prog)
d45a4bef
JB
106{
107 current_bfd = prog;
108}
109
110typedef struct Files
111{
112 struct Files *next;
113 char *filename;
114 int nlines;
115 char **lines;
116 char *data;
117} Files;
118Files *files = 0;
119
120static char *
121load_file_and_line (const char *filename, int lineno)
122{
123 Files *f;
124 for (f = files; f; f = f->next)
125 if (strcmp (f->filename, filename) == 0)
126 break;
127 if (!f)
128 {
129 int i;
130 struct stat s;
131 const char *found_filename, *slash;
132
133 found_filename = filename;
134 while (1)
135 {
136 if (stat (found_filename, &s) == 0)
137 break;
138 slash = strchr (found_filename, '/');
139 if (!slash)
140 return "";
141 found_filename = slash + 1;
142 }
143
144 f = (Files *) malloc (sizeof (Files));
145 f->next = files;
146 files = f;
147 f->filename = strdup (filename);
148 f->data = (char *) malloc (s.st_size + 2);
149 FILE *file = fopen (found_filename, "rb");
150 fread (f->data, 1, s.st_size, file);
151 f->data[s.st_size] = 0;
152 fclose (file);
153
154 f->nlines = 1;
155 for (i = 0; i < s.st_size; i++)
156 if (f->data[i] == '\n')
157 f->nlines++;
158 f->lines = (char **) malloc (f->nlines * sizeof (char *));
159 f->lines[0] = f->data;
160 f->nlines = 1;
161 for (i = 0; i < s.st_size; i++)
162 if (f->data[i] == '\n')
163 {
164 f->lines[f->nlines] = f->data + i + 1;
165 while (*f->lines[f->nlines] == ' '
166 || *f->lines[f->nlines] == '\t')
167 f->lines[f->nlines]++;
168 f->nlines++;
169 f->data[i] = 0;
170 }
171 }
172 if (lineno < 1 || lineno > f->nlines)
173 return "";
174 return f->lines[lineno - 1];
175}
176
177void
178sim_disasm_one ()
179{
180 static int initted = 0;
181 static asymbol **symtab = 0;
182 static int symcount = 0;
183 static int last_sym = -1;
184 static struct disassemble_info info;
185 int storage, sym, bestaddr;
186 int min, max, i;
187 static asection *code_section = 0;
188 static bfd_vma code_base = 0;
189 asection *s;
190 int save_trace = trace;
191
192 static const char *prev_filename = "";
193 static int prev_lineno = 0;
194 const char *filename;
195 const char *functionname;
196 unsigned int lineno;
197
198 int mypc = get_reg (pc);
199
200 if (current_bfd == 0)
201 return;
202
203 trace = 0;
204
205 if (!initted)
206 {
207 initted = 1;
208 memset (&info, 0, sizeof (info));
209 INIT_DISASSEMBLE_INFO (info, stdout, op_printf);
210 info.read_memory_func = sim_dis_read;
211 info.arch = bfd_get_arch (current_bfd);
212 info.mach = bfd_get_mach (current_bfd);
213 if (info.mach == 0)
214 {
215 info.arch = bfd_arch_m32c;
216 info.mach = default_machine;
217 }
218 disassemble_init_for_target (&info);
219
220 storage = bfd_get_symtab_upper_bound (current_bfd);
221 if (storage > 0)
222 {
223 symtab = (asymbol **) malloc (storage);
224 symcount = bfd_canonicalize_symtab (current_bfd, symtab);
225 symcount = remove_useless_symbols (symtab, symcount);
226 qsort (symtab, symcount, sizeof (asymbol *), compare_symbols);
227 }
228 for (s = current_bfd->sections; s; s = s->next)
229 {
230 if (s->flags & SEC_CODE || code_section == 0)
231 {
232 code_section = s;
233 code_base = bfd_section_lma (current_bfd, s);
234 break;
235 }
236 }
237 }
238
239 filename = functionname = 0;
240 lineno = 0;
241 if (bfd_find_nearest_line
242 (current_bfd, code_section, symtab, mypc - code_base, &filename,
243 &functionname, &lineno))
244 {
245 if (filename && functionname && lineno)
246 {
247 if (lineno != prev_lineno || strcmp (prev_filename, filename))
248 {
249 char *the_line = load_file_and_line (filename, lineno);
250 const char *slash = strrchr (filename, '/');
251 if (!slash)
252 slash = filename;
253 else
254 slash++;
255 printf
256 ("========================================"
3877a145 257 "=====================================\n");
d45a4bef
JB
258 printf ("\033[37;41m %s:%d: \033[33;40m %s\033[K\033[0m\n",
259 slash, lineno, the_line);
260 }
261 prev_lineno = lineno;
262 prev_filename = filename;
263 }
264 }
265
266 {
267 min = -1;
268 max = symcount;
269 while (min < max - 1)
270 {
271 bfd_vma sa;
272 sym = (min + max) / 2;
273 sa = bfd_asymbol_value (symtab[sym]);
274 /*printf("checking %4d %08x %s\n",
3877a145 275 sym, sa, bfd_asymbol_name (symtab[sym])); */
d45a4bef
JB
276 if (sa > mypc)
277 max = sym;
278 else if (sa < mypc)
279 min = sym;
280 else
281 {
282 min = sym;
283 break;
284 }
285 }
286 if (min != -1 && min != last_sym)
287 {
288 bestaddr = bfd_asymbol_value (symtab[min]);
289 printf ("\033[43;30m%s", bfd_asymbol_name (symtab[min]));
290 if (bestaddr != mypc)
291 printf ("+%d", mypc - bestaddr);
292 printf (":\t\t\t\033[0m\n");
293 last_sym = min;
294#if 0
295 if (trace == 1)
296 if (strcmp (bfd_asymbol_name (symtab[min]), "abort") == 0
297 || strcmp (bfd_asymbol_name (symtab[min]), "exit") == 0)
298 trace = 0;
299#endif
300 }
301 }
302
303 opbuf[0] = 0;
304 printf ("\033[33m%06x: ", mypc);
305 max = print_insn_m32c (mypc, &info);
306 for (i = 0; i < max; i++)
307 printf ("%02x", mem_get_qi (mypc + i));
308 for (; i < 6; i++)
309 printf (" ");
310 printf ("%-16s ", opbuf);
311
312 printf ("\033[0m\n");
313 trace = save_trace;
314}