Debian Codegraph @

tapset/python3.stp

raw
1 // python 3 tapset
2 // Copyright (C) 2016 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 // Here we store to address of 'PyObject_GenericGetAttr', which we use
10 // down in Py3Object_GetAttr().
11 private global Py3Object_GenericGetAttr_addr = 0
12
13 %{
14 /*
15 * Defines borrowed from object.h.
16 */
17
18 /* These flags are used to determine if a type is a subclass. */
19 #define Py3_TPFLAGS_LONG_SUBCLASS (1L<<24)
20 #define Py3_TPFLAGS_LIST_SUBCLASS (1L<<25)
21 #define Py3_TPFLAGS_TUPLE_SUBCLASS (1L<<26)
22 #define Py3_TPFLAGS_BYTES_SUBCLASS (1L<<27)
23 #define Py3_TPFLAGS_UNICODE_SUBCLASS (1L<<28)
24 #define Py3_TPFLAGS_DICT_SUBCLASS (1L<<29)
25 #define Py3_TPFLAGS_BASE_EXC_SUBCLASS (1L<<30)
26 #define Py3_TPFLAGS_TYPE_SUBCLASS (1L<<31)
27
28 /*
29 * Defines borrowed from code.h.
30 */
31
32 /* Masks for co_flags above */
33 #define Py3_CO_OPTIMIZED 0x0001
34 #define Py3_CO_NEWLOCALS 0x0002
35 #define Py3_CO_VARARGS 0x0004
36 #define Py3_CO_VARKEYWORDS 0x0008
37 #define Py3_CO_NESTED 0x0010
38 #define Py3_CO_GENERATOR 0x0020
39 /* The Py3_CO_NOFREE flag is set if there are no free or cell variables.
40 This information is redundant, but it allows a single flag test
41 to determine whether there is any extra work to be done when the
42 call frame it setup.
43 */
44 #define Py3_CO_NOFREE 0x0040
45
46 /*
47 * Defines borrowed from longintrepr.h
48 */
49 #define Py3Long_SHIFT_SMALL 15
50 #define Py3Long_SHIFT_BIG 30
51
52 /*
53 * Defines borrowed from pycore_dict.h
54 */
55 #define DICT_KEYS_GENERAL 0
56 #define DICT_KEYS_UNICODE 1
57 %}
58
59 #
60 # Macros to cast to various python 3 types
61 # PR25841 makes this easy
62 #
63
64 @define Py3Object(object) %(
65 @cast(@object, "PyObject")
66 %)
67 @define Py3VarObject(object) %(
68 @cast(@object, "PyVarObject")
69 %)
70 @define Py3LongObject(object) %(
71 @cast(@object, "PyLongObject")
72 %)
73 @define Py3BytesObject(object) %(
74 @cast(@object, "PyBytesObject")
75 %)
76 @define Py3UnicodeObject(object) %(
77 @cast(@object, "PyUnicodeObject")
78 %)
79 @define Py3TypeObject(object) %(
80 @cast(@object, "PyTypeObject")
81 %)
82 @define Py3TupleObject(object) %(
83 @cast(@object, "PyTupleObject")
84 %)
85 @define Py3ListObject(object) %(
86 @cast(@object, "PyListObject")
87 %)
88 @define Py3SetObject(object) %(
89 @cast(@object, "PySetObject")
90 %)
91 @define Py3DictObject(object) %(
92 @cast(@object, "PyDictObject")
93 %)
94 @define Py3DictEntry(object) %(
95 @cast(@object, "PyDictEntry")
96 %)
97 @define Py3DictKeysObject(object) %(
98 @cast(@object, "PyDictKeysObject")
99 %)
100 @define Py3DictKeyEntry(object) %(
101 @cast(@object, "PyDictKeyEntry")
102 %)
103 @define Py3DictUnicodeEntry(object) %(
104 @cast(@object, "PyDictUnicodeEntry")
105 %)
106 @define Py3FrameObject (object) %(
107 @cast(@object, "PyFrameObject")
108 %)
109 @define Py3CodeObject(object) %(
110 @cast(@object, "PyCodeObject")
111 %)
112
113 @define Py3ASCIIObject(object) %(
114 @cast(@object, "PyASCIIObject")
115 %)
116 @define Py3ASCIIObject_Sizeof %(
117 @cast_sizeof("PyASCIIObject")
118 %)
119
120 @define Py3CompactUnicodeObject(object) %(
121 @cast(@object, "PyCompactUnicodeObject")
122 %)
123 @define Py3CompactUnicodeObject_Sizeof %(
124 @cast_sizeof("PyCompactUnicodeObject")
125 %)
126
127 private function Py3Frame_GetCode (object)
128 {
129 return @choose_defined(@Py3FrameObject(object)->f_code,
130 @cast(object, "_stp_Py3FrameObject")->f_frame->f_code)
131 }
132
133 private function Py3Frame_GetTrace (object)
134 {
135 return @choose_defined(
136 @Py3FrameObject(object)->f_trace,
137 @cast(object, "_stp_Py3FrameObject")->f_trace)
138 }
139
140 private function Py3Frame_GetBack (object)
141 {
142 if (@defined (@Py3FrameObject(object)->f_back))
143 return @Py3FrameObject(object)->f_back
144 else
145 {
146 f_frame = @cast(object, "_stp_Py3FrameObject")->f_frame
147 back = @cast(object, "_stp_Py3FrameObject")->f_back;
148 if (back == NULL && f_frame->previous != NULL) {
149 back = f_frame->previous->frame_obj;
150 }
151 return back;
152 }
153 }
154
155 private function Py3Frame_GetFilename (object)
156 {
157 return @choose_defined(@Py3FrameObject(object)->f_code->co_filename,
158 @cast(object, "_stp_Py3FrameObject")->f_frame->f_code->co_filename)
159 }
160
161 private function Py3Frame_GetLocalsplus (object)
162 {
163 return @choose_defined(
164 @Py3FrameObject(object)->f_localsplus,
165 @cast(object, "_stp_Py3FrameObject")->f_frame->localsplus)
166 }
167
168 #
169 # TODO python 3.11 sometimes returns <NULL> for global values (timing issue?)
170 #
171 private function Py3Frame_GetGlobals (object)
172 {
173 return @choose_defined(
174 @Py3FrameObject(object)->f_globals,
175 @cast(object, "_stp_Py3FrameObject")->f_frame->f_globals)
176 }
177
178 private function Py3Dict_GetValue (object, idx)
179 {
180 return @choose_defined(
181 @Py3DictObject(object)->ma_values->values[idx],
182 @Py3DictObject(object)->ma_values[idx])
183 }
184
185 #
186 # Systemtap macros based on C macros in object.h.
187 #
188
189 @define Py3_REFCNT(object) %(
190 @Py3Object(@object)->ob_refcnt
191 %)
192 @define Py3_TYPE(object) %(
193 @Py3Object(@object)->ob_type
194 %)
195 @define Py3_SIZE(object) %(
196 @Py3VarObject(@object)->ob_size
197 %)
198
199 @define Py3Type_HasFeature(t, f) %(
200 ((@t->tp_flags & (@f)) != 0)
201 %)
202 @define Py3Type_FastSubclass(t, f) %(
203 @Py3Type_HasFeature(@t, @f)
204 %)
205
206 #
207 # Systemtap macros based on C macros in bytesobject.h.
208 #
209
210 @define Py3Bytes_Check(op) %(
211 @Py3Type_FastSubclass(@Py3_TYPE(@op), %{ Py3_TPFLAGS_BYTES_SUBCLASS %})
212 %)
213
214 #
215 # Systemtap macros based on C macros in unicodeobject.h.
216 #
217
218 @define Py3Unicode_Check(op) %(
219 @Py3Type_FastSubclass(@Py3_TYPE(@op), %{ Py3_TPFLAGS_UNICODE_SUBCLASS %})
220 %)
221
222 /* Returns the length of the unicode string. */
223 @define Py3Unicode_GET_LENGTH(op) %(
224 ((@Py3Unicode_Check(@op) && @Py3Unicode_IS_READY(@op))
225 ? @Py3ASCIIObject(@op)->length : 0)
226 %)
227
228 @define Py3Unicode_IS_READY(op) %(
229 @Py3ASCIIObject(@op)->state->ready
230 %)
231
232 @define Py3Unicode_IS_ASCII(op) %(
233 (@Py3Unicode_Check(@op) && @Py3Unicode_IS_READY(@op)
234 && @Py3ASCIIObject(@op)->state->ascii)
235 %)
236
237 /* Return true if the string is compact or 0 if not.
238 No type checks or Ready calls are performed. */
239 @define Py3Unicode_IS_COMPACT(op) %(
240 (@Py3ASCIIObject(@op)->state->compact)
241 %)
242
243 /* Return a void pointer to the raw unicode buffer. */
244 @define _Py3Unicode_COMPACT_DATA(op) %(
245 (@Py3Unicode_IS_ASCII(@op)
246 ? (@op + @Py3ASCIIObject_Sizeof)
247 : (@op + @Py3CompactUnicodeObject_Sizeof))
248 %)
249 @define _Py3Unicode_NONCOMPACT_DATA(op) %(
250 (@Py3UnicodeObject(@op)->data->any)
251 %)
252 @define Py3Unicode_DATA(op) %(
253 (@Py3Unicode_IS_COMPACT(@op) ? @_Py3Unicode_COMPACT_DATA(@op)
254 : @_Py3Unicode_NONCOMPACT_DATA(@op))
255 %)
256
257 #
258 # Systemtap macros based on C macros in longobject.h.
259 #
260
261 @define Py3Long_Check(op) %(
262 @Py3Type_FastSubclass(@Py3_TYPE(@op), %{ Py3_TPFLAGS_LONG_SUBCLASS %})
263 %)
264
265 #
266 # Systemtap macros based on C macros in dictobject.h.
267 #
268
269 @define Py3Dict_Check(op) %(
270 @Py3Type_FastSubclass(@Py3_TYPE(@op), %{ Py3_TPFLAGS_DICT_SUBCLASS %})
271 %)
272
273 #
274 # Systemtap macros based on C macros in tupleobject.h.
275 #
276
277 @define Py3Tuple_Check(op) %(
278 @Py3Type_FastSubclass(@Py3_TYPE(@op), %{ Py3_TPFLAGS_TUPLE_SUBCLASS %})
279 %)
280
281 #
282 # Systemtap macros based on C macros in listobject.h.
283 #
284
285 @define Py3List_Check(op) %(
286 @Py3Type_FastSubclass(@Py3_TYPE(@op), %{ Py3_TPFLAGS_LIST_SUBCLASS %})
287 %)
288
289 #
290 # Systemtap functions based on C functions in longobject.c.
291 #
292
293 %{
294 /* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
295 * anything about what happens when a signed integer operation overflows,
296 * and some compilers think they're doing you a favor by being "clever"
297 * then. The bit pattern for the largest positive signed long is
298 * (unsigned long)LONG_MAX, and for the smallest negative signed long
299 * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
300 * However, some other compilers warn about applying unary minus to an
301 * unsigned operand. Hence the weird "0-".
302 */
303
304 #define PY3_LLONG_MIN (-__LONG_LONG_MAX__ - 1L)
305 #define PY3_ABS_LLONG_MIN (0-(unsigned long long)PY3_LLONG_MIN)
306 %}
307
308 /* Get a C long int from an int object or any object that has an __int__
309 * method.
310 *
311 * On overflow, return -1 and throw an error.
312 */
313
314 /*
315 * The Py3Long_AsLongLong() function below is actually based on
316 * PyLong_AsLong(). Python's PyLong_AsLongLong() converts the PyLong
317 * to a Bytes object. This method is simpler.
318 *
319 * On 32-bit platforms, the default for PYLONG_BITS_IN_DIGIT is 15. On
320 * 64-bit platforms, the default for PYLONG_BITS_IN_DIGIT is 30. We're
321 * going to assume that to be true, even though it can be configured
322 * differently.
323 *
324 * Since we can be on a 64-bit kernel but probing a 32-bit python 3
325 * executable, we have to handle both cases at the same time.
326 */
327
328 private function Py3Long_AsLongLongAndOverflow_Small:long(v:long)
329 {
330 res = -1
331 i = @Py3_SIZE(v)
332 if (i == -1) {
333 /* We're using 'x & 0xffff' instead of '(short)x' */
334 res = -(@Py3LongObject(v)->ob_digit[0] & 0xffff)
335 }
336 else if (i == 0) {
337 res = 0
338 }
339 else if (i == 1) {
340 res = @Py3LongObject(v)->ob_digit[0]
341 }
342 else {
343 sign = 1
344 x = 0
345 if (i < 0) {
346 sign = -1
347 i = -(i)
348 }
349 while (--i >= 0) {
350 prev = x
351 x = ((x << %{ Py3Long_SHIFT_SMALL %})
352 | @Py3LongObject(v)->ob_digit[i])
353 if ((x >> %{ Py3Long_SHIFT_SMALL %}) != prev) {
354 error("Python int too large")
355 return res
356 }
357 }
358 /* Haven't lost any bits, but casting to long requires extra
359 * care (see comment above).
360 */
361 if (x <= %{ __LONG_LONG_MAX__ %}) {
362 res = x * sign
363 }
364 else if (sign < 0 && x == %{ PY3_ABS_LLONG_MIN %}) {
365 res = %{ PY3_LLONG_MIN %}
366 }
367 else {
368 error("Python int too large")
369 /* res is already set to -1 */
370 }
371 }
372 return res
373 }
374
375 private function Py3Long_AsLongLongAndOverflow_Big:long(v:long)
376 {
377 res = -1
378 i = @Py3_SIZE(v)
379 if (i == -1) {
380 /* We're using 'x & 0xffffffff' instead of '(int32_t)x' */
381 res = -(@Py3LongObject(v)->ob_digit[0] & 0xffffffff)
382 }
383 else if (i == 0) {
384 res = 0
385 }
386 else if (i == 1) {
387 res = @Py3LongObject(v)->ob_digit[0]
388 }
389 else {
390 sign = 1
391 x = 0
392 if (i < 0) {
393 sign = -1
394 i = -(i)
395 }
396 while (--i >= 0) {
397 prev = x
398 x = ((x << %{ Py3Long_SHIFT_BIG %})
399 | @Py3LongObject(v)->ob_digit[i])
400 if ((x >> %{ Py3Long_SHIFT_BIG %}) != prev) {
401 error("Python int too large")
402 return res
403 }
404 }
405 /* Haven't lost any bits, but casting to long requires extra
406 * care (see comment above).
407 */
408 if (x <= %{ __LONG_LONG_MAX__ %}) {
409 res = x * sign
410 }
411 else if (sign < 0 && x == %{ PY3_ABS_LLONG_MIN %}) {
412 res = %{ PY3_LLONG_MIN %}
413 }
414 else {
415 error("Python int too large")
416 /* res is already set to -1 */
417 }
418 }
419 return res
420 }
421
422 private function Py3Long_AsLongLong:long(v:long)
423 {
424 if (! @Py3Long_Check(v)) {
425 error(sprintf("Py3Long_AsLong called on %s object at %p",
426 python3_get_typename(v), v))
427 return -1
428 }
429
430 %( CONFIG_64BIT == "y" %?
431 %( CONFIG_COMPAT == "y" %?
432 if (@__compat_task) {
433 return Py3Long_AsLongLongAndOverflow_Small(v)
434 }
435 %)
436 return Py3Long_AsLongLongAndOverflow_Big(v)
437 %:
438 return Py3Long_AsLongLongAndOverflow_Small(v)
439 %)
440 }
441
442 #
443 # Systemtap functions based on C functions in bytesobject.c.
444 #
445
446 private function Py3Bytes_Size:long(object:long)
447 {
448 if (! @Py3Bytes_Check(object)) {
449 # NB: python code returns string_getsize(), creating a new
450 # string representation of the object, then returning the
451 # length of the new string. We can't create new objects here,
452 # so we'll just quit.
453 warn(sprintf("Py3Bytes_Size called on non-bytes object address 0x%p\n",
454 object))
455 return 0
456 }
457 return @Py3_SIZE(object)
458 }
459
460 #
461 # Systemtap functions based on C functions in unicodeobject.c.
462 #
463
464 private function Py3Unicode_AsASCIIString:string(object:long)
465 {
466 if (!@Py3Unicode_Check(object)) {
467 # NB: python returns value of string_getbuffer() here
468 # (basically getting a string representation of any
469 # object). We can't create new objects here, so we'll
470 # just quit.
471 warn(sprintf("Py3Unicode_AsASCIIString called on non-string address 0x%p\n",
472 object))
473 return ""
474 }
475 if (@Py3Unicode_IS_ASCII(object)) {
476 try {
477 return user_string_n_nofault(@Py3Unicode_DATA(object),
478 @Py3Unicode_GET_LENGTH(object))
479 } catch {
480 warn(sprintf("Py3Unicode_AsASCIIString failed on address 0x%p\n",
481 object))
482 }
483 return ""
484 }
485 try {
486 return text_str(user_string_n(@Py3Unicode_DATA(object),
487 @Py3Unicode_GET_LENGTH(object)))
488 } catch {
489 warn(sprintf("Py3Unicode_AsASCIIString failed(2) on address 0x%p\n",
490 @Py3Unicode_DATA(object)))
491 }
492 return ""
493 }
494
495 #
496 # Systemtap functions based on C functions in codeobject.c.
497 #
498
499 private function Py3Code_lnotab (object)
500 {
501 return @choose_defined(@Py3CodeObject(object)->co_lnotab,
502 @choose_defined(@Py3CodeObject(object)->co_linetable,
503 @cast(object, "_stp_Py3FrameObject")->f_frame->f_code->co_linetable))
504 }
505
506 private function Py3Code_firstlineno (object)
507 {
508 return @choose_defined(@Py3CodeObject(object)->co_firstlineno,
509 @cast(object, "_stp_Py3FrameObject")->f_frame->f_code->co_firstlineno)
510 }
511
512 private function Py3Code_nlocals (object)
513 {
514 return @choose_defined(@Py3CodeObject(object)->co_nlocals,
515 @cast(object, "_stp_Py3FrameObject")->f_frame->f_code->co_nlocals)
516 }
517
518 private function Py3Code_Addr2Line:long(code:long, addrq:long)
519 {
520 /*
521 * co_lnotab is used to compute the line number from a bytecode
522 * index, addrq. See the file Objects/lnotab_notes.txt in the
523 * python source for the details of the lnotab representation.
524 *
525 * We can't treat co_lnotab as a "real" null terminated string,
526 * since co_lnotab can have embedded null characters. So, we'll
527 * grab it character by character.
528 */
529 size = Py3Bytes_Size(Py3Code_lnotab (code)) / 2
530 co_lnotab_sval = @Py3BytesObject(Py3Code_lnotab (code))->ob_sval
531 line = Py3Code_firstlineno(code)
532 addr = 0
533 p = 0
534 while (--size >= 0) {
535 addr += user_char(co_lnotab_sval + p++)
536 if (addr > addrq)
537 break
538
539 line += user_char(co_lnotab_sval + p++)
540 }
541 return line
542 }
543
544 private function Py310Code_Addr2Line:long(code:long, addrq:long)
545 {
546 /*
547 * co_lnotab is used to compute the line number from a bytecode
548 * index, addrq. See the file Objects/lnotab_notes.txt in the
549 * python source for the details of the lnotab representation
550 * for <= python 3.10, for >= python 3.11 see Objects/locations.md
551 *
552 * We can't treat co_lnotab as a "real" null terminated string,
553 * since co_lnotab can have embedded null characters. So, we'll
554 * grab it character by character.
555 *
556 */
557 size = Py3Bytes_Size(Py3Code_lnotab (code)) / 2
558 co_lnotab_sval = @Py3BytesObject(Py3Code_lnotab (code))->ob_sval
559 line = Py3Code_firstlineno(code)
560 idx = 0
561 while (idx < 52) {
562 addr = user_long(co_lnotab_sval+idx)
563 idx = idx + 8
564 }
565 while (--size >= 0) {
566 addr += user_char(co_lnotab_sval)
567 co_lnotab_sval = co_lnotab_sval + 1
568 if (addr > addrq)
569 break
570
571 line += user_char(co_lnotab_sval)
572 co_lnotab_sval = co_lnotab_sval + 1
573 }
574 return line
575 }
576
577 #
578 # Systemtap functions based on C functions in frameobject.c.
579 #
580
581 private function Py3Frame_GetLineNumber (frame)
582 {
583 if (! Py3Frame_GetTrace (frame)) {
584 lineno = @choose_defined(@Py3FrameObject(frame)->f_lineno,
585 @cast(frame, "_stp_Py3FrameObject")->f_lineno)
586 } else {
587 lasti = @choose_defined(@Py3FrameObject(frame)->f_lasti,
588 @cast(frame, "_stp_Py3FrameObject")->f_frame->prev_instr - @cast(frame, "_stp_Py3FrameObject")->f_frame->f_code)
589 if (@defined(@Py3FrameObject(frame)->f_lasti))
590 lineno = Py3Code_Addr2Line(Py3Frame_GetCode(frame), lasti)
591 else
592 lineno = Py310Code_Addr2Line(Py3Frame_GetCode(frame), lasti)
593 }
594 return lineno
595 }
596
597 #
598 # Systemtap functions based on C functions in tupleobject.c.
599 #
600
601 private function Py3Tuple_GetItem:long(op:long, i:long)
602 {
603 if (!@Py3Tuple_Check(op)) {
604 error(sprintf("Py3Tuple_GetItem called on %s object at %p",
605 python3_get_typename(op), op))
606 return 0
607 }
608 if (i < 0 || i >= @Py3_SIZE(op)) {
609 error(sprintf("tuple index %d out of range (%d)", i, @Py3_SIZE(op)))
610 return 0
611 }
612 return @Py3TupleObject(op)->ob_item[i]
613 }
614
615 private function Py3Tuple_Repr:string(object:long)
616 {
617 if (!@Py3Tuple_Check(object)) {
618 error(sprintf("Py3Tuple_Repr called on %s object at %p",
619 python3_get_typename(object), object))
620 return ""
621 }
622 n = @Py3_SIZE(object)
623 if (n == 0)
624 return "()"
625
626 retstr = "("
627 first = 1
628 for (i = 0; i < n; i++) {
629 if (! first)
630 retstr .= " "
631 first = 0
632 retstr .= Py3Object_Repr(Py3Tuple_GetItem(object, i))
633 }
634 retstr .= ")"
635 return retstr
636 }
637
638 #
639 # Systemtap functions based on C functions in listobject.c.
640 #
641
642 private function Py3List_GetItem:long(op:long, i:long)
643 {
644 if (!@Py3List_Check(op)) {
645 error(sprintf("Py3List_GetItem called on %s object at %p",
646 python3_get_typename(op), op))
647 return 0
648 }
649 if (i < 0 || i >= @Py3_SIZE(op)) {
650 error(sprintf("tuple index %d out of range (%d)", i, @Py3_SIZE(op)))
651 return 0
652 }
653 return @Py3ListObject(op)->ob_item[i]
654 }
655
656 private function Py3List_Repr:string(object:long)
657 {
658 if (!@Py3List_Check(object)) {
659 error(sprintf("Py3List_Repr called on %s object at %p",
660 python3_get_typename(object), object))
661 return ""
662 }
663 n = @Py3_SIZE(object)
664 if (n == 0)
665 return "[]"
666
667 retstr = "["
668 first = 1
669 for (i = 0; i < n; i++) {
670 if (! first)
671 retstr .= " "
672 first = 0
673 retstr .= Py3Object_Repr(Py3List_GetItem(object, i))
674 }
675 retstr .= "]"
676 return retstr
677 }
678
679 #
680 # Systemtap functions based on C functions in dictobject.c.
681 #
682
683 @define DK_SIZE(dk) %(
684 (@choose_defined(@Py3DictKeysObject(@dk)->dk_size,
685 ((1)<<@Py3DictKeysObject(@dk)->dk_log2_size)))
686 %)
687 @define DK_IXSIZE(dk) %(
688 %( CONFIG_64BIT == "y" %?
689 %( CONFIG_COMPAT == "y" %?
690 (@__compat_task
691 ? (@DK_SIZE(@dk) <= 0xff ? 1 : (@DK_SIZE(@dk) <= 0xffff ? 2 : 4))
692 : (@DK_SIZE(@dk) <= 0xff ?
693 1 : (@DK_SIZE(@dk) <= 0xffff ?
694 2 : (@DK_SIZE(@dk) <= 0xffffffff ? 4 : 8))))
695 %:
696 (@DK_SIZE(@dk) <= 0xff ?
697 1 : (@DK_SIZE(@dk) <= 0xffff ?
698 2 : (@DK_SIZE(@dk) <= 0xffffffff ? 4 : 8)))
699 %)
700 %:
701 (@DK_SIZE(@dk) <= 0xff ? 1 : (@DK_SIZE(@dk) <= 0xffff ? 2 : 4))
702 %)
703 %)
704
705 @define DK_ENTRIES(dk) %(
706 (@choose_defined(@Py3DictKeysObject(@dk)->dk_entries,
707 (&@Py3DictKeyEntry(&@Py3DictKeysObject(@dk)->dk_indices[@DK_SIZE(@dk) * @DK_IXSIZE(@dk)]))))
708 // XXX: python 3.6?
709 // (&@Py3DictKeyEntry(&@Py3DictKeysObject(@dk)->dk_indices->as_1[@DK_SIZE(@dk) * @DK_IXSIZE(@dk)]))))
710 %)
711
712 @define DK_UNICODE_ENTRIES(dk) %(
713 (@choose_defined(@Py3DictKeysObject(@dk)->dk_entries,
714 @choose_defined(&@Py3DictUnicodeEntry(&@Py3DictKeysObject(@dk)->dk_indices[@DK_SIZE(@dk) * @DK_IXSIZE(@dk)]),
715 (&@Py3DictKeyEntry(&@Py3DictKeysObject(@dk)->dk_indices[@DK_SIZE(@dk) * @DK_IXSIZE(@dk)])))
716 )
717 )
718 %)
719
720 @define DK_KIND(dk) %(
721 (@choose_defined(@Py3DictKeysObject(@dk)->dk_kind, %{ DICT_KEYS_GENERAL %} ))
722 # @Py3DictKeysObject(@dk)->dk_kind
723 %)
724
725 @define ME_KEY(entry_ptr) %(
726 (@choose_defined(@Py3DictUnicodeEntry(@entry_ptr)->me_key,@Py3DictKeyEntry(@entry_ptr)->me_key))
727 %)
728
729 @define ME_VALUE(entry_ptr) %(
730 (@choose_defined(@Py3DictUnicodeEntry(@entry_ptr)->me_value,@Py3DictKeyEntry(@entry_ptr)->me_value))
731 %)
732
733 @define Py3ME_KEY(key_kind,entry_ptr) %(
734 ((@key_kind == %{ DICT_KEYS_GENERAL %} ) ?
735 (@Py3DictKeyEntry(@entry_ptr)->me_key) :
736 (@ME_KEY(@entry_ptr)))
737 %)
738
739 @define Py3ME_VALUE(key_kind,entry_ptr) %(
740 ((@key_kind == %{ DICT_KEYS_GENERAL %}) ?
741 (@Py3DictKeyEntry(@entry_ptr)->me_value) :
742 (@ME_VALUE(@entry_ptr)))
743 %)
744
745 private function Py3Dict_GetItem:long(op:long, key:string)
746 {
747 if (!@Py3Dict_Check(op)) {
748 error(sprintf("Py3Dict_GetItem called on %s object at %p",
749 python3_get_typename(op), @__pointer(op)))
750 return 0
751 }
752 // We'd like to hash 'key' here, but we can't (easily). Python
753 // randomizes its hash function at python process startup. So,
754 // instead of hashing the key and then searching for that hash in
755 // the dictionary, we'll do a linear search of the dictionary for
756 // the key.
757
758 // In python 3, each DictObject can be in one of two forms.
759 //
760 // Either:
761 // A combined table:
762 // ma_values == NULL, dk_refcnt == 1.
763 // Values are stored in the me_value field of the PyDictKeysObject.
764 // Or:
765 // A split table:
766 // ma_values != NULL, dk_refcnt >= 1
767 // Values are stored in the ma_values array.
768 // Only string (unicode) keys are allowed.
769
770 // If we've got a split table...
771 if (@Py3DictObject(op)->ma_values != 0) {
772 n = @DK_SIZE(@Py3DictObject(op)->ma_keys)
773 for (i = 0; i < n; i++) {
774 entry_ptr = &@DK_ENTRIES(@Py3DictObject(op)->ma_keys)[i]
775 key_kind = @DK_KIND(@Py3DictObject(op)->ma_keys)
776 if (@Py3ME_KEY(key_kind,entry_ptr) == 0
777 || ! @Py3Unicode_Check(@Py3ME_KEY(key_kind,entry_ptr)))
778 continue
779 if (Py3Unicode_AsASCIIString(@Py3ME_KEY(key_kind,entry_ptr)) == key)
780 return Py3Dict_GetValue(op,i)
781 }
782 }
783 // If we've got a combined table...
784 else {
785 n = @choose_defined(@Py3DictObject(op)->ma_keys->dk_nentries,
786 @DK_SIZE(@Py3DictObject(op)->ma_keys))
787 for (i = 0; i < n; i++) {
788 # if DK_IS_UNICODE ...
789 entry_ptr = (key_kind == %{ DICT_KEYS_GENERAL %} )
790 ? &@DK_ENTRIES(@Py3DictObject(op)->ma_keys)[i]
791 : &@DK_UNICODE_ENTRIES(@Py3DictObject(op)->ma_keys)[i]
792 key_kind = @DK_KIND(@Py3DictObject(op)->ma_keys)
793 if (@Py3ME_KEY(key_kind,entry_ptr) == 0 || @Py3ME_VALUE(key_kind,entry_ptr) == 0
794 || ! @Py3Unicode_Check(@Py3ME_KEY(key_kind,entry_ptr)))
795 continue
796 if (Py3Unicode_AsASCIIString(@Py3ME_KEY(key_kind,entry_ptr)) == key)
797 return @Py3ME_VALUE(key_kind,entry_ptr)
798 }
799 }
800 return 0
801 }
802
803 # This function isn't really based on a function in dictobject.c, but
804 # it is based on Py3Dict_GetItem().
805 private function Py3Dict_GetItem_FromLong:long(op:long, key:long)
806 {
807 if (!@Py3Dict_Check(op)) {
808 error(sprintf("Py3Dict_GetItem called on %s object at %p",
809 python3_get_typename(op), @__pointer(op)))
810 return 0
811 }
812
813 // See Py3Dict_GetItem() for details on dicts.
814
815 // If we've got a split table...
816 if (@Py3DictObject(op)->ma_values != 0) {
817 n = @Py3DictObject(op)->ma_used
818 for (i = 0; i < n; i++) {
819 entry_ptr(Py3Dict_GetValue(op,i))
820 if (@Py3ME_KEY(key_kind,entry_ptr) == 0
821 || ! @Py3Long_Check(@Py3ME_KEY(key_kind,entry_ptr)))
822 continue
823 if (Py3Long_AsLongLong(@Py3ME_KEY(key_kind,entry_ptr)) == key)
824 return Py3Dict_GetValue(op,i)
825 }
826 error(sprintf("Python variable '%s' cannot be found", key))
827 }
828 // If we've got a combined table...
829 else {
830 n = @choose_defined(@Py3DictObject(op)->ma_keys->dk_nentries,
831 @DK_SIZE(@Py3DictObject(op)->ma_keys))
832 for (i = 0; i < n; i++) {
833 entry_ptr = &@DK_ENTRIES(Py3Dict_GetValue(op,i))
834 key_kind = @DK_KIND(@Py3DictObject(op)->ma_keys)
835 if (@Py3ME_KEY(key_kind,entry_ptr) == 0 || @Py3ME_VALUE(key_kind,entry_ptr) == 0
836 || ! @Py3Long_Check(@Py3ME_KEY(key_kind,entry_ptr)))
837 continue
838 if (Py3Long_AsLongLong(@Py3ME_KEY(key_kind,entry_ptr)) == key)
839 return @Py3ME_VALUE(key_kind,entry_ptr)
840 }
841 error(sprintf("Python variable '%s' cannot be found", key))
842 }
843 return 0
844 }
845
846 private function Py3Dict_Repr:string(object:long)
847 {
848 if (!@Py3Dict_Check(object)) {
849 error(sprintf("Py3Dict_Repr called on %s object at %p",
850 python3_get_typename(object), @__pointer(object)))
851 return ""
852 }
853
854 // If we've got a split table...
855 if (@Py3DictObject(object)->ma_values != 0) {
856 n = @Py3DictObject(object)->ma_used
857 if (n == 0)
858 return "{}"
859
860 retstr = "{"
861 first = 1
862 for (i = 0; i < n; i++) {
863 entry_ptr = &@DK_ENTRIES(@Py3DictObject(object)->ma_keys)[i]
864 key_kind = @DK_KIND(@Py3DictObject(object)->ma_keys)
865 if (@Py3ME_KEY(key_kind,entry_ptr) == 0)
866 continue
867
868 if (! first)
869 retstr .= " "
870 first = 0
871 retstr .= Py3Object_Repr(@Py3ME_KEY(key_kind,entry_ptr))
872 retstr .= ":"
873 retstr .= Py3Object_Repr(Py3Dict_GetValue(object,i))
874 }
875 retstr .= "}"
876 }
877 // If we've got a combined table...
878 else {
879 n = @choose_defined(@Py3DictObject(object)->ma_keys->dk_nentries,
880 @DK_SIZE(@Py3DictObject(object)->ma_keys))
881 if (n == 0)
882 return "{}"
883
884 retstr = "{"
885 first = 1
886 for (i = 0; i < n; i++) {
887 entry_ptr = (key_kind == %{ DICT_KEYS_GENERAL %} )
888 ? &@DK_ENTRIES(@Py3DictObject(object)->ma_keys)[i]
889 : &@DK_UNICODE_ENTRIES(@Py3DictObject(object)->ma_keys)[i]
890 key_kind = @DK_KIND(@Py3DictObject(object)->ma_keys)
891 if (@Py3ME_KEY(key_kind,entry_ptr) == 0 || @Py3ME_VALUE(key_kind,entry_ptr) == 0)
892 continue
893
894 if (! first)
895 retstr .= " "
896 first = 0
897 retstr .= Py3Object_Repr(@Py3ME_KEY(key_kind,entry_ptr))
898 retstr .= ":"
899 retstr .= Py3Object_Repr(@Py3ME_VALUE(key_kind,entry_ptr))
900 }
901 retstr .= "}"
902 }
903 return retstr
904 }
905
906 #
907 # Systemtap functions based on C functions in object.c.
908 #
909
910 function Py3Object_Repr:string(object:long)
911 {
912 if (object == 0)
913 return "<NULL>"
914 if (@Py3Unicode_Check(object))
915 return sprintf("\"%s\"", Py3Unicode_AsASCIIString(object))
916 if (@Py3Long_Check(object))
917 return sprintf("%#x", Py3Long_AsLongLong(object))
918 if (@Py3Tuple_Check(object))
919 return Py3Tuple_Repr(object)
920 if (@Py3List_Check(object))
921 return Py3List_Repr(object)
922 if (@Py3Dict_Check(object))
923 return Py3Dict_Repr(object)
924 return sprintf("<%s object at %p>", python3_get_typename(object),
925 @__pointer(object))
926 }
927
928 # This function isn't really based on a function in object.c, but
929 # it is based on Py3Object_Repr().
930 private function Py3Object_Repr:string(object:long, index:long)
931 {
932 if (object == 0)
933 return "<NULL>"
934 if (@Py3Unicode_Check(object))
935 return sprintf("\"%c\"", stringat(Py3Unicode_AsASCIIString(object),
936 index))
937 if (@Py3Tuple_Check(object))
938 return Py3Object_Repr(Py3Tuple_GetItem(object, index))
939 if (@Py3List_Check(object))
940 return Py3Object_Repr(Py3List_GetItem(object, index))
941 if (@Py3Dict_Check(object))
942 return Py3Object_Repr(Py3Dict_GetItem_FromLong(object, index))
943
944 error(sprintf("TypeError: '%s' object cannot be indexed",
945 python3_get_typename(object)))
946 return ""
947 }
948
949 # This function isn't really based on a function in object.c, but
950 # it is based on Py3Object_Repr().
951 private function Py3Object_GetItem:string(object:long, index:long)
952 {
953 if (object == 0) {
954 error("TypeError: None object cannot be indexed")
955 return 0
956 }
957 if (@Py3Tuple_Check(object))
958 return Py3Tuple_GetItem(object, index)
959 if (@Py3List_Check(object))
960 return Py3List_GetItem(object, index)
961 if (@Py3Dict_Check(object))
962 return Py3Dict_GetItem_FromLong(object, index)
963
964 error(sprintf("TypeError: '%s' object cannot be indexed",
965 python3_get_typename(object)))
966 return 0
967 }
968
969 private function Py3Object_GenericGetAttrWithDict:long(obj:long, name:string,
970 dict:long)
971 {
972 tp = @Py3_TYPE(obj)
973 if (tp->tp_dict == 0) {
974 return 0
975 }
976
977 if (dict == 0) {
978 dictoffset = tp->tp_dictoffset
979 if (dictoffset != 0) {
980 if (dictoffset < 0) {
981 error("yikes")
982 }
983 dict = user_long(obj + dictoffset);
984 }
985 }
986 if (dict != 0) {
987 res = Py3Dict_GetItem(dict, name)
988 if (res != 0)
989 return res
990 }
991 return 0
992 }
993
994 @define Py3Object_GenericGetAttr(obj, name) %(
995 Py3Object_GenericGetAttrWithDict(@obj, @name, 0)
996 %)
997
998 # The system translator will generate code that calls this function
999
1000 function Py3Object_GetAttr:long(object:long, attr:string)
1001 {
1002 if (object == 0) {
1003 error("The None object has no attributes")
1004 return 0
1005 }
1006
1007 if (@Py3_TYPE(object)->tp_getattro == 0) {
1008 error(sprintf("The %s object has no attributes",
1009 python3_get_typename(object)))
1010 return 0
1011 }
1012
1013 // Python 3 is quite different than python 2 when it comes to
1014 // attributes. Classes are no longer their own type anymore, so we
1015 // can't really check to see if something is a class.
1016 //
1017 // The python 3.6m source uses PyObject_GenericGetAttr() for most
1018 // objects that support attributes. Now we need to figure out if
1019 // that's the function we've got. If so, we know how to handle
1020 // this object.
1021 if (Py3Object_GenericGetAttr_addr != @Py3_TYPE(object)->tp_getattro) {
1022 error(sprintf("Systemtap doesn't know how to get attributes from %s objects",
1023 python3_get_typename(object)))
1024 return 0
1025 }
1026
1027 res = @Py3Object_GenericGetAttr(object, attr)
1028 if (res != 0)
1029 return res
1030
1031 error(sprintf("The %s object has no attribute '%s'",
1032 python3_get_typename(object), attr))
1033
1034 return 0
1035 }
1036
1037 #
1038 # Systemtap support functions for python 3.
1039 #
1040
1041 /*
1042 * python_print_backtrace - Print python backtrace
1043 *
1044 * Description: This function is equivalent to
1045 * print(python_sprint_backtrace(frame)), except that deeper stack
1046 * tracing may be supported.
1047 */
1048 function python_print_backtrace:long()
1049 {
1050 if ($$$$provider != "HelperSDT3") next;
1051 frame = $$$arg3;
1052 printf("Traceback (most recent call first):\n")
1053 while (frame != 0) {
1054 lineno = Py3Frame_GetLineNumber(frame)
1055 code = Py3Frame_GetCode(frame)
1056 try {
1057 filename = Py3Unicode_AsASCIIString(Py3Frame_GetFilename(frame))
1058 } catch {
1059 filename = ""
1060 }
1061 try {
1062 name = Py3Unicode_AsASCIIString(code->co_name)
1063 } catch {
1064 name = ""
1065 }
1066
1067 # Quit when we make it back to the HelperSDT module.
1068 if (isinstr(filename, "/HelperSDT/"))
1069 break
1070
1071 printf(" File \"%s\", line %ld, in %s\n", filename, lineno, name)
1072 # NB: We'd like to print the source line here as python does,
1073 # but we can't. Python opens up the file and finds the
1074 # appropriate line, but we can't really do that when we're in
1075 # the kernel.
1076
1077 frame = Py3Frame_GetBack (frame)
1078 }
1079 }
1080
1081 /*
1082 * python_sprint_backtrace - Get python backtrace
1083 *
1084 * Description: This function returns a string containing a python
1085 * backtrace. Output may be truncated as per maximum string length
1086 * (MAXSTRINGLEN).
1087 */
1088 function python_sprint_backtrace:string()
1089 {
1090 if ($$$$provider != "HelperSDT3") next;
1091 frame = $$$arg3;
1092 retstr = "Traceback (most recent call first):\n"
1093 while (frame != 0) {
1094 lineno = Py3Frame_GetLineNumber(frame)
1095 code = Py3Frame_GetCode(frame)
1096 filename = Py3Unicode_AsASCIIString(Py3Frame_GetFilename(frame))
1097 name = Py3Unicode_AsASCIIString(code->co_name)
1098
1099 # Quit when we make it back to the HelperSDT module.
1100 if (isinstr(filename, "/HelperSDT/"))
1101 break
1102
1103 retstr .= sprintf(" File \"%s\", line %d, in %s\n", filename,
1104 lineno, name)
1105
1106 frame = Py3Frame_GetBack (frame)
1107 }
1108 return retstr
1109 }
1110
1111
1112 /*
1113 * python3_get_typename - Get python object type name
1114 *
1115 * Description: This function returns a string describing the type of
1116 * a python object.
1117 */
1118 private function python3_get_typename:string(obj:long)
1119 {
1120 if (obj == 0)
1121 return ""
1122 return user_string(@Py3_TYPE(obj)->tp_name)
1123 }
1124
1125 /*
1126 * python3_initiatlize - Initialize python tapset
1127 *
1128 * Description: This function gets values from the HelperSDT module.
1129 *
1130 * Note that users shouldn't call this directly. The translator will
1131 * generate code that calls this function with the correct arguments.
1132 */
1133 function python3_initialize:long(arg1:long)
1134 {
1135 Py3Object_GenericGetAttr_addr = arg1
1136 }
1137
1138 /*
1139 * python3_get_locals - Get python local variables
1140 *
1141 * Description: This function returns a list of python local variables
1142 * from a frame.
1143 *
1144 * frame: python frame object pointer
1145 * flags: 0: function parameters only ($$parms), 1: locals only
1146 * ($$locals), 2: all local vars, parameters and locals ($$vars)
1147 *
1148 * Note that users shouldn't call this directly, they should refer to
1149 * '$$parms', '$$locals', and '$$vars' in a python probe and the
1150 * translator will generate code that calls this function with the
1151 * correct arguments.
1152 */
1153 function python3_get_locals:string(frame:long, flags:long)
1154 {
1155 code = Py3Frame_GetCode(frame)
1156
1157 # flags == 2: get all variables
1158 if (flags == 2) {
1159 i = 0
1160 n = code->co_nlocals
1161 }
1162 else {
1163 n = code->co_argcount + code->co_kwonlyargcount
1164 if (code->co_flags & %{ Py3_CO_VARARGS %})
1165 n++
1166 if (code->co_flags & %{ Py3_CO_VARKEYWORDS %})
1167 n++
1168
1169 # flags == 0 (parms only): 0 to n (argcount)
1170 if (flags == 0)
1171 i = 0
1172 # flags == 1 (locals only): n (argcount) to max
1173 else if (flags == 1) {
1174 i = n
1175 n = code->co_nlocals
1176 }
1177 }
1178 if (i < 0 || i > n) {
1179 error(sprintf("python3_get_locals: invalid indicies (%d-%d)", i, n))
1180 return ""
1181 }
1182
1183 if (@defined(@cast(code, "PyCodeObject")->co_nlocalsplus)) {
1184 localsplus = Py3Frame_GetLocalsplus(frame)
1185 for (offset = 0; offset < code->co_nlocalsplus; offset++) {
1186 key_obj = Py3Tuple_GetItem(code->co_localsplusnames, offset)
1187 value_obj = user_long(localsplus + (offset * %{ sizeof(intptr_t) %}))
1188 retstr .= sprintf("%s=%s", Py3Unicode_AsASCIIString(key_obj),
1189 Py3Object_Repr(value_obj))
1190 }
1191 }
1192 else {
1193 # Each element in co_varnames is a tuple of strings. The values
1194 # are in f_localsplus.
1195 if (@defined(@cast(code, "PyCodeObject")->co_varnames))
1196 p = code->co_varnames;
1197 else
1198 p = 0
1199 localsplus = @choose_defined(@Py3FrameObject(frame)->f_localsplus,0)
1200 first = 1
1201 for (; i < n; i++) {
1202 if (! first)
1203 retstr .= " "
1204 first = 0
1205 key_obj = Py3Tuple_GetItem(p, i)
1206 value_obj = user_long(localsplus + (i * %{ sizeof(intptr_t) %}))
1207 retstr .= sprintf("%s=%s", Py3Unicode_AsASCIIString(key_obj),
1208 Py3Object_Repr(value_obj))
1209 }
1210 }
1211 return retstr
1212 }
1213
1214 # The system translator will generate code that calls this function
1215
1216 function python3_get_var_obj:long(frame:long, var:string)
1217 {
1218 try {
1219 f_code = Py3Frame_GetCode(frame)
1220 n = f_code->co_nlocals
1221
1222 if (@defined(@cast(f_code, "PyCodeObject")->co_nlocalsplus)) {
1223 localsplus = Py3Frame_GetLocalsplus(frame)
1224 for (offset = 0; offset < f_code->co_nlocalsplus; offset++) {
1225 key_obj = Py3Tuple_GetItem(f_code->co_localsplusnames, offset)
1226 if (var == Py3Unicode_AsASCIIString(key_obj)) {
1227 return user_long(localsplus + (offset * %{ sizeof(intptr_t) %}))
1228 }
1229 }
1230 }
1231 else {
1232 # Each element in co_varnames is a tuple of strings. The values
1233 # are in f_localsplus.
1234 if (@defined(@cast(f_code, "PyCodeObject")->co_varnames))
1235 p = f_code->co_varnames;
1236 else
1237 p = 0
1238 localsplus = @choose_defined(@Py3FrameObject(frame)->f_localsplus,0)
1239 for (i = 0; i < n; i++) {
1240 key_obj = Py3Tuple_GetItem(p, i)
1241 if (var == Py3Unicode_AsASCIIString(key_obj)) {
1242 return user_long(localsplus + (i * %{ sizeof(intptr_t) %}))
1243 }
1244 }
1245 }
1246
1247 # If we can't find it in the locals, we look in the globals.
1248 f_globals = Py3Frame_GetGlobals(frame)
1249 value_obj = Py3Dict_GetItem(f_globals, var)
1250 if (value_obj == 0) {
1251 error(sprintf("Python variable cannot be found"))
1252 return 0
1253 }
1254 else {
1255 return value_obj
1256 }
1257 } catch {
1258 # printf("Python variable '%s' cannot be accessed\n", var)
1259 }
1260 }
1261
1262 function python3_get_var_obj:long(frame:long, var:string, index:long)
1263 {
1264 f_code = Py3Frame_GetCode(frame)
1265 n = Py3Code_nlocals(f_code)
1266
1267 if (@defined(@cast(f_code, "PyCodeObject")->co_nlocalsplus)) {
1268 localsplus = Py3Frame_GetLocalsplus(frame)
1269 for (offset = 0; offset < f_code->co_nlocalsplus; offset++) {
1270 key_obj = Py3Tuple_GetItem(f_code->co_localsplusnames, offset)
1271 if (var == Py3Unicode_AsASCIIString(key_obj)) {
1272 return user_long(localsplus + (offset * %{ sizeof(intptr_t) %}))
1273 }
1274 }
1275 }
1276 else {
1277 # Each element in co_varnames is a tuple of strings. The values
1278 # are in f_localsplus.
1279 if (@defined(@cast(f_code, "PyCodeObject")->co_varnames))
1280 p = f_code->co_varnames;
1281 else
1282 p = 0
1283 localsplus = @choose_defined(@Py3FrameObject(frame)->f_localsplus,0)
1284 printf("XXX3 %d %#lx\n", n, localsplus);
1285 for (i = 0; i < n; i++) {
1286 key_obj = Py3Tuple_GetItem(p, i)
1287 if (var == Py3Unicode_AsASCIIString(key_obj)) {
1288 return user_long(localsplus + (i * %{ sizeof(intptr_t) %}))
1289 }
1290 }
1291 }
1292
1293 # If we can't find it in the locals, we look in the globals.
1294 f_globals = Py3Frame_GetGlobals(frame)
1295 value_obj = Py3Dict_GetItem(f_globals, var)
1296 if (value_obj == 0) {
1297 error(sprintf("Python variable '%s' cannot be found", var))
1298 return 0
1299 }
1300 return value_obj
1301 }