forked from dc-aalto/atmoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugging.py
More file actions
150 lines (121 loc) · 3.56 KB
/
Copy pathdebugging.py
File metadata and controls
150 lines (121 loc) · 3.56 KB
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
# -*- coding: iso-8859-15 -*-
# (c) 2011-2012 Roland Kindermann
#
# This file is part of the Aalto Timed Model Checker ATMOC.
#
# ATMOC is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ATMOC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ATMOC. If not, see <http://www.gnu.org/licenses/>.
print
print
strn = '*** Using debugging module ***'
print ' ' * ((80 - len(strn)) / 2), strn
print
print
import inspect, atexit, os, sys, pdb
###### PROFILING
calls_observed = {}
def trace(frame, event, arg):
print "%s, %s:%d" % (event, frame.f_code.co_filename, frame.f_lineno)
return trace
def observe(depth):
if not calls_observed:
sys.settrace(trace)
atexit.register(save_observations)
stk = inspect.stack()[1:1+depth]
stk = [(os.path.split(x[1])[-1], x[2], x[3]) for x in stk]
stk += [None,] * (depth - len(stk))
stk = tuple(stk)
calls_observed[stk] = calls_observed.get(stk, 0) + 1
def save_observations():
if calls_observed:
print
print
print 'PARTIAL PROFILE'
for k in sorted(calls_observed, lambda x, y: cmp(tuple((xe[0], xe[2]) for xe in x), tuple((ye[0], ye[2]) for ye in y))):
print hash(tuple(x[2] for x in k))
print '%s\n%d' % ('\t'.join(reversed(map(lambda x : '%s:%d' % (x[2], x[1]), k))), calls_observed[k])
print
###### Recursion detection
REC_WHITELIST = [
# These should be fine as they are not called with high depth during parsing
('expressions.py', '__new__'),
('expressions.py', '_to_ast_argument_list'),
('expressions.py', '_to_ast_operand'),
# These should result from decorated functions calling other decorated functions:
('preproc.py', 'actual_call'),
# These are used in two contexts: debugging (in which slowness is ok) and to print the properties (which in their original form should not be too large)
('expressions.py', 'string'),
('expressions.py', 'element_str')
]
def _rec_trace(frame, event, arg):
if event != 'call':
return
stk = []
cur = frame
dns = []
while cur != None:
code = cur.f_code
fn = code.co_filename
dn, sfn = os.path.split(fn)
name = code.co_name
stk.append((sfn, code.co_firstlineno, name))
dns.append(dn)
cur = cur.f_back
if os.path.abspath(dns[0]) == os.path.abspath(os.getcwd()): # only own code
if (not (stk[0][0], stk[0][2]) in REC_WHITELIST) and stk[0] in stk[1:]:
print '\n'.join('%s %d: %s' % x for x in reversed(stk))
print
return _rec_trace
def recursion_detection():
sys.settrace(_rec_trace)
###### STDOUT debugging
notify_strns = []
class TW(object):
def __init__(self, org):
self.org = org
def write(self, what):
if any(x in what for x in notify_strns):
raise Exception("here")
self.org.write(what)
def flush(self):
self.org.flush()
def notify_stdout(strn):
if not notify_strns:
sys.stdout = TW(sys.stdout)
notify_strns.append(strn)
###### Hit cnt bp
_dbg_hits = 0
def hit_bp(arg=None):
global _dbg_hits
_dbg_hits += 1
print "Hit debug point", _dbg_hits
if _dbg_hits == arg:
pdb.set_trace()
###### Decorator
def PrintRet(func):
def rf(*args):
ret = func(*args)
print 'return', ret
return ret
return rf
if __name__ == '__main__':
def a():
observe(2)
def b():
a()
a()
a()
a()
b()
b()
b()