static void print_die (Dwarf_Die *die, const char *what, int indent) { Dwarf_Addr entrypc; const char *name = dwarf_diename (die) ?: "<unknown>"; if (dwarf_entrypc (die, &entrypc) == 0) printf ("%*s[%" PRIx64 "] %s '%s'@%" PRIx64 "\n", indent * 2, "", dwarf_dieoffset (die), what, name, entrypc); else printf ("%*s[%" PRIx64 "] %s '%s'\n", indent * 2, "", dwarf_dieoffset (die), what, name); }
static VALUE rd_die_new(rd_shared_data_t *sd, VALUE top, VALUE cu, Dwarf_Die die) { rd_die_t *rd_die; Dwarf_Off off; Dwarf_Half tag; Dwarf_Error err; VALUE obj; VALUE klass; chkerr1(dwarf_dieoffset(die, &off, &err), &err, Qnil); obj = rb_hash_aref(sd->off2die, LL2NUM(off)); if (!NIL_P(obj)) { return obj; } chkerr1(dwarf_tag(die, &tag, &err), &err, Qnil); klass = rb_hash_aref(rdwarf_tag2class, INT2FIX(tag)); if (NIL_P(klass)) { rb_raise(rb_eRuntimeError, "unknown tag %d\n", tag); } obj = rd_die_s_alloc(klass); rd_die = GetDie(obj); rd_die->shared_data = rd_shared_data_ref(sd); rd_die->die = die; rb_ivar_set(obj, id_at_top, top); rb_ivar_set(obj, id_at_cu, NIL_P(cu) ? obj : cu); rb_hash_aset(sd->off2die, LL2NUM(off), obj); return obj; }
static void _dwarf_die_offset(Dwarf_Die die) { Dwarf_Off rel_off, die_off, cu_off, cu_len; Dwarf_Error de; if (dwarf_die_CU_offset(die, &rel_off, &de) != DW_DLV_OK) { tet_printf("dwarf_die_CU_offset failed: %s\n", dwarf_errmsg(de)); result = TET_FAIL; } TS_CHECK_INT(rel_off); if (dwarf_die_CU_offset_range(die, &cu_off, &cu_len, &de) != DW_DLV_OK) { tet_printf("dwarf_die_CU_offset_range failed: %s\n", dwarf_errmsg(de)); result = TET_FAIL; } TS_CHECK_INT(cu_off); TS_CHECK_INT(cu_len); if (dwarf_dieoffset(die, &die_off, &de) != DW_DLV_OK) { tet_printf("dwarf_dieoffset failed: %s\n", dwarf_errmsg(de)); result = TET_FAIL; } TS_CHECK_INT(die_off); }
/* Just for debug purposes */ void print_sib_offset(Dwarf_Die sibling) { Dwarf_Off sib_off; Dwarf_Error error; dwarf_dieoffset(sibling,&sib_off,&error); fprintf(stderr," SIB OFF = 0x%" DW_PR_XZEROS DW_PR_DUx,sib_off); }
/* BASE must be a base type DIE referenced by a typed DWARF expression op. */ static void print_base_type (Dwarf_Die *base) { assert (dwarf_tag (base) == DW_TAG_base_type); Dwarf_Attribute encoding; Dwarf_Word enctype; if (dwarf_attr (base, DW_AT_encoding, &encoding) == NULL || dwarf_formudata (&encoding, &enctype) != 0) error (EXIT_FAILURE, 0, "base type without encoding"); Dwarf_Attribute bsize; Dwarf_Word bits; if (dwarf_attr (base, DW_AT_byte_size, &bsize) != NULL && dwarf_formudata (&bsize, &bits) == 0) bits *= 8; else if (dwarf_attr (base, DW_AT_bit_size, &bsize) == NULL || dwarf_formudata (&bsize, &bits) != 0) error (EXIT_FAILURE, 0, "base type without byte or bit size"); printf ("{%s,%s,%" PRIu64 "@[%" PRIx64 "]}", dwarf_diename (base), dwarf_encoding_string (enctype), bits, dwarf_dieoffset (base)); }
Dwarf_Off DwarfDie::offset() const { Dwarf_Off offset; const auto res = dwarf_dieoffset(m_die, &offset, nullptr); assert(res == DW_DLV_OK); return offset; }
static VALUE rd_die_die_offset(VALUE self) { rd_die_t *die = GetDie(self); Dwarf_Off off; Dwarf_Error err; chkerr1(dwarf_dieoffset(die->die, &off, &err), &err, self); return LL2NUM(off); }
static Dwarf_Off MC_dwarf_attr_dieoffset(Dwarf_Die * die, int attribute) { Dwarf_Attribute attr; if (dwarf_hasattr_integrate(die, attribute) == 0) return 0; dwarf_attr_integrate(die, attribute, &attr); Dwarf_Die subtype_die; if (dwarf_formref_die(&attr, &subtype_die) == nullptr) xbt_die("Could not find DIE"); return dwarf_dieoffset(&subtype_die); }
static Dwarf_Off DC_get_CU_start_offset(Dwarf_Die v){ Dwarf_Error error; Dwarf_Off dieOffset; dwarf_dieoffset(v,&dieOffset,&error); Dwarf_Off dieCUOffset; dwarf_die_CU_offset(v,&dieCUOffset,&error); Dwarf_Off CUStart = dieOffset - dieCUOffset; return CUStart; }
static Dwarf_Off die_off(dwarf_t *dw, Dwarf_Die die) { Dwarf_Off off; if (dwarf_dieoffset(die, &off, &dw->dw_err) == DW_DLV_OK) return (off); terminate("failed to get offset for die: %s\n", dwarf_errmsg(dw->dw_err)); /*NOTREACHED*/ return (0); }
Dwarf_Off DieHolder::get_offset(void) { if(!m_offset_used) { Dwarf_Error err = NULL; CHECK_DWERR(dwarf_dieoffset(m_die, &m_offset, &err), err, "cannot get DIE offset"); m_offset_used = true; } return m_offset; }
static void tp_dwarf_die_offset_sanity(void) { Dwarf_Debug dbg; Dwarf_Die die; Dwarf_Error de; Dwarf_Off rel_off, die_off, cu_off, cu_len; Dwarf_Unsigned cu_next_offset; int fd; result = TET_UNRESOLVED; TS_DWARF_INIT(dbg, fd, de); TS_DWARF_CU_FOREACH(dbg, cu_next_offset, de) { if (dwarf_siblingof(dbg, NULL, &die, &de) == DW_DLV_ERROR) { tet_printf("dwarf_siblingof failed: %s\n", dwarf_errmsg(de)); result = TET_FAIL; goto done; } if (dwarf_die_CU_offset(NULL, &rel_off, &de) != DW_DLV_ERROR) { tet_infoline("dwarf_die_CU_offset didn't return" " DW_DLV_ERROR when called with NULL arguments"); result = TET_FAIL; goto done; } if (dwarf_die_CU_offset_range(NULL, &cu_off, &cu_len, &de) != DW_DLV_ERROR) { tet_infoline("dwarf_die_CU_offset_range didn't return" " DW_DLV_ERROR when called with NULL arguments"); result = TET_FAIL; goto done; } if (dwarf_dieoffset(NULL, &die_off, &de) != DW_DLV_ERROR) { tet_infoline("dwarf_dieoffset didn't return DW_DLV_ERROR" " when called with NULL arguments"); result = TET_FAIL; goto done; } } if (result == TET_UNRESOLVED) result = TET_PASS; done: TS_DWARF_FINISH(dbg, de); TS_RESULT(result); }
static VALUE rd_die_inspect(VALUE self) { rd_die_t *die = GetDie(self); char *name; Dwarf_Off dieoff = 0; Dwarf_Error err; dwarf_dieoffset(die->die, &dieoff, &err); switch (dwarf_diename(die->die, &name, &err)) { case DW_DLV_OK: return rb_sprintf("#<%s:(%llu) '%s'>", rb_obj_classname(self), dieoff, name); default: return rb_sprintf("#<%s:(%llu)>", rb_obj_classname(self), dieoff); } }
static void print_vars (unsigned int indent, Dwarf_Die *die) { Dwarf_Die child; if (dwarf_child (die, &child) == 0) do switch (dwarf_tag (&child)) { case DW_TAG_variable: case DW_TAG_formal_parameter: printf ("%*s%-30s[%6" PRIx64 "]\n", indent, "", dwarf_diename (&child), (uint64_t) dwarf_dieoffset (&child)); break; default: break; } while (dwarf_siblingof (&child, &child) == 0); Dwarf_Attribute attr_mem; Dwarf_Die origin; if (dwarf_hasattr (die, DW_AT_abstract_origin) && dwarf_formref_die (dwarf_attr (die, DW_AT_abstract_origin, &attr_mem), &origin) != NULL && dwarf_child (&origin, &child) == 0) do switch (dwarf_tag (&child)) { case DW_TAG_variable: case DW_TAG_formal_parameter: printf ("%*s%s (abstract)\n", indent, "", dwarf_diename (&child)); break; default: break; } while (dwarf_siblingof (&child, &child) == 0); }
static void print_source_intro(Dwarf_Die cu_die) { int ores = 0; Dwarf_Off off = 0; ores = dwarf_dieoffset(cu_die, &off, &err); if (ores == DW_DLV_OK) { int lres = 0; const char *sec_name = 0; lres = dwarf_get_die_section_name_b(cu_die, &sec_name,&err); if (lres != DW_DLV_OK || !sec_name || !strlen(sec_name)) { sec_name = ".debug_info"; } printf("Source lines (from CU-DIE at %s offset 0x%" DW_PR_XZEROS DW_PR_DUx "):\n", sec_name, (Dwarf_Unsigned) off); } else { printf("Source lines (for the CU-DIE at unknown location):\n"); } }
static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname, struct perf_probe_arg_field *field, struct probe_trace_arg_ref **ref_ptr, Dwarf_Die *die_mem) { struct probe_trace_arg_ref *ref = *ref_ptr; Dwarf_Die type; Dwarf_Word offs; int ret, tag; pr_debug("converting %s in %s\n", field->name, varname); if (die_get_real_type(vr_die, &type) == NULL) { pr_warning("Failed to get the type of %s.\n", varname); return -ENOENT; } pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type)); tag = dwarf_tag(&type); if (field->name[0] == '[' && (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) { if (field->next) /* Save original type for next field */ memcpy(die_mem, &type, sizeof(*die_mem)); /* Get the type of this array */ if (die_get_real_type(&type, &type) == NULL) { pr_warning("Failed to get the type of %s.\n", varname); return -ENOENT; } pr_debug2("Array real type: (%x)\n", (unsigned)dwarf_dieoffset(&type)); if (tag == DW_TAG_pointer_type) { ref = zalloc(sizeof(struct probe_trace_arg_ref)); if (ref == NULL) return -ENOMEM; if (*ref_ptr) (*ref_ptr)->next = ref; else *ref_ptr = ref; } ref->offset += dwarf_bytesize(&type) * field->index; if (!field->next) /* Save vr_die for converting types */ memcpy(die_mem, vr_die, sizeof(*die_mem)); goto next; } else if (tag == DW_TAG_pointer_type) { /* Check the pointer and dereference */ if (!field->ref) { pr_err("Semantic error: %s must be referred by '->'\n", field->name); return -EINVAL; } /* Get the type pointed by this pointer */ if (die_get_real_type(&type, &type) == NULL) { pr_warning("Failed to get the type of %s.\n", varname); return -ENOENT; } /* Verify it is a data structure */ tag = dwarf_tag(&type); if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) { pr_warning("%s is not a data structure nor an union.\n", varname); return -EINVAL; } ref = zalloc(sizeof(struct probe_trace_arg_ref)); if (ref == NULL) return -ENOMEM; if (*ref_ptr) (*ref_ptr)->next = ref; else *ref_ptr = ref; } else { /* Verify it is a data structure */ if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) { pr_warning("%s is not a data structure nor an union.\n", varname); return -EINVAL; } if (field->name[0] == '[') { pr_err("Semantic error: %s is not a pointer" " nor array.\n", varname); return -EINVAL; } /* While prcessing unnamed field, we don't care about this */ if (field->ref && dwarf_diename(vr_die)) { pr_err("Semantic error: %s must be referred by '.'\n", field->name); return -EINVAL; } if (!ref) { pr_warning("Structure on a register is not " "supported yet.\n"); return -ENOTSUP; } } if (die_find_member(&type, field->name, die_mem) == NULL) { pr_warning("%s(type:%s) has no member %s.\n", varname, dwarf_diename(&type), field->name); return -EINVAL; } /* Get the offset of the field */ if (tag == DW_TAG_union_type) { offs = 0; } else { ret = die_get_data_member_location(die_mem, &offs); if (ret < 0) { pr_warning("Failed to get the offset of %s.\n", field->name); return ret; } } ref->offset += (long)offs; /* If this member is unnamed, we need to reuse this field */ if (!dwarf_diename(die_mem)) return convert_variable_fields(die_mem, varname, field, &ref, die_mem); next: /* Converting next field */ if (field->next) return convert_variable_fields(die_mem, field->name, field->next, &ref, die_mem); else return 0; }
/* Note that the function: * - updates scopes and nscopes if the currently analyzed scopes corresponded * to an inline function * - frees scopes if currently analyzed scope was not-inlined function */ static struct frame* analyze_scopes(Dwarf_Die **scopes, int *nscopes, struct expr_context *ctx, Dwarf_Files *files, bool skip_first) { int i; Dwarf_Die *scope_die; if (*nscopes == -1) { warn("\t\tfailed to get scopes"); return NULL; } else if (*nscopes == 0) { debug("\t\tno scopes"); return NULL; } else if (*nscopes > 0) { debug("\t\tscopes:"); for (i = 0; i < *nscopes; i++) { debug("\t\t\ttag: 0x%x\toffset: %lx", dwarf_tag(&(*scopes)[i]), dwarf_dieoffset(&(*scopes)[i])); } } int tag; bool boundary = false; struct frame *frame = xalloc(sizeof(*frame)); /* iterate and extract variable until we reach function boundary */ i = (skip_first ? 1 : 0); for (; i < *nscopes; i++) { scope_die = &(*scopes)[i]; list_append(frame->vars, frame->vars_tail, child_variables(scope_die, files, ctx, false)); tag = dwarf_tag(scope_die); boundary = (tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine); if (boundary) { /* get function parameters */ list_append(frame->params, frame->params_tail, child_variables(scope_die, files, ctx, true)); /* add function name to frame struct */ analyze_name_location(scope_die, files, &frame->name, &frame->loc); info("\t\tfunction name: %s", frame->name); break; } } fail_if(!boundary, "missing function boundary"); /* we have to make a copy of the *scope_die as the pointer points inside * scopes[] which we want to free */ Dwarf_Die tmp = *scope_die; free(*scopes); /* if this function is not inlined, do not update the scopes array as we * don't want to continue further */ if (tag == DW_TAG_subprogram) { *nscopes = 0; return frame; } /* otherwise, get scopes for the inlined function, i.e. function into which * it was inlined */ *nscopes = dwarf_getscopes_die(&tmp, scopes); return frame; }
static void MC_dwarf_handle_scope_die(simgrid::mc::ObjectInformation* info, Dwarf_Die * die, Dwarf_Die * unit, simgrid::mc::Frame* parent_frame, const char *ns) { // TODO, handle DW_TAG_type/DW_TAG_location for DW_TAG_with_stmt int tag = dwarf_tag(die); simgrid::dwarf::TagClass klass = simgrid::dwarf::classify_tag(tag); // (Template) Subprogram declaration: if (klass == simgrid::dwarf::TagClass::Subprogram && MC_dwarf_attr_flag(die, DW_AT_declaration, false)) return; if (klass == simgrid::dwarf::TagClass::Scope) xbt_assert(parent_frame, "No parent scope for this scope"); simgrid::mc::Frame frame; frame.tag = tag; frame.id = dwarf_dieoffset(die); frame.object_info = info; if (klass == simgrid::dwarf::TagClass::Subprogram) { const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name); if (name && ns) frame.name = std::string(ns) + "::" + name; else if (name) frame.name = name; } frame.abstract_origin_id = MC_dwarf_attr_dieoffset(die, DW_AT_abstract_origin); // This is the base address for DWARF addresses. // Relocated addresses are offset from this base address. // See DWARF4 spec 7.5 std::uint64_t base = (std::uint64_t) info->base_address(); // TODO, support DW_AT_ranges uint64_t low_pc = MC_dwarf_attr_integrate_addr(die, DW_AT_low_pc); frame.range.begin() = low_pc ? (std::uint64_t) base + low_pc : 0; if (low_pc) { // DW_AT_high_pc: Dwarf_Attribute attr; if (not dwarf_attr_integrate(die, DW_AT_high_pc, &attr)) xbt_die("Missing DW_AT_high_pc matching with DW_AT_low_pc"); Dwarf_Sword offset; Dwarf_Addr high_pc; switch (simgrid::dwarf::classify_form(dwarf_whatform(&attr))) { // DW_AT_high_pc if an offset from the low_pc: case simgrid::dwarf::FormClass::Constant: if (dwarf_formsdata(&attr, &offset) != 0) xbt_die("Could not read constant"); frame.range.end() = frame.range.begin() + offset; break; // DW_AT_high_pc is a relocatable address: case simgrid::dwarf::FormClass::Address: if (dwarf_formaddr(&attr, &high_pc) != 0) xbt_die("Could not read address"); frame.range.end() = base + high_pc; break; default: xbt_die("Unexpected class for DW_AT_high_pc"); } } if (klass == simgrid::dwarf::TagClass::Subprogram) { Dwarf_Attribute attr_frame_base; if (dwarf_attr_integrate(die, DW_AT_frame_base, &attr_frame_base)) frame.frame_base_location = simgrid::dwarf::location_list(*info, attr_frame_base); } // Handle children: MC_dwarf_handle_children(info, die, unit, &frame, ns); // We sort them in order to have an (somewhat) efficient by name // lookup: boost::range::sort(frame.variables, MC_compare_variable); // Register it: if (klass == simgrid::dwarf::TagClass::Subprogram) info->subprograms[frame.id] = std::move(frame); else if (klass == simgrid::dwarf::TagClass::Scope) parent_frame->scopes.push_back(std::move(frame)); }
static std::unique_ptr<simgrid::mc::Variable> MC_die_to_variable( simgrid::mc::ObjectInformation* info, Dwarf_Die * die, Dwarf_Die * unit, simgrid::mc::Frame* frame, const char *ns) { // Skip declarations: if (MC_dwarf_attr_flag(die, DW_AT_declaration, false)) return nullptr; // Skip compile time constants: if (dwarf_hasattr(die, DW_AT_const_value)) return nullptr; Dwarf_Attribute attr_location; if (dwarf_attr(die, DW_AT_location, &attr_location) == nullptr) // No location: do not add it ? return nullptr; std::unique_ptr<simgrid::mc::Variable> variable = std::unique_ptr<simgrid::mc::Variable>(new simgrid::mc::Variable()); variable->id = dwarf_dieoffset(die); variable->global = frame == nullptr; // Can be override base on DW_AT_location variable->object_info = info; const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name); if (name) variable->name = name; variable->type_id = MC_dwarf_at_type(die); int form = dwarf_whatform(&attr_location); simgrid::dwarf::FormClass form_class; if (form == DW_FORM_sec_offset) form_class = simgrid::dwarf::FormClass::Constant; else form_class = simgrid::dwarf::classify_form(form); switch (form_class) { case simgrid::dwarf::FormClass::ExprLoc: case simgrid::dwarf::FormClass::Block: // Location expression: { Dwarf_Op *expr; size_t len; if (dwarf_getlocation(&attr_location, &expr, &len)) { xbt_die( "Could not read location expression in DW_AT_location " "of variable <%" PRIx64 ">%s", (uint64_t) variable->id, variable->name.c_str()); } if (len == 1 && expr[0].atom == DW_OP_addr) { variable->global = true; uintptr_t offset = (uintptr_t) expr[0].number; uintptr_t base = (uintptr_t) info->base_address(); variable->address = (void *) (base + offset); } else variable->location_list = { simgrid::dwarf::LocationListEntry(simgrid::dwarf::DwarfExpression(expr, expr + len))}; break; } case simgrid::dwarf::FormClass::LocListPtr: case simgrid::dwarf::FormClass::Constant: // Reference to location list: variable->location_list = simgrid::dwarf::location_list( *info, attr_location); break; default: xbt_die("Unexpected form 0x%x (%i), class 0x%x (%i) list for location in <%" PRIx64 ">%s", (unsigned)form, form, (unsigned)form_class, (int)form_class, (uint64_t)variable->id, variable->name.c_str()); } // Handle start_scope: if (dwarf_hasattr(die, DW_AT_start_scope)) { Dwarf_Attribute attr; dwarf_attr(die, DW_AT_start_scope, &attr); int form = dwarf_whatform(&attr); simgrid::dwarf::FormClass form_class = simgrid::dwarf::classify_form(form); if (form_class == simgrid::dwarf::FormClass::Constant) { Dwarf_Word value; variable->start_scope = dwarf_formudata(&attr, &value) == 0 ? (size_t)value : 0; } else { // TODO: FormClass::RangeListPtr xbt_die("Unhandled form 0x%x, class 0x%X for DW_AT_start_scope of variable %s", (unsigned)form, (unsigned)form_class, name == nullptr ? "?" : name); } } if (ns && variable->global) variable->name = std::string(ns) + "::" + variable->name; // The current code needs a variable name, // generate a fake one: if (variable->name.empty()) { variable->name = "@anonymous#" + std::to_string(mc_anonymous_variable_index); mc_anonymous_variable_index++; } return variable; }
/** \brief Create a MC type object from a DIE * * \param info current object info object * \param die DIE (for a given type) * \param unit compilation unit of the current DIE * \return MC representation of the type */ static simgrid::mc::Type MC_dwarf_die_to_type( simgrid::mc::ObjectInformation* info, Dwarf_Die * die, Dwarf_Die * unit, simgrid::mc::Frame* frame, const char *ns) { simgrid::mc::Type type; type.type = dwarf_tag(die); type.name = std::string(); type.element_count = -1; // Global Offset type.id = dwarf_dieoffset(die); const char *prefix = ""; switch (type.type) { case DW_TAG_structure_type: prefix = "struct "; break; case DW_TAG_union_type: prefix = "union "; break; case DW_TAG_class_type: prefix = "class "; break; default: prefix = ""; } const char *name = MC_dwarf_attr_integrate_string(die, DW_AT_name); if (name != nullptr) { if (ns) type.name = simgrid::xbt::string_printf("%s%s::%s", prefix, ns, name); else type.name = simgrid::xbt::string_printf("%s%s", prefix, name); } type.type_id = MC_dwarf_at_type(die); // Some compilers do not emit DW_AT_byte_size for pointer_type, // so we fill this. We currently assume that the model-checked process is in // the same architecture.. if (type.type == DW_TAG_pointer_type) type.byte_size = sizeof(void*); // Computation of the byte_size if (dwarf_hasattr_integrate(die, DW_AT_byte_size)) type.byte_size = MC_dwarf_attr_integrate_uint(die, DW_AT_byte_size, 0); else if (type.type == DW_TAG_array_type || type.type == DW_TAG_structure_type || type.type == DW_TAG_class_type) { Dwarf_Word size; if (dwarf_aggregate_size(die, &size) == 0) type.byte_size = size; } switch (type.type) { case DW_TAG_array_type: type.element_count = MC_dwarf_array_element_count(die, unit); // TODO, handle DW_byte_stride and (not) DW_bit_stride break; case DW_TAG_pointer_type: case DW_TAG_reference_type: case DW_TAG_rvalue_reference_type: break; case DW_TAG_structure_type: case DW_TAG_union_type: case DW_TAG_class_type: MC_dwarf_add_members(info, die, unit, &type); MC_dwarf_handle_children(info, die, unit, frame, ns ? simgrid::xbt::string_printf("%s::%s", ns, name).c_str() : type.name.c_str()); break; default: XBT_DEBUG("Unhandled type: %d (%s)", type.type, simgrid::dwarf::tagname(type.type)); break; } return type; }
static void print_die_data(Dwarf_Debug dbg, Dwarf_Die print_me,int level, struct srcfilesdata *sf) { char *name = 0; Dwarf_Error error = 0; Dwarf_Half tag = 0; const char *tagname = 0; int localname = 0; struct attributes attr; int res = dwarf_diename(print_me,&name,&error); if(res == DW_DLV_ERROR) { printf("Error in dwarf_diename , level %d \n",level); exit(1); } if(res == DW_DLV_NO_ENTRY) { name = "<no DW_AT_name attr>"; localname = 1; } res = dwarf_tag(print_me,&tag,&error); if(res != DW_DLV_OK) { printf("Error in dwarf_tag , level %d \n",level); exit(1); } res = dwarf_get_TAG_name(tag,&tagname); if(res != DW_DLV_OK) { printf("Error in dwarf_get_TAG_name , level %d \n",level); exit(1); } if (level == 1) { if (tag == DW_TAG_structure_type || tag == DW_TAG_base_type ||(tag==DW_TAG_array_type)|| (tag == DW_TAG_pointer_type) || (tag == DW_TAG_typedef) || (tag==DW_TAG_variable)) { curr_top_tag = DW_TAG_structure_type; } else { curr_top_tag = 0; } } if (curr_top_tag != 0) { Dwarf_Off offset = 0; dwarf_dieoffset(print_me, &offset, &error); if (level > 1) { printf(" "); } count_data_types++; attr.size =0; attr.type_offset =0; attr.filename = 0; attr.member_location = 0; if (get_attributes(dbg, print_me, level, sf, &attr)==1){ printf("<%d> tag: %d %s name: \"%s\" offset:%x ", level, tag, tagname, name, offset); if (attr.type_offset != 0){ printf(" type offset: %x ",attr.type_offset); } if (attr.size != 0){ printf("size : %d ",attr.size); } if (attr.filename != 0){ printf(" filename: %s ",attr.filename); } printf("\n"); db_entries[db_count].offset = offset; db_entries[db_count].type_offset = attr.type_offset; db_entries[db_count].tag = tag; db_entries[db_count].level = level; db_entries[db_count].size = attr.size; db_entries[db_count].member_location = attr.member_location; strncpy(db_entries[db_count].name,name,MAX_NAME-1); db_count++; }else{ // printf("Decleartion <%d> tag: %d %s name: \"%s\" offset:%x \n", level, tag, tagname, // name, offset); } } if (!localname) { dwarf_dealloc(dbg, name, DW_DLA_STRING); } }
void handle (Dwarf *dbg, Dwarf_Die *die, int n) { Dwarf_Die child; unsigned int tag; const char *str; char buf[30]; const char *name; Dwarf_Off off; Dwarf_Off cuoff; size_t cnt; Dwarf_Addr addr; int i; tag = dwarf_tag (die); if (tag != DW_TAG_invalid) { if (tag < ntagnames) str = tagnames[tag]; else { snprintf (buf, sizeof buf, "%#x", tag); str = buf; } } else str = "* NO TAG *"; name = dwarf_diename (die); if (name == 0) name = "* NO NAME *"; off = dwarf_dieoffset (die); cuoff = dwarf_cuoffset (die); printf ("%*s%s\n", n * 5, "", str); printf ("%*s Name : %s\n", n * 5, "", name); printf ("%*s Offset : %lld\n", n * 5, "", (long long int) off); printf ("%*s CU offset : %lld\n", n * 5, "", (long long int) cuoff); printf ("%*s Attrs :", n * 5, ""); for (cnt = 0; cnt < nattrs; ++cnt) if (dwarf_hasattr (die, attrs[cnt].code)) printf (" %s", attrs[cnt].name); puts (""); if (dwarf_hasattr (die, DW_AT_low_pc) && dwarf_lowpc (die, &addr) == 0) { Dwarf_Attribute attr; Dwarf_Addr addr2; printf ("%*s low PC : %#llx\n", n * 5, "", (unsigned long long int) addr); if (dwarf_attr (die, DW_AT_low_pc, &attr) == NULL || dwarf_formaddr (&attr, &addr2) != 0 || addr != addr2) puts ("************* DW_AT_low_pc verify failed ************"); else if (! dwarf_hasform (&attr, DW_FORM_addr)) puts ("************* DW_AT_low_pc form failed ************"); else if (dwarf_whatform (&attr) != DW_FORM_addr) puts ("************* DW_AT_low_pc form (2) failed ************"); else if (dwarf_whatattr (&attr) != DW_AT_low_pc) puts ("************* DW_AT_low_pc attr failed ************"); } if (dwarf_hasattr (die, DW_AT_high_pc) && dwarf_highpc (die, &addr) == 0) { Dwarf_Attribute attr; Dwarf_Addr addr2; printf ("%*s high PC : %#llx\n", n * 5, "", (unsigned long long int) addr); if (dwarf_attr (die, DW_AT_high_pc, &attr) == NULL || dwarf_formaddr (&attr, &addr2) != 0 || addr != addr2) puts ("************* DW_AT_high_pc verify failed ************"); else if (! dwarf_hasform (&attr, DW_FORM_addr)) puts ("************* DW_AT_high_pc form failed ************"); else if (dwarf_whatform (&attr) != DW_FORM_addr) puts ("************* DW_AT_high_pc form (2) failed ************"); else if (dwarf_whatattr (&attr) != DW_AT_high_pc) puts ("************* DW_AT_high_pc attr failed ************"); } if (dwarf_hasattr (die, DW_AT_byte_size) && (i = dwarf_bytesize (die)) != -1) { Dwarf_Attribute attr; Dwarf_Word u2; unsigned int u; printf ("%*s byte size : %d\n", n * 5, "", i); if (dwarf_attr (die, DW_AT_byte_size, &attr) == NULL || dwarf_formudata (&attr, &u2) != 0 || i != (int) u2) puts ("************* DW_AT_byte_size verify failed ************"); else if (! dwarf_hasform (&attr, DW_FORM_data1) && ! dwarf_hasform (&attr, DW_FORM_data2) && ! dwarf_hasform (&attr, DW_FORM_data4) && ! dwarf_hasform (&attr, DW_FORM_data8) && ! dwarf_hasform (&attr, DW_FORM_sdata) && ! dwarf_hasform (&attr, DW_FORM_udata)) puts ("************* DW_AT_byte_size form failed ************"); else if ((u = dwarf_whatform (&attr)) == 0 || (u != DW_FORM_data1 && u != DW_FORM_data2 && u != DW_FORM_data4 && u != DW_FORM_data8 && u != DW_FORM_sdata && u != DW_FORM_udata)) puts ("************* DW_AT_byte_size form (2) failed ************"); else if (dwarf_whatattr (&attr) != DW_AT_byte_size) puts ("************* DW_AT_byte_size attr failed ************"); } if (dwarf_hasattr (die, DW_AT_bit_size) && (i = dwarf_bitsize (die)) != -1) { Dwarf_Attribute attr; Dwarf_Word u2; unsigned int u; printf ("%*s bit size : %d\n", n * 5, "", i); if (dwarf_attr (die, DW_AT_bit_size, &attr) == NULL || dwarf_formudata (&attr, &u2) != 0 || i != (int) u2) puts ("************* DW_AT_bit_size test failed ************"); else if (! dwarf_hasform (&attr, DW_FORM_data1) && ! dwarf_hasform (&attr, DW_FORM_data2) && ! dwarf_hasform (&attr, DW_FORM_data4) && ! dwarf_hasform (&attr, DW_FORM_data8) && ! dwarf_hasform (&attr, DW_FORM_sdata) && ! dwarf_hasform (&attr, DW_FORM_udata)) puts ("************* DW_AT_bit_size form failed ************"); else if ((u = dwarf_whatform (&attr)) == 0 || (u != DW_FORM_data1 && u != DW_FORM_data2 && u != DW_FORM_data4 && u != DW_FORM_data8 && u != DW_FORM_sdata && u != DW_FORM_udata)) puts ("************* DW_AT_bit_size form (2) failed ************"); else if (dwarf_whatattr (&attr) != DW_AT_bit_size) puts ("************* DW_AT_bit_size attr failed ************"); } if (dwarf_hasattr (die, DW_AT_bit_offset) && (i = dwarf_bitoffset (die)) != -1) { Dwarf_Attribute attr; Dwarf_Word u2; unsigned int u; printf ("%*s bit offset: %d\n", n * 5, "", i); if (dwarf_attr (die, DW_AT_bit_offset, &attr) == NULL || dwarf_formudata (&attr, &u2) != 0 || i != (int) u2) puts ("************* DW_AT_bit_offset test failed ************"); else if (! dwarf_hasform (&attr, DW_FORM_data1) && ! dwarf_hasform (&attr, DW_FORM_data2) && ! dwarf_hasform (&attr, DW_FORM_data4) && ! dwarf_hasform (&attr, DW_FORM_data8) && ! dwarf_hasform (&attr, DW_FORM_sdata) && ! dwarf_hasform (&attr, DW_FORM_udata)) puts ("************* DW_AT_bit_offset form failed ************"); else if ((u = dwarf_whatform (&attr)) == 0 || (u != DW_FORM_data1 && u != DW_FORM_data2 && u != DW_FORM_data4 && u != DW_FORM_data8 && u != DW_FORM_sdata && u != DW_FORM_udata)) puts ("************* DW_AT_bit_offset form (2) failed ************"); else if (dwarf_whatattr (&attr) != DW_AT_bit_offset) puts ("************* DW_AT_bit_offset attr failed ************"); } if (dwarf_hasattr (die, DW_AT_language) && (i = dwarf_srclang (die)) != -1) { Dwarf_Attribute attr; Dwarf_Word u2; unsigned int u; printf ("%*s language : %d\n", n * 5, "", i); if (dwarf_attr (die, DW_AT_language, &attr) == NULL || dwarf_formudata (&attr, &u2) != 0 || i != (int) u2) puts ("************* DW_AT_language test failed ************"); else if (! dwarf_hasform (&attr, DW_FORM_data1) && ! dwarf_hasform (&attr, DW_FORM_data2) && ! dwarf_hasform (&attr, DW_FORM_data4) && ! dwarf_hasform (&attr, DW_FORM_data8) && ! dwarf_hasform (&attr, DW_FORM_sdata) && ! dwarf_hasform (&attr, DW_FORM_udata)) puts ("************* DW_AT_language form failed ************"); else if ((u = dwarf_whatform (&attr)) == 0 || (u != DW_FORM_data1 && u != DW_FORM_data2 && u != DW_FORM_data4 && u != DW_FORM_data8 && u != DW_FORM_sdata && u != DW_FORM_udata)) puts ("************* DW_AT_language form (2) failed ************"); else if (dwarf_whatattr (&attr) != DW_AT_language) puts ("************* DW_AT_language attr failed ************"); } if (dwarf_hasattr (die, DW_AT_ordering) && (i = dwarf_arrayorder (die)) != -1) { Dwarf_Attribute attr; Dwarf_Word u2; unsigned int u; printf ("%*s ordering : %d\n", n * 5, "", i); if (dwarf_attr (die, DW_AT_ordering, &attr) == NULL || dwarf_formudata (&attr, &u2) != 0 || i != (int) u2) puts ("************* DW_AT_ordering test failed ************"); else if (! dwarf_hasform (&attr, DW_FORM_data1) && ! dwarf_hasform (&attr, DW_FORM_data2) && ! dwarf_hasform (&attr, DW_FORM_data4) && ! dwarf_hasform (&attr, DW_FORM_data8) && ! dwarf_hasform (&attr, DW_FORM_sdata) && ! dwarf_hasform (&attr, DW_FORM_udata)) puts ("************* DW_AT_ordering failed ************"); else if ((u = dwarf_whatform (&attr)) == 0 || (u != DW_FORM_data1 && u != DW_FORM_data2 && u != DW_FORM_data4 && u != DW_FORM_data8 && u != DW_FORM_sdata && u != DW_FORM_udata)) puts ("************* DW_AT_ordering form (2) failed ************"); else if (dwarf_whatattr (&attr) != DW_AT_ordering) puts ("************* DW_AT_ordering attr failed ************"); } if (dwarf_hasattr (die, DW_AT_comp_dir)) { Dwarf_Attribute attr; if (dwarf_attr (die, DW_AT_comp_dir, &attr) == NULL || (name = dwarf_formstring (&attr)) == NULL) puts ("************* DW_AT_comp_dir attr failed ************"); else printf ("%*s directory : %s\n", n * 5, "", name); } if (dwarf_hasattr (die, DW_AT_producer)) { Dwarf_Attribute attr; if (dwarf_attr (die, DW_AT_producer, &attr) == NULL || (name = dwarf_formstring (&attr)) == NULL) puts ("************* DW_AT_comp_dir attr failed ************"); else printf ("%*s producer : %s\n", n * 5, "", name); } if (dwarf_haschildren (die) != 0 && dwarf_child (die, &child) == 0) handle (dbg, &child, n + 1); if (dwarf_siblingof (die, die) == 0) handle (dbg, die, n); }
/* Return DW_DLV_OK, or DW_DLV_ERROR Handle the addrs in a single die. */ static int process_this_die_attrs(Dwarf_Debug dbg, Dwarf_Die newdie, int *errval) { Dwarf_Error err; Dwarf_Half i; Dwarf_Half newattrnum; int res; int tres; Dwarf_Half ltag; Dwarf_Off doff; int doffres = dwarf_dieoffset(newdie, &doff, &err); if (doffres != DW_DLV_OK) { if (doffres == DW_DLV_ERROR) { *errval = (int) dwarf_errno(err); } return doffres; } tres = dwarf_tag(newdie, <ag, &err); if (tres != DW_DLV_OK) { return tres; } if (DW_TAG_compile_unit == ltag) { /* because of the way the dwarf_line code works, we ** do lines only per compile unit. ** This may turn out to be wrong if we have lines ** left unconnected to a CU. ** of course such lines will not, at present, be ** used by gnome ** This is not ideal as coded due to the dwarf_line.c issue. */ int lres; lres = handle_debug_line(dbg, newdie, send_addr_note, errval); if (lres == DW_DLV_ERROR) { return lres; } } for (i = 0; i < sizeof(might_have_addr) / sizeof(int); i++) { int resattr; Dwarf_Bool hasattr; newattrnum = might_have_addr[i]; err = 0; resattr = dwarf_hasattr(newdie, newattrnum, &hasattr, &err); if (DW_DLV_OK == resattr) { if (hasattr) { res = handle_attr_addr(dbg, newdie, newattrnum, &err); if (res != DW_DLV_OK) { *errval = (int) dwarf_errno(err); return DW_DLV_ERROR; } } } else { if (resattr == DW_DLV_ERROR) { *errval = (int) dwarf_errno(err); return resattr; } } } for (i = 0; i < sizeof(might_have_locdesc) / sizeof(int); i++) { int resattr; Dwarf_Bool hasattr; newattrnum = might_have_locdesc[i]; err = 0; resattr = dwarf_hasattr(newdie, newattrnum, &hasattr, &err); if (DW_DLV_OK == resattr) { if (hasattr) { res = handle_attr_locdesc(dbg, newdie, newattrnum, &err); if (res != DW_DLV_OK) { *errval = (int) dwarf_errno(err); return DW_DLV_ERROR; } } } else { if (resattr == DW_DLV_ERROR) { *errval = (int) dwarf_errno(err); return resattr; } } } return DW_DLV_OK; }
int main (int argc, char *argv[]) { int cnt; Dwfl *dwfl = NULL; (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &cnt, &dwfl); assert (dwfl != NULL); Dwarf_Die *cu = NULL; Dwarf_Addr bias; do { cu = dwfl_nextcu (dwfl, cu, &bias); if (cu != NULL) { Dwfl_Module *mod = dwfl_cumodule (cu); const char *modname = (dwfl_module_info (mod, NULL, NULL, NULL, NULL, NULL, NULL, NULL) ?: "<unknown>"); const char *cuname = (dwarf_diename (cu) ?: "<unknown>"); printf ("mod: %s CU: [%" PRIx64 "] %s\n", modname, dwarf_dieoffset (cu), cuname); size_t lines; if (dwfl_getsrclines (cu, &lines) != 0) continue; // No lines... for (size_t i = 0; i < lines; i++) { Dwfl_Line *line = dwfl_onesrcline (cu, i); Dwarf_Addr addr; int lineno; int colno; Dwarf_Word mtime; Dwarf_Word length; const char *src = dwfl_lineinfo (line, &addr, &lineno, &colno, &mtime, &length); Dwarf_Addr dw_bias; Dwarf_Line *dw_line = dwfl_dwarf_line (line, &dw_bias); assert (bias == dw_bias); Dwarf_Addr dw_addr; if (dwarf_lineaddr (dw_line, &dw_addr) != 0) error (EXIT_FAILURE, 0, "dwarf_lineaddr: %s", dwarf_errmsg (-1)); assert (addr == dw_addr + dw_bias); unsigned int dw_op_index; if (dwarf_lineop_index (dw_line, &dw_op_index) != 0) error (EXIT_FAILURE, 0, "dwarf_lineop_index: %s", dwarf_errmsg (-1)); int dw_lineno; if (dwarf_lineno (dw_line, &dw_lineno) != 0) error (EXIT_FAILURE, 0, "dwarf_lineno: %s", dwarf_errmsg (-1)); assert (lineno == dw_lineno); int dw_colno; if (dwarf_linecol (dw_line, &dw_colno) != 0) error (EXIT_FAILURE, 0, "dwarf_lineno: %s", dwarf_errmsg (-1)); assert (colno == dw_colno); bool begin; if (dwarf_linebeginstatement (dw_line, &begin) != 0) error (EXIT_FAILURE, 0, "dwarf_linebeginstatement: %s", dwarf_errmsg (-1)); bool end; if (dwarf_lineendsequence (dw_line, &end) != 0) error (EXIT_FAILURE, 0, "dwarf_lineendsequence: %s", dwarf_errmsg (-1)); bool pend; if (dwarf_lineprologueend (dw_line, &pend) != 0) error (EXIT_FAILURE, 0, "dwarf_lineprologueend: %s", dwarf_errmsg (-1)); bool ebegin; if (dwarf_lineepiloguebegin (dw_line, &ebegin) != 0) error (EXIT_FAILURE, 0, "dwarf_lineepiloguebegin: %s", dwarf_errmsg (-1)); bool block; if (dwarf_lineblock (dw_line, &block) != 0) error (EXIT_FAILURE, 0, "dwarf_lineblock: %s", dwarf_errmsg (-1)); unsigned int isa; if (dwarf_lineisa (dw_line, &isa) != 0) error (EXIT_FAILURE, 0, "dwarf_lineisa: %s", dwarf_errmsg (-1)); unsigned int disc; if (dwarf_linediscriminator (dw_line, &disc) != 0) error (EXIT_FAILURE, 0, "dwarf_linediscriminator: %s", dwarf_errmsg (-1)); const char *dw_src; Dwarf_Word dw_mtime; Dwarf_Word dw_length; dw_src = dwarf_linesrc (dw_line, &dw_mtime, &dw_length); assert (strcmp (src, dw_src) == 0); assert (mtime == dw_mtime); assert (length == dw_length); printf ("%zd %#" PRIx64 " %s:%d:%d\n" " time: %#" PRIX64 ", len: %" PRIu64 ", idx: %d, b: %d, e: %d" ", pe: %d, eb: %d, block: %d" ", isa: %d, disc: %d\n", i, addr, src, lineno, colno, mtime, length, dw_op_index, begin, end, pend, ebegin, block, isa, disc); Dwarf_Die *linecu = dwfl_linecu (line); assert (cu == linecu); Dwfl_Module *linemod = dwfl_linemodule (line); assert (mod == linemod); } } } while (cu != NULL); dwfl_end (dwfl); return 0; }
static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname, struct perf_probe_arg_field *field, struct probe_trace_arg_ref **ref_ptr, Dwarf_Die *die_mem) { struct probe_trace_arg_ref *ref = *ref_ptr; Dwarf_Die type; Dwarf_Word offs; int ret, tag; pr_debug("converting %s in %s\n", field->name, varname); if (die_get_real_type(vr_die, &type) == NULL) { pr_warning("Failed to get the type of %s.\n", varname); return -ENOENT; } pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type)); tag = dwarf_tag(&type); if (field->name[0] == '[' && (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) { if (field->next) memcpy(die_mem, &type, sizeof(*die_mem)); if (die_get_real_type(&type, &type) == NULL) { pr_warning("Failed to get the type of %s.\n", varname); return -ENOENT; } pr_debug2("Array real type: (%x)\n", (unsigned)dwarf_dieoffset(&type)); if (tag == DW_TAG_pointer_type) { ref = zalloc(sizeof(struct probe_trace_arg_ref)); if (ref == NULL) return -ENOMEM; if (*ref_ptr) (*ref_ptr)->next = ref; else *ref_ptr = ref; } ref->offset += dwarf_bytesize(&type) * field->index; if (!field->next) memcpy(die_mem, vr_die, sizeof(*die_mem)); goto next; } else if (tag == DW_TAG_pointer_type) { if (!field->ref) { pr_err("Semantic error: %s must be referred by '->'\n", field->name); return -EINVAL; } if (die_get_real_type(&type, &type) == NULL) { pr_warning("Failed to get the type of %s.\n", varname); return -ENOENT; } if (dwarf_tag(&type) != DW_TAG_structure_type) { pr_warning("%s is not a data structure.\n", varname); return -EINVAL; } ref = zalloc(sizeof(struct probe_trace_arg_ref)); if (ref == NULL) return -ENOMEM; if (*ref_ptr) (*ref_ptr)->next = ref; else *ref_ptr = ref; } else { if (tag != DW_TAG_structure_type) { pr_warning("%s is not a data structure.\n", varname); return -EINVAL; } if (field->name[0] == '[') { pr_err("Semantic error: %s is not a pointor" " nor array.\n", varname); return -EINVAL; } if (field->ref) { pr_err("Semantic error: %s must be referred by '.'\n", field->name); return -EINVAL; } if (!ref) { pr_warning("Structure on a register is not " "supported yet.\n"); return -ENOTSUP; } } if (die_find_member(&type, field->name, die_mem) == NULL) { pr_warning("%s(tyep:%s) has no member %s.\n", varname, dwarf_diename(&type), field->name); return -EINVAL; } ret = die_get_data_member_location(die_mem, &offs); if (ret < 0) { pr_warning("Failed to get the offset of %s.\n", field->name); return ret; } ref->offset += (long)offs; next: if (field->next) return convert_variable_fields(die_mem, field->name, field->next, &ref, die_mem); else return 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); } }