Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-debug-dev] Attaching debugger to local process causes socket accept() to fail

Hi,

I’m trying to use the “C/C++ Attach to Local Application” option of the Eclipse debugger in order to debug a test_socket.c process that is already running. The process is supposed to wait at line 51 for the accept() function to return. However, the debugger takes me one level deeper to __accept_nocancel(), which doesn’t have any debugging information. If I try to Resume, Step Into, Step Over or Step Return, the accept() function fails by returning a negative number which causes test_socket.c to quit.

If I start test_socket.c within the Eclipse debugger everything works as it should -- the process waits at line 51, and accept() only returns when it connects to a client.

Is there a way to attach the debugger to an existing test_socket.c process without causing that process to quit? The code for test_socket.c is included in this email.

Thanks for your help,

-Vlad
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>

#define MAXPENDING 5    /* Max connection requests */
#define BUFFSIZE 32

void Die(char *mess) { perror(mess); exit(1); }

void HandleClient(int sock) {
	char buffer[BUFFSIZE];
	int received = -1;
	/* Receive message */
	if ((received = recv(sock, buffer, BUFFSIZE, 0)) < 0) {
		Die("Failed to receive initial bytes from client");
	}
	buffer[received] = '\0';
	fprintf(stdout, "Client said:");
	fprintf(stdout, buffer);
	fprintf(stdout, "\n");	
	close(sock);
}

int main(int argc, char *argv[]) {
	int serversock, clientsock;
	struct sockaddr_in echoserver, echoclient;
	
	if ((serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
		Die("Failed to create socket");
	}
	
	memset(&echoserver, 0, sizeof(echoserver));       
	echoserver.sin_family = AF_INET;                  
	echoserver.sin_addr.s_addr = htonl(INADDR_ANY);   
	echoserver.sin_port = htons(atoi("30017"));       

	if (bind(serversock, (struct sockaddr *) &echoserver, sizeof(echoserver)) < 0) {
		Die("Failed to bind the server socket");
	}
	
	if (listen(serversock, MAXPENDING) < 0) {
		Die("Failed to listen on server socket");
	}

	while (1) {
		unsigned int clientlen = sizeof(echoclient);
		if ((clientsock = accept(serversock, (struct sockaddr *) &echoclient, &clientlen)) < 0) {
			Die("Failed to accept client connection");
		}
		fprintf(stdout, "Client connected: %s\n", inet_ntoa(echoclient.sin_addr));
		HandleClient(clientsock);
	}
	
	return 0;
}

Back to the top