1 | package Devel::FindRef; |
1 | package Devel::FindRef; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
5 | use XSLoader; |
5 | use XSLoader; |
6 | |
6 | use Scalar::Util; |
7 | |
7 | |
8 | BEGIN { |
8 | BEGIN { |
9 | our $VERSION = '0.1'; |
9 | our $VERSION = '1.1'; |
10 | XSLoader::load __PACKAGE__, $VERSION; |
10 | XSLoader::load __PACKAGE__, $VERSION; |
11 | } |
11 | } |
12 | |
12 | |
13 | =head1 NAME |
13 | =head1 NAME |
14 | |
14 | |
… | |
… | |
19 | use Devel::FindRef; |
19 | use Devel::FindRef; |
20 | |
20 | |
21 | =head1 DESCRIPTION |
21 | =head1 DESCRIPTION |
22 | |
22 | |
23 | Tracking down reference problems (e.g. you expect some object to be |
23 | 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 |
24 | destroyed, but there are still references to it that keep it alive) can be |
25 | be very hard, although perl keeps track of all values. |
25 | very hard. Fortunately, perl keeps track of all its values, so tracking |
|
|
26 | references "backwards" is usually possible. |
26 | |
27 | |
27 | The C<track> function can hlep track down some of those refernces back to |
28 | The C<track> function can help track down some of those references back to |
28 | the variables containing them. |
29 | the variables containing them. |
29 | |
30 | |
30 | For example, for this fragment: |
31 | For example, for this fragment: |
31 | |
32 | |
32 | package Test; |
33 | package Test; |
… | |
… | |
41 | print Devel::FindRef::track \$var; |
42 | print Devel::FindRef::track \$var; |
42 | } |
43 | } |
43 | |
44 | |
44 | testsub; |
45 | testsub; |
45 | |
46 | |
46 | The output is as follows (or similar to htis, in case I forget to update |
47 | The output is as follows (or similar to this, in case I forget to update |
47 | the manpage afetr some changes): |
48 | the manpage after some changes): |
48 | |
49 | |
49 | SCALAR(0x676fa0) is |
50 | SCALAR(0x676fa0) is |
50 | referenced by REF(0x676fb0), which is |
51 | referenced by REF(0x676fb0), which is |
51 | in the lexical '$x' in CODE(0x676370), which is |
52 | in the lexical '$x' in CODE(0x676370), which is |
52 | not found anywhere I looked :( |
53 | not found anywhere I looked :( |
… | |
… | |
61 | in the global &Test::testsub. |
62 | in the global &Test::testsub. |
62 | referenced by REF(0x77bc80), which is |
63 | referenced by REF(0x77bc80), which is |
63 | in the global $Test::hash2. |
64 | in the global $Test::hash2. |
64 | |
65 | |
65 | |
66 | |
66 | It is a bit convoluted to read, but basically it says that the value stored in C<$var> |
67 | It is a bit convoluted to read, but basically it says that the value |
67 | can be found: |
68 | stored in C<$var> can be found: |
68 | |
69 | |
69 | =over 4 |
70 | =over 4 |
70 | |
71 | |
71 | =item - in some variable C<$x> whose origin is not known (I frankly have no |
72 | =item - in some variable C<$x> whose origin is not known (I frankly have no |
72 | idea why, hints accepted). |
73 | idea why, hints accepted). |
… | |
… | |
76 | =item - in the global variable named C<$Test::var>. |
77 | =item - in the global variable named C<$Test::var>. |
77 | |
78 | |
78 | =item - in the hash element C<ukukey2>, in the hash in the my variable |
79 | =item - in the hash element C<ukukey2>, in the hash in the my variable |
79 | C<$local> in the sub C<Test::testsub> and also in the hash referenced by |
80 | C<$local> in the sub C<Test::testsub> and also in the hash referenced by |
80 | C<$Test::hash2>. |
81 | C<$Test::hash2>. |
|
|
82 | |
|
|
83 | =back |
81 | |
84 | |
82 | =head1 EXPORTS |
85 | =head1 EXPORTS |
83 | |
86 | |
84 | None. |
87 | None. |
85 | |
88 | |
… | |
… | |
98 | =cut |
101 | =cut |
99 | |
102 | |
100 | sub find($); |
103 | sub find($); |
101 | |
104 | |
102 | sub track { |
105 | sub track { |
|
|
106 | my ($ref, $depth) = @_; |
|
|
107 | @_ = (); |
|
|
108 | |
103 | my $buf = ""; |
109 | my $buf = ""; |
104 | |
110 | |
|
|
111 | Scalar::Util::weaken $ref; |
|
|
112 | |
105 | my $track; $track = sub { |
113 | my $track; $track = sub { |
106 | my (undef, $depth, $indent) = @_; |
114 | my ($refref, $depth, $indent) = @_; |
107 | |
115 | |
108 | if ($depth) { |
116 | if ($depth) { |
109 | my (@about) = find $_[0]; |
117 | my (@about) = find $$refref; |
110 | if (@about) { |
118 | if (@about) { |
111 | for my $about (@about) { |
119 | for my $about (@about) { |
112 | $buf .= (" ") x $indent; |
120 | $buf .= (" ") x $indent; |
113 | $buf .= $about->[0]; |
121 | $buf .= $about->[0]; |
114 | if (@$about > 1) { |
122 | if (@$about > 1) { |
115 | $buf .= " $about->[1], which is\n"; |
123 | $buf .= " $about->[1], which is\n"; |
116 | $track->($about->[1], $depth - 1, $indent + 1); |
124 | $track->(\$about->[1], $depth - 1, $indent + 1); |
117 | } else { |
125 | } else { |
118 | $buf .= ".\n"; |
126 | $buf .= ".\n"; |
119 | } |
127 | } |
120 | } |
128 | } |
121 | } else { |
129 | } else { |
… | |
… | |
126 | $buf .= (" ") x $indent; |
134 | $buf .= (" ") x $indent; |
127 | $buf .= "not referenced within the search depth.\n"; |
135 | $buf .= "not referenced within the search depth.\n"; |
128 | } |
136 | } |
129 | }; |
137 | }; |
130 | |
138 | |
131 | $buf .= "$_[0] is\n"; |
139 | $buf .= "$ref is\n"; |
132 | $track->($_[0], $_[1] || 10, 1); |
140 | $track->(\$ref, $depth || 10, 1); |
133 | $buf |
141 | $buf |
134 | } |
142 | } |
135 | |
143 | |
136 | =item @references = Devel::FindRef::find $ref |
144 | =item @references = Devel::FindRef::find $ref |
137 | |
145 | |
138 | Return arrayrefs that contain [$message, $ref] pairs. The message |
146 | Return arrayrefs that contain [$message, $ref] pairs. The message |
139 | describes what kind of reference was found and the C<$ref> is the |
147 | describes what kind of reference was found and the C<$ref> is the |
140 | reference itself, which cna be omitted if C<find> decided to end the |
148 | reference itself, which can be omitted if C<find> decided to end the |
141 | search. |
149 | search. The returned references are all weak references. |
142 | |
150 | |
143 | The C<track> function uses this to find references to the value you are |
151 | The C<track> function uses this to find references to the value you are |
144 | interested in and recurses on the returned references. |
152 | interested in and recurses on the returned references. |
145 | |
153 | |
146 | =cut |
154 | =cut |
147 | |
155 | |
148 | sub find($) { |
156 | sub find($) { |
149 | my ($about, $excl) = &find_; |
157 | my ($about, $excl) = &find_; |
150 | my %excl = map +($_ => 1), @$excl; |
158 | my %excl = map +($_ => undef), @$excl; |
151 | grep !$excl{$_->[1] + 0}, @$about |
159 | grep !exists $excl{$_->[1] + 0}, @$about |
152 | } |
160 | } |
153 | |
161 | |
154 | =item $ref = Devel::FindRef::ref2ptr $ptr |
162 | =item $ref = Devel::FindRef::ptr2ref $integer |
155 | |
163 | |
156 | Sometimes you know (from debugging output) the address of a perl scalar |
164 | Sometimes you know (from debugging output) the address of a perl scalar |
157 | you are interested in. This function can be used to turn the address into |
165 | you are interested in (e.g. C<HASH(0x176ff70)>). This function can be used |
158 | a reference to that scalar. It is quite safe to call on valid addresses, |
166 | to turn the address into a reference to that scalar. It is quite safe to |
159 | but extremely dangerous to call on invalid ones. |
167 | call on valid addresses, but extremely dangerous to call on invalid ones. |
|
|
168 | |
|
|
169 | # we know that HASH(0x176ff70) exists, so turn it into a hashref: |
|
|
170 | my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70; |
160 | |
171 | |
161 | =back |
172 | =back |
162 | |
173 | |
163 | =head1 AUTHOR |
174 | =head1 AUTHOR |
164 | |
175 | |
165 | Marc Lehmann <pcg@goof.com>. |
176 | Marc Lehmann <pcg@goof.com>. |
166 | |
177 | |
167 | =head1 BUGS |
178 | =head1 BUGS |
168 | |
179 | |
169 | Only code values, arrays, hashes and scalars are being looked at. Tied |
180 | Only code values, arrays, hashes, scalars and magic are being looked at. |
170 | values and magic is not currently being investigated. |
181 | |
|
|
182 | This is a quick hack only. |
171 | |
183 | |
172 | =head1 COPYRIGHT AND LICENSE |
184 | =head1 COPYRIGHT AND LICENSE |
173 | |
185 | |
174 | Copyright (C) 2007 by Marc Lehmann. |
186 | Copyright (C) 2007 by Marc Lehmann. |
175 | |
187 | |