简体   繁体   中英

Debugging a callback with gdb

I'm trying to use gdb to debug a C program which all works ok until I pass a pointer to a function (to a third party library), after which gdb loses focus and the program runs without hitting my breakpoint in my call back function.

For example I am calling pcap_loop from the libpcap library which expects a pointer to my call back function got_packet .

 pcap_loop(handle, num_packets, got_packet, NULL);

As soon as i step in or over this line with gdb my break point in got_packet is never hit.
Why ?
Any ideas?

There are some circumstances in which GDB breakpoints can get skipped, especially when debugging code compiled with optimization enabled, but the most likely answer is that the library function isn't doing what you expect.

GDB can't step into libraries that don't have debug information, so instead it just appears to "lose focus", as you put it, sets a temporary breakpoint on the return point and waits for the function to finish. That still shouldn't stop the breakpoint inside the call-back from triggering.

If you are using a library that came from your OS repository then you might find that there is a "debug" package you can install. This would allow you to step into the library code and possibly work out what the problem is that way.

However, it's usually far simpler to add printf debugging, or "break" the got_packet function in some way that will cause a signal ( *(int *)0 = 1 ), and satisfy yourself that way whether it is being called or not.

If you can show that the function really is being called, and that GDB really isn't catching the breakpoint, then you have a GDB bug (or maybe a kernel bug).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM