Batch files for CDB debugger

Introduction

Here is the list of batch files that accompany WinDbg the Easy Way article. This article introduces CDB debugger as an effective complement to Visual Studio debugger for various advanced debugging tasks. The only major obstacle on the way to using CDB effectively is the length of the command lines – too much typing has to be done. The batch files presented here solve this problem by hiding the complexities of CDB command line parameters behind a simple and easy-to-use interface. Here you can find more information about the approach used to wrap CDB commands with batch files.

Download

Download batch files(7 Kb)

How to use

The batch files cannot run successfully if cdb.exe cannot be found on the executable search path. Since CDB is distributed as part of Debugging Tools for Windows package, it is possible, for example, to add the location of cdb.exe to the executable search path using the following command:

  PATH c:\dbgtools;%PATH%

(here c:\dbgtools represents the installation directory of Debugging Tools for Windows)

Additional information about setting up and configuring Debugging Tools for Windows can be found here.

The batch files support the following command line interface:

  batchfile.bat TargetSpec [OtherOptions] 

TargetSpec lets the user specify the target of the command. It can be an already running process, or a crash dump file. If an already running process is specified, the command is always executed in noninvasive mode.

The following values of TargetSpec can be used:

TargetSpec Description Example
-p Pid Asks CDB to attach to the process with the specified process id. The process id can be obtained from Task Manager or other similar tool. batch.bat -p 1034
-pn ExeName Asks CDB to attach to the process with the specified name of its main executable (.exe). This option is usually more convenient than "-p Pid", because we usually know the name of our application's main executable, and do not have to look for it in Task Manager. But this option cannot be used if more than one process with the given executable name is currently running (CDB will report an error). batch.bat -pn myapp.exe
-psn ServiceName Asks CDB to attach to the process that contains the specified service. For example, if you want to attach to, say, Windows Management Instrumentation service, you should use WinMgmt as the service name. batch.bat -psn MyService
-z DumpFile Asks CDB to open the specified crash dump file. batch.bat -z c:\myapp.dmp

In the examples on this page, I will use only “-pn myapp.exe” TargetSpec. But unless explicitly stated otherwise, other values of TargetSpec can also be used.

All batch files print their output to the console and also save it in out.txt file in the current directory.

Batch files

Name Usage / Description / Example
Callstacks.bat callstacks targetspec
Prints call stacks of all threads in the target process.
callstacks -pn myapp.exe
Callstack_Thread.bat callstack_thread targetspec threadid
Prints the call stack of the thread with the specified thread id.
callstack_thread -pn myapp.exe 0x128
Locks.bat locks targetspec
Prints detailed information about all critical sections currently held by the threads of the process, including information about owner threads.
locks -pn myapp.exe
Runaway.bat runaway targetspec
Prints the times spent by each of the process' threads executing user mode code.
runaway -pn myapp.exe
StackUsage.bat stackusage targetspec
Prints call stacks of all threads in the target process. For every function on every call stack, the number of bytes occupied by the function on the thread's stack is reported (in the first column).
stackusage -pn myapp.exe
StackUsage_Thread.bat stackusage_thread targetspec threadid
Prints the call stack of the thread with the specified thread id. For every function on the call stack, the number of bytes occupied by the function on the thread's stack is reported (in the first column).
stackusage_thread -pn myapp.exe 0x258
MkDump.bat mkdump targetspec dumptype filename

Creates a minidump of the target process.

Parameters:

  • dumptype: Specifies the type of the minidump (e.g. /m, /ma, /mFhutwd, or any other dump type allowed by .dump command; here you can find more information)
  • filename: Name of the minidump file

mkdump -pn myapp.exe /ma c:\myapp.dmp
MkDumpU.bat mkdumpu targetspec dumptype filename

Creates a minidump of the target process. The name of the created dump file contains a suffix that reflects the time when the dump was created.

Parameters:

  • dumptype: Specifies the type of the minidump (e.g. /m, /ma, /mFhutwd, or any other dump type allowed by .dump command; here you can find more information)
  • filename: Name of the minidump file

mkdumpu -pn myapp.exe /ma c:\myapp.dmp
DumpAnalyze.bat dumpanalyze targetspec
Analyzes the specified crash dump and displays information about the current exception, including call stack and the values of function parameters and local variables at the moment of the exception. Here you can find more information about crash dump analysis.
dumpanalyze -z c:\myapp.dmp
DumpStackCtx.bat dumpstackctx targetspec excptrsaddr

Analyzes the specified crash dump and displays information about the exception specified by the address of its EXCEPTION_POINTERS structure, including call stack and the values of function parameters and local variables at the moment of the exception. Excptrsaddr parameter is used to specify the address of EXCEPTION_POINTERS structure passed as the first parameter to kernel32!UnhandledExceptionFilter function.

Here you can find more information about crash dump analysis (search for DumpStackCtx to find additional information about using this batch file).

dumpstackctx -z c:\myapp.dmp 0x00124484
Vadump.bat vadump targetspec
Displays the virtual memory map of the target process.
vadump -pn myapp.exe
Address_All.bat address_all targetspec
Displays the virtual memory map of the target process. This batch file provides more detailed information than Vadump.bat, but works only on Windows XP and newer operating systems.
address_all -pn myapp.exe
Address.bat address targetspec addr
Displays the kind of virtual memory the specified address belongs to (for example, is it located in a heap, stack or an executable image). Addr parameter is used to specify the address. Works only on Windows XP and newer operating systems.
address -pn myapp.exe 0x00141810
Address_Summary.bat address_summary targetspec
Displays the summary information about virtual memory usage of the target process. This batch file is especially useful when we are debugging a memory leak and want to determine what kind of memory is leaked (heap, stack, raw virtual memory, and so on). Works only on Windows XP and newer operating systems.
address_summary -pn myapp.exe
ListSymbols.bat listsymbols targetspec module!symbol
Prints the list of symbols with the specified name (wildcards can be used). The output is sorted by address. Here you can find more information about using this batch file and related commands.

The following command displays the list of all member functions and static data members of CMainFrame class defined in myapp.exe module:

listsymbols -pn myapp.exe myapp!*CMainFrame*

FindSymbol.bat findsymbol targetspec address
Print the name and the start address of the symbol that occupies the specified address in memory. Information about the next symbol after the specified address is also reported.
findsymbol -pn myapp.exe 0x00402588
DumpType.bat dumptype targetspec typename
Displays detailed layout of the specified data type, including offsets of class/structure members. The type name (typename) can be prefixed with the module name for further clarity (e.g. “myapp!CMyClass”).
dumptype -pn myapp.exe CMyClass
DumpObj.bat dumpobj targetspec typename address
Displays detailed layout of the specified object with the specified type. The object address is specified via Address parameter. The type name (typename) can be prefixed with the module name for further clarity (e.g. “myapp!CMyClass”).
dumpobj -pn myapp.exe CMyClass 0x00123844