]> sourceware.org Git - systemtap.git/blob - runtime/stack.c
2007-02-07 Martin Hunt <hunt@redhat.com>
[systemtap.git] / runtime / stack.c
1 /* -*- linux-c -*-
2 * Stack tracing functions
3 * Copyright (C) 2005, 2006, 2007 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 #ifndef _STACK_C_
13 #define _STACK_C_
14
15 /** @file stack.c
16 * @brief Stack Tracing Functions
17 */
18
19 /** @addtogroup stack Stack Tracing Functions
20 *
21 * @{
22 */
23
24 #include "sym.c"
25 #include "regs.h"
26 static int _stp_kta(unsigned long addr);
27
28 #define MAXBACKTRACE 20
29
30 #if defined (__x86_64__)
31 #include "stack-x86_64.c"
32 #elif defined (__ia64__)
33 #include "stack-ia64.c"
34 #elif defined (__i386__)
35 #include "stack-i386.c"
36 #elif defined (__powerpc64__)
37 #include "stack-ppc64.c"
38 #elif defined (__s390__) || defined (__s390x__)
39 #include "stack-s390.c"
40 #else
41 #error "Unsupported architecture"
42 #endif
43
44
45 /* our copy of kernel_text_address() */
46 static int _stp_kta(unsigned long addr)
47 {
48 static unsigned long stext, etext, sinittext, einittext;
49 static int init = 0;
50
51 if (init == 0) {
52 init = 1;
53 etext = _stp_kallsyms_lookup_name("_etext");
54 stext = _stp_kallsyms_lookup_name("_stext");
55 sinittext = _stp_kallsyms_lookup_name("_sinittext");
56 einittext = _stp_kallsyms_lookup_name("_einittext");
57 }
58
59 if (addr >= stext && addr <= etext)
60 return 1;
61
62 if (addr >= sinittext && addr <= einittext)
63 return 1;
64
65 return 0;
66 }
67
68 /** Prints the stack backtrace
69 * @param regs A pointer to the struct pt_regs.
70 */
71
72 void _stp_stack_print(struct pt_regs *regs, int verbose, struct kretprobe_instance *pi)
73 {
74 if (verbose) {
75 /* print the current address */
76 if (pi) {
77 _stp_print("Returning from: ");
78 _stp_symbol_print((unsigned long)_stp_probe_addr_r(pi));
79 _stp_print("\nReturning to : ");
80 _stp_symbol_print((unsigned long)_stp_ret_addr_r(pi));
81 } else {
82 _stp_print_char(' ');
83 _stp_symbol_print (REG_IP(regs));
84 }
85 _stp_print_char('\n');
86 } else
87 _stp_printf ("%p ", REG_IP(regs));
88 __stp_stack_print (regs, verbose, 0);
89 }
90
91 /** Writes stack backtrace to a string
92 *
93 * @param str string
94 * @param regs A pointer to the struct pt_regs.
95 * @returns void
96 */
97 void _stp_stack_snprint (char *str, int size, struct pt_regs *regs, int verbose, struct kretprobe_instance *pi)
98 {
99 /* To get a string, we use a simple trick. First flush the print buffer, */
100 /* then call _stp_stack_print, then copy the result into the output string */
101 /* and clear the print buffer. */
102 _stp_pbuf *pb = per_cpu_ptr(Stp_pbuf, smp_processor_id());
103 _stp_print_flush();
104 _stp_stack_print(regs, verbose, pi);
105 strlcpy(str, pb->buf, size < pb->len ? size : pb->len);
106 pb->len = 0;
107 }
108
109
110 /** Prints the user stack backtrace
111 * @param str string
112 * @returns Same string as was input with trace info appended,
113 * @note Currently limited to a depth of two. Works from jprobes and kprobes.
114 */
115 #if 0
116 void _stp_ustack_print (char *str)
117 {
118 struct pt_regs *nregs = ((struct pt_regs *) (THREAD_SIZE + (unsigned long) current->thread_info)) - 1;
119 _stp_printf ("%p : [user]\n", REG_IP(nregs));
120 if (REG_SP(nregs))
121 _stp_printf ("%p : [user]\n", *(unsigned long *)REG_SP(nregs));
122 }
123 #endif /* 0 */
124
125 /** @} */
126 #endif /* _STACK_C_ */
This page took 0.045807 seconds and 6 git commands to generate.