Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
python filename bug fix
attempt debug console inject
  • Loading branch information
a-miscellaneous committed Jun 24, 2023
1 parent a80400b commit 1c66201
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"out": true // set this to false to include "out" folder in search results
},
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
"typescript.tsc.autoDetect": "off"
"typescript.tsc.autoDetect": "off",
"cmake.configureOnOpen": false
}
88 changes: 40 additions & 48 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import json



class gdbHandler():


def __init__(self, fName, cName):
print("### initialising gdbHandler ###")
self.fileName = fName
Expand All @@ -16,7 +14,7 @@ def __init__(self, fName, cName):
self.currentStep = 0
gdb.execute("file " + self.fileName)
gdb.execute("set pagination off")
gdb.execute("set style enabled off") # remove colors
gdb.execute("set style enabled off") # remove colors

def getFrameAmount(self):
num_frames = 0
Expand Down Expand Up @@ -48,7 +46,8 @@ def getLocals(self):

def getArgs(self):
args = gdb.execute("info args", to_string=True)
if args == "No arguments.\n": return
if args == "No arguments.\n":
return
args = args.strip().split("\n")
dic = {}
for arg in args:
Expand All @@ -74,7 +73,8 @@ def analyzeLine(self):
currentLine = currentFrame.find_sal().line
currentLocals = self.getVars()
currentFile = currentFrame.find_sal().symtab.filename
currentLineStr = gdb.execute("frame ", to_string=True ).split("\n")[1].split("\t",1)[1].strip()
currentLineStr = gdb.execute("frame ", to_string=True).split("\n")[
1].split("\t", 1)[1].strip()
currentStep = self.currentStep

gdb.execute("step")
Expand All @@ -93,27 +93,30 @@ def analyzeLine(self):

# came back from recursion or just steped on new line
# find diferences and save them
self.saveAssiggnmentHistory(currentLine, currentLocals, currentLineStr, currentHeight, currentStep)
self.saveAssiggnmentHistory(
currentLine, currentLocals, currentLineStr, currentHeight, currentStep, currentFile)

# continue recurse
self.analyzeLine()

def saveAssiggnmentHistory(self, line : int, oldlocals : dict, oldLineStr : str, stackHeight : int, currentStep : int):
file = gdb.selected_frame().find_sal().symtab.filename
oldLineStr = " "+oldLineStr # to exclude any " or '
def saveAssiggnmentHistory(self, line: int, oldlocals: dict, oldLineStr: str, stackHeight: int, currentStep: int, file: str):
oldLineStr = " "+oldLineStr # to exclude any " or '
newLocals = self.getVars()
if newLocals is None: return
if newLocals is None:
return

for key in newLocals:
try:
if oldlocals[key] != newLocals[key]: # found a difference, save it
obj = {"line" : line, "value" : newLocals[key], "var": key, "file" : file, "stackHeight" : stackHeight, "step" : currentStep}
if oldlocals[key] != newLocals[key]: # found a difference, save it
obj = {"line": line, "value": newLocals[key], "var": key,
"file": file, "stackHeight": stackHeight, "step": currentStep}
self.history.append(obj)
return

except: # means that a new var was added to the scope
except: # means that a new var was added to the scope
currentLine = gdb.selected_frame().find_sal().line
obj = {"line" : currentLine, "value" : newLocals[key], "var": key, "file" : file, "stackHeight" : stackHeight, "step" : currentStep}
obj = {"line": currentLine, "value": newLocals[key], "var": key,
"file": file, "stackHeight": stackHeight, "step": currentStep}

