Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Test program for libdwfl basic module tracking, relocation. |
| 2 | Copyright (C) 2005 Red Hat, Inc. |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 3 | This file is part of Red Hat elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 5 | Red Hat elfutils is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by the |
| 7 | Free Software Foundation; version 2 of the License. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 8 | |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 9 | Red Hat elfutils is distributed in the hope that it will be useful, but |
| 10 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License along |
| 15 | with Red Hat elfutils; if not, write to the Free Software Foundation, |
Ulrich Drepper | 1e9ef50 | 2006-04-04 22:29:06 +0000 | [diff] [blame] | 16 | Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 17 | |
| 18 | Red Hat elfutils is an included package of the Open Invention Network. |
| 19 | An included package of the Open Invention Network is a package for which |
| 20 | Open Invention Network licensees cross-license their patents. No patent |
| 21 | license is granted, either expressly or impliedly, by designation as an |
| 22 | included package. Should you wish to participate in the Open Invention |
| 23 | Network licensing program, please visit www.openinventionnetwork.com |
| 24 | <http://www.openinventionnetwork.com>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 25 | |
| 26 | #include <config.h> |
| 27 | #include <assert.h> |
| 28 | #include <inttypes.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdio_ext.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <error.h> |
| 35 | #include <locale.h> |
| 36 | #include <argp.h> |
Roland McGrath | d7f8d0c | 2005-11-17 02:32:03 +0000 | [diff] [blame] | 37 | #include ELFUTILS_HEADER(dwfl) |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 38 | #include <dwarf.h> |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 39 | |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 40 | static bool show_inlines; |
| 41 | |
| 42 | struct info |
| 43 | { |
| 44 | Dwarf_Die *cudie; |
| 45 | Dwarf_Addr dwbias; |
| 46 | }; |
| 47 | |
| 48 | static int |
| 49 | print_instance (Dwarf_Die *instance, void *arg) |
| 50 | { |
| 51 | const struct info *info = arg; |
| 52 | |
| 53 | printf (" inlined"); |
| 54 | |
| 55 | Dwarf_Files *files; |
| 56 | if (dwarf_getsrcfiles (info->cudie, &files, NULL) == 0) |
| 57 | { |
| 58 | Dwarf_Attribute attr_mem; |
| 59 | Dwarf_Word val; |
| 60 | if (dwarf_formudata (dwarf_attr (instance, DW_AT_call_file, |
| 61 | &attr_mem), &val) == 0) |
| 62 | { |
| 63 | const char *file = dwarf_filesrc (files, val, NULL, NULL); |
| 64 | int lineno = 0, colno = 0; |
| 65 | if (dwarf_formudata (dwarf_attr (instance, DW_AT_call_line, |
| 66 | &attr_mem), &val) == 0) |
| 67 | lineno = val; |
| 68 | if (dwarf_formudata (dwarf_attr (instance, DW_AT_call_column, |
| 69 | &attr_mem), &val) == 0) |
| 70 | colno = val; |
| 71 | if (lineno == 0) |
| 72 | { |
| 73 | if (file != NULL) |
| 74 | printf (" from %s", file); |
| 75 | } |
| 76 | else if (colno == 0) |
| 77 | printf (" at %s:%u", file, lineno); |
| 78 | else |
| 79 | printf (" at %s:%u:%u", file, lineno, colno); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | Dwarf_Addr lo = -1, hi = -1, entry = -1; |
| 84 | if (dwarf_lowpc (instance, &lo) == 0) |
| 85 | lo += info->dwbias; |
| 86 | else |
| 87 | printf (" (lowpc => %s)", dwarf_errmsg (-1)); |
| 88 | if (dwarf_highpc (instance, &hi) == 0) |
| 89 | hi += info->dwbias; |
| 90 | else |
| 91 | printf (" (highpc => %s)", dwarf_errmsg (-1)); |
| 92 | |
| 93 | Dwarf_Attribute attr_mem; |
Roland McGrath | d7f8d0c | 2005-11-17 02:32:03 +0000 | [diff] [blame] | 94 | Dwarf_Attribute *attr = dwarf_attr (instance, DW_AT_entry_pc, &attr_mem); |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 95 | if (attr != NULL) |
| 96 | { |
Roland McGrath | d7f8d0c | 2005-11-17 02:32:03 +0000 | [diff] [blame] | 97 | if (dwarf_formaddr (attr, &entry) == 0) |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 98 | entry += info->dwbias; |
| 99 | else |
| 100 | printf (" (entrypc => %s)", dwarf_errmsg (-1)); |
| 101 | } |
| 102 | |
| 103 | if (lo != (Dwarf_Addr) -1 || hi != (Dwarf_Addr) -1) |
| 104 | printf (" %#" PRIx64 "..%#" PRIx64, lo, hi); |
| 105 | if (entry != (Dwarf_Addr) -1) |
| 106 | printf (" => %#" PRIx64 "\n", entry); |
| 107 | else |
| 108 | puts (""); |
| 109 | |
| 110 | return DWARF_CB_OK; |
| 111 | } |
| 112 | |
| 113 | static void |
Roland McGrath | 6724c90 | 2005-10-28 07:07:19 +0000 | [diff] [blame] | 114 | print_inline (Dwarf_Die *func, void *arg) |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 115 | { |
| 116 | if (dwarf_func_inline_instances (func, &print_instance, arg) != 0) |
| 117 | printf (" error finding instances: %s\n", dwarf_errmsg (-1)); |
| 118 | } |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 119 | |
| 120 | static int |
Roland McGrath | 6724c90 | 2005-10-28 07:07:19 +0000 | [diff] [blame] | 121 | print_func (Dwarf_Die *func, void *arg) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 122 | { |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 123 | const struct info *info = arg; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 124 | |
Roland McGrath | 6724c90 | 2005-10-28 07:07:19 +0000 | [diff] [blame] | 125 | const char *file = dwarf_decl_file (func); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 126 | int line = -1; |
Roland McGrath | 6724c90 | 2005-10-28 07:07:19 +0000 | [diff] [blame] | 127 | dwarf_decl_line (func, &line); |
| 128 | const char *fct = dwarf_diename (func); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 129 | |
| 130 | printf (" %s:%d: %s:", file, line, fct); |
| 131 | |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 132 | if (dwarf_func_inline (func)) |
| 133 | { |
| 134 | puts (" inline function"); |
| 135 | if (show_inlines) |
| 136 | print_inline (func, arg); |
| 137 | } |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 138 | else |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 139 | { |
| 140 | Dwarf_Addr lo = -1, hi = -1, entry = -1; |
Roland McGrath | 6724c90 | 2005-10-28 07:07:19 +0000 | [diff] [blame] | 141 | if (dwarf_lowpc (func, &lo) == 0) |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 142 | lo += info->dwbias; |
| 143 | else |
| 144 | printf (" (lowpc => %s)", dwarf_errmsg (-1)); |
Roland McGrath | 6724c90 | 2005-10-28 07:07:19 +0000 | [diff] [blame] | 145 | if (dwarf_highpc (func, &hi) == 0) |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 146 | hi += info->dwbias; |
| 147 | else |
| 148 | printf (" (highpc => %s)", dwarf_errmsg (-1)); |
Roland McGrath | 6724c90 | 2005-10-28 07:07:19 +0000 | [diff] [blame] | 149 | if (dwarf_entrypc (func, &entry) == 0) |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 150 | entry += info->dwbias; |
| 151 | else |
| 152 | printf (" (entrypc => %s)", dwarf_errmsg (-1)); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 153 | |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 154 | if (lo != (Dwarf_Addr) -1 || hi != (Dwarf_Addr) -1 |
| 155 | || entry != (Dwarf_Addr) -1) |
| 156 | printf (" %#" PRIx64 "..%#" PRIx64 " => %#" PRIx64 "\n", |
| 157 | lo, hi, entry); |
| 158 | else |
| 159 | puts (""); |
| 160 | } |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 161 | |
| 162 | return DWARF_CB_OK; |
| 163 | } |
| 164 | |
| 165 | static int |
| 166 | print_module (Dwfl_Module *mod __attribute__ ((unused)), |
| 167 | void **userdata __attribute__ ((unused)), |
| 168 | const char *name, Dwarf_Addr base, |
| 169 | Dwarf *dw, Dwarf_Addr bias, |
Roland McGrath | 8d18c29 | 2005-08-07 21:42:07 +0000 | [diff] [blame] | 170 | void *arg) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 171 | { |
| 172 | printf ("module: %30s %08" PRIx64 " %12p %" PRIx64 " (%s)\n", |
| 173 | name, base, dw, bias, dwfl_errmsg (-1)); |
| 174 | |
Roland McGrath | 8d18c29 | 2005-08-07 21:42:07 +0000 | [diff] [blame] | 175 | if (dw != NULL && *(const bool *) arg) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 176 | { |
| 177 | Dwarf_Off off = 0; |
| 178 | size_t cuhl; |
| 179 | Dwarf_Off noff; |
| 180 | |
| 181 | while (dwarf_nextcu (dw, off, &noff, &cuhl, NULL, NULL, NULL) == 0) |
| 182 | { |
| 183 | Dwarf_Die die_mem; |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 184 | struct info info = { dwarf_offdie (dw, off + cuhl, &die_mem), bias }; |
| 185 | (void) dwarf_getfuncs (info.cudie, print_func, &info, 0); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 186 | |
| 187 | off = noff; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return DWARF_CB_OK; |
| 192 | } |
| 193 | |
Roland McGrath | 8d18c29 | 2005-08-07 21:42:07 +0000 | [diff] [blame] | 194 | static bool show_functions; |
| 195 | |
Roland McGrath | d7f8d0c | 2005-11-17 02:32:03 +0000 | [diff] [blame] | 196 | /* gettext helper macro. */ |
| 197 | #undef N_ |
| 198 | #define N_(Str) Str |
| 199 | |
Roland McGrath | 8d18c29 | 2005-08-07 21:42:07 +0000 | [diff] [blame] | 200 | static const struct argp_option options[] = |
| 201 | { |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 202 | { "functions", 'f', NULL, 0, N_("Additionally show function names"), 0 }, |
| 203 | { "inlines", 'i', NULL, 0, N_("Show instances of inlined functions"), 0 }, |
Roland McGrath | 8d18c29 | 2005-08-07 21:42:07 +0000 | [diff] [blame] | 204 | { NULL, 0, NULL, 0, NULL, 0 } |
| 205 | }; |
| 206 | |
| 207 | static error_t |
| 208 | parse_opt (int key, char *arg __attribute__ ((unused)), |
| 209 | struct argp_state *state __attribute__ ((unused))) |
| 210 | { |
| 211 | switch (key) |
| 212 | { |
| 213 | case ARGP_KEY_INIT: |
| 214 | state->child_inputs[0] = state->input; |
| 215 | break; |
| 216 | |
| 217 | case 'f': |
| 218 | show_functions = true; |
| 219 | break; |
| 220 | |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 221 | case 'i': |
| 222 | show_inlines = show_functions = true; |
| 223 | break; |
| 224 | |
Roland McGrath | 8d18c29 | 2005-08-07 21:42:07 +0000 | [diff] [blame] | 225 | default: |
| 226 | return ARGP_ERR_UNKNOWN; |
| 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 231 | int |
| 232 | main (int argc, char **argv) |
| 233 | { |
| 234 | /* We use no threads here which can interfere with handling a stream. */ |
| 235 | (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER); |
| 236 | |
| 237 | /* Set locale. */ |
| 238 | (void) setlocale (LC_ALL, ""); |
| 239 | |
| 240 | Dwfl *dwfl = NULL; |
Roland McGrath | 8d18c29 | 2005-08-07 21:42:07 +0000 | [diff] [blame] | 241 | const struct argp_child argp_children[] = |
| 242 | { |
| 243 | { .argp = dwfl_standard_argp () }, |
| 244 | { .argp = NULL } |
| 245 | }; |
| 246 | const struct argp argp = |
| 247 | { |
| 248 | options, parse_opt, NULL, NULL, argp_children, NULL, NULL |
| 249 | }; |
| 250 | (void) argp_parse (&argp, argc, argv, 0, NULL, &dwfl); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 251 | assert (dwfl != NULL); |
| 252 | |
| 253 | ptrdiff_t p = 0; |
| 254 | do |
Roland McGrath | 8d18c29 | 2005-08-07 21:42:07 +0000 | [diff] [blame] | 255 | p = dwfl_getdwarf (dwfl, &print_module, &show_functions, p); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 256 | while (p > 0); |
| 257 | if (p < 0) |
| 258 | error (2, 0, "dwfl_getdwarf: %s", dwfl_errmsg (-1)); |
| 259 | |
| 260 | dwfl_end (dwfl); |
| 261 | |
| 262 | return 0; |
| 263 | } |