This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
ensure 'make regen_headers' even without perl installed
[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
76e3520e 18#ifndef PERL_OBJECT
4db58590 19static void dump(char *pat, ...);
76e3520e 20#endif /* PERL_OBJECT */
8adcabd8
LW
21
22void
8ac85365 23dump_all(void)
79072805 24{
35ff7856 25#ifdef DEBUGGING
11343788 26 dTHR;
760ac839 27 PerlIO_setlinebuf(Perl_debug_log);
3280af22
NIS
28 if (PL_main_root)
29 dump_op(PL_main_root);
30 dump_packsubs(PL_defstash);
17c3b450 31#endif /* DEBUGGING */
463ee0b2
LW
32}
33
34void
8ac85365 35dump_packsubs(HV *stash)
463ee0b2 36{
35ff7856 37#ifdef DEBUGGING
11343788 38 dTHR;
a0d0e21e 39 I32 i;
463ee0b2
LW
40 HE *entry;
41
8990e307
LW
42 if (!HvARRAY(stash))
43 return;
a0d0e21e 44 for (i = 0; i <= (I32) HvMAX(stash); i++) {
4db58590
PP
45 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
46 GV *gv = (GV*)HeVAL(entry);
463ee0b2 47 HV *hv;
e29cdcb3
GS
48 if (SvTYPE(gv) != SVt_PVGV || !GvGP(gv))
49 continue;
8ebc5c01 50 if (GvCVu(gv))
463ee0b2 51 dump_sub(gv);
85e6fe83
LW
52 if (GvFORM(gv))
53 dump_form(gv);
4db58590 54 if (HeKEY(entry)[HeKLEN(entry)-1] == ':' &&
3280af22 55 (hv = GvHV(gv)) && HvNAME(hv) && hv != PL_defstash)
463ee0b2
LW
56 dump_packsubs(hv); /* nested package */
57 }
79072805 58 }
17c3b450 59#endif /* DEBUGGING */
79072805
LW
60}
61
62void
8ac85365 63dump_sub(GV *gv)
a687059c 64{
35ff7856 65#ifdef DEBUGGING
8990e307 66 SV *sv = sv_newmortal();
85e6fe83 67
f6aff53a 68 gv_fullname3(sv, gv, Nullch);
85e6fe83 69 dump("\nSUB %s = ", SvPVX(sv));
a0d0e21e 70 if (CvXSUB(GvCV(gv)))
85e6fe83 71 dump("(xsub 0x%x %d)\n",
a0d0e21e
LW
72 (long)CvXSUB(GvCV(gv)),
73 CvXSUBANY(GvCV(gv)).any_i32);
85e6fe83
LW
74 else if (CvROOT(GvCV(gv)))
75 dump_op(CvROOT(GvCV(gv)));
76 else
77 dump("<undef>\n");
17c3b450 78#endif /* DEBUGGING */
85e6fe83
LW
79}
80
81void
8ac85365 82dump_form(GV *gv)
85e6fe83 83{
35ff7856 84#ifdef DEBUGGING
85e6fe83
LW
85 SV *sv = sv_newmortal();
86
f6aff53a 87 gv_fullname3(sv, gv, Nullch);
85e6fe83
LW
88 dump("\nFORMAT %s = ", SvPVX(sv));
89 if (CvROOT(GvFORM(gv)))
90 dump_op(CvROOT(GvFORM(gv)));
91 else
92 dump("<undef>\n");
17c3b450 93#endif /* DEBUGGING */
a687059c
LW
94}
95
8adcabd8 96void
8ac85365 97dump_eval(void)
8d063cd8 98{
35ff7856 99#ifdef DEBUGGING
3280af22 100 dump_op(PL_eval_root);
17c3b450 101#endif /* DEBUGGING */
79072805
LW
102}
103
104void
8ac85365 105dump_op(OP *o)
79072805 106{
35ff7856 107#ifdef DEBUGGING
79072805 108 dump("{\n");
11343788 109 if (o->op_seq)
5dc0d613 110 PerlIO_printf(Perl_debug_log, "%-4d", o->op_seq);
93a17b20 111 else
760ac839 112 PerlIO_printf(Perl_debug_log, " ");
22c35a8c 113 dump("TYPE = %s ===> ", PL_op_name[o->op_type]);
11343788
MB
114 if (o->op_next) {
115 if (o->op_seq)
5dc0d613 116 PerlIO_printf(Perl_debug_log, "%d\n", o->op_next->op_seq);
93a17b20 117 else
5dc0d613 118 PerlIO_printf(Perl_debug_log, "(%d)\n", o->op_next->op_seq);
93a17b20 119 }
79072805 120 else
760ac839 121 PerlIO_printf(Perl_debug_log, "DONE\n");
3280af22 122 PL_dumplvl++;
11343788
MB
123 if (o->op_targ) {
124 if (o->op_type == OP_NULL)
22c35a8c 125 dump(" (was %s)\n", PL_op_name[o->op_targ]);
8990e307 126 else
11343788 127 dump("TARG = %d\n", o->op_targ);
8990e307 128 }
748a9306 129#ifdef DUMPADDR
11343788 130 dump("ADDR = 0x%lx => 0x%lx\n",o, o->op_next);
79072805 131#endif
11343788 132 if (o->op_flags) {
46fc3d4c 133 SV *tmpsv = newSVpv("", 0);
5dc0d613 134 switch (o->op_flags & OPf_WANT) {
54310121 135 case OPf_WANT_VOID:
46fc3d4c 136 sv_catpv(tmpsv, ",VOID");
54310121
PP
137 break;
138 case OPf_WANT_SCALAR:
46fc3d4c 139 sv_catpv(tmpsv, ",SCALAR");
54310121
PP
140 break;
141 case OPf_WANT_LIST:
46fc3d4c 142 sv_catpv(tmpsv, ",LIST");
54310121
PP
143 break;
144 default:
46fc3d4c 145 sv_catpv(tmpsv, ",UNKNOWN");
54310121
PP
146 break;
147 }
11343788 148 if (o->op_flags & OPf_KIDS)
46fc3d4c 149 sv_catpv(tmpsv, ",KIDS");
11343788 150 if (o->op_flags & OPf_PARENS)
46fc3d4c 151 sv_catpv(tmpsv, ",PARENS");
11343788 152 if (o->op_flags & OPf_STACKED)
46fc3d4c 153 sv_catpv(tmpsv, ",STACKED");
11343788 154 if (o->op_flags & OPf_REF)
46fc3d4c 155 sv_catpv(tmpsv, ",REF");
11343788 156 if (o->op_flags & OPf_MOD)
46fc3d4c 157 sv_catpv(tmpsv, ",MOD");
11343788 158 if (o->op_flags & OPf_SPECIAL)
46fc3d4c
PP
159 sv_catpv(tmpsv, ",SPECIAL");
160 dump("FLAGS = (%s)\n", SvCUR(tmpsv) ? SvPVX(tmpsv) + 1 : "");
161 SvREFCNT_dec(tmpsv);
79072805 162 }
11343788 163 if (o->op_private) {
46fc3d4c 164 SV *tmpsv = newSVpv("", 0);
11343788
MB
165 if (o->op_type == OP_AASSIGN) {
166 if (o->op_private & OPpASSIGN_COMMON)
46fc3d4c 167 sv_catpv(tmpsv, ",COMMON");
8d063cd8 168 }
11343788
MB
169 else if (o->op_type == OP_SASSIGN) {
170 if (o->op_private & OPpASSIGN_BACKWARDS)
46fc3d4c 171 sv_catpv(tmpsv, ",BACKWARDS");
a0d0e21e 172 }
11343788
MB
173 else if (o->op_type == OP_TRANS) {
174 if (o->op_private & OPpTRANS_SQUASH)
46fc3d4c 175 sv_catpv(tmpsv, ",SQUASH");
11343788 176 if (o->op_private & OPpTRANS_DELETE)
46fc3d4c 177 sv_catpv(tmpsv, ",DELETE");
11343788 178 if (o->op_private & OPpTRANS_COMPLEMENT)
46fc3d4c 179 sv_catpv(tmpsv, ",COMPLEMENT");
8d063cd8 180 }
11343788
MB
181 else if (o->op_type == OP_REPEAT) {
182 if (o->op_private & OPpREPEAT_DOLIST)
46fc3d4c 183 sv_catpv(tmpsv, ",DOLIST");
8d063cd8 184 }
11343788
MB
185 else if (o->op_type == OP_ENTERSUB ||
186 o->op_type == OP_RV2SV ||
187 o->op_type == OP_RV2AV ||
188 o->op_type == OP_RV2HV ||
189 o->op_type == OP_RV2GV ||
190 o->op_type == OP_AELEM ||
191 o->op_type == OP_HELEM )
85e6fe83 192 {
5dc0d613
MB
193 if (o->op_type == OP_ENTERSUB) {
194 if (o->op_private & OPpENTERSUB_AMPER)
46fc3d4c 195 sv_catpv(tmpsv, ",AMPER");
5dc0d613 196 if (o->op_private & OPpENTERSUB_DB)
46fc3d4c 197 sv_catpv(tmpsv, ",DB");
68dc0745 198 }
5dc0d613 199 switch (o->op_private & OPpDEREF) {
5f05dabc 200 case OPpDEREF_SV:
46fc3d4c 201 sv_catpv(tmpsv, ",SV");
5f05dabc
PP
202 break;
203 case OPpDEREF_AV:
46fc3d4c 204 sv_catpv(tmpsv, ",AV");
5f05dabc
PP
205 break;
206 case OPpDEREF_HV:
46fc3d4c 207 sv_catpv(tmpsv, ",HV");
5f05dabc
PP
208 break;
209 }
5dc0d613
MB
210 if (o->op_type == OP_AELEM || o->op_type == OP_HELEM) {
211 if (o->op_private & OPpLVAL_DEFER)
46fc3d4c 212 sv_catpv(tmpsv, ",LVAL_DEFER");
68dc0745
PP
213 }
214 else {
5dc0d613 215 if (o->op_private & HINT_STRICT_REFS)
46fc3d4c 216 sv_catpv(tmpsv, ",STRICT_REFS");
68dc0745 217 }
8d063cd8 218 }
11343788
MB
219 else if (o->op_type == OP_CONST) {
220 if (o->op_private & OPpCONST_BARE)
46fc3d4c 221 sv_catpv(tmpsv, ",BARE");
79072805 222 }
11343788
MB
223 else if (o->op_type == OP_FLIP) {
224 if (o->op_private & OPpFLIP_LINENUM)
46fc3d4c 225 sv_catpv(tmpsv, ",LINENUM");
79072805 226 }
11343788
MB
227 else if (o->op_type == OP_FLOP) {
228 if (o->op_private & OPpFLIP_LINENUM)
46fc3d4c 229 sv_catpv(tmpsv, ",LINENUM");
79072805 230 }
11343788 231 if (o->op_flags & OPf_MOD && o->op_private & OPpLVAL_INTRO)
46fc3d4c
PP
232 sv_catpv(tmpsv, ",INTRO");
233 if (SvCUR(tmpsv))
234 dump("PRIVATE = (%s)\n", SvPVX(tmpsv) + 1);
235 SvREFCNT_dec(tmpsv);
8d063cd8 236 }
8d063cd8 237
11343788 238 switch (o->op_type) {
93a17b20 239 case OP_GVSV:
79072805 240 case OP_GV:
11343788 241 if (cGVOPo->op_gv) {
46fc3d4c 242 SV *tmpsv = NEWSV(0,0);
8990e307 243 ENTER;
8990e307 244 SAVEFREESV(tmpsv);
5dc0d613 245 gv_fullname3(tmpsv, cGVOPo->op_gv, Nullch);
3280af22 246 dump("GV = %s\n", SvPV(tmpsv, PL_na));
8990e307 247 LEAVE;
378cc40b 248 }
79072805
LW
249 else
250 dump("GV = NULL\n");
251 break;
252 case OP_CONST:
11343788 253 dump("SV = %s\n", SvPEEK(cSVOPo->op_sv));
79072805 254 break;
93a17b20
LW
255 case OP_NEXTSTATE:
256 case OP_DBSTATE:
11343788
MB
257 if (cCOPo->cop_line)
258 dump("LINE = %d\n",cCOPo->cop_line);
259 if (cCOPo->cop_label)
260 dump("LABEL = \"%s\"\n",cCOPo->cop_label);
79072805
LW
261 break;
262 case OP_ENTERLOOP:
263 dump("REDO ===> ");
11343788 264 if (cLOOPo->op_redoop)
5dc0d613 265 PerlIO_printf(Perl_debug_log, "%d\n", cLOOPo->op_redoop->op_seq);
79072805 266 else
760ac839 267 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805 268 dump("NEXT ===> ");
11343788 269 if (cLOOPo->op_nextop)
5dc0d613 270 PerlIO_printf(Perl_debug_log, "%d\n", cLOOPo->op_nextop->op_seq);
79072805 271 else
760ac839 272 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805 273 dump("LAST ===> ");
11343788 274 if (cLOOPo->op_lastop)
5dc0d613 275 PerlIO_printf(Perl_debug_log, "%d\n", cLOOPo->op_lastop->op_seq);
79072805 276 else
760ac839 277 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805
LW
278 break;
279 case OP_COND_EXPR:
280 dump("TRUE ===> ");
11343788 281 if (cCONDOPo->op_true)
5dc0d613 282 PerlIO_printf(Perl_debug_log, "%d\n", cCONDOPo->op_true->op_seq);
79072805 283 else
760ac839 284 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805 285 dump("FALSE ===> ");
11343788 286 if (cCONDOPo->op_false)
5dc0d613 287 PerlIO_printf(Perl_debug_log, "%d\n", cCONDOPo->op_false->op_seq);
79072805 288 else
760ac839 289 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805 290 break;
a0d0e21e 291 case OP_MAPWHILE:
79072805
LW
292 case OP_GREPWHILE:
293 case OP_OR:
294 case OP_AND:
79072805 295 dump("OTHER ===> ");
11343788 296 if (cLOGOPo->op_other)
5dc0d613 297 PerlIO_printf(Perl_debug_log, "%d\n", cLOGOPo->op_other->op_seq);
79072805 298 else
760ac839 299 PerlIO_printf(Perl_debug_log, "DONE\n");
79072805
LW
300 break;
301 case OP_PUSHRE:
302 case OP_MATCH:
8782bef2 303 case OP_QR:
79072805 304 case OP_SUBST:
11343788 305 dump_pm(cPMOPo);
79072805 306 break;
a0d0e21e
LW
307 default:
308 break;
79072805 309 }
11343788 310 if (o->op_flags & OPf_KIDS) {
79072805 311 OP *kid;
11343788 312 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
79072805 313 dump_op(kid);
8d063cd8 314 }
3280af22 315 PL_dumplvl--;
8d063cd8 316 dump("}\n");
17c3b450 317#endif /* DEBUGGING */
8d063cd8
LW
318}
319
8adcabd8 320void
8ac85365 321dump_gv(GV *gv)
378cc40b 322{
35ff7856 323#ifdef DEBUGGING
79072805 324 SV *sv;
378cc40b 325
79072805 326 if (!gv) {
760ac839 327 PerlIO_printf(Perl_debug_log, "{}\n");
378cc40b
LW
328 return;
329 }
8990e307 330 sv = sv_newmortal();
3280af22 331 PL_dumplvl++;
760ac839 332 PerlIO_printf(Perl_debug_log, "{\n");
f6aff53a 333 gv_fullname3(sv, gv, Nullch);
463ee0b2 334 dump("GV_NAME = %s", SvPVX(sv));
79072805 335 if (gv != GvEGV(gv)) {
f6aff53a 336 gv_efullname3(sv, GvEGV(gv), Nullch);
463ee0b2 337 dump("-> %s", SvPVX(sv));
8adcabd8
LW
338 }
339 dump("\n");
3280af22 340 PL_dumplvl--;
8d063cd8 341 dump("}\n");
17c3b450 342#endif /* DEBUGGING */
8d063cd8
LW
343}
344
8adcabd8 345void
8ac85365 346dump_pm(PMOP *pm)
8d063cd8 347{
35ff7856 348#ifdef DEBUGGING
8d063cd8
LW
349 char ch;
350
79072805
LW
351 if (!pm) {
352 dump("{}\n");
378cc40b
LW
353 return;
354 }
79072805 355 dump("{\n");
3280af22 356 PL_dumplvl++;
79072805
LW
357 if (pm->op_pmflags & PMf_ONCE)
358 ch = '?';
359 else
360 ch = '/';
361 if (pm->op_pmregexp)
c90c0ff4
PP
362 dump("PMf_PRE %c%s%c%s\n",
363 ch, pm->op_pmregexp->precomp, ch,
364 (pm->op_private & OPpRUNTIME) ? " (RUNTIME)" : "");
365 else
366 dump("PMf_PRE (RUNTIME)\n");
79072805
LW
367 if (pm->op_type != OP_PUSHRE && pm->op_pmreplroot) {
368 dump("PMf_REPL = ");
369 dump_op(pm->op_pmreplroot);
8d063cd8 370 }
c277df42 371 if (pm->op_pmflags || (pm->op_pmregexp && pm->op_pmregexp->check_substr)) {
46fc3d4c 372 SV *tmpsv = newSVpv("", 0);
48c036b1 373 if (pm->op_pmdynflags & PMdf_USED)
46fc3d4c 374 sv_catpv(tmpsv, ",USED");
b3eb6a9b
GS
375 if (pm->op_pmdynflags & PMdf_TAINTED)
376 sv_catpv(tmpsv, ",TAINTED");
79072805 377 if (pm->op_pmflags & PMf_ONCE)
46fc3d4c 378 sv_catpv(tmpsv, ",ONCE");
c277df42
IZ
379 if (pm->op_pmregexp && pm->op_pmregexp->check_substr
380 && !(pm->op_pmregexp->reganch & ROPT_NOSCAN))
46fc3d4c 381 sv_catpv(tmpsv, ",SCANFIRST");
c277df42
IZ
382 if (pm->op_pmregexp && pm->op_pmregexp->check_substr
383 && pm->op_pmregexp->reganch & ROPT_CHECK_ALL)
46fc3d4c 384 sv_catpv(tmpsv, ",ALL");
79072805 385 if (pm->op_pmflags & PMf_SKIPWHITE)
46fc3d4c 386 sv_catpv(tmpsv, ",SKIPWHITE");
79072805 387 if (pm->op_pmflags & PMf_CONST)
46fc3d4c 388 sv_catpv(tmpsv, ",CONST");
79072805 389 if (pm->op_pmflags & PMf_KEEP)
46fc3d4c 390 sv_catpv(tmpsv, ",KEEP");
79072805 391 if (pm->op_pmflags & PMf_GLOBAL)
46fc3d4c 392 sv_catpv(tmpsv, ",GLOBAL");
c90c0ff4
PP
393 if (pm->op_pmflags & PMf_CONTINUE)
394 sv_catpv(tmpsv, ",CONTINUE");
b3eb6a9b
GS
395 if (pm->op_pmflags & PMf_RETAINT)
396 sv_catpv(tmpsv, ",RETAINT");
79072805 397 if (pm->op_pmflags & PMf_EVAL)
46fc3d4c
PP
398 sv_catpv(tmpsv, ",EVAL");
399 dump("PMFLAGS = (%s)\n", SvCUR(tmpsv) ? SvPVX(tmpsv) + 1 : "");
400 SvREFCNT_dec(tmpsv);
378cc40b 401 }
79072805 402
3280af22 403 PL_dumplvl--;
8d063cd8 404 dump("}\n");
17c3b450 405#endif /* DEBUGGING */
8d063cd8
LW
406}
407
4db58590 408
76e3520e 409STATIC void
4db58590 410dump(char *pat,...)
4db58590 411{
35ff7856 412#ifdef DEBUGGING
4db58590
PP
413 I32 i;
414 va_list args;
4db58590 415
4db58590 416 va_start(args, pat);
3280af22 417 for (i = PL_dumplvl*4; i; i--)
760ac839
LW
418 (void)PerlIO_putc(Perl_debug_log,' ');
419 PerlIO_vprintf(Perl_debug_log,pat,args);
4db58590 420 va_end(args);
17c3b450 421#endif /* DEBUGGING */
8d063cd8 422}