in reply to Re: debugging perl module written in C
in thread debugging perl module written in C

Hi, When you say "C code with CFLAGS=-g -O0 to have debugging symbols in the resultant library that will be loaded from Perl. -O0 will remove optimization, this makes it easier to step through the code." What do you mean? Do I have to pull the c code out of the module to do this? Is there any way you can provide more specific steps?
  • Comment on Re^2: debugging perl module written in C

Replies are listed 'Best First'.
Re^3: debugging perl module written in C
by Perlbotics (Archbishop) on Nov 19, 2008 at 12:47 UTC

    Hi, -g adds information to the compiled objects/libraries in order to allow a debugger (gdb, ddd, Emacs gdb-mode, etc.) to align the op-codes currently executed with the source code lines to be displayed, to find symbols (subroutines, variables), their values, and the like. You might need to tell your debugger where to find the sources explicitly (gdb: -d switch).
    It is important to switch off optimisation (-O0) because the compiler might re-arrange instructions in order to optimise for speed and/or memory consumption. So, as a result of optimisation the debuggers task to align source code lines (C/C++) with what is executed might fail resulting in confusing output.
    Thus, you need to re-configure your module for debugging or manually edit CFLAGS in the current Makefile. This implies that you have to download the sources and to re-compile a new version of the module in question for debugging purpose. Then follow the steps rg0now has described.
    Personally, I would first (ok, after reading the modules documentation, esp. the TO DO and BUGS section) run the tests (see t/ directory) and scrutinise every warning or error related to your specific problem, possibly replacing input data for a test-case by data that caused your problem. That might be more effective than single-stepping through an external module... and it doesn't require you to compile a special debug-version of the module.

    I've the impression, you're looking for a single tool/debugger that allows you to step through a Perl program (source) and through the C sources of an XSS module simultaneously? AFIK, there is no such tool since there are two stand-alone debuggers involved. So you have to decide for a given run: do I want to use the IDE to debug the Perl part of my program or do I want to use gdb/ddd/... to debug the XSS part of the module(s) my Perl program loads and executes?

Re^3: debugging perl module written in C
by rg0now (Chaplain) on Nov 19, 2008 at 13:36 UTC
    I am not sure I understand what you ask. When I say "recompile with -g -O0, I mean that you must set the compiler flags so that the compiler emits code with debugging symbols included and optimization turned off.

    To clarify a bit: you call C code from Perl in the following way. Since C code can not be directly called from Perl, first you must compile the C code into a shared library, which then will be dynamically linked to your Perl executable when running your script. This makes it possible to switch the execution thread from Perl to C and vice versa. This means that you first must compile the C code to a shared library, only after this you can call into it from Perl. If you turn optimization on, certain code segments, lines or functions will be optimized away from the resultant library, and this makes stepping through the code by the debugger clumsy.

    So here is what you need to do: find the flags passed to the compiler when building the code (on UNIX, this is the Makefile, on Windows, I'm not sure, there must be some project file somewhere), change the compiler flags to "-g -O0", rebuild the code from scratch and use the debugger.

    If you clarify your problem and environment a bit more deeply, maybe I could be of some more help.