Search code examples
python-2.7callbackwindbgbreakpointspykd

pykd: setBp with callback gives typeerror


I am trying to write a heap tracer using windbg and pykd, but I am getting a TypeError when trying to set the callback.

Here is a part of the code:


def enter_call_back(bp):
    print "RtlAllocateHeap called"
    return False

def return_call_back(bp):
    print "RtlAllocateHeap returned"
    return False



add = get_address("ntdll!RtlAllocateHeap")
bp_init = pykd.setBp(int(add, 16), enter_call_back)
bp_end = None

When I try to run it I get the following error:

0:000> !py C:\Users\tobbe\Documents\Projects\HeapTrace\heap_trace.py
hej


TypeError: enter_call_back() takes exactly 1 argument (0 given)

I am using python version 2.7;

0:000> !py
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

and pykd version 2.0.0.22

0:000> !pykd.info

pykd bootstrapper version: 2.0.0.22

Installed python:

Version:        Status:     Image:
------------------------------------------------------------------------------
* 2.7 x86-32    Unloaded    C:\WINDOWS\SYSTEM32\python27.dll
  3.7 x86-32    Unloaded    C:\Users\Tobias.Lorek\AppData\Local\Programs\Python\Python37-32\python37.dll

I originally tried to follow the following tutorial but got a similar error, https://labs.f-secure.com/archive/heap-tracing-with-windbg-and-python/

Any help would be appreciated.

Regards,


Solution

  • enter_call_back should not has no argumnets.

    see example from pykd tests: https://githomelab.ru/pykd/pykd/blob/0.3.2/test/scripts/breakpoint.py#L67

     def stopOnBreak():
         return pykd.eventResult.Break
    
     def testBreakCallback(self):
          breakCount = callCounter(stopOnBreak)
          bp = pykd.setBp( self.targetModule.CdeclFunc, breakCount )
          self.assertEqual( pykd.executionStatus.Break, pykd.go() )
          self.assertEqual( 1, breakCount.count )