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

Hi fellow monks,

I'm using Inline::pdlpp to write some C routines, embedded in my Perl code.

My problem: I'm getting a segfault that I can't track down. I've looked through my code with particular attention to what the pointers are doing, and it looks ok. But of course it might not be. Pdlpp generates quite a bit of C that wraps round my functions, the problem could be there, too.

My question: how best to track down this segfault?

It's been a few years (like, about 15 years) since I did any C programming, but I've been doing some revision and it seems like the standard technique is to load the core file into gdb (the debugger) and go from there, or to run it under something like Valgrind which will tell me where I'm going wrong.

But: I don't know how to do those things, given that the code is itself embedded in a Perl script. Right now I'm not even getting a core file that I could load into gdb.

So, any suggestions greatly appreciated. As you can probably tell I'm fairly new to this area so apologies if I'm missing something obvious...

I'm using Mac OS X on Intel BTW, if that makes a difference to choice of tools.

Best wishes, andye

Update: I've read this node: Debugging Inline C programs but I'm still some way from being enlightened... I need to recompile perl itself with '-g'? Sounds like a pain... Currently I'm passing Config => OPTIMIZE => '-g' to Inline::Pldpp, but I'm not sure what my next step is meant to be...

  • Comment on Debugging segfault in inline C (actually inline pdlpp, but probably the same principle)
  • Download Code

Replies are listed 'Best First'.
Re: Debugging segfault in inline C (actually inline pdlpp, but probably the same principle)
by syphilis (Archbishop) on Apr 20, 2007 at 14:51 UTC
    Hi andye,
    FWIW (which may not be a lot), I usually stick in some debug printf("got to here\n") calls, so that I can identify the precise function call that's causing the segfault. Once the problem code has been identified, I often find I can figure out what's going awry.

    Cheers,
    Rob
      Hi Rob,

      Yeah, that's what I've been doing for much of today. :) I have at least found what's going wrong - I'm overrunning an array - but I'd really like to find out if there was an easier way...

      Best wishes, andye