This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

gdb attach to process to produce stacktrace


Group,

for quite some time I was trying to figure out (e.g. 
http://thread.gmane.org/gmane.os.cygwin/58420/focus=58420) 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 
/cygdrive/c/WINDOWS/system32/ntdll.dll
  3 thread 1096.0xce0  0x7c90eb94 in ntdll!LdrAccessResource () from 
/cygdrive/c/WINDOWS/system32/ntdll.dll
  2 thread 1096.0x8ec  0x7c90eb94 in ntdll!LdrAccessResource () from 
/cygdrive/c/WINDOWS/system32/ntdll.dll
  1 thread 1096.0xc40  0x7c90eb94 in ntdll!LdrAccessResource () from 
/cygdrive/c/WINDOWS/system32/ntdll.dll
[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 
/cygdrive/c/WINDOWS/system32/ntdll.dll
#1  0x7c90e9ab in ntdll!ZwWaitForMultipleObjects () from 
/cygdrive/c/WINDOWS/system32/ntdll.dll
#2  0x7c8094e2 in KERNEL32!CreateFileMappingA () from 
/cygdrive/c/WINDOWS/system32/kernel32.dll
#3  0x00000002 in ?? ()
#4  0x0021fe38 in ?? ()
#5  0x00000001 in ?? ()
#6  0x00000000 in ?? ()


In a new attempt a google search lead me to the thread 
http://www.gatago.com/gnu/gcc/15176591.html
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 
necessary?
Thx a gazillion for any clues!

H.

Here's a simplified sample (originally code from 
http://thread.gmane.org/gmane.os.cygwin/58420/focus=58420):


#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");
return;
}
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");
fclose(cmds);
snprintf( hugebuf, sizeof hugebuf,
"gdb -batch -x %s %s", CMD_FILE_NAME, program_name);

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

void handler(){
backtrace();
exit(1);
}

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




--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]