2 * Stack tracing functions
3 * Copyright (C) 2005-2009, 2014 Red Hat Inc.
4 * Copyright (C) 2005 Intel Corporation.
6 * This file is part of systemtap, and is free software. You can
7 * redistribute it and/or modify it under the terms of the GNU General
8 * Public License (GPL); either version 2, or (at your option) any
13 The translator will only include this file if the session needs any
14 of the backtrace functions. Currently indicated by having the session
15 need_unwind flag, which is set by tapset functions marked with
22 /* Maximum number of backtrace levels. */
24 #define MAXBACKTRACE 20
28 * @brief Stack Tracing Functions
31 /** @addtogroup stack Stack Tracing Functions
39 #include "linux/uprobes-inc.h"
41 #if defined(STAPCONF_KERNEL_STACKTRACE) || defined(STAPCONF_KERNEL_STACKTRACE_NO_BP)
42 #include <linux/stacktrace.h>
43 #include <asm/stacktrace.h>
46 static void _stp_stack_print_fallback(unsigned long, int, int, int);
48 #ifdef STP_USE_DWARF_UNWINDER
49 #ifdef STAPCONF_LINUX_UACCESS_H
50 #include <linux/uaccess.h>
52 #include <asm/uaccess.h>
54 #include <linux/types.h>
56 #define uintptr_t unsigned long
58 static int _stp_valid_pc_addr(unsigned long addr
, struct task_struct
*tsk
)
60 /* Just a simple check of whether the the address can be accessed
61 as a user space address. Zero is always bad. */
63 /* FIXME for s390x PR13350. */
64 #if defined (__s390__) || defined (__s390x__)
68 mm_segment_t oldfs
= get_fs();
70 ok
= access_ok(VERIFY_READ
, (long *) (intptr_t) addr
, sizeof(long));
72 return addr
!= 0L && tsk
!= NULL
? ok
: ! ok
;
77 #if defined (__ia64__)
78 #include "stack-ia64.c"
79 #elif defined (__arm__)
80 #include "stack-arm.c"
81 #elif defined (__s390__)
82 #include "stack-s390.c"
84 #ifndef STP_USE_DWARF_UNWINDER
85 #error "Unsupported architecture"
89 #if defined(STAPCONF_KERNEL_STACKTRACE) || defined(STAPCONF_KERNEL_STACKTRACE_NO_BP)
91 struct print_stack_data
98 #if defined(STAPCONF_STACKTRACE_OPS_WARNING)
99 static void print_stack_warning(void *data
, char *msg
)
104 print_stack_warning_symbol(void *data
, char *msg
, unsigned long symbol
)
109 static int print_stack_stack(void *data
, char *name
)
114 static void print_stack_address(void *data
, unsigned long addr
, int reliable
)
116 struct print_stack_data
*sdata
= data
;
119 else if (sdata
->levels
> 0) {
120 _stp_print_addr(addr
,
121 sdata
->flags
| (reliable
? 0 :_STP_SYM_INEXACT
),
127 static const struct stacktrace_ops print_stack_ops
= {
128 #if defined(STAPCONF_STACKTRACE_OPS_WARNING)
129 .warning
= print_stack_warning
,
130 .warning_symbol
= print_stack_warning_symbol
,
132 .stack
= print_stack_stack
,
133 .address
= print_stack_address
,
134 #if defined(STAPCONF_WALK_STACK)
135 .walk_stack
= print_context_stack
,
139 /* Used for kernel backtrace printing when other mechanisms fail. */
140 static void _stp_stack_print_fallback(unsigned long stack
,
141 int sym_flags
, int levels
, int skip
)
143 struct print_stack_data print_data
;
144 print_data
.flags
= sym_flags
;
145 print_data
.levels
= levels
;
146 print_data
.skip
= skip
;
147 #if defined(STAPCONF_KERNEL_STACKTRACE)
148 dump_trace(current
, NULL
, (long *)stack
, 0, &print_stack_ops
,
151 /* STAPCONF_KERNEL_STACKTRACE_NO_BP */
152 dump_trace(current
, NULL
, (long *)stack
, &print_stack_ops
,
157 static void _stp_stack_print_fallback(unsigned long s
, int v
, int l
, int k
) {
158 /* Don't guess, just give up. */
159 _stp_print_addr(0, v
| _STP_SYM_INEXACT
, NULL
);
162 #endif /* defined(STAPCONF_KERNEL_STACKTRACE) || defined(STAPCONF_KERNEL_STACKTRACE_NO_BP) */
165 /** Gets user space registers when available, also sets context
166 * full_uregs_p if appropriate. Should be used instead of accessing
167 * context uregs field directly when (full) uregs are needed from
170 static struct pt_regs
*_stp_get_uregs(struct context
*c
)
172 /* When the probe occurred in user context uregs are always complete. */
173 if (c
->uregs
&& c
->user_mode_p
)
175 else if (c
->uregs
== NULL
)
177 dbug_unwind(1, "computing uregs\n");
178 /* First try simple recovery through task_pt_regs,
179 on some platforms that already provides complete uregs. */
180 c
->uregs
= _stp_current_pt_regs();
181 if (c
->uregs
&& _stp_task_pt_regs_valid(current
, c
->uregs
))
184 /* Sadly powerpc does support the dwarf unwinder, but doesn't have enough
185 CFI in the kernel to recover fully to user space. */
186 #if defined(STP_USE_DWARF_UNWINDER) && !defined (__powerpc__)
187 else if (c
->uregs
!= NULL
&& c
->kregs
!= NULL
&& !c
->user_mode_p
)
189 struct unwind_frame_info
*info
= &c
->uwcontext_kernel
.info
;
193 /* We might be lucky and this probe already ran the kernel
194 unwind to end up in the user regs. */
195 if (UNW_PC(info
) == REG_IP(c
->uregs
))
198 dbug_unwind(1, "feeling lucky, info pc == uregs pc\n");
202 /* Try to recover the uregs by unwinding from the the kernel
204 levels
= MAXBACKTRACE
;
205 arch_unw_init_frame_info(info
, c
->kregs
, 0);
206 dbug_unwind(1, "Trying to recover... searching for 0x%llx\n",
207 (unsigned long long) REG_IP(c
->uregs
));
209 /* Mark the kernel unwind cache as invalid
210 (uwcache_kernel.depth is no longer consistent with
211 the actual current depth of the unwind).
213 We don't save PCs in the cache at this point because
214 this kernel unwind procedure does not fetch the top
215 level PC, so uwcache_kernel.pc[0] would be left
216 unpopulated. We would have to either fetch the
217 current PC here, or specially represent this state of
218 the cache, something we don't bother with at this
221 XXX: this can create (tolerable amounts of) inefficiency
222 if the probe intersperses user and kernel unwind calls,
223 since the other unwind code can clear uregs, triggering
224 a redundant unwind the next time we need them. */
225 dbug_unwind(1, "clearing kernel unwind cache\n");
226 c
->uwcache_kernel
.state
= uwcache_uninitialized
;
229 while (levels
> 0 && ret
== 0 && UNW_PC(info
) != REG_IP(c
->uregs
))
232 ret
= unwind(&c
->uwcontext_kernel
, 0);
233 dbug_unwind(1, "unwind levels: %d, ret: %d, pc=0x%llx\n",
234 levels
, ret
, (unsigned long long) UNW_PC(info
));
237 /* Have we arrived where we think user space currently is? */
238 if (ret
== 0 && UNW_PC(info
) == REG_IP(c
->uregs
))
240 /* Note we need to clear this state again when the unwinder
241 has been rerun. See __stp_stack_print invocation below. */
242 UNW_SP(info
) = REG_SP(c
->uregs
); /* Fix up user stack */
243 c
->uregs
= &info
->regs
;
245 dbug_unwind(1, "recovered with pc=0x%llx sp=0x%llx\n",
246 (unsigned long long) UNW_PC(info
),
247 (unsigned long long) UNW_SP(info
));
250 dbug_unwind(1, "failed to recover user reg state\n");
258 static unsigned long _stp_stack_unwind_one_kernel(struct context
*c
, unsigned depth
)
260 struct pt_regs
*regs
= NULL
;
261 struct unwind_frame_info
*info
= NULL
;
264 if (depth
== 0) { /* Start by fetching the current PC. */
265 dbug_unwind(1, "STARTING kernel unwind\n");
268 /* Even the current PC is unknown; so we have
269 * absolutely no data at any depth.
271 * Note that unlike _stp_stack_kernel_print(),
272 * we can't fall back to calling dump_trace()
273 * to obtain the backtrace -- since that
274 * returns a string, which we would have to
275 * tokenize. Callers that want to use the
276 * dump_trace() fallback should call
277 * _stp_stack_kernel_print() and do their own
278 * tokenization of the result. */
279 #if defined (__i386__) || defined (__x86_64__)
280 arch_unw_init_frame_info(&c
->uwcontext_kernel
.info
, NULL
, 0);
281 return UNW_PC(&c
->uwcontext_kernel
.info
);
285 } else if (c
->probe_type
== stp_probe_type_kretprobe
287 return (unsigned long)_stp_ret_addr_r(c
->ips
.krp
.pi
);
289 return REG_IP(c
->kregs
);
293 #ifdef STP_USE_DWARF_UNWINDER
294 /* Otherwise, use the DWARF unwinder to unwind one step. */
298 info
= &c
->uwcontext_kernel
.info
;
300 dbug_unwind(1, "CONTINUING kernel unwind to depth %d\n", depth
);
303 /* First step of actual DWARF unwind;
304 need to clear uregs& set up uwcontext->info. */
305 if (c
->uregs
== &c
->uwcontext_kernel
.info
.regs
) {
306 dbug_unwind(1, "clearing uregs\n");
307 /* Unwinder needs the reg state, clear uregs ref. */
312 arch_unw_init_frame_info(info
, regs
, 0);
315 ret
= unwind(&c
->uwcontext_kernel
, 0);
316 dbug_unwind(1, "ret=%d PC=%llx SP=%llx\n", ret
,
317 (unsigned long long) UNW_PC(info
),
318 (unsigned long long) UNW_SP(info
));
320 /* check if unwind hit an error */
321 if (ret
|| ! _stp_valid_pc_addr(UNW_PC(info
), NULL
)) {
331 static unsigned long _stp_stack_kernel_get(struct context
*c
, unsigned depth
)
333 unsigned long pc
= 0;
335 if (c
->uwcache_kernel
.state
== uwcache_uninitialized
) {
336 c
->uwcache_kernel
.depth
= 0;
337 c
->uwcache_kernel
.state
= uwcache_partial
;
340 if (unlikely(depth
>= MAXBACKTRACE
))
343 /* Obtain cached value if available. */
344 if (depth
< c
->uwcache_kernel
.depth
)
345 return c
->uwcache_kernel
.pc
[depth
];
346 else if (c
->uwcache_kernel
.state
== uwcache_finished
)
347 return 0; /* unwind does not reach this far */
349 /* Advance uwcontext to the required depth. */
350 while (c
->uwcache_kernel
.depth
<= depth
) {
351 pc
= c
->uwcache_kernel
.pc
[c
->uwcache_kernel
.depth
]
352 = _stp_stack_unwind_one_kernel(c
, c
->uwcache_kernel
.depth
);
353 c
->uwcache_kernel
.depth
++;
354 if (pc
== 0 || pc
== _stp_kretprobe_trampoline
) {
355 /* Mark unwind completed. */
356 c
->uwcache_kernel
.state
= uwcache_finished
;
358 /* XXX: is there a way to unwind across kretprobe trampolines? PR9999 */
362 /* Return the program counter at the current depth. */
366 /** Prints the stack backtrace
367 * @param regs A pointer to the struct pt_regs.
368 * @param verbose _STP_SYM_FULL or _STP_SYM_BRIEF
371 static void _stp_stack_kernel_print(struct context
*c
, int sym_flags
)
373 unsigned n
, remaining
;
376 /* print the current address */
377 if (c
->probe_type
== stp_probe_type_kretprobe
&& c
->ips
.krp
.pi
378 && (sym_flags
& _STP_SYM_FULL
) == _STP_SYM_FULL
) {
379 _stp_print("Returning from: ");
380 _stp_print_addr((unsigned long)_stp_probe_addr_r(c
->ips
.krp
.pi
),
382 _stp_print("Returning to : ");
384 _stp_print_addr(_stp_stack_kernel_get(c
, 0), sym_flags
, NULL
);
386 #ifdef STP_USE_DWARF_UNWINDER
387 for (n
= 1; n
< MAXBACKTRACE
; n
++) {
388 l
= _stp_stack_kernel_get(c
, n
);
390 remaining
= MAXBACKTRACE
- n
;
391 _stp_stack_print_fallback(UNW_SP(&c
->uwcontext_kernel
.info
),
392 sym_flags
, remaining
, 0);
395 _stp_print_addr(l
, sym_flags
, NULL
);
400 /* This is a fatal block for _stp_stack_kernel_get,
401 * but when printing a backtrace we can use this
404 * When compiled with frame pointers we can do
405 * a pretty good guess at the stack value,
406 * otherwise let dump_stack guess it
407 * (and skip some framework frames). */
408 #if defined(STAPCONF_KERNEL_STACKTRACE) || defined(STAPCONF_KERNEL_STACKTRACE_NO_BP)
411 #ifdef CONFIG_FRAME_POINTER
412 sp
= *(unsigned long *) __builtin_frame_address (0);
413 skip
= 1; /* Skip just this frame. */
416 skip
= 5; /* yes, that many framework frames. */
418 _stp_stack_print_fallback(sp
, sym_flags
,
421 if (sym_flags
& _STP_SYM_SYMBOL
)
422 _stp_printf("<no kernel backtrace at %s>\n",
430 /* Arch specific fallback for kernel backtraces. */
431 __stp_stack_print(c
->kregs
, sym_flags
, MAXBACKTRACE
);
435 static unsigned long _stp_stack_unwind_one_user(struct context
*c
, unsigned depth
)
437 struct pt_regs
*regs
= NULL
;
439 struct uretprobe_instance
*ri
= NULL
;
440 struct unwind_frame_info
*info
= NULL
;
442 #ifdef STAPCONF_UPROBE_GET_PC
443 unsigned long maybe_pc
;
446 if (c
->probe_type
== stp_probe_type_uretprobe
)
448 #ifdef STAPCONF_UPROBE_GET_PC
449 else if (c
->probe_type
== stp_probe_type_uprobe
)
450 ri
= GET_PC_URETPROBE_NONE
;
453 /* XXX: The computation that gives this is cached, so calling
454 * _stp_get_uregs multiple times is okay... probably. */
455 regs
= _stp_get_uregs(c
);
456 uregs_valid
= c
->full_uregs_p
;
458 if (! current
->mm
|| ! regs
)
459 return 0; // no user backtrace at this probe point
461 if (depth
== 0) { /* Start by fetching the current PC. */
462 dbug_unwind(1, "STARTING user unwind\n");
464 #ifdef STAPCONF_UPROBE_GET_PC
465 if (c
->probe_type
== stp_probe_type_uretprobe
&& ri
) {
475 #ifdef STP_USE_DWARF_UNWINDER
476 info
= &c
->uwcontext_user
.info
;
478 dbug_unwind(1, "CONTINUING user unwind to depth %d\n", depth
);
480 if (depth
== 1) { /* need to clear uregs & set up uwcontext->info */
481 if (c
->uregs
== &c
->uwcontext_user
.info
.regs
) {
482 dbug_unwind(1, "clearing uregs\n");
483 /* Unwinder needs the reg state, clear uregs ref. */
488 arch_unw_init_frame_info(info
, regs
, 0);
491 ret
= unwind(&c
->uwcontext_user
, 1);
492 #ifdef STAPCONF_UPROBE_GET_PC
495 maybe_pc
= uprobe_get_pc(ri
, UNW_PC(info
), UNW_SP(info
));
497 printk("SYSTEMTAP ERROR: uprobe_get_return returned 0\n");
499 UNW_PC(info
) = maybe_pc
;
502 dbug_unwind(1, "ret=%d PC=%llx SP=%llx\n", ret
,
503 (unsigned long long) UNW_PC(info
), (unsigned long long) UNW_SP(info
));
505 /* check if unwind hit an error */
506 if (ret
|| ! _stp_valid_pc_addr(UNW_PC(info
), current
)) {
512 /* User stack traces only supported for arches with dwarf unwinder. */
517 static unsigned long _stp_stack_user_get(struct context
*c
, unsigned depth
)
519 unsigned long pc
= 0;
521 if (c
->uwcache_user
.state
== uwcache_uninitialized
) {
522 c
->uwcache_user
.depth
= 0;
523 c
->uwcache_user
.state
= uwcache_partial
;
526 if (unlikely(depth
>= MAXBACKTRACE
))
529 /* Obtain cached value if available. */
530 if (depth
< c
->uwcache_user
.depth
)
531 return c
->uwcache_user
.pc
[depth
];
532 else if (c
->uwcache_user
.state
== uwcache_finished
)
533 return 0; /* unwind does not reach this far */
535 /* Advance uwcontext to the required depth. */
536 while (c
->uwcache_user
.depth
<= depth
) {
537 pc
= c
->uwcache_user
.pc
[c
->uwcache_user
.depth
]
538 = _stp_stack_unwind_one_user(c
, c
->uwcache_user
.depth
);
539 c
->uwcache_user
.depth
++;
541 /* Mark unwind completed. */
542 c
->uwcache_user
.state
= uwcache_finished
;
547 /* Return the program counter at the current depth. */
551 static void _stp_stack_user_print(struct context
*c
, int sym_flags
)
553 struct pt_regs
*regs
= NULL
;
554 struct uretprobe_instance
*ri
= NULL
;
555 unsigned n
; unsigned long l
;
557 if (c
->probe_type
== stp_probe_type_uretprobe
)
559 #ifdef STAPCONF_UPROBE_GET_PC
560 else if (c
->probe_type
== stp_probe_type_uprobe
)
561 ri
= GET_PC_URETPROBE_NONE
;
564 regs
= _stp_get_uregs(c
);
566 if (! current
->mm
|| ! regs
) {
567 if (sym_flags
& _STP_SYM_SYMBOL
)
568 _stp_printf("<no user backtrace at %s>\n",
575 /* print the current address */
576 #ifdef STAPCONF_UPROBE_GET_PC
577 if (c
->probe_type
== stp_probe_type_uretprobe
&& ri
) {
578 if ((sym_flags
& _STP_SYM_FULL
) == _STP_SYM_FULL
) {
579 _stp_print("Returning from: ");
580 /* ... otherwise this dereference fails */
581 _stp_print_addr(ri
->rp
->u
.vaddr
, sym_flags
, current
);
582 _stp_print("Returning to : ");
586 _stp_print_addr(_stp_stack_user_get(c
, 0), sym_flags
, current
);
588 /* print rest of stack... */
589 #ifdef STP_USE_DWARF_UNWINDER
590 for (n
= 1; n
< MAXBACKTRACE
; n
++) {
591 l
= _stp_stack_user_get(c
, n
);
592 if (l
== 0) break; // No user space fallback available
593 _stp_print_addr(l
, sym_flags
, current
);
596 /* User stack traces only supported for arches with dwarf unwinder. */
597 if (sym_flags
& _STP_SYM_SYMBOL
)
598 _stp_printf("<no user backtrace support on arch>\n");
604 /** Writes stack backtrace to a string
607 * @param regs A pointer to the struct pt_regs.
610 static void _stp_stack_kernel_sprint(char *str
, int size
, struct context
* c
,
613 /* To get an hex string, we use a simple trick.
614 * First flush the print buffer,
615 * then call _stp_stack_print,
616 * then copy the result into the output string
617 * and clear the print buffer. */
618 _stp_pbuf
*pb
= per_cpu_ptr(Stp_pbuf
, smp_processor_id());
621 _stp_stack_kernel_print(c
, sym_flags
);
623 strlcpy(str
, pb
->buf
, size
< (int)pb
->len
? size
: (int)pb
->len
);
627 static void _stp_stack_user_sprint(char *str
, int size
, struct context
* c
,
630 /* To get an hex string, we use a simple trick.
631 * First flush the print buffer,
632 * then call _stp_stack_print,
633 * then copy the result into the output string
634 * and clear the print buffer. */
635 _stp_pbuf
*pb
= per_cpu_ptr(Stp_pbuf
, smp_processor_id());
638 _stp_stack_user_print(c
, sym_flags
);
640 strlcpy(str
, pb
->buf
, size
< (int)pb
->len
? size
: (int)pb
->len
);
644 #endif /* _STACK_C_ */