pijush has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I am debugging a perl script on Linux OS which has a call to C-function from a .so file. I have tried to debug this script with perl -d option and also gdb perl . But when it enters in the C function from perl code then I failed to debug the code. It only shows an address. Is there any method so that I can start debug from perl script and debug the whole script, i.e., to debug "C" code also?
Any help really appreciated.
Thanks in advance.
Regards
-Pijush

Replies are listed 'Best First'.
Re: How can I debug perl-xs code?
by hawtin (Prior) on Feb 04, 2004 at 22:17 UTC

    I would echo the comments you already have about the debugger flag. Another thing I have found with XS is that I have had trouble getting perl to run in the debugger (admittedly this is mostly when I am messing with interrupts and with modules linked to dynamic libraries). A trick that I have found usefull is to temporarily insert an infinite loop at the start of my C code:

    int temp_val; temp_val = 100; /* An infinite loop that usually won't be optimised away */ while(temp_val > 0) { temp_val--; if(temp_val < 50) temp_val = 100; }

    Then start up the perl script, it will go into the infinite loop. Use

    ps aux

    to get its process number. Start gdb with

    gdb `which perl` <Process Number>

    And get out of the infinite loop with

    set temp_val=-1000

    Now you have a gdb session with all the correct libraries loaded stopped at the start of your routine.

      Just some improvement suggestions to an otherwise excellent method:

      1. The use of volatile decalaration for the loop guard will make the loop much more terse.
      2. Sticking in an odd call to sleep(1); will make your instrumented system much easier on your the rest of your OS.
      Cheers, ---Lars
      Cool tip. We do a lot of java-stuff at work (sadly, little Perl) and we have never been able to debug native code and usually resort to fprintf, or writing a command line tool to use the libraries as opposed to Java over JNI. This might help! Just thinking outloud here, but what happens with the Inline modules? I've never used them (I need to!), but I am guessing using Inline ::C to call into a module would pretty much keep debugging the same way? Does Inline::C complicate the Perl debugger?
      Thanks hawtin.
      Thanks all the monks who have given valuable pointers. Now what I have done, I first put an infinite loop in .c file and build it with -g and without -O option. Started perl script and now I can debug with gdb after attaching PID.
      Thanks again.
      -Pijush
Re: How can I debug perl-xs code?
by borisz (Canon) on Feb 04, 2004 at 19:30 UTC
    Have you tried to compile your .so with -g and without -fomit-frame-pointer and -O*? Then retry with gdb.
    Boris
Re: How can I debug perl-xs code?
by ysth (Canon) on Feb 04, 2004 at 19:36 UTC
    If the C function isn't built with symbolic debuging information (i.e. gcc's -g flag) you're not going to have much luck. But if you are trying to find a problem in your perl or XS code, you shouldn't need to debug into the C-function itself??