diff --git a/guider/guider.py b/guider/guider.py index 56080156..423a330d 100755 --- a/guider/guider.py +++ b/guider/guider.py @@ -7,7 +7,7 @@ __credits__ = "Peace Lee" __license__ = "GPLv2" __version__ = "3.9.8" -__revision__ = "230824" +__revision__ = "230826" __maintainer__ = "Peace Lee" __email__ = "iipeace5@gmail.com" __repository__ = "https://github.com/iipeace/guider" @@ -97943,6 +97943,7 @@ def getSymbolByOffset(self, offset, onlyFunc=True, reent=False): if not self.sortedSymTable: self.mergeSymTable(onlyFunc=onlyFunc) + # get symbol by offset # try: if UtilMgr.isString(offset): try: @@ -97953,6 +97954,16 @@ def getSymbolByOffset(self, offset, onlyFunc=True, reent=False): addrTable = self.sortedAddrTable symTable = self.sortedSymTable + # TODO: Add line info # + """ + if self.sortedAddrLineTable: + idx = UtilMgr.bisect_left(self.sortedAddrLineTable, offset) - 1 + if idx < 0: + idx = 0 + sidx, line = self.sortedLineTable[idx] + sname = self.srcTable[sidx] + """ + # get target index from address table # idx = UtilMgr.bisect_left(self.sortedAddrTable, offset) - 1 if idx < 0: @@ -98745,6 +98756,10 @@ def _printer(item): self.saved = False self.sortedSymTable = [] self.sortedAddrTable = [] + self.sortedLineTable = [] + self.sortedAddrLineTable = [] + self.srcTable = [] + self.srcIdxTable = {} self.mergedSymTable = {} self.cfaTableTitle = "" self.fileSize = size @@ -98771,8 +98786,9 @@ def _printer(item): self.attr["zipMap"] = self.archiveMap pathMap = {} isTarget = UtilMgr.isValidStr("zip", targetSect) + printable = debug and isTarget - if debug and isTarget: + if printable: printer( ( "[Zip List] (Path: %s%s)\n%s\n" @@ -98805,7 +98821,7 @@ def _printer(item): except: ratio = 0 - if debug and isTarget: + if printable: printer( "%16s(%16s) %16s %16s(%3.1f) %12s" % ( @@ -98824,12 +98840,12 @@ def _printer(item): self.archiveFile[mpath] = offset pathMap[mpath] = n - if debug and isTarget: + if printable: if not self.archiveMap: printer("\tNone") printer(oneLineLoc) - if (debug and isTarget) or "INCZIPOBJ" in SysMgr.environList: + if printable or "INCZIPOBJ" in SysMgr.environList: # print embedded ELF objects # for i in self.archiveList: mpath, size, offset = i @@ -99190,9 +99206,10 @@ def _printer(item): # check target # isTarget = UtilMgr.isValidStr("program", targetSect) + printable = debug and isTarget # print program header title # - if debug and e_phnum and isTarget: + if printable and e_phnum: printer( ( "[Program Headers]\n%s\n" @@ -99283,7 +99300,7 @@ def _printer(item): continue # print program header # - if debug and isTarget: + if printable: printer( "%16s 0x%08x 0x%014x 0x%014x 0x%010x 0x%010x %010s" % ( @@ -99364,9 +99381,10 @@ def _printer(item): # check target # isTarget = UtilMgr.isValidStr("section", targetSect) + printable = debug and isTarget # print section header title # - if debug and e_shnum > 0 and isTarget: + if printable and e_shnum > 0: printer( ( "\n[Section Headers]\n%s\n" @@ -99462,7 +99480,7 @@ def _printer(item): } # print section header # - if debug and isTarget: + if printable: try: size_per = (sh_size / float(self.fileSize)) * 100 except: @@ -99807,9 +99825,10 @@ def _printer(item): # check target # isTarget = UtilMgr.isValidStr(shname, targetSect) + printable = debug and isTarget # print .dynsym table title # - if debug and isTarget: + if printable: printer( ( "\n[%s Section] (nrItem: %s)\n%s\n" @@ -99921,7 +99940,7 @@ def _printer(item): st_shndx = "ABS" # print .dynsym table # - if debug and isTarget: + if printable: # apply filter # if SysMgr.filterGroup: if not UtilMgr.isValidStr(symbol): @@ -99949,7 +99968,7 @@ def _printer(item): printCnt += 1 - if debug and isTarget: + if printable: if printCnt == 0: printer(" None") printer(oneLineLoc) @@ -100007,9 +100026,10 @@ def _printer(item): # check target # isTarget = UtilMgr.isValidStr(shname, targetSect) + printable = debug and isTarget # parse .sym table title # - if debug and isTarget: + if printable: printer( ( "\n[%s Section] (nrItem: %s)\n%s\n" @@ -100104,7 +100124,7 @@ def _printer(item): st_shndx = "ABS" # parse .sym table # - if debug and isTarget: + if printable: # apply filter # if SysMgr.filterGroup: if not UtilMgr.isValidStr(symbol): @@ -100132,7 +100152,7 @@ def _printer(item): printCnt += 1 - if debug and isTarget: + if printable: if printCnt == 0: printer(" None") printer(oneLineLoc) @@ -100163,10 +100183,11 @@ def _printer(item): # check target # isTarget = UtilMgr.isValidStr(shname, targetSect) + printable = debug and isTarget nrItems = long(sh_size / sh_entsize) - if debug and isTarget: + if printable: printer( "\n[%s Section] (nrItem: %s)\n%s\n%16s %16s %32s %16s %s\n%s" % ( @@ -100234,7 +100255,7 @@ def _printer(item): else: saddr = 0 - if debug and isTarget: + if printable: # apply filter # if SysMgr.filterGroup: if not UtilMgr.isValidStr(symbol): @@ -100247,7 +100268,7 @@ def _printer(item): printCnt += 1 - if debug and isTarget: + if printable: if printCnt == 0: printer(" None") printer(oneLineLoc) @@ -100273,10 +100294,11 @@ def _printer(item): # check target # isTarget = UtilMgr.isValidStr(shname, targetSect) + printable = debug and isTarget nrItems = long(sh_size / sh_entsize) - if debug and isTarget: + if printable: printer( "\n[%s Section] (nrItem: %s)\n%s\n%16s %16s %32s %16s %s\n%s" % ( @@ -100354,7 +100376,7 @@ def _printer(item): if symbol: symbol = "%s + " % symbol - if debug and isTarget: + if printable: # apply filter # if SysMgr.filterGroup: if not UtilMgr.isValidStr(symbol): @@ -100373,7 +100395,7 @@ def _printer(item): printCnt += 1 - if debug and isTarget: + if printable: if printCnt == 0: printer(" None") printer(oneLineLoc) @@ -100985,8 +101007,9 @@ def _convRegName(arg, regList): # check target # isTarget = UtilMgr.isValidStr(shname, targetSect) + printable = debug and isTarget - if debug and isTarget: + if printable: printer("\n[%s Section]\n%s" % (shname, twoLineLoc)) # set position # @@ -101137,7 +101160,7 @@ def _decodeData2(table, pos): } # print info # - if debug and isTarget: + if printable: printStr = "\n%08x %016x %08x CIE\n" % ( offset, size, @@ -101313,7 +101336,7 @@ def _decodeData2(table, pos): } # print info # - if debug and isTarget: + if printable: # apply filter # if SysMgr.filterGroup: if not UtilMgr.isValidStr(symbol): @@ -101353,13 +101376,11 @@ def _decodeData2(table, pos): _decodeCFI(self, entry, cfi, cie, offset) # print CFI # - if debug and isTarget: + if printable: _printCFIs(cfi, cie, initLoc, regList) # make CFA table # - _makeCFATable( - self, entry, offset, regList, prt=debug and isTarget - ) + _makeCFATable(self, entry, offset, regList, prt=printable) # print the number of instructions for skip-update-current-line # if self.nrSkipUpdate > 0: @@ -101384,7 +101405,7 @@ def _decodeData2(table, pos): if not name in ("CFAIndex", "CFATable", "info", "abbrev"): del self.attr["dwarf"][name] - if debug and isTarget: + if printable: printer( "\n< Total CIE: %s / FDE: %s >\n%s" % (convNum(nrCIE), convNum(nrFDE), oneLineLoc) @@ -101422,8 +101443,9 @@ def _decodeData2(table, pos): # check target # isTarget = UtilMgr.isValidStr(shname, targetSect) + printable = debug and isTarget - if debug and isTarget: + if printable: printer("\n[%s Section]\n%s" % (shname, twoLineLoc)) # set position # @@ -101451,7 +101473,7 @@ def _decodeData2(table, pos): fdeCnt = _decodeData(fcEncFormat, fd) # print summary # - if debug and isTarget: + if printable: printer( ( "eh_frame pointer: %016x, FDE count: %s\n%s" @@ -101479,7 +101501,7 @@ def _decodeData2(table, pos): addr = _decodeData(tEncFormat, fd) addr = _decodeAddr(addr, sh_addr, curPos, tEncMod) - if debug and isTarget: + if printable: output = "%05s %016x %016x" % (idx, initLoc, addr) # apply filter # @@ -101491,7 +101513,7 @@ def _decodeData2(table, pos): printCnt += 1 - if debug and isTarget: + if printable: if printCnt == 0: printer("%s\n None" % oneLineLoc) printer(oneLineLoc) @@ -101532,7 +101554,9 @@ def _decodeEntry( # decode code # dobj = ElfAnalyzer.EHABIBytecodeDecoder(bytecode, self.path) - if debug and isTarget: + printable = debug and isTarget + + if printable: printer("Entry %s:" % idx) # get table offset # @@ -101569,18 +101593,18 @@ def _decodeEntry( "type": "FUNC", } - if debug and isTarget: + if printable: printer( " Function offset %s: %s" % (hex(foffset).rstrip("L"), toffset) ) if personality == -1: - if debug and isTarget: + if printable: printer("\n") return - if debug and isTarget: + if printable: printer(" Compact model index: %s" % personality) # print op code # @@ -101605,8 +101629,9 @@ def _decodeEntry( # check target # isTarget = UtilMgr.isValidStr(shname, targetSect) + printable = debug and isTarget - if debug and isTarget: + if printable: printer( "\n[%s Section] (nrItem: %s)\n%s\n" % (shname, convNum(nrItems), twoLineLoc) @@ -101723,7 +101748,7 @@ def _decodeEntry( # sort address list for CFA # self.attr["dwarf"]["CFAIndex"].sort() - if debug and isTarget: + if printable: printer(oneLineLoc) def _readNoteSection(fd, offset, size): @@ -101906,8 +101931,10 @@ def _decodeOp(value, dwarfFormat): opcode = ElfAnalyzer.DW_OPS_NAMES_MAP[op] + printable = debug and isTarget + # print data # - if debug and isTarget: + if printable: verbStr = "%s byte block: " % len(value) verbStr += " ".join(list(map(hex, value))) else: @@ -102015,7 +102042,7 @@ def _decodeOp(value, dwarfFormat): ) # print data expression # - if debug and isTarget: + if printable: if opval and not opcode in ElfAnalyzer.DW_OPS_DEC_ARGS: try: opval = hex(opval) @@ -102238,8 +102265,9 @@ def _decodeForm(table, pos, form, addrSize, dwarfFormat, addStr=""): # check target # isTarget = UtilMgr.isValidStr(shname, targetSect) + printable = debug and isTarget - if debug and isTarget: + if printable: printer("\n[%s Section]\n%s" % (shname, twoLineLoc)) # init variables # @@ -102333,7 +102361,7 @@ def _decodeForm(table, pos, form, addrSize, dwarfFormat, addStr=""): abbrevDict[idx][abbrevCode]["attrs"].append(spec) - if debug and isTarget: + if printable: printStr = "%4s %s [%s]\n" % ("Num", "TAG", "Child") printStr += "%s\n" % twoLineLoc @@ -102400,8 +102428,9 @@ def _decodeForm(table, pos, form, addrSize, dwarfFormat, addStr=""): # check target # isTarget = UtilMgr.isValidStr(shname, targetSect) + printable = debug and isTarget - if debug and isTarget: + if printable: printer("\n[%s Section]\n%s" % (shname, twoLineLoc)) # set position # @@ -102489,7 +102518,7 @@ def _decodeForm(table, pos, form, addrSize, dwarfFormat, addStr=""): pos += 1 # print # - if debug and isTarget: + if printable: printStr = "\nCompilation Unit @ offset 0x%x\n" % ( startPos - addrPos ) @@ -102536,7 +102565,7 @@ def _decodeForm(table, pos, form, addrSize, dwarfFormat, addStr=""): parentPos = None # print # - if debug and isTarget: + if printable: printer( " <%s><%x>: Abbrev Number: %s" % (depth + 1, origPos, abbrevCode) @@ -102549,17 +102578,18 @@ def _decodeForm(table, pos, form, addrSize, dwarfFormat, addStr=""): continue try: - # get a DIE # if not abbrevCode in abbrevDict[idx]: raise Exception("no table") + + # get a DIE # value = abbrevDict[idx][abbrevCode] tagid = value["tag"] tag = ElfAnalyzer.DW_TAG_MAP[tagid] child = value["child"] attrs = value["attrs"] - # print # - if debug and isTarget: + # print abbrev number # + if printable: printStr = ( " <%s><%x>: Abbrev Number: %s (%s)\n" % (depth, origPos, abbrevCode, tag) @@ -102646,8 +102676,12 @@ def _decodeForm(table, pos, form, addrSize, dwarfFormat, addStr=""): elif form == 0x17: typeAttr["loc"] = value + # stmt_list # + if at == 0x10: + stmt_list.append(value) + # print # - if debug and isTarget: + if printable: # type/sibling/abstract_origin # if at in (0x49, 0x01, 0x31): try: @@ -102677,9 +102711,6 @@ def _decodeForm(table, pos, form, addrSize, dwarfFormat, addStr=""): value, lstrip(enc, "DW_ATE_"), ) - # stmt_list # - elif at == 0x10: - stmt_list.append(value) helper = addStr if addStr else value @@ -102716,7 +102747,7 @@ def _decodeForm(table, pos, form, addrSize, dwarfFormat, addStr=""): # print progress # UtilMgr.deleteProgress() - if debug and isTarget: + if printable: printer("%s\n" % oneLineLoc) if "DEBUGINFODICT" in SysMgr.environList: SysMgr.printPipe( @@ -102728,7 +102759,6 @@ def _decodeForm(table, pos, form, addrSize, dwarfFormat, addStr=""): fd = origFd # check debug_line section # - # TODO: print debug_line info # if debuginfo and e_shdbgline >= 0: ( sh_name, @@ -102748,6 +102778,7 @@ def _decodeForm(table, pos, form, addrSize, dwarfFormat, addStr=""): # check target # isTarget = UtilMgr.isValidStr(shname, targetSect) + printable = debug and isTarget # set position # fd.seek(sh_offset) @@ -102781,15 +102812,16 @@ def _decodeForm(table, pos, form, addrSize, dwarfFormat, addStr=""): DW_LNE = ElfAnalyzer.DW_LNE contents = [] + lineTable = [] - if debug and isTarget: + if printable: printer( ("\n[%s Section] (nrItem: %s) (nrStmt: %s)\n%s\n\n") % ( shname, convNum(nrItems), convNum(len(stmt_list)), - twoLineLoc[:74], + twoLine, ) ) @@ -103181,10 +103213,10 @@ def _addEntryOldState(cmd, args, isExtended=False): fidx = 0 # print menu # - if debug and isTarget: + if printable: printer( - "{4:1}\n{0:<40} {1:>7} {2:>18} {3:>5}|\n{4:1}".format( - "File", "Line", "Addr", "Stmt", oneLineLoc[:74] + "{5:1}\n{0:<40} {1:>7} {2:>18} {3:>5} {4:<1}\n{5:1}".format( + "File", "Line", "Addr", "Stmt", "Function", oneLine ) ) @@ -103198,6 +103230,7 @@ def _addEntryOldState(cmd, args, isExtended=False): SysMgr.printWarn("failed to get line info", True, True) continue + # new file # if state is None: if cmd == DW_LNS["DW_LNS_set_file"]: fname, dindex, mtime, length = filelist[ @@ -103206,20 +103239,24 @@ def _addEntryOldState(cmd, args, isExtended=False): if type(fname) is bytes: fname = fname.decode() - if dindex == 0: - printer("{0:<73}|".format("./%s:[++]" % fname)) - else: - dname = dirlist[dindex - 1] - if type(dname) is bytes: - dname = dname.decode() - printer( - "{0:<73}|".format( - "%s/%s:" % (dname, fname) + if printable: + if dindex == 0: + printer( + "{0:<1}".format("./%s:[++]" % fname) + ) + else: + dname = dirlist[dindex - 1] + if type(dname) is bytes: + dname = dname.decode() + printer( + "{0:<1}".format( + "%s/%s:" % (dname, fname) + ) ) - ) elif cmd == DW_LNE["DW_LNE_define_file"]: # TODO: implement more # pass + # line number # else: fname = filelist[state["file"] - 1][fidx] if type(fname) is bytes: @@ -103227,7 +103264,7 @@ def _addEntryOldState(cmd, args, isExtended=False): line = ( state["line"] if not state["endSequence"] else "-" ) - addr = hex(state["addr"]) + addr = state["addr"] maxop = ( " " if (ver < 4 or maxOpPerInstr == 1) @@ -103238,27 +103275,53 @@ def _addEntryOldState(cmd, args, isExtended=False): if state["isstmt"] and not state["endSequence"] else " " ) - if debug and isTarget: + lineTable.append([addr, line, fname]) + + if printable: + # get function name # + try: + f = self.getSymbolByOffset(addr)[0] + except SystemExit: + sys.exit(0) + except: + f = "" + printer( - "{0:<40} {1:>7} {2:>18} {3:>5}|".format( - fname, convNum(line), addr, isstmt + "{0:<40} {1:>7} {2:>18} {3:>5} {4:<1}".format( + fname, convNum(line), hex(addr), isstmt, f ) ) if cmd == DW_LNS["DW_LNS_copy"]: pass - if debug and isTarget: + if printable: if entries: - printer("%s\n" % oneLineLoc[:74]) + printer("%s\n" % oneLine) else: - printer("\tNone\n%s\n" % oneLineLoc[:74]) + printer("\tNone\n%s\n" % oneLine) - if debug and isTarget: + if printable: if stmt_list: - printer("\n%s\n" % oneLineLoc[:74]) + printer("\n%s\n" % oneLine) else: - printer("\tNone\n%s\n" % oneLineLoc[:74]) + printer("\tNone\n%s\n" % oneLine) + + for x in sorted(lineTable, key=lambda x: x[0]): + addr, line, fname = x + + # get source index # + try: + sidx = self.srcIdxTable[fname] + except SystemExit: + sys.exit(0) + except: + sidx = len(self.srcTable) + self.srcIdxTable[fname] = sidx + self.srcTable.append(fname) + + self.sortedAddrLineTable.append(addr) + self.sortedLineTable.append([sidx, line]) # recover original fd # if isCompressed: @@ -103334,8 +103397,9 @@ def _addEntryOldState(cmd, args, isExtended=False): # check target # isTarget = UtilMgr.isValidStr(shname, targetSect) + printable = debug and isTarget - if debug and isTarget: + if printable: printer( ("\n[%s Section] (nrItem: %s)\n%s\n%16s %20s %1s\n%s") % ( @@ -103397,7 +103461,7 @@ def _addEntryOldState(cmd, args, isExtended=False): else: printer("%016x %20s %1s" % (d_tag, d_tag, hex(d_un))) - if debug and isTarget: + if printable: printer("%s\n\n\n" % oneLineLoc)