[nim-gdb] Fixed enums and flag output [ci skip] (#17634)

Debugger works for enums again. Additionally, flags work better than before.

Reworked object printer as well, but the approach needs much more work or has
to be replaced all together. This is mostly to save the work and myself or
someone else can revisit it.
This commit is contained in:
Saem Ghani 2021-04-05 02:37:28 -07:00 • committed by GitHub
commit 5261579175
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 204 additions and 71 deletions

View file

@ -25,6 +25,7 @@ outputs = [
'seq(3, 3) = {"one", "two", "three"}', 'seq(3, 3) = {"one", "two", "three"}',
'Table(3, 64) = {[4] = "four", [5] = "five", [6] = "six"}', 'Table(3, 64) = {[4] = "four", [5] = "five", [6] = "six"}',
'Table(3, 8) = {["two"] = 2, ["three"] = 3, ["one"] = 1}', 'Table(3, 8) = {["two"] = 2, ["three"] = 3, ["one"] = 1}',
'{a = 1, b = "some string"}'
] ]
for i, expected in enumerate(outputs): for i, expected in enumerate(outputs):
@ -32,7 +33,7 @@ for i, expected in enumerate(outputs):
gdb.flush() gdb.flush()
functionSymbol = gdb.selected_frame().block().function functionSymbol = gdb.selected_frame().block().function
assert functionSymbol.line == 21 assert functionSymbol.line == 41, str(functionSymbol.line)
if i == 6: if i == 6:
# myArray is passed as pointer to int to myDebug. I look up myArray up in the stack # myArray is passed as pointer to int to myDebug. I look up myArray up in the stack
@ -47,6 +48,6 @@ for i, expected in enumerate(outputs):
output = str(raw) output = str(raw)
assert output == expected, output + " != " + expected assert output == expected, "{0} : output: ({1}) != expected: ({2})".format(i, output, expected)
gdb.write(f"passed\n", gdb.STDLOG) gdb.write(f"passed\n", gdb.STDLOG)
gdb.execute("continue") gdb.execute("continue")

View file

@ -14,7 +14,27 @@ type
moTwo, moTwo,
moThree, moThree,
moFoure, moFoure,
MyObj = object
a*: int
b*: string
# MyVariant = ref object
# id*: int
# case kind*: MyEnum
# of meOne: mInt*: int
# of meTwo, meThree: discard
# of meFour:
# moInt*: int
# babies*: seq[MyVariant]
# after: float
# MyIntVariant = ref object
# stuff*: int
# case myKind*: range[0..32766]
# of 0: mFloat*: float
# of 2: mString*: string
# else: mBabies*: seq[MyIntVariant]
var counter = 0 var counter = 0
@ -74,6 +94,18 @@ proc testProc(): void =
var myOtherTable = {"one": 1, "two": 2, "three": 3}.toTable var myOtherTable = {"one": 1, "two": 2, "three": 3}.toTable
myDebug(myOtherTable) #14 myDebug(myOtherTable) #14
var obj = MyObj(a: 1, b: "some string")
myDebug(obj) #15
# var varObj = MyVariant(id: 13, kind: meFour, moInt: 94,
# babies: @[MyVariant(id: 18, kind: meOne, mInt: 7, after: 1.0),
# MyVariant(id: 21, kind: meThree, after: 2.0)],
# after: 3.0)
# myDebug(varObj) #16
# var varObjInt = MyIntVariant(stuff: 5, myKind: 2, mString: "this is my sweet string")
# myDebug(varObjInt) #17
echo(counter) echo(counter)

View file

