]>
Commit | Line | Data |
---|---|---|
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 <[email protected]> |
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 [email protected] |
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 |
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'}, | |
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 | ||
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\ | |
28c309a2 | 75 | [-e executable] [--exe=executable] [--demangle[=style]]\n\ |
252b5132 RH |
76 | [--basenames] [--functions] [addr addr ...]\n"), |
77 | program_name); | |
78 | list_supported_targets (program_name, stream); | |
79 | if (status == 0) | |
8ad3436c | 80 | fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO); |
252b5132 RH |
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; | |
b4c96d0d | 123 | PTR data ATTRIBUTE_UNUSED; |
252b5132 RH |
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 | |
267 | main (argc, argv) | |
268 | int argc; | |
269 | char **argv; | |
270 | { | |
4047915b | 271 | const char *filename; |
252b5132 RH |
272 | char *target; |
273 | int c; | |
274 | ||
275 | #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) | |
276 | setlocale (LC_MESSAGES, ""); | |
3882b010 L |
277 | #endif |
278 | #if defined (HAVE_SETLOCALE) | |
279 | setlocale (LC_CTYPE, ""); | |
252b5132 RH |
280 | #endif |
281 | bindtextdomain (PACKAGE, LOCALEDIR); | |
282 | textdomain (PACKAGE); | |
283 | ||
284 | program_name = *argv; | |
285 | xmalloc_set_program_name (program_name); | |
286 | ||
287 | bfd_init (); | |
288 | set_default_bfd_target (); | |
289 | ||
290 | filename = NULL; | |
291 | target = NULL; | |
292 | while ((c = getopt_long (argc, argv, "b:Ce:sfHV", long_options, (int *) 0)) | |
293 | != EOF) | |
294 | { | |
295 | switch (c) | |
296 | { | |
297 | case 0: | |
298 | break; /* we've been given a long option */ | |
299 | case 'b': | |
300 | target = optarg; | |
301 | break; | |
302 | case 'C': | |
303 | do_demangle = true; | |
28c309a2 NC |
304 | if (optarg != NULL) |
305 | { | |
306 | enum demangling_styles style; | |
307 | ||
308 | style = cplus_demangle_name_to_style (optarg); | |
309 | if (style == unknown_demangling) | |
310 | fatal (_("unknown demangling style `%s'"), | |
311 | optarg); | |
312 | ||
313 | cplus_demangle_set_style (style); | |
314 | } | |
252b5132 RH |
315 | break; |
316 | case 'e': | |
317 | filename = optarg; | |
318 | break; | |
319 | case 's': | |
320 | base_names = true; | |
321 | break; | |
322 | case 'f': | |
323 | with_functions = true; | |
324 | break; | |
325 | case 'V': | |
326 | print_version ("addr2line"); | |
327 | break; | |
328 | case 'H': | |
329 | usage (stdout, 0); | |
330 | break; | |
331 | default: | |
332 | usage (stderr, 1); | |
333 | break; | |
334 | } | |
335 | } | |
336 | ||
337 | if (filename == NULL) | |
338 | filename = "a.out"; | |
339 | ||
340 | addr = argv + optind; | |
341 | naddr = argc - optind; | |
342 | ||
343 | process_file (filename, target); | |
344 | ||
345 | return 0; | |
346 | } |