Mirror of the gdb mailing list
 help / color / mirror / Atom feed
* Problems debugging forked processes
@ 2002-04-19  0:45 Frank Schaefer
  2002-04-19  9:10 ` Daniel Jacobowitz
  0 siblings, 1 reply; 6+ messages in thread
From: Frank Schaefer @ 2002-04-19  0:45 UTC (permalink / raw)
  To: gdb

[-- Attachment #1: Type: text/plain, Size: 1869 bytes --]

Hi there,

I've written a network daemon and client. The daemon forks to serve a
client request.
Everything runs fine outside of the debugger, but I want to trace the
code at least once, before I release it.

Here goes my problem:
# gdb daemon
GNU gdb 5.0
Copyright 2000 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you
are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for
details.
This GDB was configured as "i686-pc-linux-gnu"...
(gdb) b 13
Breakpoint 1 at 0x8048899: file daemon.c, line 13.
(gdb) show follow-fork-mode
Debugger response to a program call of fork or vfork is "parent".
(gdb) set follow-fork-mode child
(gdb) show follow-fork-mode
Debugger response to a program call of fork or vfork is "child".
(gdb) run
Starting program: /v_dsk/home/p10209/Tests/daemon 

Breakpoint 1, main () at syncboot.c:15
15      int                On = 1;
(gdb) n
...
(gdb) n
67   ChildPid = fork();
(gdb) n
69      sleep( 10 );
(gdb) print ChildPid
$1 = 3700

So it seems, I'm tracing the parent process - don't it? The same
happens, If I do:
	(gdb) set follow-fork-mode ask
instead of child. I'm not asked anything and remain debugging the
parent.

If I use the attach method from a second instance of gdb ( there's the
sleep() above for ), the child process exits on exceptions ( mostly
segmentation fault and sometimes invalid operation ) on different
(random???) lines in the code. This happens everytime in the fixup()
from ld.linux.so.

I'm using gdb 5.0, my compiler is gcc 2.95.3 on a glibc-2.2.4
linux-2.4.16 system. I have Binutils 2.11 installed.

Furtheron I've attached reduced versions of the code, for which the
fault still occurs.

Any suggestions?

Regards
Frank

[-- Attachment #2: daemon.c --]
[-- Type: text/x-c, Size: 2822 bytes --]

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include <netdb.h>

#define PORT 1023

const char MESSAGE[] = "Hi there!\n";

int main( void ) {
int                SrvSocket, ChildPid;
int                On = 1;
struct linger      Linger = { 0 };
char               Hostname[80];
struct hostent     *pHostEnt = NULL;
struct sockaddr_in ServerName = { 0 };

   SrvSocket = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
   if( SrvSocket == -1 ) {
      perror( "Can't create socket!\n" );
      exit( 1 );
   }
   if( setsockopt( SrvSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&On, sizeof( On ) ) ) {
      perror( "Can't set reuse address on socket!\n" );
      exit( 1 );
   }
   Linger.l_onoff = 1;
   Linger.l_linger = 20;
   if( setsockopt( SrvSocket, SOL_SOCKET, SO_LINGER, (const char*)&Linger, sizeof( Linger ) ) ) {
      perror( "Can't set lingering for socket!\n" );
      exit( 1 );
   }
   if( gethostname( Hostname, sizeof( Hostname ) ) ) {
      perror( " Can't get my own hostname!\n" );
      exit( 1 );
   }
   pHostEnt = gethostbyname( Hostname );
   if( ! pHostEnt ) {
      perror( "Can't get hostentry!\n" );
      exit( 1 );
   }
   memset( &ServerName, 0, sizeof( ServerName ) );
   memcpy( &ServerName.sin_addr, pHostEnt->h_addr, pHostEnt->h_length );
   ServerName.sin_family = AF_INET;
   ServerName.sin_port = htons( PORT );
   if( bind( SrvSocket, (struct sockaddr*)&ServerName, sizeof( ServerName ) ) ) {
      perror( "Can't bind socket!\n" );
      exit( 1 );
   }
   if( listen(SrvSocket, BACKLOG ) ) {
      perror( "Can't listen on socket!\n" );
      exit( 1 );
   }
   while( 1 ) {
      struct sockaddr_in ClientName;
      int SlaveSocket, ClientLength = sizeof( ClientName );

      memset( &ClientName, 0, sizeof( ClientName ) );
      SlaveSocket = accept( SrvSocket, (struct sockaddr*)&ClientName, &ClientLength );
      if( SlaveSocket == -1 ) {
         perror( "Can't accept connection!\n" );
         exit( 1 );
      }
      ChildPid = fork();

sleep( 10 );

      switch( ChildPid ) {
         case -1:
            perror( "Can't fork!\n" );
            exit( 1 );
         case 0:
            close( SrvSocket );
            if( getpeername( SlaveSocket, (struct sockaddr*)&ClientName, &ClientLength )) {
               perror( "Can't get peername!\n" );
            } else {
               printf( "Connection request from %s\n", inet_ntoa( ClientName.sin_addr) );
            }
            write( SlaveSocket, MESSAGE, strlen( MESSAGE ) );
            close( SlaveSocket );
            exit( 0 );
         default:
            close( SlaveSocket );
      }
   }
   exit( 0 );
}

[-- Attachment #3: client.c --]
[-- Type: text/x-c, Size: 1452 bytes --]

#include "syncboot.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <libgen.h>

int main( int argc, char* argv[] ) {
int			CltSocket, Status = 0;
char			*RemHost = NULL;
char			*ProgName = basename( argv[0] );
char			Buff[256] = "";
struct hostent		*pHostEnt;
struct sockaddr_in	SrvName;

   RemHost = argv[1];
   CltSocket = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
   if( CltSocket == -1 ) {
      perror( "Can't create socket!\n" );
      exit( 1 );
   }
   pHostEnt = gethostbyname( RemHost );
   if( ! pHostEnt ) {
      pHostEnt = gethostbyaddr( RemHost, strlen( RemHost ), AF_INET );
   }
   if( ! pHostEnt ) {
      fprintf( stderr,"%s: Couldn't resolve remote host %s", ProgName, RemHost );
      exit( 1 );
   }
   SrvName.sin_family = AF_INET;
   SrvName.sin_port = htons( PORT );
   memcpy( &SrvName.sin_addr, pHostEnt->h_addr, pHostEnt->h_length );
   Status = connect( CltSocket, (struct sockaddr*)&SrvName, sizeof( SrvName ) );
   if( Status == -1 ) {
      perror( "Can't connect!\n" );
      exit( 1 );
   }
   while( ( Status = read( CltSocket, Buff, sizeof( Buff) -1 ) ) > 0 ) {
      printf( "%d : %s\n", Status, Buff );
   }
   if( Status == -1 ){
      perror( "Read error!\n" );
   }
   close( CltSocket );
   exit( 0 );
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2002-04-22  5:47 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-04-19  0:45 Problems debugging forked processes Frank Schaefer
2002-04-19  9:10 ` Daniel Jacobowitz
2002-04-20 22:14   ` Eli Zaretskii
2002-04-20 22:18     ` Andrew Cagney
2002-04-20 22:21     ` Daniel Jacobowitz
2002-04-21 22:47   ` Frank Schaefer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox