I'm debugging a big multi-threaded application and trying to catch a bug when an invalid pointer value is being assigned to a variable. I cannot simply backtrace from the point of failure because consumer(s) and producer(s) are different threads, so I'm trying to set up a conditional breakpoint on every assignment statement of that variable to test if assigned value is invalid.
Let's consider this example:
#include <stdio.h>
int main() {
char* arr[] = {"foo", "bar", 0x1234, "baz"};
for(int i=0; i<4; ++i) {
char* str = arr[i];
puts(str);
}
}
I found that I can handle it like this:
break test.c:7 if !arr[i][0]
It works, however, I'm unsure if this approach is anyhow reliable because it actually tries to access (probably) invalid memory addr. Also, it has an obvious false-positive when string is empty.
Also, I came across this answer, but it looks like I'd have to somehow tell linker to add those functions into my executable to be available for gdb, and also it looks too complicated to use inside a one-liner condition.
So, my question is: is there any reliable way to check if pointer is invalid inside gdb, which is short enough to use inside a one-liner condition and does not depend on pointed value.