File: conversions.stp

package info (click to toggle)
systemtap 5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,556 kB
  • sloc: cpp: 81,117; ansic: 54,933; xml: 49,795; exp: 43,595; sh: 11,526; python: 5,003; perl: 2,252; tcl: 1,312; makefile: 1,006; javascript: 149; lisp: 105; awk: 101; asm: 91; java: 70; sed: 16
file content (440 lines) | stat: -rw-r--r-- 17,273 bytes parent folder | download | duplicates (3)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// conversions tapset
// Copyright (C) 2005-2017 Red Hat Inc.
// Copyright (C) 2007 Intel Corporation.
//
// This file is part of systemtap, and is free software.  You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.

/**
 * sfunction kernel_string - Retrieves string from kernel memory
 * @addr: The kernel address to retrieve the string from
 *
 * Description: This function returns the null terminated C string
 * from a given kernel memory address. Reports an error on string
 * copy fault.
 */
function kernel_string:string (addr:long) %{ /* pure */
  __label__ deref_fault;
  char *destination = STAP_RETVALUE;
  kderef_string (destination, STAP_ARG_addr, MAXSTRINGLEN);
  if (0) {
deref_fault: /* branched to from deref_string() */
    /* Why '%1p' below? On newer kernels, the snprintf() function pads
     * out '(null)' to the same width as other pointers, which looks
     * really odd in the following error message. Setting a format
     * width of '1' fixes this. */
    snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
        "kernel string copy fault at 0x%lx [man error::fault]",
	(unsigned long) (uintptr_t) STAP_ARG_addr);
    CONTEXT->last_error = CONTEXT->error_buffer;
  }
%}

/**
 * sfunction kernel_string - Retrieves string from kernel memory with alternative error string
 * @addr: The kernel address to retrieve the string from
 * @err_msg: The error message to return when data isn't available
 *
 * Description: This function returns the null terminated C string
 * from a given kernel memory address. Reports the given error message
 * on string copy fault.
 */
function kernel_string:string (addr:long, err_msg:string) {
  try { return kernel_string(addr) } catch { return err_msg }
}
function kernel_string2:string (addr:long, err_msg:string) {
  return kernel_string(addr, err_msg);
}

/**
 * sfunction kernel_string_quoted - Retrieves and quotes string from kernel memory
 *
 * @addr: the kernel memory address to retrieve the string from
 *
 * Description: Returns the null terminated C string from a given kernel
 * memory address where any ASCII characters that are not printable are
 * replaced by the corresponding escape sequence in the returned string. Note
 * that the string will be surrounded by double quotes. If the kernel memory
 * data is not accessible at the given address, the address itself is returned
 * as a string, without double quotes.
 */
function kernel_string_quoted:string (addr:long)
%{ /* pure */
  int rc = 0;
  if (STAP_ARG_addr != 0)
    rc = _stp_text_str(STAP_RETVALUE,
                       (char *)(uintptr_t)STAP_ARG_addr,
                       MAXSTRINGLEN, MAXSTRINGLEN, 1, 0, 0);
  if (STAP_ARG_addr == 0 || rc < 0)
    snprintf(STAP_RETVALUE, MAXSTRINGLEN, "0x%lx",
		    (unsigned long)(uintptr_t)STAP_ARG_addr);
%}

/**
 * sfunction kernel_or_user_string_quoted - Retrieves and quotes string from kernel or user memory
 *
 * @addr: the kernel or user memory address to retrieve the string from
 *
 * Similar to kernel_string_quoted except user memory is a fallback method
 */
function kernel_or_user_string_quoted:string (addr:long) {
  try { return string_quoted(kernel_string(addr)) } catch { return string_quoted(user_string(addr)) }
}

/**
 * sfunction kernel_string_n - Retrieves string of given length from kernel memory
 * @addr: The kernel address to retrieve the string from
 * @n: The maximum length of the string (if not null terminated)
 *
 * Description: Returns the C string of a maximum given length from a
 * given kernel memory address. Reports an error on string copy fault.
 */
function kernel_string_n:string (addr:long, n:long) %{ /* pure */
  __label__ deref_fault;
  char *destination = STAP_RETVALUE;
  int64_t len = clamp_t(int64_t, STAP_ARG_n + 1, 1, MAXSTRINGLEN);
  kderef_string (destination, STAP_ARG_addr, len);
  if (0) {
deref_fault: /* branched to from deref_string() */
    snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
        "kernel string copy fault at 0x%lx [man error::fault]",
	(unsigned long) (uintptr_t) STAP_ARG_addr);
    CONTEXT->last_error = CONTEXT->error_buffer;
  }
