pdb.run(statement[, globals[, locals]])
pdb.runeval(expression[, globals[, locals]])
pdb.runcall(function[, argument, ...])
pdb.set_trace()
pdb.post_mortem(traceback)
pdb.pm()
#!/usr/bin/env python
import pdb
def test_debugger(some_int):
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int
if __name__ == "__main__":
pdb.run("test_debugger(0)")
#!/usr/bin/env python
import pdb
def test_debugger(some_int):
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int
if __name__ == "__main__":
pdb.runeval("test_debugger(0)")
#!/usr/bin/env python
import pdb
def test_debugger(some_int):
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int
if __name__ == "__main__":
pdb.runcall(test_debugger, 0)
#!/usr/bin/env python
import pdb
def test_debugger(some_int):
pdb.set_trace()
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int
if __name__ == "__main__":
test_debugger(0)
#!/usr/bin/env python
import pdb
def test_debugger(some_int):
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int
if __name__ == "__main__":
try:
test_debugger(0)
except:
import sys
tb = sys.exc_info()[2]
pdb.post_mortem(tb)
#!/usr/bin/env python
import pdb
import sys
def test_debugger(some_int):
print "start some_int>>", some_int
return_int = 10 / some_int
print "end some_int>>", some_int
return return_int
def do_debugger(type, value, tb):
pdb.pm()
if __name__ == "__main__":
sys.excepthook = do_debugger
test_debugger(0)
#!/usr/bin/env python
import pdb
def f1(some_arg):
print some_arg
some_other_arg = some_arg + 1
return f2(some_other_arg)
def f2(some_arg):
print some_arg
some_other_arg = some_arg + 1
return f3(some_other_arg)
def f3(some_arg):
print some_arg
some_other_arg = some_arg + 1
return f4(some_other_arg)
def f4(some_arg):
print some_arg
some_other_arg = some_arg + 1
return some_other_arg
if __name__ == "__main__":
pdb.runcall(f1, 1)
jmjones@bean:~/debugger $ python simple_debugger_example.py
> /home/jmjones/debugger/simple_debugger_example.py(8)f1()
-> print some_arg (Pdb)
(Pdb) s
1
> /home/jmjones/svn/articles/debugger/simple_debugger_example.py(9)f1()
-> some_other_arg = some_arg + 1
(Pdb) s
> /home/jmjones/svn/articles/debugger/simple_debugger_example.py(10)f1()
-> return f2(some_other_arg)
(Pdb) s
--Call--
> /home/jmjones/svn/articles/debugger/simple_debugger_example.py(12)f2()
-> def f2(some_arg):
(Pdb) s
> /home/jmjones/svn/articles/debugger/simple_debugger_example.py(13)f2()
-> print some_arg
(Pdb) l
8 print some_arg
9 some_other_arg = some_arg + 1
10 return f2(some_other_arg)
11
12 def f2(some_arg):
13 -> print some_arg
14 some_other_arg = some_arg + 1
15 return f3(some_other_arg)
16
17 def f3(some_arg):
18 print some_arg
(Pdb)
(Pdb) b f4
Breakpoint 1 at /home/jmjones/svn/articles/debugger
/simple_debugger_example.py:22
(Pdb) c
2
3
> /home/jmjones/svn/articles/debugger/simple_debugger_example.py(23)f4()
-> print some_arg
(Pdb) l
18 print some_arg
19 some_other_arg = some_arg + 1
20 return f4(some_other_arg)
21
22 B def f4(some_arg):
23 -> print some_arg
24 some_other_arg = some_arg + 1
25 return some_other_arg
26
27 if __name__ == "__main__":
28 pdb.runcall(f1, 1)
(Pdb)
(Pdb) where
/usr/local/python24/lib/python2.4/bdb.py(404)runcall()
-> res = func(*args, **kwds)
/home/jmjones/svn/articles/debugger/simple_debugger_example.py(10)f1()
-> return f2(some_other_arg)
/home/jmjones/svn/articles/debugger/simple_debugger_example.py(15)f2()
-> return f3(some_other_arg)
/home/jmjones/svn/articles/debugger/simple_debugger_example.py(20)f3()
-> return f4(some_other_arg)
> /home/jmjones/svn/articles/debugger/simple_debugger_example.py(23)f4()
-> print some_arg
(Pdb)
(Pdb) u
> /home/jmjones/debugger/simple_debugger_example.py(20)f3()
-> return f4(some_other_arg)
(Pdb) u
> /home/jmjones/debugger/simple_debugger_example.py(15)f2()
-> return f3(some_other_arg)
(Pdb) u
> /home/jmjones/debugger/simple_debugger_example.py(10)f1()
-> return f2(some_other_arg)
(Pdb) u
> /usr/local/python24/lib/python2.4/bdb.py(404)runcall()
-> res = func(*args, **kwds)
(Pdb) u
*** Oldest frame
(Pdb) l
399 self.reset()
400 sys.settrace(self.trace_dispatch)
401 res = None
402 try:
403 try:
404 -> res = func(*args, **kwds)
405 except BdbQuit:
406 pass
407 finally:
408 self.quitting = 1
409 sys.settrace(None)
(Pdb)
(Pdb) d
> /home/jmjones/svn/articles/debugger/simple_debugger_example.py(10)f1()
-> return f2(some_other_arg)
(Pdb) d
> /home/jmjones/svn/articles/debugger/simple_debugger_example.py(15)f2()
-> return f3(some_other_arg)
(Pdb) l
10 return f2(some_other_arg)
11
12 def f2(some_arg):
13 print some_arg
14 some_other_arg = some_arg + 1
15 -> return f3(some_other_arg)
16
17 def f3(some_arg):
18 print some_arg
19 some_other_arg = some_arg + 1
20 return f4(some_other_arg)
(Pdb)
(Pdb) print some_arg
2
(Pdb) d
> /home/jmjones/svn/articles/debugger/simple_debugger_example.py(20)f3()
-> return f4(some_other_arg)
(Pdb) l
15 return f3(some_other_arg)
16
17 def f3(some_arg):
18 print some_arg
19 some_other_arg = some_arg + 1
20 -> return f4(some_other_arg)
21
22 B def f4(some_arg):
23 print some_arg
24 some_other_arg = some_arg + 1
25 return some_other_arg
(Pdb) print some_arg
3
(Pdb)
(Pdb) u
> /home/jmjones/debugger/simple_debugger_example.py(15)f2()
-> return f3(some_other_arg)
(Pdb) l
10 return f2(some_other_arg)
11
12 def f2(some_arg):
13 print some_arg
14 some_other_arg = some_arg + 1
15 -> return f3(some_other_arg)
16
17 def f3(some_arg):
18 print some_arg
19 some_other_arg = some_arg + 1
20 return f4(some_other_arg)
(Pdb) s
4
> /home/jmjones/debugger/simple_debugger_example.py(24)f4()
-> some_other_arg = some_arg + 1
(Pdb) l
19 some_other_arg = some_arg + 1
20 return f4(some_other_arg)
21
22 B def f4(some_arg):
23 print some_arg
24 -> some_other_arg = some_arg + 1
25 return some_other_arg
26
27 if __name__ == "__main__":
28 pdb.runcall(f1, 1)
[EOF]
(Pdb)
#!/usr/bin/env python
import pdb
import string
import sys
class ConvertToDict:
def __init__(self):
self.tmp_dict = {}
self.return_dict = {}
def walk_string(self, some_string):
"""주어진 텍스트 문자열을 훓어서 사전을 돌려준다.
예외에 마추칠 경우 실체 속성의 상태를 유지한다"""
l = string.split(some_string)
for i in range(len(l)):
key = str(i)
self.tmp_dict[key] = int(l[i])
return_dict = self.tmp_dict
self.return_dict = self.tmp_dict
self.reset()
return return_dict
def reset(self):
"""깨끗이 치운다"""
self.tmp_dict = {}
self.return_dict = {}
def get_number_dict(self, some_string):
"""예외 처리를 여기에서 한다"""
try:
return self.walk_string(some_string)
except:
# 예외에 마추치면, 예외 시점까지 백업된 tmp_dict에 의존하면 된다
return self.tmp_dict
def main():
ctd = ConvertToDict()
for line in file(sys.argv[1]):
line = line.strip()
print "*" * 40
print "line>>", line
print ctd.get_number_dict(line)
print "*" * 40
if __name__ == "__main__":
#pdb.runcall(main)
main()
jmjones@bean:~/debugger $ cat simple_example.data
1234 2345 3456 4567
9876 8765 7654 6543
jmjones@bean:~/debugger $ python example_debugger.py simple_example.data
****************************************
line>> 1234 2345 3456 4567
{"1": 2345, "0": 1234, "3": 4567, "2": 3456}
****************************************
****************************************
line>> 9876 8765 7654 6543
{"1": 8765, "0": 9876, "3": 6543, "2": 7654}
****************************************
jmjones@bean:~/debugger $ cat example_debugger.data
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4
1 2 3 4
jmjones@bean:~/debugger $ python example_debugger.py example_debugger.data
****************************************
line>> 1 2 3 4 5 6 7 8 9 10
{"1": 2, "0": 1, "3": 4, "2": 3, "5": 6, "4": 5, "7": 8, "6": 7, "9": \
10, "8": 9}
****************************************
****************************************
line>> 1 2 3 4 5 6 7 8 9 10
{"1": 2, "0": 1, "3": 4, "2": 3, "5": 6, "4": 5, "7": 8, "6": 7}
****************************************
****************************************
line>> 1 2 3 4
{"1": 2, "0": 1, "3": 4, "2": 3, "5": 6, "4": 5, "7": 8, "6": 7}
****************************************
****************************************
line>> 1 2 3 4
{"1": 2, "0": 1, "3": 4, "2": 3}
****************************************
jmjones@bean:~/debugger $ python example_debugger.py example_debugger.data
> /home/jmjones/debugger/example_debugger.py(35)main()
-> ctd = ConvertToDict()
(Pdb)
(Pdb) l
30 except:
31 #if we hit an exception, we can rely on tmp_dict
being a backup to the point of the exception
32 return self.tmp_dict
33
34 def main():
35 -> ctd = ConvertToDict()
36 for line in file(sys.argv[1]):
37 line = line.strip()
38 print "*" * 40
39 print "line>>", line
40 print ctd.get_number_dict(line)
(Pdb) b 14
Breakpoint 1 at /home/jmjones/debugger/example_debugger.py:14
(Pdb) b 18
Breakpoint 2 at /home/jmjones/debugger/example_debugger.py:18
(Pdb) c
****************************************
line>> 1 2 3 4 5 6 7 8 9 10
> /home/jmjones/svn/home/debugger/example_debugger.py(14)walk_string()
-> l = string.split(some_string)
(Pdb) l
9 self.tmp_dict = {}
10 self.return_dict = {}
11 def walk_string(self, some_string):
12 """walk given text string and return a dictionary.
13 Maintain state in instance attributes in case
we hit an exception"""
14 B-> l = string.split(some_string)
15 for i in range(len(l)):
16 key = str(i)
17 self.tmp_dict[key] = int(l[i])
18 B return_dict = self.tmp_dict
19 self.return_dict = self.tmp_dict
(Pdb) print some_string
1 2 3 4 5 6 7 8 9 10
(Pdb) c
> /home/jmjones/svn/articles/debugger/example_debugger.py(18)walk_string()
-> return_dict = self.tmp_dict
이 시점에서, self.tmp_dict에는 가져야할 모든 것이 확보된다:
(Pdb) print self.tmp_dict
{"1": 2, "0": 1, "3": 4, "2": 3, "5": 6, "4": 5, "7": 8, "6": 7, "9": \
10, "8": 9}
이것은 이상이 없어 보였다. 계속해서 다음 입력 줄을 인쇄해 보았다:
(Pdb) c
{"1": 2, "0": 1, "3": 4, "2": 3, "5": 6, "4": 5, "7": 8, "6": 7, "9": \
10, "8": 9}
****************************************
****************************************
line>> 1 2 3 4 5 6 7 8 9 10
> /home/jmjones/svn/articles/debugger/example_debugger.py(14)walk_string()
-> l = string.split(some_string)
(Pdb) print some_string
1 2 3 4 5 6 7 8 9 10
(Pdb) c
{"1": 2, "0": 1, "3": 4, "2": 3, "5": 6, "4": 5, "7": 8, "6": 7}
****************************************
****************************************
line>> 1 2 3 4
> /home/jmjones/svn/articles/debugger/example_debugger.py(14)walk_string()
-> l = string.split(some_string)
(Pdb) print self.return_dict
{}
(Pdb) print self.tmp_dict
{"1": 2, "0": 1, "3": 4, "2": 3, "5": 6, "4": 5, "7": 8, "6": 7}
#!/usr/bin/env python
import pdb
import string
import sys
class ConvertToDict:
def __init__(self):
self.tmp_dict = {}
self.return_dict = {}
def walk_string(self, some_string):
"""walk given text string and return a dictionary.
Maintain state in instance attributes in case we hit an exception"""
l = string.split(some_string)
for i in range(len(l)):
key = str(i)
self.tmp_dict[key] = int(l[i])
return_dict = self.tmp_dict
self.return_dict = self.tmp_dict
self.reset()
return return_dict
def reset(self):
"""clean up"""
self.tmp_dict = {}
self.return_dict = {}
def get_number_dict(self, some_string):
"""do super duper exception handling here"""
try:
return self.walk_string(some_string)
except:
#modified exception handler - drop us into a debugger
tb = sys.exc_info()[2]
pdb.post_mortem(tb)
#if we hit an exception, we can rely on tmp_dict
being a backup to the point of the exception
return self.tmp_dict
def main():
ctd = ConvertToDict()
for line in file(sys.argv[1]):
line = line.strip()
print "*" * 40
print "line>>", line
print ctd.get_number_dict(line)
print "*" * 40
if __name__ == "__main__":
main()
jmjones@bean:~/debugger $ python example_debugger_pm.py example_debugger.data
****************************************
line>> 1 2 3 4 5 6 7 8 9 10
{"1": 2, "0": 1, "3": 4, "2": 3, "5": 6, "4": 5, "7": 8, "6": 7, "9": \
10, "8": 9}
****************************************
****************************************
line>> 1 2 3 4 5 6 7 8 9 10
> /home/jmjones/debugger/example_debugger_pm.py(17)walk_string()
-> self.tmp_dict[key] = int(l[i])
(Pdb) list
12 """walk given text string and return a dictionary.
13 Maintain state in instance attributes in case
we hit an exception"""
14 l = string.split(some_string)
15 for i in range(len(l)):
16 key = str(i)
17 -> self.tmp_dict[key] = int(l[i])
18 return_dict = self.tmp_dict
19 self.return_dict = self.tmp_dict
20 self.reset()
21 return return_dict
22 def reset(self):
(Pdb) for e in sys.exc_info(): print "EXCEPTION>>>", e
EXCEPTION>>> exceptions.AttributeError
EXCEPTION>>> Pdb instance has no attribute "do_for"
EXCEPTION>>>
(Pdb) import traceback
(Pdb) traceback.print_stack()
File "example_debugger_pm.py", line 48, in ?
main()
File "example_debugger_pm.py", line 44, in main
print ctd.get_number_dict(line)
File "example_debugger_pm.py", line 33, in get_number_dict
pdb.post_mortem(tb)
File "/usr/local/python24/lib/python2.4/pdb.py", line 1009, in post_mortem
p.interaction(t.tb_frame, t)
File "/usr/local/python24/lib/python2.4/pdb.py", line 158, in interaction
self.cmdloop()
File "/usr/local/python24/lib/python2.4/cmd.py", line 142, in cmdloop
stop = self.onecmd(line)
File "/usr/local/python24/lib/python2.4/cmd.py", line 218, in onecmd
return self.default(line)
File "/usr/local/python24/lib/python2.4/pdb.py", line 167, in default
exec code in globals, locals
File "", line 1, in ?
(Pdb) q
{"1": 2, "0": 1, "3": 4, "2": 3, "5": 6, "4": 5, "7": 8, "6": 7}
****************************************
****************************************
line>> 1 2 3 4
{"1": 2, "0": 1, "3": 4, "2": 3, "5": 6, "4": 5, "7": 8, "6": 7}
****************************************
****************************************
line>> 1 2 3 4
{"1": 2, "0": 1, "3": 4, "2": 3}
****************************************
jmjones@bean:~/debugger $ python example_debugger_pm.py example_debugger.data
****************************************
line>> 1 2 3 4 5 6 7 8 9 10
{"1": 2, "0": 1, "3": 4, "2": 3, "5": 6, "4": 5, "7": 8, "6": 7, "9": \
10, "8": 9}
****************************************
****************************************
line>> 1 2 3 4 5 6 7 8 9 10
> /home/jmjones/debugger/example_debugger_pm.py(17)walk_string()
-> self.tmp_dict[key] = int(l[i])
(Pdb)
(Pdb) !for e in sys.exc_info(): print "EXCEPTION", e
EXCEPTION exceptions.ValueError
EXCEPTION invalid literal for int(): 9
EXCEPTION
(Pdb) !print l[i]
9
(Pdb) !l[i]
"9\x089"
(Pdb) !print some_string
1 2 3 4 5 6 7 8 9 10
(Pdb) !some_string
"1 2 3 4 5 6 7 8 9\x089 10"
(Pdb) int("9\b9")
*** ValueError: invalid literal for int(): 9
#!/usr/bin/env python
import pdb
import string
import sys
class ConvertToDict:
def __init__(self):
self.tmp_dict = {}
self.return_dict = {}
def walk_string(self, some_string):
"""walk given text string and return a dictionary.
Maintain state in instance attributes in case we hit an exception"""
l = string.split(some_string)
for i in range(len(l)):
key = str(i)
try:
self.tmp_dict[key] = int(l[i])
except ValueError:
self.tmp_dict[key] = None
return_dict = self.tmp_dict
self.return_dict = self.tmp_dict
self.reset()
return return_dict
def reset(self):
"""clean up"""
self.tmp_dict = {}
self.return_dict = {}
def get_number_dict(self, some_string):
"""do slightly better exception handling here"""
try:
return self.walk_string(some_string)
except:
#if we hit an exception, we can rely on tmp_dict
being a backup to the point of the exception
return_dict = self.tmp_dict
self.reset()
return return_dict
def main():
ctd = ConvertToDict()
for line in file(sys.argv[1]):
line = line.strip()
print "*" * 40
print "line>>", line
print ctd.get_number_dict(line)
print "*" * 40
if __name__ == "__main__":
main()
jmjones@bean:~/debugger $ python example_debugger_fixed.py example_debugger.data
****************************************
line>> 1 2 3 4 5 6 7 8 9 10
{"1": 2, "0": 1, "3": 4, "2": 3, "5": 6, "4": 5, "7": 8, "6": 7, "9": \
10, "8": 9}
****************************************
****************************************
line>> 1 2 3 4 5 6 7 8 9 10
{"1": 2, "0": 1, "3": 4, "2": 3, "5": 6, "4": 5, "7": 8, "6": 7, "9": \
10, "8": None}
****************************************
****************************************
line>> 1 2 3 4
{"1": 2, "0": 1, "3": 4, "2": 3}
****************************************
****************************************
line>> 1 2 3 4
{"1": 2, "0": 1, "3": 4, "2": 3}
****************************************
이전 글 : MySQL 관리자를 위한 오픈 툴
다음 글 : BSD 방화벽에서 IPFW 룰셋 사용하기
최신 콘텐츠