Subject: problem debugging application that calls getservbyname()
From: Brian Dessent <[email-masked]>
To: [email-masked]
Date: Sun, 03 Apr 2005 22:22:56 -0700

I'm trying to debug an application, but when I run it under the debugger
it always dies during the application's initial config file reading,
well before the actual place I want to debug.  Outside of the debugger
this part runs fine.

I managed to reduce it down to the following simple testcase:

#include <string.h>
#include <netdb.h>

int main(char **argv, int argc)
{
  struct servent *p;

  if((p = getservbyname("smtp", "tcp"))) {
    printf("success\n");
  } else {
    printf("failure\n");
  }  
}

Simple enough, no?

$ ./getservbyname 
success

However, when I try to run this with gdb, the call to getservbyname()
dies somewhere in the autoloading code of Cygwin:

$ gdb --quiet getservbyname
(gdb) br main
Breakpoint 1 at 0x40107e: file getservbyname.c, line 8.
(gdb) run
Starting program: /tmp/getservbyname.exe 

Breakpoint 1, main (argv=0x1, argc=168109856) at getservbyname.c:8
8         if((p = getservbyname("smtp", "tcp"))) {
(gdb) n

Program received signal SIGTRAP, Trace/breakpoint trap.
0x7801b8a8 in RpcRaiseException () from /winxp/system32/rpcrt4.dll
(gdb) c
Continuing.

Program received signal SIGSEGV, Segmentation fault.
0x0022e999 in ?? ()
(gdb) c
Continuing.
failure

Program exited with code 010.
(gdb) 

The backtrace at the SIGTRAP seems to show that the stack frame is
trashed:

#0  0x7801b8a8 in RpcRaiseException () from /winxp/system32/rpcrt4.dll
#1  0x0022e984 in ?? ()
#2  0x780794f3 in RPCRT4!I_RpcReallocPipeBuffer () from
/winxp/system32/rpcrt4.dll
#3  0x76f2319f in DnsDllInit@12 ()
#4  0x76f232a3 in DnsRecordBuild_UTF8 ()
#5  0x71a53e9d in _libkernel32_a_iname () from
/winxp/System32/mswsock.dll
#6  0x00010004 in ?? ()
#7  0x00000000 in ?? () from 

I tried to step through it a little more, and it seems like everything
works ok as the code goes through the autoload stuff and eventually
calls WSAStartup(), but immediately after that the stack seems to get
all weird:

$ gdb --quiet getservbyname
(gdb) b main
Breakpoint 1 at 0x40107e: file getservbyname.c, line 8.
(gdb) run
Starting program: /tmp/getservbyname.exe 

Breakpoint 1, main (argv=0x1, argc=168109856) at getservbyname.c:8
8         if((p = getservbyname("smtp", "tcp"))) {
(gdb) b wsock_init
Breakpoint 2 at 0x61001298: file
../../../../src/winsup/cygwin/autoload.cc, line 247.
(gdb) c
Continuing.

Breakpoint 2, wsock_init () at
../../../../src/winsup/cygwin/autoload.cc:247
247       struct func_info *func = (struct func_info *)
__builtin_return_address (0);
Current language:  auto; currently c++
(gdb) n
248       struct dll_info *dll = func->dll;
(gdb) 
250       __asm__ ("                                            \n\
(gdb) 
16      {
(gdb) 
266       if (!wsock_started)
(gdb) 
271           wsastartup = (int (*)(int, WSADATA *))
(gdb) 
273           if (wsastartup)
(gdb) 
275               int res = wsastartup ((2<<8) | 2, &wsadata);
(gdb) 
0x71abddf3 in WSAEnumProtocolsA () from /winxp/System32/ws2_32.dll
(gdb) 
Single stepping until exit from function WSAEnumProtocolsA, 
which has no line number information.
0x77e74e35 in lstrcpyA () from /winxp/system32/kernel32.dll
(gdb) 
Single stepping until exit from function lstrcpyA, 
which has no line number information.
0x77e7a22b in KERNEL32!GetTickCount () from /winxp/system32/kernel32.dll
(gdb) 
Single stepping until exit from function KERNEL32!GetTickCount, 
which has no line number information.
0x77e74e41 in lstrcpyA () from /winxp/system32/kernel32.dll
(gdb) 
Single stepping until exit from function lstrcpyA, 
which has no line number information.

Program received signal SIGTRAP, Trace/breakpoint trap.
0x7801b8a8 in RpcRaiseException () from /winxp/system32/rpcrt4.dll
(gdb) 
Single stepping until exit from function RpcRaiseException, 
which has no line number information.
0x0022e984 in ?? ()
(gdb) 
Cannot find bounds of current function
(gdb) 
Cannot find bounds of current function
(gdb) 
Cannot find bounds of current function
(gdb) finish
Run till exit from #0  0x0022e984 in ?? ()

Program received signal SIGSEGV, Segmentation fault.
0x0022e999 in ?? ()

Anyway, my question is how do I debug this program?  It seems that there
is no way to get past this call to getservbyname() when using gdb, no
matter what I try.  It makes it impossible to get to the part of the
program I actually care about debugging.  Am I missing something?  I
tried putting a simple call to socket() before getservbyname() so that
winsock would alreay be initialized but I still get the same SIGTRAP
garbage, and I'm unable to run the program past that function call.

(For the record I'm running a version of cygwin1.dll built from current
CVS but the same thing happens with 1.5.{12,13,14} as well.  gdb package
is 20041228-3.)

Brian

--
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/