2 * Copyright (c) 2008-2009, 2011, Juniper Networks, Inc.
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 COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
26 * JNPR: dwarf_func.c 336441 2009-10-17 09:19:54Z deo
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
35 #include <_libdwarf.h>
38 dwarf_add_function(Dwarf_Debug dbg, Dwarf_Func func)
41 STAILQ_INSERT_TAIL(&dbg->dbg_func, func, func_next);
45 dwarf_function_get_addr_range(Dwarf_Func f, Dwarf_Addr *low_pc,
49 *low_pc = f->func_low_pc;
50 *high_pc = f->func_high_pc;
55 dwarf_inlined_function_get_addr_range(Dwarf_Inlined_Func f, Dwarf_Addr *low_pc,
59 *low_pc = f->ifunc_low_pc;
60 *high_pc = f->ifunc_high_pc;
65 dwarf_function_is_inlined(Dwarf_Func f)
68 if (f->func_is_inlined == DW_INL_inlined ||
69 f->func_is_inlined == DW_INL_declared_inlined)
76 dwarf_find_function_by_name(Dwarf_Debug dbg, const char *name)
78 /* XXX: replace with a fast version */
81 STAILQ_FOREACH(func, &dbg->dbg_func, func_next) {
82 if (strcmp(name, func->func_name) == 0)
89 dwarf_find_function_by_offset(Dwarf_Debug dbg, Dwarf_Off off)
94 /* printf("look for %llx\n", off); */
95 STAILQ_FOREACH(func, &dbg->dbg_func, func_next) {
97 if ((off_t)die->die_offset == off) {
105 dwarf_build_function_table(Dwarf_Debug dbg)
109 Dwarf_Die die, origin_die;
110 Dwarf_Func func, origin_func;
111 Dwarf_Inlined_Func ifunc;
112 unsigned long long offset;
117 * find out all the functions
119 STAILQ_FOREACH(cu, &dbg->dbg_cu, cu_next) {
120 STAILQ_FOREACH(die, &cu->cu_die, die_next) {
121 if (die->die_a->a_tag == DW_TAG_subprogram) {
123 * Some function has multiple entries, i.e.
124 * if a function is inlined, it has many
125 * abstract/concrete instances, the abstract
126 * instances are with DW_TAG_subprogram.
128 dwarf_attrval_string(die, DW_AT_name, &name,
130 func = dwarf_find_function_by_name(dbg, name);
133 sizeof(struct _Dwarf_Func));
136 func->func_die = die;
137 func->func_name = name;
139 &func->func_inlined_instances);
141 dwarf_add_function(dbg, func);
142 STAILQ_FOREACH(av, &die->die_attrval,
144 switch (av->av_attrib) {
154 func->func_is_inlined =
165 * Now check the concrete inlined instances.
167 STAILQ_FOREACH(cu, &dbg->dbg_cu, cu_next) {
168 STAILQ_FOREACH(die, &cu->cu_die, die_next) {
169 if (die->die_a->a_tag == DW_TAG_inlined_subroutine) {
171 sizeof(struct _Dwarf_Inlined_Func));
173 STAILQ_FOREACH(av, &die->die_attrval, av_next) {
174 switch (av->av_attrib) {
175 case DW_AT_abstract_origin:
176 offset = av->u[0].u64 +
177 die->die_cu->cu_offset;
178 origin_die = dwarf_die_find(
180 DWARF_ASSERT(origin_die != 0);
183 * the abstract origin must
184 * have been merged with
187 dwarf_attrval_string(
188 origin_die, DW_AT_name,
191 dwarf_find_function_by_name
193 DWARF_ASSERT(origin_func != 0);
197 func_inlined_instances,
202 ifunc->ifunc_low_pc =
206 ifunc->ifunc_high_pc =
217 dwarf_function_iterate_inlined_instance(Dwarf_Func func,
218 Dwarf_Inlined_Callback f, void *data)
220 Dwarf_Inlined_Func ifunc;
222 if (!dwarf_function_is_inlined(func))
224 STAILQ_FOREACH(ifunc, &func->func_inlined_instances, ifunc_next) {