1 /* Test program for libdwfl ... foo
2    Copyright (C) 2007 Red Hat, Inc.
3    This file is part of elfutils.
4 
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    elfutils is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 #include <config.h>
19 #include <assert.h>
20 #include <inttypes.h>
21 #include <sys/types.h>
22 #include <stdio.h>
23 #include <stdio_ext.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <locale.h>
27 #include <argp.h>
28 #include ELFUTILS_HEADER(dwfl)
29 #include <dwarf.h>
30 #include "system.h"
31 
32 static int
handle_address(Dwfl * dwfl,Dwarf_Addr address)33 handle_address (Dwfl *dwfl, Dwarf_Addr address)
34 {
35   Dwfl_Module *mod = dwfl_addrmodule (dwfl, address);
36   Dwarf_Addr adjusted = address;
37   Dwarf_Addr bias;
38   Elf_Scn *scn = dwfl_module_address_section (mod, &adjusted, &bias);
39   if (scn == NULL)
40     {
41       error (0, 0, "%#" PRIx64 ": dwfl_module_address_section: %s",
42 	     address, dwfl_errmsg (-1));
43       return 1;
44     }
45   printf ("address %#" PRIx64 " => module \"%s\" section %zu + %#" PRIx64 "\n",
46 	  address,
47 	  dwfl_module_info (mod, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
48 	  elf_ndxscn (scn), adjusted);
49   return 0;
50 }
51 
52 int
main(int argc,char ** argv)53 main (int argc, char **argv)
54 {
55   /* We use no threads here which can interfere with handling a stream.  */
56   (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
57 
58   /* Set locale.  */
59   (void) setlocale (LC_ALL, "");
60 
61   int remaining;
62   Dwfl *dwfl = NULL;
63   (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining, &dwfl);
64   assert (dwfl != NULL);
65 
66   int result = 0;
67   for (; remaining < argc; ++remaining)
68     {
69       char *endp;
70       uintmax_t addr = strtoumax (argv[remaining], &endp, 0);
71       if (endp != argv[remaining])
72 	result |= handle_address (dwfl, addr);
73       else
74 	result = 1;
75     }
76 
77   dwfl_end (dwfl);
78 
79   return result;
80 }
81