%}

/**
 * sfunction kernel_string_utf32 - Retrieves UTF-32 string from kernel memory
 * @addr: The kernel address to retrieve the string from
 *
 * Description: This function returns a null terminated UTF-8 string converted
 * from the UTF-32 string at a given kernel memory address. Reports an error on
 * string copy fault or conversion error.
 */
function kernel_string_utf32:string (addr:long) %{ /* pure */
  __label__ deref_fault;
  int rc = 0, len = MAXSTRINGLEN;
  uint32_t c32, *source = (uint32_t*)(uintptr_t)STAP_ARG_addr;
  char *destination = STAP_RETVALUE;

  *destination = '\0';
  while (len > 1 && (c32 = kread(source))) {
    if ((rc = _stp_convert_utf32(destination, len, c32)) <= 0) {
      if (rc < 0) {
        snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
            "invalid UTF-32 character U+%X at 0x%lx", c32,
	    (unsigned long) (uintptr_t) source);
        CONTEXT->last_error = CONTEXT->error_buffer;
      }
      break;
    }
    ++source;
    destination += rc;
    len -= rc;
  }

  if (0) {
deref_fault: /* branched to from deref_string() */
    snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
              "kernel string copy fault at 0x%lx [man error::fault]",
	      (unsigned long) (uintptr_t) source);
    CONTEXT->last_error = CONTEXT->error_buffer;
  }
%}

/**
 * sfunction kernel_string_utf32 - Retrieves UTF-32 string from kernel memory with alternative error string
 * @addr: The kernel address to retrieve the string from
 * @err_msg: The error message to return when data isn't available
 *
 * Description: This function returns a null terminated UTF-8 string converted
 * from the UTF-32 string at a given kernel memory address. Reports the given
 * error message on string copy fault or conversion error.
 */
function kernel_string_utf32:string (addr:long, err_msg:string) {
  try { return kernel_string_utf32(addr) } catch { return err_msg }
}
function kernel_string2_utf32:string (addr:long, err_msg:string) {
  return kernel_string_utf32(addr, err_msg);
}

/**
 * sfunction kernel_string_quoted_utf32 - Quote given UTF-32 kernel string.
 * @addr: The kernel address to retrieve the string from
 *
 * Description: This function combines quoting as per @string_quoted
 * and UTF-32 decoding as per @kernel_string_utf32.
 */
function kernel_string_quoted_utf32:string (addr:long) {
  try { return string_quoted(kernel_string_utf32(addr)) } catch { return sprintf("0x%x", addr) }
}

/**
 * sfunction kernel_or_user_string_quoted_utf32 - Retrieves and quotes UTF-32 string from kernel or user memory
 *
 * @addr: the kernel or user memory address to retrieve the string from
 *
 * Similar to kernel_string_quoted_utf32 except user memory is a fallback method
 */
function kernel_or_user_string_quoted_utf32:string (addr:long) {
 try { return string_quoted(kernel_string_utf32(addr)) } catch { return string_quoted(user_string_utf32(addr)) }
}

/**
 * sfunction kernel_string_utf16 - Retrieves UTF-16 string from kernel memory
 * @addr: The kernel address to retrieve the string from
 *
 * Description: This function returns a null terminated UTF-8 string converted
 * from the UTF-16 string at a given kernel memory address. Reports an error on
 * string copy fault or conversion error.
 */
function kernel_string_utf16:string (addr:long) %{ /* pure */
  __label__ deref_fault;
  int rc = 0, len = MAXSTRINGLEN;
  uint32_t c32;
  uint16_t c16low, *source = (uint16_t*)(uintptr_t)STAP_ARG_addr;
  char *destination = STAP_RETVALUE;

  *destination = '\0';
  while (len > 1 && (c32 = kread(source))) {
    /* Check for a UTF-16 high surrogate, then its low pair, and combine them.
     * Broken surrogates will just fall through to _stp_convert_utf32 and get
     * flagged as an error there.  (Or even allowed, if we decide to be lax.)
     */
    if (c32 >= 0xD800 && c32 <= 0xDBFF) {
      ++source;
      c16low = kread(source);
      if (c16low >= 0xDC00 && c16low <= 0xDFFF)
        c32 = 0x10000 + ((c32 & 0x3FF) << 10) + (c16low & 0x3FF);
      else
        --source;
    }

    if ((rc = _stp_convert_utf32(destination, len, c32)) <= 0) {
      if (rc < 0) {
        snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
                  "invalid UTF-16 character U+%X at 0x%lx", c32,
		  (unsigned long) (uintptr_t) source);
        CONTEXT->last_error = CONTEXT->error_buffer;
      }
      break;
    }
    ++source;
    destination += rc;
    len -= rc;
  }

  if (0) {
deref_fault: /* branched to from deref_string() */
    snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
              "kernel string copy fault at 0x%lx [man error::fault]",
	      (unsigned long) (uintptr_t) source);
    CONTEXT->last_error = CONTEXT->error_buffer;
  }
