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