-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpyc-dbg
More file actions
executable file
·91 lines (74 loc) · 2.27 KB
/
pyc-dbg
File metadata and controls
executable file
·91 lines (74 loc) · 2.27 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
#!/usr/bin/env python
# Copyright 2013 anthony cantor
# This file is part of pyc.
#
# pyc 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.
#
# pyc 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 pyc. If not, see <http://www.gnu.org/licenses/>.
import argparse
import sys
import os
import pickle
import elftools
def opt_parser():
parser = argparse.ArgumentParser(description='debug binaries generated by pyc.')
parser.add_argument('file', help='file to debug')
parser.add_argument('-v', '--verbose', action='store_true', help='print debug output.')
parser.add_argument(
'-i',
'--input',
help=('use input file when running binary')
)
parser.add_argument(
'-p',
'--cmd-prefix',
help=('prefix pyc related gdb commands with given argument. default: "pyc-"'),
default='pyc-'
)
return parser
class MissingOption(Exception):
pass
def run(options):
code_dir = os.path.realpath(os.path.dirname(__file__))
code = "%s/pyc_dbg.py" % code_dir
f_args = "/tmp/pyc-dbg-arg.pkl"
with open(f_args, 'w') as f:
pickle.dump(options, f)
f_boot = "/tmp/pyc-dbg-bootstrap.py"
with open(f_boot, 'w') as f:
print >>f, "sys.path.append(%r)" % (code_dir)
print >>f, "import pickle"
print >>f, "import pyc_dbg"
print >>f, "with open(%r, 'r') as f:" % (f_args)
print >>f, "\tpyc_dbg_opts = pickle.load(f)"
print >>f, "pyc_dbg.init(pyc_dbg_opts)"
os.execlp("gdb", "gdb", "-x", f_boot, options.file, *options.gdb_args)
raise Exception("shouldnt get here")
if __name__ == "__main__":
opt_p = opt_parser()
my_args = []
gdb_args = []
for i in range(1, len(sys.argv)):
arg = sys.argv[i]
if arg.strip() == "--":
gdb_args = sys.argv[(i+1):]
break
my_args.append(arg)
options = opt_p.parse_args(my_args)
options.gdb_args = gdb_args
try:
run(options)
except MissingOption as e:
print e
print
opt_p.print_help()
exit(-1)