Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

debugger watchpoint behavior

by hexcoder (Curate)
on May 26, 2024 at 14:23 UTC ( [id://11159674]=perlquestion: print w/replies, xml ) Need Help??

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

Dear fellow monks!

In my application I had my %hash; along with some imported modules.

When my hash was populated completely (and should be constant afterwards), I wanted to see where and when $hash{'myEntry'} changed unexpectedly. But using a watchpoint w $hash{'myEntry'} in the debugger led to a lot of unwanted breaks: each time the package/module scope changed the visibility of %hash changed also, of course.

Has anyone solved this problem of how to ignore breaks from visibility/scope changes?
Thanks!

Replies are listed 'Best First'.
Re: debugger watchpoint behavior
by haj (Vicar) on May 26, 2024 at 15:24 UTC

    I only have a workaround: set a break (or continue to the point) where %hash is initialized. Then create a reference to that hash with $href = \%hash, and watch with w $href->{'myEntry'}.

    The variables you define in the debugger don't go out of scope. But note that the behavior of your code will change if you want %hash to go out of scope and maybe be re-initialized later: As long as the reference lives, %hash will stick around.

      I didn't like the idea of having an extra reference count, because this smells like Heisenbug.

      So I thought weakening it might be a good idea, but then I realized that the reference might change and the global var will fail.

      Tricky. I could think of some approaches to solve this, but I'm AFK right now.

      And the OP didn't show us any code...

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

Re: debugger watchpoint behavior
by choroba (Cardinal) on May 26, 2024 at 18:43 UTC
    If you know what the unexpected value is, you can set the watch expression to check the value is not the unexpected one, so it will stay true even for a non-existent value.
    w $hash{key} ne "unexpected"

    Note that if the value is changed on the last line of a subroutine, the debugger won't show it, instead it will notice the change the next time you enter the scope.

    Example:

    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; { my %hash; sub set { %hash = (a => 12, b => 14); } sub get { my ($key) = @_; return $hash{$key} } sub change { $hash{b} = 'Boo'; } } set(); say get('b'); change(); say get('b');

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: debugger watchpoint behavior
by Danny (Pilgrim) on May 26, 2024 at 15:17 UTC
    This suggests creating a temporary global reference to your variable and "watching" that.
      Thanks, I tested the tip from stackoverflow using a watch with a hash reference in a global variable, and it seemed to work!
      BTW: I think this useful technique should be known more!
Re: debugger watchpoint behavior
by Anonymous Monk on May 27, 2024 at 12:53 UTC

    Another way to find out where the hash is being changed is to use Hash::Util 'lock_hash'; and then lock_hash( %hash ); once it has been set up. This turns any attempt to assign to the hash into a fatal exception.

Re: debugger watchpoint behavior
by LanX (Saint) on May 26, 2024 at 23:50 UTC
    What if you created a global %hash which is a copy of the scoped %hash?

    The global one would only be visible outside the scope and produce no value changes when watched.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

Re: debugger watchpoint behavior
by The_Dj (Scribe) on May 28, 2024 at 06:37 UTC
    What I've had to do in such cases is to set a breakpoint with the condition.

    e.g. if I have
    %hash=(1,2,3,4); while (1) { print "Something...\n"; ===cut=== }

    you can put in b 3 $hash{1} ne 2
    In a real scenario you may need to run to after %hash is populated to find out what the value is

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11159674]
Approved by haj
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-06-22 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.