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

Monks,

I've been looking all over the man pages for some information on debugging C code from within Inline. I've been using if (DEBUG) lines all over tracking down my problem code... is there an easier way to do this? Can I (cross fingers) somehow use a debugger? If not, what have you guys done to make this task less painful?

thanks

Replies are listed 'Best First'.
Re: Debugging Inline C programs
by edan (Curate) on Feb 24, 2005 at 10:32 UTC
Re: Debugging Inline C programs
by zentara (Cardinal) on Feb 24, 2005 at 12:52 UTC
Re: Debugging Inline C programs
by dragonchild (Archbishop) on Feb 24, 2005 at 13:57 UTC
    This may be a stupid answer, but isn't debugging C code the same all over the universe, whether or not it was from within Inline? I know my toy stuff didn't behave any differently ...

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Debugging Inline C programs
by talexb (Chancellor) on Feb 24, 2005 at 19:41 UTC

    Having written a fair bit of C, I would guess that debugging C would be easier while working outside of Perl and Inline::C. I would even have a test stub for the C code and set it up with its own tests.

    Trying to do the whole thing through Inline:C sounds like way too much work. Laziness, dude.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: Debugging Inline C programs
by blazar (Canon) on Feb 24, 2005 at 09:47 UTC
    I've been looking all over the man pages for some information on debugging C code from within Inline. I've been using if (DEBUG) lines all over tracking down my problem code... is there an easier way to do this? Can I (cross fingers) somehow use a debugger? If not, what have you guys done to make this task less painful?
    Just a wild guess, and I really hope that someone more informed and more experienced than I am will give you more effective help: Inline C is known to "only" provide a convenient interface to XS programming. So does any document about XS give shed more light on the issue?
Re: Debugging Inline C programs
by sfink (Deacon) on Feb 25, 2005 at 07:25 UTC
    See the previously posted link, which contains very good information. I have a few other things that I use.

    The XS routines that will call your inline function will be named _XS_package_function, where package is your full Perl package name with double-colons (::) replace with double underscores (__). (At least, that's the symbol name I get here. Use nm on the .so to find out for sure.) If you can get into the debugger after your library has been bootstrapped, you can set a breakpoints by name.

    The current value of $@ is often of interest. This isn't guaranteed to work, and depends somewhat on your compile of perl, but I use the following expression to fetch the value: p ((XPV*) my_perl->Ierrgv->sv_any->xgv_gp->gp_sv->sv_any)->xpv_pv

    Isn't that beautiful?

    Similarly but much simpler, if you want to see the string value of an SV* that you know has a string in it, use p *(XPV*)var->sv_any. (This may not work if there is anything special about the variable.)

    To generate a warning (sometimes useful just to see what line of code you're on, for example if your debugger caught a segmentation fault): p Perl_warner(my_perl, 1, "oops")

    This one is helpful for much more than Inline. Use the following code:

    // Define a BREAKPOINT macro that will send the current process a // SIGTRAP, causing a drop into the debugger if you're running under // one. If this is not an i386, then this macro will drop you into the // debugger deep inside a libc function and you'll have to single-step // out (si). #ifdef __i386__ # define BREAKPOINT do { \ int pid = getpid(); \ int signum = SIGTRAP; \ int trapnum = SYS_kill; \ asm("int $0x80" : : "b"(pid), "c"(signum), "a"(trapnum)); +\ } while(0) #else # define BREAKPOINT kill(getpid(), SIGTRAP) #endif
    to programmatically create breakpoints that will drop you into the debugger. (That was a fun one to create -- I hadn't done any assembly programming in a few years, and I'd never done inline asm in gcc. Nor used SIGTRAP. I wrote it to be cross-platform, but I've never actually tried it anywhere else.)
Re: Debugging Inline C programs
by whiteEFunk (Acolyte) on Feb 25, 2005 at 22:00 UTC
    thanks to everyone! I should also note here that Rob and Ken Williams from the Inline mailing list suggested this:

    <Rob:>To use gdb, I gather that perl, itself, needs to have been compiled with '-g'. There's a helpful discussion of debugging XS in "Extending and Embedding Perl" by Jenness and Cozens (published by Manning).

    Also 'sv_dump()' and the Devel::Peek module's 'Dump()' function allow you to see what's inside your perl structures - which can be a useful debugging aid (depending on the nature of the problem).

    <Ken:>Yes, that's true, I've done it. It then becomes a standard exercise in debugging a C program using gdb - the fact that it's reading perl code and executing it is immaterial (and largely inaccessible) from the perspective of gdb.