1 /* Test program for CFI handling.
2 Copyright (C) 2009-2010, 2013, 2015 Red Hat, Inc.
3 This file is part of elfutils.
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.
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.
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/>. */
21 #include ELFUTILS_HEADER(dwfl)
25 #include <stdio_ext.h>
30 #include "../libdw/known-dwarf.h"
33 op_name (unsigned int code)
35 static const char *const known[] =
37 #define DWARF_ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
39 #undef DWARF_ONE_KNOWN_DW_OP
42 if (likely (code < sizeof (known) / sizeof (known[0])))
49 print_detail (int result, const Dwarf_Op *ops, size_t nops, Dwarf_Addr bias)
52 printf ("indeterminate (%s)\n", dwarf_errmsg (-1));
54 printf ("%s\n", ops == NULL ? "same_value" : "undefined");
57 printf ("%s expression:", result == 0 ? "location" : "value");
58 for (size_t i = 0; i < nops; ++i)
60 printf (" %s", op_name(ops[i].atom));
61 if (ops[i].number2 == 0)
63 if (ops[i].atom == DW_OP_addr)
64 printf ("(%#" PRIx64 ")", ops[i].number + bias);
65 else if (ops[i].number != 0)
66 printf ("(%" PRId64 ")", ops[i].number);
69 printf ("(%" PRId64 ",%" PRId64 ")",
70 ops[i].number, ops[i].number2);
83 print_register (void *arg,
88 int bits __attribute__ ((unused)),
89 int type __attribute__ ((unused)))
91 struct stuff *stuff = arg;
93 printf ("\t%s reg%u (%s%s): ", setname, regno, prefix, regname);
98 int result = dwarf_frame_register (stuff->frame, regno, ops_mem, &ops, &nops);
99 print_detail (result, ops, nops, stuff->bias);
105 handle_cfi (Dwfl *dwfl, const char *which, Dwarf_CFI *cfi,
106 GElf_Addr pc, struct stuff *stuff)
110 printf ("handle_cfi no CFI (%s): %s\n", which, dwarf_errmsg (-1));
114 int result = dwarf_cfi_addrframe (cfi, pc - stuff->bias, &stuff->frame);
117 printf ("dwarf_cfi_addrframe (%s): %s\n", which, dwarf_errmsg (-1));
121 Dwarf_Addr start = pc;
124 int ra_regno = dwarf_frame_info (stuff->frame, &start, &end, &signalp);
127 start += stuff->bias;
131 printf ("%s has %#" PRIx64 " => [%#" PRIx64 ", %#" PRIx64 "):\n",
132 which, pc, start, end);
135 printf ("\treturn address register unavailable (%s)\n",
138 printf ("\treturn address in reg%u%s\n",
139 ra_regno, signalp ? " (signal frame)" : "");
141 // Point cfa_ops to dummy to match print_detail expectations.
142 // (nops == 0 && cfa_ops != NULL => "undefined")
144 Dwarf_Op *cfa_ops = &dummy;
146 result = dwarf_frame_cfa (stuff->frame, &cfa_ops, &cfa_nops);
149 print_detail (result, cfa_ops, cfa_nops, stuff->bias);
151 (void) dwfl_module_register_names (dwfl_addrmodule (dwfl, pc),
152 &print_register, stuff);
158 handle_address (GElf_Addr pc, Dwfl *dwfl)
160 Dwfl_Module *mod = dwfl_addrmodule (dwfl, pc);
165 int res = handle_cfi (dwfl, ".eh_frame",
166 dwfl_module_eh_cfi (mod, &stuff.bias), pc, &stuff);
171 res &= handle_cfi (dwfl, ".debug_frame",
172 dwfl_module_dwarf_cfi (mod, &stuff.bias), pc, &stuff);
179 main (int argc, char *argv[])
184 (void) setlocale (LC_ALL, "");
187 (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining, &dwfl);
188 assert (dwfl != NULL);
192 /* Now handle the addresses. In case none are given on the command
193 line, read from stdin. */
194 if (remaining == argc)
196 /* We use no threads here which can interfere with handling a stream. */
197 (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
201 while (!feof_unlocked (stdin))
203 if (getline (&buf, &len, stdin) < 0)
207 uintmax_t addr = strtoumax (buf, &endp, 0);
209 result |= handle_address (addr, dwfl);
221 uintmax_t addr = strtoumax (argv[remaining], &endp, 0);
222 if (endp != argv[remaining])
223 result |= handle_address (addr, dwfl);
227 while (++remaining < argc);