LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2011, 04:16 PM   #1
madsovenielsen
Member
 
Registered: Aug 2009
Posts: 182

Rep: Reputation: 15
Cygwin gcc compiled code generates a stackdump - How to analyze with Gdb


Hello.

I have compiled a program i have modified in C, the basic functionality is this:

Start client.exe (temp. name)
Select file to send to server.exe (Over socket)

This will in the future after some more development be my backup client and server.

However i am having some problems when i try to actually send a file.

Client.c
Code:
/*
* THIS PROGRAM IS FREE SOFTWARE, PLEASE USE IT FOR WHATEVER YOU SEE FIT
* COMMERCIALLY OR NON-COMMERSIALLY. YOU ARE WELCOME TO DISTRIBUTE THIS
* PROGRAM AND/OR SOURCCODE UNDER WHATEVER LICENSE YOU LIKE.   
*
* Date: 08-03-2011
*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>

void error(const char *msg)
{ 
	perror(msg); exit(0);
}

int main(int argc, char *argv[])
{
	int sockfd, portno, n;
	struct sockaddr_in serv_addr;
	struct hostent *server;
	/*char buffer[256];*/

	if(argc < 3)
	{
		fprintf(stderr,"usage %s hostname port\n", argv[0]);
		exit(0);
	}

	/* Get string from standard input */

	struct stat file_stat; 
	int status; 
	char filename[50]={0}; 
	printf("Enter filename: "); 
	scanf("%s", filename); 
	int fildes = open(filename, O_RDWR);
  
	// get file status

	status = fstat(fildes, &file_stat);  

	FILE *f;
 
	/* file_stat.st_size holds the file size in bytes */

	char buffer[file_stat.st_size];
	if(f = fopen(filename, "rt"))
	{
		/*
		    We need to get the file size in bytes to determin the size for the buffer
		*/
		fread(buffer, 1, file_stat.st_size, f);
		buffer[file_stat.st_size] = 0;
		fclose(f);
		printf("\n%s\n", buffer);
	}


	portno = atoi(argv[2]);
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd < 0) error("ERROR opening socket");
	server = gethostbyname(argv[1]);

	/* Do we have a connection to the server/socket ? */
	if(server == NULL)
	{
		fprintf(stderr,"ERROR, no such host\n");
		exit(0);
	}

	bzero((char *) &serv_addr, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
	serv_addr.sin_port = htons(portno);
	if(connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting");
	printf("Please enter the message: ");
	bzero(buffer,256);

	n = write(sockfd,buffer,strlen(buffer));
	if(n < 0) error("ERROR writing to socket");
	bzero(buffer,256);
	n = read(sockfd,buffer,255);
	if(n < 0) error("ERROR reading from socket");
	printf("%s\n",buffer);
	close(sockfd);
	return 0;
}
Server.c
Not my code.
Code:
/* A simple server in the internet domain using TCP The port number is passed as an argument This version runs forever, forking off a separate process for each connection */ 
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

void dostuff(int);
/* function prototype */
void error(const char *msg)
{
	perror(msg); exit(1);
}
int main(int argc, char *argv[])
{
	int sockfd, newsockfd, portno, pid; socklen_t clilen;
	struct sockaddr_in serv_addr, cli_addr;
	if(argc < 2)
	{
		fprintf(stderr,"ERROR, no port provided\n");
		exit(1);
	}
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(sockfd < 0) error("ERROR opening socket");
	bzero((char *) &serv_addr, sizeof(serv_addr));
	portno = atoi(argv[1]);
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno);
	if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding");
	listen(sockfd,5);
	clilen = sizeof(cli_addr);
	while(1)
	{
		newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
		if(newsockfd < 0) error("ERROR on accept");
		pid = fork();
		if(pid < 0) error("ERROR on fork");
		if(pid == 0)
		{
			close(sockfd);
			dostuff(newsockfd);
			exit(0);
		}
		else close(newsockfd);
	} /* end of while */
	close(sockfd);
	return 0;
	/* we never get here */
} 
/******** DOSTUFF() *********************
There is a separate instance of this function for each connection. It handles all communication once a connnection has been established.
*****************************************/
void dostuff (int sock)
{
	int n;
	char buffer[256];
	bzero(buffer,256);
	n = read(sock,buffer,255);
	if(n < 0) error("ERROR reading from socket");
	printf("Here is the message: %s\n",buffer);
	n = write(sock,"I got your message",18);
	if (n < 0) error("ERROR writing to socket"); 
}
I get this error and stacktrace when i try to send a file over the socket. (Its client.exe that generates the error)

Code:
Exception: STATUS_ACCESS_VIOLATION at eip=61113783
eax=00000000 ebx=0022CD30 ecx=0022CBB0 edx=00000000 esi=0022CD3B edi=00000000
ebp=0022CB98 esp=0022CB94 program=C:\cygwin\home\user\source\client.exe, pid 1448, thread main
cs=001B ds=0023 es=0023 fs=003B gs=0000 ss=0023
Stack trace:
Frame     Function  Args
0022CB98  61113783  (00000000, 00000100, 00000010, 00CE1234)
0022CD08  00401363  (00000003, 00CA81E8, 00CA80E0, 00000001)
0022CD68  61007038  (00000000, 0022CDA4, 61006980, 7FFD5000)
End of stack trace
If anybody can tell me whats wrong i wouldt be very happy. But i wouldt also like to know how i read a stackdump and maybe how to use GDB to analyze the whole thing.

I have only been able to find how to read a coredump with GDB.

Any help is greatly appriciated.

/mads
 
Old 04-13-2011, 01:59 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Have you tried web search on, say,

gdb core dump tutorial

?

Also, starting from the matches of the suggested above web search you'll learn how to compile and link in a manner which is debugging-friendlier, i.e. you can get source file names and lines causing the crash.
 
Old 04-13-2011, 04:37 AM   #3
madsovenielsen
Member
 
Registered: Aug 2009
Posts: 182

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
Have you tried web search on, say,

gdb core dump tutorial

?

Also, starting from the matches of the suggested above web search you'll learn how to compile and link in a manner which is debugging-friendlier, i.e. you can get source file names and lines causing the crash.
Have you read my post ?

Quote:
Originally Posted by madsovenielsen View Post
I am getting a stackdump, not a core dump.

If anybody can tell me whats wrong i wouldt be very happy. But i wouldt also like to know how i read a stackdump and maybe how to use GDB to analyze the whole thing.

I have only been able to find how to read a coredump with GDB.
The excutable is compiled with the GCC -g switch

Last edited by madsovenielsen; 04-13-2011 at 04:38 AM. Reason: forgot
 
Old 04-13-2011, 05:18 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by madsovenielsen View Post
Have you read my post ?



The excutable is compiled with the GCC -g switch
Yes, I read your post and I intentionally wrote the search terms I wrote. I think if you follow the instructions appropriate for segmentation fault debugging, you'll get a useful trace in your case too.

And, anyway, you can run your program with breakpoints and/or step by step in 'gdb'.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Could I get rid of Cygwin.dll/Cygwin1.dll in program compiled under Cygwin ? Kunsheng Programming 1 05-17-2009 09:30 PM
analyze process dump using GDB kskkumar Linux - Software 2 05-16-2009 06:41 AM
Using gdb to analyze core dump caused by strlen() SIGSEGV bcg121 Linux - Software 4 05-15-2009 06:28 AM
LXer: Using Cscope and SilentBob to analyze source code LXer Syndicated Linux News 0 03-09-2007 04:01 PM
gdb - analyze crashes Ephracis Programming 4 08-26-2006 06:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration