Collecting callstacks of all threads within the process

delphi package - madRes, madTools, madStrings, ...

Collecting callstacks of all threads within the process

Postby gbarcan » Thu Jul 26, 2012 3:50 pm

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.
gbarcan
 
Posts: 2
Joined: Thu Jul 26, 2012 3:22 pm

Re: Collecting callstacks of all threads within the process

Postby madshi » Thu Jul 26, 2012 5:07 pm

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.
madshi
Site Admin
 
Posts: 9431
Joined: Sun Mar 21, 2004 5:25 pm

Re: Collecting callstacks of all threads within the process

Postby gbarcan » Mon Jul 30, 2012 5:46 pm

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;
}
gbarcan
 
Posts: 2
Joined: Thu Jul 26, 2012 3:22 pm


Return to madBasic

Who is online

Users browsing this forum: No registered users and 1 guest

cron