468,101 Members | 1,360 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,101 developers. It's quick & easy.

strange behavior: sockaddr_in makes gdb fail

FBM
Hi there,

I am puzzled with the behavior of my code.. I am working on a
networking stuff, and debugging with eclipse (GNU gdb 6.6-debian)..
The problem I am experiencing is the following:

Whenever I declare the sockaddr_in structure inside the main, the
debugger crashes at line X*, not being able to access argv parameters
(see code below). It is very strange.. by only being there,
sockaddr_in does not allow me to question argc again.. after line X*
has been executed.

However, when I take the sockaddr_in out from the main() and put it as
global variable, the problem disappears..

If at line X* I ask gdb about content of argc, the answer is 0x0.. :
(gdb) p argc
Cannot access memory at address 0x0

Any idea what may be wrong? thanks a lot.
----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>

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

int main(int argc, char *argv[]) {
long portno;
char * end_of_argv = NULL;
struct sockaddr_in serv_addr;

if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
X* portno = strtol(argv[1], &end_of_argv, 10);
printf("Hello World!\n");

return 0;
}
---

Jul 30 '07 #1
8 2791
FBM wrote:
Hi there,

I am puzzled with the behavior of my code.. I am working on a
networking stuff, and debugging with eclipse (GNU gdb 6.6-debian)..
The problem I am experiencing is the following:

Whenever I declare the sockaddr_in structure inside the main, the
debugger crashes at line X*, not being able to access argv parameters
(see code below). It is very strange.. by only being there,
sockaddr_in does not allow me to question argc again.. after line X*
has been executed.

However, when I take the sockaddr_in out from the main() and put it as
global variable, the problem disappears..

If at line X* I ask gdb about content of argc, the answer is 0x0.. :
(gdb) p argc
Cannot access memory at address 0x0

Any idea what may be wrong? thanks a lot.
----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>

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

int main(int argc, char *argv[]) {
long portno;
char * end_of_argv = NULL;
struct sockaddr_in serv_addr;

if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
X* portno = strtol(argv[1], &end_of_argv, 10);
printf("Hello World!\n");

return 0;
}
---
The headers <sys/socket.hand <netinet/in.hare not standard headers, and
different systems, or even different versions of the same system, provide
different definitions in those headers. How is struct sockaddr_in defined?
If you get rid of those two headers, and define struct sockaddr_in in your
own program, the behaviour shouldn't change. Could you do this, and then
post a standard C code sample with the same problem? If you can do that, it
will be possible to tell whether the bug is in your code or in your
environment.

For what it's worth, it's possible I haven't looked closely enough, of
course, but I don't see any obvious problems in the code you have posted.
Jul 30 '07 #2
FBM
On Jul 30, 2:43 pm, Harald van D k <true...@gmail.comwrote:
FBM wrote:
Hi there,
I am puzzled with the behavior of my code.. I am working on a
networking stuff, and debugging with eclipse (GNU gdb 6.6-debian)..
The problem I am experiencing is the following:
Whenever I declare the sockaddr_in structure inside the main, the
debugger crashes at line X*, not being able to access argv parameters
(see code below). It is very strange.. by only being there,
sockaddr_in does not allow me to question argc again.. after line X*
has been executed.
However, when I take the sockaddr_in out from the main() and put it as
global variable, the problem disappears..
If at line X* I ask gdb about content of argc, the answer is 0x0.. :
(gdb) p argc
Cannot access memory at address 0x0
Any idea what may be wrong? thanks a lot.
----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[]) {
long portno;
char * end_of_argv = NULL;
struct sockaddr_in serv_addr;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
X* portno = strtol(argv[1], &end_of_argv, 10);
printf("Hello World!\n");
return 0;
}
---

The headers <sys/socket.hand <netinet/in.hare not standard headers, and
different systems, or even different versions of the same system, provide
different definitions in those headers. How is struct sockaddr_in defined?
If you get rid of those two headers, and define struct sockaddr_in in your
own program, the behaviour shouldn't change. Could you do this, and then
post a standard C code sample with the same problem? If you can do that, it
will be possible to tell whether the bug is in your code or in your
environment.

For what it's worth, it's possible I haven't looked closely enough, of
course, but I don't see any obvious problems in the code you have posted.
thanks for your answer. I have tried your advice.. However, I cannot
simply paste the sockaddr_in as it is defined. It gives me the error:
"expected specifier-qualifier-list before '__SOCKADDR_COMMON'", and I
don't know how to continue from there.. :-(

