Debian Codegraph @

tapset/linux/conversions.stp

raw
1 // conversions tapset
2 // Copyright (C) 2005-2017 Red Hat Inc.
3 // Copyright (C) 2007 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
10 /**
11 * sfunction kernel_string - Retrieves string from kernel memory
12 * @addr: The kernel address to retrieve the string from
13 *
14 * Description: This function returns the null terminated C string
15 * from a given kernel memory address. Reports an error on string
16 * copy fault.
17 */
18 function kernel_string:string (addr:long) %{ /* pure */
19 __label__ deref_fault;
20 char *destination = STAP_RETVALUE;
21 kderef_string (destination, STAP_ARG_addr, MAXSTRINGLEN);
22 if (0) {
23 deref_fault: /* branched to from deref_string() */
24 /* Why '%1p' below? On newer kernels, the snprintf() function pads
25 * out '(null)' to the same width as other pointers, which looks
26 * really odd in the following error message. Setting a format
27 * width of '1' fixes this. */
28 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
29 "kernel string copy fault at 0x%lx [man error::fault]",
30 (unsigned long) (uintptr_t) STAP_ARG_addr);
31 CONTEXT->last_error = CONTEXT->error_buffer;
32 }
33 %}
34
35 /**
36 * sfunction kernel_string - Retrieves string from kernel memory with alternative error string
37 * @addr: The kernel address to retrieve the string from
38 * @err_msg: The error message to return when data isn't available
39 *
40 * Description: This function returns the null terminated C string
41 * from a given kernel memory address. Reports the given error message
42 * on string copy fault.
43 */
44 function kernel_string:string (addr:long, err_msg:string) {
45 try { return kernel_string(addr) } catch { return err_msg }
46 }
47 function kernel_string2:string (addr:long, err_msg:string) {
48 return kernel_string(addr, err_msg);
49 }
50
51 /**
52 * sfunction kernel_string_quoted - Retrieves and quotes string from kernel memory
53 *
54 * @addr: the kernel memory address to retrieve the string from
55 *
56 * Description: Returns the null terminated C string from a given kernel
57 * memory address where any ASCII characters that are not printable are
58 * replaced by the corresponding escape sequence in the returned string. Note
59 * that the string will be surrounded by double quotes. If the kernel memory
60 * data is not accessible at the given address, the address itself is returned
61 * as a string, without double quotes.
62 */
63 function kernel_string_quoted:string (addr:long)
64 %{ /* pure */
65 int rc = 0;
66 if (STAP_ARG_addr != 0)
67 rc = _stp_text_str(STAP_RETVALUE,
68 (char *)(uintptr_t)STAP_ARG_addr,
69 MAXSTRINGLEN, MAXSTRINGLEN, 1, 0, 0);
70 if (STAP_ARG_addr == 0 || rc < 0)
71 snprintf(STAP_RETVALUE, MAXSTRINGLEN, "0x%lx",
72 (unsigned long)(uintptr_t)STAP_ARG_addr);
73 %}
74
75 /**
76 * sfunction kernel_or_user_string_quoted - Retrieves and quotes string from kernel or user memory
77 *
78 * @addr: the kernel or user memory address to retrieve the string from
79 *
80 * Similar to kernel_string_quoted except user memory is a fallback method
81 */
82 function kernel_or_user_string_quoted:string (addr:long) {
83 try { return string_quoted(kernel_string(addr)) } catch { return string_quoted(user_string(addr)) }
84 }
85
86 /**
87 * sfunction kernel_string_n - Retrieves string of given length from kernel memory
88 * @addr: The kernel address to retrieve the string from
89 * @n: The maximum length of the string (if not null terminated)
90 *
91 * Description: Returns the C string of a maximum given length from a
92 * given kernel memory address. Reports an error on string copy fault.
93 */
94 function kernel_string_n:string (addr:long, n:long) %{ /* pure */
95 __label__ deref_fault;
96 char *destination = STAP_RETVALUE;
97 int64_t len = clamp_t(int64_t, STAP_ARG_n + 1, 1, MAXSTRINGLEN);
98 kderef_string (destination, STAP_ARG_addr, len);
99 if (0) {
100 deref_fault: /* branched to from deref_string() */
101 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
102 "kernel string copy fault at 0x%lx [man error::fault]",
103 (unsigned long) (uintptr_t) STAP_ARG_addr);
104 CONTEXT->last_error = CONTEXT->error_buffer;
105 }
106 %}
107
108 /**
109 * sfunction kernel_string_utf32 - Retrieves UTF-32 string from kernel memory
110 * @addr: The kernel address to retrieve the string from
111 *
112 * Description: This function returns a null terminated UTF-8 string converted
113 * from the UTF-32 string at a given kernel memory address. Reports an error on
114 * string copy fault or conversion error.
115 */
116 function kernel_string_utf32:string (addr:long) %{ /* pure */
117 __label__ deref_fault;
118 int rc = 0, len = MAXSTRINGLEN;
119 uint32_t c32, *source = (uint32_t*)(uintptr_t)STAP_ARG_addr;
120 char *destination = STAP_RETVALUE;
121
122 *destination = '\0';
123 while (len > 1 && (c32 = kread(source))) {
124 if ((rc = _stp_convert_utf32(destination, len, c32)) <= 0) {
125 if (rc < 0) {
126 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
127 "invalid UTF-32 character U+%X at 0x%lx", c32,
128 (unsigned long) (uintptr_t) source);
129 CONTEXT->last_error = CONTEXT->error_buffer;
130 }
131 break;
132 }
133 ++source;
134 destination += rc;
135 len -= rc;
136 }
137
138 if (0) {
139 deref_fault: /* branched to from deref_string() */
140 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
141 "kernel string copy fault at 0x%lx [man error::fault]",
142 (unsigned long) (uintptr_t) source);
143 CONTEXT->last_error = CONTEXT->error_buffer;
144 }
145 %}
146
147 /**
148 * sfunction kernel_string_utf32 - Retrieves UTF-32 string from kernel memory with alternative error string
149 * @addr: The kernel address to retrieve the string from
150 * @err_msg: The error message to return when data isn't available
151 *
152 * Description: This function returns a null terminated UTF-8 string converted
153 * from the UTF-32 string at a given kernel memory address. Reports the given
154 * error message on string copy fault or conversion error.
155 */
156 function kernel_string_utf32:string (addr:long, err_msg:string) {
157 try { return kernel_string_utf32(addr) } catch { return err_msg }
158 }
159 function kernel_string2_utf32:string (addr:long, err_msg:string) {
160 return kernel_string_utf32(addr, err_msg);
161 }
162
163 /**
164 * sfunction kernel_string_quoted_utf32 - Quote given UTF-32 kernel string.
165 * @addr: The kernel address to retrieve the string from
166 *
167 * Description: This function combines quoting as per @string_quoted
168 * and UTF-32 decoding as per @kernel_string_utf32.
169 */
170 function kernel_string_quoted_utf32:string (addr:long) {
171 try { return string_quoted(kernel_string_utf32(addr)) } catch { return sprintf("0x%x", addr) }
172 }
173
174 /**
175 * sfunction kernel_or_user_string_quoted_utf32 - Retrieves and quotes UTF-32 string from kernel or user memory
176 *
177 * @addr: the kernel or user memory address to retrieve the string from
178 *
179 * Similar to kernel_string_quoted_utf32 except user memory is a fallback method
180 */
181 function kernel_or_user_string_quoted_utf32:string (addr:long) {
182 try { return string_quoted(kernel_string_utf32(addr)) } catch { return string_quoted(user_string_utf32(addr)) }
183 }
184
185 /**
186 * sfunction kernel_string_utf16 - Retrieves UTF-16 string from kernel memory
187 * @addr: The kernel address to retrieve the string from
188 *
189 * Description: This function returns a null terminated UTF-8 string converted
190 * from the UTF-16 string at a given kernel memory address. Reports an error on
191 * string copy fault or conversion error.
192 */
193 function kernel_string_utf16:string (addr:long) %{ /* pure */
194 __label__ deref_fault;
195 int rc = 0, len = MAXSTRINGLEN;
196 uint32_t c32;
197 uint16_t c16low, *source = (uint16_t*)(uintptr_t)STAP_ARG_addr;
198 char *destination = STAP_RETVALUE;
199
200 *destination = '\0';
201 while (len > 1 && (c32 = kread(source))) {
202 /* Check for a UTF-16 high surrogate, then its low pair, and combine them.
203 * Broken surrogates will just fall through to _stp_convert_utf32 and get
204 * flagged as an error there. (Or even allowed, if we decide to be lax.)
205 */
206 if (c32 >= 0xD800 && c32 <= 0xDBFF) {
207 ++source;
208 c16low = kread(source);
209 if (c16low >= 0xDC00 && c16low <= 0xDFFF)
210 c32 = 0x10000 + ((c32 & 0x3FF) << 10) + (c16low & 0x3FF);
211 else
212 --source;
213 }
214
215 if ((rc = _stp_convert_utf32(destination, len, c32)) <= 0) {
216 if (rc < 0) {
217 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
218 "invalid UTF-16 character U+%X at 0x%lx", c32,
219 (unsigned long) (uintptr_t) source);
220 CONTEXT->last_error = CONTEXT->error_buffer;
221 }
222 break;
223 }
224 ++source;
225 destination += rc;
226 len -= rc;
227 }
228
229 if (0) {
230 deref_fault: /* branched to from deref_string() */
231 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
232 "kernel string copy fault at 0x%lx [man error::fault]",
233 (unsigned long) (uintptr_t) source);
234 CONTEXT->last_error = CONTEXT->error_buffer;
235 }
236 %}
237
238 /**
239 * sfunction kernel_string_utf16 - Retrieves UTF-16 string from kernel memory with alternative error string
240 * @addr: The kernel address to retrieve the string from
241 * @err_msg: The error message to return when data isn't available
242 *
243 * Description: This function returns a null terminated UTF-8 string converted
244 * from the UTF-16 string at a given kernel memory address. Reports the given
245 * error message on string copy fault or conversion error.
246 */
247 function kernel_string_utf16:string (addr:long, err_msg:string) {
248 try { return kernel_string_utf16(addr) } catch { return err_msg }
249 }
250 function kernel_string2_utf16:string (addr:long, err_msg:string) {
251 return kernel_string_utf16(addr, err_msg);
252 }
253
254
255 /**
256 * sfunction kernel_string_quoted_utf16 - Quote given kernel UTF-16 string.
257 * @addr: The kernel address to retrieve the string from
258 *
259 * Description: This function combines quoting as per @string_quoted
260 * and UTF-16 decoding as per @kernel_string_utf16.
261 */
262 function kernel_string_quoted_utf16:string (addr:long) {
263 try { return string_quoted(kernel_string_utf16(addr)) } catch { return sprintf("0x%x", addr) }
264 }
265
266 /**
267 * sfunction kernel_or_user_string_quoted_utf16 - Retrieves and quotes UTF-16 string from kernel or user memory
268 *
269 * @addr: the kernel or user memory address to retrieve the string from
270 *
271 * Similar to kernel_string_quoted_utf16 except uses user memory as a fallback method
272 */
273 function kernel_or_user_string_quoted_utf16:string (addr:long) {
274 try { return string_quoted(kernel_string_utf16(addr)) } catch { return string_quoted(user_string_utf16(addr)) }
275 }
276
277
278 /**
279 * sfunction kernel_long - Retrieves a long value stored in kernel memory
280 * @addr: The kernel address to retrieve the long from
281 *
282 * Description: Returns the long value from a given kernel memory address.
283 * Reports an error when reading from the given address fails.
284 */
285 function kernel_long:long (addr:long) %{ /* pure */
286 __label__ deref_fault;
287 STAP_RETVALUE = kread((long *) (uintptr_t) STAP_ARG_addr);
288 if (0) {
289 deref_fault: /* branched to from kread() */
290 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
291 "kernel long copy fault at 0x%lx [man error::fault]",
292 (unsigned long) (uintptr_t) STAP_ARG_addr);
293 CONTEXT->last_error = CONTEXT->error_buffer;
294 }
295 %}
296
297 /**
298 * sfunction kernel_int - Retrieves an int value stored in kernel memory
299 * @addr: The kernel address to retrieve the int from
300 *
301 * Description: Returns the int value from a given kernel memory address.
302 * Reports an error when reading from the given address fails.
303 */
304 function kernel_int:long (addr:long) %{ /* pure */
305 __label__ deref_fault;
306 STAP_RETVALUE = kread((int *) (uintptr_t) STAP_ARG_addr);
307 if (0) {
308 deref_fault: /* branched to from kread() */
309 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
310 "kernel int copy fault at 0x%lx [man error::fault]",
311 (unsigned long) (uintptr_t) STAP_ARG_addr);
312 CONTEXT->last_error = CONTEXT->error_buffer;
313 }
314 %}
315
316 /**
317 * sfunction kernel_short - Retrieves a short value stored in kernel memory
318 * @addr: The kernel address to retrieve the short from
319 *
320 * Description: Returns the short value from a given kernel memory address.
321 * Reports an error when reading from the given address fails.
322 */
323 function kernel_short:long (addr:long) %{ /* pure */
324 __label__ deref_fault;
325 STAP_RETVALUE = kread((short *) (uintptr_t) STAP_ARG_addr);
326 if (0) {
327 deref_fault: /* branched to from kread() */
328 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
329 "kernel short copy fault at 0x%lx [man error::fault]",
330 (unsigned long) (uintptr_t) STAP_ARG_addr);
331 CONTEXT->last_error = CONTEXT->error_buffer;
332 }
333 %}
334
335 /**
336 * sfunction kernel_char - Retrieves a char value stored in kernel memory
337 * @addr: The kernel address to retrieve the char from
338 *
339 * Description: Returns the char value from a given kernel memory address.
340 * Reports an error when reading from the given address fails.
341 */
342 function kernel_char:long (addr:long) %{ /* pure */
343 __label__ deref_fault;
344 STAP_RETVALUE = kread((char *) (uintptr_t) STAP_ARG_addr);
345 if (0) {
346 deref_fault: /* branched to from kread() */
347 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
348 "kernel char copy fault at 0x%lx [man error::fault]",
349 (unsigned long) (uintptr_t) STAP_ARG_addr);
350 CONTEXT->last_error = CONTEXT->error_buffer;
351 }
352 %}
353
354 /**
355 * sfunction kernel_pointer - Retrieves a pointer value stored in kernel memory
356 * @addr: The kernel address to retrieve the pointer from
357 *
358 * Description: Returns the pointer value from a given kernel memory
359 * address. Reports an error when reading from the given address
360 * fails.
361 */
362 function kernel_pointer:long (addr:long) %{ /* pure */
363 __label__ deref_fault;
364 STAP_RETVALUE = (uintptr_t) kread((void **) (uintptr_t) STAP_ARG_addr);
365 if (0) {
366 deref_fault: /* branched to from kread() */
367 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
368 "kernel pointer copy fault at 0x%lx [man error::fault]",
369 (unsigned long) (uintptr_t) STAP_ARG_addr);
370 CONTEXT->last_error = CONTEXT->error_buffer;
371 }
372 %}
373
374 /**
375 * sfunction kernel_buffer_quoted - Retrieves and quotes buffer from kernel space
376 *
377 * @addr: the kernel space address to retrieve the buffer from
378 * @inlen: the exact length of the buffer to read
379 *
380 * Description: Reads inlen characters of a buffer from the given kernel space
381 * memory address, and returns up to MAXSTRINGLEN characters, where any ASCII
382 * characters that are not printable are replaced by the corresponding escape
383 * sequence in the returned string. Note that the string will be surrounded by
384 * double quotes. On the rare cases when kernel space data is not accessible at
385 * the given address, the address itself is returned as a string, without
386 * double quotes.
387 */
388 function kernel_buffer_quoted:string (addr:long, inlen:long) {
389 return kernel_buffer_quoted(addr, inlen, %{ /* pure */ MAXSTRINGLEN%})
390 }
391
392 /**
393 * sfunction kernel_buffer_quoted - Retrieves and quotes buffer from kernel space
394 *
395 * @addr: the kernel space address to retrieve the buffer from
396 * @inlen: the exact length of the buffer to read
397 * @outlen: the maximum length of the output string
398 *
399 * Description: Reads inlen characters of a buffer from the given kernel space
400 * memory address, and returns up to outlen characters, where any ASCII
401 * characters that are not printable are replaced by the corresponding escape
402 * sequence in the returned string. Note that the string will be surrounded by
403 * double quotes. On the rare cases when kernel space data is not accessible at
404 * the given address, the address itself is returned as a string, without
405 * double quotes.
406 */
407 function kernel_buffer_quoted:string (addr:long, inlen:long, outlen:long)
408 %{ /* pure */
409 size_t outlen = (size_t)clamp_t(int, STAP_ARG_outlen, 0, MAXSTRINGLEN);
410 if (outlen == 0)
411 return;
412 if ( _stp_text_str(STAP_RETVALUE, (char *)(uintptr_t)STAP_ARG_addr,
413 STAP_ARG_inlen, outlen, 1, 0, 1) < 0)
414 snprintf(STAP_RETVALUE, outlen, "0x%lx",
415 (unsigned long)(void *)(uintptr_t)STAP_ARG_addr);
416 %}
417
418 /**
419 * sfunction kernel_buffer_quoted_error - Retrieves and quotes buffer from kernel space
420 *
421 * @addr: the kernel space address to retrieve the buffer from
422 * @inlen: the exact length of the buffer to read
423 * @outlen: the maximum length of the output string
424 *
425 * Description: Reads inlen characters of a buffer from the given kernel space
426 * memory address, and returns up to outlen characters, where any ASCII
427 * characters that are not printable are replaced by the corresponding escape
428 * sequence in the returned string. Note that the string will be surrounded by
429 * double quotes. On the rare cases when kernel space data is not accessible at
430 * the given address, an error is thrown.
431 */
432 function kernel_buffer_quoted_error:string (addr:long, inlen:long, outlen:long)
433 %{ /* pure */
434 size_t outlen = (size_t)clamp_t(int, STAP_ARG_outlen, 0, MAXSTRINGLEN);
435 if (outlen == 0
436 || _stp_text_str(STAP_RETVALUE, (char *) (uintptr_t) STAP_ARG_addr,
437 STAP_ARG_inlen, outlen, 1, 0, 1) < 0)
438 STAP_ERROR("Unable to access kernel space data at 0x%lx",
439 (unsigned long)(void *)(uintptr_t)STAP_ARG_addr);
440 %}