def checkmarks(): '''Output all functions (sys.stdout) containing more than 1 mark.''' res = [] for a, m in database.marks(): try: res.append((function.top(a), a, m)) except ValueError: pass continue d = list(res) d.sort(lambda a, b: cmp(a[0], b[0])) flookup = {} for fn, a, m in d: try: flookup[fn].append((a, m)) except: flookup[fn] = [(a, m)] continue functions = [(k, v) for k, v in flookup.items() if len(v) > 1] if not functions: logging.warning( 'There are no functions available containing multiple marks.') return for k, v in functions: print >> sys.stdout, "{:x} : in function {:s}".format( k, function.name(function.byAddress(k))) print >> sys.stdout, '\n'.join( ("- {:x} : {:s}".format(a, m) for a, m in sorted(v))) return
def checkmarks(): """Emit all functions that contain more than 1 mark within them. As an example, if marks are used to keep track of backtraces then this tool will emit where those backtraces intersect. """ listable = [] for a, m in database.marks(): try: listable.append((func.top(a), a, m)) except internal.exceptions.FunctionNotFoundError: pass continue d = listable[:] d.sort(key=lambda item: item[0]) flookup = {} for fn, a, m in d: try: flookup[fn].append((a, m)) except: flookup[fn] = [(a, m)] continue functions = [ (k, v) for k, v in flookup.items() if len(v) > 1 ] if not functions: logging.warning('There are no functions available containing multiple marks.') return for k, v in functions: six.print_("{:#x} : in function {:s}".format(k, func.name(func.by_address(k))), file=sys.stdout) six.print_('\n'.join(("- {:#x} : {:s}".format(a, m) for a, m in sorted(v))), file=sys.stdout) return
def checkmarks(): """Emit all functions that contain more than 1 mark within them. As an example, if marks are used to keep track of backtraces then this tool will emit where those backtraces intersect. """ res = [] for a, m in database.marks(): try: res.append((func.top(a), a, m)) except internal.exceptions.FunctionNotFoundError: pass continue d = list(res) d.sort( lambda a, b: cmp(a[0], b[0]) ) flookup = {} for fn, a, m in d: try: flookup[fn].append((a, m)) except: flookup[fn] = [(a, m)] continue functions = [ (k, v) for k, v in flookup.items() if len(v) > 1 ] if not functions: logging.warning('There are no functions available containing multiple marks.') return for k, v in functions: print >>sys.stdout, "{:#x} : in function {:s}".format(k, func.name(func.byAddress(k))) print >>sys.stdout, '\n'.join( ("- {:#x} : {:s}".format(a, m) for a, m in sorted(v)) ) return
def recovermarks(): '''Utilizing any tag information found in the database, recreate all the database marks.''' # collect result = [] for fn,l in database.select('marks'): m = set( (l['marks']) if hasattr(l['marks'],'__iter__') else [int(x,16) for x in l['marks'].split(',')] if type(l['marks']) is str else [l['marks']]) res = [(ea,d['mark']) for ea,d in function.select(fn,'mark')] if m != set(a for a,_ in res): logging.warning("%x: ignoring cached version of marks due to being out-of-sync with real values : %r : %r", fn, map(hex,m), map(hex,set(a for a,_ in res))) result.extend(res) result.sort(cmp=lambda x,y: cmp(x[1],y[1])) # discovered marks versus database marks result = dict(result) current = {ea:descr for ea,descr in database.marks()} # create tags for x,y in result.items(): if (x not in current) or (current[x] != result[x]): if current[x] != result[x]: logging.info('%x: database tag is newer than mark description : %r', x, result[x]) database.mark(x, y) continue logging.warning('%x: skipping already existing mark : %r', x, current[x]) # marks that aren't reachable in the database for ea in set(current.viewkeys()).difference(result.viewkeys()): logging.warning('%x: unreachable mark (global) : %r', ea, current[ea]) # color them colormarks()
def checkmarks(): '''Output all functions (sys.stdout) containing more than 1 mark.''' res = [] for a,m in database.marks(): try: res.append((function.top(a), a, m)) except ValueError: pass continue d = list(res) d.sort( lambda a,b: cmp(a[0], b[0]) ) flookup = {} for fn,a,m in d: try: flookup[fn].append((a,m)) except: flookup[fn] = [(a,m)] continue functions = [ (k,v) for k,v in flookup.items() if len(v) > 1 ] if not functions: logging.warning('There are no functions available containing multiple marks.') return for k,v in functions: print >>sys.stdout, '%x : in function %s'% (k,function.name(function.byAddress(k))) print >>sys.stdout, '\n'.join( ('- %x : %s'%(a,m) for a,m in sorted(v)) ) return
def recovermarks(): """Walk through the tags made by ``colormarks`` and re-create the marks that were found. This is useful if any marks were accidentally deleted and can be used for recovering them as long as they were initally tagged properly. """ # collect result = [] for fn, l in database.select('marks'): m = {item for item in l['marks']} if hasattr(l['marks'], '__iter__') else { int(item, 16) for item in l['marks'].split(',') } if isinstance(l['marks'], six.string_types) else {l['marks']} res = [(ea, d['mark']) for ea, d in func.select(fn, 'mark')] if m != {ea for ea, _ in res}: logging.warning( "{:s} : Ignoring the function tag \"{:s}\" for function {:#x} due to its value being out-of-sync with the contents values ({!s} <> {!s})." .format('.'.join([__name__, 'recovermarks']), fn, builtins.map("{:#x}".format, m), builtins.map("{:#x}".format, {ea for ea, _ in res}))) result.extend(res) result.sort(key=lambda item: item[1]) # discovered marks versus database marks result = {ea: item for ea, item in result.items()} current = {ea: descr for ea, descr in database.marks()} # create tags for x, y in result.items(): if x in current: logging.warning( "{:#x}: skipping already existing mark : {!r}".format( x, current[x])) continue # x not in current if x not in current: logging.info("{:#x}: adding missing mark due to tag : {!r}".format( x, result[x])) elif current[x] != result[x]: logging.info( "{:#x}: database tag is different than mark description : {!r}" .format(x, result[x])) else: assert current[x] == result[x] database.mark(x, y) # marks that aren't reachable in the database for ea in {item for item in current.keys() }.difference({item for item in result.keys()}): logging.warning("{:#x}: unreachable mark (global) : {!r}".format( ea, current[ea])) # color them colormarks()
def marks(fn): result = [] fn = top(fn) for ea,comment in database.marks(): try: if top(ea) == fn: result.append( (ea,comment) ) except ValueError: pass continue return result
def marks(key=None): funcea = top(key) result = [] for ea,comment in database.marks(): try: if top(ea) == funcea: result.append( (ea,comment) ) except Exception: pass continue return result
def marks(fn): result = [] fn = top(fn) for ea, comment in database.marks(): try: if top(ea) == fn: result.append((ea, comment)) except ValueError: pass continue return result
def recovermarks(): '''Utilizing any tag information found in the database, recreate all the database marks.''' # collect result = [] for fn, l in database.select('marks'): m = set((l['marks']) if hasattr(l['marks'], '__iter__') else [int(x, 16) for x in l['marks']. split(',')] if type(l['marks']) is str else [l['marks']]) res = [(ea, d['mark']) for ea, d in function.select(fn, 'mark')] if m != set(a for a, _ in res): logging.warning( "{:x}: ignoring cached version of marks due to being out-of-sync with real values : {!r} : {!r}" .format(fn, map(hex, m), map(hex, set(a for a, _ in res)))) result.extend(res) result.sort(cmp=lambda x, y: cmp(x[1], y[1])) # discovered marks versus database marks result = dict(result) current = {ea: descr for ea, descr in database.marks()} # create tags for x, y in result.items(): if x in current: logging.warning( "{:x}: skipping already existing mark : {!r}".format( x, current[x])) continue # x not in current if x not in current: logging.info("{:x}: adding missing mark due to tag : {!r}".format( x, result[x])) elif current[x] != result[x]: logging.info( "{:x}: database tag is different than mark description : {!r}". format(x, result[x])) else: assert current[x] == result[x] database.mark(x, y) # marks that aren't reachable in the database for ea in set(current.viewkeys()).difference(result.viewkeys()): logging.warning("{:x}: unreachable mark (global) : {!r}".format( ea, current[ea])) # color them colormarks()
def colormarks(color=0x7f007f): '''Iterate through all database marks and tag+color their address''' # tag and color f = set() for ea, m in database.marks(): database.tag(ea, 'mark', m) database.color(ea, color) try: f.add(function.top(ea)) except (LookupError, ValueError): pass # tag the functions too for ea in list(f): m = function.marks(ea) function.tag(ea, 'marks', [ea for ea, _ in m]) return
def colormarks(color=0x7f007f): '''Iterate through all database marks and tag+color their address''' # tag and color f = set() for ea,m in database.marks(): database.tag(ea, 'mark', m) database.color(ea, color) try: f.add(function.top(ea)) except ValueError: pass continue # tag the functions too for ea in list(f): m = function.marks(ea) function.tag(ea, 'marks', [ea for ea,_ in m]) return
def recovermarks(): """Walk through the tags made by `colormarks` and re-create the marks that were found. This is useful if any marks were accidentally deleted and can be used for recovering them as long as they were initally tagged properly. """ # collect result = [] for fn, l in database.select('marks'): m = set( (l['marks']) if hasattr(l['marks'], '__iter__') else [int(x, 16) for x in l['marks'].split(',')] if type(l['marks']) is str else [l['marks']]) res = [(ea, d['mark']) for ea, d in func.select(fn, 'mark')] if m != set(a for a, _ in res): logging.warning("{:#x}: ignoring cached version of marks due to being out-of-sync with real values : {!r} : {!r}".format(fn, builtins.map(hex, m), builtins.map(hex, set(a for a, _ in res)))) result.extend(res) result.sort(cmp=lambda x, y: cmp(x[1], y[1])) # discovered marks versus database marks result = dict(result) current = {ea : descr for ea, descr in database.marks()} # create tags for x, y in result.items(): if x in current: logging.warning("{:#x}: skipping already existing mark : {!r}".format(x, current[x])) continue # x not in current if x not in current: logging.info("{:#x}: adding missing mark due to tag : {!r}".format(x, result[x])) elif current[x] != result[x]: logging.info("{:#x}: database tag is different than mark description : {!r}".format(x, result[x])) else: assert current[x] == result[x] database.mark(x, y) # marks that aren't reachable in the database for ea in set(current.viewkeys()).difference(result.viewkeys()): logging.warning("{:#x}: unreachable mark (global) : {!r}".format(ea, current[ea])) # color them colormarks()
def recovermarks(): """Walk through the tags made by ``colormarks`` and re-create the marks that were found. This is useful if any marks were accidentally deleted and can be used for recovering them as long as they were initally tagged properly. """ # collect result = [] for fn, l in database.select('marks'): m = set( (l['marks']) if hasattr(l['marks'], '__iter__') else [int(x, 16) for x in l['marks'].split(',')] if type(l['marks']) is str else [l['marks']]) res = [(ea, d['mark']) for ea, d in func.select(fn, 'mark')] if m != { a for a, _ in res }: logging.warning("{:s} : Ignoring the function tag \"{:s}\" for function {:#x} due to its value being out-of-sync with the contents values ({!s} <> {!s}).".format('.'.join((__name__, 'recovermarks')), fn, builtins.map(hex, m), builtins.map(hex, set(a for a, _ in res)))) result.extend(res) result.sort(cmp=lambda x, y: cmp(x[1], y[1])) # discovered marks versus database marks result = dict(result) current = {ea : descr for ea, descr in database.marks()} # create tags for x, y in result.items(): if x in current: logging.warning("{:#x}: skipping already existing mark : {!r}".format(x, current[x])) continue # x not in current if x not in current: logging.info("{:#x}: adding missing mark due to tag : {!r}".format(x, result[x])) elif current[x] != result[x]: logging.info("{:#x}: database tag is different than mark description : {!r}".format(x, result[x])) else: assert current[x] == result[x] database.mark(x, y) # marks that aren't reachable in the database for ea in set(current.viewkeys()).difference(result.viewkeys()): logging.warning("{:#x}: unreachable mark (global) : {!r}".format(ea, current[ea])) # color them colormarks()
def colormarks(color=0x7f007f): """Walk through the current list of marks whilst coloring them with the specified `color`. Each mark's address is tagged with its description, and if the address belongs to a function, the function is also tagged with the address of the marks that it contains. """ # tag and color f = set() for ea, m in database.marks(): database.tag(ea, 'mark', m) if database.color(ea) is None: database.color(ea, color) try: f.add(func.top(ea)) except internal.exceptions.FunctionNotFoundError: pass continue # tag the functions too for ea in list(f): m = func.marks(ea) func.tag(ea, 'marks', [ea for ea, _ in m]) return
def tag_marks(): for ea, mark in db.marks(): mark = mark.replace('[note]', '') db.tag(ea, 'note', mark)