ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Devel-FindRef/FindRef.pm
(Generate patch)

Comparing Devel-FindRef/FindRef.pm (file contents):
Revision 1.12 by root, Fri Jul 11 21:09:30 2008 UTC vs.
Revision 1.33 by root, Fri Jun 23 14:42:13 2017 UTC

1package Devel::FindRef; 1package Devel::FindRef;
2 2
3use strict; 3use common::sense;
4 4
5use XSLoader; 5use XSLoader;
6use Scalar::Util; 6use Scalar::Util;
7 7
8BEGIN { 8BEGIN {
9 our $VERSION = '1.2'; 9 our $VERSION = 1.45;
10 XSLoader::load __PACKAGE__, $VERSION; 10 XSLoader::load __PACKAGE__, $VERSION;
11} 11}
12 12
13=head1 NAME 13=head1 NAME
14 14
15Devel::FindRef - where is that reference to my scalar hiding? 15Devel::FindRef - where is that reference to my variable hiding?
16 16
17=head1 SYNOPSIS 17=head1 SYNOPSIS
18 18
19 use Devel::FindRef; 19 use Devel::FindRef;
20
21 print Devel::FindRef::track \$some_variable;
20 22
21=head1 DESCRIPTION 23=head1 DESCRIPTION
22 24
23Tracking down reference problems (e.g. you expect some object to be 25Tracking down reference problems (e.g. you expect some object to be
24destroyed, but there are still references to it that keep it alive) can be 26destroyed, but there are still references to it that keep it alive) can be
28The C<track> function can help track down some of those references back to 30The C<track> function can help track down some of those references back to
29the variables containing them. 31the variables containing them.
30 32
31For example, for this fragment: 33For example, for this fragment:
32 34
33 package Test; 35 package Test;
36
37 use Devel::FindRef;
38 use Scalar::Util;
34 39
35 our $var = "hi\n"; 40 our $var = "hi\n";
36 my $x = \$var; 41 my $global_my = \$var;
37 our %hash = (ukukey => \$var); 42 our %global_hash = (ukukey => \$var);
38 our $hash2 = {ukukey2 => \$var}; 43 our $global_hashref = { ukukey2 => \$var };
39 44
40 sub testsub { 45 sub testsub {
41 my $local = $hash2; 46 my $testsub_local = $global_hashref;
42 print Devel::FindRef::track \$var; 47 print Devel::FindRef::track \$var;
43 } 48 }
44 49
45 testsub; 50 my $closure = sub {
51 my $closure_var = \$_[0];
52 Scalar::Util::weaken (my $weak_ref = \$var);
53 testsub;
54 };
55
56 $closure->($var);
46 57
47The output is as follows (or similar to this, in case I forget to update 58The output is as follows (or similar to this, in case I forget to update
48the manpage after some changes): 59the manpage after some changes):
49 60
50 SCALAR(0x7bd2d0) is 61 SCALAR(0x7cc888) [refcount 6] is
62 +- referenced by REF(0x8abcc8) [refcount 1], which is
63 | the lexical '$closure_var' in CODE(0x8abc50) [refcount 4], which is
64 | +- the closure created at tst:18.
65 | +- referenced by REF(0x7d3c58) [refcount 1], which is
66 | | the lexical '$closure' in CODE(0x7ae530) [refcount 2], which is
67 | | +- the containing scope for CODE(0x8ab430) [refcount 3], which is
68 | | | the global &Test::testsub.
69 | | +- the main body of the program.
70 | +- the lexical '&' in CODE(0x7ae530) [refcount 2], which was seen before.
71 +- referenced by REF(0x7cc7c8) [refcount 1], which is
72 | the lexical '$global_my' in CODE(0x7ae530) [refcount 2], which was seen before.
51 in the global $Test::var. 73 +- the global $Test::var.
52 referenced by REF(0x7bd240), which is 74 +- referenced by REF(0x7cc558) [refcount 1], which is
53 in the member 'ukukey2' of HASH(0x7bd228), which is 75 | the member 'ukukey2' of HASH(0x7ae140) [refcount 2], which is
54 referenced by REF(0x81dae8), which is 76 | +- referenced by REF(0x8abad0) [refcount 1], which is
55 in the lexical '$local' in CODE(0x81da88), which is 77 | | the lexical '$testsub_local' in CODE(0x8ab430) [refcount 3], which was seen before.
56 in the global &Test::testsub. 78 | +- referenced by REF(0x8ab4f0) [refcount 1], which is
57 referenced by REF(0x81da40), which is 79 | the global $Test::global_hashref.
58 in the global $Test::hash2. 80 +- referenced by REF(0x7ae518) [refcount 1], which is
59 referenced by REF(0x79f3f8), which is 81 | the member 'ukukey' of HASH(0x7d3bb0) [refcount 1], which is
60 in the lexical '$x' in CODE(0x79f518), which is 82 | the global %Test::global_hash.
61 the containing scope for CODE(0x81da88), which is
62 in the global &Test::testsub.
63 referenced by REF(0x79f2f0), which is 83 +- referenced by REF(0x7ae2f0) [refcount 1], which is
64 not found anywhere I looked :( 84 a temporary on the stack.
65 referenced by REF(0x79f140), which is
66 in the member 'ukukey' of HASH(0x81d698), which is
67 in the global %Test::hash.
68 85
69It is a bit convoluted to read, but basically it says that the value 86It is a bit convoluted to read, but basically it says that the value
70stored in C<$var> can be found: 87stored in C<$var> is referenced by:
71 88
72=over 4 89=over 4
73 90
74=item - in some variable C<$x> whose origin is not known (I frankly have no 91=item - the lexical C<$closure_var> (0x8abcc8), which is inside an instantiated
75idea why, hints accepted). 92closure, which in turn is used quite a bit.
76 93
77=item - in the hash element with key C<ukukey> in the hash stored in C<%Test::hash>. 94=item - the package-level lexical C<$global_my>.
78 95
79=item - in the global variable named C<$Test::var>. 96=item - the global package variable named C<$Test::var>.
80 97
81=item - in the hash element C<ukukey2>, in the hash in the my variable 98=item - the hash element C<ukukey2>, in the hash in the my variable
82C<$local> in the sub C<Test::testsub> and also in the hash referenced by 99C<$testsub_local> in the sub C<Test::testsub> and also in the hash
100C<$referenced by Test::hash2>.
101
102=item - the hash element with key C<ukukey> in the hash stored in
83C<$Test::hash2>. 103C<%Test::hash>.
104
105=item - some anonymous mortalised reference on the stack (which is caused
106by calling C<track> with the expression C<\$var>, which creates the
107reference).
84 108
85=back 109=back
110
111And all these account for six reference counts.
86 112
87=head1 EXPORTS 113=head1 EXPORTS
88 114
89None. 115None.
90 116
96 122
97Track the perl value pointed to by C<$ref> up to a depth of C<$depth> and 123Track the perl value pointed to by C<$ref> up to a depth of C<$depth> and
98return a descriptive string. C<$ref> can point at any perl value, be it 124return a descriptive string. C<$ref> can point at any perl value, be it
99anonymous sub, hash, array, scalar etc. 125anonymous sub, hash, array, scalar etc.
100 126
101This is the function you most often use. 127This is the function you most likely want to use when tracking down
128references.
102 129
103=cut 130=cut
104 131
105sub find($); 132sub find($);
133
134sub _f($) {
135 "$_[0] [refcount " . (_refcnt $_[0]) . "]"
136}
106 137
107sub track { 138sub track {
108 my ($ref, $depth) = @_; 139 my ($ref, $depth) = @_;
109 @_ = (); 140 @_ = ();
110 141
118 149
119 if ($depth) { 150 if ($depth) {
120 my (@about) = find $$refref; 151 my (@about) = find $$refref;
121 if (@about) { 152 if (@about) {
122 for my $about (@about) { 153 for my $about (@about) {
154 $about->[0] =~ s/([^\x20-\x7e])/sprintf "\\{%02x}", ord $1/ge;
123 $buf .= "$indent" . (@about > 1 ? "+- " : " ") . $about->[0]; 155 $buf .= "$indent" . (@about > 1 ? "+- " : "") . $about->[0];
124 if (@$about > 1) { 156 if (@$about > 1) {
125 if ($seen{$about->[1]+0}++) { 157 if ($seen{ref2ptr $about->[1]}++) {
126 $buf .= " $about->[1], which was seen before.\n"; 158 $buf .= " " . (_f $about->[1]) . ", which was seen before.\n";
127 } else { 159 } else {
128 $buf .= " $about->[1], which is\n"; 160 $buf .= " " . (_f $about->[1]) . ", which is\n";
129 $track->(\$about->[1], $depth - 1, $about == $about[-1] ? "$indent " : "$indent| "); 161 $track->(\$about->[1], $depth - 1, $about == $about[-1] ? "$indent " : "$indent| ");
130 } 162 }
131 } else { 163 } else {
132 $buf .= ".\n"; 164 $buf .= ".\n";
133 } 165 }
138 } else { 170 } else {
139 $buf .= "$indent not referenced within the search depth.\n"; 171 $buf .= "$indent not referenced within the search depth.\n";
140 } 172 }
141 }; 173 };
142 174
143 $buf .= "$ref is\n"; 175 $buf .= (_f $ref) . " is\n";
176
144 $track->(\$ref, $depth || $ENV{PERL_DEVEL_FINDREF_DEPTH} || 10, ""); 177 $track->(\$ref, $depth || $ENV{PERL_DEVEL_FINDREF_DEPTH} || 10, "");
145 $buf 178 $buf
146} 179}
147 180
148=item @references = Devel::FindRef::find $ref 181=item @references = Devel::FindRef::find $ref
158=cut 191=cut
159 192
160sub find($) { 193sub find($) {
161 my ($about, $excl) = &find_; 194 my ($about, $excl) = &find_;
162 my %excl = map +($_ => undef), @$excl; 195 my %excl = map +($_ => undef), @$excl;
163 grep !exists $excl{$_->[1] + 0}, @$about 196 grep !($#$_ && exists $excl{ref2ptr $_->[1]}), @$about
164} 197}
165 198
166=item $ref = Devel::FindRef::ptr2ref $integer 199=item $ref = Devel::FindRef::ptr2ref $integer
167 200
168Sometimes you know (from debugging output) the address of a perl scalar 201Sometimes you know (from debugging output) the address of a perl value you
169you are interested in (e.g. C<HASH(0x176ff70)>). This function can be used 202are interested in (e.g. C<HASH(0x176ff70)>). This function can be used to
170to turn the address into a reference to that scalar. It is quite safe to 203turn the address into a reference to that value. It is quite safe to call
171call on valid addresses, but extremely dangerous to call on invalid ones. 204on valid addresses, but extremely dangerous to call on invalid ones. I<No
205checks whatsoever will be done>, so don't use this unless you really know
206the value is the address of a valid perl value.
172 207
173 # we know that HASH(0x176ff70) exists, so turn it into a hashref: 208 # we know that HASH(0x176ff70) exists, so turn it into a hashref:
174 my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70; 209 my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70;
175 210
211=item $ptr = Devel::FindRef::ref2ptr $reference
212
213The opposite of C<ptr2ref>, above: returns the internal address of the
214value pointed to by the passed reference. This function is safe to call on
215anything, and returns the same value that a normal reference would if used
216in a numeric context.
217
176=back 218=back
177 219
178=head1 ENVIRONMENT VARIABLES 220=head1 ENVIRONMENT VARIABLES
179 221
180You can set the environment variable C<PERL_DEVEL_FINDREF_DEPTH> to an 222You can set the environment variable C<PERL_DEVEL_FINDREF_DEPTH> to an
181integer to override the default depth in C<track>. If a call explicitly 223integer to override the default depth in C<track>. If a call explicitly
182specified a depth it is not overridden. 224specifies a depth, it is not overridden.
183 225
184=head1 AUTHOR 226=head1 AUTHOR
185 227
186Marc Lehmann <pcg@goof.com>. 228Marc Lehmann <pcg@goof.com>.
187 229
188=head1 BUGS
189
190Only code values, arrays, hashes, scalars and magic are being looked at.
191
192This is a quick hack only.
193
194=head1 COPYRIGHT AND LICENSE 230=head1 COPYRIGHT AND LICENSE
195 231
196Copyright (C) 2007 by Marc Lehmann. 232Copyright (C) 2007, 2008, 2009, 2013 by Marc Lehmann.
197 233
198This library is free software; you can redistribute it and/or modify 234This library is free software; you can redistribute it and/or modify
199it under the same terms as Perl itself, either Perl version 5.8.8 or, 235it under the same terms as Perl itself, either Perl version 5.8.8 or,
200at your option, any later version of Perl 5 you may have available. 236at your option, any later version of Perl 5 you may have available.
201 237

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines