Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/bin/sage-CSI-helper.py
8817 views
1
# Run by ``sage-CSI`` in gdb's Python interpreter.
2
3
import os
4
import sys
5
import glob
6
7
import gdb
8
from Cython.Debugger import libpython, libcython
9
from Cython.Debugger.libcython import cy, CythonCommand, CyGDBError
10
11
try:
12
if not color:
13
libcython.pygments = None # disable escape-sequence coloring
14
except (NameError, AttributeError):
15
pass
16
17
18
def cython_debug_files():
19
"""
20
Cython extra debug information files
21
"""
22
pattern = os.path.join(os.environ['SAGE_SRC'], 'cython_debug',
23
'cython_debug_info_*')
24
return glob.glob(pattern)
25
26
27
28
print '\n\n'
29
print 'Cython backtrace'
30
print '----------------'
31
32
# The Python interpreter in GDB does not do automatic backtraces for you
33
try:
34
35
for f in cython_debug_files():
36
cy.import_.invoke(f, None)
37
38
39
class SageBacktrace(CythonCommand):
40
name = 'cy fullbt'
41
alias = 'cy full_backtrace'
42
command_class = gdb.COMMAND_STACK
43
completer_class = gdb.COMPLETE_NONE
44
cy = cy
45
46
def print_stackframe(self, frame, index, is_c=False):
47
if not is_c and self.is_python_function(frame):
48
pyframe = libpython.Frame(frame).get_pyop()
49
if pyframe is None or pyframe.is_optimized_out():
50
# print this python function as a C function
51
return self.print_stackframe(frame, index, is_c=True)
52
func_name = pyframe.co_name
53
func_cname = 'PyEval_EvalFrameEx'
54
func_args = []
55
elif self.is_cython_function(frame):
56
cyfunc = self.get_cython_function(frame)
57
f = lambda arg: self.cy.cy_cvalue.invoke(arg, frame=frame)
58
59
func_name = cyfunc.name
60
func_cname = cyfunc.cname
61
func_args = [] # [(arg, f(arg)) for arg in cyfunc.arguments]
62
else:
63
func_name = frame.name()
64
func_cname = func_name
65
func_args = []
66
67
try:
68
gdb_value = gdb.parse_and_eval(func_cname)
69
except (RuntimeError, TypeError):
70
func_address = 0
71
else:
72
func_address = int(str(gdb_value.address).split()[0], 0)
73
74
source_desc, lineno = self.get_source_desc(frame)
75
a = ', '.join('%s=%s' % (name, val) for name, val in func_args)
76
print '#%-2d 0x%016x in %s(%s)' % (index, func_address, func_name, a),
77
if source_desc.filename is not None:
78
print 'at %s:%s' % (source_desc.filename, lineno),
79
print
80
try:
81
source = source_desc.get_source(lineno - 5, lineno + 5,
82
mark_line=lineno, lex_entire=True)
83
print source
84
except gdb.GdbError:
85
pass
86
87
88
def invoke(self, args, from_tty):
89
self.newest_first_order(args, from_tty)
90
91
92
def newest_first_order(self, args, from_tty):
93
frame = gdb.newest_frame()
94
index = 0
95
while frame:
96
frame.select()
97
self.print_stackframe(frame, index)
98
index += 1
99
frame = frame.older()
100
101
102
def newest_last_order(self, args, from_tty):
103
frame = gdb.newest_frame()
104
n_frames = 0;
105
while frame.older():
106
frame = frame.older()
107
n_frames += 1
108
index = 0
109
while frame:
110
frame.select()
111
self.print_stackframe(frame, index)
112
index += 1
113
frame = frame.newer()
114
115
116
trace = SageBacktrace.register()
117
trace.invoke(None, None)
118
119
120
except Exception as e:
121
print 'Exception: ', type(e), e
122
import traceback
123
traceback.print_exc()
124
125
sys.stdout.flush()
126
127