Linux - Debugging Cpp Plug-ins with GDB
Debugging your plug-in with GDB
On Linux you can use the GNU Project Debugger (http://www.gnu.org/software/gdb/documentation/) (GDB) to set breakpoints and examine your code execution.
Compile your plug-in with the -g flag
The -g flag generates debugging information used by GDB. Just replace -c with -g in your makefile.
For example, if you want to try with one of the SDK examples, open the file $XSISDK_ROOT/mkfiles/rules.gnu and change this line:
$(CXX) -c $(CXXFLAGS) -o $@ $<
$(CXX) -g $(CXXFLAGS) -o $@ $<
Start XSI and load your plug-in. Make sure the plug-in code is executed.
XSI uses a lazy loading process where your library's symbols are not loaded until it is executed. You must cause the library to be fully loaded before you can use GDB. For example, if you want to debug a custom command, run the command; to debug a custom display host, invoke the GUI.
Open a new shell and start GDB by giving the process id of XSI. For example, if XSI has a pid (ps -a) of 4444, then start GDB like this:
gdb -p 4444
Look for your code. After GDB loads the debug symbols, you can use the command:
to view which source files are available for debugging. You should now see your code in this list.
Set breakpoints. For example:
To resume execution after breaking, use the continue command.
- This is a quick overview of how to use GDB to debug plug-ins from with XSI. For a complete overview of GDB and the available debugging commands, see the GDB documentation (http://sources.redhat.com/gdb/current/onlinedocs/gdb_toc.html).
- For more information on gcc options such as -g, see one of the many tutorials (http://www.google.ca/search?hl=en&q=gcc+tutorial&meta=) available on the Web.
- You must load and run your plug-in before you start GDB.
- GDB must be attached to the process after your plug-in has been executed once.
- This obviously isn't so great for debugging crashes in your execute method, but with a little code refactoring, (for example a " if firsttime do nothing" check in your execute method) you should be able to attach GDB and then recreate the crash.
- Hint: If you find GDB hard to work with from the command line, try using the emacs interface (http://sources.redhat.com/gdb/current/onlinedocs/gdb_24.html#SEC238) [Ed. Haven't done it myself yet, but I don't see why it wouldn't work]