1 #pragma once
2 #include <torch/csrc/profiler/unwind/unwind_error.h>
3 #include <cstdint>
4 #include <optional>
5
6 enum {
7 DW_TAG_subprogram = 0x2e,
8 DW_TAG_inlined_subroutine = 0x1d,
9 DW_TAG_compile_unit = 0x11,
10 DW_AT_sibling = 0x1, // reference
11 DW_AT_name = 0x3, // string
12 DW_AT_stmt_list = 0x10, // lineptr
13 DW_AT_addr_base = 0x73, // sec_offset
14 DW_AT_rnglists_base = 0x74, // sec_offset
15 DW_AT_low_pc = 0x11, // address
16 DW_AT_high_pc = 0x12, // address
17 DW_AT_specification = 0x47, // reference
18 DW_AT_abstract_origin = 0x31, // reference
19 DW_AT_linkage_name = 0x6e, // string
20 DW_AT_ranges = 0x55, // rnglist
21 DW_AT_str_offsets_base = 0x72, // sec_offset
22 DW_FORM_addr = 0x01,
23 DW_FORM_block2 = 0x03,
24 DW_FORM_block4 = 0x04,
25 DW_FORM_data2 = 0x05,
26 DW_FORM_data4 = 0x06,
27 DW_FORM_data8 = 0x07,
28 DW_FORM_string = 0x08,
29 DW_FORM_block = 0x09,
30 DW_FORM_block1 = 0x0a,
31 DW_FORM_data1 = 0x0b,
32 DW_FORM_flag = 0x0c,
33 DW_FORM_sdata = 0x0d,
34 DW_FORM_strp = 0x0e,
35 DW_FORM_udata = 0x0f,
36 DW_FORM_ref_addr = 0x10,
37 DW_FORM_ref1 = 0x11,
38 DW_FORM_ref2 = 0x12,
39 DW_FORM_ref4 = 0x13,
40 DW_FORM_ref8 = 0x14,
41 DW_FORM_ref_udata = 0x15,
42 DW_FORM_indirect = 0x16,
43 DW_FORM_sec_offset = 0x17,
44 DW_FORM_exprloc = 0x18,
45 DW_FORM_flag_present = 0x19,
46 DW_FORM_strx = 0x1a,
47 DW_FORM_addrx = 0x1b,
48 DW_FORM_ref_sup4 = 0x1c,
49 DW_FORM_strp_sup = 0x1d,
50 DW_FORM_data16 = 0x1e,
51 DW_FORM_line_strp = 0x1f,
52 DW_FORM_ref_sig8 = 0x20,
53 DW_FORM_implicit_const = 0x21,
54 DW_FORM_loclistx = 0x22,
55 DW_FORM_rnglistx = 0x23,
56 DW_FORM_ref_sup8 = 0x24,
57 DW_FORM_strx1 = 0x25,
58 DW_FORM_strx2 = 0x26,
59 DW_FORM_strx3 = 0x27,
60 DW_FORM_strx4 = 0x28,
61 DW_FORM_addrx1 = 0x29,
62 DW_FORM_addrx2 = 0x2a,
63 DW_FORM_addrx3 = 0x2b,
64 DW_FORM_addrx4 = 0x2c,
65 /* GNU Debug Fission extensions. */
66 DW_FORM_GNU_addr_index = 0x1f01,
67 DW_FORM_GNU_str_index = 0x1f02,
68 DW_FORM_GNU_ref_alt = 0x1f20, /* offset in alternate .debuginfo. */
69 DW_FORM_GNU_strp_alt = 0x1f21, /* offset in alternate .debug_str. */
70 DW_LNCT_path = 0x1,
71 DW_LNCT_directory_index = 0x2,
72 DW_LNS_extended_op = 0x00,
73 DW_LNE_end_sequence = 0x01,
74 DW_LNE_set_address = 0x02,
75 DW_LNS_copy = 0x01,
76 DW_LNS_advance_pc = 0x02,
77 DW_LNS_advance_line = 0x03,
78 DW_LNS_set_file = 0x04,
79 DW_LNS_const_add_pc = 0x08,
80 DW_LNS_fixed_advance_pc = 0x09,
81 DW_RLE_end_of_list = 0x0,
82 DW_RLE_base_addressx = 0x1,
83 DW_RLE_startx_endx = 0x2,
84 DW_RLE_startx_length = 0x3,
85 DW_RLE_offset_pair = 0x4,
86 DW_RLE_base_address = 0x5,
87 DW_RLE_start_end = 0x6,
88 DW_RLE_start_length = 0x7
89 };
90
formSize(uint64_t form,uint8_t sec_offset_size)91 static std::optional<size_t> formSize(uint64_t form, uint8_t sec_offset_size) {
92 switch (form) {
93 case DW_FORM_addr:
94 return sizeof(void*);
95 case DW_FORM_block2:
96 case DW_FORM_block4:
97 return std::nullopt;
98 case DW_FORM_data2:
99 return 2;
100 case DW_FORM_data4:
101 return 4;
102 case DW_FORM_data8:
103 return 8;
104 case DW_FORM_string:
105 case DW_FORM_block:
106 case DW_FORM_block1:
107 return std::nullopt;
108 case DW_FORM_data1:
109 case DW_FORM_flag:
110 return 1;
111 case DW_FORM_sdata:
112 return std::nullopt;
113 case DW_FORM_strp:
114 return sec_offset_size;
115 case DW_FORM_udata:
116 return std::nullopt;
117 case DW_FORM_ref_addr:
118 return sec_offset_size;
119 case DW_FORM_ref1:
120 return 1;
121 case DW_FORM_ref2:
122 return 2;
123 case DW_FORM_ref4:
124 return 4;
125 case DW_FORM_ref8:
126 return 8;
127 case DW_FORM_ref_udata:
128 case DW_FORM_indirect:
129 return std::nullopt;
130 case DW_FORM_sec_offset:
131 return sec_offset_size;
132 case DW_FORM_exprloc:
133 return std::nullopt;
134 case DW_FORM_flag_present:
135 return 0;
136 case DW_FORM_strx:
137 case DW_FORM_addrx:
138 return std::nullopt;
139 case DW_FORM_ref_sup4:
140 return 4;
141 case DW_FORM_strp_sup:
142 return sec_offset_size;
143 case DW_FORM_data16:
144 return 16;
145 case DW_FORM_line_strp:
146 return sec_offset_size;
147 case DW_FORM_ref_sig8:
148 return 8;
149 case DW_FORM_implicit_const:
150 return 0;
151 case DW_FORM_loclistx:
152 case DW_FORM_rnglistx:
153 return std::nullopt;
154 case DW_FORM_ref_sup8:
155 return 8;
156 case DW_FORM_strx1:
157 return 1;
158 case DW_FORM_strx2:
159 return 2;
160 case DW_FORM_strx3:
161 return 3;
162 case DW_FORM_strx4:
163 return 4;
164 case DW_FORM_addrx1:
165 return 1;
166 case DW_FORM_addrx2:
167 return 2;
168 case DW_FORM_addrx3:
169 return 3;
170 case DW_FORM_addrx4:
171 return 4;
172 case DW_FORM_GNU_addr_index:
173 case DW_FORM_GNU_str_index:
174 case DW_FORM_GNU_ref_alt:
175 case DW_FORM_GNU_strp_alt:
176 default:
177 return std::nullopt;
178 }
179 }
180