Locale changes from Bruno Haible <haible@clisp.cons.org>.
[deliverable/binutils-gdb.git] / binutils / addr2line.c
CommitLineData
252b5132 1/* addr2line.c -- convert addresses to line number and function name
3882b010 2 Copyright 1997, 1998, 1999, 2000, 2001 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
39extern char *program_version;
40
41static boolean with_functions; /* -f, show function names. */
42static boolean do_demangle; /* -C, demangle names. */
43static boolean base_names; /* -s, strip directory names. */
44
45static int naddr; /* Number of addresses to process. */
46static char **addr; /* Hex addresses to process. */
47
48static asymbol **syms; /* Symbol table. */
49
50static struct option long_options[] =
51{
52 {"basenames", no_argument, NULL, 's'},
28c309a2 53 {"demangle", optional_argument, NULL, 'C'},
252b5132
RH
54 {"exe", required_argument, NULL, 'e'},
55 {"functions", no_argument, NULL, 'f'},
56 {"target", required_argument, NULL, 'b'},
57 {"help", no_argument, NULL, 'H'},
58 {"version", no_argument, NULL, 'V'},
59 {0, no_argument, 0, 0}
60};
61
62static void usage PARAMS ((FILE *, int));
63static void slurp_symtab PARAMS ((bfd *));
64static void find_address_in_section PARAMS ((bfd *, asection *, PTR));
65static void translate_addresses PARAMS ((bfd *));
66static void process_file PARAMS ((const char *, const char *));
67\f
68/* Print a usage message to STREAM and exit with STATUS. */
69
70static void
71usage (stream, status)
72 FILE *stream;
73 int status;
74{
75 fprintf (stream, _("\
76Usage: %s [-CfsHV] [-b bfdname] [--target=bfdname]\n\
28c309a2 77 [-e executable] [--exe=executable] [--demangle[=style]]\n\
252b5132
RH
78 [--basenames] [--functions] [addr addr ...]\n"),
79 program_name);
80 list_supported_targets (program_name, stream);
81 if (status == 0)
8ad3436c 82 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
252b5132
RH
83 exit (status);
84}
85\f
86/* Read in the symbol table. */
87
88static void
89slurp_symtab (abfd)
90 bfd *abfd;
91{
92 long storage;
93 long symcount;
94
95 if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0)
96 return;
97
98 storage = bfd_get_symtab_upper_bound (abfd);
99 if (storage < 0)
100 bfd_fatal (bfd_get_filename (abfd));
101
102 syms = (asymbol **) xmalloc (storage);
103
104 symcount = bfd_canonicalize_symtab (abfd, syms);
105 if (symcount < 0)
106 bfd_fatal (bfd_get_filename (abfd));
107}
108\f
109/* These global variables are used to pass information between
110 translate_addresses and find_address_in_section. */
111
112static bfd_vma pc;
113static const char *filename;
114static const char *functionname;
115static unsigned int line;
116static boolean found;
117
118/* Look for an address in a section. This is called via
119 bfd_map_over_sections. */
120
121static void
122find_address_in_section (abfd, section, data)
123 bfd *abfd;
124 asection *section;
b4c96d0d 125 PTR data ATTRIBUTE_UNUSED;
252b5132
RH
126{
127 bfd_vma vma;
128 bfd_size_type size;
129
130 if (found)
131 return;
132
133 if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
134 return;
135
136 vma = bfd_get_section_vma (abfd, section);
137 if (pc < vma)
138 return;
139
140 size = bfd_get_section_size_before_reloc (section);
141 if (pc >= vma + size)
142 return;
143
144 found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
145 &filename, &functionname, &line);
146}
147
148/* Read hexadecimal addresses from stdin, translate into
149 file_name:line_number and optionally function name. */
150
151static void
152translate_addresses (abfd)
153 bfd *abfd;
154{
155 int read_stdin = (naddr == 0);
156
157 for (;;)
158 {
159 if (read_stdin)
160 {
161 char addr_hex[100];
162
163 if (fgets (addr_hex, sizeof addr_hex, stdin) == NULL)
164 break;
165 pc = bfd_scan_vma (addr_hex, NULL, 16);
166 }
167 else
168 {
169 if (naddr <= 0)
170 break;
171 --naddr;
172 pc = bfd_scan_vma (*addr++, NULL, 16);
173 }
174
175 found = false;
176 bfd_map_over_sections (abfd, find_address_in_section, (PTR) NULL);
177
178 if (! found)
179 {
180 if (with_functions)
181 printf ("??\n");
182 printf ("??:0\n");
183 }
184 else
185 {
186 if (with_functions)
187 {
188 if (functionname == NULL || *functionname == '\0')
189 printf ("??\n");
190 else if (! do_demangle)
191 printf ("%s\n", functionname);
192 else
193 {
194 char *res;
195
196 res = cplus_demangle (functionname, DMGL_ANSI | DMGL_PARAMS);
197 if (res == NULL)
198 printf ("%s\n", functionname);
199 else
200 {
201 printf ("%s\n", res);
202 free (res);
203 }
204 }
205 }
206
207 if (base_names && filename != NULL)
208 {
209 char *h;
210
211 h = strrchr (filename, '/');
212 if (h != NULL)
213 filename = h + 1;
214 }
215
216 printf ("%s:%u\n", filename ? filename : "??", line);
217 }
218
219 /* fflush() is essential for using this command as a server
220 child process that reads addresses from a pipe and responds
221 with line number information, processing one address at a
222 time. */
223 fflush (stdout);
224 }
225}
226
227/* Process a file. */
228
229static void
230process_file (filename, target)
231 const char *filename;
232 const char *target;
233{
234 bfd *abfd;
235 char **matching;
236
237 abfd = bfd_openr (filename, target);
238 if (abfd == NULL)
239 bfd_fatal (filename);
240
241 if (bfd_check_format (abfd, bfd_archive))
242 fatal (_("%s: can not get addresses from archive"), filename);
243
244 if (! bfd_check_format_matches (abfd, bfd_object, &matching))
245 {
246 bfd_nonfatal (bfd_get_filename (abfd));
247 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
248 {
249 list_matching_formats (matching);
250 free (matching);
251 }
252 xexit (1);
253 }
254
255 slurp_symtab (abfd);
256
257 translate_addresses (abfd);
258
259 if (syms != NULL)
260 {
261 free (syms);
262 syms = NULL;
263 }
264
265 bfd_close (abfd);
266}
267\f
268int
269main (argc, argv)
270 int argc;
271 char **argv;
272{
4047915b 273 const char *filename;
252b5132
RH
274 char *target;
275 int c;
276
277#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
278 setlocale (LC_MESSAGES, "");
3882b010
L
279#endif
280#if defined (HAVE_SETLOCALE)
281 setlocale (LC_CTYPE, "");
252b5132
RH
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;
28c309a2
NC
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 }
252b5132
RH
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}
This page took 0.089367 seconds and 4 git commands to generate.