2002-05-09 Elena Zannoni <ezannoni@redhat.com>
[deliverable/binutils-gdb.git] / binutils / addr2line.c
CommitLineData
252b5132 1/* addr2line.c -- convert addresses to line number and function name
8b53311e 2 Copyright 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
c8c5888e 3 Contributed by Ulrich Lauther <Ulrich.Lauther@mchp.siemens.de>
252b5132
RH
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
c8c5888e 21/* Derived from objdump.c and nm.c by Ulrich.Lauther@mchp.siemens.de
252b5132
RH
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
252b5132
RH
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
252b5132
RH
39static boolean with_functions; /* -f, show function names. */
40static boolean do_demangle; /* -C, demangle names. */
41static boolean base_names; /* -s, strip directory names. */
42
43static int naddr; /* Number of addresses to process. */
44static char **addr; /* Hex addresses to process. */
45
46static asymbol **syms; /* Symbol table. */
47
48static struct option long_options[] =
49{
50 {"basenames", no_argument, NULL, 's'},
28c309a2 51 {"demangle", optional_argument, NULL, 'C'},
252b5132
RH
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
60static void usage PARAMS ((FILE *, int));
61static void slurp_symtab PARAMS ((bfd *));
62static void find_address_in_section PARAMS ((bfd *, asection *, PTR));
63static void translate_addresses PARAMS ((bfd *));
64static void process_file PARAMS ((const char *, const char *));
65\f
66/* Print a usage message to STREAM and exit with STATUS. */
67
68static void
69usage (stream, status)
70 FILE *stream;
71 int status;
72{
8b53311e
NC
73 fprintf (stream, _("Usage: %s [option(s)] [addr(s)]\n"), program_name);
74 fprintf (stream, _(" Convert addresses into line number/file name pairs.\n"));
75 fprintf (stream, _(" If no addresses are specified on the command line, they will be read from stdin\n"));
76 fprintf (stream, _(" The options are:\n\
77 -b --target=<bfdname> Set the binary file format\n\
78 -e --exe=<executable> Set the input file name (default is a.out)\n\
79 -s --basenames Strip directory names\n\
80 -f --functions Show function names\n\
81 -C --demangle[=style] Demangle function names\n\
82 -h --help Display this information\n\
83 -v --version Display the program's version\n\
84\n"));
85
252b5132
RH
86 list_supported_targets (program_name, stream);
87 if (status == 0)
8ad3436c 88 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
252b5132
RH
89 exit (status);
90}
91\f
92/* Read in the symbol table. */
93
94static void
95slurp_symtab (abfd)
96 bfd *abfd;
97{
98 long storage;
99 long symcount;
100
101 if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0)
102 return;
103
104 storage = bfd_get_symtab_upper_bound (abfd);
105 if (storage < 0)
106 bfd_fatal (bfd_get_filename (abfd));
107
108 syms = (asymbol **) xmalloc (storage);
109
110 symcount = bfd_canonicalize_symtab (abfd, syms);
111 if (symcount < 0)
112 bfd_fatal (bfd_get_filename (abfd));
113}
114\f
115/* These global variables are used to pass information between
116 translate_addresses and find_address_in_section. */
117
118static bfd_vma pc;
119static const char *filename;
120static const char *functionname;
121static unsigned int line;
122static boolean found;
123
124/* Look for an address in a section. This is called via
125 bfd_map_over_sections. */
126
127static void
128find_address_in_section (abfd, section, data)
129 bfd *abfd;
130 asection *section;
b4c96d0d 131 PTR data ATTRIBUTE_UNUSED;
252b5132
RH
132{
133 bfd_vma vma;
134 bfd_size_type size;
135
136 if (found)
137 return;
138
139 if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
140 return;
141
142 vma = bfd_get_section_vma (abfd, section);
143 if (pc < vma)
144 return;
145
146 size = bfd_get_section_size_before_reloc (section);
147 if (pc >= vma + size)
148 return;
149
150 found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
151 &filename, &functionname, &line);
152}
153
154/* Read hexadecimal addresses from stdin, translate into
155 file_name:line_number and optionally function name. */
156
157static void
158translate_addresses (abfd)
159 bfd *abfd;
160{
161 int read_stdin = (naddr == 0);
162
163 for (;;)
164 {
165 if (read_stdin)
166 {
167 char addr_hex[100];
168
169 if (fgets (addr_hex, sizeof addr_hex, stdin) == NULL)
170 break;
171 pc = bfd_scan_vma (addr_hex, NULL, 16);
172 }
173 else
174 {
175 if (naddr <= 0)
176 break;
177 --naddr;
178 pc = bfd_scan_vma (*addr++, NULL, 16);
179 }
180
181 found = false;
182 bfd_map_over_sections (abfd, find_address_in_section, (PTR) NULL);
183
184 if (! found)
185 {
186 if (with_functions)
187 printf ("??\n");
188 printf ("??:0\n");
189 }
190 else
191 {
192 if (with_functions)
193 {
194 if (functionname == NULL || *functionname == '\0')
195 printf ("??\n");
196 else if (! do_demangle)
197 printf ("%s\n", functionname);
198 else
199 {
200 char *res;
201
202 res = cplus_demangle (functionname, DMGL_ANSI | DMGL_PARAMS);
203 if (res == NULL)
204 printf ("%s\n", functionname);
205 else
206 {
207 printf ("%s\n", res);
208 free (res);
209 }
210 }
211 }
212
213 if (base_names && filename != NULL)
214 {
215 char *h;
216
217 h = strrchr (filename, '/');
218 if (h != NULL)
219 filename = h + 1;
220 }
221
222 printf ("%s:%u\n", filename ? filename : "??", line);
223 }
224
225 /* fflush() is essential for using this command as a server
226 child process that reads addresses from a pipe and responds
227 with line number information, processing one address at a
228 time. */
229 fflush (stdout);
230 }
231}
232
233/* Process a file. */
234
235static void
236process_file (filename, target)
237 const char *filename;
238 const char *target;
239{
240 bfd *abfd;
241 char **matching;
242
243 abfd = bfd_openr (filename, target);
244 if (abfd == NULL)
245 bfd_fatal (filename);
246
247 if (bfd_check_format (abfd, bfd_archive))
248 fatal (_("%s: can not get addresses from archive"), filename);
249
250 if (! bfd_check_format_matches (abfd, bfd_object, &matching))
251 {
252 bfd_nonfatal (bfd_get_filename (abfd));
253 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
254 {
255 list_matching_formats (matching);
256 free (matching);
257 }
258 xexit (1);
259 }
260
261 slurp_symtab (abfd);
262
263 translate_addresses (abfd);
264
265 if (syms != NULL)
266 {
267 free (syms);
268 syms = NULL;
269 }
270
271 bfd_close (abfd);
272}
273\f
65de42c0
TS
274int main PARAMS ((int, char **));
275
252b5132
RH
276int
277main (argc, argv)
278 int argc;
279 char **argv;
280{
4047915b 281 const char *filename;
252b5132
RH
282 char *target;
283 int c;
284
285#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
286 setlocale (LC_MESSAGES, "");
3882b010
L
287#endif
288#if defined (HAVE_SETLOCALE)
289 setlocale (LC_CTYPE, "");
252b5132
RH
290#endif
291 bindtextdomain (PACKAGE, LOCALEDIR);
292 textdomain (PACKAGE);
293
294 program_name = *argv;
295 xmalloc_set_program_name (program_name);
296
297 bfd_init ();
298 set_default_bfd_target ();
299
300 filename = NULL;
301 target = NULL;
8b53311e 302 while ((c = getopt_long (argc, argv, "b:Ce:sfHhVv", long_options, (int *) 0))
252b5132
RH
303 != EOF)
304 {
305 switch (c)
306 {
307 case 0:
8b53311e 308 break; /* We've been given a long option. */
252b5132
RH
309 case 'b':
310 target = optarg;
311 break;
312 case 'C':
313 do_demangle = true;
28c309a2
NC
314 if (optarg != NULL)
315 {
316 enum demangling_styles style;
317
318 style = cplus_demangle_name_to_style (optarg);
319 if (style == unknown_demangling)
320 fatal (_("unknown demangling style `%s'"),
321 optarg);
322
323 cplus_demangle_set_style (style);
324 }
252b5132
RH
325 break;
326 case 'e':
327 filename = optarg;
328 break;
329 case 's':
330 base_names = true;
331 break;
332 case 'f':
333 with_functions = true;
334 break;
8b53311e 335 case 'v':
252b5132
RH
336 case 'V':
337 print_version ("addr2line");
338 break;
8b53311e 339 case 'h':
252b5132
RH
340 case 'H':
341 usage (stdout, 0);
342 break;
343 default:
344 usage (stderr, 1);
345 break;
346 }
347 }
348
349 if (filename == NULL)
350 filename = "a.out";
351
352 addr = argv + optind;
353 naddr = argc - optind;
354
355 process_file (filename, target);
356
357 return 0;
358}
This page took 0.125654 seconds and 4 git commands to generate.