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