Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: debugging perl module written in C

by rg0now (Chaplain)
on Nov 16, 2008 at 21:24 UTC ( #723950=note: print w/ replies, xml ) Need Help??


in reply to debugging perl module written in C

Here is how I usually debug XSS code (not with Komodo, though):

  1. Recompile the C code with CFLAGS=-g -O0 to have debugging symbols in the resultant library that will be loaded from Perl. -O0 will remove optmization, this makes it easier to step through the code.
  2. Edit the Perl code and put a dump() somewhere before the offending XSS function is called. This will make your Perl exit immediately when calling this function and dump a core. Note that you must have core files enabled (on UNIX, you achieve this by issuing ulimit -c unlimited).
  3. Fire up a debugger (I recommend the venerable gdb) and read in the core
    r@everything:/tmp$ gdb (gdb) core core ... (gdb) file /usr/bin/perl Reading symbols from /usr/bin/perl...(no debugging symbols found)...do +ne. (gdb) set args a.pl (gdb) r Starting program: /usr/bin/perl a.pl ...
    Now, you have all the symbols read in by gdb, so you can start placing breakpoints whereever you want.
  4. After placing the breakpoints, remove the dump() from your Perl code, rerun and wow, your debugger will stop at your breakpoint.
Two final notes are in order here. First, I haven't tested this on Windows, but something along these lines should work there too. Second, the above method will allow you to step through only the XSS code but not the Perl code. I don't quite know any methods to simultaneously debug C and Perl code at the same time.


Comment on Re: debugging perl module written in C
Select or Download Code
Re^2: debugging perl module written in C
by chuckd (Scribe) on Nov 16, 2008 at 22:04 UTC
    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?

      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?

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://723950]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-12-25 21:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls