While working on Devel::Profiler I've stumbled into a dark pit. Basically, Devel::Profiler works by instrumenting every sub it can find by walking symbol tables. When it finds a sub it wraps it in a profiling routine and installs it in the sub's slot in the symbol table. This is generally working fine. The problem occurs when that subroutine isn't a real subroutine, but a fake produced by exporting a symbol that doesn't exist. This exported sub will then call AUTOLOAD when it's called. Fcntl, for example, uses this pattern.

Here's a bit of code that shows a simplified version of what I'm doing. CAUTION: this code loops infinitely printing "LOCK_EX called" - it might lock up your machine if you're not quick with Ctrl-C.

#!/usr/bin/perl -w use Fcntl qw(LOCK_EX); # instrument LOCK_EX my $lock_ex_coderef = \&Fcntl::LOCK_EX; *Fcntl::LOCK_EX = sub { print "LOCK_EX called\n"; return &$lock_ex_coderef; }; # call LOCK_EX $_ = LOCK_EX;

Now, what I'd expected was that this code would print "LOCK_EX called" just once and then call Fcntl::AUTOLOAD as usual. Can anyone think of a way to get that behavior? Or perhaps I just need to understand why this method isn't working. Can anyone explain why a calling code-ref to a "fake" sub causes this recursion?

Alternately, can anyone propose a way to detect "fake" subroutines so I could skip them for now? B:: solutions are acceptable.

Thanks!
-sam


In reply to How to instrument a "fake" subroutine? by samtregar

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.