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