static int call_probe_finder(Dwarf_Die *sc_die, struct probe_finder *pf) { Dwarf_Attribute fb_attr; size_t nops; int ret; if (!sc_die) { pr_err("Caller must pass a scope DIE. Program error.\n"); return -EINVAL; } if (dwarf_tag(sc_die) != DW_TAG_subprogram) { if (!die_find_realfunc(&pf->cu_die, pf->addr, &pf->sp_die)) { pr_warning("Failed to find probe point in any " "functions.\n"); return -ENOENT; } } else memcpy(&pf->sp_die, sc_die, sizeof(Dwarf_Die)); dwarf_attr(&pf->sp_die, DW_AT_frame_base, &fb_attr); ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1); if (ret <= 0 || nops == 0) { pf->fb_ops = NULL; #if _ELFUTILS_PREREQ(0, 142) } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa && pf->cfi != NULL) { Dwarf_Frame *frame; if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 || dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) { pr_warning("Failed to get call frame on 0x%jx\n", (uintmax_t)pf->addr); return -ENOENT; } #endif } ret = pf->callback(sc_die, pf); pf->fb_ops = NULL; return ret; }
/* Call probe_finder callback with scope DIE */ static int call_probe_finder(Dwarf_Die *sc_die, struct probe_finder *pf) { Dwarf_Attribute fb_attr; size_t nops; int ret; if (!sc_die) { pr_err("Caller must pass a scope DIE. Program error.\n"); return -EINVAL; } /* If not a real subprogram, find a real one */ if (!die_is_func_def(sc_die)) { if (!die_find_realfunc(&pf->cu_die, pf->addr, &pf->sp_die)) { pr_warning("Failed to find probe point in any " "functions.\n"); return -ENOENT; } } else memcpy(&pf->sp_die, sc_die, sizeof(Dwarf_Die)); /* Get the frame base attribute/ops from subprogram */ dwarf_attr(&pf->sp_die, DW_AT_frame_base, &fb_attr); ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1); if (ret <= 0 || nops == 0) { pf->fb_ops = NULL; #if _ELFUTILS_PREREQ(0, 142) } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa && pf->cfi != NULL) { Dwarf_Frame *frame; if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 || dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) { pr_warning("Failed to get call frame on 0x%jx\n", (uintmax_t)pf->addr); return -ENOENT; } #endif } /* Call finder's callback handler */ ret = pf->callback(sc_die, pf); /* *pf->fb_ops will be cached in libdw. Don't free it. */ pf->fb_ops = NULL; return ret; }
/* Call probe_finder callback with real subprogram DIE */ static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf) { Dwarf_Die die_mem; Dwarf_Attribute fb_attr; size_t nops; int ret; /* If no real subprogram, find a real one */ if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) { sp_die = die_find_real_subprogram(&pf->cu_die, pf->addr, &die_mem); if (!sp_die) { pr_warning("Failed to find probe point in any " "functions.\n"); return -ENOENT; } } /* Get the frame base attribute/ops */ dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr); ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1); if (ret <= 0 || nops == 0) { pf->fb_ops = NULL; #if _ELFUTILS_PREREQ(0, 142) } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa && pf->cfi != NULL) { Dwarf_Frame *frame; if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 || dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) { pr_warning("Failed to get call frame on 0x%jx\n", (uintmax_t)pf->addr); return -ENOENT; } #endif } /* Call finder's callback handler */ ret = pf->callback(sp_die, pf); /* *pf->fb_ops will be cached in libdw. Don't free it. */ pf->fb_ops = NULL; return ret; }
/* Show a variables in kprobe event format */ static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf) { Dwarf_Attribute attr; Dwarf_Die die_mem; Dwarf_Op *expr; size_t nexpr; int ret; if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL) goto error; /* TODO: handle more than 1 exprs */ ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1); if (ret <= 0 || nexpr == 0) goto error; ret = convert_location(expr, pf); if (ret == 0 && pf->pvar->field) { ret = convert_variable_fields(vr_die, pf->pvar->var, pf->pvar->field, &pf->tvar->ref, &die_mem); vr_die = &die_mem; } if (ret == 0) { if (pf->pvar->type) { pf->tvar->type = strdup(pf->pvar->type); if (pf->tvar->type == NULL) ret = -ENOMEM; } else ret = convert_variable_type(vr_die, pf->tvar); } /* *expr will be cached in libdw. Don't free it. */ return ret; error: /* TODO: Support const_value */ pr_err("Failed to find the location of %s at this address.\n" " Perhaps, it has been optimized out.\n", pf->pvar->var); return -ENOENT; }
static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr, Dwarf_Op *fb_ops, struct probe_trace_arg *tvar) { Dwarf_Attribute attr; Dwarf_Op *op; size_t nops; unsigned int regn; Dwarf_Word offs = 0; bool ref = false; const char *regs; int ret; if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL) goto static_var; if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL || dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 || nops == 0) { return -ENOENT; } if (op->atom == DW_OP_addr) { static_var: if (!tvar) return 0; ret = strlen(dwarf_diename(vr_die)); tvar->value = zalloc(ret + 2); if (tvar->value == NULL) return -ENOMEM; snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die)); tvar->ref = alloc_trace_arg_ref((long)offs); if (tvar->ref == NULL) return -ENOMEM; return 0; } if (op->atom == DW_OP_fbreg) { if (fb_ops == NULL) return -ENOTSUP; ref = true; offs = op->number; op = &fb_ops[0]; } if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) { regn = op->atom - DW_OP_breg0; offs += op->number; ref = true; } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) { regn = op->atom - DW_OP_reg0; } else if (op->atom == DW_OP_bregx) { regn = op->number; offs += op->number2; ref = true; } else if (op->atom == DW_OP_regx) { regn = op->number; } else { pr_debug("DW_OP %x is not supported.\n", op->atom); return -ENOTSUP; } if (!tvar) return 0; regs = get_arch_regstr(regn); if (!regs) { pr_warning("Mapping for the register number %u " "missing on this architecture.\n", regn); return -ERANGE; } tvar->value = strdup(regs); if (tvar->value == NULL) return -ENOMEM; if (ref) { tvar->ref = alloc_trace_arg_ref((long)offs); if (tvar->ref == NULL) return -ENOMEM; } return 0; }
/* * Convert a location into trace_arg. * If tvar == NULL, this just checks variable can be converted. * If fentry == true and vr_die is a parameter, do huristic search * for the location fuzzed by function entry mcount. */ static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr, Dwarf_Op *fb_ops, Dwarf_Die *sp_die, struct probe_trace_arg *tvar) { Dwarf_Attribute attr; Dwarf_Addr tmp = 0; Dwarf_Op *op; size_t nops; unsigned int regn; Dwarf_Word offs = 0; bool ref = false; const char *regs; int ret; if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL) goto static_var; /* TODO: handle more than 1 exprs */ if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL) return -EINVAL; /* Broken DIE ? */ if (dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0) { ret = dwarf_entrypc(sp_die, &tmp); if (ret || addr != tmp || dwarf_tag(vr_die) != DW_TAG_formal_parameter || dwarf_highpc(sp_die, &tmp)) return -ENOENT; /* * This is fuzzed by fentry mcount. We try to find the * parameter location at the earliest address. */ for (addr += 1; addr <= tmp; addr++) { if (dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) > 0) goto found; } return -ENOENT; } found: if (nops == 0) /* TODO: Support const_value */ return -ENOENT; if (op->atom == DW_OP_addr) { static_var: if (!tvar) return 0; /* Static variables on memory (not stack), make @varname */ ret = strlen(dwarf_diename(vr_die)); tvar->value = zalloc(ret + 2); if (tvar->value == NULL) return -ENOMEM; snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die)); tvar->ref = alloc_trace_arg_ref((long)offs); if (tvar->ref == NULL) return -ENOMEM; return 0; } /* If this is based on frame buffer, set the offset */ if (op->atom == DW_OP_fbreg) { if (fb_ops == NULL) return -ENOTSUP; ref = true; offs = op->number; op = &fb_ops[0]; } if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) { regn = op->atom - DW_OP_breg0; offs += op->number; ref = true; } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) { regn = op->atom - DW_OP_reg0; } else if (op->atom == DW_OP_bregx) { regn = op->number; offs += op->number2; ref = true; } else if (op->atom == DW_OP_regx) { regn = op->number; } else { pr_debug("DW_OP %x is not supported.\n", op->atom); return -ENOTSUP; } if (!tvar) return 0; regs = get_arch_regstr(regn); if (!regs) { /* This should be a bug in DWARF or this tool */ pr_warning("Mapping for the register number %u " "missing on this architecture.\n", regn); return -ERANGE; } tvar->value = strdup(regs); if (tvar->value == NULL) return -ENOMEM; if (ref) { tvar->ref = alloc_trace_arg_ref((long)offs); if (tvar->ref == NULL) return -ENOMEM; } return 0; }
/* Show a probe point to output buffer */ static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf) { struct kprobe_trace_event *tev; Dwarf_Addr eaddr; Dwarf_Die die_mem; const char *name; int ret, i; Dwarf_Attribute fb_attr; size_t nops; if (pf->ntevs == pf->max_tevs) { pr_warning("Too many( > %d) probe point found.\n", pf->max_tevs); return -ERANGE; } tev = &pf->tevs[pf->ntevs++]; /* If no real subprogram, find a real one */ if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) { sp_die = die_find_real_subprogram(&pf->cu_die, pf->addr, &die_mem); if (!sp_die) { pr_warning("Failed to find probe point in any " "functions.\n"); return -ENOENT; } } /* Copy the name of probe point */ name = dwarf_diename(sp_die); if (name) { if (dwarf_entrypc(sp_die, &eaddr) != 0) { pr_warning("Failed to get entry pc of %s\n", dwarf_diename(sp_die)); return -ENOENT; } tev->point.symbol = strdup(name); if (tev->point.symbol == NULL) return -ENOMEM; tev->point.offset = (unsigned long)(pf->addr - eaddr); } else /* This function has no name. */ tev->point.offset = (unsigned long)pf->addr; pr_debug("Probe point found: %s+%lu\n", tev->point.symbol, tev->point.offset); /* Get the frame base attribute/ops */ dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr); ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1); if (ret <= 0 || nops == 0) { pf->fb_ops = NULL; #if _ELFUTILS_PREREQ(0, 142) } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa && pf->cfi != NULL) { Dwarf_Frame *frame; if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 || dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) { pr_warning("Failed to get CFA on 0x%jx\n", (uintmax_t)pf->addr); return -ENOENT; } #endif } /* Find each argument */ tev->nargs = pf->pev->nargs; tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs); if (tev->args == NULL) return -ENOMEM; for (i = 0; i < pf->pev->nargs; i++) { pf->pvar = &pf->pev->args[i]; pf->tvar = &tev->args[i]; ret = find_variable(sp_die, pf); if (ret != 0) return ret; } /* *pf->fb_ops will be cached in libdw. Don't free it. */ pf->fb_ops = NULL; return 0; }
/* Get all variables and print their value expressions. */ static void print_varlocs (Dwarf_Die *funcdie) { // Display frame base for function if it exists. // Should be used for DW_OP_fbreg. has_frame_base = dwarf_hasattr (funcdie, DW_AT_frame_base); if (has_frame_base) { Dwarf_Attribute fb_attr; if (dwarf_attr (funcdie, DW_AT_frame_base, &fb_attr) == NULL) error (EXIT_FAILURE, 0, "dwarf_attr fb: %s", dwarf_errmsg (-1)); Dwarf_Op *fb_expr; size_t fb_exprlen; if (dwarf_getlocation (&fb_attr, &fb_expr, &fb_exprlen) == 0) { // Covers all of function. Dwarf_Addr entrypc; if (dwarf_entrypc (funcdie, &entrypc) != 0) error (EXIT_FAILURE, 0, "dwarf_entrypc: %s", dwarf_errmsg (-1)); printf (" frame_base: "); if (entrypc == 0) printf ("XXX zero address"); // XXX bad DWARF? else print_expr_block (&fb_attr, fb_expr, fb_exprlen, entrypc); printf ("\n"); } else { Dwarf_Addr base, start, end; ptrdiff_t off = 0; printf (" frame_base:\n"); while ((off = dwarf_getlocations (&fb_attr, off, &base, &start, &end, &fb_expr, &fb_exprlen)) > 0) { printf (" (%" PRIx64 ",%" PRIx64 ") ", start, end); print_expr_block (&fb_attr, fb_expr, fb_exprlen, start); printf ("\n"); } if (off < 0) error (EXIT_FAILURE, 0, "dwarf_getlocations fb: %s", dwarf_errmsg (-1)); } } else if (dwarf_tag (funcdie) == DW_TAG_inlined_subroutine) { // See whether the subprogram we are inlined into has a frame // base we should use. Dwarf_Die *scopes; int n = dwarf_getscopes_die (funcdie, &scopes); if (n <= 0) error (EXIT_FAILURE, 0, "dwarf_getscopes_die: %s", dwarf_errmsg (-1)); while (n-- > 0) if (dwarf_tag (&scopes[n]) == DW_TAG_subprogram && dwarf_hasattr (&scopes[n], DW_AT_frame_base)) { has_frame_base = true; break; } free (scopes); } if (! dwarf_haschildren (funcdie)) return; Dwarf_Die child; int res = dwarf_child (funcdie, &child); if (res < 0) error (EXIT_FAILURE, 0, "dwarf_child: %s", dwarf_errmsg (-1)); /* We thought there was a child, but the child list was actually empty. This isn't technically an error in the DWARF, but it is certainly non-optimimal. */ if (res == 1) return; do { int tag = dwarf_tag (&child); if (tag == DW_TAG_variable || tag == DW_TAG_formal_parameter) { const char *what = tag == DW_TAG_variable ? "variable" : "parameter"; print_die (&child, what, 2); if (dwarf_hasattr (&child, DW_AT_location)) { Dwarf_Attribute attr; if (dwarf_attr (&child, DW_AT_location, &attr) == NULL) error (EXIT_FAILURE, 0, "dwarf_attr: %s", dwarf_errmsg (-1)); Dwarf_Op *expr; size_t exprlen; if (dwarf_getlocation (&attr, &expr, &exprlen) == 0) { // Covers all ranges of the function. // Evaluate the expression block for each range. ptrdiff_t offset = 0; Dwarf_Addr base, begin, end; do { offset = dwarf_ranges (funcdie, offset, &base, &begin, &end); if (offset < 0) error (EXIT_FAILURE, 0, "dwarf_ranges: %s", dwarf_errmsg (-1)); if (offset > 0) { if (exprlen == 0) printf (" (%" PRIx64 ",%" PRIx64 ") <empty expression>\n", begin, end); else print_expr_block_addrs (&attr, begin, end, expr, exprlen); } } while (offset > 0); if (offset < 0) error (EXIT_FAILURE, 0, "dwarf_ranges: %s", dwarf_errmsg (-1)); } else { Dwarf_Addr base, begin, end; ptrdiff_t offset = 0; while ((offset = dwarf_getlocations (&attr, offset, &base, &begin, &end, &expr, &exprlen)) > 0) if (begin >= end) printf (" (%" PRIx64 ",%" PRIx64 ") <empty range>\n", begin, end); // XXX report? else { print_expr_block_addrs (&attr, begin, end, expr, exprlen); // Extra sanity check for dwarf_getlocation_addr // Must at least find one range for begin and end-1. Dwarf_Op *expraddr; size_t expraddr_len; int locs = dwarf_getlocation_addr (&attr, begin, &expraddr, &expraddr_len, 1); assert (locs == 1); locs = dwarf_getlocation_addr (&attr, end - 1, &expraddr, &expraddr_len, 1); assert (locs == 1); } if (offset < 0) error (EXIT_FAILURE, 0, "dwarf_getlocations: %s", dwarf_errmsg (-1)); } } else if (dwarf_hasattr (&child, DW_AT_const_value)) { printf (" <constant value>\n"); // Lookup type and print. } else { printf (" <no value>\n"); } } } while (dwarf_siblingof (&child, &child) == 0); }
static void print_expr (Dwarf_Attribute *attr, Dwarf_Op *expr, Dwarf_Addr addr) { uint8_t atom = expr->atom; const char *opname = dwarf_opcode_string (atom); assert (opname != NULL); switch (atom) { case DW_OP_deref: case DW_OP_dup: case DW_OP_drop: case DW_OP_over: case DW_OP_swap: case DW_OP_rot: case DW_OP_xderef: case DW_OP_abs: case DW_OP_and: case DW_OP_div: case DW_OP_minus: case DW_OP_mod: case DW_OP_mul: case DW_OP_neg: case DW_OP_not: case DW_OP_or: case DW_OP_plus: case DW_OP_shl: case DW_OP_shr: case DW_OP_shra: case DW_OP_xor: case DW_OP_eq: case DW_OP_ge: case DW_OP_gt: case DW_OP_le: case DW_OP_lt: case DW_OP_ne: case DW_OP_lit0 ... DW_OP_lit31: case DW_OP_reg0 ... DW_OP_reg31: case DW_OP_nop: case DW_OP_stack_value: /* No arguments. */ printf ("%s", opname); break; case DW_OP_form_tls_address: /* No arguments. Special. Pops an address and pushes the corresponding address in the current thread local storage. Uses the thread local storage block of the defining module (executable, shared library). */ printf ("%s", opname); break; case DW_OP_GNU_push_tls_address: /* No arguments. Special. Not the same as DW_OP_form_tls_address. Pops an offset into the current thread local strorage and pushes back the actual address. */ printf ("%s", opname); break; case DW_OP_call_frame_cfa: /* No arguments. Special. Pushes Call Frame Address as computed by CFI data (dwarf_cfi_addrframe will fetch that info (either from the .eh_frame or .debug_frame CFI) and dwarf_frame_cfa translatesr the CFI instructions into a plain DWARF expression. Never used in CFI itself. */ if (attr == NULL) error (EXIT_FAILURE, 0, "%s used in CFI", opname); printf ("%s ", opname); if (cfi_eh == NULL && cfi_debug == NULL) error (EXIT_FAILURE, 0, "DW_OP_call_frame_cfa used but no cfi found."); Dwarf_Frame *frame; if (dwarf_cfi_addrframe (cfi_eh, addr + cfi_eh_bias, &frame) != 0 && dwarf_cfi_addrframe (cfi_debug, addr, &frame) != 0) error (EXIT_FAILURE, 0, "dwarf_cfi_addrframe 0x%" PRIx64 ": %s", addr, dwarf_errmsg (-1)); Dwarf_Op *cfa_ops; size_t cfa_nops; if (dwarf_frame_cfa (frame, &cfa_ops, &cfa_nops) != 0) error (EXIT_FAILURE, 0, "dwarf_frame_cfa 0x%" PRIx64 ": %s", addr, dwarf_errmsg (-1)); if (cfa_nops < 1) error (EXIT_FAILURE, 0, "dwarf_frame_cfa no ops"); print_expr_block (NULL, cfa_ops, cfa_nops, 0); free (frame); break; case DW_OP_push_object_address: /* No arguments. Special. Pushes object address explicitly. Normally only done implicitly by DW_AT_data_member_location. Never used in CFI. */ if (attr == NULL) error (EXIT_FAILURE, 0, "%s used in CFI", opname); printf ("%s", opname); break; case DW_OP_addr: /* 1 address argument. */ printf ("%s(0x%" PRIx64 ")", opname, (Dwarf_Addr) expr->number); break; case DW_OP_const1u: case DW_OP_const2u: case DW_OP_const4u: case DW_OP_const8u: case DW_OP_constu: case DW_OP_pick: case DW_OP_plus_uconst: case DW_OP_regx: case DW_OP_piece: case DW_OP_deref_size: case DW_OP_xderef_size: /* 1 numeric unsigned argument. */ printf ("%s(%" PRIu64 ")", opname, expr->number); break; case DW_OP_call2: case DW_OP_call4: case DW_OP_call_ref: /* 1 DIE offset argument for more ops in location attribute of DIE. Never used in CFI. */ { if (attr == NULL) error (EXIT_FAILURE, 0, "%s used in CFI", opname); Dwarf_Attribute call_attr; if (dwarf_getlocation_attr (attr, expr, &call_attr) != 0) error (EXIT_FAILURE, 0, "dwarf_getlocation_attr for %s error %s", opname, dwarf_errmsg (-1)); Dwarf_Die call_die; if (dwarf_getlocation_die (attr, expr, &call_die) != 0) error (EXIT_FAILURE, 0, "dwarf_getlocation_die for %s error %s", opname, dwarf_errmsg (-1)); Dwarf_Op *call_ops; size_t call_len; if (dwarf_getlocation (&call_attr, &call_ops, &call_len) != 0) error (EXIT_FAILURE, 0, "dwarf_getlocation for entry: %s", dwarf_errmsg (-1)); printf ("%s([%" PRIx64 "]) ", opname, dwarf_dieoffset (&call_die)); print_expr_block (&call_attr, call_ops, call_len, addr); } break; case DW_OP_const1s: case DW_OP_const2s: case DW_OP_const4s: case DW_OP_const8s: case DW_OP_consts: case DW_OP_skip: case DW_OP_bra: case DW_OP_breg0 ... DW_OP_breg31: /* 1 numeric signed argument. */ printf ("%s(%" PRId64 ")", opname, (Dwarf_Sword) expr->number); break; case DW_OP_fbreg: /* 1 numeric signed argument. Offset from frame base. */ if (attr == NULL) error (EXIT_FAILURE, 0, "%s used in CFI", opname); if (! has_frame_base) error (EXIT_FAILURE, 0, "DW_OP_fbreg used without a frame base"); printf ("%s(%" PRId64 ")", opname, (Dwarf_Sword) expr->number); break; case DW_OP_bregx: /* 2 arguments, unsigned register number, signed offset. */ printf ("%s(%" PRIu64 ",%" PRId64 ")", opname, expr->number, (Dwarf_Sword) expr->number2); break; case DW_OP_bit_piece: /* 2 arguments, unsigned size, unsigned offset. */ printf ("%s(%" PRIu64 ",%" PRIu64 ")", opname, expr->number, expr->number2); break; case DW_OP_implicit_value: /* Special, unsigned size plus block. */ { Dwarf_Attribute const_attr; Dwarf_Block block; if (dwarf_getlocation_attr (attr, expr, &const_attr) != 0) error (EXIT_FAILURE, 0, "dwarf_getlocation_attr: %s", dwarf_errmsg (-1)); if (dwarf_formblock (&const_attr, &block) != 0) error (EXIT_FAILURE, 0, "dwarf_formblock: %s", dwarf_errmsg (-1)); /* This is the "old" way. Check they result in the same. */ Dwarf_Block block_impl; if (dwarf_getlocation_implicit_value (attr, expr, &block_impl) != 0) error (EXIT_FAILURE, 0, "dwarf_getlocation_implicit_value: %s", dwarf_errmsg (-1)); assert (expr->number == block.length); assert (block.length == block_impl.length); printf ("%s(%" PRIu64 "){", opname, block.length); for (size_t i = 0; i < block.length; i++) { printf ("%02x", block.data[i]); assert (block.data[i] == block_impl.data[i]); } printf("}"); } break; case DW_OP_GNU_implicit_pointer: /* Special, DIE offset, signed offset. Referenced DIE has a location or const_value attribute. */ { if (attr == NULL) error (EXIT_FAILURE, 0, "%s used in CFI", opname); Dwarf_Attribute attrval; if (dwarf_getlocation_implicit_pointer (attr, expr, &attrval) != 0) error (EXIT_FAILURE, 0, "dwarf_getlocation_implicit_pointer: %s", dwarf_errmsg (-1)); // Sanity check, results should be the same. Dwarf_Attribute attrval2; if (dwarf_getlocation_attr (attr, expr, &attrval2) != 0) error (EXIT_FAILURE, 0, "dwarf_getlocation_attr: %s", dwarf_errmsg (-1)); assert (dwarf_whatattr (&attrval) == dwarf_whatattr (&attrval2)); assert (dwarf_whatform (&attrval) == dwarf_whatform (&attrval2)); // In theory two different valp pointers could point to the same // value. But here we really expect them to be the equal. assert (attrval.valp == attrval2.valp); Dwarf_Die impl_die; if (dwarf_getlocation_die (attr, expr, &impl_die) != 0) error (EXIT_FAILURE, 0, "dwarf_getlocation_due: %s", dwarf_errmsg (-1)); printf ("%s([%" PRIx64 "],%" PRId64 ") ", opname, dwarf_dieoffset (&impl_die), expr->number2); if (dwarf_whatattr (&attrval) == DW_AT_const_value) printf ("<constant value>"); // Lookup type... else { // Lookup the location description at the current address. Dwarf_Op *exprval; size_t exprval_len; int locs = dwarf_getlocation_addr (&attrval, addr, &exprval, &exprval_len, 1); if (locs == 0) printf ("<no location>"); // This means "optimized out". else if (locs == 1) print_expr_block (&attrval, exprval, exprval_len, addr); else error (EXIT_FAILURE, 0, "dwarf_getlocation_addr attrval at addr 0x%" PRIx64 ", locs (%d): %s", addr, locs, dwarf_errmsg (-1)); } } break; case DW_OP_GNU_entry_value: /* Special, unsigned size plus expression block. All registers inside the block should be interpreted as they had on entering the function. dwarf_getlocation_attr will return an attribute containing the block as locexpr which can be retrieved with dwarf_getlocation. */ { Dwarf_Attribute entry_attr; if (dwarf_getlocation_attr (attr, expr, &entry_attr) != 0) error (EXIT_FAILURE, 0, "dwarf_getlocation_attr: %s", dwarf_errmsg (-1)); Dwarf_Op *entry_ops; size_t entry_len; if (dwarf_getlocation (&entry_attr, &entry_ops, &entry_len) != 0) error (EXIT_FAILURE, 0, "dwarf_getlocation for entry: %s", dwarf_errmsg (-1)); printf ("%s(%zd) ", opname, entry_len); print_expr_block (attr, entry_ops, entry_len, addr); } break; case DW_OP_GNU_parameter_ref: /* Special, unsigned CU relative DIE offset pointing to a DW_TAG_formal_parameter. The value that parameter had at the call site of the current function will be put on the DWARF stack. The value can be retrieved by finding the DW_TAG_GNU_call_site_parameter which has as DW_AT_abstract_origin the same formal parameter DIE. */ { Dwarf_Die param; if (dwarf_getlocation_die (attr, expr, ¶m) != 0) error (EXIT_FAILURE, 0, "dwarf_getlocation_die: %s", dwarf_errmsg (-1)); // XXX actually lookup DW_TAG_GNU_call_site_parameter printf ("%s[%" PRIx64 "]", opname, dwarf_dieoffset (¶m)); assert (expr->number == dwarf_cuoffset (¶m)); assert (dwarf_tag (¶m) == DW_TAG_formal_parameter); } break; case DW_OP_GNU_convert: case DW_OP_GNU_reinterpret: /* Special, unsigned CU relative DIE offset pointing to a DW_TAG_base_type. Pops a value, converts or reinterprets the value to the given type. When the argument is zero the value becomes untyped again. */ { Dwarf_Die type; Dwarf_Off off = expr->number; if (off != 0) { if (dwarf_getlocation_die (attr, expr, &type) != 0) error (EXIT_FAILURE, 0, "dwarf_getlocation_die: %s", dwarf_errmsg (-1)); off = dwarf_dieoffset (&type); assert (expr->number == dwarf_cuoffset (&type)); printf ("%s", opname); print_base_type (&type); } else printf ("%s[%" PRIu64 "]", opname, off); } break; case DW_OP_GNU_regval_type: /* Special, unsigned register number plus unsigned CU relative DIE offset pointing to a DW_TAG_base_type. */ { Dwarf_Die type; if (dwarf_getlocation_die (attr, expr, &type) != 0) error (EXIT_FAILURE, 0, "dwarf_getlocation_die: %s", dwarf_errmsg (-1)); assert (expr->number2 == dwarf_cuoffset (&type)); // XXX check size against base_type size? printf ("%s(reg%" PRIu64 ")", opname, expr->number); print_base_type (&type); } break; case DW_OP_GNU_deref_type: /* Special, unsigned size plus unsigned CU relative DIE offset pointing to a DW_TAG_base_type. */ { Dwarf_Die type; if (dwarf_getlocation_die (attr, expr, &type) != 0) error (EXIT_FAILURE, 0, "dwarf_getlocation_die: %s", dwarf_errmsg (-1)); assert (expr->number2 == dwarf_cuoffset (&type)); // XXX check size against base_type size? printf ("%s(%" PRIu64 ")", opname, expr->number); print_base_type (&type); } break; case DW_OP_GNU_const_type: /* Special, unsigned CU relative DIE offset pointing to a DW_TAG_base_type, an unsigned size length plus a block with the constant value. */ { Dwarf_Die type; if (dwarf_getlocation_die (attr, expr, &type) != 0) error (EXIT_FAILURE, 0, "dwarf_getlocation_die: %s", dwarf_errmsg (-1)); assert (expr->number == dwarf_cuoffset (&type)); Dwarf_Attribute const_attr; if (dwarf_getlocation_attr (attr, expr, &const_attr) != 0) error (EXIT_FAILURE, 0, "dwarf_getlocation_attr for type: %s", dwarf_errmsg (-1)); Dwarf_Block block; if (dwarf_formblock (&const_attr, &block) != 0) error (EXIT_FAILURE, 0, "dwarf_formblock for type: %s", dwarf_errmsg (-1)); printf ("%s", opname); print_base_type (&type); printf ("(%" PRIu64 ")[", block.length); for (size_t i = 0; i < block.length; i++) printf ("%02x", block.data[i]); printf("]"); } break; default: error (EXIT_FAILURE, 0, "unhandled opcode: DW_OP_%s (0x%x)", opname, atom); } }
static struct variable* analyze_variable(Dwarf_Die *die, Dwarf_Files *files, struct expr_context *ctx) { int ret; Dwarf_Attribute at; struct variable* var; /* ignore declarations */ if (dwarf_attr_integrate(die, DW_AT_declaration, &at) != NULL) { bool flag; ret = dwarf_formflag(&at, &flag); fail_if(ret == -1, "dwarf_formflag"); if (flag) return NULL; } var = xalloc(sizeof(struct variable)); analyze_name_location(die, files, &var->name, &var->loc); if (dwarf_attr_integrate(die, DW_AT_type, &at) != NULL) { Dwarf_Die type_die; if (dwarf_formref_die(&at, &type_die) == NULL) fail("dwarf_formref_die"); analyze_type(&type_die, &(var->type)); } if (dwarf_attr_integrate(die, DW_AT_const_value, &at) != NULL) { Dwarf_Word w; Dwarf_Block bl; unsigned int form = dwarf_whatform(&at); debug("variable %s has constant value of form %x", var->name, form); if (dwarf_formudata(&at, &w) == 0) { fail_if(sizeof(w) < var->type.width, "constant value too small"); var->value = xalloc(var->type.width); memcpy(var->value, &w, var->type.width); } else if (dwarf_formblock(&at, &bl) == 0) { fail_if(bl.length < var->type.width, "constant value too small"); var->value = xalloc(var->type.width); memcpy(var->value, bl.data, var->type.width); } else { warn("unable to get constant value of variable %x (form %x)", var->name, form); } } else if (dwarf_attr_integrate(die, DW_AT_location, &at) != NULL) { size_t exprlen; Dwarf_Op *expr; ret = dwarf_getlocation_addr(&at, ctx->ip, &expr, &exprlen, 1); if (ret != 1) { if (ret == -1) /* it seems that elfutils have some kind of problem with * DW_OP_GNU_entry_value but that operation is useless for us * anyway */ warn("cannot get location for variable %s (ip: %lx), %s", var->name, ctx->ip, dwarf_errmsg(-1)); else if (ret == 0) debug("no location available for variable %s (ip: %lx)", var->name, ctx->ip); else fail("unreachable reached"); return var; } var->value = evaluate_loc_expr(expr, exprlen, ctx, var->type.width); } return var; }