Debian Codegraph @

runtime/unwind/unwind.h

raw
1 /* -*- linux-c -*-
2 *
3 * dwarf unwinder header file
4 * Copyright (C) 2008-2010, 2013-2019 Red Hat Inc.
5 * Copyright (C) 2002-2006 Novell, Inc.
6 *
7 * This file is part of systemtap, and is free software. You can
8 * redistribute it and/or modify it under the terms of the GNU General
9 * Public License (GPL); either version 2, or (at your option) any
10 * later version.
11 */
12
13 #ifndef _STP_UNWIND_H_
14 #define _STP_UNWIND_H_
15
16 // portions of code needed by the debug_line decoder
17 #if defined(STP_USE_DWARF_UNWINDER) || defined(STP_NEED_LINE_DATA)
18
19 struct unwind_frame_info
20 {
21 struct pt_regs regs;
22 unsigned call_frame:1;
23 };
24
25 #if defined (__x86_64__)
26 #include "x86_64.h"
27 #elif defined (__i386__)
28 #include "i386.h"
29 #elif defined (__powerpc64__)
30 #include "ppc64.h"
31 #elif defined (__s390x__)
32 #include "s390x.h"
33 #elif defined (__arm__)
34 #include "arm.h"
35 #elif defined (__aarch64__)
36 #include "arm64.h"
37 #elif defined (__mips__)
38 #include "mips.h"
39 #elif defined (__riscv)
40 #include "riscv.h"
41 #else
42 #error "Unsupported dwarf unwind architecture"
43 #endif
44
45 #define DW_EH_PE_absptr 0x00
46 #define DW_EH_PE_leb128 0x01
47 #define DW_EH_PE_data2 0x02
48 #define DW_EH_PE_data4 0x03
49 #define DW_EH_PE_data8 0x04
50 #define DW_EH_PE_FORM 0x07 /* mask */
51 #define DW_EH_PE_signed 0x08 /* signed versions of above have this bit set */
52
53 #define DW_EH_PE_pcrel 0x10
54 #define DW_EH_PE_textrel 0x20
55 #define DW_EH_PE_datarel 0x30
56 #define DW_EH_PE_funcrel 0x40
57 #define DW_EH_PE_aligned 0x50
58 #define DW_EH_PE_ADJUST 0x70 /* mask */
59 #define DW_EH_PE_indirect 0x80
60 #define DW_EH_PE_omit 0xff
61
62 #define DW_FORM_udata 0x0f
63 #define DW_FORM_data16 0x1e
64 #define DW_FORM_line_strp 0x1f
65
66 typedef unsigned long uleb128_t;
67 typedef signed long sleb128_t;
68
69
70 static uleb128_t get_uleb128(const u8 **pcur, const u8 *end)
71 {
72 const u8 *cur = *pcur;
73 uleb128_t value = 0;
74 unsigned shift;
75
76 for (shift = 0; cur < end; shift += 7) {
77 if (shift + 7 > 8 * sizeof(value)
78 && (*cur & 0x7fU) >= (1U << (8 * sizeof(value) - shift))) {
79 cur = end + 1;
80 break;
81 }
82 value |= (uleb128_t)(*cur & 0x7f) << shift;
83 if (!(*cur++ & 0x80))
84 break;
85 }
86 *pcur = cur;
87
88 return value;
89 }
90
91 static sleb128_t get_sleb128(const u8 **pcur, const u8 *end)
92 {
93 const u8 *cur = *pcur;
94 sleb128_t value = 0;
95 unsigned shift;
96
97 for (shift = 0; cur < end; shift += 7) {
98 const u8 cur_val = *cur++;
99 if (shift + 7 > 8 * sizeof(value)
100 && (cur_val & 0x7fU) >= (1U << (8 * sizeof(value) - shift))) {
101 cur = end + 1;
102 break;
103 }
104 value |= (sleb128_t)(cur_val & 0x7f) << shift;
105 if (!(cur_val & 0x80)) {
106 value |= -(cur_val & 0x40) << shift;
107 break;
108 }
109 }
110 *pcur = cur;
111
112 return value;
113 }
114
115 /* read an encoded pointer and increment *pLoc past the end of the
116 * data read. */
117 static unsigned long read_ptr_sect(const u8 **pLoc, const void *end,
118 signed ptrType, unsigned long textAddr,
119 unsigned long dataAddr, int user, int compat_task, int tableSize)
120 {
121 unsigned long value = 0;
122 union {
123 const u8 *p8;
124 const u16 *p16u;
125 const s16 *p16s;
126 const u32 *p32u;
127 const s32 *p32s;
128 const unsigned long *pul;
129 const unsigned int *pui;
130 } ptr;
131
132 if (ptrType < 0 || ptrType == DW_EH_PE_omit)
133 return 0;
134
135 ptr.p8 = *pLoc;
136 switch (ptrType & DW_EH_PE_FORM) {
137 case DW_EH_PE_data2:
138 if (end < (const void *)(ptr.p16u + 1))
139 return 0;
140 if (ptrType & DW_EH_PE_signed)
141 value = _stp_get_unaligned(ptr.p16s++);
142 else
143 value = _stp_get_unaligned(ptr.p16u++);
144 break;
145 case DW_EH_PE_data4:
146 #ifdef CONFIG_64BIT
147
148 /* If the tableSize matches the length of data we're trying to return
149 * or if specifically set to 0 in the call it means we actually want a
150 * DW_EH_PE_data4 and not a DW_EH_PE_absptr. If this is not the case
151 * then we want to fall through to DW_EH_PE_absptr */
152 if (!compat_task || (compat_task && (tableSize == 4 || tableSize == 0)))
153 {
154 if (end < (const void *)(ptr.p32u + 1))
155 return 0;
156
157 if (ptrType & DW_EH_PE_signed)
158 value = _stp_get_unaligned(ptr.p32s++);
159 else
160 value = _stp_get_unaligned(ptr.p32u++);
161 break;
162 }
163 fallthrough;
164 case DW_EH_PE_data8:
165 BUILD_BUG_ON(sizeof(u64) != sizeof(value));
166 #else
167 BUILD_BUG_ON(sizeof(u32) != sizeof(value));
168 #endif
169 fallthrough;
170 case DW_EH_PE_absptr:
171 if (compat_task)
172 {
173 if (end < (const void *)(ptr.pui + 1))
174 return 0;
175 value = _stp_get_unaligned(ptr.pui++);
176 } else {
177 if (end < (const void *)(ptr.pul + 1))
178 return 0;
179 value = _stp_get_unaligned(ptr.pul++);
180 }
181
182 break;
183 case DW_EH_PE_leb128:
184 BUILD_BUG_ON(sizeof(uleb128_t) > sizeof(value));
185 value = ptrType & DW_EH_PE_signed ? get_sleb128(&ptr.p8, end)
186 : get_uleb128(&ptr.p8, end);
187 if ((const void *)ptr.p8 > end)
188 return 0;
189 break;
190 default:
191 return 0;
192 }
193 switch (ptrType & DW_EH_PE_ADJUST) {
194 case DW_EH_PE_absptr:
195 break;
196 case DW_EH_PE_pcrel:
197 value += (unsigned long)*pLoc;
198 break;
199 case DW_EH_PE_textrel:
200 value += textAddr;
201 break;
202 case DW_EH_PE_datarel:
203 value += dataAddr;
204 break;
205 default:
206 return 0;
207 }
208 if (ptrType & DW_EH_PE_indirect
209 && _stp_deref_nofault(value, sizeof(value),
210 (unsigned long *)value,
211 (user ? STP_USER_DS : STP_KERNEL_DS)))
212 return 0;
213 *pLoc = ptr.p8;
214
215 return value;
216 }
217
218 static unsigned long read_pointer(const u8 **pLoc, const void *end, signed ptrType,
219 int user, int compat_task)
220 {
221 return read_ptr_sect(pLoc, end, ptrType, 0, 0, user, compat_task, 0);
222 }
223
224 #endif /* defined(STP_USE_DWARF_UNWINDER) || defined(STP_NEED_LINE_DATA) */
225
226 #ifdef STP_USE_DWARF_UNWINDER
227
228 /* Used for DW_CFA_remember_state and DW_CFA_restore_state. */
229 #define STP_MAX_STACK_DEPTH 4
230
231 #ifndef BUILD_BUG_ON_ZERO
232 #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
233 #endif
234
235 #ifndef FIELD_SIZEOF
236 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
237 #endif
238
239
240 #define EXTRA_INFO(f) { \
241 BUILD_BUG_ON_ZERO(offsetof(struct unwind_frame_info, f) \
242 % FIELD_SIZEOF(struct unwind_frame_info, f)) \
243 + offsetof(struct unwind_frame_info, f) \
244 / FIELD_SIZEOF(struct unwind_frame_info, f), \
245 FIELD_SIZEOF(struct unwind_frame_info, f) \
246 }
247 #define PTREGS_INFO(f) EXTRA_INFO(regs.f)
248
249 static const struct {
250 unsigned offs:BITS_PER_LONG / 2;
251 unsigned width:BITS_PER_LONG / 2;
252 } reg_info[] = {
253 UNW_REGISTER_INFO
254 };
255
256 #undef PTREGS_INFO
257 #undef EXTRA_INFO
258
259 /* The reg_info array assumes dwarf register numbers start at zero and
260 are consecutive. If that isn't the case for some architecture (e.g. ppc)
261 then redefine to map the given dwarf register number to the actual
262 reg_info index. */
263 #ifndef DWARF_REG_MAP
264 #define DWARF_REG_MAP(r) r
265 #endif
266
267 /* COMPAT_REG_MAP is the mapping from 32bit to 64bit DWARF registers. In
268 the case where they're not the same (e.g. x86_64 and i386) the alternate
269 mapping will be located in the 64bit header file */
270 #ifndef COMPAT_REG_MAP
271 #define COMPAT_REG_MAP(r) r
272 #endif
273
274 /* The number of real registers in the register map. These are all assumed
275 to be the Same in the new frame. All others will be Unknown untill they
276 have been explictly set. (e.g. the x86 return register). */
277 #ifndef UNW_NR_REAL_REGS
278 #define UNW_NR_REAL_REGS ARRAY_SIZE(reg_info)
279 #endif
280
281 #ifndef REG_INVALID
282 #define REG_INVALID(r) (reg_info[r].width == 0)
283 #endif
284
285 /* Whether the stack pointer should be set from the CFA.
286 If this isn't what the architecture wants, then it should define
287 this as zero. */
288 #ifndef UNW_SP_FROM_CFA
289 #define UNW_SP_FROM_CFA 1
290 #endif
291
292 /* Whether the instruction pointer should be set from the return address
293 column. If this isn't what the architecture wants, then it should
294 define this as zero. */
295 #ifndef UNW_PC_FROM_RA
296 #define UNW_PC_FROM_RA 1
297 #endif
298
299 #define DW_CFA_nop 0x00
300 #define DW_CFA_set_loc 0x01
301 #define DW_CFA_advance_loc1 0x02
302 #define DW_CFA_advance_loc2 0x03
303 #define DW_CFA_advance_loc4 0x04
304 #define DW_CFA_offset_extended 0x05
305 #define DW_CFA_restore_extended 0x06
306 #define DW_CFA_undefined 0x07
307 #define DW_CFA_same_value 0x08
308 #define DW_CFA_register 0x09
309 #define DW_CFA_remember_state 0x0a
310 #define DW_CFA_restore_state 0x0b
311 #define DW_CFA_def_cfa 0x0c
312 #define DW_CFA_def_cfa_register 0x0d
313 #define DW_CFA_def_cfa_offset 0x0e
314 #define DW_CFA_def_cfa_expression 0x0f
315 #define DW_CFA_expression 0x10
316 #define DW_CFA_offset_extended_sf 0x11
317 #define DW_CFA_def_cfa_sf 0x12
318 #define DW_CFA_def_cfa_offset_sf 0x13
319 #define DW_CFA_val_offset 0x14
320 #define DW_CFA_val_offset_sf 0x15
321 #define DW_CFA_val_expression 0x16
322 #define DW_CFA_lo_user 0x1c
323 #define DW_CFA_GNU_window_save 0x2d
324 #define DW_CFA_GNU_args_size 0x2e
325 #define DW_CFA_GNU_negative_offset_extended 0x2f
326 #define DW_CFA_hi_user 0x3f
327
328 #define DW_OP_addr 0x03
329 #define DW_OP_deref 0x06
330 #define DW_OP_const1u 0x08
331 #define DW_OP_const1s 0x09
332 #define DW_OP_const2u 0x0a
333 #define DW_OP_const2s 0x0b
334 #define DW_OP_const4u 0x0c
335 #define DW_OP_const4s 0x0d
336 #define DW_OP_const8u 0x0e
337 #define DW_OP_const8s 0x0f
338 #define DW_OP_constu 0x10
339 #define DW_OP_consts 0x11
340 #define DW_OP_dup 0x12
341 #define DW_OP_drop 0x13
342 #define DW_OP_over 0x14
343 #define DW_OP_pick 0x15
344 #define DW_OP_swap 0x16
345 #define DW_OP_rot 0x17
346 #define DW_OP_xderef 0x18
347 #define DW_OP_abs 0x19
348 #define DW_OP_and 0x1a
349 #define DW_OP_div 0x1b
350 #define DW_OP_minus 0x1c
351 #define DW_OP_mod 0x1d
352 #define DW_OP_mul 0x1e
353 #define DW_OP_neg 0x1f
354 #define DW_OP_not 0x20
355 #define DW_OP_or 0x21
356 #define DW_OP_plus 0x22
357 #define DW_OP_plus_uconst 0x23
358 #define DW_OP_shl 0x24
359 #define DW_OP_shr 0x25
360 #define DW_OP_shra 0x26
361 #define DW_OP_xor 0x27
362 #define DW_OP_bra 0x28
363 #define DW_OP_eq 0x29
364 #define DW_OP_ge 0x2a
365 #define DW_OP_gt 0x2b
366 #define DW_OP_le 0x2c
367 #define DW_OP_lt 0x2d
368 #define DW_OP_ne 0x2e
369 #define DW_OP_skip 0x2f
370 #define DW_OP_lit0 0x30
371 #define DW_OP_lit1 0x31
372 #define DW_OP_lit2 0x32
373 #define DW_OP_lit3 0x33
374 #define DW_OP_lit4 0x34
375 #define DW_OP_lit5 0x35
376 #define DW_OP_lit6 0x36
377 #define DW_OP_lit7 0x37
378 #define DW_OP_lit8 0x38
379 #define DW_OP_lit9 0x39
380 #define DW_OP_lit10 0x3a
381 #define DW_OP_lit11 0x3b
382 #define DW_OP_lit12 0x3c
383 #define DW_OP_lit13 0x3d
384 #define DW_OP_lit14 0x3e
385 #define DW_OP_lit15 0x3f
386 #define DW_OP_lit16 0x40
387 #define DW_OP_lit17 0x41
388 #define DW_OP_lit18 0x42
389 #define DW_OP_lit19 0x43
390 #define DW_OP_lit20 0x44
391 #define DW_OP_lit21 0x45
392 #define DW_OP_lit22 0x46
393 #define DW_OP_lit23 0x47
394 #define DW_OP_lit24 0x48
395 #define DW_OP_lit25 0x49
396 #define DW_OP_lit26 0x4a
397 #define DW_OP_lit27 0x4b
398 #define DW_OP_lit28 0x4c
399 #define DW_OP_lit29 0x4d
400 #define DW_OP_lit30 0x4e
401 #define DW_OP_lit31 0x4f
402 #define DW_OP_reg0 0x50
403 #define DW_OP_reg1 0x51
404 #define DW_OP_reg2 0x52
405 #define DW_OP_reg3 0x53
406 #define DW_OP_reg4 0x54
407 #define DW_OP_reg5 0x55
408 #define DW_OP_reg6 0x56
409 #define DW_OP_reg7 0x57
410 #define DW_OP_reg8 0x58
411 #define DW_OP_reg9 0x59
412 #define DW_OP_reg10 0x5a
413 #define DW_OP_reg11 0x5b
414 #define DW_OP_reg12 0x5c
415 #define DW_OP_reg13 0x5d
416 #define DW_OP_reg14 0x5e
417 #define DW_OP_reg15 0x5f
418 #define DW_OP_reg16 0x60
419 #define DW_OP_reg17 0x61
420 #define DW_OP_reg18 0x62
421 #define DW_OP_reg19 0x63
422 #define DW_OP_reg20 0x64
423 #define DW_OP_reg21 0x65
424 #define DW_OP_reg22 0x66
425 #define DW_OP_reg23 0x67
426 #define DW_OP_reg24 0x68
427 #define DW_OP_reg25 0x69
428 #define DW_OP_reg26 0x6a
429 #define DW_OP_reg27 0x6b
430 #define DW_OP_reg28 0x6c
431 #define DW_OP_reg29 0x6d
432 #define DW_OP_reg30 0x6e
433 #define DW_OP_reg31 0x6f
434 #define DW_OP_breg0 0x70
435 #define DW_OP_breg1 0x71
436 #define DW_OP_breg2 0x72
437 #define DW_OP_breg3 0x73
438 #define DW_OP_breg4 0x74
439 #define DW_OP_breg5 0x75
440 #define DW_OP_breg6 0x76
441 #define DW_OP_breg7 0x77
442 #define DW_OP_breg8 0x78
443 #define DW_OP_breg9 0x79
444 #define DW_OP_breg10 0x7a
445 #define DW_OP_breg11 0x7b
446 #define DW_OP_breg12 0x7c
447 #define DW_OP_breg13 0x7d
448 #define DW_OP_breg14 0x7e
449 #define DW_OP_breg15 0x7f
450 #define DW_OP_breg16 0x80
451 #define DW_OP_breg17 0x81
452 #define DW_OP_breg18 0x82
453 #define DW_OP_breg19 0x83
454 #define DW_OP_breg20 0x84
455 #define DW_OP_breg21 0x85
456 #define DW_OP_breg22 0x86
457 #define DW_OP_breg23 0x87
458 #define DW_OP_breg24 0x88
459 #define DW_OP_breg25 0x89
460 #define DW_OP_breg26 0x8a
461 #define DW_OP_breg27 0x8b
462 #define DW_OP_breg28 0x8c
463 #define DW_OP_breg29 0x8d
464 #define DW_OP_breg30 0x8e
465 #define DW_OP_breg31 0x8f
466 #define DW_OP_regx 0x90
467 #define DW_OP_fbreg 0x91
468 #define DW_OP_bregx 0x92
469 #define DW_OP_deref_size 0x94
470 #define DW_OP_xderef_size 0x95
471 #define DW_OP_nop 0x96
472
473 #define DW_LNCT_path 0x1
474 #define DW_LNCT_directory_index 0x2
475
476 struct unwind_item {
477 enum item_location {
478 Same, /* no state */
479 Nowhere, /* no state */
480 Memory, /* signed offset from CFA */
481 Register, /* unsigned register number */
482 Value, /* signed offset from CFA */
483 Expr, /* DWARF expression */
484 ValExpr /* DWARF expression */
485 } where;
486 union {
487 uleb128_t reg;
488 sleb128_t off;
489 const u8 *expr;
490 } state;
491 };
492
493 struct unwind_reg_state {
494 union {
495 struct cfa {
496 uleb128_t reg;
497 sleb128_t off;
498 } cfa;
499 const u8 *cfa_expr;
500 };
501 struct unwind_item regs[ARRAY_SIZE(reg_info)];
502 unsigned cfa_is_expr:1;
503 };
504
505 struct uw_state {
506 uleb128_t loc;
507 uleb128_t codeAlign;
508 sleb128_t dataAlign;
509 unsigned stackDepth:8;
510 struct unwind_reg_state reg[STP_MAX_STACK_DEPTH];
511 struct unwind_item cie_regs[ARRAY_SIZE(reg_info)];
512 };
513
514 struct stp_unwind_context {
515 struct unwind_frame_info info;
516 struct uw_state state;
517 };
518
519 static const struct cfa badCFA = { ARRAY_SIZE(reg_info), 1 };
520 #else /* !STP_USE_DWARF_UNWINDER */
521 struct stp_unwind_context { };
522 #endif /* !STP_USE_DWARF_UNWINDER */
523
524 #ifndef MAXBACKTRACE
525 #define MAXBACKTRACE 20
526 #endif
527
528 struct stp_unwind_cache {
529 enum uwcache_state {
530 uwcache_uninitialized,
531 uwcache_partial,
532 uwcache_finished
533 } state;
534 unsigned depth; /* pc[0..(depth-1)] contains valid entries */
535 unsigned long pc[MAXBACKTRACE];
536 };
537
538 #endif /*_STP_UNWIND_H_*/