http://www.perlmonks.org?node_id=988181

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

O great Perl Monks —

I beg forgiveness yet again. I make mistakes which is why debuggers are important to me.

What I cite below always gives the first COP on a line. This is not necessarily the place you are stopped at. And this was the whole point of this.

Again for reference....

I beg forgiveness for my feeble mind. I now realize I somewhat had the answer before me all along, but had somewhat lost the courage of conviction.

The answer I seek is in the value of the numeric value of @DB::dbline array. I don't recall the meditation yielding this, let alone if this is part of a public API or an implementation artifact that should not be relied upon. For this, I seek further guidance.

The below is for reference:

Persuing one of the avenues described in Identifying a location with finer granularity than a line number, I ponder how to get the B:OP Code address (analogous to a Program Counter in Assembly Code) in a running Perl program?

Thanks

Replies are listed 'Best First'.
Re: How can I get the current OP address (akin to PC)?
by Khen1950fx (Canon) on Aug 18, 2012 at 18:30 UTC
    Maybe something like this:
    #!/usr/bin/perl -l BEGIN { $| = 1; } use autodie; use common::sense; use Opcode; use Opcodes qw/ppaddr opdesc/; my $ppaddr = &Opcodes::ppaddr(0); print $ppaddr; my $opdesc = &Opcodes::opdesc(0); print $opdesc;

      Thanks for the suggestion. I tried and it didn't work for me.

      Here is something that sort of works but doesn't really.

      In file /tmp/Devel/COP.pm:

      package DB; sub DB { ($pkg, $filename, $lineno) = caller; $COP = 0; local(*DB::dbline) = "::_<$DB::filename"; if (defined $DB::dbline[$lineno]) { $COP = 0 + $DB::dbline[$lineno] ; } }; 1;
      And to test it, put in file /tmp/code-test.pl:
      #!/usr/bin/perl use B::Concise; sub testit () { my $walker = B::Concise::compile('-basic', '-src', 'testit'); B::Concise::set_style_standard('debug'); B::Concise::walk_output(\my $buf); $walker->(); # walks and renders into $buf; print $buf, "\n"; printf "COP is 0%x\n", $DB::COP; } testit();
      Now run as perl -I/tmp d:COP /tmp/code-test.pl

      The problem is that the COP address is always the first statement for the line. And what I really want is the one we are currently at which might not be the first COP of the line. Sigh.
Re: How can I get the current OP address (akin to PC)?
by flexvault (Monsignor) on Aug 18, 2012 at 17:49 UTC

    rockyb,

    I've never used it, but '__LINE__' represents the current line number. Maybe you could use super search to find out more. I know I have seen references to its use on PM. I had hoped someone who had used it would pipe-in, but it is the weekend!

    Good Luck

    "Well done is better than well said." - Benjamin Franklin

Re: How can I get the current OP address (akin to PC)?
by CountZero (Bishop) on Aug 18, 2012 at 19:40 UTC
    What use would it be to know the B::OP Code address?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
        I see. As I try to have only one instruction per line, I could not think of a good use for it.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: How can I get the current OP address (akin to PC)?
by rockyb (Scribe) on Sep 11, 2012 at 01:06 UTC

    The answer I am seeking is Devel::Callsite, and I am grateful to Father Chrysostomos for enlightening me.

    As pointed out by Eric Brine on p5p, this module currently fails on non-threaded Perl builds, but with luck we can get that fixed.

    Starting with commit db2e1bc I am using this in Devel::Trepan.

    Again, thanks to all that have helped me in this journey.

      Recently, with the good work of Ben Morrow, Devel::Callsite now has an optional level parameter similar to perl's builtin caller. With this you can get the OP address up the call stack.

      In the next release of Devel::Trepan, OP addresses will be shown in locations, by default if Devel::Callsite is installed. Currently they are shown only after the debugger command set display op on is run.

      But this got me thinking about this and identifiying the exact position. Given the tree structure nature the Perl program, it should be possible to narrow positions in a line further. For example, one can use words to describe a position within a line. If for example, that an op address is in the second statement of line 10, one might say just that. Or perhaps something like "second function call on line 10".

      Going further, one might take B::Concise output and decompile some fragment with a given op address

      I realize this probably has limited appeal, but still I think it cool. Any takers on an package that can take B::Concise output and an perl OP addresss, and narrow within a line where that OP is?