def reference(ea, **module): """Return a reference containing the module and offset of the address `ea`. If the string `module` is specified, then use it as the module name instead of the database filename. """ module = module.get('module', db.filename()) return '{:s}+{:x}'.format(module.replace(' ', ''), db.offset(ea))
def breakpoints(f=None, **kwargs): """Query the function `f` for the "break" tag, and use it to emit a list of breakpoints for windbg. If the string `module` is specified, then use it instead of the current filename when emitting the location. If the string `tagname` is provided, then use it to query instead of "break". """ tagname = kwargs.get('tagname', 'break') # if no function was provided, then recurse into ourself for all of them if f is None: for f, _ in db.selectcontents(tagname): breakpoints(f, **kwargs) return #entry, exit = func.top(f), func.bottom(f) #funcname = func.name(f) #[(entry,{tagname:'.printf "Entering {:s} %x,%x\\n",poi(@esp),@esp'.format(funcname)})], [(x,{tagname:'.printf "Exiting {:s} %x,%x\\n",poi(@esp),@esp'.format(funcname)}) for x in exit], # query the given function for the requested tagname tags, select = {}, itertools.chain(func.select(f, And=(tagname,), Or=('',))) for ea, t in select: h = tags.setdefault(ea, {}) for k in t.keys(): if k == tagname: h.setdefault(k, []).extend(t[k] if isinstance(t[k], builtins.list) else t[k].split(';')) elif operator.contains(h, k): logging.warning(u"{:s}.breakpoints({:#x}{:s}) : The specified key \"{:s}\" already exists in dictionary for address {:#x}.".format(__name__, func.addr(f), u", {:s}".format(utils.strings.kwargs(kwargs)) if kwargs else '', utils.string.escape(k, '"'), ea)) else: h[k] = t[k] continue continue # aggregate all of the discovered tags into a list of breakpoints for ea, t in tags.items(): ofs, commands = db.offset(ea), [] # create the command that emits the current label label_t = string.Template(r'.printf "$label -- $note\n"' if operator.contains(t, '') else r'.printf "$label\n"') commands.append(label_t.safe_substitute(label=label(ea), note=t.get('', ''))) # append the commands to execute when encountering the given breakpoint res = t.get(tagname, ['g']) if isinstance(res, builtins.list): commands.extend(res) else: commands.append(res) # escape all of the commands since we're going to join them together commands = (escape(cmd) for cmd in commands) six.print_('bp {:s} "{:s}"'.format(reference(ea, **kwargs), escape(';'.join(commands), depth=1))) return
def dump_breaks(func=None, tagname='break', stdout=True): if func is None: for func, _ in db.selectcontents(tagname): dump_breaks(func, tagname=tagname) return Escape = lambda s: s.replace('"', '\\"') entry, exit = fn.top(func), fn.bottom(func) funcname = fn.name(func) #[(entry,{tagname:'.printf "Entering {:s} %x,%x\\n",poi(@esp),@esp'.format(funcname)})], [(x,{tagname:'.printf "Exiting {:s} %x,%x\\n",poi(@esp),@esp'.format(funcname)}) for x in exit], select = itertools.chain(fn.select(func, And=(tagname, ), Or=('', ))) res = {} for ea, t in select: h = res.setdefault(ea, {}) for k in t.keys(): if k == tagname: h.setdefault(k, []).extend(t[k].split(';')) else: assert k not in h h[k] = t[k] continue output = [] for ea, t in res.iteritems(): ofs = db.offset(ea) commands = [] label = Template('.printf "$label -- $note\\n"' if t. has_key('') else '.printf "$label\\n"') commands.append( label.safe_substitute(label=eaToLabel(ea), note=t.get('', ''))) commands.extend(t.get(tagname, ['g'])) commands = map(windbgescape, commands) breakpoint = 'bp {:s} "{:s}"'.format(eaToReference(ea), Escape(';'.join(commands))) if stdout: print(breakpoint) output.append(breakpoint) if len(output) == 1: return output[0] + '\n' return '\n'.join(output)
def label(ea): '''Return a label for the given address `ea`.''' try: res = '{:s}{{+{:x}}}'.format(func.name(ea), db.offset(ea)) except: res = '+{:x}'.format(db.offset(ea)) return '{:s}!{:s}'.format(db.module(), res)
def eaToReference(ea): return '{:s}+{:x}'.format(db.module(), db.offset(ea))
def eaToLabel(ea): try: res = '{:s}{{+{:x}}}'.format(fn.name(ea), db.offset(ea)) except: res = '+{:x}'.format(db.offset(ea)) return '{:s}!{:s}'.format(db.module(), res)