@ -1,6 +1,7 @@
import gdb import gdb
import re import re
import sys import sys
import traceback
# some feedback that the nim runtime support is loading, isn't a bad # some feedback that the nim runtime support is loading, isn't a bad
# thing at all. # thing at all.
@ -13,14 +14,14 @@ def printErrorOnce(id, message):
global errorSet global errorSet
if id not in errorSet: if id not in errorSet:
errorSet.add(id) errorSet.add(id)
gdb.write(message, gdb.STDERR) gdb.write("printErrorOnce: " + message, gdb.STDERR)
################################################################################ ################################################################################
##### Type pretty printers ##### Type pretty printers
################################################################################ ################################################################################
type_hash_regex = re.compile("^\w*_([A-Za-z0-9]*)$") type_hash_regex = re.compile("^([A-Za-z0-9]*)_([A-Za-z0-9]*)_+([A-Za-z0-9]*)$")
def getNimRti(type_name): def getNimRti(type_name):
""" Return a ``gdb.Value`` object for the Nim Runtime Information of ``type_name``. """ """ Return a ``gdb.Value`` object for the Nim Runtime Information of ``type_name``. """
@ -28,13 +29,20 @@ def getNimRti(type_name):
# Get static const TNimType variable. This should be available for # Get static const TNimType variable. This should be available for
# every non trivial Nim type. # every non trivial Nim type.
m = type_hash_regex.match(type_name) m = type_hash_regex.match(type_name)
lookups = [
"NTI" + m.group(2).lower() + "__" + m.group(3) + "_",
"NTI" + "__" + m.group(3) + "_",
"NTI" + m.group(2).replace("colon", "58").lower() + "__" + m.group(3) + "_"
]
if m: if m:
try: for l in lookups:
return gdb.parse_and_eval("NTI__" + m.group(1) + "_") try:
except: return gdb.parse_and_eval(l)
return None except:
pass
None
def getNameFromNimRti(rti_val): def getNameFromNimRti(rti):
""" Return name (or None) given a Nim RTI ``gdb.Value`` """ """ Return name (or None) given a Nim RTI ``gdb.Value`` """
try: try:
# sometimes there isn't a name field -- example enums # sometimes there isn't a name field -- example enums
@ -192,6 +200,7 @@ class NimStringEqFunction (gdb.Function):
NimStringEqFunction() NimStringEqFunction()
################################################################################ ################################################################################
##### GDB Command, equivalent of Nim's $ operator ##### GDB Command, equivalent of Nim's $ operator
################################################################################ ################################################################################
@ -315,22 +324,6 @@ class NimStringPrinter:
else: else:
return "" return ""
# class NimStringPrinter:
# pattern = re.compile(r'^NimStringDesc$')
# def __init__(self, val):
# self.val = val
# def display_hint(self):
# return 'string'
# def to_string(self):
# if self.val:
# l = int(self.val['Sup']['len'])
# return self.val['data'].lazy_string(encoding="utf-8", length=l)
# else:
# return ""
class NimRopePrinter: class NimRopePrinter:
pattern = re.compile(r'^tyObject_RopeObj__([A-Za-z0-9]*) \*$') pattern = re.compile(r'^tyObject_RopeObj__([A-Za-z0-9]*) \*$')
@ -372,8 +365,8 @@ def reprEnum(e, typ):
e = int(e) e = int(e)
n = typ["node"] n = typ["node"]
flags = int(typ["flags"]) flags = int(typ["flags"])
# 1 << 2 is {ntfEnumHole} # 1 << 6 is {ntfEnumHole}
if ((1 << 2) & flags) == 0: if ((1 << 6) & flags) == 0:
o = e - int(n["sons"][0]["offset"]) o = e - int(n["sons"][0]["offset"])
if o >= 0 and 0 < int(n["len"]): if o >= 0 and 0 < int(n["len"]):
return n["sons"][o]["name"].string("utf-8", "ignore") return n["sons"][o]["name"].string("utf-8", "ignore")
@ -386,19 +379,26 @@ def reprEnum(e, typ):
return str(e) + " (invalid data!)" return str(e) + " (invalid data!)"
def enumNti(typeNimName, idString):
typeInfoName = "NTI" + typeNimName.lower() + "__" + idString + "_"
nti = gdb.lookup_global_symbol(typeInfoName)
if nti is None:
typeInfoName = "NTI" + "__" + idString + "_"
nti = gdb.lookup_global_symbol(typeInfoName)
return (typeInfoName, nti)
class NimEnumPrinter: class NimEnumPrinter:
pattern = re.compile(r'^tyEnum_(\w*)__([A-Za-z0-9]*)$') pattern = re.compile(r'^tyEnum_([A-Za-z0-9]+)__([A-Za-z0-9]*)$')
def __init__(self, val): def __init__(self, val):
self.val = val self.val = val
typeName = self.val.type.name typeName = self.val.type.name
match = self.pattern.match(typeName) match = self.pattern.match(typeName)
self.typeNimName = match.group(1) self.typeNimName = match.group(1)
typeInfoName = "NTI__" + match.group(2) + "_" typeInfoName, self.nti = enumNti(self.typeNimName, match.group(2))
self.nti = gdb.lookup_global_symbol(typeInfoName)
if self.nti is None: if self.nti is None:
printErrorOnce(typeInfoName, f"NimEnumPrinter: lookup global symbol '{typeInfoName}' failed for {typeName}.\n") printErrorOnce(typeInfoName, f"NimEnumPrinter: lookup global symbol: '{typeInfoName}' failed for {typeName}.\n")
def to_string(self): def to_string(self):
if self.nti: if self.nti:
@ -414,18 +414,17 @@ class NimSetPrinter:
## the set printer is limited to sets that fit in an integer. Other ## the set printer is limited to sets that fit in an integer. Other
## sets are compiled to `NU8 *` (ptr uint8) and are invisible to ## sets are compiled to `NU8 *` (ptr uint8) and are invisible to
## gdb (currently). ## gdb (currently).
pattern = re.compile(r'^tySet_tyEnum_(\w*)_([A-Za-z0-9]*)$') pattern = re.compile(r'^tySet_tyEnum_([A-Za-z0-9]+)__([A-Za-z0-9]*)$')
def __init__(self, val): def __init__(self, val):
self.val = val self.val = val
match = self.pattern.match(self.val.type.name) typeName = self.val.type.name
self.typeNimName = match.group(1) match = self.pattern.match(typeName)
self.typeNimName = match.group(1)
typeInfoName = "NTI__" + match.group(2) + "_" typeInfoName, self.nti = enumNti(self.typeNimName, match.group(2))
self.nti = gdb.lookup_global_symbol(typeInfoName)
if self.nti is None: if self.nti is None:
printErrorOnce(typeInfoName, "NimSetPrinter: lookup global symbol '"+ typeInfoName +" failed for " + self.val.type.name + ".\n") printErrorOnce(typeInfoName, f"NimSetPrinter: lookup global symbol: '{typeInfoName}' failed for {typeName}.\n")
def to_string(self): def to_string(self):
if self.nti: if self.nti:
@ -563,7 +562,7 @@ class NimStringTablePrinter:
def children(self): def children(self):
if self.val: if self.val:
data = NimSeqPrinter(self.val['data'].dereference()) data = NimSeqPrinter(self.val['data'].referenced_value())
for idxStr, entry in data.children(): for idxStr, entry in data.children():
if int(entry['Field0']) != 0: if int(entry['Field0']) != 0:
yield (idxStr + ".Field0", entry['Field0']) yield (idxStr + ".Field0", entry['Field0'])
@ -603,55 +602,156 @@ class NimTablePrinter:
# this is untested, therefore disabled # this is untested, therefore disabled
# class NimObjectPrinter: # class NimObjectPrinter:
# pattern = re.compile(r'^tyObject_.*$') # pattern = re.compile(r'^tyObject_([A-Za-z0-9]+)__(_?[A-Za-z0-9]*)(:? \*)?$')
# def __init__(self, val): # def __init__(self, val):
# self.val = val # self.val = val
# self.valType = None
# self.valTypeNimName = None
# def display_hint(self): # def display_hint(self):
# return 'object' # return 'object'
# def _determineValType(self):
# if self.valType is None:
# vt = self.val.type
# if vt.name is None:
# target = vt.target()
# self.valType = target.pointer()
# self.fields = target.fields()
# self.valTypeName = target.name
# self.isPointer = True
# else:
# self.valType = vt
# self.fields = vt.fields()
# self.valTypeName = vt.name
# self.isPointer = False
# def to_string(self): # def to_string(self):
# return str(self.val.type) # if self.valTypeNimName is None:
# self._determineValType()
# match = self.pattern.match(self.valTypeName)
# self.valTypeNimName = match.group(1)
# return self.valTypeNimName
# def children(self): # def children(self):
# if not self.val: # self._determineValType()
# yield "object", "<nil>" # if self.isPointer and int(self.val) == 0:
# raise StopIteration # return
# self.baseVal = self.val.referenced_value() if self.isPointer else self.val
# for (i, field) in enumerate(self.val.type.fields()): # for c in self.handleFields(self.baseVal, getNimRti(self.valTypeName)):
# if field.type.code == gdb.TYPE_CODE_UNION: # yield c
# yield _union_field
# else: # def handleFields(self, currVal, rti, fields = None):
# yield (field.name, self.val[field]) # rtiSons = None
# discField = (0, None)
# seenSup = False
# if fields is None:
# fields = self.fields
# try: # XXX: remove try after finished debugging this method
# for (i, field) in enumerate(fields):
# if field.name == "Sup": # inherited data
# seenSup = True
# baseRef = rti['base']
# if baseRef:
# baseRti = baseRef.referenced_value()
# baseVal = currVal['Sup']
# baseValType = baseVal.type
# if baseValType.name is None:
# baseValType = baseValType.target().pointer()
# baseValFields = baseValType.target().fields()
# else:
# baseValFields = baseValType.fields()
# for c in self.handleFields(baseVal, baseRti, baseValFields):
# yield c
# else:
# if field.type.code == gdb.TYPE_CODE_UNION:
# # if not rtiSons:
# rtiNode = rti['node'].referenced_value()
# rtiSons = rtiNode['sons']
# def _union_field(self, i, field): # if not rtiSons and int(rtiNode['len']) == 0 and str(rtiNode['name']) != "0x0":
# rti = getNimRti(self.val.type.name) # rtiSons = [rti['node']] # sons are dereferenced by the consumer
# if rti is None:
# return (field.name, "UNION field can't be displayed without RTI") # if not rtiSons:
# printErrorOnce(self.valTypeName, f"NimObjectPrinter: UNION field can't be displayed without RTI {self.valTypeName}, using fallback.\n")
# # yield (field.name, self.baseVal[field]) # XXX: this fallback seems wrong
# return # XXX: this should probably continue instead?
# node_sons = rti['node'].dereference()['sons'] # if int(rtiNode['len']) != 0 and str(rtiNode['name']) != "0x0":
# prev_field = self.val.type.fields()[i - 1] # gdb.write(f"wtf IT HAPPENED {self.valTypeName}\n", gdb.STDERR)
# descriminant_node = None # discNode = rtiSons[discField[0]].referenced_value()
# for i in range(int(node['len'])): # if not discNode:
# son = node_sons[i].dereference() # raise ValueError("Can't find union discriminant field in object RTI")
# if son['name'].string("utf-8", "ignore") == str(prev_field.name):
# descriminant_node = son # discNodeLen = int(discNode['len'])
# break # discFieldVal = int(currVal[discField[1].name])
# if descriminant_node is None:
# raise ValueError("Can't find union descriminant field in object RTI")
# if descriminant_node is None: raise ValueError("Can't find union field in object RTI") # unionNodeRef = None
# union_node = descriminant_node['sons'][int(self.val[prev_field])].dereference() # if discFieldVal < discNodeLen:
# union_val = self.val[field] # unionNodeRef = discNode['sons'][discFieldVal]
# if not unionNodeRef:
# unionNodeRef = discNode['sons'][discNodeLen]
# for f1 in union_val.type.fields(): # if not unionNodeRef:
# for f2 in union_val[f1].type.fields(): # printErrorOnce(self.valTypeName + "no union node", f"wtf is up with sons {self.valTypeName} {unionNodeRef} {rtiNode['offset']} {discNode} {discFieldVal} {discNodeLen} {discField[1].name} {field.name} {field.type}\n")
# if str(f2.name) == union_node['name'].string("utf-8", "ignore"): # continue
# return (str(f2.name), union_val[f1][f2])
# raise ValueError("RTI is absent or incomplete, can't find union definition in RTI") # unionNode = unionNodeRef.referenced_value()
# fieldName = "" if field.name == None else field.name.lower()
# unionNodeName = "" if not unionNode['name'] else unionNode['name'].string("utf-8", "ignore")
# if not unionNodeName or unionNodeName.lower() != fieldName:
# unionFieldName = f"_{discField[1].name.lower()}_{int(rti['node'].referenced_value()['len'])}"
# gdb.write(f"wtf i: {i} union: {unionFieldName} field: {fieldName} type: {field.type.name} tag: {field.type.tag}\n", gdb.STDERR)
# else:
# unionFieldName = unionNodeName
# if discNodeLen == 0:
# yield (unionFieldName, currVal[unionFieldName])
# else:
# unionNodeLen = int(unionNode['len'])
# if unionNodeLen > 0:
# for u in range(unionNodeLen):
# un = unionNode['sons'][u].referenced_value()['name'].string("utf-8", "ignore")
# yield (un, currVal[unionFieldName][un])
# else:
# yield(unionNodeName, currVal[unionFieldName])
# else:
# discIndex = i - 1 if seenSup else i
# discField = (discIndex, field) # discriminant field is the last normal field
# yield (field.name, currVal[field.name])
# except GeneratorExit:
# raise
# except:
# gdb.write(f"wtf {self.valTypeName} {i} fn: {field.name} df: {discField} rti: {rti} rtiNode: {rti['node'].referenced_value()} rtiSons: {rtiSons} {sys.exc_info()} {traceback.format_tb(sys.exc_info()[2], limit = 10)}\n", gdb.STDERR)
# gdb.write(f"wtf {self.valTypeName} {i} {field.name}\n", gdb.STDERR)
# # seenSup = False
# # for (i, field) in enumerate(fields):
# # # if field.name:
# # # val = currVal[field.name]
# # # else:
# # # val = None
# # rtiNode = rti['node'].referenced_value()
# # rtiLen = int(rtiNode['len'])
# # if int(rtiNode['len']) > 0:
# # sons = rtiNode['sons']
# # elif int(rti['len']) == 0 and str(rti['name']) != "0x0":
# # sons = [rti['node']] # sons are dereferenced by the consumer
# # sonsIdx = i - 1 if seenSup else i
# # s = sons[sonsIdx].referenced_value()
# # addr = int(currVal.address)
# # off = addr + int(rtiNode['offset'])
# # seenSup = seenSup or field.name == "Sup"
# # gdb.write(f"wtf: i: {i} sonsIdx: {sonsIdx} field: {field.name} rtiLen: {rtiLen} rti: {rti} rtiNode: {rtiNode} isUnion: {field.type.code == gdb.TYPE_CODE_UNION} s: {s}\n", gdb.STDERR)
# raise
################################################################################ ################################################################################