3
3
import json
4
4
5
5
6
-
7
6
class gdbHandler ():
8
7
9
-
10
8
def __init__ (self , fName , cName ):
11
9
print ("### initialising gdbHandler ###" )
12
10
self .fileName = fName
@@ -16,7 +14,7 @@ def __init__(self, fName, cName):
16
14
self .currentStep = 0
17
15
gdb .execute ("file " + self .fileName )
18
16
gdb .execute ("set pagination off" )
19
- gdb .execute ("set style enabled off" ) # remove colors
17
+ gdb .execute ("set style enabled off" ) # remove colors
20
18
21
19
def getFrameAmount (self ):
22
20
num_frames = 0
@@ -48,7 +46,8 @@ def getLocals(self):
48
46
49
47
def getArgs (self ):
50
48
args = gdb .execute ("info args" , to_string = True )
51
- if args == "No arguments.\n " : return
49
+ if args == "No arguments.\n " :
50
+ return
52
51
args = args .strip ().split ("\n " )
53
52
dic = {}
54
53
for arg in args :
@@ -74,7 +73,8 @@ def analyzeLine(self):
74
73
currentLine = currentFrame .find_sal ().line
75
74
currentLocals = self .getVars ()
76
75
currentFile = currentFrame .find_sal ().symtab .filename
77
- currentLineStr = gdb .execute ("frame " , to_string = True ).split ("\n " )[1 ].split ("\t " ,1 )[1 ].strip ()
76
+ currentLineStr = gdb .execute ("frame " , to_string = True ).split ("\n " )[
77
+ 1 ].split ("\t " , 1 )[1 ].strip ()
78
78
currentStep = self .currentStep
79
79
80
80
gdb .execute ("step" )
@@ -93,27 +93,30 @@ def analyzeLine(self):
93
93
94
94
# came back from recursion or just steped on new line
95
95
# find diferences and save them
96
- self .saveAssiggnmentHistory (currentLine , currentLocals , currentLineStr , currentHeight , currentStep )
96
+ self .saveAssiggnmentHistory (
97
+ currentLine , currentLocals , currentLineStr , currentHeight , currentStep , currentFile )
97
98
98
99
# continue recurse
99
100
self .analyzeLine ()
100
101
101
- def saveAssiggnmentHistory (self , line : int , oldlocals : dict , oldLineStr : str , stackHeight : int , currentStep : int ):
102
- file = gdb .selected_frame ().find_sal ().symtab .filename
103
- oldLineStr = " " + oldLineStr # to exclude any " or '
102
+ def saveAssiggnmentHistory (self , line : int , oldlocals : dict , oldLineStr : str , stackHeight : int , currentStep : int , file : str ):
103
+ oldLineStr = " " + oldLineStr # to exclude any " or '
104
104
newLocals = self .getVars ()
105
- if newLocals is None : return
105
+ if newLocals is None :
106
+ return
106
107
107
108
for key in newLocals :
108
109
try :
109
- if oldlocals [key ] != newLocals [key ]: # found a difference, save it
110
- obj = {"line" : line , "value" : newLocals [key ], "var" : key , "file" : file , "stackHeight" : stackHeight , "step" : currentStep }
110
+ if oldlocals [key ] != newLocals [key ]: # found a difference, save it
111
+ obj = {"line" : line , "value" : newLocals [key ], "var" : key ,
112
+ "file" : file , "stackHeight" : stackHeight , "step" : currentStep }
111
113
self .history .append (obj )
112
114
return
113
115
114
- except : # means that a new var was added to the scope
116
+ except : # means that a new var was added to the scope
115
117
currentLine = gdb .selected_frame ().find_sal ().line
116
- obj = {"line" : currentLine , "value" : newLocals [key ], "var" : key , "file" : file , "stackHeight" : stackHeight , "step" : currentStep }
118
+ obj = {"line" : currentLine , "value" : newLocals [key ], "var" : key ,
119
+ "file" : file , "stackHeight" : stackHeight , "step" : currentStep }
117
120
118
121
# only accept the new var if it came from a for loop
119
122
if self .findForLoop (key ):
@@ -125,11 +128,13 @@ def saveAssiggnmentHistory(self, line : int, oldlocals : dict, oldLineStr : str,
125
128
match = re .search (assignmentRegX , oldLineStr )
126
129
if match :
127
130
res = match .group (3 ).strip ()
128
- obj = {"line" : line , "value" : oldlocals [res ], "var" : res , "file" : file , "stackHeight" : stackHeight , "step" : currentStep }
131
+ obj = {"line" : line , "value" : oldlocals [res ], "var" : res ,
132
+ "file" : file , "stackHeight" : stackHeight , "step" : currentStep }
129
133
self .history .append (obj )
130
134
131
135
def findForLoop (self , var ):
132
- currentLineStr = gdb .execute ("frame " , to_string = True ).split ("\n " )[1 ].split ("\t " ,1 )[1 ].strip ()
136
+ currentLineStr = gdb .execute ("frame " , to_string = True ).split ("\n " )[
137
+ 1 ].split ("\t " , 1 )[1 ].strip ()
133
138
for_regex = r"^(\s*)(for)(\s*)(\()([a-zA-Z_][a-zA-Z0-9_]*)(\s*)(\+\=|\-\=|\*\=|\/\=|\%\=|\=)(\s*)"
134
139
match = re .search (for_regex , currentLineStr )
135
140
if not match or match .group (5 ) != var :
@@ -141,9 +146,11 @@ def saveFunctionParams(self):
141
146
file = gdb .selected_frame ().find_sal ().symtab .filename
142
147
functionName = gdb .selected_frame ().name ()
143
148
dic = self .getArgs ()
144
- if dic is None : return
149
+ if dic is None :
150
+ return
145
151
146
- obj = {"line" : line , "value" : dic , "file" : file , "stackHeight" : self .getFrameAmount (), "stackName" : functionName , "step" : self .currentStep }
152
+ obj = {"line" : line , "value" : dic , "file" : file , "stackHeight" : self .getFrameAmount (
153
+ ), "stackName" : functionName , "step" : self .currentStep }
147
154
self .history .append (obj )
148
155
149
156
@@ -154,11 +161,12 @@ def __init__(self, stackName):
154
161
self .values = []
155
162
156
163
def append (self , varObj , step , stackHeight ):
157
- self .values .append ({"dict" : varObj , "stackHeight" : stackHeight , "step" : step })
164
+ self .values .append (
165
+ {"dict" : varObj , "stackHeight" : stackHeight , "step" : step })
158
166
# step is the first step in this function so it is incorrect, used for finding nearest step with <= step
159
167
160
168
def asSerial (self ):
161
- return { "functionName" : self .stackName , "values" : self .values }
169
+ return {"functionName" : self .stackName , "values" : self .values }
162
170
163
171
164
172
class lineHistory ():
@@ -170,7 +178,8 @@ def __init__(self, var):
170
178
self .minValue = float ("inf" )
171
179
172
180
def append (self , value , step , stackHeight ):
173
- self .values .append ({"value" : value , "step" : step , "stackHeight" : stackHeight })
181
+ self .values .append ({"value" : value , "step" : step ,
182
+ "stackHeight" : stackHeight })
174
183
value = value
175
184
try :
176
185
value = float (value )
@@ -180,15 +189,15 @@ def append(self, value, step, stackHeight):
180
189
self .minValue = min (self .minValue , value )
181
190
182
191
def asSerial (self ):
183
- self .maxValue = self .maxValue if self .maxValue > float ("-inf" ) else None
192
+ self .maxValue = self .maxValue if self .maxValue > float (
193
+ "-inf" ) else None
184
194
self .minValue = self .minValue if self .minValue < float ("inf" ) else None
185
- return { "var" : self .var , "values" : self .values , "maxValue" : self .maxValue , "minValue" : self .minValue }
195
+ return {"var" : self .var , "values" : self .values , "maxValue" : self .maxValue , "minValue" : self .minValue }
186
196
187
197
188
198
class exeHistory ():
189
199
history = {}
190
200
191
-
192
201
def append (self , obj ):
193
202
fileName = obj ["file" ]
194
203
line = obj ["line" ]
@@ -198,47 +207,37 @@ def append(self, obj):
198
207
stackHeight = obj ["stackHeight" ]
199
208
stackName = obj ["stackName" ] if "stackName" in obj else None
200
209
201
- if fileName not in self .history : # first time
210
+ if fileName not in self .history : # first time
202
211
self .history [fileName ] = {}
203
212
204
- if var is None : # args
205
- self .handleArgs (fileName , line , value , step , stackHeight , stackName )
206
- else : # line
213
+ if var is None : # args
214
+ self .handleArgs (fileName , line , value , step ,
215
+ stackHeight , stackName )
216
+ else : # line
207
217
self .handleLines (fileName , line , var , value , step , stackHeight )
208
218
209
219
def handleArgs (self , fileName , line , value , step , stackHeight , stackName ):
210
- if line not in self .history [fileName ]: # first time
220
+ if line not in self .history [fileName ]: # first time
211
221
self .history [fileName ][line ] = argsHistory (stackName )
212
222
213
223
self .history [fileName ][line ].append (value , step , stackHeight )
214
224
215
225
def handleLines (self , fileName , line , var , value , step , stackHeight ):
216
- if line not in self .history [fileName ]: # first time
226
+ if line not in self .history [fileName ]: # first time
217
227
self .history [fileName ][line ] = lineHistory (var )
218
228
219
229
self .history [fileName ][line ].append (value , step , stackHeight )
220
230
221
-
222
231
def asSerial (self ):
223
232
return self .history
224
233
225
234
226
-
227
-
228
-
229
235
def serializer (obj ):
230
236
if hasattr (obj , "asSerial" ):
231
237
return obj .asSerial ()
232
238
return obj .__dict__
233
239
234
240
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
241
if __name__ == "__main__" :
243
242
gdbHandler = gdbHandler ("a.out" , "hello.c" )
244
243
gdb .execute ("b main" )
@@ -250,15 +249,8 @@ def serializer(obj):
250
249
print (e )
251
250
print ("### end of program ###" )
252
251
253
-
254
252
print ("### printing history ###" )
255
253
with open ("history.json" , "w" ) as f :
256
254
json .dump (gdbHandler .history , f , indent = 4 , default = serializer )
257
255
258
-
259
256
gdb .execute ("quit" )
260
-
261
-
262
-
263
-
264
-
0 commit comments