Repository navigation
Expand file tree
/
Copy pathstrings.bt
More file actions
226 lines (206 loc) · 5.5 KB
/
Copy pathstrings.bt
File metadata and controls
226 lines (206 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// String helpers.
//
// This file contains various string-related functions and helpers. Strings are
// complex, especially C strings. In general, the complexities come from the
// need for dynamically-sized types or dynamic memory management. In bpftrace,
// we introduce an automatic parameterized `string` type in order to make this
// simpler, but it also makes it more complex. These string helpers are
// intended to operate uniformally on the wider class of "string-like" objects.
//
// This means that these functions should efficiently support:
// * Pointers to int8 (signed char)
// * Arrays of int8
// * Native bpftrace `string` objects of various sizes
// Checks that this value is string-like.
macro assert_str(exp)
{
if comptime (!is_str(exp)) {
if comptime is_array(exp) {
// wrap exp in parens to force it to be evaluated as an expression
static_assert(typeinfo((exp[0])) == typeinfo(int8),
"string-like arrays must contain a char-like type");
} else if comptime is_ptr(exp) {
static_assert(typeinfo((exp[0])) == typeinfo(int8),
"string-like pointers must point to a char-like type");
} else {
fail("string-like values must be either strings, arrays or pointers");
}
}
}
// Returns the default unbounded length.
macro default_str_length()
{
sizeof(str((int8*)0))
}
// Returns the length of a string-like object.
// :variant uint64 strlen(string exp)
// :variant uint64 strlen(int8 exp[])
// :variant uint64 strlen(int8 *exp)
macro strlen($var)
{
assert_str($var);
let $sz = (int64)0;
if comptime is_str($var) {
$sz = __strnlen(&$var, sizeof($var));
} else if comptime is_array($var) {
$sz = __strnlen($var, sizeof($var));
} else {
$sz = __strnlen($var, default_str_length());
}
(uint64)$sz
}
macro strlen(exp)
{
assert_str(exp);
if comptime (is_str(exp) || is_array(exp)) {
// This must be unbound, unfortunately we need to bind it.
let $x = exp;
strlen($x)
} else {
(uint64)__strnlen(exp, default_str_length())
}
}
macro __strnlen(ptr, max_size)
{
import "stdlib/strings/strings.bpf.c";
__bpf_strnlen((int8*)ptr, (size_t)max_size)
}
// Returns the "capacity" of a string-like object.
//
// In most cases this is the same as the length, but for bpftrace-native
// strings and arrays, this is the underlying object capacity. This is used to
// bound searches and lookups without needing to scan the string itself.
// :variant int64 strcap(string exp)
// :variant int64 strcap(int8 exp[])
// :variant int64 strcap(int8 *exp)
macro strcap(exp)
{
assert_str(exp);
if comptime is_str(exp) {
sizeof(exp)
} else if comptime is_array(exp) {
sizeof(exp) / sizeof((exp[0]))
} else {
strlen(exp) // Pointer, so pass by value.
}
}
// Returns the index of the first occurrence of the string needle in the string haystack. If needle is not in haystack then -1 is returned.
// :variant int64 strstr(string haystack, string needle)
macro strstr($haystack, $needle)
{
import "stdlib/strings/strings.bpf.c";
assert_str($haystack);
assert_str($needle);
let $haystack_size = strcap($haystack);
let $needle_size = strcap($needle);
if ($needle_size == 0) {
// Special case: the needle is zero-sized.
0
} else if ($needle[0] == 0) {
// Special case: the needle is empty.
0
} else {
__bpf_strnstr((int8*)(&$haystack),
(int8*)(&$needle), (uint64)$haystack_size, (uint64)$needle_size)
}
}
macro strstr($haystack, needle)
{
let $needle = needle;
strstr($haystack, $needle)
}
macro strstr(haystack, $needle)
{
let $haystack = haystack;
strstr($haystack, $needle)
}
macro strstr(haystack, needle)
{
let $haystack = haystack;
strstr($haystack, needle)
}
// Concatenate two strings into a new string.
// Returns the new string.
//
// :variant string str_concat(string s1, string s2)
macro str_concat($s1, $s2) {
import "stdlib/strings/strings.bpf.c";
let $new : string[1024] = "";
assert_str($s1);
assert_str($s2);
let $new_sz = strcap($new);
_ = __bpf_str_concat((int8 *)&$new, (uint64)$new_sz, (int8 *)&$s1);
_ = __bpf_str_concat((int8 *)&$new, (uint64)$new_sz, (int8 *)&$s2);
$new
}
macro str_concat($s1, s2) {
let $s2 = s2;
str_concat($s1, $s2)
}
macro str_concat(s1, $s2) {
let $s1 = s1;
str_concat($s1, $s2)
}
macro str_concat(s1, s2) {
let $s1 = s1;
str_concat($s1, s2)
}
// :variant bool strcontains(string haystack, string needle)
// Compares whether the string haystack contains the string needle.
//
// If needle is contained then true is returned, else false is returned.
macro strcontains(haystack, needle)
{
strstr(haystack, needle) >= 0
}
// :variant string strerror(int error)
//
// Convert errno code to string.
//
// ```
// #include <errno.h>
// begin {
// print(strerror(EPERM));
// }
// ```
macro strerror(errno)
{
import "stdlib/strings/strings.bpf.c";
let $v : string[64];
__strerror((int32)errno, &$v);
$v
}
// :variant string signal_name(int sig)
//
// Convert signal code to string.
//
// ```
// #include <signal.h>
// begin {
// print(signal_name(SIGINT));
// }
// ```
macro signal_name(sig)
{
import "stdlib/strings/strings.bpf.c";
let $v : string[64];
__signal_name((int32)sig, &$v);
$v
}
// :variant string syscall_name(int nr_syscall)
//
// Convert syscall number to string.
//
// ```
// #include <syscall.h>
// begin {
// print(syscall_name(__NR_read)); // outputs "read"
// }
// ```
macro syscall_name(n)
{
import "stdlib/strings/strings.bpf.c";
let $v : string[64];
__syscall_name((int32)n, &$v);
$v
}