blob: e0d65d3dc75e560a9257e09140e675dbd0d95943 [file] [log] [blame]
Roland McGrathd7f8d0c2005-11-17 02:32:03 +00001/* Copyright (C) 2005 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02002 This file is part of elfutils.
Roland McGrathd7f8d0c2005-11-17 02:32:03 +00003
Mark Wielaardde2ed972012-06-05 17:15:16 +02004 This file is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
Roland McGrathd7f8d0c2005-11-17 02:32:03 +00008
Mark Wielaardde2ed972012-06-05 17:15:16 +02009 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper361df7d2006-04-04 21:38:57 +000010 WITHOUT ANY WARRANTY; without even the implied warranty of
Mark Wielaardde2ed972012-06-05 17:15:16 +020011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000013
Mark Wielaardde2ed972012-06-05 17:15:16 +020014 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
Roland McGrathd7f8d0c2005-11-17 02:32:03 +000016
17#ifdef HAVE_CONFIG_H
18# include <config.h>
19#endif
20
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000021#include <inttypes.h>
Roland McGrathd17fac72005-08-23 08:20:21 +000022#include <assert.h>
Roland McGrathd7f8d0c2005-11-17 02:32:03 +000023#include ELFUTILS_HEADER(dwfl)
Roland McGrathd17fac72005-08-23 08:20:21 +000024#include <argp.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000025#include <stdio.h>
Roland McGrathd17fac72005-08-23 08:20:21 +000026#include <locale.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000027#include <stdlib.h>
Roland McGrathd17fac72005-08-23 08:20:21 +000028#include <string.h>
29#include <error.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000030
31
Roland McGrathd17fac72005-08-23 08:20:21 +000032static void
33print_address (Dwfl_Module *mod, Dwarf_Addr address)
34{
35 int n = dwfl_module_relocations (mod);
36 if (n < 0)
37 error (0, 0, "dwfl_module_relocations: %s", dwfl_errmsg (-1));
38 else if (n > 0)
39 {
40 int i = dwfl_module_relocate_address (mod, &address);
41 if (i < 0)
42 error (0, 0, "dwfl_module_relocate_address: %s", dwfl_errmsg (-1));
43 else
44 {
45 const char *modname = dwfl_module_info (mod, NULL, NULL, NULL,
46 NULL, NULL, NULL, NULL);
47 const char *secname = dwfl_module_relocation_info (mod, i, NULL);
48 if (n > 1 || secname[0] != '\0')
49 printf ("%s(%s)+%#" PRIx64, modname, secname, address);
50 else
Roland McGrath4c305da2005-08-25 01:49:35 +000051 printf ("%s+%#" PRIx64, modname, address);
Roland McGrathd17fac72005-08-23 08:20:21 +000052 return;
53 }
54 }
55
56 printf ("%#" PRIx64, address);
57}
58
59
60struct args
61{
62 const char *arg;
63 char *file;
64 int line;
65};
66
67static int
68handle_module (Dwfl_Module *mod __attribute__ ((unused)),
69 void **udata __attribute__ ((unused)),
70 const char *modname, Dwarf_Addr base __attribute__ ((unused)),
71 Dwarf *dbg __attribute__ ((unused)),
72 Dwarf_Addr bias __attribute__ ((unused)), void *arg)
73{
74 const struct args *const a = arg;
75
76 Dwfl_Line **lines = NULL;
77 size_t nlines = 0;
78
79 if (dwfl_module_getsrc_file (mod, a->file, a->line, 0, &lines, &nlines) == 0)
80 {
81 for (size_t inner = 0; inner < nlines; ++inner)
82 {
83 Dwarf_Addr addr;
84 int line = a->line, col = 0;
85 const char *file = dwfl_lineinfo (lines[inner], &addr, &line, &col,
86 NULL, NULL);
87 if (file != NULL)
88 {
89 printf ("%s -> ", a->arg);
90 print_address (mod, addr);
91 if (modname[0] != '\0')
92 printf (" (%s:", modname);
93 if (strcmp (file, a->file) || line != a->line || col != 0)
94 printf (" %s%s:%d", modname[0] != '\0' ? "" : "(",
95 file, line);
96 if (col != 0)
Ulrich Drepper3840c1c2005-11-09 16:13:48 +000097 printf (":%d", col);
Roland McGrathd17fac72005-08-23 08:20:21 +000098 if (modname[0] != '\0'
99 || strcmp (file, a->file) || line != a->line || col != 0)
100 puts (")");
101 else
102 puts ("");
103 }
104 }
105 free (lines);
106 }
107
108 return DWARF_CB_OK;
109}
110
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000111int
112main (int argc, char *argv[])
113{
Roland McGrathd17fac72005-08-23 08:20:21 +0000114 int cnt;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000115
Roland McGrathd17fac72005-08-23 08:20:21 +0000116 /* Set locale. */
117 (void) setlocale (LC_ALL, "");
118
119 Dwfl *dwfl = NULL;
120 (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &cnt, &dwfl);
121 assert (dwfl != NULL);
122
123 for (; cnt < argc; ++cnt)
124 {
125 struct args a = { .arg = argv[cnt] };
126
Ulrich Drepperf231e272008-01-02 18:07:17 +0000127 switch (sscanf (a.arg, "%m[^:]:%d", &a.file, &a.line))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000128 {
129 default:
130 case 0:
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000131 printf ("ignored %s\n", argv[cnt]);
132 continue;
Roland McGrathd17fac72005-08-23 08:20:21 +0000133 case 1:
134 a.line = 0;
135 break;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000136 case 2:
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000137 break;
138 }
139
Roland McGrathd17fac72005-08-23 08:20:21 +0000140 (void) dwfl_getdwarf (dwfl, &handle_module, &a, 0);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000141
Roland McGrathd17fac72005-08-23 08:20:21 +0000142 free (a.file);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000143 }
144
Roland McGrath994b4892005-12-05 22:46:21 +0000145 dwfl_end (dwfl);
146
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000147 return 0;
148}