| 1 |
/* |
| 2 |
Copyright (c) 2009-2021 David Anderson. All rights reserved. |
| 3 |
This example code is hereby placed in the Public Domain |
| 4 |
for anyone to use for any purpose. |
| 5 |
*/ |
| 6 |
|
| 7 |
/* frame1.c |
| 8 |
It specifically calls dwarf_expand_frame_instructions() |
| 9 |
to verify that works without crashing! |
| 10 |
|
| 11 |
To use, try |
| 12 |
make |
| 13 |
./frame1 frame1 |
| 14 |
|
| 15 |
This uses public structs from libdwarf.h so it is |
| 16 |
stuck with limitations for compatibility. |
| 17 |
|
| 18 |
There needs to be a more flexible alternative, one |
| 19 |
can be updated when DWARF changes. |
| 20 |
|
| 21 |
gcc/clang may produce .eh_frame without .debug_frame. |
| 22 |
To read .eh_frame call dwarf_get_fde_list_eh() |
| 23 |
below instead of dwarf_get_fde_list() . |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <config.h> |
| 27 |
|
| 28 |
#include <stddef.h> /* NULL */ |
| 29 |
#include <stdio.h> /* printf() snprintf() */ |
| 30 |
#include <stdlib.h> /* exit() free() malloc() */ |
| 31 |
#include <string.h> /* strcat() strcmp() strcpy() strncmp() */ |
| 32 |
|
| 33 |
#ifdef _WIN32 |
| 34 |
#include <io.h> /* close() open() */ |
| 35 |
#elif defined HAVE_UNISTD_H |
| 36 |
#include <unistd.h> /* close() */ |
| 37 |
#endif /* _WIN32 */ |
| 38 |
|
| 39 |
#ifdef HAVE_FCNTL_H |
| 40 |
#include <fcntl.h> /* open() O_RDONLY */ |
| 41 |
#endif /* HAVE_FCNTL_H */ |
| 42 |
|
| 43 |
#include "dwarf.h" |
| 44 |
#include "libdwarf.h" |
| 45 |
#include "libdwarf_private.h" |
| 46 |
|
| 47 |
#ifdef _O_BINARY |
| 48 |
/* This is for a Windows environment */ |
| 49 |
#define O_BINARY _O_BINARY |
| 50 |
#else |
| 51 |
# ifndef O_BINARY |
| 52 |
# define O_BINARY 0 /* So it does nothing in Linux/Unix */ |
| 53 |
# endif |
| 54 |
#endif /* O_BINARY */ |
| 55 |
|
| 56 |
static void read_frame_data(Dwarf_Debug dbg,const char *sec); |
| 57 |
static void print_fde_instrs(Dwarf_Debug dbg, Dwarf_Fde fde, |
| 58 |
Dwarf_Error *error); |
| 59 |
static void print_regtable(Dwarf_Regtable3 *tab3); |
| 60 |
static void print_cie_instrs(Dwarf_Cie cie,Dwarf_Error *error); |
| 61 |
static void print_fde_selected_regs( Dwarf_Fde fde); |
| 62 |
static void print_reg(int r); |
| 63 |
|
| 64 |
static int just_print_selected_regs = 0; |
| 65 |
|
| 66 |
/* Depending on the ABI we set INITIAL_VAL |
| 67 |
differently. For ia64 initial value is |
| 68 |
UNDEF_VAL, for MIPS and others initial |
| 69 |
value is SAME_VAL. |
| 70 |
Here we'll set it UNDEF_VAL |
| 71 |
as that way we'll see when first set. */ |
| 72 |
#define UNDEF_VAL 2000 |
| 73 |
#define SAME_VAL 2001 |
| 74 |
#define CFA_VAL 2002 |
| 75 |
#define INITIAL_VAL UNDEF_VAL |
| 76 |
|
| 77 |
/* Dumping a dwarf-expression as a byte stream. */ |
| 78 |
static void |
| 79 |
dump_block(char *prefix, Dwarf_Small *data, Dwarf_Unsigned len) |
| 80 |
{ |
| 81 |
Dwarf_Small *end_data = data + len; |
| 82 |
Dwarf_Small *cur = data; |
| 83 |
int i = 0; |
| 84 |
|
| 85 |
printf("%s", prefix); |
| 86 |
for (; cur < end_data; ++cur, ++i) { |
| 87 |
if (i > 0 && i % 4 == 0) |
| 88 |
printf(" "); |
| 89 |
printf("%02x", 0xff & *cur); |
| 90 |
|
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/* Don't really want getopt here. yet. |
| 95 |
so hand do options. */ |
| 96 |
|
| 97 |
int |
| 98 |
main(int argc, char **argv) |
| 99 |
{ |
| 100 |
|
| 101 |
Dwarf_Debug dbg = 0; |
| 102 |
int fd = -1; |
| 103 |
const char *filepath = "<stdin>"; |
| 104 |
int res = DW_DLV_ERROR; |
| 105 |
Dwarf_Error error; |
| 106 |
Dwarf_Handler errhand = 0; |
| 107 |
Dwarf_Ptr errarg = 0; |
| 108 |
int regtabrulecount = 0; |
| 109 |
int curopt = 0; |
| 110 |
|
| 111 |
for (curopt = 1;curopt < argc; ++curopt) { |
| 112 |
if (strncmp(argv[curopt],"--",2)) { |
| 113 |
break; |
| 114 |
} |
| 115 |
if (!strcmp(argv[curopt],"--just-print-selected-regs")) { |
| 116 |
just_print_selected_regs++; |
| 117 |
continue; |
| 118 |
} |
| 119 |
if (!strcmp(argv[curopt],"--suppress-de-alloc-tree")) { |
| 120 |
/* Do nothing, ignore the argument */ |
| 121 |
continue; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if (curopt == argc ) { |
| 126 |
fd = 0; /* stdin */ |
| 127 |
} else if (curopt == (argc-1)) { |
| 128 |
filepath = argv[curopt]; |
| 129 |
fd = open(filepath,O_RDONLY|O_BINARY); |
| 130 |
if (fd < 0) { |
| 131 |
printf("Unable to open %s, giving up.\n",filepath); |
| 132 |
exit(EXIT_FAILURE); |
| 133 |
} |
| 134 |
} else { |
| 135 |
printf("Too many args, giving up. \n"); |
| 136 |
exit(EXIT_FAILURE); |
| 137 |
} |
| 138 |
res = dwarf_init_b(fd,DW_GROUPNUMBER_ANY, |
| 139 |
errhand,errarg, &dbg,&error); |
| 140 |
if (res != DW_DLV_OK) { |
| 141 |
printf("Giving up, dwarf_init failed, " |
| 142 |
"cannot do DWARF processing\n"); |
| 143 |
if (res == DW_DLV_ERROR) { |
| 144 |
printf("Error code %s\n",dwarf_errmsg(error)); |
| 145 |
} |
| 146 |
dwarf_dealloc_error(dbg,error); |
| 147 |
exit(EXIT_FAILURE); |
| 148 |
} |
| 149 |
/* Do this setting after init before any real operations. |
| 150 |
These return the old values, but here we do not |
| 151 |
need to know the old values. The sizes and |
| 152 |
values here are higher than most ABIs and entirely |
| 153 |
arbitrary. |
| 154 |
|
| 155 |
The setting of initial_value to |
| 156 |
the same as undefined-value (the other possible choice being |
| 157 |
same-value) is arbitrary, different ABIs do differ, and |
| 158 |
you have to know which is right. |
| 159 |
|
| 160 |
In dwarfdump we get the SAME_VAL, UNDEF_VAL, |
| 161 |
INITIAL_VAL CFA_VAL from dwconf_s struct. */ |
| 162 |
regtabrulecount=1999; |
| 163 |
dwarf_set_frame_undefined_value(dbg, UNDEF_VAL); |
| 164 |
dwarf_set_frame_rule_initial_value(dbg, INITIAL_VAL); |
| 165 |
dwarf_set_frame_same_value(dbg,SAME_VAL); |
| 166 |
dwarf_set_frame_cfa_value(dbg,CFA_VAL); |
| 167 |
dwarf_set_frame_rule_table_size(dbg,regtabrulecount); |
| 168 |
|
| 169 |
read_frame_data(dbg,".debug_frame"); |
| 170 |
read_frame_data(dbg,".eh_frame"); |
| 171 |
res = dwarf_finish(dbg); |
| 172 |
if (res != DW_DLV_OK) { |
| 173 |
printf("dwarf_finish failed!\n"); |
| 174 |
} |
| 175 |
close(fd); |
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
static void |
| 180 |
read_frame_data(Dwarf_Debug dbg,const char *sect) |
| 181 |
{ |
| 182 |
Dwarf_Error error; |
| 183 |
Dwarf_Signed cie_element_count = 0; |
| 184 |
Dwarf_Signed fde_element_count = 0; |
| 185 |
Dwarf_Cie *cie_data = 0; |
| 186 |
Dwarf_Fde *fde_data = 0; |
| 187 |
int res = DW_DLV_ERROR; |
| 188 |
Dwarf_Signed fdenum = 0; |
| 189 |
|
| 190 |
/* If you wish to read .eh_frame data, use |
| 191 |
dwarf_get_fde_list_eh() instead. |
| 192 |
Get debug_frame with dwarf_get_fde_list. */ |
| 193 |
printf(" Print %s\n",sect); |
| 194 |
if (!strcmp(sect,".eh_frame")) |
| 195 |
{ |
| 196 |
res = dwarf_get_fde_list_eh(dbg,&cie_data,&cie_element_count, |
| 197 |
&fde_data,&fde_element_count,&error); |
| 198 |
} else { |
| 199 |
res = dwarf_get_fde_list(dbg,&cie_data,&cie_element_count, |
| 200 |
&fde_data,&fde_element_count,&error); |
| 201 |
} |
| 202 |
if (res == DW_DLV_NO_ENTRY) { |
| 203 |
printf("No %s data present\n",sect); |
| 204 |
return; |
| 205 |
} |
| 206 |
if ( res == DW_DLV_ERROR) { |
| 207 |
printf("Error reading frame data "); |
| 208 |
exit(EXIT_FAILURE); |
| 209 |
} |
| 210 |
printf( "%" DW_PR_DSd " cies present. " |
| 211 |
"%" DW_PR_DSd " fdes present. \n", |
| 212 |
cie_element_count,fde_element_count); |
| 213 |
/*if (fdenum >= fde_element_count) { |
| 214 |
printf("Want fde %d but only %" DW_PR_DSd " present\n",fdenum, |
| 215 |
fde_element_count); |
| 216 |
exit(EXIT_FAILURE); |
| 217 |
}*/ |
| 218 |
|
| 219 |
for (fdenum = 0; fdenum < fde_element_count; ++fdenum) { |
| 220 |
Dwarf_Cie cie = 0; |
| 221 |
|
| 222 |
res = dwarf_get_cie_of_fde(fde_data[fdenum],&cie,&error); |
| 223 |
if (res != DW_DLV_OK) { |
| 224 |
printf("Error accessing cie of fdenum %" DW_PR_DSd |
| 225 |
" to get its cie\n",fdenum); |
| 226 |
exit(EXIT_FAILURE); |
| 227 |
} |
| 228 |
printf("Print cie of fde %" DW_PR_DSd "\n",fdenum); |
| 229 |
print_cie_instrs(cie,&error); |
| 230 |
printf("Print fde %" DW_PR_DSd "\n",fdenum); |
| 231 |
if (just_print_selected_regs) { |
| 232 |
print_fde_selected_regs(fde_data[fdenum]); |
| 233 |
} else { |
| 234 |
print_fde_instrs(dbg,fde_data[fdenum],&error); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/* Done with the data. */ |
| 239 |
dwarf_dealloc_fde_cie_list(dbg,cie_data,cie_element_count, |
| 240 |
fde_data, fde_element_count); |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
/* Simply shows the instructions at hand for this fde. */ |
| 245 |
static void |
| 246 |
print_cie_instrs(Dwarf_Cie cie,Dwarf_Error *error) |
| 247 |
{ |
| 248 |
int res = DW_DLV_ERROR; |
| 249 |
Dwarf_Unsigned bytes_in_cie = 0; |
| 250 |
Dwarf_Small version = 0; |
| 251 |
char *augmentation = 0; |
| 252 |
Dwarf_Unsigned code_alignment_factor = 0; |
| 253 |
Dwarf_Signed data_alignment_factor = 0; |
| 254 |
Dwarf_Half return_address_register_rule = 0; |
| 255 |
Dwarf_Small *instrp = 0; |
| 256 |
Dwarf_Unsigned instr_len = 0; |
| 257 |
Dwarf_Half offset_size = 0; |
| 258 |
|
| 259 |
res = dwarf_get_cie_info_b(cie,&bytes_in_cie, |
| 260 |
&version, &augmentation, &code_alignment_factor, |
| 261 |
&data_alignment_factor, &return_address_register_rule, |
| 262 |
&instrp,&instr_len,&offset_size,error); |
| 263 |
if (res != DW_DLV_OK) { |
| 264 |
printf("Unable to get cie info!\n"); |
| 265 |
exit(EXIT_FAILURE); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
static void |
| 270 |
print_fde_col(Dwarf_Signed k, |
| 271 |
Dwarf_Addr jsave, |
| 272 |
Dwarf_Small value_type, |
| 273 |
Dwarf_Signed offset_relevant, |
| 274 |
Dwarf_Signed reg_used, |
| 275 |
Dwarf_Signed offset, |
| 276 |
Dwarf_Block *block, |
| 277 |
Dwarf_Addr row_pc, |
| 278 |
Dwarf_Bool has_more_rows, |
| 279 |
Dwarf_Addr subsequent_pc) |
| 280 |
{ |
| 281 |
char *type_title = ""; |
| 282 |
Dwarf_Unsigned rule_id = k; |
| 283 |
|
| 284 |
printf(" pc=0x%" DW_PR_DUx ,jsave); |
| 285 |
if (row_pc != jsave) { |
| 286 |
printf(" row_pc=0x%" DW_PR_DUx ,row_pc); |
| 287 |
} |
| 288 |
printf(" col=%" DW_PR_DSd " ",k); |
| 289 |
switch(value_type) { |
| 290 |
case DW_EXPR_OFFSET: |
| 291 |
type_title = "off"; |
| 292 |
goto preg2; |
| 293 |
case DW_EXPR_VAL_OFFSET: |
| 294 |
type_title = "valoff"; |
| 295 |
|
| 296 |
preg2: |
| 297 |
printf("<%s ", type_title); |
| 298 |
if (reg_used == SAME_VAL) { |
| 299 |
printf(" SAME_VAL"); |
| 300 |
break; |
| 301 |
} else if (reg_used == INITIAL_VAL) { |
| 302 |
printf(" INITIAL_VAL"); |
| 303 |
break; |
| 304 |
} |
| 305 |
print_reg(rule_id); |
| 306 |
|
| 307 |
printf("="); |
| 308 |
if (offset_relevant == 0) { |
| 309 |
print_reg(reg_used); |
| 310 |
printf(" "); |
| 311 |
} else { |
| 312 |
printf("%02" DW_PR_DSd , offset); |
| 313 |
printf("("); |
| 314 |
print_reg(reg_used); |
| 315 |
printf(") "); |
| 316 |
} |
| 317 |
break; |
| 318 |
case DW_EXPR_EXPRESSION: |
| 319 |
type_title = "expr"; |
| 320 |
goto pexp2; |
| 321 |
case DW_EXPR_VAL_EXPRESSION: |
| 322 |
type_title = "valexpr"; |
| 323 |
|
| 324 |
pexp2: |
| 325 |
printf("<%s ", type_title); |
| 326 |
print_reg(rule_id); |
| 327 |
printf("="); |
| 328 |
printf("expr-block-len=%" DW_PR_DUu , block->bl_len); |
| 329 |
{ |
| 330 |
char pref[40]; |
| 331 |
|
| 332 |
strcpy(pref, "<"); |
| 333 |
strcat(pref, type_title); |
| 334 |
strcat(pref, "bytes:"); |
| 335 |
/* The data being dumped comes direct from |
| 336 |
libdwarf so libdwarf validated it. */ |
| 337 |
dump_block(pref, block->bl_data, block->bl_len); |
| 338 |
printf("%s", "> \n"); |
| 339 |
#if 0 |
| 340 |
if (glflags.verbose) { |
| 341 |
struct esb_s exprstring; |
| 342 |
esb_constructor(&exprstring); |
| 343 |
get_string_from_locs(dbg, |
| 344 |
block_ptr,offset,addr_size, |
| 345 |
offset_size,version,&exprstring); |
| 346 |
printf("<expr:%s>",esb_get_string(&exprstring)); |
| 347 |
esb_destructor(&exprstring); |
| 348 |
} |
| 349 |
#endif |
| 350 |
} |
| 351 |
break; |
| 352 |
default: |
| 353 |
printf("Internal error in libdwarf, value type %d\n", |
| 354 |
value_type); |
| 355 |
exit(EXIT_FAILURE); |
| 356 |
} |
| 357 |
printf(" more=%d",has_more_rows); |
| 358 |
printf(" next=0x%" DW_PR_DUx,subsequent_pc); |
| 359 |
printf("%s", "> "); |
| 360 |
printf("\n"); |
| 361 |
} |
| 362 |
|
| 363 |
/* In dwarfdump we use |
| 364 |
dwarf_get_fde_info_for_cfa_reg3_b() to get subsequent pc |
| 365 |
and avoid incrementing pc by for the next cfa, |
| 366 |
using has_more_rows and subsequent_pc passed back. |
| 367 |
|
| 368 |
Here, to verify function added in May 2018, |
| 369 |
we instead use dwarf_get_fde_info_for_reg3_b() |
| 370 |
which has the has_more_rows and subsequent_pc functions |
| 371 |
for the case where one is tracking a particular register |
| 372 |
and not closely watching the CFA value itself. */ |
| 373 |
|
| 374 |
static const Dwarf_Block dwblockzero; |
| 375 |
static void |
| 376 |
print_fde_selected_regs( Dwarf_Fde fde) |
| 377 |
{ |
| 378 |
Dwarf_Error oneferr = 0; |
| 379 |
/* Arbitrary column numbers for testing. */ |
| 380 |
static int selected_cols[] = {1,3,5}; |
| 381 |
static int selected_cols_count = |
| 382 |
sizeof(selected_cols)/sizeof(selected_cols[0]); |
| 383 |
Dwarf_Signed k = 0; |
| 384 |
int fres = 0; |
| 385 |
|
| 386 |
Dwarf_Addr low_pc = 0; |
| 387 |
Dwarf_Unsigned func_length = 0; |
| 388 |
Dwarf_Small *fde_bytes = NULL; |
| 389 |
Dwarf_Unsigned fde_bytes_length = 0; |
| 390 |
Dwarf_Off cie_offset = 0; |
| 391 |
Dwarf_Signed cie_index = 0; |
| 392 |
Dwarf_Off fde_offset = 0; |
| 393 |
Dwarf_Fde curfde = fde; |
| 394 |
Dwarf_Cie cie = 0; |
| 395 |
Dwarf_Addr jsave = 0; |
| 396 |
Dwarf_Addr high_addr = 0; |
| 397 |
Dwarf_Addr next_jsave = 0; |
| 398 |
Dwarf_Bool has_more_rows = FALSE; |
| 399 |
Dwarf_Addr subsequent_pc = 0; |
| 400 |
Dwarf_Error error = 0; |
| 401 |
int res = 0; |
| 402 |
|
| 403 |
fres = dwarf_get_fde_range(curfde, |
| 404 |
&low_pc, &func_length, |
| 405 |
&fde_bytes, |
| 406 |
&fde_bytes_length, |
| 407 |
&cie_offset, &cie_index, |
| 408 |
&fde_offset, &oneferr); |
| 409 |
|
| 410 |
if (fres == DW_DLV_ERROR) { |
| 411 |
printf("FAIL: dwarf_get_fde_range err %" DW_PR_DUu |
| 412 |
" line %d\n", |
| 413 |
dwarf_errno(oneferr),__LINE__); |
| 414 |
exit(EXIT_FAILURE); |
| 415 |
} |
| 416 |
if (fres == DW_DLV_NO_ENTRY) { |
| 417 |
printf("No fde range data available\n"); |
| 418 |
return; |
| 419 |
} |
| 420 |
res = dwarf_get_cie_of_fde(fde,&cie,&error); |
| 421 |
if (res != DW_DLV_OK) { |
| 422 |
printf("Error getting cie from fde\n"); |
| 423 |
exit(EXIT_FAILURE); |
| 424 |
} |
| 425 |
|
| 426 |
high_addr = low_pc + func_length; |
| 427 |
/* Could check has_more_rows here instead of high_addr, |
| 428 |
If we initialized has_more_rows to 1 above. */ |
| 429 |
for (jsave = low_pc ; next_jsave < high_addr; |
| 430 |
jsave = next_jsave) { |
| 431 |
next_jsave = jsave+1; |
| 432 |
printf("\n"); |
| 433 |
for (k = 0; k < selected_cols_count ; ++k ) { |
| 434 |
Dwarf_Unsigned reg = 0; |
| 435 |
Dwarf_Unsigned offset_relevant = 0; |
| 436 |
int fires = 0; |
| 437 |
Dwarf_Small value_type = 0; |
| 438 |
Dwarf_Block block; |
| 439 |
Dwarf_Unsigned offset; |
| 440 |
Dwarf_Addr row_pc = 0; |
| 441 |
|
| 442 |
block = dwblockzero; |
| 443 |
fires = dwarf_get_fde_info_for_reg3_b(curfde, |
| 444 |
selected_cols[k], |
| 445 |
jsave, |
| 446 |
&value_type, |
| 447 |
&offset_relevant, |
| 448 |
®, |
| 449 |
&offset, |
| 450 |
&block, |
| 451 |
&row_pc, |
| 452 |
&has_more_rows, |
| 453 |
&subsequent_pc, |
| 454 |
&oneferr); |
| 455 |
if (fires == DW_DLV_ERROR) { |
| 456 |
printf("FAIL: reading reg err %" DW_PR_DUu " line %d", |
| 457 |
dwarf_errno(oneferr),__LINE__); |
| 458 |
exit(EXIT_FAILURE); |
| 459 |
} |
| 460 |
if (fires == DW_DLV_NO_ENTRY) { |
| 461 |
continue; |
| 462 |
} |
| 463 |
print_fde_col( |
| 464 |
selected_cols[k],jsave, |
| 465 |
value_type,offset_relevant, |
| 466 |
reg,offset,&block,row_pc, |
| 467 |
has_more_rows, subsequent_pc); |
| 468 |
if (has_more_rows) { |
| 469 |
next_jsave = subsequent_pc; |
| 470 |
} else { |
| 471 |
next_jsave = high_addr; |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
static int |
| 478 |
print_frame_instrs(Dwarf_Debug dbg, |
| 479 |
Dwarf_Frame_Instr_Head frame_instr_head, |
| 480 |
Dwarf_Unsigned frame_instr_count, |
| 481 |
Dwarf_Error *error) |
| 482 |
{ |
| 483 |
Dwarf_Unsigned i = 0; |
| 484 |
|
| 485 |
printf("\nPrint %" DW_PR_DUu " frame instructions\n", |
| 486 |
frame_instr_count); |
| 487 |
for ( ; i < frame_instr_count; ++i) { |
| 488 |
int res = 0; |
| 489 |
Dwarf_Unsigned instr_offset_in_instrs = 0; |
| 490 |
Dwarf_Small cfa_operation = 0; |
| 491 |
const char *fields= 0; |
| 492 |
Dwarf_Unsigned u0 = 0; |
| 493 |
Dwarf_Unsigned u1 = 0; |
| 494 |
Dwarf_Signed s0 = 0; |
| 495 |
Dwarf_Signed s1 = 0; |
| 496 |
Dwarf_Block expression_block; |
| 497 |
Dwarf_Unsigned code_alignment_factor = 0; |
| 498 |
Dwarf_Signed data_alignment_factor = 0; |
| 499 |
const char *op_name = 0; |
| 500 |
|
| 501 |
expression_block = dwblockzero; |
| 502 |
res = dwarf_get_frame_instruction(frame_instr_head, |
| 503 |
i,&instr_offset_in_instrs, &cfa_operation, |
| 504 |
&fields, &u0,&u1,&s0,&s1, |
| 505 |
&code_alignment_factor, |
| 506 |
&data_alignment_factor, |
| 507 |
&expression_block,error); |
| 508 |
if (res != DW_DLV_OK) { |
| 509 |
if (res == DW_DLV_ERROR) { |
| 510 |
printf("ERROR reading frame instruction " |
| 511 |
"%" DW_PR_DUu "\n", |
| 512 |
frame_instr_count); |
| 513 |
dwarf_dealloc_error(dbg,*error); |
| 514 |
*error = 0; |
| 515 |
} else { |
| 516 |
printf("NO ENTRY reading frame instruction " |
| 517 |
" %" DW_PR_DUu "\n",frame_instr_count); |
| 518 |
} |
| 519 |
break; |
| 520 |
} |
| 521 |
dwarf_get_CFA_name(cfa_operation,&op_name); |
| 522 |
printf("[%2" DW_PR_DUu "] %" DW_PR_DUu " %s ",i, |
| 523 |
instr_offset_in_instrs,op_name); |
| 524 |
switch(fields[0]) { |
| 525 |
case 'u': { |
| 526 |
if (!fields[1]) { |
| 527 |
printf("%" DW_PR_DUu "\n",u0); |
| 528 |
} |
| 529 |
if (fields[1] == 'c') { |
| 530 |
Dwarf_Unsigned final = |
| 531 |
u0*code_alignment_factor; |
| 532 |
printf("%" DW_PR_DUu ,final); |
| 533 |
#if 0 |
| 534 |
if (glflags.verbose) { |
| 535 |
printf(" (%" DW_PR_DUu " * %" DW_PR_DUu, |
| 536 |
u0,code_alignment_factor); |
| 537 |
|
| 538 |
} |
| 539 |
#endif |
| 540 |
printf("\n"); |
| 541 |
} |
| 542 |
} |
| 543 |
break; |
| 544 |
case 'r': { |
| 545 |
if (!fields[1]) { |
| 546 |
printf("r%" DW_PR_DUu "\n",u0); |
| 547 |
break; |
| 548 |
} |
| 549 |
if (fields[1] == 'u') { |
| 550 |
if (!fields[2]) { |
| 551 |
printf("%" DW_PR_DUu ,u1); |
| 552 |
printf("\n"); |
| 553 |
break; |
| 554 |
} |
| 555 |
if (fields[2] == 'd') { |
| 556 |
Dwarf_Signed final = |
| 557 |
(Dwarf_Signed)u0 * |
| 558 |
data_alignment_factor; |
| 559 |
printf("%" DW_PR_DUu ,final); |
| 560 |
printf("\n"); |
| 561 |
} |
| 562 |
} |
| 563 |
if (fields[1] == 'r') { |
| 564 |
printf("r%" DW_PR_DUu "\n",u0); |
| 565 |
printf(" "); |
| 566 |
printf("r%" DW_PR_DUu "\n",u1); |
| 567 |
printf("\n"); |
| 568 |
} |
| 569 |
if (fields[1] == 's') { |
| 570 |
if (fields[2] == 'd') { |
| 571 |
Dwarf_Signed final = s1 * data_alignment_factor; |
| 572 |
printf("r%" DW_PR_DUu "\n",u0); |
| 573 |
printf("%" DW_PR_DSd , final); |
| 574 |
#if 0 |
| 575 |
if (glflags.verbose) { |
| 576 |
printf(" (%" DW_PR_DSd " * %" DW_PR_DSd, |
| 577 |
s1,data_alignment_factor); |
| 578 |
} |
| 579 |
#endif |
| 580 |
printf("\n"); |
| 581 |
} |
| 582 |
} |
| 583 |
if (fields[1] == 'b') { |
| 584 |
/* rb */ |
| 585 |
printf("r%" DW_PR_DUu "\n",u0); |
| 586 |
printf("%" DW_PR_DUu ,u0); |
| 587 |
printf(" expr block len %" DW_PR_DUu "\n", |
| 588 |
expression_block.bl_len); |
| 589 |
dump_block(" ", expression_block.bl_data, |
| 590 |
(Dwarf_Signed) expression_block.bl_len); |
| 591 |
printf("\n"); |
| 592 |
#if 0 |
| 593 |
if (glflags.verbose) { |
| 594 |
print_expression(dbg,die,&expression_block, |
| 595 |
addr_size,offset_size, |
| 596 |
version); |
| 597 |
} |
| 598 |
#endif |
| 599 |
} |
| 600 |
} |
| 601 |
break; |
| 602 |
case 's': { |
| 603 |
if (fields[1] == 'd') { |
| 604 |
Dwarf_Signed final = s0*data_alignment_factor; |
| 605 |
|
| 606 |
printf(" %" DW_PR_DSd ,final); |
| 607 |
#if 0 |
| 608 |
if (glflags.verbose) { |
| 609 |
printf(" (%" DW_PR_DSd " * %" DW_PR_DSd, |
| 610 |
s0,data_alignment_factor); |
| 611 |
} |
| 612 |
#endif |
| 613 |
printf("\n"); |
| 614 |
} |
| 615 |
} |
| 616 |
break; |
| 617 |
case 'b': { |
| 618 |
if (!fields[1]) { |
| 619 |
printf(" expr block len %" DW_PR_DUu "\n", |
| 620 |
expression_block.bl_len); |
| 621 |
dump_block(" ", expression_block.bl_data, |
| 622 |
(Dwarf_Signed) expression_block.bl_len); |
| 623 |
printf("\n"); |
| 624 |
#if 0 |
| 625 |
if (glflags.verbose) { |
| 626 |
print_expression(dbg,die,&expression_block, |
| 627 |
addr_size,offset_size, |
| 628 |
version); |
| 629 |
} |
| 630 |
#endif |
| 631 |
} |
| 632 |
} |
| 633 |
break; |
| 634 |
case 0: |
| 635 |
printf("\n"); |
| 636 |
break; |
| 637 |
default: |
| 638 |
printf("UNKNOWN FIELD 0x%x\n",fields[0]); |
| 639 |
} |
| 640 |
} |
| 641 |
return DW_DLV_OK; |
| 642 |
} |
| 643 |
|
| 644 |
/* Just prints the instructions in the fde. */ |
| 645 |
static void |
| 646 |
print_fde_instrs(Dwarf_Debug dbg, |
| 647 |
Dwarf_Fde fde, Dwarf_Error *error) |
| 648 |
{ |
| 649 |
int res; |
| 650 |
Dwarf_Addr lowpc = 0; |
| 651 |
Dwarf_Unsigned func_length = 0; |
| 652 |
Dwarf_Small * fde_bytes; |
| 653 |
Dwarf_Unsigned fde_byte_length = 0; |
| 654 |
Dwarf_Off cie_offset = 0; |
| 655 |
Dwarf_Signed cie_index = 0; |
| 656 |
Dwarf_Off fde_offset = 0; |
| 657 |
Dwarf_Addr arbitrary_addr = 0; |
| 658 |
Dwarf_Addr subsequent_pc = 0; |
| 659 |
Dwarf_Bool has_more_rows = TRUE; |
| 660 |
Dwarf_Addr actual_pc = 0; |
| 661 |
Dwarf_Regtable3 tab3; |
| 662 |
int oldrulecount = 0; |
| 663 |
Dwarf_Small *outinstrs = 0; |
| 664 |
Dwarf_Unsigned instrslen = 0; |
| 665 |
Dwarf_Cie cie = 0; |
| 666 |
|
| 667 |
res = dwarf_get_fde_range(fde,&lowpc,&func_length,&fde_bytes, |
| 668 |
&fde_byte_length,&cie_offset,&cie_index,&fde_offset,error); |
| 669 |
if (res != DW_DLV_OK) { |
| 670 |
printf("Problem getting fde range \n"); |
| 671 |
exit(EXIT_FAILURE); |
| 672 |
} |
| 673 |
|
| 674 |
arbitrary_addr = lowpc + (func_length/2); |
| 675 |
printf("function low pc 0x%" DW_PR_DUx |
| 676 |
" and length 0x%" DW_PR_DUx |
| 677 |
" and midpoint addr we choose 0x%" DW_PR_DUx |
| 678 |
"\n", |
| 679 |
lowpc,func_length,arbitrary_addr); |
| 680 |
|
| 681 |
/* 1 is arbitrary. We are winding up getting the |
| 682 |
rule count here while leaving things unchanged. */ |
| 683 |
oldrulecount = dwarf_set_frame_rule_table_size(dbg,1); |
| 684 |
dwarf_set_frame_rule_table_size(dbg,oldrulecount); |
| 685 |
|
| 686 |
tab3.rt3_reg_table_size = oldrulecount; |
| 687 |
tab3.rt3_rules = (struct Dwarf_Regtable_Entry3_s *) malloc( |
| 688 |
sizeof(struct Dwarf_Regtable_Entry3_s)* oldrulecount); |
| 689 |
if (!tab3.rt3_rules) { |
| 690 |
printf("Unable to malloc for %d rules\n",oldrulecount); |
| 691 |
exit(EXIT_FAILURE); |
| 692 |
} |
| 693 |
|
| 694 |
res = dwarf_get_fde_info_for_all_regs3(fde,arbitrary_addr , |
| 695 |
&tab3,&actual_pc,error); |
| 696 |
printf("function Requested_pc 0x%" |
| 697 |
DW_PR_DUx " Actual addr of row 0x%" DW_PR_DUx "\n", |
| 698 |
arbitrary_addr,actual_pc); |
| 699 |
if (res != DW_DLV_OK) { |
| 700 |
free(tab3.rt3_rules); |
| 701 |
tab3.rt3_rules = 0; |
| 702 |
printf("dwarf_get_fde_info_for_all_regs3 failed!\n"); |
| 703 |
exit(EXIT_FAILURE); |
| 704 |
} |
| 705 |
/* Now for an example of iterating through a range of addrs |
| 706 |
efficiently, lets redo the above and iterate pc values. |
| 707 |
the function called is new as of 0.9.0 October 2023. */ |
| 708 |
for (arbitrary_addr=lowpc; has_more_rows ; |
| 709 |
arbitrary_addr = subsequent_pc) { |
| 710 |
res = dwarf_get_fde_info_for_all_regs3_b(fde,arbitrary_addr , |
| 711 |
&tab3,&actual_pc,&has_more_rows, &subsequent_pc,error); |
| 712 |
if (res != DW_DLV_OK) { |
| 713 |
free(tab3.rt3_rules); |
| 714 |
tab3.rt3_rules = 0; |
| 715 |
printf("dwarf_get_fde_info_for_all_regs3_b failed!\n"); |
| 716 |
exit(EXIT_FAILURE); |
| 717 |
} |
| 718 |
printf("iterating Requested addr of row 0x%" DW_PR_DUx |
| 719 |
" Actual addr 0x%" DW_PR_DUx |
| 720 |
" More rows? %s Subsequent_pc 0x%" DW_PR_DUx "\n", |
| 721 |
arbitrary_addr,actual_pc,has_more_rows?"yes":"no", |
| 722 |
subsequent_pc); |
| 723 |
} |
| 724 |
print_regtable(&tab3); |
| 725 |
|
| 726 |
res = dwarf_get_fde_instr_bytes(fde,&outinstrs,&instrslen,error); |
| 727 |
if (res != DW_DLV_OK) { |
| 728 |
free(tab3.rt3_rules); |
| 729 |
tab3.rt3_rules = 0; |
| 730 |
printf("dwarf_get_fde_instr_bytes failed!\n"); |
| 731 |
exit(EXIT_FAILURE); |
| 732 |
} |
| 733 |
res = dwarf_get_cie_of_fde(fde,&cie,error); |
| 734 |
if (res != DW_DLV_OK) { |
| 735 |
free(tab3.rt3_rules); |
| 736 |
tab3.rt3_rules = 0; |
| 737 |
printf("Error getting cie from fde\n"); |
| 738 |
exit(EXIT_FAILURE); |
| 739 |
} |
| 740 |
|
| 741 |
{ |
| 742 |
Dwarf_Frame_Instr_Head frame_instr_head = 0; |
| 743 |
Dwarf_Unsigned frame_instr_count = 0; |
| 744 |
res = dwarf_expand_frame_instructions(cie, |
| 745 |
outinstrs,instrslen, |
| 746 |
&frame_instr_head, |
| 747 |
&frame_instr_count, |
| 748 |
error); |
| 749 |
if (res != DW_DLV_OK) { |
| 750 |
free(tab3.rt3_rules); |
| 751 |
tab3.rt3_rules = 0; |
| 752 |
printf("dwarf_expand_frame_instructions failed!\n"); |
| 753 |
exit(EXIT_FAILURE); |
| 754 |
} |
| 755 |
printf("Frame op count: %" DW_PR_DUu "\n",frame_instr_count); |
| 756 |
print_frame_instrs(dbg,frame_instr_head, |
| 757 |
frame_instr_count, error); |
| 758 |
|
| 759 |
dwarf_dealloc_frame_instr_head(frame_instr_head); |
| 760 |
} |
| 761 |
free(tab3.rt3_rules); |
| 762 |
tab3.rt3_rules = 0; |
| 763 |
} |
| 764 |
|
| 765 |
static void |
| 766 |
print_reg(int r) |
| 767 |
{ |
| 768 |
switch(r) { |
| 769 |
case SAME_VAL: |
| 770 |
printf(" %d SAME_VAL ",r); |
| 771 |
break; |
| 772 |
case UNDEF_VAL: |
| 773 |
printf(" %d UNDEF_VAL ",r); |
| 774 |
break; |
| 775 |
case CFA_VAL: |
| 776 |
printf(" %d (CFA) ",r); |
| 777 |
break; |
| 778 |
default: |
| 779 |
printf(" r%d ",r); |
| 780 |
break; |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
static void |
| 785 |
print_one_regentry(const char *prefix, |
| 786 |
struct Dwarf_Regtable_Entry3_s *entry) |
| 787 |
{ |
| 788 |
int is_cfa = !strcmp("cfa",prefix); |
| 789 |
printf("%s ",prefix); |
| 790 |
printf("type: %d %s ", |
| 791 |
entry->dw_value_type, |
| 792 |
(entry->dw_value_type == DW_EXPR_OFFSET)? "DW_EXPR_OFFSET": |
| 793 |
(entry->dw_value_type == DW_EXPR_VAL_OFFSET)? |
| 794 |
"DW_EXPR_VAL_OFFSET": |
| 795 |
(entry->dw_value_type == DW_EXPR_EXPRESSION)? |
| 796 |
"DW_EXPR_EXPRESSION": |
| 797 |
(entry->dw_value_type == DW_EXPR_VAL_EXPRESSION)? |
| 798 |
"DW_EXPR_VAL_EXPRESSION": |
| 799 |
"Unknown"); |
| 800 |
switch(entry->dw_value_type) { |
| 801 |
case DW_EXPR_OFFSET: |
| 802 |
print_reg(entry->dw_regnum); |
| 803 |
printf(" offset_rel? %d ",entry->dw_offset_relevant); |
| 804 |
if (entry->dw_offset_relevant) { |
| 805 |
printf(" offset %" DW_PR_DSd " " , |
| 806 |
(Dwarf_Signed)entry->dw_offset); |
| 807 |
if (is_cfa) { |
| 808 |
printf("defines cfa value"); |
| 809 |
} else { |
| 810 |
printf("address of value is CFA plus signed offset"); |
| 811 |
} |
| 812 |
if (!is_cfa && entry->dw_regnum != CFA_VAL) { |
| 813 |
printf(" compiler botch, regnum != CFA_VAL"); |
| 814 |
} |
| 815 |
} else { |
| 816 |
printf("value in register"); |
| 817 |
} |
| 818 |
break; |
| 819 |
case DW_EXPR_VAL_OFFSET: |
| 820 |
print_reg(entry->dw_regnum); |
| 821 |
printf(" offset %" DW_PR_DSd " " , |
| 822 |
(Dwarf_Signed)entry->dw_offset); |
| 823 |
if (is_cfa) { |
| 824 |
printf("does this make sense? No?"); |
| 825 |
} else { |
| 826 |
printf("value at CFA plus signed offset"); |
| 827 |
} |
| 828 |
if (!is_cfa && entry->dw_regnum != CFA_VAL) { |
| 829 |
printf(" compiler botch, regnum != CFA_VAL"); |
| 830 |
} |
| 831 |
break; |
| 832 |
case DW_EXPR_EXPRESSION: |
| 833 |
print_reg(entry->dw_regnum); |
| 834 |
if (entry->dw_offset_relevant) { |
| 835 |
printf(" FAIL. ERROR: a DW_EXPR_EXPRESSION " |
| 836 |
"must not have the dw_offset marked as " |
| 837 |
"offset_relevant \n"); |
| 838 |
printf(" offset_rel ERROR: %d ", |
| 839 |
entry->dw_offset_relevant); |
| 840 |
printf(" offset %" DW_PR_DSd " " , |
| 841 |
(Dwarf_Signed)entry->dw_offset); |
| 842 |
} |
| 843 |
printf("Block ptr set? %s ", |
| 844 |
entry->dw_block.bl_data?"yes":"no"); |
| 845 |
printf(" Value is at address given by expr val "); |
| 846 |
/* printf(" block-ptr 0x%" DW_PR_DUx " ", |
| 847 |
(Dwarf_Unsigned)entry->dw_block_ptr); */ |
| 848 |
break; |
| 849 |
case DW_EXPR_VAL_EXPRESSION: |
| 850 |
printf(" expression byte len %" DW_PR_DUu " " , |
| 851 |
entry->dw_block.bl_len); |
| 852 |
printf("Block ptr set? %s ", |
| 853 |
entry->dw_block.bl_data?"yes":"no"); |
| 854 |
if (entry->dw_offset_relevant) { |
| 855 |
printf(" FAIL. ERROR: a DW_EXPR_VAL_EXPRESSION " |
| 856 |
"must not have the dw_offset marked as " |
| 857 |
"offset_relevant \n"); |
| 858 |
printf(" offset_rel ERROR: %d ", |
| 859 |
entry->dw_offset_relevant); |
| 860 |
printf(" offset %" DW_PR_DSd " " , |
| 861 |
(Dwarf_Signed)entry->dw_offset); |
| 862 |
} |
| 863 |
printf(" Value is expr val "); |
| 864 |
if (!entry->dw_block.bl_data) { |
| 865 |
printf("Compiler or libdwarf botch, " |
| 866 |
"NULL block data pointer. "); |
| 867 |
} |
| 868 |
/* printf(" block-ptr 0x%" DW_PR_DUx " ", |
| 869 |
(Dwarf_Unsigned)entry->dw_block.bl_data); */ |
| 870 |
break; |
| 871 |
default: break; |
| 872 |
} |
| 873 |
printf("\n"); |
| 874 |
} |
| 875 |
|
| 876 |
static void |
| 877 |
print_regtable(Dwarf_Regtable3 *tab3) |
| 878 |
{ |
| 879 |
int r; |
| 880 |
/* We won't print too much. A bit arbitrary. */ |
| 881 |
int max = 10; |
| 882 |
if (max > tab3->rt3_reg_table_size) { |
| 883 |
max = tab3->rt3_reg_table_size; |
| 884 |
} |
| 885 |
print_one_regentry("cfa",&tab3->rt3_cfa_rule); |
| 886 |
|
| 887 |
for (r = 0; r < max; r++) { |
| 888 |
char rn[30]; |
| 889 |
snprintf(rn,sizeof(rn),"reg %d",r); |
| 890 |
print_one_regentry(rn,tab3->rt3_rules+r); |
| 891 |
} |
| 892 |
} |