Debian Codegraph @

tapset/linux/context-symbols.stp

raw
1 // context-symbols tapset
2 // Copyright (C) 2005-2016 Red Hat Inc.
3 // Copyright (C) 2006 Intel Corporation.
4 //
5 // This file is part of systemtap, and is free software. You can
6 // redistribute it and/or modify it under the terms of the GNU General
7 // Public License (GPL); either version 2, or (at your option) any
8 // later version.
9 // <tapsetdescription>
10 // Context functions provide additional information about where an event occurred. These functions can
11 //provide information such as a backtrace to where the event occurred and the current register values for the
12 //processor.
13 // </tapsetdescription>
14
15 @__private30 function __stack_raw:long (n:long) %{ /* pragma:unwind */ /* pure */
16 /* basic sanity check for bounds: */
17 if (unlikely(STAP_ARG_n < 0 || STAP_ARG_n >= MAXBACKTRACE))
18 STAP_RETVALUE = 0;
19 else
20 STAP_RETVALUE = _stp_stack_kernel_get (CONTEXT, (unsigned)STAP_ARG_n);
21 %}
22
23 /**
24 * sfunction stack - Return address at given depth of kernel stack backtrace
25 * @n: number of levels to descend in the stack.
26 *
27 * Description: Performs a simple (kernel) backtrace, and returns the
28 * element at the specified position. The results of the backtrace itself
29 * are cached, so that the backtrace computation is performed at most once
30 * no matter how many times stack() is called, or in what order.
31 */
32 function stack:long (n:long) {
33 __r = __stack_raw(n);
34 if (__r != 0) return __r
35
36 /* fallback: parse backtrace() to go deeper in the stack */
37 __b = backtrace (); __orig_n = n;
38 __sym = tokenize (__b, " ");
39 if (__sym == "") @__context_unwind_error(__orig_n);
40 while (n > 0) {
41 __sym = tokenize ("", " ");
42 if (__sym == "") @__context_unwind_error(__orig_n);
43 n--;
44 }
45 return strtol(__sym, 16)
46 }
47
48 /**
49 * sfunction print_stack - Print out kernel stack from string
50 * @stk: String with list of hexadecimal addresses
51 *
52 * Description: This function performs a symbolic lookup of the addresses
53 * in the given string,
54 * which is assumed to be the result of a prior call to
55 * backtrace().
56 *
57 * Print one line per address, including the address, the
58 * name of the function containing the address, and an estimate of
59 * its position within that function. Return nothing.
60 *
61 * NOTE: it is recommended to use print_syms() instead of this function.
62 */
63 function print_stack(stk:string) { print_syms(stk) }
64
65 /**
66 * sfunction sprint_stack - Return stack for kernel addresses from string
67 * @stk: String with list of hexadecimal (kernel) addresses
68 *
69 * Perform a symbolic lookup of the addresses in the given string,
70 * which is assumed to be the result of a prior call to backtrace().
71 *
72 * Returns a simple backtrace from the given hex string. One line per
73 * address. Includes the symbol name (or hex address if symbol
74 * couldn't be resolved) and module name (if found). Includes the
75 * offset from the start of the function if found, otherwise the
76 * offset will be added to the module (if found, between
77 * brackets). Returns the backtrace as string (each line terminated by
78 * a newline character). Note that the returned stack will be
79 * truncated to MAXSTRINGLEN, to print fuller and richer stacks use
80 * print_stack.
81 *
82 * NOTE: it is recommended to use sprint_syms() instead of this function.
83 */
84 function sprint_stack:string(stk:string) { return sprint_syms(stk) }
85
86 /**
87 * sfunction probefunc - Return the probe point's function name, if known
88 *
89 * Description: This function returns the name of the function being probed
90 * based on the current address, as computed by symname(addr()) or
91 * usymname(uaddr()) depending on probe context (whether the probe is
92 * a user probe or a kernel probe).
93 *
94 * Please note: this function's behaviour differs between SystemTap 2.0
95 * and earlier versions. Prior to 2.0, probefunc() obtained the function
96 * name from the probe point string as returned by pp(), and used the
97 * current address as a fallback.
98 *
99 * Consider using ppfunc() instead.
100 */
101 function probefunc:string () %( systemtap_v < "2.0" %?
102 %{ /* pure */ /* stable */ /* pragma:symbols */
103 char *ptr, *start;
104
105 STAP_RETVALUE[0] = '\0';
106 start = strstr(CONTEXT->probe_point, "function(\"");
107 ptr = start + 10;
108 if (!start) {
109 start = strstr(CONTEXT->probe_point, "inline(\"");
110 ptr = start + 8;
111 }
112
113 if (start) {
114 int len = MAXSTRINGLEN;
115 char *dst = STAP_RETVALUE;
116 while (*ptr != '@' && *ptr != '"' && --len > 0 && *ptr)
117 *dst++ = *ptr++;
118 *dst = 0;
119
120 } else {
121 struct pt_regs *regs;
122 int user_mode;
123 user_mode = c->user_mode_p;
124 regs = user_mode ? CONTEXT->uregs : CONTEXT->kregs;
125 if (regs) {
126 _stp_snprint_addr(STAP_RETVALUE, MAXSTRINGLEN,
127 REG_IP(regs), _STP_SYM_SYMBOL,
128 (user_mode ? current : NULL),
129 CONTEXT);
130 }
131 }
132 %}
133 %:
134 {
135 %( systemtap_privilege != "stapusr" %?
136 return user_mode() ? usymname(uaddr()) : symname(addr())
137 %:
138 return user_mode() ? usymname(uaddr())
139 : error("kernel probefunc() query from unprivileged script") /* XXX should be impossible */
140 %)
141 }
142 %)
143
144 /**
145 * sfunction probemod - Return the probe point's kernel module name
146 *
147 * Description: This function returns the name of the kernel module
148 * containing the probe point, if known.
149 */
150 function probemod:string () %{ /* pure */ /* stable */
151 char *ptr, *start;
152
153 start = strstr(CONTEXT->probe_point, "module(\"");
154 ptr = start + 8;
155
156 if (start) {
157 int len = MAXSTRINGLEN;
158 char *dst = STAP_RETVALUE;
159 while (*ptr != '"' && --len && *ptr)
160 *dst++ = *ptr++;
161 *dst = 0;
162 } else if (CONTEXT->kregs && ! CONTEXT->user_mode_p) {
163 struct _stp_module *m;
164 m = _stp_kmod_sec_lookup (REG_IP(CONTEXT->kregs), NULL);
165 if (m && m->name)
166 strlcpy (STAP_RETVALUE, m->name, MAXSTRINGLEN);
167 else
168 #if STAP_COMPAT_VERSION <= STAP_VERSION(2,2)
169 strlcpy (STAP_RETVALUE, "<unknown>", MAXSTRINGLEN);
170 #else
171 CONTEXT->last_error = "Cannot determine kernel module name";
172 #endif
173 } else
174 #if STAP_COMPAT_VERSION <= STAP_VERSION(2,2)
175 strlcpy (STAP_RETVALUE, "<unknown>", MAXSTRINGLEN);
176 #else
177 CONTEXT->last_error = "Cannot determine kernel module name";
178 #endif
179 %}
180
181 /**
182 * sfunction modname - Return the kernel module name loaded at the address
183 * @addr: The address to map to a kernel module name
184 *
185 * Description: Returns the module name associated with the given
186 * address if known. If not known it will raise an error. If the
187 * address was not in a kernel module, but in the kernel itself, then
188 * the string "kernel" will be returned.
189 */
190 function modname:string (addr: long) %{ /* pure */
191 struct _stp_module *m;
192 #ifdef STAPCONF_MODULE_TEXT_ADDRESS
193 struct module *ko;
194 #endif
195 m = _stp_kmod_sec_lookup (STAP_ARG_addr, NULL);
196 if (m && m->name)
197 {
198 strlcpy (STAP_RETVALUE, m->name, MAXSTRINGLEN);
199 return;
200 }
201
202 #ifdef STAPCONF_MODULE_TEXT_ADDRESS
203 preempt_disable();
204 ko = __module_text_address (STAP_ARG_addr);
205 if (ko && ko->name)
206 {
207 strlcpy (STAP_RETVALUE, ko->name, MAXSTRINGLEN);
208 preempt_enable_no_resched();
209 return;
210 }
211 preempt_enable_no_resched();
212 #endif
213 #if STAP_COMPAT_VERSION <= STAP_VERSION(2,2)
214 strlcpy (STAP_RETVALUE, "<unknown>", MAXSTRINGLEN);
215 #else
216 CONTEXT->last_error = "Cannot determine kernel module name";
217 #endif
218 %}
219
220 /**
221 * sfunction symname - Return the kernel symbol associated with the given address
222 * @addr: The address to translate
223 *
224 * Description: Returns the (function) symbol name associated with the
225 * given address if known. If not known it will return the hex string
226 * representation of addr.
227 */
228 function symname:string (addr: long) %{ /* pure */ /* pragma:symbols */
229 _stp_snprint_addr(STAP_RETVALUE, MAXSTRINGLEN, STAP_ARG_addr,
230 _STP_SYM_SYMBOL, NULL, CONTEXT);
231 %}
232
233 /**
234 * sfunction symdata - Return the kernel symbol and module offset for the address
235 * @addr: The address to translate
236 *
237 * Description: Returns the (function) symbol name associated with the
238 * given address if known, the offset from the start and size of the
239 * symbol, plus module name (between brackets). If symbol is unknown,
240 * but module is known, the offset inside the module, plus the size of
241 * the module is added. If any element is not known it will be
242 * omitted and if the symbol name is unknown it will return the hex
243 * string for the given address.
244 */
245 function symdata:string (addr: long) %{ /* pure */ /* pragma:symbols */
246 _stp_snprint_addr(STAP_RETVALUE, MAXSTRINGLEN, STAP_ARG_addr,
247 _STP_SYM_DATA, NULL, CONTEXT);
248 %}
249
250 /**
251 * sfunction print_syms - Print out kernel stack from string
252 * @callers: String with list of hexadecimal (kernel) addresses
253 *
254 * Description: This function performs a symbolic lookup of the addresses
255 * in the given string,
256 * which are assumed to be the result of prior calls to stack(),
257 * callers(), and similar functions.
258 *
259 * Prints one line per address, including the address, the
260 * name of the function containing the address, and an estimate of
261 * its position within that function, as obtained by symdata().
262 * Returns nothing.
263 */
264 function print_syms (callers:string) {
265 __sym = tokenize (callers, " ");
266 while (__sym != "") {
267 printf (" %s : %s\n", __sym, symdata (strtol(__sym,16)));
268 __sym = tokenize ("", " ");
269 }
270 }
271
272 /**
273 * sfunction sprint_syms - Return stack for kernel addresses from string
274 *
275 * @callers: String with list of hexadecimal (kernel) addresses
276 *
277 * Perform a symbolic lookup of the addresses in the given string,
278 * which are assumed to be the result of a prior calls to stack(),
279 * callers(), and similar functions.
280 *
281 * Returns a simple backtrace from the given hex string. One line per
282 * address. Includes the symbol name (or hex address if symbol
283 * couldn't be resolved) and module name (if found), as obtained from
284 * symdata(). Includes the offset from the start of the function if
285 * found, otherwise the offset will be added to the module (if found, between
286 * brackets). Returns the backtrace as string (each line terminated by
287 * a newline character). Note that the returned stack will be
288 * truncated to MAXSTRINGLEN, to print fuller and richer stacks use
289 * print_syms().
290 */
291 function sprint_syms (callers:string) {
292 __sym = tokenize (callers, " ");
293 __foo = ""; __l = 0
294 while (__sym != "") {
295 // cleanly handle overflow instead of printing partial line:
296 __line = sprintf (" %s : %s\n", __sym, symdata (strtol(__sym,16)));
297 __l += strlen(__line)
298 if (__l > @MAXSTRINGLEN) break
299 __foo .= __line
300 __sym = tokenize ("", " ")
301 }
302 return __foo
303 }
304
305 /**
306 * sfunction symfileline - Return the file name and line number of an address.
307 * @addr: The address to translate.
308 *
309 * Description: Returns the file name and the (approximate) line number of the
310 * given address, if known. If the file name or the line number cannot be
311 * found, the hex string representation of the address will be returned.
312 */
313 function symfileline:string (addr:long) %{
314 /* pure */ /* pragma:symbols */ /* pragma:lines */
315 _stp_snprint_addr(STAP_RETVALUE, MAXSTRINGLEN, STAP_ARG_addr,
316 _STP_SYM_LINENUMBER + _STP_SYM_FILENAME, NULL, CONTEXT);
317 %}
318
319 /**
320 * sfunction usymfile - Return the file name of a given address.
321 * @addr: The address to translate.
322 *
323 * Description: Returns the file name of the given address, if known. If the
324 * file name cannot be found, the hex string representation of the address
325 * will be returned.
326 */
327 function symfile:string (addr:long) %{
328 /* pure */ /* pragma:symbols */ /* pragma:lines */
329 _stp_snprint_addr(STAP_RETVALUE, MAXSTRINGLEN, STAP_ARG_addr,
330 _STP_SYM_FILENAME, NULL, CONTEXT);
331 %}
332
333 /**
334 * sfunction usymline - Return the line number of an address.
335 * @addr: The address to translate.
336 *
337 * Description: Returns the (approximate) line number of the given address, if
338 * known. If the line number cannot be found, the hex string representation of
339 * the address will be returned.
340 */
341 function symline:string (addr:long) %{
342 /* pure */ /* pragma:symbols */ /* pragma:lines */
343 _stp_snprint_addr(STAP_RETVALUE, MAXSTRINGLEN, STAP_ARG_addr,
344 _STP_SYM_LINENUMBER, NULL, CONTEXT);
345 %}