]> sourceware.org Git - systemtap.git/blob - runtime/stack.c
a7d03db722d5c263b6d575f17104cc3ef326d953
[systemtap.git] / runtime / stack.c
1 /* -*- linux-c -*-
2 * Stack tracing functions
3 * Copyright (C) 2005-2009, 2014 Red Hat Inc.
4 * Copyright (C) 2005 Intel Corporation.
5 *
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
9 * later version.
10 */
11
12 /*
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
16 pragme:unwind.
17 */
18
19 #ifndef _STACK_C_
20 #define _STACK_C_
21
22 /* Maximum number of backtrace levels. */
23 #ifndef MAXBACKTRACE
24 #define MAXBACKTRACE 20
25 #endif
26
27 /** @file stack.c
28 * @brief Stack Tracing Functions
29 */
30
31 /** @addtogroup stack Stack Tracing Functions
32 *
33 * @{
34 */
35
36 #include "sym.c"
37 #include "regs.h"
38
39 #include "linux/uprobes-inc.h"
40
41 #if defined(STAPCONF_KERNEL_STACKTRACE) || defined(STAPCONF_KERNEL_STACKTRACE_NO_BP)
42 #include <linux/stacktrace.h>
43 #include <asm/stacktrace.h>
44 #endif
45
46 static void _stp_stack_print_fallback(unsigned long, int, int, int);
47
48 #ifdef STP_USE_DWARF_UNWINDER
49 #ifdef STAPCONF_LINUX_UACCESS_H
50 #include <linux/uaccess.h>
51 #else
52 #include <asm/uaccess.h>
53 #endif
54 #include <linux/types.h>
55 #define intptr_t long
56 #define uintptr_t unsigned long
57
58 static int _stp_valid_pc_addr(unsigned long addr, struct task_struct *tsk)
59 {
60 /* Just a simple check of whether the the address can be accessed
61 as a user space address. Zero is always bad. */
62
63 /* FIXME for s390x PR13350. */
64 #if defined (__s390__) || defined (__s390x__)
65 return addr != 0L;
66 #else
67 int ok;
68 mm_segment_t oldfs = get_fs();
69 set_fs(USER_DS);
70 ok = access_ok(VERIFY_READ, (long *) (intptr_t) addr, sizeof(long));
71 set_fs(oldfs);
72 return addr != 0L && tsk != NULL ? ok : ! ok;
73 #endif
74 }
75 #endif
76
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"
83 #else
84 #ifndef STP_USE_DWARF_UNWINDER
85 #error "Unsupported architecture"
86 #endif
87 #endif
88
89 #if defined(STAPCONF_KERNEL_STACKTRACE) || defined(STAPCONF_KERNEL_STACKTRACE_NO_BP)
90
91 struct print_stack_data
92 {
93 int flags;
94 int levels;
95 int skip;
96 };
97
98 #if defined(STAPCONF_STACKTRACE_OPS_WARNING)
99 static void print_stack_warning(void *data, char *msg)
100 {
101 }
102
103 static void
104 print_stack_warning_symbol(void *data, char *msg, unsigned long symbol)
105 {
106 }
107 #endif
108
109 static int print_stack_stack(void *data, char *name)
110 {
111 return -1;
112 }
113
114 static void print_stack_address(void *data, unsigned long addr, int reliable)
115 {
116 struct print_stack_data *sdata = data;
117 if (sdata->skip > 0)
118 sdata->skip--;
119 else if (sdata->levels > 0) {
120 _stp_print_addr(addr,
121 sdata->flags | (reliable ? 0 :_STP_SYM_INEXACT),
122 NULL);
123 sdata->levels--;
124 }
125 }
126
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,
131 #endif
132 .stack = print_stack_stack,
133 .address = print_stack_address,
134 #if defined(STAPCONF_WALK_STACK)
135 .walk_stack = print_context_stack,
136 #endif
137 };
138
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)
142 {
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,
149 &print_data);
150 #else
151 /* STAPCONF_KERNEL_STACKTRACE_NO_BP */
152 dump_trace(current, NULL, (long *)stack, &print_stack_ops,
153 &print_data);
154 #endif
155 }
156 #else
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);
160 }
161
162 #endif /* defined(STAPCONF_KERNEL_STACKTRACE) || defined(STAPCONF_KERNEL_STACKTRACE_NO_BP) */
163
164
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
168 * kernel context.
169 */
170 static struct pt_regs *_stp_get_uregs(struct context *c)
171 {
172 /* When the probe occurred in user context uregs are always complete. */
173 if (c->uregs && c->user_mode_p)
174 c->full_uregs_p = 1;
175 else if (c->uregs == NULL)
176 {
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))
182 c->full_uregs_p = 1;
183
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)
188 {
189 struct unwind_frame_info *info = &c->uwcontext_kernel.info;
190 int ret = 0;
191 int levels;
192
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))
196 {
197 levels = 0;
198 dbug_unwind(1, "feeling lucky, info pc == uregs pc\n");
199 }
200 else
201 {
202 /* Try to recover the uregs by unwinding from the the kernel
203 probe location. */
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));
208
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).
212
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
219 stage.
220
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;
227 }
228
229 while (levels > 0 && ret == 0 && UNW_PC(info) != REG_IP(c->uregs))
230 {
231 levels--;
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));
235 }
236
237 /* Have we arrived where we think user space currently is? */
238 if (ret == 0 && UNW_PC(info) == REG_IP(c->uregs))
239 {
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;
244 c->full_uregs_p = 1;
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));
248 }
249 else
250 dbug_unwind(1, "failed to recover user reg state\n");
251 }
252 #endif
253 }
254 return c->uregs;
255 }
256
257
258 static unsigned long _stp_stack_unwind_one_kernel(struct context *c, unsigned depth)
259 {
260 struct pt_regs *regs = NULL;
261 struct unwind_frame_info *info = NULL;
262 int ret;
263
264 if (depth == 0) { /* Start by fetching the current PC. */
265 dbug_unwind(1, "STARTING kernel unwind\n");
266
267 if (! c->kregs) {
268 /* Even the current PC is unknown; so we have
269 * absolutely no data at any depth.
270 *
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);
282 #else
283 return 0;
284 #endif
285 } else if (c->probe_type == stp_probe_type_kretprobe
286 && c->ips.krp.pi) {
287 return (unsigned long)_stp_ret_addr_r(c->ips.krp.pi);
288 } else {
289 return REG_IP(c->kregs);
290 }
291 }
292
293 #ifdef STP_USE_DWARF_UNWINDER
294 /* Otherwise, use the DWARF unwinder to unwind one step. */
295
296 regs = c->kregs;
297
298 info = &c->uwcontext_kernel.info;
299
300 dbug_unwind(1, "CONTINUING kernel unwind to depth %d\n", depth);
301
302 if (depth == 1) {
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. */
308 c->uregs = NULL;
309 c->full_uregs_p = 0;
310 }
311
312 arch_unw_init_frame_info(info, regs, 0);
313 }
314
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));
319
320 /* check if unwind hit an error */
321 if (ret || ! _stp_valid_pc_addr(UNW_PC(info), NULL)) {
322 return 0;
323 }
324
325 return UNW_PC(info);
326 #else
327 return 0;
328 #endif
329 }
330
331 static unsigned long _stp_stack_kernel_get(struct context *c, unsigned depth)
332 {
333 unsigned long pc = 0;
334
335 if (c->uwcache_kernel.state == uwcache_uninitialized) {
336 c->uwcache_kernel.depth = 0;
337 c->uwcache_kernel.state = uwcache_partial;
338 }
339
340 if (unlikely(depth >= MAXBACKTRACE))
341 return 0;
342
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 */
348
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;
357 break;
358 /* XXX: is there a way to unwind across kretprobe trampolines? PR9999 */
359 }
360 }
361
362 /* Return the program counter at the current depth. */
363 return pc;
364 }
365
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
369 */
370
371 static void _stp_stack_kernel_print(struct context *c, int sym_flags)
372 {
373 unsigned n, remaining;
374 unsigned long l;
375
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),
381 sym_flags, NULL);
382 _stp_print("Returning to : ");
383 }
384 _stp_print_addr(_stp_stack_kernel_get(c, 0), sym_flags, NULL);
385
386 #ifdef STP_USE_DWARF_UNWINDER
387 for (n = 1; n < MAXBACKTRACE; n++) {
388 l = _stp_stack_kernel_get(c, n);
389 if (l == 0) {
390 remaining = MAXBACKTRACE - n;
391 _stp_stack_print_fallback(UNW_SP(&c->uwcontext_kernel.info),
392 sym_flags, remaining, 0);
393 break;
394 } else {
395 _stp_print_addr(l, sym_flags, NULL);
396 }
397 }
398 #else
399 if (! c->kregs) {
400 /* This is a fatal block for _stp_stack_kernel_get,
401 * but when printing a backtrace we can use this
402 * inexact fallback.
403 *
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)
409 unsigned long sp;
410 int skip;
411 #ifdef CONFIG_FRAME_POINTER
412 sp = *(unsigned long *) __builtin_frame_address (0);
413 skip = 1; /* Skip just this frame. */
414 #else
415 sp = 0;
416 skip = 5; /* yes, that many framework frames. */
417 #endif
418 _stp_stack_print_fallback(sp, sym_flags,
419 MAXBACKTRACE, skip);
420 #else
421 if (sym_flags & _STP_SYM_SYMBOL)
422 _stp_printf("<no kernel backtrace at %s>\n",
423 c->probe_point);
424 else
425 _stp_print("\n");
426 #endif
427 return;
428 }
429 else
430 /* Arch specific fallback for kernel backtraces. */
431 __stp_stack_print(c->kregs, sym_flags, MAXBACKTRACE);
432 #endif
433 }
434
435 static unsigned long _stp_stack_unwind_one_user(struct context *c, unsigned depth)
436 {
437 struct pt_regs *regs = NULL;
438 int uregs_valid = 0;
439 struct uretprobe_instance *ri = NULL;
440 struct unwind_frame_info *info = NULL;
441 int ret;
442 #ifdef STAPCONF_UPROBE_GET_PC
443 unsigned long maybe_pc;
444 #endif
445
446 if (c->probe_type == stp_probe_type_uretprobe)
447 ri = c->ips.ri;
448 #ifdef STAPCONF_UPROBE_GET_PC
449 else if (c->probe_type == stp_probe_type_uprobe)
450 ri = GET_PC_URETPROBE_NONE;
451 #endif
452
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;
457
458 if (! current->mm || ! regs)
459 return 0; // no user backtrace at this probe point
460
461 if (depth == 0) { /* Start by fetching the current PC. */
462 dbug_unwind(1, "STARTING user unwind\n");
463
464 #ifdef STAPCONF_UPROBE_GET_PC
465 if (c->probe_type == stp_probe_type_uretprobe && ri) {
466 return ri->ret_addr;
467 } else {
468 return REG_IP(regs);
469 }
470 #else
471 return REG_IP(regs);
472 #endif
473 }
474
475 #ifdef STP_USE_DWARF_UNWINDER
476 info = &c->uwcontext_user.info;
477
478 dbug_unwind(1, "CONTINUING user unwind to depth %d\n", depth);
479
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. */
484 c->uregs = NULL;
485 c->full_uregs_p = 0;
486 }
487
488 arch_unw_init_frame_info(info, regs, 0);
489 }
490
491 ret = unwind(&c->uwcontext_user, 1);
492 #ifdef STAPCONF_UPROBE_GET_PC
493 maybe_pc = 0;
494 if (ri) {
495 maybe_pc = uprobe_get_pc(ri, UNW_PC(info), UNW_SP(info));
496 if (!maybe_pc)
497 printk("SYSTEMTAP ERROR: uprobe_get_return returned 0\n");
498 else
499 UNW_PC(info) = maybe_pc;
500 }
501 #endif
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));
504
505 /* check if unwind hit an error */
506 if (ret || ! _stp_valid_pc_addr(UNW_PC(info), current)) {
507 return 0;
508 }
509
510 return UNW_PC(info);
511 #else
512 /* User stack traces only supported for arches with dwarf unwinder. */
513 return 0;
514 #endif
515 }
516
517 static unsigned long _stp_stack_user_get(struct context *c, unsigned depth)
518 {
519 unsigned long pc = 0;
520
521 if (c->uwcache_user.state == uwcache_uninitialized) {
522 c->uwcache_user.depth = 0;
523 c->uwcache_user.state = uwcache_partial;
524 }
525
526 if (unlikely(depth >= MAXBACKTRACE))
527 return 0;
528
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 */
534
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 ++;
540 if (pc == 0) {
541 /* Mark unwind completed. */
542 c->uwcache_user.state = uwcache_finished;
543 break;
544 }
545 }
546
547 /* Return the program counter at the current depth. */
548 return pc;
549 }
550
551 static void _stp_stack_user_print(struct context *c, int sym_flags)
552 {
553 struct pt_regs *regs = NULL;
554 struct uretprobe_instance *ri = NULL;
555 unsigned n; unsigned long l;
556
557 if (c->probe_type == stp_probe_type_uretprobe)
558 ri = c->ips.ri;
559 #ifdef STAPCONF_UPROBE_GET_PC
560 else if (c->probe_type == stp_probe_type_uprobe)
561 ri = GET_PC_URETPROBE_NONE;
562 #endif
563
564 regs = _stp_get_uregs(c);
565
566 if (! current->mm || ! regs) {
567 if (sym_flags & _STP_SYM_SYMBOL)
568 _stp_printf("<no user backtrace at %s>\n",
569 c->probe_point);
570 else
571 _stp_print("\n");
572 return;
573 }
574
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 : ");
583 }
584 }
585 #endif
586 _stp_print_addr(_stp_stack_user_get(c, 0), sym_flags, current);
587
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);
594 }
595 #else
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");
599 else
600 _stp_print("\n");
601 #endif
602 }
603
604 /** Writes stack backtrace to a string
605 *
606 * @param str string
607 * @param regs A pointer to the struct pt_regs.
608 * @returns void
609 */
610 static void _stp_stack_kernel_sprint(char *str, int size, struct context* c,
611 int sym_flags)
612 {
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());
619 _stp_print_flush();
620
621 _stp_stack_kernel_print(c, sym_flags);
622
623 strlcpy(str, pb->buf, size < (int)pb->len ? size : (int)pb->len);
624 pb->len = 0;
625 }
626
627 static void _stp_stack_user_sprint(char *str, int size, struct context* c,
628 int sym_flags)
629 {
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());
636 _stp_print_flush();
637
638 _stp_stack_user_print(c, sym_flags);
639
640 strlcpy(str, pb->buf, size < (int)pb->len ? size : (int)pb->len);
641 pb->len = 0;
642 }
643
644 #endif /* _STACK_C_ */
This page took 0.064518 seconds and 4 git commands to generate.