Reputation: 1990
I am having trouble with a bug caused by overwriting a pointer with an invalid value. I have not been able to find the bug using valgrind (in it's default mode) or with GDB because they only point me to the invalid pointer, and NOT what overwrote that pointer to the incorrect value.
It's always the same variable, however, I do not explicitly set it to the bad value. Some other line in the program must be accessing memory out of it's bounds but by chance it happens to hit the storage for this pointer instead.
I am unsure what debugging tools/options I should use to approach this bug.
Example crash:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff6ffc700 (LWP 2425)]
0x00000000004058b2 in writeToConn (conn=0x7ffff0004f40) at streamHandling.c:115
115 ssize_t result = send(conn->fd, conn->head->data->string + position, conn->head->data->size - position, 0);
(gdb) print conn
$1 = (struct connection *) 0x7ffff0004f40
(gdb) print conn->head->data
$2 = (struct dbstring *) 0x35
Unfortunately I can't simply watch the variable conn->head->data
because I have about 5,000 conn structs.
This code works most of the time, however if run under a moderately heavy load it will crash after a few seconds.
Upvotes: 1
Views: 1069
Reputation: 239011
You can have gdb
automatically execute commands when a breakpoint is triggered, with Break Commands.
You could set up a Break Command to run whenever a struct connection
is allocated, and have it add a watchpoint on the field of interest.
Upvotes: 2
Reputation: 3154
Would a stack backtrace help? Here is a page that tells how to do it.
How can one grab a stack trace in C?
Upvotes: 0