[PATCH 22/57][Arm][GAS] Add support for MVE instructions: vmlaldav, vmlalv, vmlsldav...
[binutils-gdb.git] / sim / rx / trace.c
blobed65d19a8f947e84c4ebb853fcfd5221decd50cb
1 /* trace.c --- tracing output for the RX simulator.
3 Copyright (C) 2005-2019 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
6 This file is part of the GNU simulators.
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.
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.
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/>. */
22 #include "config.h"
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>
31 #include "bfd.h"
32 #include "dis-asm.h"
34 #include "cpu.h"
35 #include "mem.h"
36 #include "load.h"
38 static int
39 sim_dis_read (bfd_vma memaddr, bfd_byte * ptr, unsigned int length,
40 struct disassemble_info *info)
42 int i;
44 if (rx_big_endian)
46 /* See load.c for an explanation of this. */
47 for (i=0; i<length; i++)
48 ptr[i] = mem_get_qi ((memaddr + i) ^ 3);
50 else
51 mem_get_blk (memaddr, ptr, length);
52 return 0;
55 /* Filter out (in place) symbols that are useless for disassembly.
56 COUNT is the number of elements in SYMBOLS.
57 Return the number of useful symbols. */
59 static long
60 remove_useless_symbols (asymbol ** symbols, long count)
62 register asymbol **in_ptr = symbols, **out_ptr = symbols;
64 while (--count >= 0)
66 asymbol *sym = *in_ptr++;
68 if (strstr (sym->name, "gcc2_compiled"))
69 continue;
70 if (sym->name == NULL || sym->name[0] == '\0')
71 continue;
72 if (sym->flags & (BSF_DEBUGGING))
73 continue;
74 if (bfd_is_und_section (sym->section)
75 || bfd_is_com_section (sym->section))
76 continue;
78 *out_ptr++ = sym;
80 return out_ptr - symbols;
83 static int
84 compare_symbols (const PTR ap, const PTR bp)
86 const asymbol *a = *(const asymbol **) ap;
87 const asymbol *b = *(const asymbol **) bp;
89 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
90 return 1;
91 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
92 return -1;
93 return 0;
96 static char opbuf[1000];
98 static int
99 op_printf (char *buf, char *fmt, ...)
101 int ret;
102 va_list ap;
104 va_start (ap, fmt);
105 ret = vsprintf (opbuf + strlen (opbuf), fmt, ap);
106 va_end (ap);
107 return ret;
110 static bfd * current_bfd = NULL;
111 static asymbol ** symtab = NULL;
112 static int symcount = 0;
113 static asection * code_section = NULL;
114 static bfd_vma code_base = 0;
115 static struct disassemble_info info;
117 void
118 sim_disasm_init (bfd *prog)
120 current_bfd = prog;
123 typedef struct Files
125 struct Files *next;
126 char *filename;
127 int nlines;
128 char **lines;
129 char *data;
130 } Files;
131 Files *files = 0;
133 static char *
134 load_file_and_line (const char *filename, int lineno)
136 Files *f;
137 for (f = files; f; f = f->next)
138 if (strcmp (f->filename, filename) == 0)
139 break;
140 if (!f)
142 int i;
143 struct stat s;
144 const char *found_filename, *slash;
146 found_filename = filename;
147 while (1)
149 if (stat (found_filename, &s) == 0)
150 break;
151 slash = strchr (found_filename, '/');
152 if (!slash)
153 return "";
154 found_filename = slash + 1;
157 f = (Files *) malloc (sizeof (Files));
158 f->next = files;
159 files = f;
160 f->filename = strdup (filename);
161 f->data = (char *) malloc (s.st_size + 2);
162 FILE *file = fopen (found_filename, "rb");
163 fread (f->data, 1, s.st_size, file);
164 f->data[s.st_size] = 0;
165 fclose (file);
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 **) malloc (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')
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;
185 if (lineno < 1 || lineno > f->nlines)
186 return "";
187 return f->lines[lineno - 1];
191 sim_get_current_source_location (const char ** pfilename,
192 const char ** pfunctionname,
193 unsigned int * plineno)
195 static int initted = 0;
196 int mypc = get_reg (pc);
198 if (current_bfd == NULL)
199 return 0;
201 if (!initted)
203 int storage;
204 asection * s;
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_rx;
215 disassemble_init_for_target (& info);
217 storage = bfd_get_symtab_upper_bound (current_bfd);
218 if (storage > 0)
220 symtab = (asymbol **) malloc (storage);
221 symcount = bfd_canonicalize_symtab (current_bfd, symtab);
222 symcount = remove_useless_symbols (symtab, symcount);
223 qsort (symtab, symcount, sizeof (asymbol *), compare_symbols);
226 for (s = current_bfd->sections; s; s = s->next)
228 if (s->flags & SEC_CODE || code_section == 0)
230 code_section = s;
231 code_base = bfd_section_lma (current_bfd, s);
232 break;
237 *pfilename = *pfunctionname = NULL;
238 *plineno = 0;
240 bfd_find_nearest_line
241 (current_bfd, code_section, symtab, mypc - code_base,
242 pfilename, pfunctionname, plineno);
244 return 1;
247 void
248 sim_disasm_one (void)
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 = get_reg (pc);
261 if (! sim_get_current_source_location (& filename, & functionname, & lineno))
262 return;
264 trace = 0;
266 if (filename && functionname && lineno)
268 if (lineno != prev_lineno || strcmp (prev_filename, filename))
270 char * the_line = load_file_and_line (filename, lineno);
271 const char * slash = strrchr (filename, '/');
273 if (!slash)
274 slash = filename;
275 else
276 slash++;
277 printf
278 ("========================================"
279 "=====================================\n");
280 printf ("\033[37;41m %s:%d: \033[33;40m %s\033[K\033[0m\n",
281 slash, lineno, the_line);
283 prev_lineno = lineno;
284 prev_filename = filename;
287 min = -1;
288 max = symcount;
289 while (min < max - 1)
291 bfd_vma sa;
293 sym = (min + max) / 2;
294 sa = bfd_asymbol_value (symtab[sym]);
295 /*printf("checking %4d %08x %s\n",
296 sym, sa, bfd_asymbol_name (symtab[sym])); */
297 if (sa > mypc)
298 max = sym;
299 else if (sa < mypc)
300 min = sym;
301 else
303 min = sym;
304 break;
308 if (min != -1 && min != last_sym)
310 bestaddr = bfd_asymbol_value (symtab[min]);
311 printf ("\033[43;30m%s", bfd_asymbol_name (symtab[min]));
312 if (bestaddr != mypc)
313 printf ("+%d", mypc - bestaddr);
314 printf (":\t\t\t\033[0m\n");
315 last_sym = min;
316 #if 0
317 if (trace == 1)
318 if (strcmp (bfd_asymbol_name (symtab[min]), "abort") == 0
319 || strcmp (bfd_asymbol_name (symtab[min]), "exit") == 0)
320 trace = 0;
321 #endif
324 opbuf[0] = 0;
325 #ifdef CYCLE_ACCURATE
326 printf ("\033[33m %04u %06x: ", (int)(regs.cycle_count % 10000), mypc);
327 #else
328 printf ("\033[33m %06x: ", mypc);
330 #endif
332 max = print_insn_rx (mypc, & info);
334 for (i = 0; i < max; i++)
336 if (rx_big_endian)
337 printf ("%02x", mem_get_qi ((mypc + i) ^ 3));
338 else
339 printf ("%02x", mem_get_qi (mypc + i));
344 printf (" ");
345 i ++;
347 while (i < 6);
349 printf ("%-16s ", opbuf);
351 printf ("\033[0m\n");
352 trace = save_trace;