Debian Codegraph @

tapset/uconversions.stp

raw
1 // userspace conversions tapset
2 // Copyright (C) 2005-2021 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 // implement in terms of runtime/loc2c-runtime.h macro uread()
11 %{
12 #define STP_GET_USER(t) \
13 do { \
14 t *_ptr = (t*) (uintptr_t) STAP_ARG_addr; \
15 STAP_RETVALUE = uread (_ptr); \
16 } while (0)
17 %}
18
19 /**
20 * sfunction user_string - Retrieves string from user space
21 *
22 * @addr: the user space address to retrieve the string from
23 *
24 * Description: Returns the null terminated C string from a given user space
25 * memory address. Reports an error on the rare cases when userspace
26 * data is not accessible.
27 */
28 function user_string:string (addr:long) {
29 return user_string_n(addr, @MAXSTRINGLEN)
30 }
31
32 /**
33 * sfunction user_string_nofault - Retrieves string from user space
34 *
35 * @addr: the user space address to retrieve the string from
36 *
37 * Description: Returns the null terminated C string from a given user space
38 * memory address. Returns the empty string if userspace data is not accessible.
39 */
40 function user_string_nofault:string (addr:long) {
41 try { return user_string(addr) } catch { return "" }
42 }
43
44 /**
45 * sfunction user_string - Retrieves string from user space with alternative error string
46 *
47 * @addr: the user space address to retrieve the string from
48 * @err_msg: the error message to return when data isn't available
49 *
50 * Description: Returns the null terminated C string from a given user space
51 * memory address. Reports the given error message on the rare cases when
52 * userspace data is not accessible.
53 */
54 function user_string:string (addr:long, err_msg:string) {
55 return user_string_n(addr, @MAXSTRINGLEN, err_msg)
56 }
57 function user_string2:string (addr:long, err_msg:string) {
58 return user_string_n(addr, @MAXSTRINGLEN, err_msg)
59 }
60
61 /**
62 * sfunction user_string_warn - Retrieves string from user space
63 *
64 * @addr: the user space address to retrieve the string from
65 *
66 * Description: Returns the null terminated C string from a given user space
67 * memory address. Reports "" on the rare cases when userspace data is
68 * not accessible and warns (but does not abort) about the failure.
69 */
70 function user_string_warn:string (addr:long) {
71 %(systemtap_v < "2.3" %? // PR15044
72 return user_string_warn(addr, "<unknown>")
73 %:
74 return user_string_warn(addr, "")
75 %)
76 }
77
78
79 /**
80 * sfunction user_string_warn - Retrieves string from user space with alternative warning string
81 *
82 * @addr: the user space address to retrieve the string from
83 * @warn_msg: the warning message to return when data isn't available
84 *
85 * Description: Returns the null terminated C string from a given user space
86 * memory address. Reports the given warning message on the rare cases
87 * when userspace data is not accessible and warns (but does not abort)
88 * about the failure.
89 */
90 function user_string_warn:string (addr:long, warn_msg:string) {
91 return user_string_n_warn(addr, @MAXSTRINGLEN, warn_msg)
92 }
93 function user_string2_warn:string (addr:long, warn_msg:string) {
94 return user_string_n_warn(addr, @MAXSTRINGLEN, warn_msg)
95 }
96
97 /**
98 * sfunction user_string_quoted - Retrieves and quotes string from user space
99 *
100 * @addr: the user space address to retrieve the string from
101 *
102 * Description: Returns the null terminated C string from a given user space
103 * memory address where any ASCII characters that are not printable are
104 * replaced by the corresponding escape sequence in the returned string. Note
105 * that the string will be surrounded by double quotes. On the rare cases when
106 * userspace data is not accessible at the given address, the address itself is
107 * returned as a string, without double quotes.
108 */
109 function user_string_quoted:string (addr:long) {
110 try { return string_quoted(user_string(addr)) } catch { return sprintf("0x%x", addr) }
111 }
112
113
114
115
116 // XXX this and other functions also implemented in tapset/bpf/uconversions.stp
117 %( runtime != "bpf" %?
118 /**
119 * sfunction user_string_n - Retrieves string of given length from user space
120 *
121 * @addr: the user space address to retrieve the string from
122 * @n: the maximum length of the string (if not null terminated)
123 *
124 * Description: Returns the C string of a maximum given length from a
125 * given user space address. Reports an error on the rare cases
126 * when userspace data is not accessible at the given address.
127 */
128 function user_string_n:string (addr:long, n:long)
129 %( systemtap_v < "2.3" %? // PR15044
130 { return user_string_n(addr, n, "<unknown>") }
131 %:
132 %{ /* pure */ /* myproc-unprivileged */
133 long rc;
134 int64_t len = clamp_t(int64_t, STAP_ARG_n + 1, 1,
135 MAXSTRINGLEN - 1);
136 rc = _stp_strncpy_from_user(STAP_RETVALUE,
137 (char __user *) (uintptr_t) STAP_ARG_addr, len);
138 if (rc < 0) {
139 strlcpy(STAP_RETVALUE, "<unknown>", MAXSTRINGLEN);
140 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
141 "user string copy fault %ld at 0x%lx [man error::fault]", rc,
142 (unsigned long) (uintptr_t) STAP_ARG_addr);
143 CONTEXT->last_error = CONTEXT->error_buffer;
144 } else
145 STAP_RETVALUE[len - 1] = '\0';
146 %}
147 %)
148 %)
149
150 /**
151 * sfunction user_string_n_nofault - Retrieves string of given length from user space
152 *
153 * @addr: the user space address to retrieve the string from
154 * @n: the maximum length of the string (if not null terminated)
155 *
156 * Description: Returns the C string of a maximum given length from a
157 * given user space address. Returns the empty string when userspace
158 * data is not accessible at the given address.
159 */
160 function user_string_n_nofault (addr:long, n:long) {
161 try { return user_string_n(addr, n) } catch { return "" }
162 }
163
164 %( runtime != "bpf" %?
165 /**
166 * sfunction user_string_n - Retrieves string of given length from user space
167 *
168 * @addr: the user space address to retrieve the string from
169 * @n: the maximum length of the string (if not null terminated)
170 * @err_msg: the error message to return when data isn't available
171 *
172 * Description: Returns the C string of a maximum given length from a
173 * given user space address. Returns the given error message string on
174 * the rare cases when userspace data is not accessible at the given
175 * address.
176 */
177 function user_string_n:string (addr:long, n:long, err_msg:string)
178 %{ /* pure */ /* myproc-unprivileged */ /* unmodified-fnargs */
179 int64_t len = clamp_t(int64_t, STAP_ARG_n + 1, 1, MAXSTRINGLEN - 1);
180 if (_stp_strncpy_from_user(STAP_RETVALUE,
181 (char __user *) (uintptr_t) STAP_ARG_addr, len) < 0)
182 strlcpy(STAP_RETVALUE, STAP_ARG_err_msg, MAXSTRINGLEN);
183 else
184 STAP_RETVALUE[len - 1] = '\0';
185 %}
186 %)
187 function user_string_n2:string (addr:long, n:long, err_msg:string) {
188 return user_string_n(addr, n, err_msg);
189 }
190
191 /**
192 * sfunction user_string_n_warn - Retrieves string from user space
193 *
194 * @addr: the user space address to retrieve the string from
195 * @n: the maximum length of the string (if not null terminated)
196 *
197 * Description: Returns up to n characters of a C string from a given
198 * user space memory address. Reports "<unknown>" on the rare cases
199 * when userspace data is not accessible and warns (but does not abort)
200 * about the failure.
201 */
202 function user_string_n_warn:string (addr:long, n:long) {
203 %(systemtap_v < "2.3" %? // PR15044
204 return user_string_n_warn(addr, n, "<unknown>")
205 %:
206 return user_string_n_warn(addr, n, "")
207 %)
208 }
209
210 %( runtime != "bpf" %?
211 /**
212 * sfunction user_string_n_warn - Retrieves string from user space with alternative warning string
213 *
214 * @addr: the user space address to retrieve the string from
215 * @n: the maximum length of the string (if not null terminated)
216 * @warn_msg: the warning message to return when data isn't available
217 *
218 * Description: Returns up to n characters of a C string from a given
219 * user space memory address. Reports the given warning message on the
220 * rare cases when userspace data is not accessible and warns (but does
221 * not abort) about the failure.
222 */
223 function user_string_n_warn:string (addr:long, n:long, warn_msg:string)
224 %{ /* pure */ /* myproc-unprivileged */ /* unmodified-fnargs */
225 int64_t len = clamp_t(int64_t, STAP_ARG_n + 1, 1, MAXSTRINGLEN - 1);
226 long rc;
227 rc = _stp_strncpy_from_user(STAP_RETVALUE,
228 (char __user *) (uintptr_t) STAP_ARG_addr, len);
229 if (rc < 0) {
230 // NB: using error_buffer to get local space for the warning, but we're
231 // not aborting, so leave last_error alone.
232 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
233 "user string copy fault %ld at 0x%lx [man error::fault]", rc,
234 (unsigned long) (uintptr_t) STAP_ARG_addr);
235 _stp_warn(CONTEXT->error_buffer);
236 strlcpy (STAP_RETVALUE, STAP_ARG_warn_msg, MAXSTRINGLEN);
237 } else
238 STAP_RETVALUE[len - 1] = '\0';
239 %}
240 %)
241 function user_string2_n_warn:string (addr:long, n:long, warn_msg:string) {
242 user_string_n_warn(addr, n, warn_msg);
243 }
244
245 /**
246 * sfunction user_string_n_quoted - Retrieves and quotes string from user space
247 *
248 * @addr: the user space address to retrieve the string from
249 * @n: the maximum length of the string (if not null terminated)
250 *
251 * Description: Returns up to n characters of a C string from the given
252 * user space memory address where any ASCII characters that are not
253 * printable are replaced by the corresponding escape sequence in the
254 * returned string. Note that the string will be surrounded by double quotes.
255 * On the rare cases when userspace data is not accessible at the given address,
256 * the address itself is returned as a string, without double quotes.
257 */
258 function user_string_n_quoted:string (addr:long, n:long) {
259 %(systemtap_v < "2.4" %?
260 // We used to count n by output characters
261 return user_string_n_quoted(addr, @MAXSTRINGLEN, n)
262 %:
263 return user_string_n_quoted(addr, n, @MAXSTRINGLEN)
264 %)
265 }
266
267 /**
268 * sfunction user_string_n_quoted - Retrieves and quotes string from user space
269 *
270 * @addr: the user space address to retrieve the string from
271 * @inlen: the maximum length of the string to read (if not null terminated)
272 * @outlen: the maximum length of the output string
273 *
274 * Description: Reads up to inlen characters of a C string from the given user
275 * space memory address, and returns up to outlen characters, where any ASCII
276 * characters that are not printable are replaced by the corresponding escape
277 * sequence in the returned string. Note that the string will be surrounded by
278 * double quotes. On the rare cases when userspace data is not accessible at
279 * the given address, the address itself is returned as a string, without
280 * double quotes.
281 */
282 function user_string_n_quoted:string (addr:long, inlen:long, outlen:long)
283 %( systemtap_v >= "3.0" %?
284 {
285 try
286 {
287 __str = string_quoted(user_string_n(addr, inlen))
288 if (strlen(__str) > outlen-2) // room for the double-quotes plus \ldots
289 __str = (substr(__str,0,outlen-4) . "\"...")
290 return __str
291 }
292 catch
293 {
294 return sprintf("0x%x", addr)
295 }
296 }
297 %:
298
299 %{ /* pure */ /* myproc-unprivileged */
300 // Note the lack of STAP_ARG_n+1 as in other funcs() -- PR15617
301 int64_t inlen = clamp_t(int64_t, STAP_ARG_inlen, 0, MAXSTRINGLEN);
302 int64_t outlen = clamp_t(int64_t, STAP_ARG_outlen, 0, MAXSTRINGLEN);
303 if (STAP_ARG_addr == 0)
304 #if STAP_COMPAT_VERSION < STAP_VERSION(2,3) // PR15044
305 strlcpy(STAP_RETVALUE, "NULL", MAXSTRINGLEN);
306 #else
307 snprintf(STAP_RETVALUE, MAXSTRINGLEN, "(null)");
308 #endif
309 else {
310 int rc = _stp_text_str(STAP_RETVALUE,
311 (char *)(uintptr_t)STAP_ARG_addr, inlen, outlen, 1, 1, 0);
312 if (rc < 0) {
313 #if STAP_COMPAT_VERSION < STAP_VERSION(2,3) // PR15044
314 strlcpy(STAP_RETVALUE, "<unknown>", MAXSTRINGLEN);
315 #else
316 #ifdef CONFIG_COMPAT
317 if (_stp_is_compat_task())
318 snprintf(STAP_RETVALUE, MAXSTRINGLEN, "%x",
319 (compat_long_t)STAP_ARG_addr);
320 else
321 #endif
322 snprintf(STAP_RETVALUE, MAXSTRINGLEN, "0x%lx",
323 (unsigned long) (uintptr_t)STAP_ARG_addr);
324 #endif
325 }
326 }
327 %}
328
329 %)
330 function user_string_n2_quoted:string (addr:long, inlen:long, outlen:long) {
331 return user_string_n_quoted(addr, inlen, outlen);
332
333 }
334
335 /**
336 * sfunction user_string_utf32 - Retrieves UTF-32 string from user memory
337 * @addr: The user address to retrieve the string from
338 *
339 * Description: This function returns a null terminated UTF-8 string converted
340 * from the UTF-32 string at a given user memory address. Reports an error on
341 * string copy fault or conversion error.
342 */
343 function user_string_utf32:string (addr:long) %{ /* pure */ /* myproc-unprivileged */
344 __label__ deref_fault;
345 int rc = 0, len = MAXSTRINGLEN;
346 uint32_t c32, *source = (uint32_t*)(uintptr_t)STAP_ARG_addr;
347 char *destination = STAP_RETVALUE;
348
349 *destination = '\0';
350 while (len > 1 && (c32 = uread(source))) {
351 if ((rc = _stp_convert_utf32(destination, len, c32)) <= 0) {
352 if (rc < 0) {
353 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
354 "invalid UTF-32 character U+%X at 0x%lx", c32,
355 (unsigned long) (uintptr_t) source);
356 CONTEXT->last_error = CONTEXT->error_buffer;
357 }
358 break;
359 }
360 ++source;
361 destination += rc;
362 len -= rc;
363 }
364
365 if (0) {
366 deref_fault: /* branched to from deref_string() */
367 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
368 "user string copy fault at 0x%lx [man error::fault]",
369 (unsigned long) (uintptr_t) source);
370 CONTEXT->last_error = CONTEXT->error_buffer;
371 }
372 %}
373
374 /**
375 * sfunction user_string_utf32 - Retrieves UTF-32 string from user memory with alternative error string
376 * @addr: The user address to retrieve the string from
377 * @err_msg: The error message to return when data isn't available
378 *
379 * Description: This function returns a null terminated UTF-8 string converted
380 * from the UTF-32 string at a given user memory address. Reports the given
381 * error message on string copy fault or conversion error.
382 */
383 function user_string_utf32:string (addr:long, err_msg:string) {
384 try { return user_string_utf32(addr) } catch { return err_msg }
385 }
386 function user_string2_utf32:string (addr:long, err_msg:string) {
387 return user_string_utf32 (addr, err_msg);
388 }
389
390
391 /**
392 * sfunction user_string_quoted_utf32 - Quote given user UTF-32 string.
393 * @addr: The user address to retrieve the string from
394 *
395 * Description: This function combines quoting as per @string_quoted
396 * and UTF-32 decoding as per @user_string_utf32.
397 */
398 function user_string_quoted_utf32:string (addr:long) {
399 try { return string_quoted(user_string_utf32(addr)) } catch { return sprintf("0x%x", addr) }
400 }
401
402
403
404 /**
405 * sfunction user_string_utf16 - Retrieves UTF-16 string from user memory
406 * @addr: The user address to retrieve the string from
407 *
408 * Description: This function returns a null terminated UTF-8 string converted
409 * from the UTF-16 string at a given user memory address. Reports an error on
410 * string copy fault or conversion error.
411 */
412 function user_string_utf16:string (addr:long) %{ /* pure */ /* myproc-unprivileged */
413 __label__ deref_fault;
414 int rc = 0, len = MAXSTRINGLEN;
415 uint32_t c32;
416 uint16_t c16low, *source = (uint16_t*)(uintptr_t)STAP_ARG_addr;
417 char *destination = STAP_RETVALUE;
418
419 *destination = '\0';
420 while (len > 1 && (c32 = uread(source))) {
421 /* Check for a UTF-16 high surrogate, then its low pair, and combine them.
422 * Broken surrogates will just fall through to _stp_convert_utf32 and get
423 * flagged as an error there. (Or even allowed, if we decide to be lax.)
424 */
425 if (c32 >= 0xD800 && c32 <= 0xDBFF) {
426 ++source;
427 c16low = uread(source);
428 if (c16low >= 0xDC00 && c16low <= 0xDFFF)
429 c32 = 0x10000 + ((c32 & 0x3FF) << 10) + (c16low & 0x3FF);
430 else
431 --source;
432 }
433
434 if ((rc = _stp_convert_utf32(destination, len, c32)) <= 0) {
435 if (rc < 0) {
436 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
437 "invalid UTF-16 character U+%X at 0x%lx", c32,
438 (unsigned long) (uintptr_t) source);
439 CONTEXT->last_error = CONTEXT->error_buffer;
440 }
441 break;
442 }
443 ++source;
444 destination += rc;
445 len -= rc;
446 }
447
448 if (0) {
449 deref_fault: /* branched to from deref_string() */
450 snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
451 "user string copy fault at 0x%lx [man error::fault]",
452 (unsigned long) (uintptr_t) source);
453 CONTEXT->last_error = CONTEXT->error_buffer;
454 }
455 %}
456
457 /**
458 * sfunction user_string_utf16 - Retrieves UTF-16 string from user memory with alternative error string
459 * @addr: The user address to retrieve the string from
460 * @err_msg: The error message to return when data isn't available
461 *
462 * Description: This function returns a null terminated UTF-8 string converted
463 * from the UTF-16 string at a given user memory address. Reports the given
464 * error message on string copy fault or conversion error.
465 */
466 function user_string_utf16:string (addr:long, err_msg:string) {
467 try { return user_string_utf16(addr) } catch { return err_msg }
468 }
469 function user_string2_utf16:string (addr:long, err_msg:string) {
470 return user_string_utf16(addr, err_msg);
471 }
472
473
474 /**
475 * sfunction user_string_quoted_utf16 - Quote given user UTF-16 string.
476 * @addr: The user address to retrieve the string from
477 *
478 * Description: This function combines quoting as per @string_quoted
479 * and UTF-16 decoding as per @user_string_utf16.
480 */
481 function user_string_quoted_utf16:string (addr:long) {
482 try { return string_quoted(user_string_utf16(addr)) } catch { return sprintf("0x%x", addr) }
483 }
484
485
486 %( runtime != "bpf" %?
487 /**
488 * sfunction user_char_error - Retrieves a char value stored in user space
489 *
490 * @addr: the user space address to retrieve the char from
491 *
492 * Description: Returns the char value from a given user space address.
493 * If the user space data is not accessible, an error will occur.
494 */
495 function user_char_error:long (addr:long) %{ /* pure */ /* myproc-unprivileged */
496 STP_GET_USER(char);
497 CATCH_DEREF_FAULT();
498 %}
499 %)
500
501 /**
502 * sfunction user_char - Retrieves a char value stored in user space
503 *
504 * @addr: the user space address to retrieve the char from
505 *
506 * Description: Returns the char value from a given user space address.
507 * Returns zero when user space data is not accessible.
508 */
509 function user_char:long (addr:long) {
510 try { return user_char_error(addr) } catch { return 0 }
511 }
512
513 /**
514 * sfunction user_char_warn - Retrieves a char value stored in user space
515 *
516 * @addr: the user space address to retrieve the char from
517 *
518 * Description: Returns the char value from a given user space
519 * address. Returns zero when user space data is not accessible and
520 * warns about the failure (but does not error).
521 */
522 function user_char_warn:long (addr:long) {
523 try {
524 return user_char_error(addr)
525 } catch {
526 warn(sprintf("user char copy fault at %p [man error::fault]",
527 addr))
528 return 0
529 }
530 }
531
532 %( runtime != "bpf" %?
533 /**
534 * sfunction user_short_error - Retrieves a short value stored in user space
535 *
536 * @addr: the user space address to retrieve the short from
537 *
538 * Description: Returns the short value from a given user space address.
539 * If the user space data is not accessible, an error will occur.
540 */
541 function user_short_error:long (addr:long) %{ /* pure */ /* myproc-unprivileged */
542 STP_GET_USER(short);
543 CATCH_DEREF_FAULT();
544 %}
545 %)
546
547 /**
548 * sfunction user_short - Retrieves a short value stored in user space
549 *
550 * @addr: the user space address to retrieve the short from
551 *
552 * Description: Returns the short value from a given user space address.
553 * Returns zero when user space data is not accessible.
554 */
555 function user_short:long (addr:long) {
556 try { return user_short_error(addr) } catch { return 0 }
557 }
558
559 /**
560 * sfunction user_short_warn - Retrieves a short value stored in user space
561 *
562 * @addr: the user space address to retrieve the short from
563 *
564 * Description: Returns the short value from a given user space
565 * address. Returns zero when user space data is not accessible and
566 * warns about the failure (but does not error).
567 */
568 function user_short_warn:long (addr:long) {
569 try {
570 return user_short_error(addr)
571 } catch {
572 warn(sprintf("user short copy fault %p [man error::fault]",
573 addr))
574 return 0
575 }
576 }
577
578 %( runtime != "bpf" %?
579 /**
580 * sfunction user_ushort_error - Retrieves an unsigned short value stored in user space
581 *
582 * @addr: the user space address to retrieve the unsigned short from
583 *
584 * Description: Returns the unsigned short value from a given user
585 * space address. If the user space data is not accessible, an error
586 * will occur.
587 */
588 function user_ushort_error:long (addr:long) %{ /* pure */ /* myproc-unprivileged */
589 STP_GET_USER(unsigned short);
590 CATCH_DEREF_FAULT();
591 %}
592 %)
593
594 /**
595 * sfunction user_ushort - Retrieves an unsigned short value stored in user space
596 *
597 * @addr: the user space address to retrieve the unsigned short from
598 *
599 * Description: Returns the unsigned short value from a given user
600 * space address. Returns zero when user space data is not accessible.
601 */
602 function user_ushort:long (addr:long)
603 {
604 try { return user_ushort_error(addr) } catch { return 0 }
605 }
606
607 /**
608 * sfunction user_ushort_warn - Retrieves an unsigned short value stored in user space
609 *
610 * @addr: the user space address to retrieve the unsigned short from
611 *
612 * Description: Returns the unsigned short value from a given user
613 * space address. Returns zero when user space data is not accessible
614 * and warns about the failure (but does not error).
615 */
616 function user_ushort_warn:long (addr:long)
617 {
618 try {
619 return user_ushort_error(addr)
620 } catch {
621 warn(sprintf("user unsigned short copy fault %p [man error::fault]",
622 addr))
623 return 0
624 }
625 }
626
627 %( runtime != "bpf" %?
628 /**
629 * sfunction user_int_error - Retrieves an int value stored in user space
630 *
631 * @addr: the user space address to retrieve the int from
632 *
633 * Description: Returns the int value from a given user space address.
634 * If the user space data is not accessible, an error will occur.
635 */
636 function user_int_error:long (addr:long) %{ /* pure */ /* myproc-unprivileged */
637 STP_GET_USER(int);
638 CATCH_DEREF_FAULT();
639 %}
640 %)
641
642 /**
643 * sfunction user_int - Retrieves an int value stored in user space
644 *
645 * @addr: the user space address to retrieve the int from
646 *
647 * Description: Returns the int value from a given user space address.
648 * Returns zero when user space data is not accessible.
649 */
650 function user_int:long (addr:long)
651 {
652 try { return user_int_error(addr) } catch { return 0 }
653 }
654
655 /**
656 * sfunction user_int_warn - Retrieves an int value stored in user space
657 *
658 * @addr: the user space address to retrieve the int from
659 *
660 * Description: Returns the int value from a given user space address.
661 * Returns zero when user space data is not accessible and warns about
662 * the failure (but does not error).
663 */
664 function user_int_warn:long (addr:long)
665 {
666 try {
667 return user_int_error(addr)
668 } catch {
669 warn(sprintf("user int copy fault %p [man error::fault]",
670 addr))
671 return 0
672 }
673 }
674
675 %( runtime != "bpf" %?
676 /**
677 * sfunction user_long_error - Retrieves a long value stored in user space
678 *
679 * @addr: the user space address to retrieve the long from
680 *
681 * Description: Returns the long value from a given user space
682 * address. If the user space data is not accessible, an error will
683 * occur. Note that the size of the long depends on the architecture
684 * of the current user space task (for those architectures that
685 * support both 64/32 bit compat tasks).
686 */
687 function user_long_error:long (addr:long)
688 %{ /* pure */ /* myproc-unprivileged */
689 #ifdef CONFIG_COMPAT
690 if (_stp_is_compat_task())
691 STP_GET_USER(compat_long_t);
692 else
693 #endif
694 STP_GET_USER(long);
695 CATCH_DEREF_FAULT();
696 %}
697 %)
698
699 /**
700 * sfunction user_long - Retrieves a long value stored in user space
701 *
702 * @addr: the user space address to retrieve the long from
703 *
704 * Description: Returns the long value from a given user space address.
705 * Returns zero when user space data is not accessible. Note that the
706 * size of the long depends on the architecture of the current user space
707 * task (for those architectures that support both 64/32 bit compat tasks).
708 */
709 function user_long:long (addr:long) {
710 try { return user_long_error(addr) } catch { return 0 }
711 }
712
713 /**
714 * sfunction user_long_warn - Retrieves a long value stored in user space
715 *
716 * @addr: the user space address to retrieve the long from
717 *
718 * Description: Returns the long value from a given user space
719 * address. Returns zero when user space data is not accessible and
720 * warns about the failure (but does not error). Note that the size
721 * of the long depends on the architecture of the current user space
722 * task (for those architectures that support both 64/32 bit compat
723 * tasks).
724 */
725 function user_long_warn:long (addr:long) {
726 try {
727 return user_long_error(addr)
728 } catch {
729 warn(sprintf("user long copy fault %p [man error::fault]",
730 addr))
731 return 0
732 }
733 }
734
735 %( runtime != "bpf" %?
736 /**
737 * sfunction user_ulong_error - Retrieves a unsigned long value stored in user space
738 *
739 * @addr: the user space address to retrieve the unsigned long from
740 *
741 * Description: Returns the unsigned long value from a given user space
742 * address. If the user space data is not accessible, an error will
743 * occur. Note that the size of the unsigned long depends on the architecture
744 * of the current user space task (for those architectures that
745 * support both 64/32 bit compat tasks).
746 */
747 function user_ulong_error:long (addr:long) %{ /* pure */ /* myproc-unprivileged */
748 #ifdef CONFIG_COMPAT
749 if (_stp_is_compat_task())
750 STP_GET_USER(compat_ulong_t);
751 else
752 #endif
753 STP_GET_USER(unsigned long);
754 CATCH_DEREF_FAULT();
755 %}
756 %)
757
758 /**
759 * sfunction user_ulong - Retrieves an unsigned long value stored in user space
760 *
761 * @addr: the user space address to retrieve the unsigned long from
762 *
763 * Description: Returns the unsigned long value from a given user
764 * space address. Returns zero when user space data is not
765 * accessible. Note that the size of the unsigned long depends on the
766 * architecture of the current user space task (for those
767 * architectures that support both 64/32 bit compat tasks).
768 */
769 function user_ulong:long (addr:long) {
770 try { return user_ulong_error(addr) } catch { return 0 }
771 }
772
773 /**
774 * sfunction user_ulong_warn - Retrieves an unsigned long value stored in user space
775 *
776 * @addr: the user space address to retrieve the unsigned long from
777 *
778 * Description: Returns the unsigned long value from a given user
779 * space address. Returns zero when user space data is not accessible
780 * and warns about the failure (but does not error). Note that the
781 * size of the unsigned long depends on the architecture of the
782 * current user space task (for those architectures that support both
783 * 64/32 bit compat tasks).
784 */
785 function user_ulong_warn:long (addr:long) {
786 try {
787 return user_ulong_error(addr)
788 } catch {
789 warn(sprintf("user unsigned long copy fault %p [man error::fault]",
790 addr))
791 return 0
792 }
793 }
794
795 %( runtime != "bpf" %?
796 /**
797 * sfunction user_int8_error - Retrieves a 8-bit integer value stored in user space
798 *
799 * @addr: the user space address to retrieve the 8-bit integer from
800 *
801 * Description: Returns the 8-bit integer value from a given user
802 * space address. If the user space data is not accessible, an error
803 * will occur.
804 */
805 function user_int8_error:long (addr:long) %{ /* pure */ /* myproc-unprivileged */
806 STP_GET_USER(int8_t);
807 CATCH_DEREF_FAULT();
808 %}
809 %)
810
811 /**
812 * sfunction user_int8 - Retrieves a 8-bit integer value stored in user space
813 *
814 * @addr: the user space address to retrieve the 8-bit integer from
815 *
816 * Description: Returns the 8-bit integer value from a given user space
817 * address. Returns zero when user space data is not accessible.
818 */
819 function user_int8:long (addr:long) {
820 try { return user_int8_error(addr) } catch { return 0 }
821 }
822
823
824 %( runtime != "bpf" %?
825 /**
826 * sfunction user_uint8_error - Retrieves a unsigned 8-bit integer value stored in user space
827 *
828 * @addr: the user space address to retrieve the unsigned 8-bit integer from
829 *
830 * Description: Returns the unsigned 8-bit integer value from a given
831 * user space address. If the user space data is not accessible, an
832 * error will occur.
833 */
834 function user_uint8_error:long (addr:long) %{ /* pure */ /* myproc-unprivileged */
835 STP_GET_USER(uint8_t);
836 CATCH_DEREF_FAULT();
837 %}
838 %)
839
840 /**
841 * sfunction user_uint8 - Retrieves a unsigned 8-bit integer value stored in user space
842 *
843 * @addr: the user space address to retrieve the unsigned 8-bit integer from
844 *
845 * Description: Returns the unsigned 8-bit integer value from a given
846 * user space address. Returns zero when user space data is not
847 * accessible.
848 */
849 function user_uint8:long (addr:long) {
850 try { return user_uint8_error(addr) } catch { return 0 }
851 }
852
853 %( runtime != "bpf" %?
854 /**
855 * sfunction user_int16_error - Retrieves a 16-bit integer value stored in user space
856 *
857 * @addr: the user space address to retrieve the 16-bit integer from
858 *
859 * Description: Returns the 16-bit integer value from a given user
860 * space address. If the user space data is not accessible, an error
861 * will occur.
862 */
863 function user_int16_error:long (addr:long) %{ /* pure */ /* myproc-unprivileged */
864 STP_GET_USER(int16_t);
865 CATCH_DEREF_FAULT();
866 %}
867 %)
868
869 /**
870 * sfunction user_int16 - Retrieves a 16-bit integer value stored in user space
871 *
872 * @addr: the user space address to retrieve the 16-bit integer from
873 *
874 * Description: Returns the 16-bit integer value from a given user space
875 * address. Returns zero when user space data is not accessible.
876 */
877 function user_int16:long (addr:long) {
878 try { return user_int16_error(addr) } catch { return 0 }
879 }
880
881 %( runtime != "bpf" %?
882 /**
883 * sfunction user_uint16_error - Retrieves an unsigned 16-bit integer value stored in user space
884 *
885 * @addr: the user space address to retrieve the unsigned 16-bit integer from
886 *
887 * Description: Returns the unsigned 16-bit integer value from a given user
888 * space address. If the user space data is not accessible, an error
889 * will occur.
890 */
891 function user_uint16_error:long (addr:long) %{ /* pure */ /* myproc-unprivileged */
892 STP_GET_USER(uint16_t);
893 CATCH_DEREF_FAULT();
894 %}
895 %)
896
897 /**
898 * sfunction user_uint16 - Retrieves an unsigned 16-bit integer value stored in user space
899 *
900 * @addr: the user space address to retrieve the unsigned 16-bit integer from
901 *
902 * Description: Returns the unsigned 16-bit integer value from a given user
903 * space address. Returns zero when user space data is not accessible.
904 */
905 function user_uint16:long (addr:long) {
906 try { return user_uint16_error(addr) } catch { return 0 }
907 }
908
909 %( runtime != "bpf" %?
910 /**
911 * sfunction user_int32_error - Retrieves a 32-bit integer value stored in user space
912 *
913 * @addr: the user space address to retrieve the 32-bit integer from
914 *
915 * Description: Returns the 32-bit integer value from a given user
916 * space address. If the user space data is not accessible, an error
917 * will occur.
918 */
919 function user_int32_error:long (addr:long) %{ /* pure */ /* myproc-unprivileged */
920 STP_GET_USER(int32_t);
921 CATCH_DEREF_FAULT();
922 %}
923 %)
924
925 /**
926 * sfunction user_int32 - Retrieves a 32-bit integer value stored in user space
927 *
928 * @addr: the user space address to retrieve the 32-bit integer from
929 *
930 * Description: Returns the 32-bit integer value from a given user space
931 * address. Returns zero when user space data is not accessible.
932 */
933 function user_int32:long (addr:long) {
934 try { return user_int32_error(addr) } catch { return 0 }
935 }
936
937 %( runtime != "bpf" %?
938 /**
939 * sfunction user_uint32_error - Retrieves an unsigned 32-bit integer value stored in user space
940 *
941 * @addr: the user space address to retrieve the unsigned 32-bit integer from
942 *
943 * Description: Returns the unsigned 32-bit integer value from a given
944 * user space address. If the user space data is not accessible, an
945 * error will occur.
946 */
947 function user_uint32_error:long (addr:long) %{ /* pure */ /* myproc-unprivileged */
948 STP_GET_USER(uint32_t);
949 CATCH_DEREF_FAULT();
950 %}
951 %)
952
953 /**
954 * sfunction user_uint32 - Retrieves an unsigned 32-bit integer value stored in user space
955 *
956 * @addr: the user space address to retrieve the unsigned 32-bit integer from
957 *
958 * Description: Returns the unsigned 32-bit integer value from a given user
959 * space address. Returns zero when user space data is not accessible.
960 */
961 function user_uint32:long (addr:long) {
962 try { return user_uint32_error(addr) } catch { return 0 }
963 }
964
965 %( runtime != "bpf" %?
966 /**
967 * sfunction user_int64_error - Retrieves a 64-bit integer value stored in user space
968 *
969 * @addr: the user space address to retrieve the 64-bit integer from
970 *
971 * Description: Returns the 64-bit integer value from a given user
972 * space address. If the user space data is not accessible, an error
973 * will occur.
974 */
975 function user_int64_error:long (addr:long) %{ /* pure */ /* myproc-unprivileged */
976 STP_GET_USER(int64_t);
977 CATCH_DEREF_FAULT();
978 %}
979 %)
980
981 /**
982 * sfunction user_int64 - Retrieves a 64-bit integer value stored in user space
983 *
984 * @addr: the user space address to retrieve the 64-bit integer from
985 *
986 * Description: Returns the 64-bit integer value from a given user space
987 * address. Returns zero when user space data is not accessible.
988 */
989 function user_int64:long (addr:long) {
990 try { return user_int64_error(addr) } catch { return 0 }
991 }
992
993 %( runtime != "bpf" %?
994 /**
995 * sfunction user_uint64_error - Retrieves an unsigned 64-bit integer value stored in user space
996 *
997 * @addr: the user space address to retrieve the unsigned 64-bit integer from
998 *
999 * Description: Returns the unsigned 64-bit integer value from a given
1000 * user space address. If the user space data is not accessible, an
1001 * error will occur.
1002 */
1003 function user_uint64_error:long (addr:long) {
1004 /* NB: We have no script-level notion of 64-bit unsigned, but
1005 * we keep user_uint64_error anyway for API completeness. */
1006 return user_int64_error(addr)
1007 }
1008 %)
1009
1010 /**
1011 * sfunction user_uint64 - Retrieves an unsigned 64-bit integer value stored in user space
1012 *
1013 * @addr: the user space address to retrieve the unsigned 64-bit integer from
1014 *
1015 * Description: Returns the unsigned 64-bit integer value from a given user
1016 * space address. Returns zero when user space data is not accessible.
1017 */
1018 function user_uint64:long (addr:long) {
1019 /* NB: We have no script-level notion of 64-bit unsigned,
1020 * but we keep user_uint64 anyway for API completeness. */
1021 return user_int64(addr)
1022 }
1023
1024 /**
1025 * sfunction user_buffer_quoted - Retrieves and quotes buffer from user space
1026 *
1027 * @addr: the user space address to retrieve the buffer from
1028 * @inlen: the exact length of the buffer to read
1029 * @outlen: the maximum length of the output string
1030 *
1031 * Description: Reads inlen characters of a buffer from the given user space
1032 * memory address, and returns up to outlen characters, where any ASCII
1033 * characters that are not printable are replaced by the corresponding escape
1034 * sequence in the returned string. Note that the string will be surrounded by
1035 * double quotes. On the rare cases when user space data is not accessible at
1036 * the given address, the address itself is returned as a string, without
1037 * double quotes.
1038 */
1039 function user_buffer_quoted:string (addr:long, inlen:long, outlen:long)
1040 %{
1041 size_t outlen = (size_t)clamp_t(int, STAP_ARG_outlen, 0, MAXSTRINGLEN);
1042 if (outlen == 0)
1043 return;
1044 if ( _stp_text_str(STAP_RETVALUE, (char *)(uintptr_t)STAP_ARG_addr,
1045 STAP_ARG_inlen, outlen, 1, 1, 1) < 0)
1046 snprintf(STAP_RETVALUE, outlen, "0x%lx",
1047 (unsigned long)(void *)(uintptr_t)STAP_ARG_addr);
1048 %}
1049
1050 /**
1051 * sfunction user_buffer_quoted_error - Retrieves and quotes buffer from user space
1052 *
1053 * @addr: the user space address to retrieve the buffer from
1054 * @inlen: the exact length of the buffer to read
1055 * @outlen: the maximum length of the output string
1056 *
1057 * Description: Reads inlen characters of a buffer from the given user space
1058 * memory address, and returns up to outlen characters, where any ASCII
1059 * characters that are not printable are replaced by the corresponding escape
1060 * sequence in the returned string. Note that the string will be surrounded by
1061 * double quotes. On the rare cases when user space data is not accessible at
1062 * the given address, an error is thrown.
1063 */
1064 function user_buffer_quoted_error:string (addr:long, inlen:long, outlen:long)
1065 %{
1066 size_t outlen = (size_t)clamp_t(int, STAP_ARG_outlen, 0, MAXSTRINGLEN);
1067 if (outlen == 0
1068 || _stp_text_str(STAP_RETVALUE,
1069 (char *) (uintptr_t) STAP_ARG_addr,
1070 STAP_ARG_inlen, outlen, 1, 1, 1) < 0)
1071 STAP_ERROR("Unable to access user space data at 0x%lx",
1072 (unsigned long)(void *)(uintptr_t)STAP_ARG_addr);
1073 %}