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