I don't know what code is the cause of the problem.

Historically, I've been reasonably adept at using debuggers, but despite many attempts with several different debuggers, I find tracing into Perl with a debugger pointless. The nature of the run-loop dispatching means that stack traces are nearly always useless, and setting break & watch points an exercise in futility.

The most effective tool I have found for tracking down segfaults in XS code is printf. I most frequently use the following macro to track down the source of problems:

#define TAG printf( "# %s(%d)\n", __FILE__, __LINE__ )

I start by adding TAG;s at the top and bottom of each routine. The last enter tells me which routine to concentrate on. Then I tag strategic points in the routine:

SV *mapAt( SV *fileMap, U32 hiOff, U32 loOff, size_t size ) { SV *map = newSVpvn( "", 0 ); void *mapAddr; size_t n; TAG; printf( "received size: %I64d\n", size ); ERR( mapAddr = MapViewOfFile( (HANDLE)SvUV( fileMap ), FILE_MAP_ALL_ACCESS, hiOff, loOff, size )); TAG; if( ! size ) { // Mapping entire file MEMORY_BASIC_INFORMATION mi; ERR( VirtualQuery( mapAddr, &mi, sizeof( MEMORY_BASIC_INFORMAT +ION ) ) ); size = mi.RegionSize; printf( "Actual size mapped: %I64u\n", size ); } TAG; SvPV_set( map, mapAddr ); SvCUR_set( map, size ); SvLEN_set( map, size+1 ); TAG; return map; }

Ideally, the macro would use fprintf( stderr,...), but using stderr within XS code under Windows blows up in horrible ways. (Due to PerlIOs layers of redefinitions of CRT entities in incompatible ways.)

It is simplistic, but it works. Once you know where your code is failing, working out why is usually not too difficult.

I also have editor macros to insert and remove the tags on the current line, but that just a nice to have.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^3: Perl stack issue by BrowserUk
in thread Perl stack issue by jalopeura

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.