%}

/**
 * sfunction kernel_string_utf16 - Retrieves UTF-16 string from kernel memory with alternative error string
 * @addr: The kernel address to retrieve the string from
 * @err_msg: The error message to return when data isn't available
 *
 * Description: This function returns a null terminated UTF-8 string converted
 * from the UTF-16 string at a given kernel memory address. Reports the given
 * error message on string copy fault or conversion error.
 */
function kernel_string_utf16:string (addr:long, err_msg:string) {
  try { return kernel_string_utf16(addr) } catch { return err_msg }
}
function kernel_string2_utf16:string (addr:long, err_msg:string) {
  return kernel_string_utf16(addr, err_msg);
}


/**
 * sfunction kernel_string_quoted_utf16 - Quote given kernel UTF-16 string.
 * @addr: The kernel address to retrieve the string from
 *
 * Description: This function combines quoting as per @string_quoted
 * and UTF-16 decoding as per @kernel_string_utf16.
 */
function kernel_string_quoted_utf16:string (addr:long) {
  try { return string_quoted(kernel_string_utf16(addr)) } catch { return sprintf("0x%x", addr) }
}

/**
 * sfunction kernel_or_user_string_quoted_utf16 - Retrieves and quotes UTF-16 string from kernel or user memory
 *
 * @addr: the kernel or user memory address to retrieve the string from
 *
 * Similar to kernel_string_quoted_utf16 except uses user memory as a fallback method
 */
function kernel_or_user_string_quoted_utf16:string (addr:long) {
 try { return string_quoted(kernel_string_utf16(addr)) } catch { return string_quoted(user_string_utf16(addr)) }
}


/**
 * sfunction kernel_long - Retrieves a long value stored in kernel memory
 * @addr: The kernel address to retrieve the long from
 *
 * Description: Returns the long value from a given kernel memory address.
 * Reports an error when reading from the given address fails.
 */
function kernel_long:long (addr:long) %{ /* pure */
  __label__ deref_fault;
  STAP_RETVALUE = kread((long *) (uintptr_t) STAP_ARG_addr);
  if (0) {
deref_fault: /* branched to from kread() */
    snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
        "kernel long copy fault at 0x%lx [man error::fault]",
	(unsigned long) (uintptr_t) STAP_ARG_addr);
    CONTEXT->last_error = CONTEXT->error_buffer;
  }
%}

/**
 * sfunction kernel_int - Retrieves an int value stored in kernel memory
 * @addr: The kernel address to retrieve the int from
 *
 * Description: Returns the int value from a given kernel memory address.
 * Reports an error when reading from the given address fails.
 */
function kernel_int:long (addr:long) %{ /* pure */
  __label__ deref_fault;
  STAP_RETVALUE = kread((int *) (uintptr_t) STAP_ARG_addr);
  if (0) {
deref_fault: /* branched to from kread() */
    snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
        "kernel int copy fault at 0x%lx [man error::fault]",
	(unsigned long) (uintptr_t) STAP_ARG_addr);
    CONTEXT->last_error = CONTEXT->error_buffer;
  }
%}

/**
 * sfunction kernel_short - Retrieves a short value stored in kernel memory
 * @addr: The kernel address to retrieve the short from
 *
 * Description: Returns the short value from a given kernel memory address.
 * Reports an error when reading from the given address fails.
 */
function kernel_short:long (addr:long) %{ /* pure */
  __label__ deref_fault;
  STAP_RETVALUE = kread((short *) (uintptr_t) STAP_ARG_addr);
  if (0) {
deref_fault: /* branched to from kread() */
    snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
        "kernel short copy fault at 0x%lx [man error::fault]",
	(unsigned long) (uintptr_t) STAP_ARG_addr);
    CONTEXT->last_error = CONTEXT->error_buffer;
  }
%}

/**
 * sfunction kernel_char - Retrieves a char value stored in kernel memory
 * @addr: The kernel address to retrieve the char from
 *
 * Description: Returns the char value from a given kernel memory address.
 * Reports an error when reading from the given address fails.
 */
