Debian Codegraph @

tapset/string.stp

raw
1 // Standard string functions tapset.
2 // Copyright (C) 2009-2019 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 strlen - Returns the length of a string
11 *
12 * @s: the string
13 *
14 * Description: This function returns the length of the string, which
15 * can be zero up to MAXSTRINGLEN.
16 */
17 function strlen:long(s:string)
18 %{ /* pure */ /* unprivileged */ /* unmodified-fnargs */
19 STAP_RETURN(strlen(STAP_ARG_s));
20 %}
21
22 /**
23 * sfunction substr - Returns a substring
24 *
25 * @str: the string to take a substring from
26 * @start: starting position of the extracted string (first character is 0)
27 * @length: length of string to return
28 *
29 * Description: Returns the substring of the given string at the given
30 * start position with the given length (or smaller if the length of the
31 * original string is less than start + length, or length is bigger than
32 * MAXSTRINGLEN).
33 */
34 function substr:string(str:string,start:long, length:long)
35 %{ /* pure */ /* unprivileged */ /* unmodified-fnargs */
36 int64_t length = clamp_t(int64_t, STAP_ARG_length + 1, 0, MAXSTRINGLEN);
37 if (STAP_ARG_start >= 0 && STAP_ARG_start < strlen(STAP_ARG_str))
38 strlcpy(STAP_RETVALUE, STAP_ARG_str + STAP_ARG_start, length);
39 %}
40
41 /**
42 * sfunction stringat - Returns the char at a given position in the string
43 *
44 * @str: the string to fetch the character from
45 * @pos: the position to get the character from (first character is 0)
46 *
47 * Description: This function returns the character at a given position in
48 * the string or zero if the string doesn't have as many characters. Reports
49 * an error if pos is out of bounds.
50 */
51 function stringat:long(str:string, pos:long)
52 %{ /* pure */ /* unprivileged */ /* unmodified-fnargs */
53 if (STAP_ARG_pos >= 0 && STAP_ARG_pos < strlen(STAP_ARG_str))
54 STAP_RETURN(STAP_ARG_str[STAP_ARG_pos]);
55 else {
56 STAP_RETVALUE = 0;
57 #if STAP_COMPAT_VERSION >= STAP_VERSION(2,3) // PR15044
58 STAP_ERROR("Position out of bounds");
59 #endif
60 }
61 %}
62
63 /**
64 * sfunction isinstr - Returns whether a string is a substring of another string
65 *
66 * @s1: string to search in
67 * @s2: substring to find
68 *
69 * Description: This function returns 1 if string @s1 contains @s2,
70 * otherwise zero.
71 */
72 function isinstr:long(s1:string,s2:string)
73 %{ /* pure */ /* unprivileged */ /* unmodified-fnargs */
74 STAP_RETURN (strstr(STAP_ARG_s1,STAP_ARG_s2) != NULL);
75 %}
76
77 /**
78 * sfunction strpos - Returns location of a substring within another string
79 *
80 * @s1: string to search in
81 * @s2: substring to find
82 *
83 * Description: This function returns location of the first occurence of string
84 * @s2 within @s1, namely the return value is 0 in case @s2 is a prefix of @s1.
85 * If @s2 is not a substring of @s1, then the return value is -1.
86 */
87 function strpos:long(s1:string,s2:string)
88 %{ /* pure */ /* unprivileged */ /* unmodified-fnargs */
89 long pos = (long)strstr(STAP_ARG_s1,STAP_ARG_s2);
90 STAP_RETURN ((pos != 0) ? (pos - (long)STAP_ARG_s1) : (-1));
91 %}
92
93 /**
94 * sfunction text_str - Escape any non-printable chars in a string
95 *
96 * @input: the string to escape
97 *
98 * Description: This function accepts a string argument,
99 * and any ASCII characters that are not
100 * printable are replaced by the corresponding escape sequence in the
101 * returned string.
102 */
103 function text_str:string(input:string)
104 %( runtime != "bpf" %?
105 %{ /* pure */ /* unprivileged */ /* unmodified-fnargs */
106 if (_stp_text_str(STAP_RETVALUE, STAP_ARG_input, 0, 0, 0, 0, 0) < 0) {
107 STAP_RETVALUE[0] = '\0';
108 }
109 %}
110 %:
111 %{ /* bpf */
112 0xbf, 1, $input, -, -; /* mov r1, $input */
113 0x85, 0, 0, 0, -10; /* call BPF_FUNC_TEXT_STR */
114 0xbf, $$, 0, -, -; /* return r0 */
115 %}
116 %)
117
118 /**
119 * sfunction text_strn - Escape any non-printable chars in a string
120 *
121 * @input: the string to escape
122 * @len: maximum length of string to return (0 implies MAXSTRINGLEN)
123 * @quoted: put double quotes around the string. If input string is
124 * truncated it will have "..." after the second quote
125 *
126 * Description: This function accepts a string of designated length,
127 * and any ASCII characters that are not
128 * printable are replaced by the corresponding escape sequence in the
129 * returned string.
130 */
131 function text_strn:string(input:string, len:long, quoted:long)
132 %{ /* pure */ /* unprivileged */ /* unmodified-fnargs */
133 int64_t len = clamp_t(int64_t, STAP_ARG_len, 0, MAXSTRINGLEN);
134 if (_stp_text_str(
135 STAP_RETVALUE, STAP_ARG_input, 0, len, STAP_ARG_quoted, 0, 0) < 0) {
136 STAP_RETVALUE[0] = '\0';
137 }
138 %}
139
140
141 /**
142 * sfunction - str_replace Replaces all instances of a substring with another
143 *
144 * @prnt_str: the string to search and replace in
145 * @srch_str: the substring which is used to search in @prnt_str string
146 * @rplc_str: the substring which is used to replace @srch_str
147 *
148 * Description: This function returns the given string with
149 * substrings replaced.
150 */
151 function str_replace:string (prnt_str:string, srch_str:string, rplc_str:string)
152 %{ /* pure */ /* unprivileged */
153 char *ptr = (char *)(uintptr_t)STAP_ARG_prnt_str;
154 char *ptr_base = (char *)(uintptr_t)STAP_ARG_prnt_str;
155 int strlen_srch_str = strlen(STAP_ARG_srch_str);
156
157 STAP_RETVALUE[0] = '\0';
158 if(strlen_srch_str == 0) {
159 STAP_RETURN (ptr_base);
160 }
161
162 while((ptr = strstr(ptr, STAP_ARG_srch_str)) != NULL) {
163
164 *ptr = '\0';
165 strlcat(STAP_RETVALUE, ptr_base, MAXSTRINGLEN);
166 strlcat(STAP_RETVALUE, STAP_ARG_rplc_str, MAXSTRINGLEN);
167 ptr = ptr + strlen_srch_str;
168 ptr_base = ptr;
169 }
170
171 strlcat(STAP_RETVALUE, ptr_base, MAXSTRINGLEN);
172 return;
173 %}
174
175 /**
176 * sfunction - strtol - Convert a string to a long
177 *
178 * @str: string to convert
179 * @base: the base to use
180 *
181 * Description: This function converts the string representation of a number to an integer.
182 * The @base parameter indicates the number base to assume for the string (eg. 16 for hex, 8 for octal, 2 for binary).
183 */
184 function strtol:long(str:string, base:long)
185 %{ /* pure */ /* unprivileged */ /* unmodified-fnargs */
186 STAP_RETURN(simple_strtol(STAP_ARG_str, NULL, STAP_ARG_base));
187 %}
188
189 /**
190 * sfunction isdigit - Checks for a digit
191 *
192 * @str: string to check
193 *
194 * Description: Checks for a digit (0 through 9) as the first
195 * character of a string. Returns non-zero if true, and a zero if
196 * false.
197 */
198 function isdigit:long(str:string)
199 %{ /* pure */ /* unprivileged */ /* unmodified-fnargs */
200 STAP_RETURN(isdigit(STAP_ARG_str[0]));
201 %}
202
203
204 /**
205 * sfunction string_quoted - Quotes a given string
206 * @str: The kernel address to retrieve the string from
207 *
208 * Description: Returns the quoted string version of the given string,
209 * with characters where any ASCII characters that are not printable
210 * are replaced by the corresponding escape sequence in the returned
211 * string. Note that the string will be surrounded by double quotes.
212 */
213 function string_quoted:string (str:string)
214 %( runtime != "bpf" %?
215 %{ /* pure */ /* unmodified-fnargs */
216 /* This can't fail, since the input string is already in stap context space. */
217 (void) _stp_text_str(STAP_RETVALUE,
218 (char *)(uintptr_t)STAP_ARG_str,
219 MAXSTRINGLEN, MAXSTRINGLEN, 1, 0, 0);
220 %}
221 %:
222 %{ /* bpf */
223 0xbf, 1, $str, -, -; /* mov r1, $input */
224 0x85, 0, 0, 0, -11; /* call BPF_FUNC_STRING_QUOTED */
225 0xbf, $$, 0, -, -; /* return r0 */
226 %}
227 %)