This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pod/perlipc.pod patch
[perl5.git] / dump.c
CommitLineData
a0d0e21e 1/* dump.c
a687059c 2 *
a0d0e21e 3 * Copyright (c) 1991-1994, 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
LW
52 HV *hv;
53 if (GvCV(gv))
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,");
85e6fe83
LW
192 if (op->op_private & OPpDEREF_AV)
193 (void)strcat(buf,"AV,");
194 if (op->op_private & OPpDEREF_HV)
195 (void)strcat(buf,"HV,");
196 if (op->op_private & HINT_STRICT_REFS)
197 (void)strcat(buf,"STRICT_REFS,");
8d063cd8 198 }
79072805
LW
199 else if (op->op_type == OP_CONST) {
200 if (op->op_private & OPpCONST_BARE)
201 (void)strcat(buf,"BARE,");
202 }
203 else if (op->op_type == OP_FLIP) {
204 if (op->op_private & OPpFLIP_LINENUM)
205 (void)strcat(buf,"LINENUM,");
206 }
207 else if (op->op_type == OP_FLOP) {
208 if (op->op_private & OPpFLIP_LINENUM)
209 (void)strcat(buf,"LINENUM,");
210 }
a0d0e21e
LW
211 if (op->op_flags & OPf_MOD && op->op_private & OPpLVAL_INTRO)
212 (void)strcat(buf,"INTRO,");
79072805
LW
213 if (*buf) {
214 buf[strlen(buf)-1] = '\0';
215 dump("PRIVATE = (%s)\n",buf);
8d063cd8 216 }
8d063cd8 217 }
8d063cd8 218
79072805 219 switch (op->op_type) {
93a17b20 220 case OP_GVSV:
79072805
LW
221 case OP_GV:
222 if (cGVOP->op_gv) {
8990e307 223 ENTER;
79072805 224 tmpsv = NEWSV(0,0);
8990e307 225 SAVEFREESV(tmpsv);
f6aff53a 226 gv_fullname3(tmpsv, cGVOP->op_gv, Nullch);
463ee0b2 227 dump("GV = %s\n", SvPV(tmpsv, na));
8990e307 228 LEAVE;
378cc40b 229 }
79072805
LW
230 else
231 dump("GV = NULL\n");
232 break;
233 case OP_CONST:
234 dump("SV = %s\n", SvPEEK(cSVOP->op_sv));
235 break;
93a17b20
LW
236 case OP_NEXTSTATE:
237 case OP_DBSTATE:
79072805
LW
238 if (cCOP->cop_line)
239 dump("LINE = %d\n",cCOP->cop_line);
240 if (cCOP->cop_label)
241 dump("LABEL = \"%s\"\n",cCOP->cop_label);
242 break;
243 case OP_ENTERLOOP:
244 dump("REDO ===> ");
93a17b20 245 if (cLOOP->op_redoop)
760ac839 246 PerlIO_printf(Perl_debug_log, "%d\n", cLOOP->op_redoop->op_seq);
79072805 247 else
760ac839 248 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805 249 dump("NEXT ===> ");
93a17b20 250 if (cLOOP->op_nextop)
760ac839 251 PerlIO_printf(Perl_debug_log, "%d\n", cLOOP->op_nextop->op_seq);
79072805 252 else
760ac839 253 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805 254 dump("LAST ===> ");
93a17b20 255 if (cLOOP->op_lastop)
760ac839 256 PerlIO_printf(Perl_debug_log, "%d\n", cLOOP->op_lastop->op_seq);
79072805 257 else
760ac839 258 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805
LW
259 break;
260 case OP_COND_EXPR:
261 dump("TRUE ===> ");
93a17b20 262 if (cCONDOP->op_true)
760ac839 263 PerlIO_printf(Perl_debug_log, "%d\n", cCONDOP->op_true->op_seq);
79072805 264 else
760ac839 265 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805 266 dump("FALSE ===> ");
93a17b20 267 if (cCONDOP->op_false)
760ac839 268 PerlIO_printf(Perl_debug_log, "%d\n", cCONDOP->op_false->op_seq);
79072805 269 else
760ac839 270 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805 271 break;
a0d0e21e 272 case OP_MAPWHILE:
79072805
LW
273 case OP_GREPWHILE:
274 case OP_OR:
275 case OP_AND:
79072805 276 dump("OTHER ===> ");
93a17b20 277 if (cLOGOP->op_other)
760ac839 278 PerlIO_printf(Perl_debug_log, "%d\n", cLOGOP->op_other->op_seq);
79072805 279 else
760ac839 280 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805
LW
281 break;
282 case OP_PUSHRE:
283 case OP_MATCH:
284 case OP_SUBST:
463ee0b2 285 dump_pm((PMOP*)op);
79072805 286 break;
a0d0e21e
LW
287 default:
288 break;
79072805
LW
289 }
290 if (op->op_flags & OPf_KIDS) {
291 OP *kid;
292 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling)
293 dump_op(kid);
8d063cd8
LW
294 }
295 dumplvl--;
296 dump("}\n");
297}
298
8adcabd8 299void
79072805
LW
300dump_gv(gv)
301register GV *gv;
378cc40b 302{
79072805 303 SV *sv;
378cc40b 304
79072805 305 if (!gv) {
760ac839 306 PerlIO_printf(Perl_debug_log, "{}\n");
378cc40b
LW
307 return;
308 }
8990e307 309 sv = sv_newmortal();
8d063cd8 310 dumplvl++;
760ac839 311 PerlIO_printf(Perl_debug_log, "{\n");
f6aff53a 312 gv_fullname3(sv, gv, Nullch);
463ee0b2 313 dump("GV_NAME = %s", SvPVX(sv));
79072805 314 if (gv != GvEGV(gv)) {
f6aff53a 315 gv_efullname3(sv, GvEGV(gv), Nullch);
463ee0b2 316 dump("-> %s", SvPVX(sv));
8adcabd8
LW
317 }
318 dump("\n");
8d063cd8
LW
319 dumplvl--;
320 dump("}\n");
321}
322
8adcabd8 323void
79072805
LW
324dump_pm(pm)
325register PMOP *pm;
8d063cd8
LW
326{
327 char ch;
328
79072805
LW
329 if (!pm) {
330 dump("{}\n");
378cc40b
LW
331 return;
332 }
79072805 333 dump("{\n");
8d063cd8 334 dumplvl++;
79072805
LW
335 if (pm->op_pmflags & PMf_ONCE)
336 ch = '?';
337 else
338 ch = '/';
339 if (pm->op_pmregexp)
340 dump("PMf_PRE %c%s%c\n",ch,pm->op_pmregexp->precomp,ch);
341 if (pm->op_type != OP_PUSHRE && pm->op_pmreplroot) {
342 dump("PMf_REPL = ");
343 dump_op(pm->op_pmreplroot);
8d063cd8 344 }
79072805
LW
345 if (pm->op_pmshort) {
346 dump("PMf_SHORT = %s\n",SvPEEK(pm->op_pmshort));
8d063cd8 347 }
79072805
LW
348 if (pm->op_pmflags) {
349 *buf = '\0';
350 if (pm->op_pmflags & PMf_USED)
351 (void)strcat(buf,"USED,");
352 if (pm->op_pmflags & PMf_ONCE)
353 (void)strcat(buf,"ONCE,");
354 if (pm->op_pmflags & PMf_SCANFIRST)
355 (void)strcat(buf,"SCANFIRST,");
356 if (pm->op_pmflags & PMf_ALL)
357 (void)strcat(buf,"ALL,");
358 if (pm->op_pmflags & PMf_SKIPWHITE)
359 (void)strcat(buf,"SKIPWHITE,");
79072805
LW
360 if (pm->op_pmflags & PMf_CONST)
361 (void)strcat(buf,"CONST,");
362 if (pm->op_pmflags & PMf_KEEP)
363 (void)strcat(buf,"KEEP,");
364 if (pm->op_pmflags & PMf_GLOBAL)
365 (void)strcat(buf,"GLOBAL,");
366 if (pm->op_pmflags & PMf_RUNTIME)
367 (void)strcat(buf,"RUNTIME,");
368 if (pm->op_pmflags & PMf_EVAL)
369 (void)strcat(buf,"EVAL,");
370 if (*buf)
371 buf[strlen(buf)-1] = '\0';
372 dump("PMFLAGS = (%s)\n",buf);
378cc40b 373 }
79072805 374
8d063cd8
LW
375 dumplvl--;
376 dump("}\n");
377}
378
4db58590
PP
379
380#if !defined(I_STDARG) && !defined(I_VARARGS)
378cc40b 381/* VARARGS1 */
8adcabd8 382static void dump(arg1,arg2,arg3,arg4,arg5)
378cc40b
LW
383char *arg1;
384long arg2, arg3, arg4, arg5;
8d063cd8 385{
79072805 386 I32 i;
8d063cd8
LW
387
388 for (i = dumplvl*4; i; i--)
760ac839
LW
389 (void)PerlIO_putc(Perl_debug_log,' ');
390 PerlIO_printf(Perl_debug_log, arg1, arg2, arg3, arg4, arg5);
4db58590
PP
391}
392
393#else
394
395#ifdef I_STDARG
396static void
397dump(char *pat,...)
398#else
399/*VARARGS0*/
400static void
401dump(pat,va_alist)
402 char *pat;
403 va_dcl
404#endif
405{
406 I32 i;
407 va_list args;
4db58590
PP
408
409#ifdef I_STDARG
410 va_start(args, pat);
411#else
412 va_start(args);
413#endif
414 for (i = dumplvl*4; i; i--)
760ac839
LW
415 (void)PerlIO_putc(Perl_debug_log,' ');
416 PerlIO_vprintf(Perl_debug_log,pat,args);
4db58590 417 va_end(args);
8d063cd8
LW
418}
419#endif
4db58590
PP
420
421#endif