Skip to content

Commit

Permalink
Wrap cmd to return output as a string instead of printing
Browse files Browse the repository at this point in the history
  • Loading branch information
dshikashio committed May 4, 2022
1 parent eed8fbc commit b06f4f7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 15 additions & 3 deletions pybag/dbgeng/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,14 +374,26 @@ class DbgEngCallbacks(CoClass):
comtypes.errorinfo.ISupportErrorInfo,
comtypes.connectionpoints.IConnectionPointContainer]

def __init__(self, event_handler, outfn):
def __init__(self, event_handler, stdout):
super().__init__()
self.outfn = outfn
self._stdout_orig = stdout
self.stdout = stdout
self._ev = event_handler

@property
def stdout(self):
return self._stdout

@stdout.setter
def stdout(self, stdout):
self._stdout = stdout

def reset_stdout(self):
self.stdout = self._stdout_orig

def IDebugOutputCallbacks_Output(self, mask, text):
#print("OutputCallbacks Output called {}".format(mask))
self.outfn(text.decode('utf-8')) # XXX - can just be decode? no utf-8
self.stdout.write(text.decode('utf-8')) # XXX - can just be decode? no utf-8

#
# The Event callbacks need a 'this' parameter. It is special to comtypes and lets us
Expand Down
8 changes: 7 additions & 1 deletion pybag/pydbg.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
import itertools
import queue
import struct
Expand Down Expand Up @@ -33,7 +34,7 @@ def InitComObjects(Dbg):
Dbg.mod = Modules(Dbg._dataspaces, Dbg._symbols)
Dbg.events = EventHandler(Dbg)
Dbg.breakpoints = Breakpoints(Dbg._control)
Dbg.callbacks = DbgEngCallbacks(Dbg.events, sys.stdout.write)
Dbg.callbacks = DbgEngCallbacks(Dbg.events, sys.stdout)

Dbg.events.breakpoint(Dbg.breakpoints)

Expand Down Expand Up @@ -240,7 +241,12 @@ def wait(self, timeout=DbgEng.WAIT_INFINITE):

def cmd(self, cmdline):
"""cmd(cmdline) -> execute a windbg console command"""
buffer = io.StringIO()
self.callbacks.stdout = buffer
self._control.Execute(cmdline)
self.callbacks.reset_stdout()
buffer.seek(0)
return buffer.read()

def bitness(self):
"""bitness() -> Return target bitness"""
Expand Down

0 comments on commit b06f4f7

Please sign in to comment.