1 /* trace.c --- tracing output for the MSP430 simulator.
3 Copyright (C) 2005-2015 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/>.
28 #include <sys/types.h>
32 #include "libiberty.h"
38 sim_dis_read (bfd_vma memaddr
, bfd_byte
* ptr
, unsigned int length
,
39 struct disassemble_info
*info
)
44 /* Filter out (in place) symbols that are useless for disassembly.
45 COUNT is the number of elements in SYMBOLS.
46 Return the number of useful symbols. */
49 remove_useless_symbols (asymbol
** symbols
, long count
)
51 asymbol
**in_ptr
= symbols
, **out_ptr
= symbols
;
55 asymbol
*sym
= *in_ptr
++;
57 if (strstr (sym
->name
, "gcc2_compiled"))
59 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
61 if (sym
->flags
& (BSF_DEBUGGING
))
63 if (bfd_is_und_section (sym
->section
)
64 || bfd_is_com_section (sym
->section
))
67 if (sym
->name
[0] == '.' && sym
->name
[1] == 'L')
70 /* If the symbol ends in ^A or ^B it is
71 an assembler generated local label. */
72 if (sym
->name
[strlen (sym
->name
) - 1] < 32)
77 return out_ptr
- symbols
;
81 compare_symbols (const PTR ap
, const PTR bp
)
83 const asymbol
*a
= *(const asymbol
**) ap
;
84 const asymbol
*b
= *(const asymbol
**) bp
;
86 if (bfd_asymbol_value (a
) > bfd_asymbol_value (b
))
88 else if (bfd_asymbol_value (a
) < bfd_asymbol_value (b
))
93 static char opbuf
[1000];
96 op_printf (char *buf
, char *fmt
, ...)
102 ret
= vsprintf (opbuf
+ strlen (opbuf
), fmt
, ap
);
107 static bfd
* current_bfd
= NULL
;
108 static asymbol
** symtab
= NULL
;
109 static int symcount
= 0;
110 static asection
* code_section
= NULL
;
111 static bfd_vma code_base
= 0;
112 static struct disassemble_info info
;
115 msp430_trace_init (bfd
*prog
)
131 load_file_and_line (const char *filename
, int lineno
)
134 for (f
= files
; f
; f
= f
->next
)
135 if (strcmp (f
->filename
, filename
) == 0)
141 const char *found_filename
, *slash
;
144 found_filename
= filename
;
147 if (stat (found_filename
, &s
) == 0)
149 slash
= strchr (found_filename
, '/');
152 found_filename
= slash
+ 1;
155 f
= (Files
*) xmalloc (sizeof (Files
));
158 f
->filename
= xstrdup (filename
);
159 f
->data
= (char *) xmalloc (s
.st_size
+ 2);
160 file
= fopen (found_filename
, "rb");
161 fread (f
->data
, 1, s
.st_size
, file
);
162 f
->data
[s
.st_size
] = 0;
166 for (i
= 0; i
< s
.st_size
; i
++)
167 if (f
->data
[i
] == '\n')
169 f
->lines
= (char **) xmalloc (f
->nlines
* sizeof (char *));
170 f
->lines
[0] = f
->data
;
172 for (i
= 0; i
< s
.st_size
; i
++)
173 if (f
->data
[i
] == '\n')
175 f
->lines
[f
->nlines
] = f
->data
+ i
+ 1;
176 while (*f
->lines
[f
->nlines
] == ' '
177 || *f
->lines
[f
->nlines
] == '\t')
178 f
->lines
[f
->nlines
] ++;
183 if (lineno
< 1 || lineno
> f
->nlines
)
185 return f
->lines
[lineno
- 1];
189 msp430_get_current_source_location (int mypc
,
190 const char ** pfilename
,
191 const char ** pfunctionname
,
192 unsigned int * plineno
)
194 static int initted
= 0;
196 if (current_bfd
== NULL
)
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
);
214 info
.arch
= bfd_arch_msp430
;
216 disassemble_init_for_target (& info
);
218 storage
= bfd_get_symtab_upper_bound (current_bfd
);
221 symtab
= (asymbol
**) xmalloc (storage
);
222 symcount
= bfd_canonicalize_symtab (current_bfd
, symtab
);
223 symcount
= remove_useless_symbols (symtab
, symcount
);
224 qsort (symtab
, symcount
, sizeof (asymbol
*), compare_symbols
);
227 for (s
= current_bfd
->sections
; s
; s
= s
->next
)
229 if (s
->flags
& SEC_CODE
|| code_section
== 0)
232 code_base
= bfd_section_lma (current_bfd
, s
);
238 *pfilename
= *pfunctionname
= NULL
;
241 bfd_find_nearest_line
242 (current_bfd
, code_section
, symtab
, mypc
- code_base
,
243 pfilename
, pfunctionname
, plineno
);
249 msp430_trace_one (int mypc
)
251 static int last_sym
= -1;
252 static const char * prev_filename
= "";
253 static int prev_lineno
= 0;
254 const char * filename
;
255 const char * functionname
;
260 if (! msp430_get_current_source_location (mypc
, & filename
, & functionname
, & lineno
))
263 if (filename
&& functionname
&& lineno
)
265 if (lineno
!= prev_lineno
|| strcmp (prev_filename
, filename
))
267 char * the_line
= load_file_and_line (filename
, lineno
);
268 const char * slash
= strrchr (filename
, '/');
275 (stderr
, "========================================"
276 "=====================================\n");
277 fprintf (stderr
, "\033[37;41m %s:%d: \033[33;40m %s\033[K\033[0m\n",
278 slash
, lineno
, the_line
);
280 prev_lineno
= lineno
;
281 prev_filename
= filename
;
286 while (min
< max
- 1)
290 sym
= (min
+ max
) / 2;
291 sa
= bfd_asymbol_value (symtab
[sym
]);
292 /*printf ("checking %4d %08x %s\n",
293 sym, sa, bfd_asymbol_name (symtab[sym])); */
305 if (min
!= -1 && min
!= last_sym
)
307 bestaddr
= bfd_asymbol_value (symtab
[min
]);
308 fprintf (stderr
, "\033[43;30m%s", bfd_asymbol_name (symtab
[min
]));
309 if (bestaddr
!= mypc
)
310 fprintf (stderr
, "+%d", mypc
- bestaddr
);
311 fprintf (stderr
, ":\t\t\t\033[0m\n");
315 if (strcmp (bfd_asymbol_name (symtab
[min
]), "abort") == 0
316 || strcmp (bfd_asymbol_name (symtab
[min
]), "exit") == 0)