| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| struct args |
| { |
| Dwarf_Addr pc; |
| Dwarf_Die *scopes; |
| unsigned int inlined, nscopes; |
| Dwarf_Die inlined_origin; |
| }; |
| |
| |
| static int |
| pc_match (unsigned int depth, struct Dwarf_Die_Chain *die, void *arg) |
| { |
| struct args *a = arg; |
| |
| if (a->scopes != NULL) |
| die->prune = true; |
| else |
| { |
| |
| |
| |
| |
| int result = INTUSE(dwarf_haspc) (&die->die, a->pc); |
| if (result < 0) |
| { |
| int error = INTUSE(dwarf_errno) (); |
| if (error != DWARF_E_NOERROR |
| && error != DWARF_E_NO_DEBUG_RANGES |
| && error != DWARF_E_NO_DEBUG_RNGLISTS) |
| { |
| __libdw_seterrno (error); |
| return -1; |
| } |
| result = 0; |
| } |
| if (result == 0) |
| die->prune = true; |
| |
| if (!die->prune |
| && INTUSE (dwarf_tag) (&die->die) == DW_TAG_inlined_subroutine) |
| a->inlined = depth; |
| } |
| |
| return 0; |
| } |
| |
| |
| |
| static int |
| origin_match (unsigned int depth, struct Dwarf_Die_Chain *die, void *arg) |
| { |
| struct args *a = arg; |
| |
| if (die->die.addr != a->inlined_origin.addr) |
| return 0; |
| |
| |
| |
| |
| |
| unsigned int nscopes = a->nscopes + depth; |
| Dwarf_Die *scopes = realloc (a->scopes, nscopes * sizeof scopes[0]); |
| if (scopes == NULL) |
| { |
| free (a->scopes); |
| __libdw_seterrno (DWARF_E_NOMEM); |
| return -1; |
| } |
| |
| a->scopes = scopes; |
| do |
| { |
| die = die->parent; |
| scopes[a->nscopes++] = die->die; |
| } |
| while (a->nscopes < nscopes); |
| assert (die->parent == NULL); |
| return a->nscopes; |
| } |
| |
| |
| static int |
| pc_record (unsigned int depth, struct Dwarf_Die_Chain *die, void *arg) |
| { |
| struct args *a = arg; |
| |
| if (die->prune) |
| return 0; |
| |
| if (a->scopes == NULL) |
| { |
| |
| |
| a->nscopes = depth + 1 - a->inlined; |
| a->scopes = malloc (a->nscopes * sizeof a->scopes[0]); |
| if (a->scopes == NULL) |
| { |
| __libdw_seterrno (DWARF_E_NOMEM); |
| return -1; |
| } |
| |
| for (unsigned int i = 0; i < a->nscopes; ++i) |
| { |
| a->scopes[i] = die->die; |
| die = die->parent; |
| } |
| |
| if (a->inlined == 0) |
| { |
| assert (die == NULL); |
| return a->nscopes; |
| } |
| |
| |
| |
| Dwarf_Die *const inlinedie = &a->scopes[depth - a->inlined]; |
| |
| assert (INTUSE (dwarf_tag) (inlinedie) == DW_TAG_inlined_subroutine); |
| Dwarf_Attribute attr_mem; |
| Dwarf_Attribute *attr = INTUSE (dwarf_attr) (inlinedie, |
| DW_AT_abstract_origin, |
| &attr_mem); |
| if (INTUSE (dwarf_formref_die) (attr, &a->inlined_origin) == NULL) |
| return -1; |
| return 0; |
| } |
| |
| |
| |
| |
| |
| |
| assert (a->inlined); |
| if (depth >= a->inlined) |
| |
| return 0; |
| |
| |
| |
| |
| |
| |
| return __libdw_visit_scopes (depth, die, NULL, &origin_match, NULL, a); |
| } |
| |
| |
| int |
| dwarf_getscopes (Dwarf_Die *cudie, Dwarf_Addr pc, Dwarf_Die **scopes) |
| { |
| if (cudie == NULL) |
| return -1; |
| |
| struct Dwarf_Die_Chain cu = { .parent = NULL, .die = *cudie }; |
| struct args a = { .pc = pc }; |
| |
| int result = __libdw_visit_scopes (0, &cu, NULL, &pc_match, &pc_record, &a); |
| |
| if (result == 0 && a.scopes != NULL) |
| result = __libdw_visit_scopes (0, &cu, NULL, &origin_match, NULL, &a); |
| |
| if (result > 0) |
| *scopes = a.scopes; |
| |
| return result; |
| } |