Here's code from KinoSearch::Util::Carp, which calls Carp::confess from C.

void Kino_confess (char* pat, ...) { va_list args; SV *error_sv; dSP; error_sv = newSV(0); va_start(args, pat); sv_vsetpvf(error_sv, pat, &args); va_end(args); ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs( sv_2mortal(error_sv) ); PUTBACK; call_pv("Carp::confess", G_DISCARD); FREETMPS; LEAVE; }

It assumes that somewhere, someone has invoked use Carp;.

If you know that either ISO or GNU variadic macros are available, you can transparently insert the filename and the line number of the C file from where the exception was thrown, plus the function if either __func__ (C99) or __FUNCTION__ (GNU) is defined. Take a look at KinoSearch::Util::Carp in my svn repository if you want to see how that can work.

The bonus contextual info is just plain ol' nasty C wrangling though -- the XS-specific stuff is the same. Check out perlcall for more info on how to call back to Perl from C.

--
Marvin Humphrey
Rectangular Research ― http://www.rectangular.com

In reply to Re: Output package and function name as well as linenum in a die in XS by creamygoodness
in thread Output package and function name as well as linenum in a die in XS by tbusch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.