function kernel_char:long (addr:long) %{ /* pure */
  __label__ deref_fault;
  STAP_RETVALUE = kread((char *) (uintptr_t) STAP_ARG_addr);
  if (0) {
deref_fault: /* branched to from kread() */
    snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
        "kernel char copy fault at 0x%lx [man error::fault]",
	(unsigned long) (uintptr_t) STAP_ARG_addr);
    CONTEXT->last_error = CONTEXT->error_buffer;
  }
%}

/**
 * sfunction kernel_pointer - Retrieves a pointer value stored in kernel memory
 * @addr: The kernel address to retrieve the pointer from
 *
 * Description: Returns the pointer value from a given kernel memory
 * address. Reports an error when reading from the given address
 * fails.
 */
function kernel_pointer:long (addr:long) %{ /* pure */
  __label__ deref_fault;
  STAP_RETVALUE = (uintptr_t) kread((void **) (uintptr_t) STAP_ARG_addr);
  if (0) {
deref_fault: /* branched to from kread() */
    snprintf (CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
        "kernel pointer copy fault at 0x%lx [man error::fault]",
	(unsigned long) (uintptr_t) STAP_ARG_addr);
    CONTEXT->last_error = CONTEXT->error_buffer;
  }
%}

/**
 * sfunction kernel_buffer_quoted - Retrieves and quotes buffer from kernel space
 *
 * @addr: the kernel space address to retrieve the buffer from
 * @inlen: the exact length of the buffer to read
 *
 * Description: Reads inlen characters of a buffer from the given kernel space
 * memory address, and returns up to MAXSTRINGLEN characters, where any ASCII
 * characters that are not printable are replaced by the corresponding escape
 * sequence in the returned string. Note that the string will be surrounded by
 * double quotes. On the rare cases when kernel space data is not accessible at
 * the given address, the address itself is returned as a string, without
 * double quotes.
 */
function kernel_buffer_quoted:string (addr:long, inlen:long) {
  return kernel_buffer_quoted(addr, inlen, %{ /* pure */ MAXSTRINGLEN%})
}

/**
 * sfunction kernel_buffer_quoted - Retrieves and quotes buffer from kernel space
 *
 * @addr: the kernel space address to retrieve the buffer from
 * @inlen: the exact length of the buffer to read
 * @outlen: the maximum length of the output string
 *
 * Description: Reads inlen characters of a buffer from the given kernel space
 * memory address, and returns up to outlen characters, where any ASCII
 * characters that are not printable are replaced by the corresponding escape
 * sequence in the returned string. Note that the string will be surrounded by
 * double quotes. On the rare cases when kernel space data is not accessible at
 * the given address, the address itself is returned as a string, without
 * double quotes.
 */
function kernel_buffer_quoted:string (addr:long, inlen:long, outlen:long)
%{ /* pure */
  size_t outlen = (size_t)clamp_t(int, STAP_ARG_outlen, 0, MAXSTRINGLEN);
  if (outlen == 0)
    return;
  if ( _stp_text_str(STAP_RETVALUE, (char *)(uintptr_t)STAP_ARG_addr,
		     STAP_ARG_inlen, outlen, 1, 0, 1) < 0)
    snprintf(STAP_RETVALUE, outlen, "0x%lx",
	     (unsigned long)(void *)(uintptr_t)STAP_ARG_addr);
%}

/**
 * sfunction kernel_buffer_quoted_error - Retrieves and quotes buffer from kernel space
 *
 * @addr: the kernel space address to retrieve the buffer from
 * @inlen: the exact length of the buffer to read
 * @outlen: the maximum length of the output string
 *
 * Description: Reads inlen characters of a buffer from the given kernel space
 * memory address, and returns up to outlen characters, where any ASCII
 * characters that are not printable are replaced by the corresponding escape
 * sequence in the returned string. Note that the string will be surrounded by
 * double quotes. On the rare cases when kernel space data is not accessible at
 * the given address, an error is thrown.
 */
function kernel_buffer_quoted_error:string (addr:long, inlen:long, outlen:long)
%{ /* pure */
  size_t outlen = (size_t)clamp_t(int, STAP_ARG_outlen, 0, MAXSTRINGLEN);
  if (outlen == 0
      || _stp_text_str(STAP_RETVALUE, (char *) (uintptr_t) STAP_ARG_addr,
		       STAP_ARG_inlen, outlen, 1, 0, 1) < 0)
    STAP_ERROR("Unable to access kernel space data at 0x%lx",
	       (unsigned long)(void *)(uintptr_t)STAP_ARG_addr);
%}