# only accept the new var if it came from a for loop
if self.findForLoop(key):
Expand All @@ -125,11 +128,13 @@ def saveAssiggnmentHistory(self, line : int, oldlocals : dict, oldLineStr : str,
match = re.search(assignmentRegX, oldLineStr)
if match:
res = match.group(3).strip()
obj = {"line" : line, "value" : oldlocals[res], "var": res, "file" : file, "stackHeight" : stackHeight, "step" : currentStep}
obj = {"line": line, "value": oldlocals[res], "var": res,
"file": file, "stackHeight": stackHeight, "step": currentStep}
self.history.append(obj)

def findForLoop(self, var):
currentLineStr = gdb.execute("frame ", to_string=True ).split("\n")[1].split("\t",1)[1].strip()
currentLineStr = gdb.execute("frame ", to_string=True).split("\n")[
1].split("\t", 1)[1].strip()
for_regex = r"^(\s*)(for)(\s*)(\()([a-zA-Z_][a-zA-Z0-9_]*)(\s*)(\+\=|\-\=|\*\=|\/\=|\%\=|\=)(\s*)"
match = re.search(for_regex, currentLineStr)
if not match or match.group(5) != var:
Expand All @@ -141,9 +146,11 @@ def saveFunctionParams(self):
file = gdb.selected_frame().find_sal().symtab.filename
functionName = gdb.selected_frame().name()
dic = self.getArgs()
if dic is None: return
if dic is None:
return

obj = {"line" : line, "value" : dic, "file": file, "stackHeight" : self.getFrameAmount(), "stackName" : functionName, "step" : self.currentStep}
obj = {"line": line, "value": dic, "file": file, "stackHeight": self.getFrameAmount(
), "stackName": functionName, "step": self.currentStep}
self.history.append(obj)


Expand All @@ -154,11 +161,12 @@ def __init__(self, stackName):
self.values = []

def append(self, varObj, step, stackHeight):
self.values.append({"dict": varObj, "stackHeight": stackHeight, "step": step})
self.values.append(
{"dict": varObj, "stackHeight": stackHeight, "step": step})
# step is the first step in this function so it is incorrect, used for finding nearest step with <= step

def asSerial(self):
return { "functionName": self.stackName, "values": self.values}
return {"functionName": self.stackName, "values": self.values}


class lineHistory():
Expand All @@ -170,7 +178,8 @@ def __init__(self, var):
self.minValue = float("inf")

def append(self, value, step, stackHeight):
self.values.append({"value": value, "step": step, "stackHeight": stackHeight})
self.values.append({"value": value, "step": step,
"stackHeight": stackHeight})
value = value
try:
value = float(value)
Expand All @@ -180,15 +189,15 @@ def append(self, value, step, stackHeight):
self.minValue = min(self.minValue, value)

def asSerial(self):
self.maxValue = self.maxValue if self.maxValue > float("-inf") else None
self.maxValue = self.maxValue if self.maxValue > float(
"-inf") else None
self.minValue = self.minValue if self.minValue < float("inf") else None
return { "var": self.var, "values": self.values, "maxValue": self.maxValue, "minValue": self.minValue}
return {"var": self.var, "values": self.values, "maxValue": self.maxValue, "minValue": self.minValue}


class exeHistory():
history = {}


def append(self, obj):
fileName = obj["file"]
line = obj["line"]
Expand All @@ -198,47 +207,37 @@ def append(self, obj):
stackHeight = obj["stackHeight"]
stackName = obj["stackName"] if "stackName" in obj else None

if fileName not in self.history: # first time
if fileName not in self.history: # first time
self.history[fileName] = {}

if var is None: # args
self.handleArgs(fileName, line, value, step, stackHeight, stackName)
else: # line
if var is None: # args
self.handleArgs(fileName, line, value, step,
stackHeight, stackName)
else: # line
self.handleLines(fileName, line, var, value, step, stackHeight)

def handleArgs(self, fileName, line, value, step, stackHeight, stackName):
if line not in self.history[fileName]: # first time
if line not in self.history[fileName]: # first time
self.history[fileName][line] = argsHistory(stackName)

self.history[fileName][line].append(value, step, stackHeight)

def handleLines(self, fileName, line, var, value, step, stackHeight):
if line not in self.history[fileName]: # first time
if line not in self.history[fileName]: # first time
self.history[fileName][line] = lineHistory(var)

self.history[fileName][line].append(value, step, stackHeight)


def asSerial(self):
return self.history





def serializer(obj):
if hasattr(obj, "asSerial"):
return obj.asSerial()
return obj.__dict__









if __name__ == "__main__":
gdbHandler = gdbHandler("a.out", "hello.c")
gdb.execute("b main")
Expand All @@ -250,15 +249,8 @@ def serializer(obj):
print(e)
print("### end of program ###")


print("### printing history ###")
with open("history.json", "w") as f:
json.dump(gdbHandler.history, f, indent=4, default=serializer)


gdb.execute("quit")





5 changes: 5 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,15 @@ function getCSS(lineHeight: number): string {
}



function webviewMessageHandler(message: any) {
switch (message.command) {
case "highlight-line":
utils.changeHighlightedLine(message.id);
const debugC = vscode.debug.activeDebugConsole;

if (!debugC) { break; }
debugC.appendLine("-exec s");
break;
default:
console.log("Unknown command: " + message.command);
Expand Down
1 change: 0 additions & 1 deletion src/webview/webview_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ function removeAllBars() {
});
}


function changeDivToBar(div, overlap) {


Expand Down

0 comments on commit 1c66201

Please sign in to comment.