gdb attach to process to produce stacktrace

Hans Horn
Wed Sep 20 14:09:00 GMT 2006


for quite some time I was trying to figure out (e.g. how to attach gdb 
to a process in order to produce a useful stacktrace.

All attempts however produced something useless like the following:

[Switching to thread 1096.0x7f4]
* 4 thread 1096.0x7f4  0x7c901231 in ntdll!DbgUiConnectToDbg () from 
  3 thread 1096.0xce0  0x7c90eb94 in ntdll!LdrAccessResource () from 
  2 thread 1096.0x8ec  0x7c90eb94 in ntdll!LdrAccessResource () from 
  1 thread 1096.0xc40  0x7c90eb94 in ntdll!LdrAccessResource () from 
[Switching to thread 1 (thread 1096.0xc40)]#0  0x7c90eb94 in 
ntdll!LdrAccessResource () from /cygdrive/c/WINDOWS/system32/ntdll.dll
#6  0x00000000 in ?? ()
#0  0x7c90eb94 in ntdll!LdrAccessResource () from 
#1  0x7c90e9ab in ntdll!ZwWaitForMultipleObjects () from 
#2  0x7c8094e2 in KERNEL32!CreateFileMappingA () from 
#3  0x00000002 in ?? ()
#4  0x0021fe38 in ?? ()
#5  0x00000001 in ?? ()
#6  0x00000000 in ?? ()

In a new attempt a google search lead me to the thread
in which someone managed to actually produce a useful trace by attaching gdb 
to a forked child process.
This, however, only works if the parent process is going into an infinite 
while(1) loop; having it just going to sleep for a while (e.g. sleep(60)) 
does not work!

Does anybody have have an explanation for as to why the while(1) is 
Thx a gazillion for any clues!


Here's a simplified sample (originally code from

#include "stdio.h"
#include "signal.h"

static char program_name[256];

#define CMD_FILE_NAME "./.teja_gdb_cmds"

void backtrace() {
char hugebuf[512];
FILE *cmds;

cmds = fopen(CMD_FILE_NAME, "w");
if (!cmds) {
printf("unable to obtain stack trace (couldn't open cmd file)\n");
fprintf(cmds, "attach %d\n", getpid());
fprintf(cmds, "info threads\n");
fprintf(cmds, "thread 1\n");
fprintf(cmds, "bt\n");
fprintf(cmds, "detach\n");
fprintf(cmds, "quit\n");
snprintf( hugebuf, sizeof hugebuf,
"gdb -batch -x %s %s", CMD_FILE_NAME, program_name);

if (0 == fork()) {
} else {
  while(1); /*   <------------------- useful stacktrace <------------------  
/* sleep(60); */  /*   <------------------- useless stacktrace 
<------------------ */

void handler(){

int main(int argc, char **argv) {
strcpy(program_name, argv[0]);
return 0;

Unsubscribe info:
Problem reports:

More information about the Cygwin mailing list