blob: 7678884b92504fca9fc3c5d11269ec621e721964 [file] [log] [blame]
Roland McGrath3c84db32009-06-24 17:41:40 -07001/* Test program for CFI handling.
Mark Wielaard292b1782013-08-27 15:40:56 +02002 Copyright (C) 2009-2010, 2013 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Roland McGrath3c84db32009-06-24 17:41:40 -07004
Mark Wielaardde2ed972012-06-05 17:15:16 +02005 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.
Roland McGrath3c84db32009-06-24 17:41:40 -07009
Mark Wielaardde2ed972012-06-05 17:15:16 +020010 elfutils is distributed in the hope that it will be useful, but
Roland McGrath3c84db32009-06-24 17:41:40 -070011 WITHOUT ANY WARRANTY; without even the implied warranty of
Mark Wielaardde2ed972012-06-05 17:15:16 +020012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
Roland McGrath3c84db32009-06-24 17:41:40 -070014
Mark Wielaardde2ed972012-06-05 17:15:16 +020015 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/>. */
Roland McGrath3c84db32009-06-24 17:41:40 -070017
18#include <config.h>
19#include <assert.h>
20#include <inttypes.h>
21#include ELFUTILS_HEADER(dwfl)
22#include <dwarf.h>
23#include <argp.h>
24#include <stdio.h>
25#include <stdio_ext.h>
26#include <locale.h>
27#include <stdlib.h>
28#include <error.h>
29#include <string.h>
30
Mark Wielaard292b1782013-08-27 15:40:56 +020031#include "../libdw/known-dwarf.h"
32
33static const char *
34op_name (unsigned int code)
35{
36 static const char *const known[] =
37 {
38#define ONE_KNOWN_DW_OP_DESC(NAME, CODE, DESC) ONE_KNOWN_DW_OP (NAME, CODE)
39#define ONE_KNOWN_DW_OP(NAME, CODE) [CODE] = #NAME,
40 ALL_KNOWN_DW_OP
41#undef ONE_KNOWN_DW_OP
42#undef ONE_KNOWN_DW_OP_DESC
43 };
44
45 if (likely (code < sizeof (known) / sizeof (known[0])))
46 return known[code];
47
48 return NULL;
49}
Roland McGrath3c84db32009-06-24 17:41:40 -070050
51static void
52print_detail (int result, const Dwarf_Op *ops, size_t nops, Dwarf_Addr bias)
53{
54 if (result < 0)
55 printf ("indeterminate (%s)\n", dwarf_errmsg (-1));
56 else if (nops == 0)
Mark Wielaard292b1782013-08-27 15:40:56 +020057 printf ("%s\n", ops == NULL ? "same_value" : "undefined");
Roland McGrath3c84db32009-06-24 17:41:40 -070058 else
59 {
60 printf ("%s expression:", result == 0 ? "location" : "value");
61 for (size_t i = 0; i < nops; ++i)
62 {
Mark Wielaard292b1782013-08-27 15:40:56 +020063 printf (" %s", op_name(ops[i].atom));
Roland McGrath3c84db32009-06-24 17:41:40 -070064 if (ops[i].number2 == 0)
65 {
66 if (ops[i].atom == DW_OP_addr)
67 printf ("(%#" PRIx64 ")", ops[i].number + bias);
68 else if (ops[i].number != 0)
69 printf ("(%" PRId64 ")", ops[i].number);
70 }
71 else
72 printf ("(%" PRId64 ",%" PRId64 ")",
73 ops[i].number, ops[i].number2);
74 }
75 puts ("");
76 }
77}
78
79struct stuff
80{
81 Dwarf_Frame *frame;
82 Dwarf_Addr bias;
83};
84
85static int
86print_register (void *arg,
87 int regno,
88 const char *setname,
89 const char *prefix,
90 const char *regname,
91 int bits __attribute__ ((unused)),
92 int type __attribute__ ((unused)))
93{
94 struct stuff *stuff = arg;
95
96 printf ("\t%s reg%u (%s%s): ", setname, regno, prefix, regname);
97
98 Dwarf_Op ops_mem[2];
99 Dwarf_Op *ops;
100 size_t nops;
101 int result = dwarf_frame_register (stuff->frame, regno, ops_mem, &ops, &nops);
102 print_detail (result, ops, nops, stuff->bias);
103
104 return DWARF_CB_OK;
105}
106
107static int
108handle_cfi (Dwfl *dwfl, const char *which, Dwarf_CFI *cfi,
109 GElf_Addr pc, struct stuff *stuff)
110{
111 int result = dwarf_cfi_addrframe (cfi, pc - stuff->bias, &stuff->frame);
112 if (result != 0)
113 {
Roland McGrath3c6ee4b2010-04-22 22:38:58 -0700114 error (0, 0, "dwarf_cfi_addrframe (%s): %s", which, dwarf_errmsg (-1));
Roland McGrath3c84db32009-06-24 17:41:40 -0700115 return 1;
116 }
117
118 Dwarf_Addr start = pc;
119 Dwarf_Addr end = pc;
120 bool signalp;
121 int ra_regno = dwarf_frame_info (stuff->frame, &start, &end, &signalp);
122 if (ra_regno >= 0)
123 {
124 start += stuff->bias;
125 end += stuff->bias;
126 }
127
128 printf ("%s has %#" PRIx64 " => [%#" PRIx64 ", %#" PRIx64 "):\n",
129 which, pc, start, end);
130
131 if (ra_regno < 0)
132 printf ("\treturn address register unavailable (%s)\n",
133 dwarf_errmsg (0));
134 else
135 printf ("\treturn address in reg%u%s\n",
136 ra_regno, signalp ? " (signal frame)" : "");
137
Mark Wielaard292b1782013-08-27 15:40:56 +0200138 // Point cfa_ops to dummy to match print_detail expectations.
139 // (nops == 0 && cfa_ops != NULL => "undefined")
140 Dwarf_Op dummy;
141 Dwarf_Op *cfa_ops = &dummy;
Roland McGrathaf800142009-07-22 13:55:50 -0700142 size_t cfa_nops;
143 result = dwarf_frame_cfa (stuff->frame, &cfa_ops, &cfa_nops);
Roland McGrath3c84db32009-06-24 17:41:40 -0700144
145 printf ("\tCFA ");
Roland McGrathaf800142009-07-22 13:55:50 -0700146 print_detail (result, cfa_ops, cfa_nops, stuff->bias);
Roland McGrath3c84db32009-06-24 17:41:40 -0700147
148 (void) dwfl_module_register_names (dwfl_addrmodule (dwfl, pc),
149 &print_register, stuff);
150
151 return 0;
152}
153
154static int
155handle_address (GElf_Addr pc, Dwfl *dwfl)
156{
157 Dwfl_Module *mod = dwfl_addrmodule (dwfl, pc);
158
159 struct stuff stuff;
160 return (handle_cfi (dwfl, ".eh_frame",
161 dwfl_module_eh_cfi (mod, &stuff.bias), pc, &stuff)
162 & handle_cfi (dwfl, ".debug_frame",
163 dwfl_module_dwarf_cfi (mod, &stuff.bias), pc, &stuff));
164}
165
166int
167main (int argc, char *argv[])
168{
169 int remaining;
170
171 /* Set locale. */
172 (void) setlocale (LC_ALL, "");
173
174 Dwfl *dwfl = NULL;
175 (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining, &dwfl);
176 assert (dwfl != NULL);
177
178 int result = 0;
179
180 /* Now handle the addresses. In case none are given on the command
181 line, read from stdin. */
182 if (remaining == argc)
183 {
184 /* We use no threads here which can interfere with handling a stream. */
185 (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
186
187 char *buf = NULL;
188 size_t len = 0;
189 while (!feof_unlocked (stdin))
190 {
191 if (getline (&buf, &len, stdin) < 0)
192 break;
193
194 char *endp;
195 uintmax_t addr = strtoumax (buf, &endp, 0);
196 if (endp != buf)
197 result |= handle_address (addr, dwfl);
198 else
199 result = 1;
200 }
201
202 free (buf);
203 }
204 else
205 {
206 do
207 {
208 char *endp;
209 uintmax_t addr = strtoumax (argv[remaining], &endp, 0);
210 if (endp != argv[remaining])
211 result |= handle_address (addr, dwfl);
212 else
213 result = 1;
214 }
215 while (++remaining < argc);
216 }
217
218 dwfl_end (dwfl);
219
220 return result;
221}