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