Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* addr2line.c -- convert addresses to line number and function name |
135dfb4a | 2 | Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 |
2da42df6 | 3 | Free Software Foundation, Inc. |
c8c5888e | 4 | Contributed by Ulrich Lauther <Ulrich.Lauther@mchp.siemens.de> |
252b5132 RH |
5 | |
6 | This file is part of GNU Binutils. | |
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 2, or (at your option) | |
11 | 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, write to the Free Software | |
20 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
21 | ||
c8c5888e | 22 | /* Derived from objdump.c and nm.c by Ulrich.Lauther@mchp.siemens.de |
252b5132 | 23 | |
f462a9ea | 24 | Usage: |
252b5132 RH |
25 | addr2line [options] addr addr ... |
26 | or | |
f462a9ea | 27 | addr2line [options] |
252b5132 RH |
28 | |
29 | both forms write results to stdout, the second form reads addresses | |
30 | to be converted from stdin. */ | |
31 | ||
252b5132 RH |
32 | #include <string.h> |
33 | ||
34 | #include "bfd.h" | |
35 | #include "getopt.h" | |
36 | #include "libiberty.h" | |
37 | #include "demangle.h" | |
38 | #include "bucomm.h" | |
a6637ec0 | 39 | #include "budemang.h" |
252b5132 | 40 | |
b34976b6 AM |
41 | static bfd_boolean with_functions; /* -f, show function names. */ |
42 | static bfd_boolean do_demangle; /* -C, demangle names. */ | |
43 | static bfd_boolean base_names; /* -s, strip directory names. */ | |
252b5132 RH |
44 | |
45 | static int naddr; /* Number of addresses to process. */ | |
46 | static char **addr; /* Hex addresses to process. */ | |
47 | ||
48 | static asymbol **syms; /* Symbol table. */ | |
49 | ||
50 | static 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 | ||
2da42df6 AJ |
62 | static void usage (FILE *, int); |
63 | static void slurp_symtab (bfd *); | |
64 | static void find_address_in_section (bfd *, asection *, void *); | |
65 | static void translate_addresses (bfd *); | |
66 | static void process_file (const char *, const char *); | |
252b5132 RH |
67 | \f |
68 | /* Print a usage message to STREAM and exit with STATUS. */ | |
69 | ||
70 | static void | |
2da42df6 | 71 | usage (FILE *stream, int status) |
252b5132 | 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 | ||
94 | static void | |
2da42df6 | 95 | slurp_symtab (bfd *abfd) |
252b5132 | 96 | { |
252b5132 | 97 | long symcount; |
f309035a | 98 | unsigned int size; |
252b5132 RH |
99 | |
100 | if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0) | |
101 | return; | |
102 | ||
2da42df6 | 103 | symcount = bfd_read_minisymbols (abfd, FALSE, (void *) &syms, &size); |
f309035a | 104 | if (symcount == 0) |
2da42df6 | 105 | symcount = bfd_read_minisymbols (abfd, TRUE /* dynamic */, (void *) &syms, &size); |
252b5132 | 106 | |
252b5132 RH |
107 | if (symcount < 0) |
108 | bfd_fatal (bfd_get_filename (abfd)); | |
109 | } | |
110 | \f | |
111 | /* These global variables are used to pass information between | |
112 | translate_addresses and find_address_in_section. */ | |
113 | ||
114 | static bfd_vma pc; | |
115 | static const char *filename; | |
116 | static const char *functionname; | |
117 | static unsigned int line; | |
b34976b6 | 118 | static bfd_boolean found; |
252b5132 RH |
119 | |
120 | /* Look for an address in a section. This is called via | |
121 | bfd_map_over_sections. */ | |
122 | ||
123 | static void | |
2da42df6 AJ |
124 | find_address_in_section (bfd *abfd, asection *section, |
125 | void *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 | ||
135dfb4a | 140 | size = bfd_get_section_size (section); |
252b5132 RH |
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 | ||
151 | static void | |
2da42df6 | 152 | translate_addresses (bfd *abfd) |
252b5132 RH |
153 | { |
154 | int read_stdin = (naddr == 0); | |
155 | ||
156 | for (;;) | |
157 | { | |
158 | if (read_stdin) | |
159 | { | |
160 | char addr_hex[100]; | |
161 | ||
162 | if (fgets (addr_hex, sizeof addr_hex, stdin) == NULL) | |
163 | break; | |
164 | pc = bfd_scan_vma (addr_hex, NULL, 16); | |
165 | } | |
166 | else | |
167 | { | |
168 | if (naddr <= 0) | |
169 | break; | |
170 | --naddr; | |
171 | pc = bfd_scan_vma (*addr++, NULL, 16); | |
172 | } | |
173 | ||
b34976b6 | 174 | found = FALSE; |
2da42df6 | 175 | bfd_map_over_sections (abfd, find_address_in_section, NULL); |
252b5132 RH |
176 | |
177 | if (! found) | |
178 | { | |
179 | if (with_functions) | |
180 | printf ("??\n"); | |
181 | printf ("??:0\n"); | |
182 | } | |
183 | else | |
184 | { | |
185 | if (with_functions) | |
186 | { | |
a6637ec0 AM |
187 | const char *name; |
188 | char *alloc = NULL; | |
189 | ||
190 | name = functionname; | |
191 | if (name == NULL || *name == '\0') | |
192 | name = "??"; | |
193 | else if (do_demangle) | |
252b5132 | 194 | { |
a6637ec0 AM |
195 | alloc = demangle (abfd, name); |
196 | name = alloc; | |
252b5132 | 197 | } |
a6637ec0 AM |
198 | |
199 | printf ("%s\n", name); | |
200 | ||
201 | if (alloc != NULL) | |
202 | free (alloc); | |
252b5132 RH |
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 | |
2da42df6 | 228 | process_file (const char *file_name, const char *target) |
252b5132 RH |
229 | { |
230 | bfd *abfd; | |
231 | char **matching; | |
232 | ||
f24ddbdd NC |
233 | if (get_file_size (file_name) < 1) |
234 | return; | |
235 | ||
47badb7b | 236 | abfd = bfd_openr (file_name, target); |
252b5132 | 237 | if (abfd == NULL) |
47badb7b | 238 | bfd_fatal (file_name); |
252b5132 RH |
239 | |
240 | if (bfd_check_format (abfd, bfd_archive)) | |
47badb7b | 241 | fatal (_("%s: can not get addresses from archive"), file_name); |
252b5132 RH |
242 | |
243 | if (! bfd_check_format_matches (abfd, bfd_object, &matching)) | |
244 | { | |
245 | bfd_nonfatal (bfd_get_filename (abfd)); | |
246 | if (bfd_get_error () == bfd_error_file_ambiguously_recognized) | |
247 | { | |
248 | list_matching_formats (matching); | |
249 | free (matching); | |
250 | } | |
251 | xexit (1); | |
252 | } | |
253 | ||
254 | slurp_symtab (abfd); | |
255 | ||
256 | translate_addresses (abfd); | |
257 | ||
258 | if (syms != NULL) | |
259 | { | |
260 | free (syms); | |
261 | syms = NULL; | |
262 | } | |
263 | ||
264 | bfd_close (abfd); | |
265 | } | |
266 | \f | |
2da42df6 | 267 | int main (int, char **); |
65de42c0 | 268 | |
252b5132 | 269 | int |
2da42df6 | 270 | main (int argc, char **argv) |
252b5132 | 271 | { |
47badb7b | 272 | const char *file_name; |
252b5132 RH |
273 | char *target; |
274 | int c; | |
275 | ||
276 | #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) | |
277 | setlocale (LC_MESSAGES, ""); | |
3882b010 L |
278 | #endif |
279 | #if defined (HAVE_SETLOCALE) | |
280 | setlocale (LC_CTYPE, ""); | |
252b5132 RH |
281 | #endif |
282 | bindtextdomain (PACKAGE, LOCALEDIR); | |
283 | textdomain (PACKAGE); | |
284 | ||
285 | program_name = *argv; | |
286 | xmalloc_set_program_name (program_name); | |
287 | ||
288 | bfd_init (); | |
289 | set_default_bfd_target (); | |
290 | ||
47badb7b | 291 | file_name = NULL; |
252b5132 | 292 | target = NULL; |
8b53311e | 293 | while ((c = getopt_long (argc, argv, "b:Ce:sfHhVv", long_options, (int *) 0)) |
252b5132 RH |
294 | != EOF) |
295 | { | |
296 | switch (c) | |
297 | { | |
298 | case 0: | |
8b53311e | 299 | break; /* We've been given a long option. */ |
252b5132 RH |
300 | case 'b': |
301 | target = optarg; | |
302 | break; | |
303 | case 'C': | |
b34976b6 | 304 | do_demangle = TRUE; |
28c309a2 NC |
305 | if (optarg != NULL) |
306 | { | |
307 | enum demangling_styles style; | |
f462a9ea | 308 | |
28c309a2 | 309 | style = cplus_demangle_name_to_style (optarg); |
f462a9ea | 310 | if (style == unknown_demangling) |
28c309a2 NC |
311 | fatal (_("unknown demangling style `%s'"), |
312 | optarg); | |
f462a9ea | 313 | |
28c309a2 | 314 | cplus_demangle_set_style (style); |
f462a9ea | 315 | } |
252b5132 RH |
316 | break; |
317 | case 'e': | |
47badb7b | 318 | file_name = optarg; |
252b5132 RH |
319 | break; |
320 | case 's': | |
b34976b6 | 321 | base_names = TRUE; |
252b5132 RH |
322 | break; |
323 | case 'f': | |
b34976b6 | 324 | with_functions = TRUE; |
252b5132 | 325 | break; |
8b53311e | 326 | case 'v': |
252b5132 RH |
327 | case 'V': |
328 | print_version ("addr2line"); | |
329 | break; | |
8b53311e | 330 | case 'h': |
252b5132 RH |
331 | case 'H': |
332 | usage (stdout, 0); | |
333 | break; | |
334 | default: | |
335 | usage (stderr, 1); | |
336 | break; | |
337 | } | |
338 | } | |
339 | ||
47badb7b NC |
340 | if (file_name == NULL) |
341 | file_name = "a.out"; | |
252b5132 RH |
342 | |
343 | addr = argv + optind; | |
344 | naddr = argc - optind; | |
345 | ||
47badb7b | 346 | process_file (file_name, target); |
252b5132 RH |
347 | |
348 | return 0; | |
349 | } |