Debian Codegraph @

rpython/rlib/rvmprof/traceback.py

raw symbols SCIP
1 """
2 Semi-public interface to gather and print a raw traceback, e.g.
3 from the faulthandler module.
4 """
5
6 from rpython.rlib.rvmprof import cintf, rvmprof
7 from rpython.rlib.objectmodel import specialize
8 from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
9 from rpython.rlib.rarithmetic import widen
10
11
12 def traceback(estimate_number_of_entries):
13 """Build and return a vmprof-like traceback, as a pair (array_p,
14 array_length). The caller must free array_p. Not for signal handlers:
15 for these, call vmprof_get_traceback() from C code.
16 """
17 if not cintf.IS_SUPPORTED:
18 return (None, 0)
19 _cintf = rvmprof._get_vmprof().cintf
20 size = estimate_number_of_entries * 2 + 4
21 stack = cintf.get_rvmprof_stack()
22 array_p = lltype.malloc(rffi.SIGNEDP.TO, size, flavor='raw')
23 NULL = llmemory.NULL
24 voidpp = rffi.cast(rffi.VOIDPP, array_p)
25 array_length = _cintf.vmprof_get_traceback(stack, NULL, voidpp, size)
26 return (array_p, array_length)
27
28
29 LOC_INTERPRETED = 0
30 LOC_JITTED = 1
31 LOC_JITTED_INLINED = 2
32
33
34 @specialize.arg(0, 1)
35 def _traceback_one(CodeClass, callback, arg, code_id, loc):
36 found_code = None
37 code_id = widen(code_id)
38 if code_id != 0:
39 all_code_wrefs = CodeClass._vmprof_weak_list.get_all_handles()
40 i = len(all_code_wrefs) - 1
41 while i >= 0:
42 code = all_code_wrefs[i]()
43 if code is not None and code._vmprof_unique_id == code_id:
44 found_code = code
45 break
46 i -= 1
47 callback(found_code, loc, arg)
48
49 @specialize.arg(0, 1)
50 def walk_traceback(CodeClass, callback, arg, array_p, array_length):
51 """Invoke 'callback(code_obj, loc, arg)' for every traceback entry.
52 'code_obj' may be None if it can't be determined. 'loc' is one
53 of the LOC_xxx constants.
54 """
55 if not cintf.IS_SUPPORTED:
56 return
57 i = 0
58 array_length = widen(array_length)
59 array_p = rffi.cast(rffi.INTPTR_TP, array_p)
60 while i < array_length - 1:
61 tag = array_p[i]
62 tagged_value = array_p[i + 1]
63 if tag == rvmprof.VMPROF_CODE_TAG:
64 loc = LOC_INTERPRETED
65 _traceback_one(CodeClass, callback, arg, tagged_value, loc)
66 elif tag == rvmprof.VMPROF_JITTED_TAG:
67 if i + 2 >= array_length: # skip last entry, can't determine if
68 break # it's LOC_JITTED_INLINED or LOC_JITTED
69 if widen(array_p[i + 2]) == rvmprof.VMPROF_JITTED_TAG:
70 loc = LOC_JITTED_INLINED
71 else:
72 loc = LOC_JITTED
73 _traceback_one(CodeClass, callback, arg, tagged_value, loc)
74 i += 2