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.
Collecting callstacks of all threads within the process
Re: Collecting callstacks of all threads within the process
You can use this function exported by madExcept:
You can leave all parameters "empty", except the first.
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;
Re: Collecting callstacks of all threads within the process
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;
}
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;
}