Skip to content

Commit 4591168

Browse files
committed
Run profile on api.debugger entry
1 parent 6e3aae2 commit 4591168

5 files changed

Lines changed: 138 additions & 15 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/python
2+
"""Greatest Common Divisor
3+
4+
Some characterstics of this program used for testing check_args() does
5+
not have a 'return' statement.
6+
7+
check_args() raises an uncaught exception when given the wrong number
8+
of parameters.
9+
10+
"""
11+
import sys
12+
13+
from trepan.api import debug
14+
15+
16+
def check_args():
17+
if len(sys.argv) != 3:
18+
# Rather than use sys.exit let's just raise an error
19+
raise Exception("Need to give two numbers")
20+
for i in range(2):
21+
try:
22+
sys.argv[i + 1] = int(sys.argv[i + 1])
23+
except ValueError:
24+
print("** Expecting an integer, got: %s" % repr(sys.argv[i]))
25+
sys.exit(2)
26+
27+
28+
def gcd(a, b):
29+
"""GCD. We assume positive numbers"""
30+
31+
# Make: a <= b
32+
if a > b:
33+
(a, b) = (b, a)
34+
35+
if a <= 0:
36+
debug(step_ignore=0)
37+
return None
38+
if a == 1 or b - a == 0:
39+
debug(start_opts={"startup-profile": True})
40+
return a
41+
return gcd(b - a, a)
42+
43+
44+
if __name__ == "__main__":
45+
check_args()
46+
47+
(a, b) = sys.argv[1:3]
48+
print("The GCD of %d and %d is %d" % (a, b, gcd(a, b)))
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/python
2+
"""Greatest Common Divisor
3+
4+
Some characterstics of this program used for testing check_args() does
5+
not have a 'return' statement.
6+
7+
check_args() raises an uncaught exception when given the wrong number
8+
of parameters.
9+
10+
"""
11+
import sys
12+
13+
from trepan.api import debug
14+
15+
16+
def check_args():
17+
if len(sys.argv) != 3:
18+
# Rather than use sys.exit let's just raise an error
19+
raise Exception("Need to give two numbers")
20+
for i in range(2):
21+
try:
22+
sys.argv[i + 1] = int(sys.argv[i + 1])
23+
except ValueError:
24+
print("** Expecting an integer, got: %s" % repr(sys.argv[i]))
25+
sys.exit(2)
26+
27+
28+
def gcd(a, b):
29+
"""GCD. We assume positive numbers"""
30+
31+
# Make: a <= b
32+
if a > b:
33+
(a, b) = (b, a)
34+
35+
if a <= 0:
36+
debug(step_ignore=0)
37+
return None
38+
if a == 1 or b - a == 0:
39+
debug(start_opts={"startup-profile": False})
40+
return a
41+
return gcd(b - a, a)
42+
43+
44+
if __name__ == "__main__":
45+
check_args()
46+
47+
(a, b) = sys.argv[1:3]
48+
print("The GCD of %d and %d is %d" % (a, b, gcd(a, b)))

test/example/gcd-dbgcall.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,39 @@
99
1010
"""
1111
import sys
12+
1213
from trepan.api import debug
1314

15+
1416
def check_args():
1517
if len(sys.argv) != 3:
1618
# Rather than use sys.exit let's just raise an error
1719
raise Exception("Need to give two numbers")
1820
for i in range(2):
1921
try:
20-
sys.argv[i+1] = int(sys.argv[i+1])
22+
sys.argv[i + 1] = int(sys.argv[i + 1])
2123
except ValueError:
2224
print("** Expecting an integer, got: %s" % repr(sys.argv[i]))
2325
sys.exit(2)
2426

25-
def gcd(a,b):
26-
""" GCD. We assume positive numbers"""
27+
28+
def gcd(a, b):
29+
"""GCD. We assume positive numbers"""
2730

2831
# Make: a <= b
2932
if a > b:
30-
(a, b) = (b, a)
33+
(a, b) = (b, a)
3134

3235
if a <= 0:
3336
debug(step_ignore=0)
3437
return None
35-
if a == 1 or b-a == 0:
36-
debug(start_opts={'startup-profile': True})
38+
if a == 1 or b - a == 0:
39+
debug()
3740
return a
38-
return gcd(b-a, a)
41+
return gcd(b - a, a)
42+
3943

40-
if __name__=='__main__':
44+
if __name__ == "__main__":
4145
check_args()
4246

4347
(a, b) = sys.argv[1:3]

trepan/api.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,21 +211,43 @@ def debug(
211211
`dbg_opts' is an optional "options" dictionary that gets fed
212212
trepan.Debugger(); `start_opts' are the optional "options"
213213
dictionary that gets fed to trepan.Debugger.core.start()."""
214+
215+
# We have a global debugger_obj that we reuse
214216
global debugger_obj
217+
218+
# A list of debugger profiles we might run
219+
dbg_initfiles = []
220+
215221
if debugger_obj is None:
222+
# If debugger_obj has not been set this is the first time
223+
# we are entering the debugger.
224+
# create the object, and set to run the user's
225+
# debugger initialization profile
226+
216227
debugger_obj = Trepan(dbg_opts)
217228
debugger_obj.core.add_ignore(debug, stop)
229+
230+
# Run user profile if first time and we haven't
231+
# explicit set to ignore profile loading.
232+
if not start_opts or start_opts.get("startup-profile", True):
233+
from trepan.options import add_startup_file
234+
235+
add_startup_file(dbg_initfiles)
236+
218237
pass
238+
219239
core = debugger_obj.core
220240
frame = sys._getframe(0 + level)
221241
core.set_next(frame)
222-
if start_opts and "startup-profile" in start_opts and start_opts["startup-profile"]:
223-
dbg_initfiles = start_opts["startup-profile"]
224-
from trepan import options
225242

226-
options.add_startup_file(dbg_initfiles)
227-
for init_cmdfile in dbg_initfiles:
228-
core.processor.queue_startfile(init_cmdfile)
243+
# If we've specified profile loading, add that
244+
if start_opts and start_opts.get("startup-profile", False):
245+
from trepan.options import add_startup_file
246+
247+
add_startup_file(dbg_initfiles)
248+
249+
for init_cmdfile in dbg_initfiles:
250+
core.processor.queue_startfile(init_cmdfile)
229251

230252
if not core.is_started():
231253
core.start(start_opts)

trepan/options.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from pygments.styles import STYLE_MAP
2424

2525
import trepan.api
26-
from trepan.api import debugger_on_post_mortem
2726
from trepan.clifns import path_expanduser_abs
2827
from trepan.inout.output import DebuggerUserOutput
2928
from trepan.lib.file import readable
@@ -420,6 +419,8 @@ def postprocess_options(dbg, opts):
420419
# dbg.cmdqueue = list(opts.execute.split(';;'))
421420

422421
if opts.post_mortem:
422+
from trepan.api import debugger_on_post_mortem
423+
423424
debugger_on_post_mortem()
424425
pass
425426
return

0 commit comments

Comments
 (0)