GDB copyright headers update after running GDB's copyright.py script.
[external/binutils.git] / sim / msp430 / trace.c
1 /* trace.c --- tracing output for the MSP430 simulator.
2
3    Copyright (C) 2005-2016 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 #include "trace.h"
36
37 static int
38 sim_dis_read (bfd_vma memaddr, bfd_byte * ptr, unsigned int length,
39               struct disassemble_info *info)
40 {
41   return 0;
42 }
43
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. */
47
48 static long
49 remove_useless_symbols (asymbol ** symbols, long count)
50 {
51   asymbol **in_ptr = symbols, **out_ptr = symbols;
52
53   while (-- count >= 0)
54     {
55       asymbol *sym = *in_ptr ++;
56
57       if (strstr (sym->name, "gcc2_compiled"))
58         continue;
59       if (sym->name == NULL || sym->name[0] == '\0')
60         continue;
61       if (sym->flags & (BSF_DEBUGGING))
62         continue;
63       if (bfd_is_und_section (sym->section)
64           || bfd_is_com_section (sym->section))
65         continue;
66       
67       if (sym->name[0] == '.' && sym->name[1] == 'L')
68         continue;
69
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)
73         continue;
74
75       *out_ptr++ = sym;
76     }
77   return out_ptr - symbols;
78 }
79
80 static int
81 compare_symbols (const PTR ap, const PTR bp)
82 {
83   const asymbol *a = *(const asymbol **) ap;
84   const asymbol *b = *(const asymbol **) bp;
85
86   if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
87     return 1;
88   else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
89     return -1;
90   return 0;
91 }
92
93 static char opbuf[1000];
94
95 static int
96 op_printf (char *buf, char *fmt, ...)
97 {
98   int ret;
99   va_list ap;
100
101   va_start (ap, fmt);
102   ret = vsprintf (opbuf + strlen (opbuf), fmt, ap);
103   va_end (ap);
104   return ret;
105 }
106
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;
113
114 void
115 msp430_trace_init (bfd *prog)
116 {
117   current_bfd = prog;
118 }
119
120 typedef struct Files
121 {
122   struct Files *next;
123   char *filename;
124   int nlines;
125   char **lines;
126   char *data;
127 } Files;
128 Files *files = 0;
129
130 static char *
131 load_file_and_line (const char *filename, int lineno)
132 {
133   Files *f;
134   for (f = files; f; f = f->next)
135     if (strcmp (f->filename, filename) == 0)
136       break;
137   if (!f)
138     {
139       int i;
140       struct stat s;
141       const char *found_filename, *slash;
142       FILE *file;
143
144       found_filename = filename;
145       while (1)
146         {
147           if (stat (found_filename, &s) == 0)
148             break;
149           slash = strchr (found_filename, '/');
150           if (!slash)
151             return "";
152           found_filename = slash + 1;
153         }
154
155       f = (Files *) xmalloc (sizeof (Files));
156       f->next = files;
157       files = f;
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;
163       fclose (file);
164
165       f->nlines = 1;
166       for (i = 0; i < s.st_size; i ++)
167         if (f->data[i] == '\n')
168           f->nlines ++;
169       f->lines = (char **) xmalloc (f->nlines * sizeof (char *));
170       f->lines[0] = f->data;
171       f->nlines = 1;
172       for (i = 0; i < s.st_size; i ++)
173         if (f->data[i] == '\n')
174           {
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] ++;
179             f->nlines ++;
180             f->data[i] = 0;
181           }
182     }
183   if (lineno < 1 || lineno > f->nlines)
184     return "";
185   return f->lines[lineno - 1];
186 }
187
188 int
189 msp430_get_current_source_location (int mypc,
190                                     const char **  pfilename,
191                                     const char **  pfunctionname,
192                                     unsigned int * plineno)
193 {
194   static int   initted = 0;
195
196   if (current_bfd == NULL)
197     {
198       printf("no bfd\n");
199       return 0;
200     }
201
202   if (!initted)
203     {
204       int storage;
205       asection * s;
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         info.arch = bfd_arch_msp430;
215
216       disassemble_init_for_target (& info);
217
218       storage = bfd_get_symtab_upper_bound (current_bfd);
219       if (storage > 0)
220         {
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);
225         }
226
227       for (s = current_bfd->sections; s; s = s->next)
228         {
229           if (s->flags & SEC_CODE || code_section == 0)
230             {
231               code_section = s;
232               code_base = bfd_section_lma (current_bfd, s);
233               break;
234             }
235         }
236     }
237
238   *pfilename = *pfunctionname = NULL;
239   *plineno = 0;
240
241   bfd_find_nearest_line
242     (current_bfd, code_section, symtab, mypc - code_base,
243      pfilename, pfunctionname, plineno);
244
245   return 1;
246 }
247
248 void
249 msp430_trace_one (int mypc)
250 {
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;
256   unsigned int  lineno;
257   int           sym, bestaddr;
258   int           min, max, i;
259
260   if (! msp430_get_current_source_location (mypc, & filename, & functionname, & lineno))
261     return;
262
263   if (filename && functionname && lineno)
264     {
265       if (lineno != prev_lineno || strcmp (prev_filename, filename))
266         {
267           char *       the_line = load_file_and_line (filename, lineno);
268           const char * slash = strrchr (filename, '/');
269
270           if (!slash)
271             slash = filename;
272           else
273             slash ++;
274           fprintf
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);
279         }
280       prev_lineno = lineno;
281       prev_filename = filename;
282     }
283
284   min = -1;
285   max = symcount;
286   while (min < max - 1)
287     {
288       bfd_vma sa;
289
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])); */
294       if (sa > mypc)
295         max = sym;
296       else if (sa < mypc)
297         min = sym;
298       else
299         {
300           min = sym;
301           break;
302         }
303     }
304
305   if (min != -1 && min != last_sym)
306     {
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");
312       last_sym = min;
313 #if 0
314       if (trace == 1)
315         if (strcmp (bfd_asymbol_name (symtab[min]), "abort") == 0
316             || strcmp (bfd_asymbol_name (symtab[min]), "exit") == 0)
317           trace = 0;
318 #endif
319     }
320 }