Malcolm McCaffery
Malcolm McCaffery

Reputation: 2576

WinDbg Extension C++ dprintf Doesn't Display Until Function Exits

When used within a WinDbg extension is there a way to ensure dprintf updates the WinDbg console output immediately, instead of waiting until the end of the function?

This doesn't seem to be covered in the documentation here

Example code

DECLARE_API(myextension)
{
    dprintf("Print some text\r\n");
    Sleep(5000);
}

If in WinDbg I load this extension and execute it

!myextension

There is a 5 second delay before the text is printed to the console - how can I ensure this text displays immediately.

In the actual solution I have a long running task and want output to continue while the task runs; however I can reproduce the issue by using a sleep as well.

Upvotes: 1

Views: 402

Answers (1)

blabb
blabb

Reputation: 9007

wdbgexts style extensions are old and have some limitations
you should try writing engextcpp style extensions
or atleast dbgeng style extensions

here is a dbgeng style sample

#include <windows.h>
#include <dbgeng.h>
PDEBUG_CLIENT2   g_Client = NULL;
PDEBUG_CONTROL2  g_Control = NULL;
HRESULT CALLBACK  DebugExtensionInitialize(PULONG Version, PULONG Flags)
{
  *Version = DEBUG_EXTENSION_VERSION(1, 0);
  *Flags = 0;
  DebugCreate(__uuidof(IDebugClient2),(void **)&g_Client);  
  return g_Client->QueryInterface(__uuidof(IDebugControl2),(void **)&g_Control);  
}
HRESULT CALLBACK testsleep(PDEBUG_CLIENT , PCSTR )
{
  ULONG now = 0;
  g_Control->GetCurrentSystemUpTime(&now);
  g_Control->Output(DEBUG_OUTPUT_NORMAL,"sleep %x or %d now at %d\n",5000,5000,now);
  Sleep(5000);
  g_Control->GetCurrentSystemUpTime(&now);
  g_Control->Output(DEBUG_OUTPUT_NORMAL,"woke  %x or %d now at %d\n",5000,5000,now);
  return S_OK;
}

result of running !testsleep in windbg

0:000> .time
....
System Uptime: 0 days 12:50:30.766
.....
0:000> !testsleep
sleep 1388 or 5000 now at 46235
woke  1388 or 5000 now at 46240
0:000> .formats 0n12*0n3600+0n50*0n60+0n30
.....
  Decimal: 46230

sample for engextcpp style extension neat an clean dont even have to include windows.h

#include <engextcpp.hpp>
class EXT_CLASS : public ExtExtension {
public:
    EXT_COMMAND_METHOD(testsleep);
};
EXT_DECLARE_GLOBALS();
EXT_COMMAND(testsleep,"","")
{
  ULONG now =0;
  m_Control2->GetCurrentSystemUpTime(&now);
  Out("Sleeping for 5000 milliseconds from %d\n",now);
  Sleep(5000);
  m_Control2->GetCurrentSystemUpTime(&now);
  Out("Slept    for 5000 milliseconds upto %d\n",now);  
}

a run from !testSleep

0:000> !testsleep
Sleeping for 5000 milliseconds from 48921
Slept    for 5000 milliseconds upto 48926

Upvotes: 2

Related Questions