• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2007-2010 The Android Open Source Project
2 **
3 ** This software is licensed under the terms of the GNU General Public
4 ** License version 2, as published by the Free Software Foundation, and
5 ** may be copied, distributed, and modified under those terms.
6 **
7 ** This program is distributed in the hope that it will be useful,
8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 ** GNU General Public License for more details.
11 */
12 
13 /*
14  * Contains implementation of misc. DWARF utility routines.
15  */
16 
17 #include "stdio.h"
18 #include "dwarf_utils.h"
19 
20 /* "Stringifies" the parameter. */
21 #define DWARF_NAMEFY(val) case val: return "" #val ""
22 
23 /* "Stringifies" two parameters. */
24 #define DWARF_NAMEFY2(val1, val2) case val1: return "" #val1 " | " #val2 ""
25 
26 const char*
dwarf_at_name(Dwarf_At at)27 dwarf_at_name(Dwarf_At at) {
28   switch (at) {
29     DWARF_NAMEFY(DW_AT_sibling);
30     DWARF_NAMEFY(DW_AT_location);
31     DWARF_NAMEFY(DW_AT_name);
32     DWARF_NAMEFY(DW_AT_ordering);
33     DWARF_NAMEFY(DW_AT_subscr_data);
34     DWARF_NAMEFY(DW_AT_byte_size);
35     DWARF_NAMEFY(DW_AT_bit_offset);
36     DWARF_NAMEFY(DW_AT_bit_size);
37     DWARF_NAMEFY(DW_AT_element_list);
38     DWARF_NAMEFY(DW_AT_stmt_list);
39     DWARF_NAMEFY(DW_AT_low_pc);
40     DWARF_NAMEFY(DW_AT_high_pc);
41     DWARF_NAMEFY(DW_AT_language);
42     DWARF_NAMEFY(DW_AT_member);
43     DWARF_NAMEFY(DW_AT_discr);
44     DWARF_NAMEFY(DW_AT_discr_value);
45     DWARF_NAMEFY(DW_AT_visibility);
46     DWARF_NAMEFY(DW_AT_import);
47     DWARF_NAMEFY(DW_AT_string_length);
48     DWARF_NAMEFY(DW_AT_common_reference);
49     DWARF_NAMEFY(DW_AT_comp_dir);
50     DWARF_NAMEFY(DW_AT_const_value);
51     DWARF_NAMEFY(DW_AT_containing_type);
52     DWARF_NAMEFY(DW_AT_default_value);
53     DWARF_NAMEFY(DW_AT_inline);
54     DWARF_NAMEFY(DW_AT_is_optional);
55     DWARF_NAMEFY(DW_AT_lower_bound);
56     DWARF_NAMEFY(DW_AT_producer);
57     DWARF_NAMEFY(DW_AT_prototyped);
58     DWARF_NAMEFY(DW_AT_return_addr);
59     DWARF_NAMEFY(DW_AT_start_scope);
60     DWARF_NAMEFY2(DW_AT_bit_stride, DW_AT_stride_size);
61     DWARF_NAMEFY(DW_AT_upper_bound);
62     DWARF_NAMEFY(DW_AT_abstract_origin);
63     DWARF_NAMEFY(DW_AT_accessibility);
64     DWARF_NAMEFY(DW_AT_address_class);
65     DWARF_NAMEFY(DW_AT_artificial);
66     DWARF_NAMEFY(DW_AT_base_types);
67     DWARF_NAMEFY(DW_AT_calling_convention);
68     DWARF_NAMEFY(DW_AT_count);
69     DWARF_NAMEFY(DW_AT_data_member_location);
70     DWARF_NAMEFY(DW_AT_decl_column);
71     DWARF_NAMEFY(DW_AT_decl_file);
72     DWARF_NAMEFY(DW_AT_decl_line);
73     DWARF_NAMEFY(DW_AT_declaration);
74     DWARF_NAMEFY(DW_AT_discr_list);
75     DWARF_NAMEFY(DW_AT_encoding);
76     DWARF_NAMEFY(DW_AT_external);
77     DWARF_NAMEFY(DW_AT_frame_base);
78     DWARF_NAMEFY(DW_AT_friend);
79     DWARF_NAMEFY(DW_AT_identifier_case);
80     DWARF_NAMEFY(DW_AT_macro_info);
81     DWARF_NAMEFY(DW_AT_namelist_item);
82     DWARF_NAMEFY(DW_AT_priority);
83     DWARF_NAMEFY(DW_AT_segment);
84     DWARF_NAMEFY(DW_AT_specification);
85     DWARF_NAMEFY(DW_AT_static_link);
86     DWARF_NAMEFY(DW_AT_type);
87     DWARF_NAMEFY(DW_AT_use_location);
88     DWARF_NAMEFY(DW_AT_variable_parameter);
89     DWARF_NAMEFY(DW_AT_virtuality);
90     DWARF_NAMEFY(DW_AT_vtable_elem_location);
91     DWARF_NAMEFY(DW_AT_allocated);
92     DWARF_NAMEFY(DW_AT_associated);
93     DWARF_NAMEFY(DW_AT_data_location);
94     DWARF_NAMEFY2(DW_AT_byte_stride, DW_AT_stride);
95     DWARF_NAMEFY(DW_AT_entry_pc);
96     DWARF_NAMEFY(DW_AT_use_UTF8);
97     DWARF_NAMEFY(DW_AT_extension);
98     DWARF_NAMEFY(DW_AT_ranges);
99     DWARF_NAMEFY(DW_AT_trampoline);
100     DWARF_NAMEFY(DW_AT_call_column);
101     DWARF_NAMEFY(DW_AT_call_file);
102     DWARF_NAMEFY(DW_AT_call_line);
103     DWARF_NAMEFY(DW_AT_description);
104     DWARF_NAMEFY(DW_AT_binary_scale);
105     DWARF_NAMEFY(DW_AT_decimal_scale);
106     DWARF_NAMEFY(DW_AT_small);
107     DWARF_NAMEFY(DW_AT_decimal_sign);
108     DWARF_NAMEFY(DW_AT_digit_count);
109     DWARF_NAMEFY(DW_AT_picture_string);
110     DWARF_NAMEFY(DW_AT_mutable);
111     DWARF_NAMEFY(DW_AT_threads_scaled);
112     DWARF_NAMEFY(DW_AT_explicit);
113     DWARF_NAMEFY(DW_AT_object_pointer);
114     DWARF_NAMEFY(DW_AT_endianity);
115     DWARF_NAMEFY(DW_AT_elemental);
116     DWARF_NAMEFY(DW_AT_pure);
117     DWARF_NAMEFY(DW_AT_recursive);
118     DWARF_NAMEFY(DW_AT_signature);
119     DWARF_NAMEFY(DW_AT_main_subprogram);
120     DWARF_NAMEFY(DW_AT_data_bit_offset);
121     DWARF_NAMEFY(DW_AT_const_expr);
122     DWARF_NAMEFY(DW_AT_enum_class);
123     DWARF_NAMEFY(DW_AT_linkage_name);
124     default:
125       return "DW_AT_Unknown";
126   }
127 }
128 
129 const char*
dwarf_form_name(Dwarf_Form form)130 dwarf_form_name(Dwarf_Form form) {
131   switch (form) {
132     DWARF_NAMEFY(DW_FORM_addr);
133     DWARF_NAMEFY(DW_FORM_block2);
134     DWARF_NAMEFY(DW_FORM_block4);
135     DWARF_NAMEFY(DW_FORM_data2);
136     DWARF_NAMEFY(DW_FORM_data4);
137     DWARF_NAMEFY(DW_FORM_data8);
138     DWARF_NAMEFY(DW_FORM_string);
139     DWARF_NAMEFY(DW_FORM_block);
140     DWARF_NAMEFY(DW_FORM_block1);
141     DWARF_NAMEFY(DW_FORM_data1);
142     DWARF_NAMEFY(DW_FORM_flag);
143     DWARF_NAMEFY(DW_FORM_sdata);
144     DWARF_NAMEFY(DW_FORM_strp);
145     DWARF_NAMEFY(DW_FORM_udata);
146     DWARF_NAMEFY(DW_FORM_ref_addr);
147     DWARF_NAMEFY(DW_FORM_ref1);
148     DWARF_NAMEFY(DW_FORM_ref2);
149     DWARF_NAMEFY(DW_FORM_ref4);
150     DWARF_NAMEFY(DW_FORM_ref8);
151     DWARF_NAMEFY(DW_FORM_ref_udata);
152     DWARF_NAMEFY(DW_FORM_indirect);
153     DWARF_NAMEFY(DW_FORM_sec_offset);
154     DWARF_NAMEFY(DW_FORM_exprloc);
155     DWARF_NAMEFY(DW_FORM_flag_present);
156     DWARF_NAMEFY(DW_FORM_ref_sig8);
157     default:
158       return "DW_FORM_Unknown";
159   }
160 }
161 
162 const char*
dwarf_tag_name(Dwarf_Tag tag)163 dwarf_tag_name(Dwarf_Tag tag) {
164   switch (tag) {
165     DWARF_NAMEFY(DW_TAG_array_type);
166     DWARF_NAMEFY(DW_TAG_class_type);
167     DWARF_NAMEFY(DW_TAG_entry_point);
168     DWARF_NAMEFY(DW_TAG_enumeration_type);
169     DWARF_NAMEFY(DW_TAG_formal_parameter);
170     DWARF_NAMEFY(DW_TAG_imported_declaration);
171     DWARF_NAMEFY(DW_TAG_label);
172     DWARF_NAMEFY(DW_TAG_lexical_block);
173     DWARF_NAMEFY(DW_TAG_member);
174     DWARF_NAMEFY(DW_TAG_pointer_type);
175     DWARF_NAMEFY(DW_TAG_reference_type);
176     DWARF_NAMEFY(DW_TAG_compile_unit);
177     DWARF_NAMEFY(DW_TAG_string_type);
178     DWARF_NAMEFY(DW_TAG_structure_type);
179     DWARF_NAMEFY(DW_TAG_subroutine_type);
180     DWARF_NAMEFY(DW_TAG_typedef);
181     DWARF_NAMEFY(DW_TAG_union_type);
182     DWARF_NAMEFY(DW_TAG_unspecified_parameters);
183     DWARF_NAMEFY(DW_TAG_variant);
184     DWARF_NAMEFY(DW_TAG_common_block);
185     DWARF_NAMEFY(DW_TAG_common_inclusion);
186     DWARF_NAMEFY(DW_TAG_inheritance);
187     DWARF_NAMEFY(DW_TAG_inlined_subroutine);
188     DWARF_NAMEFY(DW_TAG_module);
189     DWARF_NAMEFY(DW_TAG_ptr_to_member_type);
190     DWARF_NAMEFY(DW_TAG_set_type);
191     DWARF_NAMEFY(DW_TAG_subrange_type);
192     DWARF_NAMEFY(DW_TAG_with_stmt);
193     DWARF_NAMEFY(DW_TAG_access_declaration);
194     DWARF_NAMEFY(DW_TAG_base_type);
195     DWARF_NAMEFY(DW_TAG_catch_block);
196     DWARF_NAMEFY(DW_TAG_const_type);
197     DWARF_NAMEFY(DW_TAG_constant);
198     DWARF_NAMEFY(DW_TAG_enumerator);
199     DWARF_NAMEFY(DW_TAG_file_type);
200     DWARF_NAMEFY(DW_TAG_friend);
201     DWARF_NAMEFY(DW_TAG_namelist);
202     DWARF_NAMEFY2(DW_TAG_namelist_item, DW_TAG_namelist_items);
203     DWARF_NAMEFY(DW_TAG_packed_type);
204     DWARF_NAMEFY(DW_TAG_subprogram);
205     DWARF_NAMEFY2(DW_TAG_template_type_parameter, DW_TAG_template_type_param);
206     DWARF_NAMEFY2(DW_TAG_template_value_parameter,
207                   DW_TAG_template_value_param);
208     DWARF_NAMEFY(DW_TAG_thrown_type);
209     DWARF_NAMEFY(DW_TAG_try_block);
210     DWARF_NAMEFY(DW_TAG_variant_part);
211     DWARF_NAMEFY(DW_TAG_variable);
212     DWARF_NAMEFY(DW_TAG_volatile_type);
213     DWARF_NAMEFY(DW_TAG_dwarf_procedure);
214     DWARF_NAMEFY(DW_TAG_restrict_type);
215     DWARF_NAMEFY(DW_TAG_interface_type);
216     DWARF_NAMEFY(DW_TAG_namespace);
217     DWARF_NAMEFY(DW_TAG_imported_module);
218     DWARF_NAMEFY(DW_TAG_unspecified_type);
219     DWARF_NAMEFY(DW_TAG_partial_unit);
220     DWARF_NAMEFY(DW_TAG_imported_unit);
221     DWARF_NAMEFY(DW_TAG_mutable_type);
222     DWARF_NAMEFY(DW_TAG_condition);
223     DWARF_NAMEFY(DW_TAG_shared_type);
224     DWARF_NAMEFY(DW_TAG_type_unit);
225     DWARF_NAMEFY(DW_TAG_rvalue_reference_type);
226     DWARF_NAMEFY(DW_TAG_template_alias);
227     default:
228       return "DW_TAG_Unknown";
229   }
230 }
231 
232 void
dump_attrib(Dwarf_At at,Dwarf_Form form,const Dwarf_Value * val)233 dump_attrib(Dwarf_At at, Dwarf_Form form, const Dwarf_Value* val) {
234   if (form != 0) {
235     printf("   +++ Attribute: %s [%s]\n",
236            dwarf_at_name(at), dwarf_form_name(form));
237   } else {
238     printf("   +++ Attribute: %s\n", dwarf_at_name(at));
239   }
240   dump_value(val);
241 }
242 
243 void
dump_value(const Dwarf_Value * attr_value)244 dump_value(const Dwarf_Value* attr_value) {
245   printf("       Data[%03u]: (", attr_value->encoded_size);
246   switch (attr_value->type) {
247     case DWARF_VALUE_U8:
248       printf("BYTE)   = %u (x%02X)\n", (Elf_Word)attr_value->u8,
249                                       (Elf_Word)attr_value->u8);
250       break;
251 
252     case DWARF_VALUE_S8:
253       printf("SBYTE)  = %d (x%02X)\n", (Elf_Sword)attr_value->s8,
254                                       (Elf_Sword)attr_value->s8);
255       break;
256 
257     case DWARF_VALUE_U16:
258       printf("WORD)   = %u (x%04X)\n", (Elf_Word)attr_value->u16,
259                                       (Elf_Word)attr_value->u16);
260       break;
261 
262     case DWARF_VALUE_S16:
263       printf("SWORD)  = %d (x%04X)\n", (Elf_Sword)attr_value->s16,
264                                       (Elf_Sword)attr_value->s16);
265       break;
266 
267     case DWARF_VALUE_U32:
268       printf("DWORD)  = %u (x%08X)\n", attr_value->u32,
269                                       attr_value->u32);
270       break;
271 
272     case DWARF_VALUE_S32:
273       printf("SDWORD) = %d (x%08X)\n", attr_value->s32,
274                                       attr_value->s32);
275       break;
276 
277     case DWARF_VALUE_U64:
278       printf("XWORD)  = %I64u (x%I64X)\n", attr_value->u64,
279                                           attr_value->u64);
280       break;
281 
282     case DWARF_VALUE_S64:
283       printf("SXWORD) = %I64d (x%I64X)\n", attr_value->s64,
284                                           attr_value->s64);
285       break;
286 
287     case DWARF_VALUE_STR:
288       printf("STRING) = %s\n", attr_value->str);
289       break;
290 
291     case DWARF_VALUE_PTR32:
292       printf("PTR32)  = x%08X\n", attr_value->ptr32);
293       break;
294 
295     case DWARF_VALUE_PTR64:
296       printf("PTR64)  = x%08I64X\n", attr_value->ptr64);
297       break;
298 
299     case DWARF_VALUE_BLOCK:
300       printf("BLOCK)  = [%I64u]:", attr_value->block.block_size);
301       for (Elf_Xword i = 0; i < attr_value->block.block_size; i++) {
302         Elf_Byte prnt = *((const Elf_Byte*)attr_value->block.block_ptr + i);
303         printf(" x%02X", prnt);
304       }
305       printf("\n");
306       break;
307 
308     case DWARF_VALUE_UNKNOWN:
309     default:
310       printf("UNKNOWN)");
311       break;
312   }
313 }
314