OSDN Git Service

* addr2line.c (main): Protoype.
[pf3gnuchains/pf3gnuchains3x.git] / binutils / addr2line.c
1 /* addr2line.c -- convert addresses to line number and function name
2    Copyright 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3    Contributed by Ulrich Lauther <Ulrich.Lauther@mchp.siemens.de>
4
5    This file is part of GNU Binutils.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /* Derived from objdump.c and nm.c by Ulrich.Lauther@mchp.siemens.de
22
23    Usage: 
24    addr2line [options] addr addr ...
25    or
26    addr2line [options] 
27
28    both forms write results to stdout, the second form reads addresses
29    to be converted from stdin.  */
30
31 #include <string.h>
32
33 #include "bfd.h"
34 #include "getopt.h"
35 #include "libiberty.h"
36 #include "demangle.h"
37 #include "bucomm.h"
38
39 static boolean with_functions;  /* -f, show function names.  */
40 static boolean do_demangle;     /* -C, demangle names.  */
41 static boolean base_names;      /* -s, strip directory names.  */
42
43 static int naddr;               /* Number of addresses to process.  */
44 static char **addr;             /* Hex addresses to process.  */
45
46 static asymbol **syms;          /* Symbol table.  */
47
48 static struct option long_options[] =
49 {
50   {"basenames", no_argument, NULL, 's'},
51   {"demangle", optional_argument, NULL, 'C'},
52   {"exe", required_argument, NULL, 'e'},
53   {"functions", no_argument, NULL, 'f'},
54   {"target", required_argument, NULL, 'b'},
55   {"help", no_argument, NULL, 'H'},
56   {"version", no_argument, NULL, 'V'},
57   {0, no_argument, 0, 0}
58 };
59
60 static void usage PARAMS ((FILE *, int));
61 static void slurp_symtab PARAMS ((bfd *));
62 static void find_address_in_section PARAMS ((bfd *, asection *, PTR));
63 static void translate_addresses PARAMS ((bfd *));
64 static void process_file PARAMS ((const char *, const char *));
65 \f
66 /* Print a usage message to STREAM and exit with STATUS.  */
67
68 static void
69 usage (stream, status)
70      FILE *stream;
71      int status;
72 {
73   fprintf (stream, _("\
74 Usage: %s [-CfsHV] [-b bfdname] [--target=bfdname]\n\
75        [-e executable] [--exe=executable] [--demangle[=style]]\n\
76        [--basenames] [--functions] [addr addr ...]\n"),
77            program_name);
78   list_supported_targets (program_name, stream);
79   if (status == 0)
80     fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
81   exit (status);
82 }
83 \f
84 /* Read in the symbol table.  */
85
86 static void
87 slurp_symtab (abfd)
88      bfd *abfd;
89 {
90   long storage;
91   long symcount;
92
93   if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0)
94     return;
95
96   storage = bfd_get_symtab_upper_bound (abfd);
97   if (storage < 0)
98     bfd_fatal (bfd_get_filename (abfd));
99
100   syms = (asymbol **) xmalloc (storage);
101
102   symcount = bfd_canonicalize_symtab (abfd, syms);
103   if (symcount < 0)
104     bfd_fatal (bfd_get_filename (abfd));
105 }
106 \f
107 /* These global variables are used to pass information between
108    translate_addresses and find_address_in_section.  */
109
110 static bfd_vma pc;
111 static const char *filename;
112 static const char *functionname;
113 static unsigned int line;
114 static boolean found;
115
116 /* Look for an address in a section.  This is called via
117    bfd_map_over_sections.  */
118
119 static void
120 find_address_in_section (abfd, section, data)
121      bfd *abfd;
122      asection *section;
123      PTR data ATTRIBUTE_UNUSED;
124 {
125   bfd_vma vma;
126   bfd_size_type size;
127
128   if (found)
129     return;
130
131   if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
132     return;
133
134   vma = bfd_get_section_vma (abfd, section);
135   if (pc < vma)
136     return;
137
138   size = bfd_get_section_size_before_reloc (section);
139   if (pc >= vma + size)
140     return;
141
142   found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
143                                  &filename, &functionname, &line);
144 }
145
146 /* Read hexadecimal addresses from stdin, translate into
147    file_name:line_number and optionally function name.  */
148
149 static void
150 translate_addresses (abfd)
151      bfd *abfd;
152 {
153   int read_stdin = (naddr == 0);
154
155   for (;;)
156     {
157       if (read_stdin)
158         {
159           char addr_hex[100];
160
161           if (fgets (addr_hex, sizeof addr_hex, stdin) == NULL)
162             break;
163           pc = bfd_scan_vma (addr_hex, NULL, 16);
164         }
165       else
166         {
167           if (naddr <= 0)
168             break;
169           --naddr;
170           pc = bfd_scan_vma (*addr++, NULL, 16);
171         }
172
173       found = false;
174       bfd_map_over_sections (abfd, find_address_in_section, (PTR) NULL);
175
176       if (! found)
177         {
178           if (with_functions)
179             printf ("??\n");
180           printf ("??:0\n");
181         }
182       else
183         {
184           if (with_functions)
185             {
186               if (functionname == NULL || *functionname == '\0')
187                 printf ("??\n");
188               else if (! do_demangle)
189                 printf ("%s\n", functionname);
190               else
191                 {
192                   char *res;
193
194                   res = cplus_demangle (functionname, DMGL_ANSI | DMGL_PARAMS);
195                   if (res == NULL)
196                     printf ("%s\n", functionname);
197                   else
198                     {
199                       printf ("%s\n", res);
200                       free (res);
201                     }
202                 }
203             }
204
205           if (base_names && filename != NULL)
206             {
207               char *h;
208
209               h = strrchr (filename, '/');
210               if (h != NULL)
211                 filename = h + 1;
212             }
213
214           printf ("%s:%u\n", filename ? filename : "??", line);
215         }
216
217       /* fflush() is essential for using this command as a server
218          child process that reads addresses from a pipe and responds
219          with line number information, processing one address at a
220          time.  */
221       fflush (stdout);
222     }
223 }
224
225 /* Process a file.  */
226
227 static void
228 process_file (filename, target)
229      const char *filename;
230      const char *target;
231 {
232   bfd *abfd;
233   char **matching;
234
235   abfd = bfd_openr (filename, target);
236   if (abfd == NULL)
237     bfd_fatal (filename);
238
239   if (bfd_check_format (abfd, bfd_archive))
240     fatal (_("%s: can not get addresses from archive"), filename);
241
242   if (! bfd_check_format_matches (abfd, bfd_object, &matching))
243     {
244       bfd_nonfatal (bfd_get_filename (abfd));
245       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
246         {
247           list_matching_formats (matching);
248           free (matching);
249         }
250       xexit (1);
251     }
252
253   slurp_symtab (abfd);
254
255   translate_addresses (abfd);
256
257   if (syms != NULL)
258     {
259       free (syms);
260       syms = NULL;
261     }
262
263   bfd_close (abfd);
264 }
265 \f
266 int main PARAMS ((int, char **));
267
268 int
269 main (argc, argv)
270      int argc;
271      char **argv;
272 {
273   const char *filename;
274   char *target;
275   int c;
276
277 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
278   setlocale (LC_MESSAGES, "");
279 #endif
280 #if defined (HAVE_SETLOCALE)
281   setlocale (LC_CTYPE, "");
282 #endif
283   bindtextdomain (PACKAGE, LOCALEDIR);
284   textdomain (PACKAGE);
285
286   program_name = *argv;
287   xmalloc_set_program_name (program_name);
288
289   bfd_init ();
290   set_default_bfd_target ();
291
292   filename = NULL;
293   target = NULL;
294   while ((c = getopt_long (argc, argv, "b:Ce:sfHV", long_options, (int *) 0))
295          != EOF)
296     {
297       switch (c)
298         {
299         case 0:
300           break;                /* we've been given a long option */
301         case 'b':
302           target = optarg;
303           break;
304         case 'C':
305           do_demangle = true;
306           if (optarg != NULL)
307             {
308               enum demangling_styles style;
309               
310               style = cplus_demangle_name_to_style (optarg);
311               if (style == unknown_demangling) 
312                 fatal (_("unknown demangling style `%s'"),
313                        optarg);
314               
315               cplus_demangle_set_style (style);
316            }
317           break;
318         case 'e':
319           filename = optarg;
320           break;
321         case 's':
322           base_names = true;
323           break;
324         case 'f':
325           with_functions = true;
326           break;
327         case 'V':
328           print_version ("addr2line");
329           break;
330         case 'H':
331           usage (stdout, 0);
332           break;
333         default:
334           usage (stderr, 1);
335           break;
336         }
337     }
338
339   if (filename == NULL)
340     filename = "a.out";
341
342   addr = argv + optind;
343   naddr = argc - optind;
344
345   process_file (filename, target);
346
347   return 0;
348 }