Forgotten from #16656.
[p5sagit/p5-mst-13.2.git] / ext / B / B / Debug.pm
1 package B::Debug;
2
3 our $VERSION = '1.00';
4
5 use strict;
6 use B qw(peekop class walkoptree walkoptree_exec
7          main_start main_root cstring sv_undef);
8 use B::Asmdata qw(@specialsv_name);
9
10 my %done_gv;
11
12 sub B::OP::debug {
13     my ($op) = @_;
14     printf <<'EOT', class($op), $$op, ${$op->next}, ${$op->sibling}, $op->ppaddr, $op->targ, $op->type, $op->seq, $op->flags, $op->private;
15 %s (0x%lx)
16         op_next         0x%x
17         op_sibling      0x%x
18         op_ppaddr       %s
19         op_targ         %d
20         op_type         %d
21         op_seq          %d
22         op_flags        %d
23         op_private      %d
24 EOT
25 }
26
27 sub B::UNOP::debug {
28     my ($op) = @_;
29     $op->B::OP::debug();
30     printf "\top_first\t0x%x\n", ${$op->first};
31 }
32
33 sub B::BINOP::debug {
34     my ($op) = @_;
35     $op->B::UNOP::debug();
36     printf "\top_last\t\t0x%x\n", ${$op->last};
37 }
38
39 sub B::LOOP::debug {
40     my ($op) = @_;
41     $op->B::BINOP::debug();
42     printf <<'EOT', ${$op->redoop}, ${$op->nextop}, ${$op->lastop};
43        op_redoop       0x%x
44        op_nextop       0x%x
45        op_lastop       0x%x
46 EOT
47 }
48
49 sub B::LOGOP::debug {
50     my ($op) = @_;
51     $op->B::UNOP::debug();
52     printf "\top_other\t0x%x\n", ${$op->other};
53 }
54
55 sub B::LISTOP::debug {
56     my ($op) = @_;
57     $op->B::BINOP::debug();
58     printf "\top_children\t%d\n", $op->children;
59 }
60
61 sub B::PMOP::debug {
62     my ($op) = @_;
63     $op->B::LISTOP::debug();
64     printf "\top_pmreplroot\t0x%x\n", ${$op->pmreplroot};
65     printf "\top_pmreplstart\t0x%x\n", ${$op->pmreplstart};
66     printf "\top_pmnext\t0x%x\n", ${$op->pmnext};
67     printf "\top_pmregexp->precomp\t%s\n", cstring($op->precomp);
68     printf "\top_pmflags\t0x%x\n", $op->pmflags;
69     $op->pmreplroot->debug;
70 }
71
72 sub B::COP::debug {
73     my ($op) = @_;
74     $op->B::OP::debug();
75     printf <<'EOT', $op->label, $op->stashpv, $op->file, $op->seq, $op->arybase, $op->line, ${$op->warnings};
76         cop_label       %s
77         cop_stashpv     %s
78         cop_file        %s
79         cop_seq         %d
80         cop_arybase     %d
81         cop_line        %d
82         cop_warnings    0x%x
83 EOT
84 }
85
86 sub B::SVOP::debug {
87     my ($op) = @_;
88     $op->B::OP::debug();
89     printf "\top_sv\t\t0x%x\n", ${$op->sv};
90     $op->sv->debug;
91 }
92
93 sub B::PVOP::debug {
94     my ($op) = @_;
95     $op->B::OP::debug();
96     printf "\top_pv\t\t%s\n", cstring($op->pv);
97 }
98
99 sub B::PADOP::debug {
100     my ($op) = @_;
101     $op->B::OP::debug();
102     printf "\top_padix\t\t%ld\n", $op->padix;
103 }
104
105 sub B::CVOP::debug {
106     my ($op) = @_;
107     $op->B::OP::debug();
108     printf "\top_cv\t\t0x%x\n", ${$op->cv};
109 }
110
111 sub B::NULL::debug {
112     my ($sv) = @_;
113     if ($$sv == ${sv_undef()}) {
114         print "&sv_undef\n";
115     } else {
116         printf "NULL (0x%x)\n", $$sv;
117     }
118 }
119
120 sub B::SV::debug {
121     my ($sv) = @_;
122     if (!$$sv) {
123         print class($sv), " = NULL\n";
124         return;
125     }
126     printf <<'EOT', class($sv), $$sv, $sv->REFCNT, $sv->FLAGS;
127 %s (0x%x)
128         REFCNT          %d
129         FLAGS           0x%x
130 EOT
131 }
132
133 sub B::RV::debug {
134     my ($rv) = @_;
135     B::SV::debug($rv);
136     printf <<'EOT', ${$rv->RV};
137         RV              0x%x
138 EOT
139     $rv->RV->debug;
140 }
141
142 sub B::PV::debug {
143     my ($sv) = @_;
144     $sv->B::SV::debug();
145     my $pv = $sv->PV();
146     printf <<'EOT', cstring($pv), length($pv);
147         xpv_pv          %s
148         xpv_cur         %d
149 EOT
150 }
151
152 sub B::IV::debug {
153     my ($sv) = @_;
154     $sv->B::SV::debug();
155     printf "\txiv_iv\t\t%d\n", $sv->IV;
156 }
157
158 sub B::NV::debug {
159     my ($sv) = @_;
160     $sv->B::IV::debug();
161     printf "\txnv_nv\t\t%s\n", $sv->NV;
162 }
163
164 sub B::PVIV::debug {
165     my ($sv) = @_;
166     $sv->B::PV::debug();
167     printf "\txiv_iv\t\t%d\n", $sv->IV;
168 }
169
170 sub B::PVNV::debug {
171     my ($sv) = @_;
172     $sv->B::PVIV::debug();
173     printf "\txnv_nv\t\t%s\n", $sv->NV;
174 }
175
176 sub B::PVLV::debug {
177     my ($sv) = @_;
178     $sv->B::PVNV::debug();
179     printf "\txlv_targoff\t%d\n", $sv->TARGOFF;
180     printf "\txlv_targlen\t%u\n", $sv->TARGLEN;
181     printf "\txlv_type\t%s\n", cstring(chr($sv->TYPE));
182 }
183
184 sub B::BM::debug {
185     my ($sv) = @_;
186     $sv->B::PVNV::debug();
187     printf "\txbm_useful\t%d\n", $sv->USEFUL;
188     printf "\txbm_previous\t%u\n", $sv->PREVIOUS;
189     printf "\txbm_rare\t%s\n", cstring(chr($sv->RARE));
190 }
191
192 sub B::CV::debug {
193     my ($sv) = @_;
194     $sv->B::PVNV::debug();
195     my ($stash) = $sv->STASH;
196     my ($start) = $sv->START;
197     my ($root) = $sv->ROOT;
198     my ($padlist) = $sv->PADLIST;
199     my ($file) = $sv->FILE;
200     my ($gv) = $sv->GV;
201     printf <<'EOT', $$stash, $$start, $$root, $$gv, $file, $sv->DEPTH, $padlist, ${$sv->OUTSIDE};
202         STASH           0x%x
203         START           0x%x
204         ROOT            0x%x
205         GV              0x%x
206         FILE            %s
207         DEPTH           %d
208         PADLIST         0x%x                           
209         OUTSIDE         0x%x
210 EOT
211     $start->debug if $start;
212     $root->debug if $root;
213     $gv->debug if $gv;
214     $padlist->debug if $padlist;
215 }
216
217 sub B::AV::debug {
218     my ($av) = @_;
219     $av->B::SV::debug;
220     my(@array) = $av->ARRAY;
221     print "\tARRAY\t\t(", join(", ", map("0x" . $$_, @array)), ")\n";
222     printf <<'EOT', scalar(@array), $av->MAX, $av->OFF, $av->AvFLAGS;
223         FILL            %d    
224         MAX             %d
225         OFF             %d
226         AvFLAGS         %d
227 EOT
228 }
229     
230 sub B::GV::debug {
231     my ($gv) = @_;
232     if ($done_gv{$$gv}++) {
233         printf "GV %s::%s\n", $gv->STASH->NAME, $gv->SAFENAME;
234         return;
235     }
236     my ($sv) = $gv->SV;
237     my ($av) = $gv->AV;
238     my ($cv) = $gv->CV;
239     $gv->B::SV::debug;
240     printf <<'EOT', $gv->SAFENAME, $gv->STASH->NAME, $gv->STASH, $$sv, $gv->GvREFCNT, $gv->FORM, $$av, ${$gv->HV}, ${$gv->EGV}, $$cv, $gv->CVGEN, $gv->LINE, $gv->FILE, $gv->GvFLAGS;
241         NAME            %s
242         STASH           %s (0x%x)
243         SV              0x%x
244         GvREFCNT        %d
245         FORM            0x%x
246         AV              0x%x
247         HV              0x%x
248         EGV             0x%x
249         CV              0x%x
250         CVGEN           %d
251         LINE            %d
252         FILE            %s
253         GvFLAGS         0x%x
254 EOT
255     $sv->debug if $sv;
256     $av->debug if $av;
257     $cv->debug if $cv;
258 }
259
260 sub B::SPECIAL::debug {
261     my $sv = shift;
262     print $specialsv_name[$$sv], "\n";
263 }
264
265 sub compile {
266     my $order = shift;
267     B::clearsym();
268     if ($order && $order eq "exec") {
269         return sub { walkoptree_exec(main_start, "debug") }
270     } else {
271         return sub { walkoptree(main_root, "debug") }
272     }
273 }
274
275 1;
276
277 __END__
278
279 =head1 NAME
280
281 B::Debug - Walk Perl syntax tree, printing debug info about ops
282
283 =head1 SYNOPSIS
284
285         perl -MO=Debug[,OPTIONS] foo.pl
286
287 =head1 DESCRIPTION
288
289 See F<ext/B/README>.
290
291 =head1 AUTHOR
292
293 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
294
295 =cut