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

Hi Monks!

I am facing a typical problem on the Linux box. I have a perl script which contacts the "C" code through Perl-XS. In Solaris box the script is running fine, but in Linux box the same script generates segmentation fault. I debug the code and found that in "C" code it gets an address which is out of scope and that's why it is generated segmentation fault. To crack the problem I am thinking to track which address is passed from perl code to "C" code. Can anyone please help me how can I track which address Perl code is passing to "C" code? Or, how can I track the problem?
Any help really appreciated.
Thanks in advance.
-Pijush

  • Comment on Can I track which address Perl code is using?

Replies are listed 'Best First'.
Re: Can I track which address Perl code is using?
by mugwumpjism (Hermit) on Feb 16, 2004 at 21:14 UTC

    Devel::Peek is probably what you're after. This will (from Perl) dump the addresses and flags of a module. Also see the refaddr method in Scalar::Util.

    You could try wrapping the XS function in a Pure Perl method that uses the above to dump info on its arguments.

    Here's an example of calling Devel::Peek::Dump from XS, which can be darned handy too:

    ENTER; SAVETMPS; PUSHMARK(sp); XPUSHs(sv); PUTBACK; perl_call_pv("Devel::Peek::Dump", G_SCALAR|G_DISCARD); FREETMPS; LEAVE;

    You'll also need dSP; at the beginning of your function to declare the sp variable; in the above, sv is the dumped variable.

    $h=$ENV{HOME};my@q=split/\n\n/,`cat $h/.quotes`;$s="$h/." ."signature";$t=`cat $s`;print$t,"\n",$q[rand($#q)],"\n";
Re: Can I track which address Perl code is using?
by samtregar (Abbot) on Feb 17, 2004 at 18:05 UTC
    You can use the Perl debugger, which should be able to give you a stack trace when your program segfaults. You can also watch variables and set breakpoints just like a standard debugger. See perldebug for details.

    Of course, if the Perl debugger isn't up to the task, there's always gdb. Since Perl is a C program it can be compiled with debugging symbols and run through gdb.

    -sam