blob: 948e971aa6cec9ba42e67d55ccc89bfc476e9a29 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Test program for libdwfl basic module tracking, relocation.
2 Copyright (C) 2005 Red Hat, Inc.
3
4 This program is Open Source software; you can redistribute it and/or
5 modify it under the terms of the Open Software License version 1.0 as
6 published by the Open Source Initiative.
7
8 You should have received a copy of the Open Software License along
9 with this program; if not, you may obtain a copy of the Open Software
10 License version 1.0 from http://www.opensource.org/licenses/osl.php or
11 by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
12 3001 King Ranch Road, Ukiah, CA 95482. */
13
14#include <config.h>
15#include <assert.h>
16#include <inttypes.h>
17#include <sys/types.h>
18#include <stdio.h>
19#include <stdio_ext.h>
20#include <stdlib.h>
21#include <string.h>
22#include <error.h>
23#include <locale.h>
24#include <argp.h>
25#include <libdwfl.h>
26
27
28static int
29print_func (Dwarf_Func *func, void *arg)
30{
31 const Dwarf_Addr dwbias = *(Dwarf_Addr *) arg;
32
33 const char *file = dwarf_func_file (func);
34 int line = -1;
35 dwarf_func_line (func, &line);
36 const char *fct = dwarf_func_name (func);
37
38 printf (" %s:%d: %s:", file, line, fct);
39
40 Dwarf_Addr lo = -1, hi = -1, entry = -1;
41 if (dwarf_func_lowpc (func, &lo) == 0)
42 lo += dwbias;
43 else
44 printf (" (lowpc => %s)", dwarf_errmsg (-1));
45 if (dwarf_func_highpc (func, &hi) == 0)
46 hi += dwbias;
47 else
48 printf (" (highpc => %s)", dwarf_errmsg (-1));
49 if (dwarf_func_entrypc (func, &entry) == 0)
50 entry += dwbias;
51 else
52 printf (" (entrypc => %s)", dwarf_errmsg (-1));
53
54 if (lo != (Dwarf_Addr) -1 || hi != (Dwarf_Addr) -1
55 || entry != (Dwarf_Addr) -1)
56 printf (" %#" PRIx64 "..%#" PRIx64 " => %#" PRIx64 "\n",
57 lo, hi, entry);
58 else
59 puts ("");
60
61 return DWARF_CB_OK;
62}
63
64static int
65print_module (Dwfl_Module *mod __attribute__ ((unused)),
66 void **userdata __attribute__ ((unused)),
67 const char *name, Dwarf_Addr base,
68 Dwarf *dw, Dwarf_Addr bias,
69 void *arg __attribute__ ((unused)))
70{
71 printf ("module: %30s %08" PRIx64 " %12p %" PRIx64 " (%s)\n",
72 name, base, dw, bias, dwfl_errmsg (-1));
73
74 if (dw != NULL)
75 {
76 Dwarf_Off off = 0;
77 size_t cuhl;
78 Dwarf_Off noff;
79
80 while (dwarf_nextcu (dw, off, &noff, &cuhl, NULL, NULL, NULL) == 0)
81 {
82 Dwarf_Die die_mem;
83 Dwarf_Die *die = dwarf_offdie (dw, off + cuhl, &die_mem);
84
85 (void) dwarf_getfuncs (die, print_func, &bias, 0);
86
87 off = noff;
88 }
89 }
90
91 return DWARF_CB_OK;
92}
93
94int
95main (int argc, char **argv)
96{
97 /* We use no threads here which can interfere with handling a stream. */
98 (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
99
100 /* Set locale. */
101 (void) setlocale (LC_ALL, "");
102
103 Dwfl *dwfl = NULL;
104 (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, NULL, &dwfl);
105 assert (dwfl != NULL);
106
107 ptrdiff_t p = 0;
108 do
109 p = dwfl_getdwarf (dwfl, &print_module, NULL, p);
110 while (p > 0);
111 if (p < 0)
112 error (2, 0, "dwfl_getdwarf: %s", dwfl_errmsg (-1));
113
114 dwfl_end (dwfl);
115
116 return 0;
117}