2 * Copyright (c) 2007 John Birrell (jb@freebsd.org)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include "_libdwarf.h"
33 dwarf_die_add(Dwarf_CU cu, int level, uint64_t offset, uint64_t abnum, Dwarf_Abbrev a, Dwarf_Die *diep, Dwarf_Error *err)
37 int ret = DWARF_E_NONE;
42 if (cu == NULL || a == NULL) {
43 DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
44 return DWARF_E_ARGUMENT;
47 if ((die = malloc(sizeof(struct _Dwarf_Die))) == NULL) {
48 DWARF_SET_ERROR(err, DWARF_E_MEMORY);
49 return DWARF_E_MEMORY;
52 /* Initialise the abbrev structure. */
53 die->die_level = level;
54 die->die_offset = offset;
55 die->die_abnum = abnum;
60 /* Initialise the list of attribute values. */
61 STAILQ_INIT(&die->die_attrval);
63 /* Add the die to the list in the compilation unit. */
64 STAILQ_INSERT_TAIL(&cu->cu_die, die, die_next);
66 /* Add the die to the hash table in the compilation unit. */
67 key = offset % DWARF_DIE_HASH_SIZE;
68 STAILQ_INSERT_TAIL(&cu->cu_die_hash[key], die, die_hash);
77 dwarf_dieoffset(Dwarf_Die die, Dwarf_Off *ret_offset, Dwarf_Error *err __unused)
79 *ret_offset = die->die_offset;
85 dwarf_child(Dwarf_Die die, Dwarf_Die *ret_die, Dwarf_Error *err)
88 int ret = DWARF_E_NONE;
93 if (die == NULL || ret_die == NULL) {
94 DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
95 return DWARF_E_ARGUMENT;
98 if ((next = STAILQ_NEXT(die, die_next)) == NULL ||
99 next->die_level != die->die_level + 1) {
101 DWARF_SET_ERROR(err, DWARF_E_NO_ENTRY);
102 ret = DWARF_E_NO_ENTRY;
110 dwarf_tag(Dwarf_Die die, Dwarf_Half *tag, Dwarf_Error *err)
115 return DWARF_E_ERROR;
117 if (die == NULL || tag == NULL || (a = die->die_a) == NULL) {
118 DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
119 return DWARF_E_ARGUMENT;
128 dwarf_siblingof(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Die *caller_ret_die, Dwarf_Error *err)
132 int ret = DWARF_E_NONE;
135 return DWARF_E_ERROR;
137 if (dbg == NULL || caller_ret_die == NULL) {
138 DWARF_SET_ERROR(err, DWARF_E_ARGUMENT);
139 return DWARF_E_ARGUMENT;
142 if ((cu = dbg->dbg_cu_current) == NULL) {
143 DWARF_SET_ERROR(err, DWARF_E_CU_CURRENT);
144 return DWARF_E_CU_CURRENT;
148 *caller_ret_die = STAILQ_FIRST(&cu->cu_die);
150 if (*caller_ret_die == NULL) {
151 DWARF_SET_ERROR(err, DWARF_E_NO_ENTRY);
152 ret = DWARF_E_NO_ENTRY;
156 while ((next = STAILQ_NEXT(next, die_next)) != NULL) {
157 if (next->die_level < die->die_level) {
161 if (next->die_level == die->die_level) {
162 *caller_ret_die = next;
168 *caller_ret_die = NULL;
169 DWARF_SET_ERROR(err, DWARF_E_NO_ENTRY);
170 ret = DWARF_E_NO_ENTRY;
178 dwarf_die_find(Dwarf_Die die, Dwarf_Unsigned off)
180 Dwarf_CU cu = die->die_cu;
183 STAILQ_FOREACH(die1, &cu->cu_die, die_next) {
184 if (die1->die_offset == off)