Saturday, June 1, 2019

how to put break points for static functions

 
 
functions declared "static" in the code don't show up in GDB
backtraces when the code was compiled at -O2 and above, nor can you
set breakpoints on them.  You can however set a breakpoint in the
static function if you know the file name + line number.

Sample code:
#include <stdio.h>
void f3 (const char *a)
{
 fprintf (stderr, "f3: '%s'\n", a);
}
static void f2 (const char *a)
{
 f3 (a);
}
int main (int argc, char **argv)
{
 f2 ("bork");
}

So, "break f2" won't work, but "break foo.c:8" will work to break in
f2().  This greatly hampers the ability to debug programs that use
static functions since you cannot see or switch to a frame that's static.
 
 
 
or 
 
1
Assuming the routine really exists as you'd expect (i.e., not optimized away or inlined) - I've used a couple approaches in situations like this:
Assembly breakpoint
  • let's say you want to set a breakpoint at static function foo(). Find code that calls foo() - let's say bar() calls foo(). Where bar() calls foo(), set a breakpoint.
  • run until you hit the breakpoint where bar() calls foo(). step at the assembly level. This should put you at the first instruction of foo(). Note that you might have to step through a few instructions if there are parameters being passed - hopefully you know what a branch / subroutine call looks like in your architecture.
  • Set an assembly breakpoint when you land at the first instruction of foo().
Function pointer
I've also worked around this by initializing a function pointer with foo()'s address. When system is running, read the function pointer in the debugger. Get foo()'s address. Set breakpoint based on this address.
Note that in these cases you might not have interleaved source, though.
 
 
 
 
 
Reference taken from:
 
https://bugzilla.redhat.com/show_bug.cgi?id=136536
https://stackoverflow.com/questions/8759737/how-do-i-set-a-vxworks-breakpoint-on-a-c-static-function
 
 
 
 
 

No comments:

Post a Comment