1 /* Test program for CFI handling.
2 Copyright (C) 2009-2010 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
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.
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.
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,
16 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
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>. */
29 #include ELFUTILS_HEADER(dwfl)
33 #include <stdio_ext.h>
41 print_detail (int result, const Dwarf_Op *ops, size_t nops, Dwarf_Addr bias)
44 printf ("indeterminate (%s)\n", dwarf_errmsg (-1));
46 printf ("%s\n", result == 0 ? "same_value" : "undefined");
49 printf ("%s expression:", result == 0 ? "location" : "value");
50 for (size_t i = 0; i < nops; ++i)
52 printf (" %#x", ops[i].atom);
53 if (ops[i].number2 == 0)
55 if (ops[i].atom == DW_OP_addr)
56 printf ("(%#" PRIx64 ")", ops[i].number + bias);
57 else if (ops[i].number != 0)
58 printf ("(%" PRId64 ")", ops[i].number);
61 printf ("(%" PRId64 ",%" PRId64 ")",
62 ops[i].number, ops[i].number2);
75 print_register (void *arg,
80 int bits __attribute__ ((unused)),
81 int type __attribute__ ((unused)))
83 struct stuff *stuff = arg;
85 printf ("\t%s reg%u (%s%s): ", setname, regno, prefix, regname);
90 int result = dwarf_frame_register (stuff->frame, regno, ops_mem, &ops, &nops);
91 print_detail (result, ops, nops, stuff->bias);
97 handle_cfi (Dwfl *dwfl, const char *which, Dwarf_CFI *cfi,
98 GElf_Addr pc, struct stuff *stuff)
100 int result = dwarf_cfi_addrframe (cfi, pc - stuff->bias, &stuff->frame);
103 error (0, 0, "dwarf_cfi_addrframe (%s): %s", which, dwarf_errmsg (-1));
107 Dwarf_Addr start = pc;
110 int ra_regno = dwarf_frame_info (stuff->frame, &start, &end, &signalp);
113 start += stuff->bias;
117 printf ("%s has %#" PRIx64 " => [%#" PRIx64 ", %#" PRIx64 "):\n",
118 which, pc, start, end);
121 printf ("\treturn address register unavailable (%s)\n",
124 printf ("\treturn address in reg%u%s\n",
125 ra_regno, signalp ? " (signal frame)" : "");
129 result = dwarf_frame_cfa (stuff->frame, &cfa_ops, &cfa_nops);
132 print_detail (result, cfa_ops, cfa_nops, stuff->bias);
134 (void) dwfl_module_register_names (dwfl_addrmodule (dwfl, pc),
135 &print_register, stuff);
141 handle_address (GElf_Addr pc, Dwfl *dwfl)
143 Dwfl_Module *mod = dwfl_addrmodule (dwfl, pc);
146 return (handle_cfi (dwfl, ".eh_frame",
147 dwfl_module_eh_cfi (mod, &stuff.bias), pc, &stuff)
148 & handle_cfi (dwfl, ".debug_frame",
149 dwfl_module_dwarf_cfi (mod, &stuff.bias), pc, &stuff));
153 main (int argc, char *argv[])
158 (void) setlocale (LC_ALL, "");
161 (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining, &dwfl);
162 assert (dwfl != NULL);
166 /* Now handle the addresses. In case none are given on the command
167 line, read from stdin. */
168 if (remaining == argc)
170 /* We use no threads here which can interfere with handling a stream. */
171 (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
175 while (!feof_unlocked (stdin))
177 if (getline (&buf, &len, stdin) < 0)
181 uintmax_t addr = strtoumax (buf, &endp, 0);
183 result |= handle_address (addr, dwfl);
195 uintmax_t addr = strtoumax (argv[remaining], &endp, 0);
196 if (endp != argv[remaining])
197 result |= handle_address (addr, dwfl);
201 while (++remaining < argc);