This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: Inline PI function
[perl5.git] / dump.c
CommitLineData
a0d0e21e 1/* dump.c
a687059c 2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
a687059c 4 *
6e21c824
LW
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
8d063cd8 7 *
a0d0e21e
LW
8 */
9
10/*
11 * "'You have talked long in your sleep, Frodo,' said Gandalf gently, 'and
12 * it has not been hard for me to read your mind and memory.'"
8d063cd8
LW
13 */
14
8d063cd8 15#include "EXTERN.h"
8d063cd8
LW
16#include "perl.h"
17
2304df62
AD
18#ifndef DEBUGGING
19void
20dump_all()
21{
22}
23#else /* Rest of file is for DEBUGGING */
8d063cd8 24
4db58590
PP
25#ifdef I_STDARG
26static void dump(char *pat, ...);
27#else
8adcabd8 28static void dump();
4db58590 29#endif
8adcabd8
LW
30
31void
93a17b20 32dump_all()
79072805 33{
760ac839 34 PerlIO_setlinebuf(Perl_debug_log);
93a17b20
LW
35 if (main_root)
36 dump_op(main_root);
463ee0b2
LW
37 dump_packsubs(defstash);
38}
39
40void
41dump_packsubs(stash)
42HV* stash;
43{
a0d0e21e 44 I32 i;
463ee0b2
LW
45 HE *entry;
46
8990e307
LW
47 if (!HvARRAY(stash))
48 return;
a0d0e21e 49 for (i = 0; i <= (I32) HvMAX(stash); i++) {
4db58590
PP
50 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
51 GV *gv = (GV*)HeVAL(entry);
463ee0b2 52 HV *hv;
8ebc5c01 53 if (GvCVu(gv))
463ee0b2 54 dump_sub(gv);
85e6fe83
LW
55 if (GvFORM(gv))
56 dump_form(gv);
4db58590 57 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
a0d0e21e 58 (hv = GvHV(gv)) && HvNAME(hv) && hv != defstash)
463ee0b2
LW
59 dump_packsubs(hv); /* nested package */
60 }
79072805
LW
61 }
62}
63
64void
93a17b20
LW
65dump_sub(gv)
66GV* gv;
a687059c 67{
8990e307 68 SV *sv = sv_newmortal();
85e6fe83 69
f6aff53a 70 gv_fullname3(sv, gv, Nullch);
85e6fe83 71 dump("\nSUB %s = ", SvPVX(sv));
a0d0e21e 72 if (CvXSUB(GvCV(gv)))
85e6fe83 73 dump("(xsub 0x%x %d)\n",
a0d0e21e
LW
74 (long)CvXSUB(GvCV(gv)),
75 CvXSUBANY(GvCV(gv)).any_i32);
85e6fe83
LW
76 else if (CvROOT(GvCV(gv)))
77 dump_op(CvROOT(GvCV(gv)));
78 else
79 dump("<undef>\n");
80}
81
82void
83dump_form(gv)
84GV* gv;
85{
86 SV *sv = sv_newmortal();
87
f6aff53a 88 gv_fullname3(sv, gv, Nullch);
85e6fe83
LW
89 dump("\nFORMAT %s = ", SvPVX(sv));
90 if (CvROOT(GvFORM(gv)))
91 dump_op(CvROOT(GvFORM(gv)));
92 else
93 dump("<undef>\n");
a687059c
LW
94}
95
8adcabd8 96void
79072805 97dump_eval()
8d063cd8 98{
79072805
LW
99 dump_op(eval_root);
100}
101
102void
103dump_op(op)
104register OP *op;
105{
106 SV *tmpsv;
107
79072805 108 dump("{\n");
93a17b20 109 if (op->op_seq)
760ac839 110 PerlIO_printf(Perl_debug_log, "%-4d", op->op_seq);
93a17b20 111 else
760ac839 112 PerlIO_printf(Perl_debug_log, " ");
79072805 113 dump("TYPE = %s ===> ", op_name[op->op_type]);
93a17b20
LW
114 if (op->op_next) {
115 if (op->op_seq)
760ac839 116 PerlIO_printf(Perl_debug_log, "%d\n", op->op_next->op_seq);
93a17b20 117 else
760ac839 118 PerlIO_printf(Perl_debug_log, "(%d)\n", op->op_next->op_seq);
93a17b20 119 }
79072805 120 else
760ac839 121 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805 122 dumplvl++;
8990e307
LW
123 if (op->op_targ) {
124 if (op->op_type == OP_NULL)
125 dump(" (was %s)\n", op_name[op->op_targ]);
126 else
127 dump("TARG = %d\n", op->op_targ);
128 }
748a9306 129#ifdef DUMPADDR
79072805
LW
130 dump("ADDR = 0x%lx => 0x%lx\n",op, op->op_next);
131#endif
132 if (op->op_flags) {
8d063cd8 133 *buf = '\0';
79072805
LW
134 if (op->op_flags & OPf_KNOW) {
135 if (op->op_flags & OPf_LIST)
136 (void)strcat(buf,"LIST,");
137 else
138 (void)strcat(buf,"SCALAR,");
139 }
140 else
141 (void)strcat(buf,"UNKNOWN,");
142 if (op->op_flags & OPf_KIDS)
143 (void)strcat(buf,"KIDS,");
144 if (op->op_flags & OPf_PARENS)
145 (void)strcat(buf,"PARENS,");
146 if (op->op_flags & OPf_STACKED)
147 (void)strcat(buf,"STACKED,");
a0d0e21e
LW
148 if (op->op_flags & OPf_REF)
149 (void)strcat(buf,"REF,");
150 if (op->op_flags & OPf_MOD)
151 (void)strcat(buf,"MOD,");
79072805
LW
152 if (op->op_flags & OPf_SPECIAL)
153 (void)strcat(buf,"SPECIAL,");
8d063cd8
LW
154 if (*buf)
155 buf[strlen(buf)-1] = '\0';
79072805
LW
156 dump("FLAGS = (%s)\n",buf);
157 }
158 if (op->op_private) {
159 *buf = '\0';
160 if (op->op_type == OP_AASSIGN) {
161 if (op->op_private & OPpASSIGN_COMMON)
162 (void)strcat(buf,"COMMON,");
8d063cd8 163 }
a0d0e21e
LW
164 else if (op->op_type == OP_SASSIGN) {
165 if (op->op_private & OPpASSIGN_BACKWARDS)
166 (void)strcat(buf,"BACKWARDS,");
167 }
79072805
LW
168 else if (op->op_type == OP_TRANS) {
169 if (op->op_private & OPpTRANS_SQUASH)
170 (void)strcat(buf,"SQUASH,");
171 if (op->op_private & OPpTRANS_DELETE)
172 (void)strcat(buf,"DELETE,");
173 if (op->op_private & OPpTRANS_COMPLEMENT)
174 (void)strcat(buf,"COMPLEMENT,");
8d063cd8 175 }
79072805
LW
176 else if (op->op_type == OP_REPEAT) {
177 if (op->op_private & OPpREPEAT_DOLIST)
178 (void)strcat(buf,"DOLIST,");
8d063cd8 179 }
a0d0e21e 180 else if (op->op_type == OP_ENTERSUB ||
85e6fe83
LW
181 op->op_type == OP_RV2SV ||
182 op->op_type == OP_RV2AV ||
183 op->op_type == OP_RV2HV ||
184 op->op_type == OP_RV2GV ||
185 op->op_type == OP_AELEM ||
186 op->op_type == OP_HELEM )
187 {
c07a80fd
PP
188 if (op->op_private & OPpENTERSUB_AMPER)
189 (void)strcat(buf,"AMPER,");
190 if (op->op_private & OPpENTERSUB_DB)
79072805 191 (void)strcat(buf,"DB,");
5f05dabc
PP
192 switch (op->op_private & OPpDEREF) {
193 case OPpDEREF_SV:
194 (void)strcat(buf, "SV,");
195 break;
196 case OPpDEREF_AV:
197 (void)strcat(buf, "AV,");
198 break;
199 case OPpDEREF_HV:
200 (void)strcat(buf, "HV,");
201 break;
202 }
85e6fe83
LW
203 if (op->op_private & HINT_STRICT_REFS)
204 (void)strcat(buf,"STRICT_REFS,");
8d063cd8 205 }
79072805
LW
206 else if (op->op_type == OP_CONST) {
207 if (op->op_private & OPpCONST_BARE)
208 (void)strcat(buf,"BARE,");
209 }
210 else if (op->op_type == OP_FLIP) {
211 if (op->op_private & OPpFLIP_LINENUM)
212 (void)strcat(buf,"LINENUM,");
213 }
214 else if (op->op_type == OP_FLOP) {
215 if (op->op_private & OPpFLIP_LINENUM)
216 (void)strcat(buf,"LINENUM,");
217 }
a0d0e21e
LW
218 if (op->op_flags & OPf_MOD && op->op_private & OPpLVAL_INTRO)
219 (void)strcat(buf,"INTRO,");
79072805
LW
220 if (*buf) {
221 buf[strlen(buf)-1] = '\0';
222 dump("PRIVATE = (%s)\n",buf);
8d063cd8 223 }
8d063cd8 224 }
8d063cd8 225
79072805 226 switch (op->op_type) {
93a17b20 227 case OP_GVSV:
79072805
LW
228 case OP_GV:
229 if (cGVOP->op_gv) {
8990e307 230 ENTER;
79072805 231 tmpsv = NEWSV(0,0);
8990e307 232 SAVEFREESV(tmpsv);
f6aff53a 233 gv_fullname3(tmpsv, cGVOP->op_gv, Nullch);
463ee0b2 234 dump("GV = %s\n", SvPV(tmpsv, na));
8990e307 235 LEAVE;
378cc40b 236 }
79072805
LW
237 else
238 dump("GV = NULL\n");
239 break;
240 case OP_CONST:
241 dump("SV = %s\n", SvPEEK(cSVOP->op_sv));
242 break;
93a17b20
LW
243 case OP_NEXTSTATE:
244 case OP_DBSTATE:
79072805
LW
245 if (cCOP->cop_line)
246 dump("LINE = %d\n",cCOP->cop_line);
247 if (cCOP->cop_label)
248 dump("LABEL = \"%s\"\n",cCOP->cop_label);
249 break;
250 case OP_ENTERLOOP:
251 dump("REDO ===> ");
93a17b20 252 if (cLOOP->op_redoop)
760ac839 253 PerlIO_printf(Perl_debug_log, "%d\n", cLOOP->op_redoop->op_seq);
79072805 254 else
760ac839 255 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805 256 dump("NEXT ===> ");
93a17b20 257 if (cLOOP->op_nextop)
760ac839 258 PerlIO_printf(Perl_debug_log, "%d\n", cLOOP->op_nextop->op_seq);
79072805 259 else
760ac839 260 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805 261 dump("LAST ===> ");
93a17b20 262 if (cLOOP->op_lastop)
760ac839 263 PerlIO_printf(Perl_debug_log, "%d\n", cLOOP->op_lastop->op_seq);
79072805 264 else
760ac839 265 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805
LW
266 break;
267 case OP_COND_EXPR:
268 dump("TRUE ===> ");
93a17b20 269 if (cCONDOP->op_true)
760ac839 270 PerlIO_printf(Perl_debug_log, "%d\n", cCONDOP->op_true->op_seq);
79072805 271 else
760ac839 272 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805 273 dump("FALSE ===> ");
93a17b20 274 if (cCONDOP->op_false)
760ac839 275 PerlIO_printf(Perl_debug_log, "%d\n", cCONDOP->op_false->op_seq);
79072805 276 else
760ac839 277 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805 278 break;
a0d0e21e 279 case OP_MAPWHILE:
79072805
LW
280 case OP_GREPWHILE:
281 case OP_OR:
282 case OP_AND:
79072805 283 dump("OTHER ===> ");
93a17b20 284 if (cLOGOP->op_other)
760ac839 285 PerlIO_printf(Perl_debug_log, "%d\n", cLOGOP->op_other->op_seq);
79072805 286 else
760ac839 287 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805
LW
288 break;
289 case OP_PUSHRE:
290 case OP_MATCH:
291 case OP_SUBST:
463ee0b2 292 dump_pm((PMOP*)op);
79072805 293 break;
a0d0e21e
LW
294 default:
295 break;
79072805
LW
296 }
297 if (op->op_flags & OPf_KIDS) {
298 OP *kid;
299 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling)
300 dump_op(kid);
8d063cd8
LW
301 }
302 dumplvl--;
303 dump("}\n");
304}
305
8adcabd8 306void
79072805
LW
307dump_gv(gv)
308register GV *gv;
378cc40b 309{
79072805 310 SV *sv;
378cc40b 311
79072805 312 if (!gv) {
760ac839 313 PerlIO_printf(Perl_debug_log, "{}\n");
378cc40b
LW
314 return;
315 }
8990e307 316 sv = sv_newmortal();
8d063cd8 317 dumplvl++;
760ac839 318 PerlIO_printf(Perl_debug_log, "{\n");
f6aff53a 319 gv_fullname3(sv, gv, Nullch);
463ee0b2 320 dump("GV_NAME = %s", SvPVX(sv));
79072805 321 if (gv != GvEGV(gv)) {
f6aff53a 322 gv_efullname3(sv, GvEGV(gv), Nullch);
463ee0b2 323 dump("-> %s", SvPVX(sv));
8adcabd8
LW
324 }
325 dump("\n");
8d063cd8
LW
326 dumplvl--;
327 dump("}\n");
328}
329
8adcabd8 330void
79072805
LW
331dump_pm(pm)
332register PMOP *pm;
8d063cd8
LW
333{
334 char ch;
335
79072805
LW
336 if (!pm) {
337 dump("{}\n");
378cc40b
LW
338 return;
339 }
79072805 340 dump("{\n");
8d063cd8 341 dumplvl++;
79072805
LW
342 if (pm->op_pmflags & PMf_ONCE)
343 ch = '?';
344 else
345 ch = '/';
346 if (pm->op_pmregexp)
347 dump("PMf_PRE %c%s%c\n",ch,pm->op_pmregexp->precomp,ch);
348 if (pm->op_type != OP_PUSHRE && pm->op_pmreplroot) {
349 dump("PMf_REPL = ");
350 dump_op(pm->op_pmreplroot);
8d063cd8 351 }
79072805
LW
352 if (pm->op_pmshort) {
353 dump("PMf_SHORT = %s\n",SvPEEK(pm->op_pmshort));
8d063cd8 354 }
79072805
LW
355 if (pm->op_pmflags) {
356 *buf = '\0';
357 if (pm->op_pmflags & PMf_USED)
358 (void)strcat(buf,"USED,");
359 if (pm->op_pmflags & PMf_ONCE)
360 (void)strcat(buf,"ONCE,");
361 if (pm->op_pmflags & PMf_SCANFIRST)
362 (void)strcat(buf,"SCANFIRST,");
363 if (pm->op_pmflags & PMf_ALL)
364 (void)strcat(buf,"ALL,");
365 if (pm->op_pmflags & PMf_SKIPWHITE)
366 (void)strcat(buf,"SKIPWHITE,");
79072805
LW
367 if (pm->op_pmflags & PMf_CONST)
368 (void)strcat(buf,"CONST,");
369 if (pm->op_pmflags & PMf_KEEP)
370 (void)strcat(buf,"KEEP,");
371 if (pm->op_pmflags & PMf_GLOBAL)
372 (void)strcat(buf,"GLOBAL,");
373 if (pm->op_pmflags & PMf_RUNTIME)
374 (void)strcat(buf,"RUNTIME,");
375 if (pm->op_pmflags & PMf_EVAL)
376 (void)strcat(buf,"EVAL,");
377 if (*buf)
378 buf[strlen(buf)-1] = '\0';
379 dump("PMFLAGS = (%s)\n",buf);
378cc40b 380 }
79072805 381
8d063cd8
LW
382 dumplvl--;
383 dump("}\n");
384}
385
4db58590
PP
386
387#if !defined(I_STDARG) && !defined(I_VARARGS)
378cc40b 388/* VARARGS1 */
8adcabd8 389static void dump(arg1,arg2,arg3,arg4,arg5)
378cc40b
LW
390char *arg1;
391long arg2, arg3, arg4, arg5;
8d063cd8 392{
79072805 393 I32 i;
8d063cd8
LW
394
395 for (i = dumplvl*4; i; i--)
760ac839
LW
396 (void)PerlIO_putc(Perl_debug_log,' ');
397 PerlIO_printf(Perl_debug_log, arg1, arg2, arg3, arg4, arg5);
4db58590
PP
398}
399
400#else
401
402#ifdef I_STDARG
403static void
404dump(char *pat,...)
405#else
406/*VARARGS0*/
407static void
408dump(pat,va_alist)
409 char *pat;
410 va_dcl
411#endif
412{
413 I32 i;
414 va_list args;
4db58590
PP
415
416#ifdef I_STDARG
417 va_start(args, pat);
418#else
419 va_start(args);
420#endif
421 for (i = dumplvl*4; i; i--)
760ac839
LW
422 (void)PerlIO_putc(Perl_debug_log,' ');
423 PerlIO_vprintf(Perl_debug_log,pat,args);
4db58590 424 va_end(args);
8d063cd8
LW
425}
426#endif
4db58590
PP
427
428#endif