2 * kernel stack unwinding
3 * Copyright (C) 2008-2011, 2014 Red Hat Inc.
5 * Based on old kernel code that is
6 * Copyright (C) 2002-2006 Novell, Inc.
7 * Jan Beulich <jbeulich@novell.com>
9 * This code is released under version 2 of the GNU GPL.
11 * This code currently does stack unwinding in the kernel and modules.
12 * It has been extended to handle userspace unwinding using systemtap
16 #include "unwind/unwind.h"
18 static uleb128_t
get_uleb128(const u8
**pcur
, const u8
*end
)
20 const u8
*cur
= *pcur
;
24 for (shift
= 0; cur
< end
; shift
+= 7) {
25 if (shift
+ 7 > 8 * sizeof(value
)
26 && (*cur
& 0x7fU
) >= (1U << (8 * sizeof(value
) - shift
))) {
30 value
|= (uleb128_t
)(*cur
& 0x7f) << shift
;
39 static sleb128_t
get_sleb128(const u8
**pcur
, const u8
*end
)
41 const u8
*cur
= *pcur
;
45 for (shift
= 0; cur
< end
; shift
+= 7) {
46 if (shift
+ 7 > 8 * sizeof(value
)
47 && (*cur
& 0x7fU
) >= (1U << (8 * sizeof(value
) - shift
))) {
51 value
|= (sleb128_t
)(*cur
& 0x7f) << shift
;
53 value
|= -(*cur
++ & 0x40) << shift
;
62 /* Whether this is a real CIE. Assumes CIE (length) sane. */
63 static int has_cie_id(const u32
*cie
, int is_ehframe
)
65 /* CIE id for eh_frame is 0, otherwise 0xffffffff */
66 if (is_ehframe
&& cie
[1] == 0)
68 else if (cie
[1] == 0xffffffff)
74 /* whether this is a real fde or not */
75 static int is_fde(const u32
*fde
, void *table
, uint32_t table_len
,
80 /* check that length is proper */
81 if (!*fde
|| (*fde
& (sizeof(*fde
) - 1))) {
82 _stp_warn("bad fde\n");
86 if (has_cie_id(fde
, is_ehframe
))
89 end
= (const u8
*)(fde
+ 1) + *fde
;
91 /* end should fall within unwind table. */
92 if (((void*)end
) < table
93 || ((void *)end
) > ((void *)(table
+ table_len
))) {
94 _stp_warn("bad fde length\n");
101 /* given an FDE, find its CIE and sanity check */
102 static const u32
*cie_for_fde(const u32
*fde
, void *unwind_data
,
103 uint32_t table_len
, int is_ehframe
)
109 /* CIE_pointer must be a proper offset */
110 if ((fde
[1] & (sizeof(*fde
) - 1)) || fde
[1] > (unsigned long)(fde
+ 1) - (unsigned long)unwind_data
) {
111 _stp_warn("invalid fde[1]=%lx fde+1=%lx, unwind_data=%lx %lx\n",
112 (unsigned long)fde
[1], (unsigned long)(fde
+ 1),
113 (unsigned long)unwind_data
, (unsigned long)(fde
+ 1) - (unsigned long)unwind_data
);
114 return NULL
; /* this is not a valid FDE */
117 /* cie pointer field is different in eh_frame vs debug_frame */
119 cie
= fde
+ 1 - fde
[1] / sizeof(*fde
);
121 cie
= unwind_data
+ fde
[1];
123 /* Make sure address falls in the table */
124 if (((void *)cie
) < ((void*)unwind_data
)
125 || ((void*)cie
) > ((void*)(unwind_data
+ table_len
))) {
126 _stp_warn("cie address falls outside table\n");
130 if (*cie
<= sizeof(*cie
) + 4 || *cie
>= fde
[1] - sizeof(*fde
)
131 || ! has_cie_id(cie
, is_ehframe
)) {
132 _stp_warn("cie is not valid %lx %x %x %x\n", (unsigned long)cie
, *cie
, fde
[1], cie
[1]);
133 return NULL
; /* this is not a (valid) CIE */
136 version
= *(const u8
*)(cie
+ 2);
137 if (version
!= 1 && version
!= 3 && version
!= 4) {
138 _stp_warn ("Unsupported CIE version: %d\n", version
);
142 end
= (const u8
*)(cie
+ 1) + *cie
;
144 /* end should fall within unwind table. */
145 if (((void *)end
) < (void *)unwind_data
146 || ((void *)end
) > ((void *)(unwind_data
+ table_len
))) {
147 _stp_warn ("CIE end falls outside table\n");
154 /* read an encoded pointer and increment *pLoc past the end of the
156 static unsigned long read_ptr_sect(const u8
**pLoc
, const void *end
,
157 signed ptrType
, unsigned long textAddr
,
158 unsigned long dataAddr
, int user
, int compat_task
, int tableSize
)
160 unsigned long value
= 0;
167 const unsigned long *pul
;
168 const unsigned int *pui
;
171 if (ptrType
< 0 || ptrType
== DW_EH_PE_omit
)
175 switch (ptrType
& DW_EH_PE_FORM
) {
177 if (end
< (const void *)(ptr
.p16u
+ 1))
179 if (ptrType
& DW_EH_PE_signed
)
180 value
= _stp_get_unaligned(ptr
.p16s
++);
182 value
= _stp_get_unaligned(ptr
.p16u
++);
187 /* If the tableSize matches the length of data we're trying to return
188 * or if specifically set to 0 in the call it means we actually want a
189 * DW_EH_PE_data4 and not a DW_EH_PE_absptr. If this is not the case
190 * then we want to fall through to DW_EH_PE_absptr */
191 if (!compat_task
|| (compat_task
&& (tableSize
== 4 || tableSize
== 0)))
193 if (end
< (const void *)(ptr
.p32u
+ 1))
196 if (ptrType
& DW_EH_PE_signed
)
197 value
= _stp_get_unaligned(ptr
.p32s
++);
199 value
= _stp_get_unaligned(ptr
.p32u
++);
203 BUILD_BUG_ON(sizeof(u64
) != sizeof(value
));
205 BUILD_BUG_ON(sizeof(u32
) != sizeof(value
));
207 case DW_EH_PE_absptr
:
210 if (end
< (const void *)(ptr
.pui
+ 1))
212 value
= _stp_get_unaligned(ptr
.pui
++);
214 if (end
< (const void *)(ptr
.pul
+ 1))
216 value
= _stp_get_unaligned(ptr
.pul
++);
220 case DW_EH_PE_leb128
:
221 BUILD_BUG_ON(sizeof(uleb128_t
) > sizeof(value
));
222 value
= ptrType
& DW_EH_PE_signed
? get_sleb128(&ptr
.p8
, end
)
223 : get_uleb128(&ptr
.p8
, end
);
224 if ((const void *)ptr
.p8
> end
)
230 switch (ptrType
& DW_EH_PE_ADJUST
) {
231 case DW_EH_PE_absptr
:
234 value
+= (unsigned long)*pLoc
;
236 case DW_EH_PE_textrel
:
239 case DW_EH_PE_datarel
:
245 if ((ptrType
& DW_EH_PE_indirect
)
246 && _stp_read_address(value
, (unsigned long *)value
,
247 (user
? USER_DS
: KERNEL_DS
)))
254 static unsigned long read_pointer(const u8
**pLoc
, const void *end
, signed ptrType
,
255 int user
, int compat_task
)
257 return read_ptr_sect(pLoc
, end
, ptrType
, 0, 0, user
, compat_task
, 0);
260 /* Parse FDE and CIE content. Basic sanity checks should already have
261 been done start/end/version/id (done by is_fde and cie_for_fde).
262 Returns -1 if FDE or CIE cannot be parsed.*/
263 static int parse_fde_cie(const u32
*fde
, const u32
*cie
,
264 void *unwind_data
, uint32_t table_len
,
265 unsigned *ptrType
, int user
,
266 unsigned long *startLoc
, unsigned long *locRange
,
267 const u8
**fdeStart
, const u8
**fdeEnd
,
268 const u8
**cieStart
, const u8
**cieEnd
,
269 uleb128_t
*codeAlign
, sleb128_t
*dataAlign
,
270 uleb128_t
*retAddrReg
, unsigned *call_frame
, int compat_task
)
272 const u8
*ciePtr
= (const u8
*)(cie
+ 2);
273 const u8
*fdePtr
= (const u8
*)(fde
+ 2);
274 unsigned version
= *ciePtr
++;
275 const char *aug
= (const void *)ciePtr
;
276 uleb128_t augLen
= 0; /* Set to non-zero if cie aug starts with z */
278 *cieEnd
= (const u8
*)(cie
+ 1) + *cie
;
279 *fdeEnd
= (const u8
*)(fde
+ 1) + *fde
;
281 /* check if augmentation string is nul-terminated */
282 if ((ciePtr
= memchr(aug
, 0, *cieEnd
- ciePtr
)) == NULL
) {
283 _stp_warn("Unterminated augmentation string\n");
286 ciePtr
++; /* skip aug terminator */
288 *codeAlign
= get_uleb128(&ciePtr
, *cieEnd
);
289 *dataAlign
= get_sleb128(&ciePtr
, *cieEnd
);
290 dbug_unwind(2, "codeAlign=%lx, dataAlign=%lx\n",
291 *codeAlign
, *dataAlign
);
292 if (*codeAlign
== 0 || *dataAlign
== 0) {
293 _stp_warn("zero codeAlign or dataAlign values\n");
297 *retAddrReg
= ((version
<= 1)
298 ? *ciePtr
++ : get_uleb128(&ciePtr
, *cieEnd
));
300 dbug_unwind(1, "map retAddrReg value %ld to reg_info idx %ld\n",
301 *retAddrReg
, COMPAT_REG_MAP(DWARF_REG_MAP(*retAddrReg
)));
302 *retAddrReg
= COMPAT_REG_MAP(DWARF_REG_MAP(*retAddrReg
));
304 dbug_unwind(1, "map retAddrReg value %ld to reg_info idx %ld\n",
305 *retAddrReg
, DWARF_REG_MAP(*retAddrReg
));
306 *retAddrReg
= DWARF_REG_MAP(*retAddrReg
);
310 augLen
= get_uleb128(&ciePtr
, *cieEnd
);
311 if (augLen
> (const u8
*)cie
- *cieEnd
312 || ciePtr
+ augLen
> *cieEnd
) {
313 _stp_warn("Bogus CIE augmentation length\n");
317 *cieStart
= ciePtr
+ augLen
;
319 /* Read augmentation string to determine frame_call and ptrType. */
321 *ptrType
= DW_EH_PE_absptr
;
323 if (ciePtr
> *cieStart
) {
324 _stp_warn("Augmentation data runs past end\n");
334 /* We are not actually interested in
335 the value, so don't try to deref.
336 Mask off DW_EH_PE_indirect. */
337 signed pType
= *ciePtr
++ & 0x7F;
338 if (!read_pointer(&ciePtr
, *cieStart
, pType
, user
, compat_task
)) {
339 _stp_warn("couldn't read personality routine handler\n");
345 *ptrType
= *ciePtr
++;
351 _stp_warn("Unknown augmentation char '%c'\n", *(aug
- 1));
356 if (ciePtr
!= *cieStart
) {
357 _stp_warn("Bogus CIE augmentation data\n");
361 /* Now we finally know the type encoding and whether or not the
362 augmentation string starts with 'z' indicating the FDE might also
363 have some augmentation data, so we can parse the FDE. */
364 *startLoc
= read_pointer(&fdePtr
, *fdeEnd
, *ptrType
, user
, compat_task
);
365 *locRange
= read_pointer(&fdePtr
, *fdeEnd
,
366 *ptrType
& (DW_EH_PE_FORM
| DW_EH_PE_signed
),
368 dbug_unwind(2, "startLoc: %lx, locrange: %lx\n",
369 *startLoc
, *locRange
);
371 /* Skip FDE augmentation length (not interested in data). */
373 augLen
= get_uleb128(&fdePtr
, *fdeEnd
);
374 if (augLen
> (const u8
*)fde
- *fdeEnd
375 || fdePtr
+ augLen
> *fdeEnd
) {
376 _stp_warn("Bogus FDE augmentation length\n");
380 *fdeStart
= fdePtr
+ augLen
;
385 #define REG_STATE state->reg[state->stackDepth]
387 static int advance_loc(unsigned long delta
, struct unwind_state
*state
)
389 state
->loc
+= delta
* state
->codeAlign
;
390 dbug_unwind(1, "state->loc=%lx\n", state
->loc
);
394 /* Set Same or Nowhere rule for register. */
395 static void set_no_state_rule(uleb128_t reg
, enum item_location where
,
396 struct unwind_state
*state
)
398 dbug_unwind(1, "reg=%lx, where=%d\n", reg
, where
);
399 if (reg
< ARRAY_SIZE(REG_STATE
.regs
)) {
400 REG_STATE
.regs
[reg
].where
= where
;
404 /* Memory or Value rule */
405 static void set_offset_rule(uleb128_t reg
, enum item_location where
,
406 sleb128_t svalue
, struct unwind_state
*state
)
408 dbug_unwind(1, "reg=%lx, where=%d, svalue=%lx\n", reg
, where
, svalue
);
409 if (reg
< ARRAY_SIZE(REG_STATE
.regs
)) {
410 REG_STATE
.regs
[reg
].where
= where
;
411 REG_STATE
.regs
[reg
].state
.off
= svalue
;
416 static void set_register_rule(uleb128_t reg
, uleb128_t value
,
417 struct unwind_state
*state
)
419 dbug_unwind(1, "reg=%lx, value=%lx\n", reg
, value
);
420 if (reg
< ARRAY_SIZE(REG_STATE
.regs
)) {
421 REG_STATE
.regs
[reg
].where
= Register
;
422 REG_STATE
.regs
[reg
].state
.reg
= value
;
426 /* Expr or ValExpr rule. */
427 static void set_expr_rule(uleb128_t reg
, enum item_location where
,
428 const u8
**expr
, const u8
*end
,
429 struct unwind_state
*state
)
431 const u8
*const start
= *expr
;
432 uleb128_t len
= get_uleb128(expr
, end
);
433 dbug_unwind(1, "reg=%lx, where=%d, expr=%lu@%p\n",
434 reg
, where
, len
, *expr
);
435 /* Sanity check that expr falls completely inside known data. */
436 if (end
- *expr
>= len
&& reg
< ARRAY_SIZE(REG_STATE
.regs
)) {
437 REG_STATE
.regs
[reg
].where
= where
;
438 REG_STATE
.regs
[reg
].state
.expr
= start
;
443 /* Limit the number of instructions we process. Arbitrary limit.
444 512 should be enough for anybody... */
447 static int processCFI(const u8
*start
, const u8
*end
, unsigned long targetLoc
,
448 signed ptrType
, int user
, struct unwind_state
*state
, int compat_task
)
457 if (end
- start
> MAX_CFI
) {
458 _stp_warn("Too many CFI instuctions\n");
462 dbug_unwind(1, "targetLoc=%lx state->loc=%lx\n", targetLoc
, state
->loc
);
463 for (ptr
.p8
= start
; result
&& ptr
.p8
< end
;) {
464 switch (*ptr
.p8
>> 6) {
471 dbug_unwind(1, "DW_CFA_nop\n");
474 if ((state
->loc
= read_pointer(&ptr
.p8
, end
, ptrType
, user
, compat_task
)) == 0)
476 dbug_unwind(1, "DW_CFA_set_loc %lx (result=%d)\n", state
->loc
, result
);
478 case DW_CFA_advance_loc1
:
479 result
= ptr
.p8
< end
&& advance_loc(*ptr
.p8
++, state
);
480 dbug_unwind(1, "DW_CFA_advance_loc1 (result=%d)\n", result
);
482 case DW_CFA_advance_loc2
:
483 result
= ptr
.p8
<= end
+ 2 && advance_loc(*ptr
.p16
++, state
);
484 dbug_unwind(1, "DW_CFA_advance_loc2 (result=%d)\n", result
);
486 case DW_CFA_advance_loc4
:
487 result
= ptr
.p8
<= end
+ 4 && advance_loc(*ptr
.p32
++, state
);
488 dbug_unwind(1, "DW_CFA_advance_loc4 (result=%d)\n", result
);
490 case DW_CFA_offset_extended
:
491 value
= get_uleb128(&ptr
.p8
, end
);
492 value2
= get_uleb128(&ptr
.p8
, end
);
494 dbug_unwind(1, "map DW_CFA_offset_extended value %ld to reg_info idx %ld, with offset %ld\n",
495 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)), value2
);
496 value
= COMPAT_REG_MAP(DWARF_REG_MAP(value
));
498 dbug_unwind(1, "map DW_CFA_offset_extended value %ld to reg_info idx %ld, with offset %ld\n",
499 value
, DWARF_REG_MAP(value
), value2
);
500 value
= DWARF_REG_MAP(value
);
502 set_offset_rule(value
, Memory
,
503 value2
* state
->dataAlign
,
506 case DW_CFA_val_offset
:
507 value
= get_uleb128(&ptr
.p8
, end
);
508 value2
= get_uleb128(&ptr
.p8
, end
);
510 dbug_unwind(1, "map DW_CFA_val_offset value %ld to reg_info idx %ld\n, with offset: %ld",
511 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)), value2
);
512 value
= COMPAT_REG_MAP(DWARF_REG_MAP(value
));
514 dbug_unwind(1, "map DW_CFA_val_offset value %ld to reg_info idx %ld\n, with offset: %ld",
515 value
, DWARF_REG_MAP(value
), value2
);
516 value
= DWARF_REG_MAP(value
);
518 set_offset_rule(value
, Value
,
519 value2
* state
->dataAlign
,
522 case DW_CFA_offset_extended_sf
:
523 value
= get_uleb128(&ptr
.p8
, end
);
524 svalue
= get_sleb128(&ptr
.p8
, end
);
526 dbug_unwind(1, "map DW_CFA_offset_extended_sf value %ld to reg_info idx %ld, with offset: %ld\n",
527 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)), svalue
);
528 value
= COMPAT_REG_MAP(DWARF_REG_MAP(value
));
530 dbug_unwind(1, "map DW_CFA_offset_extended_sf value %ld to reg_info idx %ld, with offset: %ld\n",
531 value
, DWARF_REG_MAP(value
), svalue
);
532 value
= DWARF_REG_MAP(value
);
534 set_offset_rule(value
, Memory
,
535 svalue
* state
->dataAlign
,
538 case DW_CFA_val_offset_sf
:
539 value
= get_uleb128(&ptr
.p8
, end
);
540 svalue
= get_sleb128(&ptr
.p8
, end
);
542 dbug_unwind(1, "map DW_CFA_val_offset_sf value %ld to reg_info idx %ld\n",
543 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)));
544 value
= COMPAT_REG_MAP(DWARF_REG_MAP(value
));
546 dbug_unwind(1, "map DW_CFA_val_offset_sf value %ld to reg_info idx %ld\n",
547 value
, DWARF_REG_MAP(value
));
548 value
= DWARF_REG_MAP(value
);
550 set_offset_rule(value
, Value
,
551 svalue
* state
->dataAlign
,
554 case DW_CFA_same_value
:
555 value
= get_uleb128(&ptr
.p8
, end
);
557 dbug_unwind(1, "map DW_CFA_same_value value %ld to reg_info idx %ld\n",
558 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)));
559 value
= COMPAT_REG_MAP(DWARF_REG_MAP(value
));
561 dbug_unwind(1, "map DW_CFA_same_value value %ld to reg_info idx %ld\n",
562 value
, DWARF_REG_MAP(value
));
563 value
= DWARF_REG_MAP(value
);
565 set_no_state_rule(value
, Same
, state
);
567 case DW_CFA_restore_extended
:
568 value
= get_uleb128(&ptr
.p8
, end
);
570 dbug_unwind(1, "map DW_CFA_restore_extended value %ld to reg_info idx %ld\n",
571 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)));
572 value
= COMPAT_REG_MAP(DWARF_REG_MAP(value
));
574 dbug_unwind(1, "map DW_CFA_restore_extended value %ld to reg_info idx %ld\n",
575 value
, DWARF_REG_MAP(value
));
576 value
= DWARF_REG_MAP(value
);
578 memcpy(®_STATE
.regs
[value
], &state
->cie_regs
[value
], sizeof(struct unwind_item
));
580 case DW_CFA_undefined
:
581 value
= get_uleb128(&ptr
.p8
, end
);
583 dbug_unwind(1, "map DW_CFA_undefined value %ld to reg_info idx %ld\n",
584 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)));
585 value
= COMPAT_REG_MAP(DWARF_REG_MAP(value
));
587 dbug_unwind(1, "map DW_CFA_undefined value %ld to reg_info idx %ld\n",
588 value
, DWARF_REG_MAP(value
));
589 value
= DWARF_REG_MAP(value
);
591 set_no_state_rule(value
, Nowhere
, state
);
593 case DW_CFA_register
: {
595 value
= get_uleb128(&ptr
.p8
, end
);
596 reg_value
= get_uleb128(&ptr
.p8
, end
);
598 dbug_unwind(1, "map DW_CFA_register value %ld to reg_info idx %ld (reg_value %ld to reg_info idx %ld)\n",
599 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)), reg_value
, COMPAT_REG_MAP(DWARF_REG_MAP(reg_value
)));
600 value
= COMPAT_REG_MAP(DWARF_REG_MAP(value
));
601 reg_value
= DWARF_REG_MAP(reg_value
);
603 dbug_unwind(1, "map DW_CFA_register value %ld to reg_info idx %ld (reg_value %ld to reg_info idx %ld)\n",
604 value
, DWARF_REG_MAP(value
), reg_value
, DWARF_REG_MAP(reg_value
));
605 value
= DWARF_REG_MAP(value
);
606 reg_value
= DWARF_REG_MAP(reg_value
);
608 set_register_rule(value
, reg_value
, state
);
611 case DW_CFA_expression
:
612 value
= get_uleb128(&ptr
.p8
, end
);
614 dbug_unwind(1, "map DW_CFA_expression value %ld to reg_info idx %ld\n",
615 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)));
616 value
= COMPAT_REG_MAP(DWARF_REG_MAP(value
));
618 dbug_unwind(1, "map DW_CFA_expression value %ld to reg_info idx %ld\n",
619 value
, DWARF_REG_MAP(value
));
620 value
= DWARF_REG_MAP(value
);
622 set_expr_rule(value
, Expr
, &ptr
.p8
, end
, state
);
624 case DW_CFA_val_expression
:
625 value
= get_uleb128(&ptr
.p8
, end
);
627 dbug_unwind(1, "map DW_CFA_val_expression value %ld to reg_info idx %ld\n",
628 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)));
629 value
= COMPAT_REG_MAP(DWARF_REG_MAP(value
));
631 dbug_unwind(1, "map DW_CFA_val_expression value %ld to reg_info idx %ld\n",
632 value
, DWARF_REG_MAP(value
));
633 value
= DWARF_REG_MAP(value
);
635 set_expr_rule(value
, ValExpr
, &ptr
.p8
, end
,
638 case DW_CFA_remember_state
:
640 if (state
->stackDepth
>= STP_MAX_STACK_DEPTH
) {
641 _stp_warn("Too many stacked DW_CFA_remember_state\n");
645 &state
->reg
[state
->stackDepth
- 1],
647 dbug_unwind(1, "DW_CFA_remember_state (stackDepth=%d)\n", state
->stackDepth
);
649 case DW_CFA_restore_state
:
650 if (state
->stackDepth
== 0) {
651 _stp_warn("Unbalanced DW_CFA_restore_state\n");
655 dbug_unwind(1, "DW_CFA_restore_state (stackDepth=%d)\n", state
->stackDepth
);
658 value
= get_uleb128(&ptr
.p8
, end
);
659 REG_STATE
.cfa_is_expr
= 0;
661 dbug_unwind(1, "map DW_CFA_def_cfa value %ld to reg_info idx %ld\n",
662 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)));
663 REG_STATE
.cfa
.reg
= COMPAT_REG_MAP(value
);
664 dbug_unwind(1, "DW_CFA_def_cfa reg=%ld\n", COMPAT_REG_MAP(REG_STATE
.cfa
.reg
));
666 dbug_unwind(1, "map DW_CFA_def_cfa value %ld to reg_info idx %ld\n",
667 value
, DWARF_REG_MAP(value
));
668 REG_STATE
.cfa
.reg
= value
;
669 dbug_unwind(1, "DW_CFA_def_cfa reg=%ld\n", REG_STATE
.cfa
.reg
);
672 case DW_CFA_def_cfa_offset
:
673 if (REG_STATE
.cfa_is_expr
!= 0) {
674 _stp_warn("Unexpected DW_CFA_def_cfa_offset\n");
676 /* non-factored uleb128 */
677 REG_STATE
.cfa
.off
= get_uleb128(&ptr
.p8
, end
);
678 dbug_unwind(1, "DW_CFA_def_cfa_offset offs=%lx\n", REG_STATE
.cfa
.off
);
681 case DW_CFA_def_cfa_sf
:
682 value
= get_uleb128(&ptr
.p8
, end
);
683 REG_STATE
.cfa_is_expr
= 0;
685 dbug_unwind(1, "map DW_CFA_def_cfa_sf value %ld to reg_info idx %ld\n",
686 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)));
687 REG_STATE
.cfa
.reg
= COMPAT_REG_MAP(value
);
689 dbug_unwind(1, "map DW_CFA_def_cfa_sf value %ld to reg_info idx %ld\n",
690 value
, DWARF_REG_MAP(value
));
691 REG_STATE
.cfa
.reg
= value
;
694 case DW_CFA_def_cfa_offset_sf
:
695 if (REG_STATE
.cfa_is_expr
!= 0) {
696 _stp_warn("Unexpected DW_CFA_def_cfa_offset_sf\n");
698 /* factored sleb128 */
699 REG_STATE
.cfa
.off
= get_sleb128(&ptr
.p8
, end
) * state
->dataAlign
;
700 dbug_unwind(1, "DW_CFA_def_cfa_offset_sf offs=%lx\n", REG_STATE
.cfa
.off
);
703 case DW_CFA_def_cfa_register
:
704 if (REG_STATE
.cfa_is_expr
!= 0) {
705 _stp_warn("Unexpected DW_CFA_def_cfa_register\n");
707 value
= get_uleb128(&ptr
.p8
, end
);
709 dbug_unwind(1, "map DW_CFA_def_cfa_register value %ld to reg_info idx %ld (%ld)\n",
710 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)), DWARF_REG_MAP(value
));
711 REG_STATE
.cfa
.reg
= COMPAT_REG_MAP(value
);
713 dbug_unwind(1, "map DW_CFA_def_cfa_register value %ld to reg_info idx %ld (%ld)\n",
714 value
, DWARF_REG_MAP(value
), DWARF_REG_MAP(value
));
715 REG_STATE
.cfa
.reg
= value
;
719 case DW_CFA_def_cfa_expression
: {
720 const u8
*cfa_expr
= ptr
.p8
;
721 value
= get_uleb128(&ptr
.p8
, end
);
722 /* Sanity check that cfa_expr falls completely
723 inside known data. */
724 if (ptr
.p8
< end
&& end
- ptr
.p8
>= value
) {
725 REG_STATE
.cfa_is_expr
= 1;
726 REG_STATE
.cfa_expr
= cfa_expr
;
728 dbug_unwind(1, "DW_CFA_def_cfa_expression %lu@%p\n", value
, cfa_expr
);
731 _stp_warn("BAD DW_CFA_def_cfa_expression value %lu\n", value
);
734 /* Size of all arguments pushed on the stack. */
735 case DW_CFA_GNU_args_size
:
736 get_uleb128(&ptr
.p8
, end
);
737 dbug_unwind(1, "DW_CFA_GNU_args_size\n");
739 /* This is only produced by GCC before 2002.
740 Like DW_CFA_offset_extended_sf but using an
741 uleb128 that is subtracted from CFA. */
742 case DW_CFA_GNU_negative_offset_extended
:
743 value
= get_uleb128(&ptr
.p8
, end
);
745 dbug_unwind(1, "map DW_CFA_GNU_negative_offset_extended value %ld to reg_info idx %ld\n",
746 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)));
747 value
= COMPAT_REG_MAP(DWARF_REG_MAP(value
));
749 dbug_unwind(1, "map DW_CFA_GNU_negative_offset_extended value %ld to reg_info idx %ld\n",
750 value
, DWARF_REG_MAP(value
));
751 value
= DWARF_REG_MAP(value
);
753 set_offset_rule(value
, Memory
,
754 (uleb128_t
)0 - get_uleb128(&ptr
.p8
, end
), state
);
756 case DW_CFA_GNU_window_save
:
758 _stp_warn("unimplemented call frame instruction: 0x%x\n", *(ptr
.p8
- 1));
764 result
= advance_loc(*ptr
.p8
++ & 0x3f, state
);
765 dbug_unwind(1, "DW_CFA_advance_loc\n");
768 value
= *ptr
.p8
++ & 0x3f;
770 dbug_unwind(1, "map DW_CFA_offset value %ld to reg_info idx %ld\n",
771 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)));
772 value
= COMPAT_REG_MAP(DWARF_REG_MAP(value
));
774 dbug_unwind(1, "map DW_CFA_offset value %ld to reg_info idx %ld\n",
775 value
, DWARF_REG_MAP(value
));
776 value
= DWARF_REG_MAP(value
);
778 value2
= get_uleb128(&ptr
.p8
, end
);
779 set_offset_rule(value
, Memory
,
780 value2
* state
->dataAlign
, state
);
783 value
= *ptr
.p8
++ & 0x3f;
785 dbug_unwind(1, "map DW_CFA_restore value %ld to reg_info idx %ld\n",
786 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)));
787 value
= COMPAT_REG_MAP(DWARF_REG_MAP(value
));
789 dbug_unwind(1, "map DW_CFA_restore value %ld to reg_info idx %ld\n",
790 value
, DWARF_REG_MAP(value
));
791 value
= DWARF_REG_MAP(value
);
793 memcpy(®_STATE
.regs
[value
], &state
->cie_regs
[value
], sizeof(struct unwind_item
));
796 dbug_unwind(1, "targetLoc=%lx state->loc=%lx\n", targetLoc
, state
->loc
);
797 dbug_unwind(1, "result: %d\n", result
);
800 if (result
&& targetLoc
!= 0 && targetLoc
< state
->loc
)
803 return result
&& ptr
.p8
== end
;
807 static const char *_stp_enc_hi_name
[] = {
815 static const char *_stp_enc_lo_name
[] = {
826 static char *_stp_eh_enc_name(signed type
)
830 if (type
== DW_EH_PE_omit
)
831 return "DW_EH_PE_omit";
833 hi
= (type
& DW_EH_PE_ADJUST
) >> 4;
834 low
= type
& DW_EH_PE_FORM
;
835 if (hi
> 5 || low
> 4 || (low
== 0 && (type
& DW_EH_PE_signed
))) {
836 snprintf(buf
, sizeof(buf
), "ERROR:encoding=0x%x", type
);
841 if (type
& DW_EH_PE_indirect
)
842 strlcpy(buf
, "DW_EH_PE_indirect|", sizeof(buf
));
843 strlcat(buf
, _stp_enc_hi_name
[hi
], sizeof(buf
));
845 if (type
& DW_EH_PE_signed
)
847 strlcat(buf
, _stp_enc_lo_name
[low
], sizeof(buf
));
850 #endif /* DEBUG_UNWIND */
852 // If this is an address inside a module, adjust for section relocation
853 // and the elfutils base relocation done during loading of the .dwarf_frame
856 adjustStartLoc (unsigned long startLoc
,
857 struct _stp_module
*m
,
858 struct _stp_section
*s
,
859 unsigned ptrType
, int is_ehframe
, int user
)
861 unsigned long vm_addr
= 0;
863 /* If we're unwinding the current module, then the addresses
864 we've got don't require adjustment, they didn't come from user
866 if(strcmp(THIS_MODULE
->name
,m
->name
)==0)
869 /* XXX - some, or all, of this should really be done by
870 _stp_module_relocate and/or read_pointer. */
871 dbug_unwind(2, "adjustStartLoc=%lx, ptrType=%s, m=%s, s=%s eh=%d\n",
872 startLoc
, _stp_eh_enc_name(ptrType
), m
->path
, s
->name
, is_ehframe
);
874 || strcmp (m
->name
, "kernel") == 0
875 || (strcmp (s
->name
, ".absolute") == 0 && !is_ehframe
))
878 /* eh_frame data has been loaded in the kernel, so readjust offset. */
880 dbug_unwind(2, "eh_frame=%lx, eh_frame_addr=%lx\n", (unsigned long) m
->eh_frame
, m
->eh_frame_addr
);
881 if ((ptrType
& DW_EH_PE_ADJUST
) == DW_EH_PE_pcrel
) {
882 startLoc
-= (unsigned long) m
->eh_frame
;
883 startLoc
+= m
->eh_frame_addr
;
885 /* User space exec */
886 if (strcmp (s
->name
, ".absolute") == 0)
890 /* User space or kernel dynamic module. */
891 if (user
&& strcmp (s
->name
, ".dynamic") == 0)
892 stap_find_vma_map_info_user(current
->group_leader
, m
, &vm_addr
, NULL
, NULL
);
894 vm_addr
= s
->static_addr
;
897 return startLoc
+ vm_addr
;
899 return startLoc
+ vm_addr
- s
->sec_load_offset
;
903 /* If we previously created an unwind header, then use it now to binary search */
904 /* for the FDE corresponding to pc. */
905 static u32
*_stp_search_unwind_hdr(unsigned long pc
,
906 struct _stp_module
*m
,
907 struct _stp_section
*s
,
908 int is_ehframe
, int user
, int compat_task
)
910 const u8
*ptr
, *end
, *hdr
= is_ehframe
? m
->unwind_hdr
: s
->debug_hdr
;
911 uint32_t hdr_len
= is_ehframe
? m
->unwind_hdr_len
: s
->debug_hdr_len
;
912 unsigned long startLoc
;
914 unsigned num
, tableSize
, t2
;
915 unsigned long eh_hdr_addr
= m
->unwind_hdr_addr
;
917 if (hdr
== NULL
|| hdr_len
< 4 || hdr
[0] != 1) {
918 _stp_warn("no or bad debug frame hdr\n");
922 dbug_unwind(1, "binary search for %lx", pc
);
925 switch (hdr
[3] & DW_EH_PE_FORM
) {
926 case DW_EH_PE_absptr
:
928 tableSize
= sizeof(unsigned long);
930 tableSize
= sizeof(unsigned int);
942 _stp_warn("bad unwind table encoding");
948 // XXX Can the header validity be checked just once?
949 unsigned long eh
= read_ptr_sect(&ptr
, end
, hdr
[1], 0,
950 eh_hdr_addr
, user
, compat_task
, tableSize
);
951 if ((hdr
[1] & DW_EH_PE_ADJUST
) == DW_EH_PE_pcrel
)
952 eh
= eh
- (unsigned long)hdr
+ eh_hdr_addr
;
953 if ((is_ehframe
&& eh
!= (unsigned long)m
->eh_frame_addr
)) {
954 _stp_warn("eh_frame_ptr in eh_frame_hdr 0x%lx not valid; eh_frame_addr = 0x%lx", eh
, (unsigned long)m
->eh_frame_addr
);
958 num
= read_ptr_sect(&ptr
, end
, hdr
[2], 0, eh_hdr_addr
, user
, compat_task
, tableSize
);
959 if (num
== 0 || num
!= (end
- ptr
) / (2 * tableSize
)
960 || (end
- ptr
) % (2 * tableSize
)) {
961 _stp_warn("unwind Bad num=%d end-ptr=%ld 2*tableSize=%d",
962 num
, (long)(end
- ptr
), 2 * tableSize
);
967 const u8
*cur
= ptr
+ (num
/ 2) * (2 * tableSize
);
968 startLoc
= read_ptr_sect(&cur
, cur
+ tableSize
, hdr
[3], 0,
969 eh_hdr_addr
, user
, compat_task
, tableSize
);
970 startLoc
= adjustStartLoc(startLoc
, m
, s
, hdr
[3],
975 ptr
= cur
- tableSize
;
978 } while (startLoc
&& num
> 1);
981 && (startLoc
= adjustStartLoc(read_ptr_sect(&ptr
, ptr
+ tableSize
, hdr
[3], 0,
982 eh_hdr_addr
, user
, compat_task
, tableSize
),
983 m
, s
, hdr
[3], is_ehframe
, user
)) != 0 && pc
>= startLoc
) {
985 off
= read_ptr_sect(&ptr
, ptr
+ tableSize
, hdr
[3],
986 0, eh_hdr_addr
, user
, compat_task
, tableSize
);
987 dbug_unwind(1, "fde off=%lx\n", off
);
988 /* For real eh_frame_hdr the actual fde address is at the
989 new eh_frame load address. For our own debug_hdr created
990 table the fde is an offset into the debug_frame table. */
992 fde
= off
- m
->eh_frame_addr
+ m
->eh_frame
;
994 fde
= m
->debug_frame
+ off
;
997 dbug_unwind(1, "returning fde=%lx startLoc=%lx", (unsigned long) fde
, startLoc
);
1001 #define FRAME_REG(r, t) (((t *)frame)[reg_info[r].offs])
1003 #ifndef CONFIG_64BIT
1004 # define CASES CASE(8); CASE(16); CASE(32)
1006 # define CASES CASE(8); CASE(16); CASE(32); CASE(64)
1009 #define MAX_EXPR_STACK 8 /* arbitrary */
1011 static int compute_expr(const u8
*expr
, struct unwind_frame_info
*frame
,
1012 unsigned long *result
, int user
, int compat_task
)
1015 * We previously validated the length, so we won't read off the end.
1016 * See sanity checks in set_expr() and for DW_CFA_def_cfa_expression.
1018 uleb128_t len
= get_uleb128(&expr
, (const u8
*) -1UL);
1019 const u8
*const start
= expr
;
1020 const u8
*const end
= expr
+ len
;
1022 long stack
[MAX_EXPR_STACK
]; /* stack slots are signed */
1023 unsigned int sp
= 0;
1024 #define PUSH(val) do { \
1025 if (sp == MAX_EXPR_STACK) \
1027 stack[sp++] = (val); \
1034 #define NEED(n) do { \
1035 if (end - expr < (n)) \
1039 while (expr
< end
) {
1051 const u8 op
= *expr
++;
1052 dbug_unwind(3, " expr op 0x%x (%ld left)\n", op
, (long)(end
- expr
));
1062 NEED(sizeof(u
.s16
));
1063 memcpy(&u
.s16
, expr
, sizeof(u
.s16
));
1064 expr
+= sizeof(u
.s16
);
1066 unlikely(expr
- start
< -u
.s16
) :
1067 unlikely(end
- expr
< u
.s16
)) {
1068 _stp_warn("invalid skip %d in CFI expression\n", (int) u
.s16
);
1072 * A backward branch could lead to an infinite loop.
1073 * So punt it until we find we actually need it.
1076 _stp_warn("backward branch in CFI expression not supported\n");
1091 unsigned long tos
= POP
;
1092 unsigned long nos
= POP
;
1107 value
= stack
[sp
- value
];
1111 #define CONSTANT(type) \
1112 NEED(sizeof(u.type)); \
1113 memcpy(&u.type, expr, sizeof(u.type)); \
1114 expr += sizeof(u.type); \
1120 if (sizeof(unsigned long) == 8) { /* XXX 32/64!! */
1127 case DW_OP_const1u
: CONSTANT(u8
);
1128 case DW_OP_const1s
: CONSTANT(s8
);
1129 case DW_OP_const2u
: CONSTANT(u16
);
1130 case DW_OP_const2s
: CONSTANT(s16
);
1131 case DW_OP_const4u
: CONSTANT(u32
);
1132 case DW_OP_const4s
: CONSTANT(s32
);
1133 case DW_OP_const8u
: CONSTANT(u64
);
1134 case DW_OP_const8s
: CONSTANT(s64
);
1139 value
= get_uleb128(&expr
, end
);
1143 value
= get_sleb128(&expr
, end
);
1147 case DW_OP_lit0
... DW_OP_lit31
:
1148 PUSH(op
- DW_OP_lit0
);
1151 case DW_OP_plus_uconst
:
1152 value
= get_uleb128(&expr
, end
);
1156 #define BINOP(name, operator) \
1157 case DW_OP_##name: { \
1160 PUSH(a operator b); \
1181 unsigned long b
= POP
;
1182 unsigned long a
= POP
;
1197 unsigned long b
= POP
;
1198 unsigned long a
= POP
;
1215 value
= get_uleb128(&expr
, end
);
1217 case DW_OP_breg0
... DW_OP_breg31
:
1218 value
= op
- DW_OP_breg0
;
1221 dbug_unwind(1, "map DW_OP_breg value %ld to reg_info idx %ld\n",
1222 value
, COMPAT_REG_MAP(DWARF_REG_MAP(value
)));
1223 value
= COMPAT_REG_MAP(DWARF_REG_MAP(value
));
1225 dbug_unwind(1, "map DW_OP_breg value %ld to reg_info idx %ld\n",
1226 value
, DWARF_REG_MAP(value
));
1227 value
= DWARF_REG_MAP(value
);
1229 if (unlikely(value
>= ARRAY_SIZE(reg_info
))) {
1230 _stp_warn("invalid register number %lu in CFI expression\n", value
);
1233 sleb128_t offset
= get_sleb128(&expr
, end
);
1234 value
= FRAME_REG(value
, unsigned long);
1235 PUSH(value
+ offset
);
1240 value
= sizeof(long); /* XXX 32/64!! */
1242 case DW_OP_deref_size
:
1245 if (unlikely(value
> sizeof(stack
[0]))) {
1247 _stp_warn("invalid DW_OP_deref_size %lu in CFI expression\n", value
);
1251 unsigned long addr
= POP
;
1253 #define CASE(n) case sizeof(u##n): \
1254 if (unlikely(_stp_read_address(value, (u##n *)addr, \
1255 (user ? USER_DS : KERNEL_DS)))) \
1261 goto bad_deref_size
;
1268 _stp_warn("unimplemented CFI expression operation: 0x%x\n", op
);
1277 _stp_warn("_stp_read_address failed to access memory for deref\n");
1280 _stp_warn("invalid (truncated) DWARF expression in CFI\n");
1283 _stp_warn("DWARF expression stack overflow in CFI\n");
1286 _stp_warn("DWARF expression stack underflow in CFI\n");
1289 _stp_warn("DWARF expression stack divide by zero in CFI\n");
1297 /* Unwind to previous to frame. Returns 0 if successful, negative
1298 * number in case of an error. A positive return means unwinding is finished;
1299 * don't try to fallback to dumping addresses on the stack. */
1300 static int unwind_frame(struct unwind_context
*context
,
1301 struct _stp_module
*m
, struct _stp_section
*s
,
1302 void *table
, uint32_t table_len
, int is_ehframe
,
1303 int user
, int compat_task
)
1305 const u32
*fde
= NULL
, *cie
= NULL
;
1306 /* The start and end of the CIE CFI instructions. */
1307 const u8
*cieStart
= NULL
, *cieEnd
= NULL
;
1308 /* The start and end of the FDE CFI instructions. */
1309 const u8
*fdeStart
= NULL
, *fdeEnd
= NULL
;
1310 struct unwind_frame_info
*frame
= &context
->info
;
1311 unsigned long pc
= UNW_PC(frame
) - frame
->call_frame
;
1312 unsigned long startLoc
= 0, endLoc
= 0, locRange
= 0, cfa
;
1314 signed ptrType
= -1, call_frame
= 1;
1315 uleb128_t retAddrReg
= 0;
1316 struct unwind_state
*state
= &context
->state
;
1319 if (unlikely(table_len
== 0)) {
1320 // Don't _stp_warn about this, debug_frame and/or eh_frame
1321 // might actually not be there.
1322 dbug_unwind(1, "Module %s: no unwind frame data", m
->path
);
1325 if (unlikely(table_len
& (sizeof(*fde
) - 1))) {
1326 _stp_warn("Module %s: frame_len=%d", m
->path
, table_len
);
1330 /* Sets all rules to default Same value. */
1331 memset(state
, 0, sizeof(*state
));
1333 /* All "fake" dwarf registers should start out Nowhere. */
1334 for (i
= UNW_NR_REAL_REGS
; i
< ARRAY_SIZE(REG_STATE
.regs
); ++i
)
1335 set_no_state_rule(i
, Nowhere
, state
);
1337 fde
= _stp_search_unwind_hdr(pc
, m
, s
, is_ehframe
, user
, compat_task
);
1338 dbug_unwind(1, "%s: fde=%lx\n", m
->path
, (unsigned long) fde
);
1340 /* found the fde, now set startLoc and endLoc */
1341 if (fde
!= NULL
&& is_fde(fde
, table
, table_len
, is_ehframe
)) {
1342 cie
= cie_for_fde(fde
, table
, table_len
, is_ehframe
);
1343 dbug_unwind(1, "%s: cie=%lx\n", m
->path
, (unsigned long) cie
);
1344 if (likely(cie
!= NULL
)) {
1345 if (parse_fde_cie(fde
, cie
, table
, table_len
,
1347 &startLoc
, &locRange
,
1356 startLoc
= adjustStartLoc(startLoc
, m
, s
, ptrType
, is_ehframe
, user
);
1357 endLoc
= startLoc
+ locRange
;
1358 dbug_unwind(1, "startLoc: %lx, endLoc: %lx\n", startLoc
, endLoc
);
1360 dbug_unwind(1, "pc (%lx) > endLoc(%lx)\n", pc
, endLoc
);
1364 _stp_warn("fde found in header, but cie is bad!\n");
1367 } else if ((is_ehframe
? m
->unwind_hdr
: s
->debug_hdr
) == NULL
) {
1368 /* Only do a linear search if there isn't a search header.
1369 There always should be one, we create it in the translator
1370 if it didn't exist. These should never be missing except
1371 when there are toolchain bugs. */
1372 unsigned long tableSize
;
1373 _stp_warn("No binary search table for %s frame, doing slow linear search for %s\n", (is_ehframe
? "eh" : "debug"), m
->path
);
1374 for (fde
= table
, tableSize
= table_len
; cie
= NULL
, tableSize
> sizeof(*fde
)
1375 && tableSize
- sizeof(*fde
) >= *fde
; tableSize
-= sizeof(*fde
) + *fde
, fde
+= 1 + *fde
/ sizeof(*fde
)) {
1376 dbug_unwind(3, "fde=%lx tableSize=%d\n", (long)*fde
, (int)tableSize
);
1377 if (!is_fde(fde
, table
, table_len
, is_ehframe
))
1379 cie
= cie_for_fde(fde
, table
, table_len
, is_ehframe
);
1381 || parse_fde_cie(fde
, cie
, table
, table_len
,
1383 &startLoc
, &locRange
,
1389 &call_frame
, compat_task
) < 0)
1391 startLoc
= adjustStartLoc(startLoc
, m
, s
, ptrType
, is_ehframe
, user
);
1394 endLoc
= startLoc
+ locRange
;
1395 // removal because this is a way of checking if the next fde is in range, if the fde's aren't sorted (which is why we're doing a linear search in the first place, than this check is bogus
1396 /*if (pc > endLoc) {
1397 dbug_unwind(1, "pc (%lx) > endLoc(%lx)\n", pc, endLoc);
1400 dbug_unwind(3, "endLoc=%lx\n", endLoc
);
1401 if (pc
>= startLoc
&& pc
< endLoc
)
1406 dbug_unwind(1, "cie=%lx fde=%lx startLoc=%lx endLoc=%lx, pc=%lx\n",
1407 (unsigned long) cie
, (unsigned long)fde
, (unsigned long) startLoc
, (unsigned long) endLoc
, pc
);
1408 if (cie
== NULL
|| fde
== NULL
)
1411 /* found the CIE and FDE */
1413 // Sanity check return address register value.
1414 if (retAddrReg
>= ARRAY_SIZE(reg_info
)
1415 || REG_INVALID(retAddrReg
)
1416 || reg_info
[retAddrReg
].width
!= sizeof(unsigned long)) {
1417 _stp_warn("Bad retAddrReg value\n");
1421 frame
->call_frame
= call_frame
;
1422 state
->stackDepth
= 0;
1423 state
->loc
= startLoc
;
1424 memcpy(®_STATE
.cfa
, &badCFA
, sizeof(REG_STATE
.cfa
));
1426 /* Common Information Entry (CIE) instructions. */
1427 dbug_unwind (1, "processCFI for CIE\n");
1428 if (!processCFI(cieStart
, cieEnd
, 0, ptrType
, user
, state
, compat_task
))
1431 /* Store initial state registers for use with DW_CFA_restore... */
1432 memcpy(&state
->cie_regs
, ®_STATE
.regs
, sizeof (REG_STATE
.regs
));
1434 /* Process Frame Description Entry (FDE) instructions. */
1435 dbug_unwind (1, "processCFI for FDE\n");
1436 if (!processCFI(fdeStart
, fdeEnd
, pc
, ptrType
, user
, state
, compat_task
)
1437 || state
->loc
> endLoc
1438 || REG_STATE
.regs
[retAddrReg
].where
== Nowhere
)
1442 if (REG_STATE
.cfa_is_expr
) {
1443 if (compute_expr(REG_STATE
.cfa_expr
, frame
, &cfa
, user
, compat_task
))
1447 // We expect the offset to be a multiple of the address size
1448 if(REG_STATE
.cfa
.reg
>= ARRAY_SIZE(reg_info
)
1449 || reg_info
[REG_STATE
.cfa
.reg
].width
!= sizeof(unsigned long)
1450 || REG_STATE
.cfa
.off
% (sizeof(unsigned long)/2))
1453 dbug_unwind(1, "cfa reg=%ld, off=%lx\n",
1454 REG_STATE
.cfa
.reg
, REG_STATE
.cfa
.off
);
1455 cfa
= FRAME_REG(REG_STATE
.cfa
.reg
, unsigned long) + REG_STATE
.cfa
.off
;
1457 startLoc
= min((unsigned long)UNW_SP(frame
), cfa
);
1458 endLoc
= max((unsigned long)UNW_SP(frame
), cfa
);
1459 dbug_unwind(1, "cfa=%lx startLoc=%lx, endLoc=%lx\n", cfa
, startLoc
, endLoc
);
1460 if (STACK_LIMIT(startLoc
) != STACK_LIMIT(endLoc
)) {
1461 startLoc
= min(STACK_LIMIT(cfa
), cfa
);
1462 endLoc
= max(STACK_LIMIT(cfa
), cfa
);
1463 dbug_unwind(1, "cfa startLoc=%lx, endLoc=%lx\n",
1464 (unsigned long)startLoc
, (unsigned long)endLoc
);
1466 dbug_unwind(1, "cie=%lx fde=%lx\n", (unsigned long) cie
, (unsigned long) fde
);
1467 for (i
= 0; i
< ARRAY_SIZE(REG_STATE
.regs
); ++i
) {
1468 if (REG_INVALID(i
)) {
1469 if (REG_STATE
.regs
[i
].where
== Nowhere
)
1471 _stp_warn("REG_INVALID %d\n", i
);
1474 dbug_unwind(2, "register %d. where=%d\n", i
, REG_STATE
.regs
[i
].where
);
1475 switch (REG_STATE
.regs
[i
].where
) {
1479 if (REG_STATE
.regs
[i
].state
.reg
>= ARRAY_SIZE(reg_info
)
1480 || REG_INVALID(REG_STATE
.regs
[i
].state
.reg
)
1481 || reg_info
[i
].width
> reg_info
[REG_STATE
.regs
[i
].state
.reg
].width
) {
1482 _stp_warn("case Register bad\n");
1485 switch (reg_info
[REG_STATE
.regs
[i
].state
.reg
].width
) {
1487 case sizeof(u##n): \
1488 REG_STATE.regs[i].state.reg = FRAME_REG(REG_STATE.regs[i].state.reg, \
1494 _stp_warn("bad Register size\n");
1500 for (i
= 0; i
< ARRAY_SIZE(REG_STATE
.regs
); ++i
) {
1501 dbug_unwind(2, "register %d. invalid=%d\n", i
, REG_INVALID(i
));
1504 dbug_unwind(2, "register %d. where=%d\n", i
, REG_STATE
.regs
[i
].where
);
1506 #if (UNW_SP_FROM_CFA == 1)
1507 if (i
== UNW_SP_IDX
) {
1508 UNW_SP(frame
) = cfa
;
1513 #if (UNW_PC_FROM_RA == 1)
1514 if (i
== UNW_PC_IDX
) {
1515 UNW_PC(frame
) = FRAME_REG(retAddrReg
, unsigned long);
1520 switch (REG_STATE
.regs
[i
].where
) {
1522 /* Preserve register from current frame. */
1525 switch (reg_info
[i
].width
) {
1526 #define CASE(n) case sizeof(u##n): \
1527 FRAME_REG(i, u##n) = 0; \
1532 _stp_warn("bad Register size (Nowhere)\n");
1537 switch (reg_info
[i
].width
) {
1538 #define CASE(n) case sizeof(u##n): \
1539 FRAME_REG(i, u##n) = REG_STATE.regs[i].state.reg; \
1544 _stp_warn("bad Register size (Register)\n");
1549 if (compute_expr(REG_STATE
.regs
[i
].state
.expr
, frame
, &addr
, user
, compat_task
))
1553 if (compute_expr(REG_STATE
.regs
[i
].state
.expr
, frame
, &addr
, user
, compat_task
))
1557 addr
= cfa
+ REG_STATE
.regs
[i
].state
.off
;
1559 if (reg_info
[i
].width
!= sizeof(unsigned long)) {
1560 _stp_warn("bad Register width for value state\n");
1563 FRAME_REG(i
, unsigned long) = addr
;
1566 addr
= cfa
+ REG_STATE
.regs
[i
].state
.off
;
1568 dbug_unwind(2, "addr=%lx width=%d\n", addr
, reg_info
[i
].width
);
1569 /* We only want the lower half of the address defined, however
1570 _stp_read_address will sometimes return garbage in the top half.
1571 for 32-on-64 bit unwinding we need to ensure this is 0xFFFFFFFF */
1572 switch (reg_info
[i
].width
) {
1573 #define CASE(n) case sizeof(u##n): \
1574 if (unlikely(_stp_read_address(FRAME_REG(i, u##n), (u##n *)addr, \
1575 (user ? USER_DS : KERNEL_DS)))) \
1577 if (compat_task) FRAME_REG(i, u##n) &= 0xFFFFFFFF; \
1578 dbug_unwind(1, "set register %d to %lx\n", i, (long)FRAME_REG(i,u##n)); \
1583 _stp_warn("bad Register width\n");
1589 dbug_unwind(1, "returning 0 (%lx)\n", UNW_PC(frame
));
1593 _stp_warn("_stp_read_address failed to access memory location\n");
1598 /* PC was in a range convered by a module but no unwind info */
1599 /* found for the specific PC. This seems to happen only for kretprobe */
1600 /* trampolines and at the end of interrupt backtraces. */
1606 static int unwind(struct unwind_context
*context
, int user
)
1608 struct _stp_module
*m
;
1609 struct _stp_section
*s
= NULL
;
1610 struct unwind_frame_info
*frame
= &context
->info
;
1611 unsigned long pc
= UNW_PC(frame
) - frame
->call_frame
;
1613 const char *module_name
= 0;
1614 /* compat_task is a flag for 32bit process unwinding on a 64-bit
1615 architecture. If this flag is set, it means a mapping of
1616 register numbers is required, as well as being aware of 32-bit
1617 values on 64-bit registers. */
1618 int compat_task
= _stp_is_compat_task();
1620 dbug_unwind(1, "pc=%lx, %lx", pc
, UNW_PC(frame
));
1622 if (UNW_PC(frame
) == 0)
1627 m
= _stp_umod_lookup (pc
, current
, & module_name
, NULL
, NULL
);
1629 s
= &m
->sections
[0];
1633 m
= _stp_kmod_sec_lookup (pc
, &s
);
1635 #ifdef STAPCONF_MODULE_TEXT_ADDRESS
1638 ko
= __module_text_address (pc
);
1639 if (ko
) { module_name
= ko
->name
; }
1641 /* Possible heuristic: we could assume we're talking
1642 about the kernel. If __kernel_text_address()
1643 were SYMBOL_EXPORT'd, we could call that and be
1646 preempt_enable_no_resched();
1651 if (unlikely(m
== NULL
)) {
1653 _stp_warn ("Missing unwind data for module, rerun with 'stap -d %s'\n",
1655 // Don't _stp_warn about this, will use fallback unwinder.
1656 dbug_unwind(1, "No module found for pc=%lx", pc
);
1660 dbug_unwind(1, "trying debug_frame\n");
1661 res
= unwind_frame (context
, m
, s
, m
->debug_frame
,
1662 m
->debug_frame_len
, 0, user
, compat_task
);
1664 dbug_unwind(1, "debug_frame failed: %d, trying eh_frame\n", res
);
1665 res
= unwind_frame (context
, m
, s
, m
->eh_frame
,
1666 m
->eh_frame_len
, 1, user
, compat_task
);
1669 /* This situation occurs where some unwind data was found, but
1670 it was lacking somehow. */
1672 dbug_unwind (2, "unwinding failed: %d\n", res
);