Collecting callstacks of all threads within the process

delphi package - madRes, madTools, madStrings, ...
Post Reply
gbarcan
Posts: 2
Joined: Thu Jul 26, 2012 3:22 pm

Collecting callstacks of all threads within the process

Post by gbarcan »

Can I get some advice on how to collect the callstacks of all running threads in the process programatically ?
We have a monitoring thread and we would like at certain times to collect the callstacks for all threads and write them to a file.
madshi
Site Admin
Posts: 10749
Joined: Sun Mar 21, 2004 5:25 pm

Re: Collecting callstacks of all threads within the process

Post by madshi »

You can use this function exported by madExcept:

Code: Select all

function GetThreadStackTrace(threadId            : dword             = 0;
                             hideUglyItems       : boolean           = false;
                             showRelativeAddrs   : boolean           = false;
                             showRelativeLines   : boolean           = false;
                             stackTrace          : TPStackTrace      = nil;
                             const progressAlert : IProgressAlert    = nil;
                             dumbTrace           : boolean           = false;
                             pCrc1               : TPCardinal        = nil;
                             pCrc2               : TPCardinal        = nil;
                             pDelphiThread       : TPBoolean         = nil;
                             pMinDebugInfos      : TPDAUnicodeString = nil  ) : UnicodeString;
You can leave all parameters "empty", except the first.
gbarcan
Posts: 2
Joined: Thu Jul 26, 2012 3:22 pm

Re: Collecting callstacks of all threads within the process

Post by gbarcan »

Enumerating threads within the process using the tool help library:
http://blogs.msdn.com/b/oldnewthing/arc ... 37856.aspx

#include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>

int __cdecl main(int argc, char **argv)
{
HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (h != INVALID_HANDLE_VALUE) {
THREADENTRY32 te;
te.dwSize = sizeof(te);
if (Thread32First(h, &te)) {
do {
if (te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) +
sizeof(te.th32OwnerProcessID)) {
printf("Process 0x%04x Thread 0x%04x\n",
te.th32OwnerProcessID, te.th32ThreadID);
}
te.dwSize = sizeof(te);
} while (Thread32Next(h, &te));
}
CloseHandle(h);
}
return 0;
}
Post Reply