Debian Codegraph @

tapset/bpf/conversions.stp

raw
1 // conversions tapset -- BPF version
2 // Copyright (C) 2018-2021 Red Hat Inc.
3 //
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
7 // later version.
8
9 /**
10 * sfunction kernel_string - Retrieves string from kernel memory
11 * @addr: The kernel address to retrieve the string from
12 *
13 * Description: This function returns the null terminated C string
14 * from a given kernel memory address. Reports an error on string
15 * copy fault.
16 */
17 function kernel_string:string (addr:long) {
18 return kernel_string_n(addr, 64 /*TODO: define BPF_MAXSTRINGLEN*/)
19 }
20
21 // TODO kernel_string:string(addr:long, err_msg:string)
22 // TODO kernel_string2:string(addr:long, err_msg:string)
23
24 /**
25 * sfunction kernel_string - Retrieves string from kernel memory with alternative error string
26 * @addr: The kernel address to retrieve the string from
27 * @err_msg: The error message to return when data isn't available
28 *
29 * Description: This function returns the null terminated C string
30 * from a given kernel memory address. Reports the given error message
31 * on string copy fault.
32 */
33 function kernel_string:string (addr:long, err_msg:string)
34 %{ /* bpf */ /* pure */
35 /* buf = bpf_stk_alloc(BPF_MAXSTRINGLEN);
36 buf[0] = 0x0; // guarantee NUL byte
37 rc = bpf_probe_read_str(buf, BPF_MAXSTRINGLEN, addr); */
38 alloc, $buf, BPF_MAXSTRINGLEN;
39 0x62, $buf, -, -, 0x0; /* stw [$buf+0], 0x0 -- guarantee NUL byte */
40 /* TODO PR25168: probe_read_str is used for both kernel and user memory.
41 BPF will soon deprecate these in favour of separate functions. */
42 call, $rc, probe_read_str, $buf, BPF_MAXSTRINGLEN, $addr;
43 /* call, $rc, probe_read_kernel_str, $buf, BPF_MAXSTRINGLEN, $addr; */
44
45 /* if (rc < 0) return err_msg;
46 return buf; */
47 0xc5, $rc, -, _err, 0; /* jslt $rc, 0, _err */
48 0xbf, $$, $buf, -, -; /* mov $$, $buf */
49 0x05, -, -, _done, -; /* ja _done; */
50
51 label, _err;
52 0xbf, $$, $err_msg, -, -; /* mov $$, $err_msg */
53
54 label, _done;
55 %}
56 function kernel_string2:string (addr:long, err_msg:string) {
57 return kernel_string(addr, err_msg);
58 }
59
60 // TODO kernel_string_quoted:string(addr:long) -- requires pseudo-loop to quote unprintable chars
61
62 /**
63 * sfunction kernel_string_n - Retrieves string of given length from kernel memory
64 * @addr: The kernel address to retrieve the string from
65 * @n: The maximum length of the string (if not null terminated)
66 *
67 * Description: Returns the C string of a maximum given length from a
68 * given kernel memory address. Reports an error on string copy fault.
69 */
70 function kernel_string_n:string (addr:long, n:long)
71 %{ /* bpf */ /* pure */
72 /* if (n > BPF_MAXSTRINGLEN) n = BPF_MAXSTRINGLEN; */
73 0xb5, $n, -, _skip, BPF_MAXSTRINGLEN; /* jle $n, BPF_MAXSTRINGLEN, _skip */
74 0xb7, $n, -, -, BPF_MAXSTRINGLEN; /* mov $n, BPF_MAXSTRINGLEN */
75
76 label, _skip;
77 /* buf = bpf_stk_alloc(BPF_MAXSTRINGLEN);
78 buf[0] = 0x0; // guarantee NUL byte
79 rc = bpf_probe_read_str(buf, n, addr); */
80 alloc, $buf, BPF_MAXSTRINGLEN;
81 0x62, $buf, -, -, 0x0; /* stw [buf+0], 0 -- guarantee NUL byte */
82 /* TODO PR25168: probe_read_str is used for both kernel and user memory.
83 BPF will soon deprecate these in favour of separate functions. */
84 call, $rc, probe_read_str, $buf, $n, $addr; /* TODO: should work if the helper is named bpf_probe_read_str() too */
85 /* call, $rc, probe_read_kernel_str, $buf, $n, $addr; */
86 /* TODO substitute probe_read_str automatically for older bpf? */
87
88 /* if (rc < 0) error("...", addr); */
89 0x75, $rc, -, _done, 0; /* jsge $rc, 0, _done */
90 call, -, printf, "ERROR: string copy fault at %p [man error::fault]", $addr; /* TODO document stapbpf version of error::fault */
91 call, -, exit;
92
93 label, _done;
94 /* return buf; */
95 0xbf, $$, $buf, -, -; /* mov $$, buf */
96 %}
97
98 // TODO kernel_string_utf32:string(addr:long)
99 // TODO kernel_string_utf32:string(addr:long,err_msg:string)
100 // TODO kernel_string2_utf32:string(addr:long,err_msg:string)
101 // TODO kernel_string_quoted_utf32:string(addr:long)
102
103 // TODO kernel_string_utf16:string(addr:long)
104 // TODO kernel_string_utf16:string(addr:long,err_msg:string)
105 // TODO kernel_string2_utf16:string(addr:long,err_msg:string)
106 // TODO kernel_string_quoted_utf16:string(addr:long)
107
108 // TODO kernel_long:long(addr:long)
109 // TODO kernel_int:long(addr:long)
110 // TODO kernel_short:long(addr:long)
111 // TODO kernel_char:long(addr:long)
112
113 // TODO kernel_pointer:long(addr:long)
114
115 // TODO kernel_buffer_quoted:string(addr:long,inlen:long)
116 // TODO kernel_buffer_quoted:string(addr:long,inlen:long,outlen:long)
117 // TODO kernel_buffer_quoted_error:string(addr:long,inlen:long,outlen:long)