Below I posted the code w/o the two headers in question..

----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*#include <sys/socket.h>
#include <netinet/in.h>*/

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

int main(int argc, char *argv[]) {
long portno;
char * end_of_argv = NULL;
/* Structure describing an Internet socket address. */
struct sockaddr_in
{
__SOCKADDR_COMMON (sin_);
in_port_t sin_port; /* Port number. */
struct in_addr sin_addr; /* Internet address. */

/* Pad to size of `struct sockaddr'. */
unsigned char sin_zero[sizeof (struct sockaddr) -
__SOCKADDR_COMMON_SIZE -
sizeof (in_port_t) -
sizeof (struct in_addr)];
};

struct sockaddr_in serv_addr;

if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}

portno = strtol(argv[1], &end_of_argv, 10);
printf("Hello World!\n");

return 0;
}

---

Jul 30 '07 #3
FBM wrote:
On Jul 30, 2:43 pm, Harald van D k <true...@gmail.comwrote:
>FBM wrote:
>>Hi there,
I am puzzled with the behavior of my code.. I am working on a
networking stuff, and debugging with eclipse (GNU gdb 6.6-debian)..
The problem I am experiencing is the following:
Whenever I declare the sockaddr_in structure inside the main, the
debugger crashes at line X*, not being able to access argv parameters
(see code below). It is very strange.. by only being there,
sockaddr_in does not allow me to question argc again.. after line X*
has been executed.
However, when I take the sockaddr_in out from the main() and put it as
global variable, the problem disappears..
If at line X* I ask gdb about content of argc, the answer is 0x0.. :
(gdb) p argc
Cannot access memory at address 0x0
Any idea what may be wrong? thanks a lot.
----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[]) {
long portno;
char * end_of_argv = NULL;
struct sockaddr_in serv_addr;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
X* portno = strtol(argv[1], &end_of_argv, 10);
printf("Hello World!\n");
return 0;
}
---
The headers <sys/socket.hand <netinet/in.hare not standard headers, and
different systems, or even different versions of the same system, provide
different definitions in those headers. How is struct sockaddr_in defined?
If you get rid of those two headers, and define struct sockaddr_in in your
own program, the behaviour shouldn't change. Could you do this, and then
post a standard C code sample with the same problem? If you can do that, it
will be possible to tell whether the bug is in your code or in your
environment.

For what it's worth, it's possible I haven't looked closely enough, of
course, but I don't see any obvious problems in the code you have posted.

thanks for your answer. I have tried your advice.. However, I cannot
simply paste the sockaddr_in as it is defined. It gives me the error:
"expected specifier-qualifier-list before '__SOCKADDR_COMMON'", and I
don't know how to continue from there.. :-(

Below I posted the code w/o the two headers in question..

----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*#include <sys/socket.h>
#include <netinet/in.h>*/

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

int main(int argc, char *argv[]) {
long portno;
char * end_of_argv = NULL;
/* Structure describing an Internet socket address. */
struct sockaddr_in
{
__SOCKADDR_COMMON (sin_);
in_port_t sin_port; /* Port number. */
struct in_addr sin_addr; /* Internet address. */

/* Pad to size of `struct sockaddr'. */
unsigned char sin_zero[sizeof (struct sockaddr) -
__SOCKADDR_COMMON_SIZE -
sizeof (in_port_t) -
sizeof (struct in_addr)];
};

struct sockaddr_in serv_addr;

if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}

portno = strtol(argv[1], &end_of_argv, 10);
printf("Hello World!\n");

return 0;
}

---
Which system are you using?

This is a gdb bug. A debugger should never crash.
Probably there is a problem with the debug information/executable
format/whatever.
Jul 30 '07 #4
FBM
On Jul 30, 2:57 pm, jacob navia <ja...@jacob.remcomp.frwrote:
FBM wrote:
On Jul 30, 2:43 pm, Harald van D k <true...@gmail.comwrote:
FBM wrote:
Hi there,
I am puzzled with the behavior of my code.. I am working on a
networking stuff, and debugging with eclipse (GNU gdb 6.6-debian)..
The problem I am experiencing is the following:
Whenever I declare the sockaddr_in structure inside the main, the
debugger crashes at line X*, not being able to access argv parameters
(see code below). It is very strange.. by only being there,
sockaddr_in does not allow me to question argc again.. after line X*
has been executed.
However, when I take the sockaddr_in out from the main() and put it as
global variable, the problem disappears..
If at line X* I ask gdb about content of argc, the answer is 0x0.. :
(gdb) p argc
Cannot access memory at address 0x0
Any idea what may be wrong? thanks a lot.
----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[]) {
long portno;
char * end_of_argv = NULL;
struct sockaddr_in serv_addr;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
X* portno = strtol(argv[1], &end_of_argv, 10);
printf("Hello World!\n");
return 0;
}
---
The headers <sys/socket.hand <netinet/in.hare not standard headers, and
different systems, or even different versions of the same system, provide
different definitions in those headers. How is struct sockaddr_in defined?
If you get rid of those two headers, and define struct sockaddr_in in your
own program, the behaviour shouldn't change. Could you do this, and then
post a standard C code sample with the same problem? If you can do that, it
will be possible to tell whether the bug is in your code or in your
environment.
For what it's worth, it's possible I haven't looked closely enough, of
course, but I don't see any obvious problems in the code you have posted.
thanks for your answer. I have tried your advice.. However, I cannot
simply paste the sockaddr_in as it is defined. It gives me the error:
"expected specifier-qualifier-list before '__SOCKADDR_COMMON'", and I
don't know how to continue from there.. :-(
Below I posted the code w/o the two headers in question..
----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*#include <sys/socket.h>
#include <netinet/in.h>*/
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[]) {
long portno;
char * end_of_argv = NULL;
/* Structure describing an Internet socket address. */
struct sockaddr_in
{
__SOCKADDR_COMMON (sin_);
in_port_t sin_port; /* Port number. */
struct in_addr sin_addr; /* Internet address. */
/* Pad to size of `struct sockaddr'. */
unsigned char sin_zero[sizeof (struct sockaddr) -
__SOCKADDR_COMMON_SIZE -
sizeof (in_port_t) -
sizeof (struct in_addr)];
};
struct sockaddr_in serv_addr;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
portno = strtol(argv[1], &end_of_argv, 10);
printf("Hello World!\n");
return 0;
}
---

Which system are you using?

This is a gdb bug. A debugger should never crash.
Probably there is a problem with the debug information/executable
format/whatever.
I am using Ubuntu v. 7.04

fbm_laptop:~/workspace/server_app/Debug$ gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: (...)
Thread model: posix
gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4)

tnx.

Jul 30 '07 #5
FBM wrote:
On Jul 30, 2:43 pm, Harald van D k <true...@gmail.comwrote:
>FBM wrote:
Hi there,
I am puzzled with the behavior of my code.. I am working on a
networking stuff, and debugging with eclipse (GNU gdb 6.6-debian)..
The problem I am experiencing is the following:
Whenever I declare the sockaddr_in structure inside the main, the
debugger crashes at line X*, not being able to access argv parameters
(see code below). It is very strange.. by only being there,
sockaddr_in does not allow me to question argc again.. after line X*
has been executed.
However, when I take the sockaddr_in out from the main() and put it as
global variable, the problem disappears..

The headers <sys/socket.hand <netinet/in.hare not standard headers,
and different systems, or even different versions of the same system,
provide different definitions in those headers. How is struct sockaddr_in
defined? If you get rid of those two headers, and define struct
sockaddr_in in your own program, the behaviour shouldn't change. Could
you do this, and then post a standard C code sample with the same
problem? If you can do that, it will be possible to tell whether the bug
is in your code or in your environment.

For what it's worth, it's possible I haven't looked closely enough, of
course, but I don't see any obvious problems in the code you have posted.

thanks for your answer. I have tried your advice.. However, I cannot
simply paste the sockaddr_in as it is defined. It gives me the error:
"expected specifier-qualifier-list before '__SOCKADDR_COMMON'", and I
don't know how to continue from there.. :-(

Below I posted the code w/o the two headers in question..
You haven't included the macros and typedefs referenced by struct
sockaddr_in. Your definition of the structure looks identical to mine, so I
took the liberty of plugging in my system's, leaving me (after
reformatting, because it had got botched up) with this program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

typedef unsigned short int sa_family_t;

struct sockaddr
{
sa_family_t sa_family;
char sa_data[14];
};

typedef uint16_t in_port_t;
typedef uint32_t in_addr_t;

struct in_addr
{
in_addr_t s_addr;
};

struct sockaddr_in
{
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;

unsigned char sin_zero[sizeof (struct sockaddr) -
sizeof (unsigned short int) -
sizeof (in_port_t) -
sizeof (struct in_addr)];
};

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

int
main (int argc, char *argv[])
{
long portno;
char *end_of_argv = NULL;
struct sockaddr_in serv_addr;

if (argc < 2)
{
fprintf (stderr, "ERROR, no port provided\n");
exit (1);
}
portno = strtol (argv[1], &end_of_argv, 10);
printf ("Hello World!\n");

return 0;
}

Aside from the assumption that the size of sin_zero is positive, and that
uint16_t and uint32_t are available, I don't see anything non-portable in
this program. Does this program show the same behaviour on your system? If
it does, I believe it is a bug in your compiler or debugger, and I
recommend you check if an updated version is available.
Jul 30 '07 #6
Harald van Dń≥k wrote:
FBM wrote:
>On Jul 30, 2:43 pm, Harald van D k <true...@gmail.comwrote:
>>FBM wrote:
Hi there,
I am puzzled with the behavior of my code.. I am working on a
networking stuff, and debugging with eclipse (GNU gdb 6.6-debian)..
The problem I am experiencing is the following:
Whenever I declare the sockaddr_in structure inside the main, the
debugger crashes at line X*, not being able to access argv parameters
(see code below). It is very strange.. by only being there,
sockaddr_in does not allow me to question argc again.. after line X*
has been executed.
However, when I take the sockaddr_in out from the main() and put it as
global variable, the problem disappears..
The headers <sys/socket.hand <netinet/in.hare not standard headers,
and different systems, or even different versions of the same system,
provide different definitions in those headers. How is struct sockaddr_in
defined? If you get rid of those two headers, and define struct
sockaddr_in in your own program, the behaviour shouldn't change. Could
you do this, and then post a standard C code sample with the same
problem? If you can do that, it will be possible to tell whether the bug
is in your code or in your environment.

For what it's worth, it's possible I haven't looked closely enough, of
course, but I don't see any obvious problems in the code you have posted.
thanks for your answer. I have tried your advice.. However, I cannot
simply paste the sockaddr_in as it is defined. It gives me the error:
"expected specifier-qualifier-list before '__SOCKADDR_COMMON'", and I
don't know how to continue from there.. :-(

Below I posted the code w/o the two headers in question..

You haven't included the macros and typedefs referenced by struct
sockaddr_in. Your definition of the structure looks identical to mine, so I
took the liberty of plugging in my system's, leaving me (after
reformatting, because it had got botched up) with this program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

typedef unsigned short int sa_family_t;

struct sockaddr
{
sa_family_t sa_family;
char sa_data[14];
};

typedef uint16_t in_port_t;
typedef uint32_t in_addr_t;

struct in_addr
{
in_addr_t s_addr;
};

struct sockaddr_in
{
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;

unsigned char sin_zero[sizeof (struct sockaddr) -
sizeof (unsigned short int) -
sizeof (in_port_t) -
sizeof (struct in_addr)];
};

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

int
main (int argc, char *argv[])
{
long portno;
char *end_of_argv = NULL;
struct sockaddr_in serv_addr;

if (argc < 2)
{
fprintf (stderr, "ERROR, no port provided\n");
exit (1);
}
portno = strtol (argv[1], &end_of_argv, 10);
printf ("Hello World!\n");

return 0;
}

Aside from the assumption that the size of sin_zero is positive, and that
uint16_t and uint32_t are available, I don't see anything non-portable in
this program. Does this program show the same behaviour on your system? If
it does, I believe it is a bug in your compiler or debugger, and I
recommend you check if an updated version is available.
Same here. I compiled in an old Redhat 5 and it works without
any problems. GDB displays the correct values.

jacob
Jul 30 '07 #7
FBM wrote:
On Jul 30, 2:57 pm, jacob navia <ja...@jacob.remcomp.frwrote:
>FBM wrote:
On Jul 30, 2:43 pm, Harald van D k <true...@gmail.comwrote:
FBM wrote:
Hi there,
I am puzzled with the behavior of my code.. I am working on a
networking stuff, and debugging with eclipse (GNU gdb 6.6-debian)..
The problem I am experiencing is the following:
Whenever I declare the sockaddr_in structure inside the main, the
debugger crashes at line X*, not being able to access argv
parameters (see code below). It is very strange.. by only being
there, sockaddr_in does not allow me to question argc again.. after
line X* has been executed.
However, when I take the sockaddr_in out from the main() and put it
as global variable, the problem disappears..
If at line X* I ask gdb about content of argc, the answer is 0x0.. :
(gdb) p argc
Cannot access memory at address 0x0
Any idea what may be wrong? thanks a lot.
<snip>
I am using Ubuntu v. 7.04

fbm_laptop:~/workspace/server_app/Debug$ gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: (...)
Thread model: posix
gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4)
Since there's nothing obviously wrong with the code you've presented, you
might want to post to a more system specific group like
<news:comp.os.linux.development.appsand a gnu/gdb group or mailing list.

Jul 30 '07 #8
FBM
On Jul 30, 3:17 pm, Harald van D k <true...@gmail.comwrote:
FBM wrote:
On Jul 30, 2:43 pm, Harald van D k <true...@gmail.comwrote:
FBM wrote:
Hi there,
I am puzzled with the behavior of my code.. I am working on a
networking stuff, and debugging with eclipse (GNU gdb 6.6-debian)..
The problem I am experiencing is the following:
Whenever I declare the sockaddr_in structure inside the main, the
debugger crashes at line X*, not being able to access argv parameters
(see code below). It is very strange.. by only being there,
sockaddr_in does not allow me to question argc again.. after line X*
has been executed.
However, when I take the sockaddr_in out from the main() and put it as
global variable, the problem disappears..
The headers <sys/socket.hand <netinet/in.hare not standard headers,
and different systems, or even different versions of the same system,
provide different definitions in those headers. How is struct sockaddr_in
defined? If you get rid of those two headers, and define struct
sockaddr_in in your own program, the behaviour shouldn't change. Could
you do this, and then post a standard C code sample with the same
problem? If you can do that, it will be possible to tell whether the bug
is in your code or in your environment.
For what it's worth, it's possible I haven't looked closely enough, of
course, but I don't see any obvious problems in the code you have posted.
thanks for your answer. I have tried your advice.. However, I cannot
simply paste the sockaddr_in as it is defined. It gives me the error:
"expected specifier-qualifier-list before '__SOCKADDR_COMMON'", and I
don't know how to continue from there.. :-(
Below I posted the code w/o the two headers in question..

You haven't included the macros and typedefs referenced by struct
sockaddr_in. Your definition of the structure looks identical to mine, so I
took the liberty of plugging in my system's, leaving me (after
reformatting, because it had got botched up) with this program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

typedef unsigned short int sa_family_t;

struct sockaddr
{
sa_family_t sa_family;
char sa_data[14];

};

typedef uint16_t in_port_t;
typedef uint32_t in_addr_t;

struct in_addr
{
in_addr_t s_addr;

};

struct sockaddr_in
{
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;

unsigned char sin_zero[sizeof (struct sockaddr) -
sizeof (unsigned short int) -
sizeof (in_port_t) -
sizeof (struct in_addr)];

};

void
error (char *msg)
{
perror (msg);
exit (1);

}

int
main (int argc, char *argv[])
{
long portno;
char *end_of_argv = NULL;
struct sockaddr_in serv_addr;

if (argc < 2)
{
fprintf (stderr, "ERROR, no port provided\n");
exit (1);
}
portno = strtol (argv[1], &end_of_argv, 10);
printf ("Hello World!\n");

return 0;

}

Aside from the assumption that the size of sin_zero is positive, and that
uint16_t and uint32_t are available, I don't see anything non-portable in
this program. Does this program show the same behaviour on your system? If
it does, I believe it is a bug in your compiler or debugger, and I
recommend you check if an updated version is available.
thanks again for your help. Unfortunately, it does show the same
behavior. There seems to be a more recent version of gcc (mine is
4.1.2 and 4.2.1 is now available). I could try to update the gcc ...
and see what happens. My gdb is up to date since latest version is
6.6.

tnx.

Jul 30 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

36 posts views Thread by Dmitriy Iassenev | last post: by
13 posts views Thread by Generic Usenet Account | last post: by
3 posts views Thread by Mr. X | last post: by
8 posts views Thread by fabio | last post: by
7 posts views Thread by =?Utf-8?B?QnJpYW4gS3Vueg==?= | last post: by
20 posts views Thread by Pilcrow | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.