vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Hi, I can not seem to figure out why I can't debug some programs on OpenBSD/macppc. It seems to me that the -ggdb3 switch given to gcc is broken on macppc. I've created a sample program that should show the problem. Please compile it with the -ggdb3 flag and then you can see the problem if you put a breakpoint in read_struct with gdb. What happens is that the variables in the argument list are incorrect. Also, if you take away the extra buf[512] the problem goes away. In my opinion the stack is messed up. The unused variable buf is not required for this program but it's there to illustrate the problem since it is required for some larger more complicated programs. I can get around the problem if I use the -g switch instead of -ggdb3. The -ggdb3 flag works fine on i386 (the only other machine I have to test on). I would be curious to see if this switch also works on other architectures. Is this a known problem? #include <stdio.h> #include <string.h> /* * Compile this program with: gcc -ggdb3 -o a a.c * * Then: $ gdb ./a * * and break at read_struct, look at the arguments. * * The problem can be fixed by * 1. Getting rid of buf and using a dynamic array * instead of a static one * or * 2. Compiling with the -g switch instead of -ggdb3. */ union val_type { int i; char *s; }; struct test_struct { char *name; int type; union val_type val; }; int read_struct(char *, struct test_struct *, int); int main(void) { int num_elements; struct test_struct ts[] = { { "an_integer", 0, { .i = 0xBEEF }, }, { "a_null_string", 1, { .s = NULL }, }, { "a_string", 1, { .s = "hello" }, } }; num_elements = sizeof(ts) / sizeof(ts[0]); /* Should print 1 0 1 */ printf("%d - an_integer present.\n", read_struct("an_integer", ts, num_elements)); printf("%d - number_10 present.\n", read_struct("number_10", ts, num_elements)); printf("%d - a_string present.\n", read_struct("a_string", ts, num_elements)); return 0; } int read_struct(char *key, struct test_struct *ts, int num_elements) { int i; char buf[512]; /* this buffer messes up the stack in gdb */ for (i = 0; i < num_elements; i++) { if (strncmp(key, ts[i].name, strlen(key)) == 0) return 1; } return 0; } |