-
Notifications
You must be signed in to change notification settings - Fork 1
/
gdb.py
140 lines (116 loc) · 4.19 KB
/
gdb.py
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
from bt import parse_bt, cmp_bt
from graph import graphPainter
from asmAnalyser import asmAnalyser
import time
from config import SOURCEFOLDER, ASMFILE, GDBPORT, ARCH
from arch import getArch
class gdbTracer:
def __init__(self, p, funcName):
self.p = p
self.func = funcName
self.sourceFolder = SOURCEFOLDER
ah = getArch(ARCH)
self.eip = ah.getEip()
self.max_brkTime = 30 # If a breakpoint is triggered more than this number, it will be removed
self.ed = '394743516231415926' # A random number as end of output of a session
print('loading %s' % ASMFILE)
cur_time = time.clock()
self.asm = asmAnalyser(ASMFILE)
print('finished loading, cost %ss' % str(time.clock() - cur_time))
if(not self.asm.funcExist(funcName)):
print('ERROR! Function %s not exist!' % funcName)
return
p.stdin.write(bytes('target remote:%s\n' % GDBPORT, encoding='utf8'))
def configure(self, dotFileName, logFileName, depth):
self.dotFileName = dotFileName
self.log = open(logFileName, 'w')
self.maxDepth = depth
self.endAddrs = []
self. existBreakpoints = set()
self.brks = {}
def flush(self):
self.write('p ' + self.ed)
self.p.stdin.flush()
def write(self, s):
self.p.stdin.write(bytes(s + '\n', encoding='utf8'))
self.log.write(s + '\n')
def read(self):
ls = []
while(True):
l = self.p.stdout.readline().decode()
ls.append(l)
if(self.ed in l):
break
assert(len(ls) > 0)
self.log.writelines(ls)
ls.pop()
return ls
def getRip(self):
self.write('p/x $' + self.eip)
self.flush()
ls = self.read()
assert(len(ls) > 0 and '= ' in ls[-1])
return ls[-1].split('= ')[-1].strip()
def bk(self, point):
if(point[0] != '*'):
self.write('p/x %s' % point)
self.flush()
ls = self.read()
assert(len(ls) > 0 and '=' in ls[-1])
point = '*' + ls[-1].split('= ')[-1].strip()
if(point not in self.existBreakpoints):
self.write('b %s' % point)
self.existBreakpoints.add(point)
def checkBreak(self, ls):
b = 'Breakpoint'
lb = len(b)
bn = -1
for l in ls:
if(len(l) > lb and l[:lb] == b):
bn = int(l.split(',')[0].split()[1])
break
assert(bn != -1)
if(bn in self.brks.keys()):
self.brks[bn] += 1
else:
self.brks[bn] = 1
if(self.brks[bn] > self.max_brkTime):
self.write('d %d' % bn)
def run(self):
self.write('delete')
self.write('b ' + self.func)
self.write('c')
self.write('bt 100')
self.flush()
ls = self.read()
bt = parse_bt(ls)
base_bt = bt
rip = self.getRip()
func = bt[0][0]
assert(func == self.func)
for point in self.asm.getRets(func):
self.endAddrs.append(point[1:])
self.bk(point)
self.painter = graphPainter(self.dotFileName, bt, self.sourceFolder, self.maxDepth)
while(True):
if(cmp_bt(bt, base_bt)):
if(rip in self.endAddrs and len(bt) == len(base_bt)):
break
new = self.asm.funcExist(func) and rip == self.asm.getFuncAddr(func)
self.painter.paint(bt, new)
if(len(bt) - len(base_bt) <= self.maxDepth):
if(new):
for point in self.asm.getCalls(func):
self.bk(point)
if(rip in self.asm.getCallSrcs()):
self.bk(self.asm.getCallDst(rip))
self.write('c')
self.write('bt 100')
self.flush()
ls = self.read()
bt = parse_bt(ls)
rip = self.getRip()
func = bt[0][0]
self.checkBreak(ls)
self.painter.close()
self.log.close()