From: Frank Schaefer <frank.schafer@setuza.cz>
To: gdb@sources.redhat.com
Subject: Problems debugging forked processes
Date: Fri, 19 Apr 2002 00:45:00 -0000 [thread overview]
Message-ID: <1019202349.3213.5.camel@ADMIN> (raw)
[-- 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 );
}
next reply other threads:[~2002-04-19 7:45 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-04-19 0:45 Frank Schaefer [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1019202349.3213.5.camel@ADMIN \
--to=frank.schafer@setuza.cz \
--cc=gdb@sources.redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox