| 1 |
// python 2 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 |
%{ |
| 10 |
/* |
| 11 |
* Defines borrowed from object.h. |
| 12 |
*/ |
| 13 |
|
| 14 |
/* These flags are used to determine if a type is a subclass. */ |
| 15 |
#define Py2_TPFLAGS_INT_SUBCLASS (1L<<23) |
| 16 |
#define Py2_TPFLAGS_LONG_SUBCLASS (1L<<24) |
| 17 |
#define Py2_TPFLAGS_LIST_SUBCLASS (1L<<25) |
| 18 |
#define Py2_TPFLAGS_TUPLE_SUBCLASS (1L<<26) |
| 19 |
#define Py2_TPFLAGS_STRING_SUBCLASS (1L<<27) |
| 20 |
#define Py2_TPFLAGS_UNICODE_SUBCLASS (1L<<28) |
| 21 |
#define Py2_TPFLAGS_DICT_SUBCLASS (1L<<29) |
| 22 |
#define Py2_TPFLAGS_BASE_EXC_SUBCLASS (1L<<30) |
| 23 |
#define Py2_TPFLAGS_TYPE_SUBCLASS (1L<<31) |
| 24 |
|
| 25 |
/* |
| 26 |
* Defines borrowed from code.h. |
| 27 |
*/ |
| 28 |
|
| 29 |
/* Masks for co_flags above */ |
| 30 |
#define Py2_CO_OPTIMIZED 0x0001 |
| 31 |
#define Py2_CO_NEWLOCALS 0x0002 |
| 32 |
#define Py2_CO_VARARGS 0x0004 |
| 33 |
#define Py2_CO_VARKEYWORDS 0x0008 |
| 34 |
#define Py2_CO_NESTED 0x0010 |
| 35 |
#define Py2_CO_GENERATOR 0x0020 |
| 36 |
/* The Py2_CO_NOFREE flag is set if there are no free or cell variables. |
| 37 |
This information is redundant, but it allows a single flag test |
| 38 |
to determine whether there is any extra work to be done when the |
| 39 |
call frame it setup. |
| 40 |
*/ |
| 41 |
#define Py2_CO_NOFREE 0x0040 |
| 42 |
|
| 43 |
/* |
| 44 |
* Defines borrowed from longintrepr.h |
| 45 |
*/ |
| 46 |
#define Py2Long_SHIFT_SMALL 15 |
| 47 |
#define Py2Long_SHIFT_BIG 30 |
| 48 |
|
| 49 |
%} |
| 50 |
|
| 51 |
# |
| 52 |
# Macros to cast to various python 2 types |
| 53 |
# PR25841 makes this easy |
| 54 |
# |
| 55 |
|
| 56 |
########################################### |
| 57 |
|
| 58 |
@define Py2Object(object) %( |
| 59 |
@cast(@object, "PyObject") |
| 60 |
%) |
| 61 |
@define Py2VarObject(object) %( |
| 62 |
@cast(@object, "PyVarObject") |
| 63 |
%) |
| 64 |
@define Py2IntObject(object) %( |
| 65 |
@cast(@object, "PyIntObject") |
| 66 |
%) |
| 67 |
@define Py2StringObject(object) %( |
| 68 |
@cast(@object, "PyStringObject") |
| 69 |
%) |
| 70 |
@define Py2UnicodeObject(object) %( |
| 71 |
@cast(@object, "PyUnicodeObject") |
| 72 |
%) |
| 73 |
@define Py2TypeObject(object) %( |
| 74 |
@cast(@object, "PyTypeObject") |
| 75 |
%) |
| 76 |
@define Py2TupleObject(object) %( |
| 77 |
@cast(@object, "PyTupleObject") |
| 78 |
%) |
| 79 |
@define Py2ListObject(object) %( |
| 80 |
@cast(@object, "PyListObject") |
| 81 |
%) |
| 82 |
@define Py2DictObject(object) %( |
| 83 |
@cast(@object, "PyDictObject") |
| 84 |
%) |
| 85 |
@define Py2DictEntry(object) %( |
| 86 |
@cast(@object, "PyDictEntry") |
| 87 |
%) |
| 88 |
@define Py2FrameObject(object) %( |
| 89 |
@cast(@object, "PyFrameObject") |
| 90 |
%) |
| 91 |
@define Py2CodeObject(object) %( |
| 92 |
@cast(@object, "PyCodeObject") |
| 93 |
%) |
| 94 |
@define Py2LongObject(object) %( |
| 95 |
@cast(@object, "PyLongObject") |
| 96 |
%) |
| 97 |
@define Py2ClassObject(object) %( |
| 98 |
@cast(@object, "PyClassObject") |
| 99 |
%) |
| 100 |
@define Py2InstanceObject(object) %( |
| 101 |
@cast(@object, "PyInstanceObject") |
| 102 |
%) |
| 103 |
|
| 104 |
# |
| 105 |
# Systemtap macros based on C macros in object.h. |
| 106 |
# |
| 107 |
|
| 108 |
@define Py2_REFCNT(object) %( |
| 109 |
@Py2Object(@object)->ob_refcnt |
| 110 |
%) |
| 111 |
@define Py2_TYPE(object) %( |
| 112 |
@Py2Object(@object)->ob_type |
| 113 |
%) |
| 114 |
@define Py2_SIZE(object) %( |
| 115 |
@Py2VarObject(@object)->ob_size |
| 116 |
%) |
| 117 |
|
| 118 |
@define Py2Type_HasFeature(t, f) %( |
| 119 |
((@t->tp_flags & (@f)) != 0) |
| 120 |
%) |
| 121 |
@define Py2Type_FastSubclass(t, f) %( |
| 122 |
@Py2Type_HasFeature(@t, @f) |
| 123 |
%) |
| 124 |
|
| 125 |
# |
| 126 |
# Systemtap macros based on C macros in stringobject.h. |
| 127 |
# |
| 128 |
|
| 129 |
@define Py2String_Check(op) %( |
| 130 |
@Py2Type_FastSubclass(@Py2_TYPE(@op), %{ Py2_TPFLAGS_STRING_SUBCLASS %}) |
| 131 |
%) |
| 132 |
|
| 133 |
# |
| 134 |
# Systemtap macros based on C macros in unicodeobject.h. |
| 135 |
# |
| 136 |
|
| 137 |
@define Py2Unicode_Check(op) %( |
| 138 |
@Py2Type_FastSubclass(@Py2_TYPE(@op), %{ Py2_TPFLAGS_UNICODE_SUBCLASS %}) |
| 139 |
%) |
| 140 |
@define Py2Unicode_AS_UNICODE(op) %( |
| 141 |
(@Py2UnicodeObject(@op)->str) |
| 142 |
%) |
| 143 |
|
| 144 |
# |
| 145 |
# Systemtap macros based on C macros in intobject.h. |
| 146 |
# |
| 147 |
|
| 148 |
@define Py2Int_Check(op) %( |
| 149 |
@Py2Type_FastSubclass(@Py2_TYPE(@op), %{ Py2_TPFLAGS_INT_SUBCLASS %}) |
| 150 |
%) |
| 151 |
|
| 152 |
@define Py2Int_AS_LONG(op) %( |
| 153 |
@Py2IntObject(@op)->ob_ival |
| 154 |
%) |
| 155 |
|
| 156 |
# |
| 157 |
# Systemtap macros based on C macros in longobject.h. |
| 158 |
# |
| 159 |
|
| 160 |
@define Py2Long_Check(op) %( |
| 161 |
@Py2Type_FastSubclass(@Py2_TYPE(@op), %{ Py2_TPFLAGS_LONG_SUBCLASS %}) |
| 162 |
%) |
| 163 |
|
| 164 |
# |
| 165 |
# Systemtap macros based on C macros in dictobject.h. |
| 166 |
# |
| 167 |
|
| 168 |
@define Py2Dict_Check(op) %( |
| 169 |
@Py2Type_FastSubclass(@Py2_TYPE(@op), %{ Py2_TPFLAGS_DICT_SUBCLASS %}) |
| 170 |
%) |
| 171 |
|
| 172 |
# |
| 173 |
# Systemtap macros based on C macros in tupleobject.h. |
| 174 |
# |
| 175 |
|
| 176 |
@define Py2Tuple_Check(op) %( |
| 177 |
@Py2Type_FastSubclass(@Py2_TYPE(@op), %{ Py2_TPFLAGS_TUPLE_SUBCLASS %}) |
| 178 |
%) |
| 179 |
|
| 180 |
# |
| 181 |
# Systemtap macros based on C macros in listobject.h. |
| 182 |
# |
| 183 |
|
| 184 |
@define Py2List_Check(op) %( |
| 185 |
@Py2Type_FastSubclass(@Py2_TYPE(@op), %{ Py2_TPFLAGS_LIST_SUBCLASS %}) |
| 186 |
%) |
| 187 |
|
| 188 |
# |
| 189 |
# Systemtap macros based on C macros in classobject.h. |
| 190 |
# |
| 191 |
# Note that the real *_Check() macros compare global variables. We'll |
| 192 |
# just check the type name field instead. |
| 193 |
# |
| 194 |
|
| 195 |
@define Py2Class_Check(op) %( |
| 196 |
(python2_get_typename(@op) == "classobj") |
| 197 |
%) |
| 198 |
@define Py2Instance_Check(op) %( |
| 199 |
(python2_get_typename(@op) == "instance") |
| 200 |
%) |
| 201 |
@define Py2Method_Check(op) %( |
| 202 |
(python2_get_typename(@op) == "instancemethod") |
| 203 |
%) |
| 204 |
|
| 205 |
# |
| 206 |
# Systemtap functions based on C functions in intobject.c. |
| 207 |
# |
| 208 |
|
| 209 |
private function Py2Int_AsLong:long(op:long) |
| 210 |
{ |
| 211 |
if (op && @Py2Int_Check(op)) |
| 212 |
return @Py2Int_AS_LONG(op) |
| 213 |
error(sprintf("Py2Int_AsLong called on %s object at %p", |
| 214 |
python2_get_typename(op), @__pointer(op))) |
| 215 |
return -1 |
| 216 |
} |
| 217 |
|
| 218 |
# |
| 219 |
# Systemtap functions based on C functions in longobject.c. |
| 220 |
# |
| 221 |
|
| 222 |
%{ |
| 223 |
/* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define |
| 224 |
* anything about what happens when a signed integer operation overflows, |
| 225 |
* and some compilers think they're doing you a favor by being "clever" |
| 226 |
* then. The bit pattern for the largest positive signed long is |
| 227 |
* (unsigned long)LONG_MAX, and for the smallest negative signed long |
| 228 |
* it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN. |
| 229 |
* However, some other compilers warn about applying unary minus to an |
| 230 |
* unsigned operand. Hence the weird "0-". |
| 231 |
*/ |
| 232 |
|
| 233 |
#define PY2_LLONG_MIN (-__LONG_LONG_MAX__ - 1L) |
| 234 |
#define PY2_ABS_LLONG_MIN (0-(unsigned long long)PY2_LLONG_MIN) |
| 235 |
%} |
| 236 |
|
| 237 |
/* Get a C long int from an int object or any object that has an __int__ |
| 238 |
* method. |
| 239 |
* |
| 240 |
* On overflow, return -1 and throw an error. |
| 241 |
*/ |
| 242 |
|
| 243 |
/* |
| 244 |
* The Py2Long_AsLongLong() function below is actually based on |
| 245 |
* PyLong_AsLong(). Python's PyLong_AsLongLong() converts the PyLong |
| 246 |
* to a Bytes object. This method is simpler. |
| 247 |
* |
| 248 |
* On 32-bit platforms, the default for PYLONG_BITS_IN_DIGIT is 15. On |
| 249 |
* 64-bit platforms, the default for PYLONG_BITS_IN_DIGIT is 30. We're |
| 250 |
* going to assume that to be true, even though it can be configured |
| 251 |
* differently. |
| 252 |
* |
| 253 |
* Since we can be on a 64-bit kernel but probing a 32-bit python 3 |
| 254 |
* executable, we have to handle both cases at the same time. |
| 255 |
*/ |
| 256 |
|
| 257 |
private function Py2Long_AsLongLongAndOverflow_Small:long(v:long) |
| 258 |
{ |
| 259 |
if (@defined(@Py2LongObject(0)->ob_digit[0])) { |
| 260 |
res = -1 |
| 261 |
i = @Py2_SIZE(v) |
| 262 |
if (i == -1) { |
| 263 |
/* We're using 'x & 0xffff' instead of '(short)x' */ |
| 264 |
res = -(@Py2LongObject(v)->ob_digit[0] & 0xffff) |
| 265 |
} |
| 266 |
else if (i == 0) { |
| 267 |
res = 0 |
| 268 |
} |
| 269 |
else if (i == 1) { |
| 270 |
res = @Py2LongObject(v)->ob_digit[0] |
| 271 |
} |
| 272 |
else { |
| 273 |
sign = 1 |
| 274 |
x = 0 |
| 275 |
if (i < 0) { |
| 276 |
sign = -1 |
| 277 |
i = -(i) |
| 278 |
} |
| 279 |
while (--i >= 0) { |
| 280 |
prev = x |
| 281 |
x = ((x << %{ Py2Long_SHIFT_SMALL %}) |
| 282 |
| @Py2LongObject(v)->ob_digit[i]) |
| 283 |
if ((x >> %{ Py2Long_SHIFT_SMALL %}) != prev) { |
| 284 |
error("Python int too large") |
| 285 |
return res |
| 286 |
} |
| 287 |
} |
| 288 |
/* Haven't lost any bits, but casting to long requires extra |
| 289 |
* care (see comment above). |
| 290 |
*/ |
| 291 |
if (x <= %{ __LONG_LONG_MAX__ %}) { |
| 292 |
res = x * sign |
| 293 |
} |
| 294 |
else if (sign < 0 && x == %{ PY2_ABS_LLONG_MIN %}) { |
| 295 |
res = %{ PY2_LLONG_MIN %} |
| 296 |
} |
| 297 |
else { |
| 298 |
error("Python int too large") |
| 299 |
/* res is already set to -1 */ |
| 300 |
} |
| 301 |
} |
| 302 |
return res |
| 303 |
} |
| 304 |
else { |
| 305 |
error("Py2Long_AsLongLongAndOverflow_Small isn't implemented") |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
private function Py2Long_AsLongLongAndOverflow_Big:long(v:long) |
| 310 |
{ |
| 311 |
if (@defined(@Py2LongObject(0)->ob_digit[0])) { |
| 312 |
res = -1 |
| 313 |
i = @Py2_SIZE(v) |
| 314 |
if (i == -1) { |
| 315 |
/* We're using 'x & 0xffffffff' instead of '(int32_t)x' */ |
| 316 |
res = -(@Py2LongObject(v)->ob_digit[0] & 0xffffffff) |
| 317 |
} |
| 318 |
else if (i == 0) { |
| 319 |
res = 0 |
| 320 |
} |
| 321 |
else if (i == 1) { |
| 322 |
res = @Py2LongObject(v)->ob_digit[0] |
| 323 |
} |
| 324 |
else { |
| 325 |
sign = 1 |
| 326 |
x = 0 |
| 327 |
if (i < 0) { |
| 328 |
sign = -1 |
| 329 |
i = -(i) |
| 330 |
} |
| 331 |
while (--i >= 0) { |
| 332 |
prev = x |
| 333 |
x = ((x << %{ Py2Long_SHIFT_BIG %}) |
| 334 |
| @Py2LongObject(v)->ob_digit[i]) |
| 335 |
if ((x >> %{ Py2Long_SHIFT_BIG %}) != prev) { |
| 336 |
error("Python int too large") |
| 337 |
return res |
| 338 |
} |
| 339 |
} |
| 340 |
/* Haven't lost any bits, but casting to long requires extra |
| 341 |
* care (see comment above). |
| 342 |
*/ |
| 343 |
if (x <= %{ __LONG_LONG_MAX__ %}) { |
| 344 |
res = x * sign |
| 345 |
} |
| 346 |
else if (sign < 0 && x == %{ PY2_ABS_LLONG_MIN %}) { |
| 347 |
res = %{ PY2_LLONG_MIN %} |
| 348 |
} |
| 349 |
else { |
| 350 |
error("Python int too large") |
| 351 |
/* res is already set to -1 */ |
| 352 |
} |
| 353 |
} |
| 354 |
return res |
| 355 |
} |
| 356 |
else { |
| 357 |
error("Py2Long_AsLongLongAndOverflow_Big isn't implemented") |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
private function Py2Long_AsLongLong:long(v:long) |
| 362 |
{ |
| 363 |
if (@defined(@Py2LongObject(0)->ob_digit[0])) { |
| 364 |
if (! @Py2Long_Check(v)) { |
| 365 |
error(sprintf("Py2Long_AsLong called on %s object at %p", |
| 366 |
python2_get_typename(v), @__pointer(v))) |
| 367 |
return -1 |
| 368 |
} |
| 369 |
|
| 370 |
%( CONFIG_64BIT == "y" %? |
| 371 |
%( CONFIG_COMPAT == "y" %? |
| 372 |
if (@__compat_task) { |
| 373 |
return Py2Long_AsLongLongAndOverflow_Small(v) |
| 374 |
} |
| 375 |
%) |
| 376 |
return Py2Long_AsLongLongAndOverflow_Big(v) |
| 377 |
%: |
| 378 |
return Py2Long_AsLongLongAndOverflow_Small(v) |
| 379 |
%) |
| 380 |
} |
| 381 |
else { |
| 382 |
error("Py2Long_AsLongLong isn't implemented") |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
# |
| 387 |
# Systemtap functions based on C functions in stringobject.c. |
| 388 |
# |
| 389 |
|
| 390 |
private function Py2String_AsString:string(object:long) |
| 391 |
{ |
| 392 |
if (!@Py2String_Check(object)) { |
| 393 |
# NB: python returns value of string_getbuffer() here |
| 394 |
# (basically getting a string representation of any |
| 395 |
# object). We can't create new objects here, so we'll |
| 396 |
# just quit. |
| 397 |
warn(sprintf("Py2String_AsString called on non-string address 0x%p\n", |
| 398 |
@__pointer(object))) |
| 399 |
return "" |
| 400 |
} |
| 401 |
try { |
| 402 |
return user_string(@Py2StringObject(object)->ob_sval) |
| 403 |
} catch { |
| 404 |
warn(sprintf("Py2String_AsString failed on address 0x%p\n", |
| 405 |
@__pointer(@Py2StringObject(object)->ob_sval))) |
| 406 |
return "" |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
private function Py2String_Size:long(object:long) |
| 411 |
{ |
| 412 |
if (! @Py2String_Check(object)) { |
| 413 |
# NB: python code returns string_getsize(), creating a new |
| 414 |
# string representation of the object, then returning the |
| 415 |
# length of the new string. We can't create new objects here, |
| 416 |
# so we'll just quit. |
| 417 |
warn(sprintf("Py2string_Size called on non-string object address 0x%p\n", |
| 418 |
@__pointer(object))) |
| 419 |
return 0 |
| 420 |
} |
| 421 |
return @Py2_SIZE(object) |
| 422 |
} |
| 423 |
|
| 424 |
# |
| 425 |
# Systemtap functions based on C functions in unicodeobject.c. |
| 426 |
# |
| 427 |
|
| 428 |
private function Py2Unicode_AsASCIIString:string(object:long) |
| 429 |
{ |
| 430 |
if (!@Py2Unicode_Check(object)) { |
| 431 |
warn(sprintf("Py2Unicode_AsASCIIString called on non-unicode string address 0x%p\n", |
| 432 |
@__pointer(object))) |
| 433 |
return "" |
| 434 |
} |
| 435 |
try { |
| 436 |
return user_string_utf32(@Py2Unicode_AS_UNICODE(object)) |
| 437 |
} catch { |
| 438 |
warn(sprintf("Py2Unicode_AsASCIIString failed on address 0x%p\n", |
| 439 |
@__pointer(object))) |
| 440 |
return "" |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
# |
| 445 |
# Systemtap functions based on C functions in codeobject.c. |
| 446 |
# |
| 447 |
|
| 448 |
private function Py2Code_Addr2Line:long(code:long, addrq:long) |
| 449 |
{ |
| 450 |
/* |
| 451 |
* co_lnotab is used to compute the line number from a bytecode |
| 452 |
* index, addrq. See the file Objects/lnotab_notes.txt in the |
| 453 |
* python source for the details of the lnotab representation. |
| 454 |
* |
| 455 |
* We can't treat co_lnotab as a "real" null terminated string, |
| 456 |
* since co_lnotab can have embedded null characters. So, we'll |
| 457 |
* grab it character by character. |
| 458 |
*/ |
| 459 |
size = Py2String_Size(@Py2CodeObject(code)->co_lnotab) / 2 |
| 460 |
co_lnotab_sval = @Py2StringObject(@Py2CodeObject(code)->co_lnotab)->ob_sval |
| 461 |
line = @Py2CodeObject(code)->co_firstlineno |
| 462 |
addr = 0 |
| 463 |
p = 0 |
| 464 |
while (--size >= 0) { |
| 465 |
addr += user_char(co_lnotab_sval + p++) |
| 466 |
if (addr > addrq) |
| 467 |
break |
| 468 |
|
| 469 |
line += user_char(co_lnotab_sval + p++) |
| 470 |
} |
| 471 |
return line |
| 472 |
} |
| 473 |
|
| 474 |
# |
| 475 |
# Systemtap functions based on C functions in frameobject.c. |
| 476 |
# |
| 477 |
|
| 478 |
private function Py2Frame_GetLineNumber:long(frame:long) |
| 479 |
{ |
| 480 |
# As of python 2.3, f_lineno is only valid when tracing is active. |
| 481 |
if (@Py2FrameObject(frame)->f_trace) |
| 482 |
return @Py2FrameObject(frame)->f_lineno |
| 483 |
else |
| 484 |
return Py2Code_Addr2Line(@Py2FrameObject(frame)->f_code, |
| 485 |
@Py2FrameObject(frame)->f_lasti) |
| 486 |
} |
| 487 |
|
| 488 |
# |
| 489 |
# Systemtap functions based on C functions in tupleobject.c. |
| 490 |
# |
| 491 |
|
| 492 |
private function Py2Tuple_Size:long(op:long) |
| 493 |
{ |
| 494 |
if (!@Py2Tuple_Check(op)) { |
| 495 |
error(sprintf("Py2Tuple_Size called on %s object at %p", |
| 496 |
python2_get_typename(op), @__pointer(op))) |
| 497 |
return 0 |
| 498 |
} |
| 499 |
return @Py2_SIZE(op) |
| 500 |
} |
| 501 |
|
| 502 |
private function Py2Tuple_GetItem:long(op:long, i:long) |
| 503 |
{ |
| 504 |
if (!@Py2Tuple_Check(op)) { |
| 505 |
error(sprintf("Py2Tuple_GetItem called on %s object at %p", |
| 506 |
python2_get_typename(op), @__pointer(op))) |
| 507 |
return 0 |
| 508 |
} |
| 509 |
if (i < 0 || i >= @Py2_SIZE(op)) { |
| 510 |
error(sprintf("tuple index %d out of range (%d)", i, @Py2_SIZE(op))) |
| 511 |
return 0 |
| 512 |
} |
| 513 |
return @Py2TupleObject(op)->ob_item[i] |
| 514 |
} |
| 515 |
|
| 516 |
private function Py2Tuple_Repr:string(object:long) |
| 517 |
{ |
| 518 |
if (!@Py2Tuple_Check(object)) { |
| 519 |
error(sprintf("Py2Tuple_Repr called on %s object at %p", |
| 520 |
python2_get_typename(object), @__pointer(object))) |
| 521 |
return "" |
| 522 |
} |
| 523 |
n = @Py2_SIZE(object) |
| 524 |
if (n == 0) |
| 525 |
return "()" |
| 526 |
|
| 527 |
retstr = "(" |
| 528 |
first = 1 |
| 529 |
for (i = 0; i < n; i++) { |
| 530 |
if (! first) |
| 531 |
retstr .= " " |
| 532 |
first = 0 |
| 533 |
retstr .= Py2Object_Repr(Py2Tuple_GetItem(object, i)) |
| 534 |
} |
| 535 |
retstr .= ")" |
| 536 |
return retstr |
| 537 |
} |
| 538 |
|
| 539 |
# |
| 540 |
# Systemtap functions based on C functions in listobject.c. |
| 541 |
# |
| 542 |
|
| 543 |
private function Py2List_GetItem:long(op:long, i:long) |
| 544 |
{ |
| 545 |
if (!@Py2List_Check(op)) { |
| 546 |
error(sprintf("Py2List_GetItem called on %s object at %p", |
| 547 |
python2_get_typename(op), @__pointer(op))) |
| 548 |
return 0 |
| 549 |
} |
| 550 |
if (i < 0 || i >= @Py2_SIZE(op)) { |
| 551 |
error(sprintf("tuple index %d out of range (%d)", i, @Py2_SIZE(op))) |
| 552 |
return 0 |
| 553 |
} |
| 554 |
return @Py2ListObject(op)->ob_item[i]; |
| 555 |
} |
| 556 |
|
| 557 |
private function Py2List_Repr:string(object:long) |
| 558 |
{ |
| 559 |
if (!@Py2List_Check(object)) { |
| 560 |
error(sprintf("Py2List_Repr called on %s object at %p", |
| 561 |
python2_get_typename(object), @__pointer(object))) |
| 562 |
return "" |
| 563 |
} |
| 564 |
n = @Py2_SIZE(object) |
| 565 |
if (n == 0) |
| 566 |
return "[]" |
| 567 |
|
| 568 |
retstr = "[" |
| 569 |
first = 1 |
| 570 |
for (i = 0; i < n; i++) { |
| 571 |
if (! first) |
| 572 |
retstr .= " " |
| 573 |
first = 0 |
| 574 |
retstr .= Py2Object_Repr(Py2List_GetItem(object, i)) |
| 575 |
} |
| 576 |
retstr .= "]" |
| 577 |
return retstr |
| 578 |
} |
| 579 |
|
| 580 |
# |
| 581 |
# Systemtap functions based on C functions in dictobject.c. |
| 582 |
# |
| 583 |
|
| 584 |
private function Py2Dict_GetItem:long(op:long, key:string) |
| 585 |
{ |
| 586 |
if (!@Py2Dict_Check(op)) { |
| 587 |
error(sprintf("Py2Dict_GetItem called on %s object at %p", |
| 588 |
python2_get_typename(op), @__pointer(op))) |
| 589 |
return 0 |
| 590 |
} |
| 591 |
// We'd like to hash 'key' here, but we can't (easily). Python |
| 592 |
// randomizes its hash function at python process startup. So, |
| 593 |
// instead of hashing the key and then searching for that hash in |
| 594 |
// the dictionary, we'll do a linear search of the dictionary for |
| 595 |
// the key. |
| 596 |
n = @Py2DictObject(op)->ma_mask; |
| 597 |
for (i = 0; i <= n; i++) { |
| 598 |
entry_ptr = &@Py2DictObject(op)->ma_table[i] |
| 599 |
if (@Py2DictEntry(entry_ptr)->me_key == 0 |
| 600 |
|| @Py2DictEntry(entry_ptr)->me_value == 0 |
| 601 |
|| ! @Py2String_Check(@Py2DictEntry(entry_ptr)->me_key)) |
| 602 |
continue |
| 603 |
if (Py2String_AsString(@Py2DictEntry(entry_ptr)->me_key) == key) |
| 604 |
return @Py2DictEntry(entry_ptr)->me_value |
| 605 |
} |
| 606 |
return 0 |
| 607 |
} |
| 608 |
|
| 609 |
# This function isn't really based on a function in dictobject.c, but |
| 610 |
# it is based on Py2Dict_GetItem(). |
| 611 |
private function Py2Dict_GetItem_FromLong:long(op:long, key:long) |
| 612 |
{ |
| 613 |
if (!@Py2Dict_Check(op)) { |
| 614 |
error(sprintf("Py2Dict_GetItem called on %s object at %p", |
| 615 |
python2_get_typename(op), @__pointer(op))) |
| 616 |
return 0 |
| 617 |
} |
| 618 |
// We'd like to hash 'key' here, but we can't (easily). Python |
| 619 |
// randomizes its hash function at python process startup. So, |
| 620 |
// instead of hashing the key and then searching for that hash in |
| 621 |
// the dictionary, we'll do a linear search of the dictionary for |
| 622 |
// the key. |
| 623 |
n = @Py2DictObject(op)->ma_mask; |
| 624 |
for (i = 0; i <= n; i++) { |
| 625 |
entry_ptr = &@Py2DictObject(op)->ma_table[i] |
| 626 |
if (@Py2DictEntry(entry_ptr)->me_key == 0 |
| 627 |
|| @Py2DictEntry(entry_ptr)->me_value == 0 |
| 628 |
|| (! @Py2Int_Check(@Py2DictEntry(entry_ptr)->me_key) |
| 629 |
&& ! @Py2Long_Check(@Py2DictEntry(entry_ptr)->me_key))) |
| 630 |
continue |
| 631 |
if ((@Py2Int_Check(@Py2DictEntry(entry_ptr)->me_key) |
| 632 |
&& @Py2Int_AS_LONG(@Py2DictEntry(entry_ptr)->me_key) == key) |
| 633 |
|| (@Py2Long_Check(@Py2DictEntry(entry_ptr)->me_key) |
| 634 |
&& Py2Long_AsLongLong(@Py2DictEntry(entry_ptr)->me_key) |
| 635 |
== key)) |
| 636 |
return @Py2DictEntry(entry_ptr)->me_value |
| 637 |
} |
| 638 |
return 0 |
| 639 |
} |
| 640 |
|
| 641 |
private function Py2Dict_Repr:string(object:long) |
| 642 |
{ |
| 643 |
if (!@Py2Dict_Check(object)) { |
| 644 |
error(sprintf("Py2Dict_Repr called on %s object at %p", |
| 645 |
python2_get_typename(object), @__pointer(object))) |
| 646 |
return "" |
| 647 |
} |
| 648 |
n = @Py2DictObject(object)->ma_mask; |
| 649 |
if (n == 0) |
| 650 |
return "{}" |
| 651 |
|
| 652 |
retstr = "{" |
| 653 |
first = 1 |
| 654 |
for (i = 0; i <= n; i++) { |
| 655 |
entry_ptr = &@Py2DictObject(object)->ma_table[i] |
| 656 |
if (@Py2DictEntry(entry_ptr)->me_key == 0 |
| 657 |
|| @Py2DictEntry(entry_ptr)->me_value == 0) |
| 658 |
continue |
| 659 |
|
| 660 |
if (! first) |
| 661 |
retstr .= " " |
| 662 |
first = 0 |
| 663 |
retstr .= Py2Object_Repr(@Py2DictEntry(entry_ptr)->me_key) |
| 664 |
retstr .= ":" |
| 665 |
retstr .= Py2Object_Repr(@Py2DictEntry(entry_ptr)->me_value) |
| 666 |
} |
| 667 |
retstr .= "}" |
| 668 |
return retstr |
| 669 |
} |
| 670 |
|
| 671 |
# |
| 672 |
# Systemtap functions based on C functions in classobject.c. |
| 673 |
# |
| 674 |
|
| 675 |
private function Py2Class_Lookup:long(op:long, name:string) |
| 676 |
{ |
| 677 |
v = Py2Dict_GetItem(@Py2ClassObject(op)->cl_dict, name) |
| 678 |
if (v != 0) |
| 679 |
return v |
| 680 |
|
| 681 |
n = Py2Tuple_Size(@Py2ClassObject(op)->cl_bases) |
| 682 |
for (i = 0; i < n; i++) { |
| 683 |
v = Py2Class_Lookup(Py2Tuple_GetItem(@Py2ClassObject(op)->cl_bases, i), |
| 684 |
name) |
| 685 |
if (v != 0) |
| 686 |
return v |
| 687 |
} |
| 688 |
return 0 |
| 689 |
} |
| 690 |
|
| 691 |
private function Py2Class_GetAttr:long(op:long, name:string) |
| 692 |
{ |
| 693 |
if (!@Py2Class_Check(op)) { |
| 694 |
error(sprintf("Py2Class_GetAttr called on %s object at %p", |
| 695 |
python2_get_typename(op), @__pointer(op))) |
| 696 |
return 0 |
| 697 |
} |
| 698 |
if (substr(name, 0, 2) == "__") { |
| 699 |
if (name == "__dict__") |
| 700 |
return @Py2ClassObject(op)->cl_dict |
| 701 |
if (name == "__bases__") |
| 702 |
return @Py2ClassObject(op)->cl_bases |
| 703 |
if (name == "__name__") { |
| 704 |
if (@Py2ClassObject(op)->cl_name == 0) |
| 705 |
return 0 |
| 706 |
return @Py2ClassObject(op)->cl_name |
| 707 |
} |
| 708 |
} |
| 709 |
v = Py2Class_Lookup(op, name) |
| 710 |
if (v == 0) { |
| 711 |
error(sprintf("class %s has no attribute '%s'", |
| 712 |
Py2String_AsString(@Py2ClassObject(op)->cl_name), |
| 713 |
name)) |
| 714 |
return 0 |
| 715 |
} |
| 716 |
return v |
| 717 |
} |
| 718 |
|
| 719 |
private function Py2Class_Repr:string(object:long) |
| 720 |
{ |
| 721 |
if (!@Py2Class_Check(object)) { |
| 722 |
error(sprintf("Py2Class_Repr called on %s object at %p", |
| 723 |
python2_get_typename(object), @__pointer(object))) |
| 724 |
return "" |
| 725 |
} |
| 726 |
mod = Py2Dict_GetItem(@Py2ClassObject(object)->cl_dict, "__module__") |
| 727 |
if (@Py2ClassObject(object)->cl_name == 0 |
| 728 |
|| !@Py2String_Check(@Py2ClassObject(object)->cl_name)) |
| 729 |
name = "?" |
| 730 |
else |
| 731 |
name = Py2String_AsString(@Py2ClassObject(object)->cl_name) |
| 732 |
if (mod == 0 || !@Py2String_Check(mod)) |
| 733 |
return sprintf("<class ?.%s at %p>", name, @__pointer(object)) |
| 734 |
else |
| 735 |
return sprintf("<class %s.%s at %p>", name, |
| 736 |
Py2String_AsString(mod), @__pointer(object)) |
| 737 |
} |
| 738 |
|
| 739 |
private function Py2Instance_GetAttr:long(inst:long, name:string) |
| 740 |
{ |
| 741 |
if (!@Py2Instance_Check(inst)) { |
| 742 |
error(sprintf("Py2Instance_GetAttr called on %s object at %p", |
| 743 |
python2_get_typename(inst), @__pointer(inst))) |
| 744 |
return 0 |
| 745 |
} |
| 746 |
if (substr(name, 0, 2) == "__") { |
| 747 |
if (name == "__dict__") |
| 748 |
return @Py2InstanceObject(inst)->in_dict |
| 749 |
if (name == "__class__") |
| 750 |
return @Py2InstanceObject(inst)->in_class |
| 751 |
} |
| 752 |
|
| 753 |
v = Py2Dict_GetItem(@Py2InstanceObject(inst)->in_dict, name) |
| 754 |
if (v != 0) |
| 755 |
return v |
| 756 |
v = Py2Class_Lookup(@Py2InstanceObject(inst)->in_class, name) |
| 757 |
if (v == 0) { |
| 758 |
error(sprintf("%s instance has no attribute '%s'", |
| 759 |
Py2String_AsString(@Py2InstanceObject(inst)->in_class->cl_name), |
| 760 |
name)) |
| 761 |
return 0 |
| 762 |
} |
| 763 |
return v |
| 764 |
} |
| 765 |
|
| 766 |
private function Py2Instance_Repr:string(object:long) |
| 767 |
{ |
| 768 |
if (!@Py2Instance_Check(object)) { |
| 769 |
error(sprintf("Py2Instance_Repr called on %s object at %p", |
| 770 |
python2_get_typename(object), @__pointer(object))) |
| 771 |
return "" |
| 772 |
} |
| 773 |
|
| 774 |
# Note that class instances can have custom '__repr__' |
| 775 |
# functions. But, we can't call them. We'll just use the default |
| 776 |
# 'repr' logic. |
| 777 |
|
| 778 |
classname = @Py2InstanceObject(object)->in_class->cl_name |
| 779 |
mod = Py2Dict_GetItem(@Py2InstanceObject(object)->in_class->cl_dict, |
| 780 |
"__module__") |
| 781 |
|
| 782 |
if (classname == 0 |
| 783 |
|| !@Py2String_Check(classname)) |
| 784 |
cname = "?" |
| 785 |
else |
| 786 |
cname = Py2String_AsString(classname) |
| 787 |
if (mod == 0 || !@Py2String_Check(mod)) |
| 788 |
return sprintf("<?.%s instance at %p>", cname, @__pointer(object)) |
| 789 |
else |
| 790 |
return sprintf("<%s.%s instance at %p>", Py2String_AsString(mod), |
| 791 |
cname, @__pointer(object)) |
| 792 |
} |
| 793 |
|
| 794 |
# |
| 795 |
# Systemtap functions based on C functions in object.c. |
| 796 |
# |
| 797 |
|
| 798 |
function Py2Object_Repr:string(object:long) |
| 799 |
{ |
| 800 |
if (object == 0) |
| 801 |
return "<NULL>" |
| 802 |
if (@Py2String_Check(object)) |
| 803 |
return sprintf("\"%s\"", Py2String_AsString(object)) |
| 804 |
if (@Py2Unicode_Check(object)) |
| 805 |
return sprintf("\"%s\"", Py2Unicode_AsASCIIString(object)) |
| 806 |
if (@Py2Int_Check(object)) |
| 807 |
return sprintf("%#x", Py2Int_AsLong(object)) |
| 808 |
if (@Py2Long_Check(object)) |
| 809 |
return sprintf("%#x", Py2Long_AsLongLong(object)) |
| 810 |
if (@Py2Tuple_Check(object)) |
| 811 |
return Py2Tuple_Repr(object) |
| 812 |
if (@Py2List_Check(object)) |
| 813 |
return Py2List_Repr(object) |
| 814 |
if (@Py2Dict_Check(object)) |
| 815 |
return Py2Dict_Repr(object) |
| 816 |
if (@Py2Instance_Check(object)) |
| 817 |
return Py2Instance_Repr(object) |
| 818 |
if (@Py2Class_Check(object)) |
| 819 |
return Py2Class_Repr(object) |
| 820 |
return sprintf("<%s object at %p>", python2_get_typename(object), |
| 821 |
@__pointer(object)) |
| 822 |
} |
| 823 |
|
| 824 |
# This function isn't really based on a function in object.c, but |
| 825 |
# it is based on Py2Object_Repr(). |
| 826 |
private function Py2Object_Repr:string(object:long, index:long) |
| 827 |
{ |
| 828 |
if (object == 0) |
| 829 |
return "<NULL>" |
| 830 |
if (@Py2String_Check(object)) |
| 831 |
return sprintf("\"%c\"", stringat(Py2String_AsString(object), index)) |
| 832 |
if (@Py2Unicode_Check(object)) |
| 833 |
return sprintf("\"%c\"", stringat(Py2Unicode_AsASCIIString(object), |
| 834 |
index)) |
| 835 |
if (@Py2Tuple_Check(object)) |
| 836 |
return Py2Object_Repr(Py2Tuple_GetItem(object, index)) |
| 837 |
if (@Py2List_Check(object)) |
| 838 |
return Py2Object_Repr(Py2List_GetItem(object, index)) |
| 839 |
if (@Py2Dict_Check(object)) |
| 840 |
return Py2Object_Repr(Py2Dict_GetItem_FromLong(object, index)) |
| 841 |
|
| 842 |
error(sprintf("TypeError: '%s' object cannot be indexed", |
| 843 |
python2_get_typename(object))) |
| 844 |
return "" |
| 845 |
} |
| 846 |
|
| 847 |
# This function isn't really based on a function in object.c, but |
| 848 |
# it is based on Py2Object_Repr(). |
| 849 |
private function Py2Object_GetItem:string(object:long, index:long) |
| 850 |
{ |
| 851 |
if (object == 0) { |
| 852 |
error("TypeError: None object cannot be indexed") |
| 853 |
return 0 |
| 854 |
} |
| 855 |
if (@Py2Tuple_Check(object)) |
| 856 |
return Py2Tuple_GetItem(object, index) |
| 857 |
if (@Py2List_Check(object)) |
| 858 |
return Py2List_GetItem(object, index) |
| 859 |
if (@Py2Dict_Check(object)) |
| 860 |
return Py2Dict_GetItem_FromLong(object, index) |
| 861 |
|
| 862 |
error(sprintf("TypeError: '%s' object cannot be indexed", |
| 863 |
python2_get_typename(object))) |
| 864 |
return 0 |
| 865 |
} |
| 866 |
|
| 867 |
function Py2Object_GetAttr:long(object:long, attr:string) |
| 868 |
{ |
| 869 |
if (object == 0) { |
| 870 |
error("The None object has no attributes") |
| 871 |
return 0 |
| 872 |
} |
| 873 |
if (@Py2Instance_Check(object)) |
| 874 |
return Py2Instance_GetAttr(object, attr) |
| 875 |
if (@Py2Class_Check(object)) |
| 876 |
return Py2Class_GetAttr(object, attr) |
| 877 |
|
| 878 |
error(sprintf("The %s object has no attributes", |
| 879 |
python2_get_typename(object))) |
| 880 |
return 0 |
| 881 |
} |
| 882 |
|
| 883 |
# |
| 884 |
# Systemtap support functions for python 2. |
| 885 |
# |
| 886 |
|
| 887 |
/* |
| 888 |
* python_print_backtrace - Print python backtrace |
| 889 |
* |
| 890 |
* Description: This function is equivalent to |
| 891 |
* print(python_sprint_backtrace(frame)), except that deeper stack |
| 892 |
* tracing may be supported. |
| 893 |
*/ |
| 894 |
function python_print_backtrace:long() |
| 895 |
{ |
| 896 |
if ($$$$provider != "HelperSDT2") next; |
| 897 |
frame = $$$arg3; |
| 898 |
printf("Traceback (most recent call first):\n") |
| 899 |
while (frame != 0) { |
| 900 |
lineno = Py2Frame_GetLineNumber(frame) |
| 901 |
f_code = @Py2FrameObject(frame)->f_code |
| 902 |
filename = Py2String_AsString(@Py2CodeObject(f_code)->co_filename) |
| 903 |
name = Py2String_AsString(@Py2CodeObject(f_code)->co_name) |
| 904 |
|
| 905 |
# Quit when we make it back to the HelperSDT module. |
| 906 |
if (isinstr(filename, "/HelperSDT/")) |
| 907 |
break; |
| 908 |
|
| 909 |
printf(" File \"%s\", line %d, in %s\n", filename, lineno, name) |
| 910 |
# NB: We'd like to print the source line here as python does, |
| 911 |
# but we can't. Python opens up the file and finds the |
| 912 |
# appropriate line, but we can't really do that when we're in |
| 913 |
# the kernel. |
| 914 |
|
| 915 |
frame = @Py2FrameObject(frame)->f_back |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
/* |
| 920 |
* python_sprint_backtrace - Get python backtrace |
| 921 |
* |
| 922 |
* Description: This function returns a string containing a python |
| 923 |
* backtrace. Output may be truncated as per maximum string length |
| 924 |
* (MAXSTRINGLEN). |
| 925 |
*/ |
| 926 |
function python_sprint_backtrace:string() |
| 927 |
{ |
| 928 |
if ($$$$provider != "HelperSDT2") next; |
| 929 |
frame = $$$arg3; |
| 930 |
retstr = "Traceback (most recent call first):\n" |
| 931 |
while (frame != 0) { |
| 932 |
lineno = Py2Frame_GetLineNumber(frame) |
| 933 |
f_code = @Py2FrameObject(frame)->f_code |
| 934 |
filename = Py2String_AsString(@Py2CodeObject(f_code)->co_filename) |
| 935 |
name = Py2String_AsString(@Py2CodeObject(f_code)->co_name) |
| 936 |
|
| 937 |
# Quit when we make it back to the HelperSDT module. |
| 938 |
if (isinstr(filename, "/HelperSDT/")) |
| 939 |
break; |
| 940 |
|
| 941 |
retstr .= sprintf(" File \"%s\", line %d, in %s\n", filename, |
| 942 |
lineno, name) |
| 943 |
|
| 944 |
frame = @Py2FrameObject(frame)->f_back |
| 945 |
} |
| 946 |
return retstr |
| 947 |
} |
| 948 |
|
| 949 |
/* |
| 950 |
* python2_get_typename - Get python object type name |
| 951 |
* |
| 952 |
* Description: This function returns a string describing the type of |
| 953 |
* a python object. |
| 954 |
*/ |
| 955 |
private function python2_get_typename:string(obj:long) |
| 956 |
{ |
| 957 |
if (obj == 0) |
| 958 |
return "" |
| 959 |
return user_string(@Py2_TYPE(obj)->tp_name) |
| 960 |
} |
| 961 |
|
| 962 |
/* |
| 963 |
* python2_get_locals - Get python local variables |
| 964 |
* |
| 965 |
* Description: This function returns a list of python local variables |
| 966 |
* from a frame. |
| 967 |
* |
| 968 |
* frame: python frame object pointer |
| 969 |
* flags: 0: function parameters only ($$parms), 1: locals only |
| 970 |
* ($$locals), 2: all local vars, parameters and locals ($$vars) |
| 971 |
* |
| 972 |
* Note that users shouldn't call this directly, they should refer to |
| 973 |
* '$$parms', '$$locals', and '$$vars' in a python probe and the |
| 974 |
* translator will generate code that calls this function with the |
| 975 |
* correct arguments. |
| 976 |
*/ |
| 977 |
function python2_get_locals:string(frame:long, flags:long) |
| 978 |
{ |
| 979 |
f_code = @Py2FrameObject(frame)->f_code |
| 980 |
|
| 981 |
# flags == 2: get all variables |
| 982 |
if (flags == 2) { |
| 983 |
i = 0 |
| 984 |
n = @Py2CodeObject(f_code)->co_nlocals |
| 985 |
} |
| 986 |
else { |
| 987 |
n = @Py2CodeObject(f_code)->co_argcount |
| 988 |
if (@Py2CodeObject(f_code)->co_flags & %{ Py2_CO_VARARGS %}) |
| 989 |
n++ |
| 990 |
if (@Py2CodeObject(f_code)->co_flags & %{ Py2_CO_VARKEYWORDS %}) |
| 991 |
n++ |
| 992 |
|
| 993 |
# flags == 0 (parms only): 0 to n (argcount) |
| 994 |
if (flags == 0) |
| 995 |
i = 0 |
| 996 |
# flags == 1 (locals only): n (argcount) to max |
| 997 |
else if (flags == 1) { |
| 998 |
i = n |
| 999 |
n = @Py2CodeObject(f_code)->co_nlocals |
| 1000 |
} |
| 1001 |
} |
| 1002 |
if (i < 0 || i > n) { |
| 1003 |
error(sprintf("python2_get_locals: invalid indicies (%d-%d)", i, n)) |
| 1004 |
return "" |
| 1005 |
} |
| 1006 |
|
| 1007 |
# Each element in co_varnames is a tuple of strings. The values |
| 1008 |
# are in f_localsplus. |
| 1009 |
p = @Py2CodeObject(f_code)->co_varnames |
| 1010 |
localsplus = @Py2FrameObject(frame)->f_localsplus |
| 1011 |
first = 1 |
| 1012 |
for (; i < n; i++) { |
| 1013 |
if (! first) |
| 1014 |
retstr .= " " |
| 1015 |
first = 0 |
| 1016 |
key_obj = Py2Tuple_GetItem(p, i) |
| 1017 |
value_obj = user_long(localsplus + (i * %{ sizeof(intptr_t) %})) |
| 1018 |
retstr .= sprintf("%s=%s", Py2String_AsString(key_obj), |
| 1019 |
Py2Object_Repr(value_obj)) |
| 1020 |
} |
| 1021 |
return retstr |
| 1022 |
} |
| 1023 |
|
| 1024 |
function python2_get_var_obj:long(frame:long, var:string) |
| 1025 |
{ |
| 1026 |
f_code = @Py2FrameObject(frame)->f_code |
| 1027 |
n = @Py2CodeObject(f_code)->co_nlocals |
| 1028 |
|
| 1029 |
# Each element in co_varnames is a tuple of strings. The values |
| 1030 |
# are in f_localsplus. |
| 1031 |
p = @Py2CodeObject(f_code)->co_varnames |
| 1032 |
localsplus = @Py2FrameObject(frame)->f_localsplus |
| 1033 |
for (i = 0; i < n; i++) { |
| 1034 |
key_obj = Py2Tuple_GetItem(p, i) |
| 1035 |
if (var == Py2String_AsString(key_obj)) { |
| 1036 |
return user_long(localsplus + (i * %{ sizeof(intptr_t) %})) |
| 1037 |
} |
| 1038 |
} |
| 1039 |
|
| 1040 |
# If we can't find it in the locals, we look in the globals. |
| 1041 |
f_globals = @Py2FrameObject(frame)->f_globals |
| 1042 |
value_obj = Py2Dict_GetItem(f_globals, var) |
| 1043 |
if (value_obj == 0) { |
| 1044 |
error(sprintf("Python variable '%s' cannot be found", var)) |
| 1045 |
return 0 |
| 1046 |
} |
| 1047 |
return value_obj |
| 1048 |
} |
| 1049 |
|
| 1050 |
function python2_get_var_obj:long(frame:long, var:string, index:long) |
| 1051 |
{ |
| 1052 |
f_code = @Py2FrameObject(frame)->f_code |
| 1053 |
n = @Py2CodeObject(f_code)->co_nlocals |
| 1054 |
|
| 1055 |
# Each element in co_varnames is a tuple of strings. The values |
| 1056 |
# are in f_localsplus. |
| 1057 |
p = @Py2CodeObject(f_code)->co_varnames |
| 1058 |
localsplus = @Py2FrameObject(frame)->f_localsplus |
| 1059 |
for (i = 0; i < n; i++) { |
| 1060 |
key_obj = Py2Tuple_GetItem(p, i) |
| 1061 |
if (var == Py2String_AsString(key_obj)) { |
| 1062 |
return user_long(localsplus + (i * %{ sizeof(intptr_t) %})) |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
# If we can't find it in the locals, we look in the globals. |
| 1067 |
f_globals = @Py2FrameObject(frame)->f_globals |
| 1068 |
value_obj = Py2Dict_GetItem(f_globals, var) |
| 1069 |
if (value_obj == 0) { |
| 1070 |
error(sprintf("Python variable '%s' cannot be found", var)) |
| 1071 |
return 0 |
| 1072 |
} |
| 1073 |
return value_obj |
| 1074 |
} |