in reply to Re: How can I print variable contents from the debugger non-interactively?
in thread How can I print variable contents from the debugger non-interactively?

Thank you very much LanX for the reply.

Using PERL5DB="sub DB::DB { print $var }" perl -d your-script is a really great idea. I was not aware of the DB::DB sub.

I tried defining this and running it with something like:
PERL5DB="sub DB::DB { print \"VAR: \$var\n\" if(defined \$var); }" perl -d your-script

However, it seems that this affects the way the std debugger works as I do not get the output from the debugger listing all the lines that are executed as before. It seems to not be called recursively all the way through the program without the std. debugger working. Looks like it only gets called top level and internal statements are not getting the DB::DB injected before execution.

I saw your responses giving commands to the debugger ie. watch and action statements etc. Is there a way to provide this to the debugger via the $ENV{PERLDB_OPTS} variable?

Thanks again.
--dave
  • Comment on Re^2: How can I print variable contents from the debugger non-interactively?

Replies are listed 'Best First'.
Re^3: How can I print variable contents from the debugger non-interactively? (@DB::typeahead)
by LanX (Saint) on Jul 17, 2017 at 16:11 UTC
    > , it seems that this affects the way the std debugger works

    yes, it's still unclear for me if you want to step thru or run an automatic trace.

    > Is there a way to provide this to the debugger via the $ENV{PERLDB_OPTS} variable?

    Yes.

    edit

    At least in .perldb with afterinit

    sub afterinit { push @DB::typeahead, "b 4", "b 6"; }

    See perldebug#Debugger-Customization

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      you can also send debugger commands from inside the debugged Perl program ...

      (though I don't recommend hard-coding linenumbers there, use $DB::single=1 to set a breakpoint in place)

      use strict; use warnings; push @DB::typeahead, 'b 19 $y==3', '{ X y' if exists &DB::DB; $|=1; my $a; our $y; for (0..10) { print; $a++; $y++; }
      runs as
      DB<22> c 0123auto(-3) DB<22> X y $y = 3 auto(-2) DB<23> b 19 $y==3 auto(-1) DB<24> { X y DB<25> c 45678910auto(-1) DB<25> X y DB<26>

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

      > yes, it's still unclear for me if you want to step thru or run an automatic trace.

      I think stepping thru the code would imply an interactive session. I am looking for an automatic trace, it would be nice to indicate which lines/variables should be printed, but having something that prints all of the variables modified and statements run would suffice. (huge output, but that isn't a problem!) There are some autotrace modules that may work, but I would like to accomplish this without additional modules. (lets make believe we are air gapped and getting to cpan is a problem)

      > At least in .perldb with afterinit
      sub afterinit { push @DB::typeahead, "b 4", "b 6"; }

      So this isn't exactly thru the $ENV{PERLDB_OPTS} environment variable but using a file .perldb is easy enough to do. (though, @DB::typeahead is not a supported interface and is subject to change in future releases.)

      I attempted the following:
      echo "sub afterinit { push @DB::typeahead, \"a 119 p \$var\"; }" > .perldb

      I see:

      29: main(); auto(-1) DB<1> a 199 p __DB<2> (some program output) and then it shows 6556 segmentation fault with exit code 139.

      not sure what is causing the seg fault and I again lose the automatic printing of all the statements, but I see some of the program output. Hopefully the above typeahead gives you an idea of what I'm trying to accomplish.


      Thanks again for all the thought and replies.

        There is already a whole trace infrastructure apart from the debugger.

        Please note the debugger IS eventually for interaction.

        And I've shown you already 3 approaches, with varying details and footprint

        • print when you break back to prompt {
        • print whenever you pass a certain point with a or b
        • print whenever you execute any statement with w

        > push @DB::typeahead, \"a 119 p \$var\";

        Please note again that a , b and w only take real Perl code

        but p is a debugger command

        Commands accepting debugger commands are documented with db_cmd or db_command

        DB<27> h { { db_command Define debugger command to run before each prompt. { ? List debugger commands to run before each prompt. {{ db_command Add to the list of debugger commands to run before ea +ch prompt. { * Delete the list of debugger commands to run before eac +h prompt.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!