This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Revert "Perl_do_sv_dump: alert when skipping elements"
[perl5.git] / dump.c
CommitLineData
a0d0e21e 1/* dump.c
a687059c 2 *
1129b882
NC
3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
a687059c 5 *
6e21c824
LW
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8d063cd8 8 *
a0d0e21e
LW
9 */
10
11/*
4ac71550
TC
12 * 'You have talked long in your sleep, Frodo,' said Gandalf gently, 'and
13 * it has not been hard for me to read your mind and memory.'
14 *
15 * [p.220 of _The Lord of the Rings_, II/i: "Many Meetings"]
8d063cd8
LW
16 */
17
166f8a29 18/* This file contains utility routines to dump the contents of SV and OP
61296642 19 * structures, as used by command-line options like -Dt and -Dx, and
166f8a29
DM
20 * by Devel::Peek.
21 *
22 * It also holds the debugging version of the runops function.
23 */
24
8d063cd8 25#include "EXTERN.h"
864dbfa3 26#define PERL_IN_DUMP_C
8d063cd8 27#include "perl.h"
f722798b 28#include "regcomp.h"
0bd48802
AL
29#include "proto.h"
30
8d063cd8 31
5357ca29
NC
32static const char* const svtypenames[SVt_LAST] = {
33 "NULL",
1cb9cd50 34 "BIND",
5357ca29 35 "IV",
b53eecb4 36 "NV",
5357ca29
NC
37 "PV",
38 "PVIV",
39 "PVNV",
40 "PVMG",
5c35adbb 41 "REGEXP",
5357ca29
NC
42 "PVGV",
43 "PVLV",
44 "PVAV",
45 "PVHV",
46 "PVCV",
47 "PVFM",
48 "PVIO"
49};
50
51
52static const char* const svshorttypenames[SVt_LAST] = {
53 "UNDEF",
1cb9cd50 54 "BIND",
5357ca29 55 "IV",
b53eecb4 56 "NV",
5357ca29
NC
57 "PV",
58 "PVIV",
59 "PVNV",
60 "PVMG",
5c35adbb 61 "REGEXP",
5357ca29
NC
62 "GV",
63 "PVLV",
64 "AV",
65 "HV",
66 "CV",
67 "FM",
68 "IO"
69};
70
a0c2f4dd
NC
71struct flag_to_name {
72 U32 flag;
73 const char *name;
74};
75
76static void
77S_append_flags(pTHX_ SV *sv, U32 flags, const struct flag_to_name *start,
78 const struct flag_to_name *const end)
79{
80 do {
81 if (flags & start->flag)
82 sv_catpv(sv, start->name);
83 } while (++start < end);
84}
85
86#define append_flags(sv, f, flags) \
cd431fde 87 S_append_flags(aTHX_ (sv), (f), (flags), C_ARRAY_END(flags))
a0c2f4dd
NC
88
89
27da23d5 90#define Sequence PL_op_sequence
2814eb74 91
3967c732 92void
864dbfa3 93Perl_dump_indent(pTHX_ I32 level, PerlIO *file, const char* pat, ...)
3967c732 94{
3967c732 95 va_list args;
7918f24d 96 PERL_ARGS_ASSERT_DUMP_INDENT;
3967c732 97 va_start(args, pat);
c5be433b 98 dump_vindent(level, file, pat, &args);
3967c732
JD
99 va_end(args);
100}
8adcabd8
LW
101
102void
c5be433b
GS
103Perl_dump_vindent(pTHX_ I32 level, PerlIO *file, const char* pat, va_list *args)
104{
97aff369 105 dVAR;
7918f24d 106 PERL_ARGS_ASSERT_DUMP_VINDENT;
c8db6e60 107 PerlIO_printf(file, "%*s", (int)(level*PL_dumpindent), "");
c5be433b
GS
108 PerlIO_vprintf(file, pat, *args);
109}
110
111void
864dbfa3 112Perl_dump_all(pTHX)
79072805 113{
712a6fe4 114 dump_all_perl(FALSE);
f0e3f042
CS
115}
116
117void
118Perl_dump_all_perl(pTHX_ bool justperl)
119{
120
97aff369 121 dVAR;
760ac839 122 PerlIO_setlinebuf(Perl_debug_log);
3280af22 123 if (PL_main_root)
3967c732 124 op_dump(PL_main_root);
f0e3f042 125 dump_packsubs_perl(PL_defstash, justperl);
463ee0b2
LW
126}
127
128void
e1ec3a88 129Perl_dump_packsubs(pTHX_ const HV *stash)
463ee0b2 130{
28eb953d 131 PERL_ARGS_ASSERT_DUMP_PACKSUBS;
f0e3f042
CS
132 dump_packsubs_perl(stash, FALSE);
133}
134
135void
136Perl_dump_packsubs_perl(pTHX_ const HV *stash, bool justperl)
137{
97aff369 138 dVAR;
a0d0e21e 139 I32 i;
463ee0b2 140
28eb953d 141 PERL_ARGS_ASSERT_DUMP_PACKSUBS_PERL;
7918f24d 142
8990e307
LW
143 if (!HvARRAY(stash))
144 return;
a0d0e21e 145 for (i = 0; i <= (I32) HvMAX(stash); i++) {
e1ec3a88 146 const HE *entry;
4db58590 147 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
159b6efe 148 const GV * const gv = (const GV *)HeVAL(entry);
e29cdcb3
GS
149 if (SvTYPE(gv) != SVt_PVGV || !GvGP(gv))
150 continue;
8ebc5c01 151 if (GvCVu(gv))
f0e3f042 152 dump_sub_perl(gv, justperl);
85e6fe83
LW
153 if (GvFORM(gv))
154 dump_form(gv);
61f9802b
AL
155 if (HeKEY(entry)[HeKLEN(entry)-1] == ':') {
156 const HV * const hv = GvHV(gv);
157 if (hv && (hv != PL_defstash))
f0e3f042 158 dump_packsubs_perl(hv, justperl); /* nested package */
61f9802b 159 }
463ee0b2 160 }
79072805
LW
161 }
162}
163
164void
e1ec3a88 165Perl_dump_sub(pTHX_ const GV *gv)
a687059c 166{
28eb953d 167 PERL_ARGS_ASSERT_DUMP_SUB;
f0e3f042
CS
168 dump_sub_perl(gv, FALSE);
169}
170
171void
172Perl_dump_sub_perl(pTHX_ const GV *gv, bool justperl)
173{
174 SV * sv;
85e6fe83 175
28eb953d 176 PERL_ARGS_ASSERT_DUMP_SUB_PERL;
7918f24d 177
f0e3f042
CS
178 if (justperl && (CvISXSUB(GvCV(gv)) || !CvROOT(GvCV(gv))))
179 return;
180
181 sv = sv_newmortal();
bd61b366 182 gv_fullname3(sv, gv, NULL);
b15aece3 183 Perl_dump_indent(aTHX_ 0, Perl_debug_log, "\nSUB %s = ", SvPVX_const(sv));
aed2304a 184 if (CvISXSUB(GvCV(gv)))
91f3b821
GS
185 Perl_dump_indent(aTHX_ 0, Perl_debug_log, "(xsub 0x%"UVxf" %d)\n",
186 PTR2UV(CvXSUB(GvCV(gv))),
894356b3 187 (int)CvXSUBANY(GvCV(gv)).any_i32);
85e6fe83 188 else if (CvROOT(GvCV(gv)))
3967c732 189 op_dump(CvROOT(GvCV(gv)));
85e6fe83 190 else
cea2e8a9 191 Perl_dump_indent(aTHX_ 0, Perl_debug_log, "<undef>\n");
85e6fe83
LW
192}
193
194void
e1ec3a88 195Perl_dump_form(pTHX_ const GV *gv)
85e6fe83 196{
b464bac0 197 SV * const sv = sv_newmortal();
85e6fe83 198
7918f24d
NC
199 PERL_ARGS_ASSERT_DUMP_FORM;
200
bd61b366 201 gv_fullname3(sv, gv, NULL);
b15aece3 202 Perl_dump_indent(aTHX_ 0, Perl_debug_log, "\nFORMAT %s = ", SvPVX_const(sv));
85e6fe83 203 if (CvROOT(GvFORM(gv)))
3967c732 204 op_dump(CvROOT(GvFORM(gv)));
85e6fe83 205 else
cea2e8a9 206 Perl_dump_indent(aTHX_ 0, Perl_debug_log, "<undef>\n");
a687059c
LW
207}
208
8adcabd8 209void
864dbfa3 210Perl_dump_eval(pTHX)
8d063cd8 211{
97aff369 212 dVAR;
3967c732
JD
213 op_dump(PL_eval_root);
214}
215
3df15adc
YO
216
217/*
87cea99e 218=for apidoc pv_escape
3df15adc
YO
219
220Escapes at most the first "count" chars of pv and puts the results into
ab3bbdeb 221dsv such that the size of the escaped string will not exceed "max" chars
3df15adc
YO
222and will not contain any incomplete escape sequences.
223
ab3bbdeb
YO
224If flags contains PERL_PV_ESCAPE_QUOTE then any double quotes in the string
225will also be escaped.
3df15adc
YO
226
227Normally the SV will be cleared before the escaped string is prepared,
ab3bbdeb
YO
228but when PERL_PV_ESCAPE_NOCLEAR is set this will not occur.
229
38a44b82 230If PERL_PV_ESCAPE_UNI is set then the input string is treated as Unicode,
ab3bbdeb 231if PERL_PV_ESCAPE_UNI_DETECT is set then the input string is scanned
38a44b82 232using C<is_utf8_string()> to determine if it is Unicode.
ab3bbdeb
YO
233
234If PERL_PV_ESCAPE_ALL is set then all input chars will be output
681f01c2
KW
235using C<\x01F1> style escapes, otherwise if PERL_PV_ESCAPE_NONASCII is set, only
236chars above 127 will be escaped using this style; otherwise, only chars above
237255 will be so escaped; other non printable chars will use octal or
238common escaped patterns like C<\n>. Otherwise, if PERL_PV_ESCAPE_NOBACKSLASH
239then all chars below 255 will be treated as printable and
ab3bbdeb
YO
240will be output as literals.
241
242If PERL_PV_ESCAPE_FIRSTCHAR is set then only the first char of the
c8536afa
KW
243string will be escaped, regardless of max. If the output is to be in hex,
244then it will be returned as a plain hex
245sequence. Thus the output will either be a single char,
246an octal escape sequence, a special escape like C<\n> or a hex value.
3df15adc 247
44a2ac75
YO
248If PERL_PV_ESCAPE_RE is set then the escape char used will be a '%' and
249not a '\\'. This is because regexes very often contain backslashed
250sequences, whereas '%' is not a particularly common character in patterns.
251
ab3bbdeb 252Returns a pointer to the escaped text as held by dsv.
3df15adc
YO
253
254=cut
255*/
ab3bbdeb 256#define PV_ESCAPE_OCTBUFSIZE 32
ddc5bc0f 257
3967c732 258char *
ddc5bc0f 259Perl_pv_escape( pTHX_ SV *dsv, char const * const str,
ab3bbdeb
YO
260 const STRLEN count, const STRLEN max,
261 STRLEN * const escaped, const U32 flags )
262{
61f9802b
AL
263 const char esc = (flags & PERL_PV_ESCAPE_RE) ? '%' : '\\';
264 const char dq = (flags & PERL_PV_ESCAPE_QUOTE) ? '"' : esc;
44a2ac75 265 char octbuf[PV_ESCAPE_OCTBUFSIZE] = "%123456789ABCDF";
ab3bbdeb
YO
266 STRLEN wrote = 0; /* chars written so far */
267 STRLEN chsize = 0; /* size of data to be written */
268 STRLEN readsize = 1; /* size of data just read */
38a44b82 269 bool isuni= flags & PERL_PV_ESCAPE_UNI ? 1 : 0; /* is this Unicode */
ddc5bc0f 270 const char *pv = str;
61f9802b 271 const char * const end = pv + count; /* end of string */
44a2ac75 272 octbuf[0] = esc;
ab3bbdeb 273
7918f24d
NC
274 PERL_ARGS_ASSERT_PV_ESCAPE;
275
9ed8b5e5 276 if (!(flags & PERL_PV_ESCAPE_NOCLEAR)) {
7fddd944 277 /* This won't alter the UTF-8 flag */
76f68e9b 278 sv_setpvs(dsv, "");
7fddd944 279 }
ab3bbdeb 280
ddc5bc0f 281 if ((flags & PERL_PV_ESCAPE_UNI_DETECT) && is_utf8_string((U8*)pv, count))
ab3bbdeb
YO
282 isuni = 1;
283
284 for ( ; (pv < end && (!max || (wrote < max))) ; pv += readsize ) {
ddc5bc0f 285 const UV u= (isuni) ? utf8_to_uvchr((U8*)pv, &readsize) : (U8)*pv;
ab3bbdeb
YO
286 const U8 c = (U8)u & 0xFF;
287
681f01c2
KW
288 if ( ( u > 255 )
289 || (flags & PERL_PV_ESCAPE_ALL)
290 || (( u > 127 ) && (flags & PERL_PV_ESCAPE_NONASCII)))
291 {
ab3bbdeb
YO
292 if (flags & PERL_PV_ESCAPE_FIRSTCHAR)
293 chsize = my_snprintf( octbuf, PV_ESCAPE_OCTBUFSIZE,
294 "%"UVxf, u);
295 else
296 chsize = my_snprintf( octbuf, PV_ESCAPE_OCTBUFSIZE,
44a2ac75 297 "%cx{%"UVxf"}", esc, u);
ab3bbdeb
YO
298 } else if (flags & PERL_PV_ESCAPE_NOBACKSLASH) {
299 chsize = 1;
300 } else {
44a2ac75
YO
301 if ( (c == dq) || (c == esc) || !isPRINT(c) ) {
302 chsize = 2;
ab3bbdeb 303 switch (c) {
44a2ac75
YO
304
305 case '\\' : /* fallthrough */
306 case '%' : if ( c == esc ) {
307 octbuf[1] = esc;
308 } else {
309 chsize = 1;
310 }
311 break;
3df15adc
YO
312 case '\v' : octbuf[1] = 'v'; break;
313 case '\t' : octbuf[1] = 't'; break;
314 case '\r' : octbuf[1] = 'r'; break;
315 case '\n' : octbuf[1] = 'n'; break;
316 case '\f' : octbuf[1] = 'f'; break;
44a2ac75 317 case '"' :
ab3bbdeb 318 if ( dq == '"' )
3df15adc 319 octbuf[1] = '"';
ab3bbdeb
YO
320 else
321 chsize = 1;
44a2ac75 322 break;
3df15adc 323 default:
bbae360a 324 if ( (pv+readsize < end) && isDIGIT((U8)*(pv+readsize)) )
ab3bbdeb 325 chsize = my_snprintf( octbuf, PV_ESCAPE_OCTBUFSIZE,
44a2ac75
YO
326 "%c%03o", esc, c);
327 else
ab3bbdeb 328 chsize = my_snprintf( octbuf, PV_ESCAPE_OCTBUFSIZE,
44a2ac75 329 "%c%o", esc, c);
ab3bbdeb
YO
330 }
331 } else {
44a2ac75 332 chsize = 1;
ab3bbdeb 333 }
44a2ac75
YO
334 }
335 if ( max && (wrote + chsize > max) ) {
336 break;
ab3bbdeb 337 } else if (chsize > 1) {
44a2ac75
YO
338 sv_catpvn(dsv, octbuf, chsize);
339 wrote += chsize;
3df15adc 340 } else {
7fddd944
NC
341 /* If PERL_PV_ESCAPE_NOBACKSLASH is set then bytes in the range
342 128-255 can be appended raw to the dsv. If dsv happens to be
343 UTF-8 then we need catpvf to upgrade them for us.
344 Or add a new API call sv_catpvc(). Think about that name, and
345 how to keep it clear that it's unlike the s of catpvs, which is
346 really an array octets, not a string. */
347 Perl_sv_catpvf( aTHX_ dsv, "%c", c);
3df15adc
YO
348 wrote++;
349 }
ab3bbdeb
YO
350 if ( flags & PERL_PV_ESCAPE_FIRSTCHAR )
351 break;
3967c732 352 }
ab3bbdeb
YO
353 if (escaped != NULL)
354 *escaped= pv - str;
355 return SvPVX(dsv);
356}
357/*
87cea99e 358=for apidoc pv_pretty
ab3bbdeb
YO
359
360Converts a string into something presentable, handling escaping via
95b611b0 361pv_escape() and supporting quoting and ellipses.
ab3bbdeb
YO
362
363If the PERL_PV_PRETTY_QUOTE flag is set then the result will be
364double quoted with any double quotes in the string escaped. Otherwise
365if the PERL_PV_PRETTY_LTGT flag is set then the result be wrapped in
366angle brackets.
6cba11c8 367
95b611b0
RGS
368If the PERL_PV_PRETTY_ELLIPSES flag is set and not all characters in
369string were output then an ellipsis C<...> will be appended to the
ab3bbdeb 370string. Note that this happens AFTER it has been quoted.
6cba11c8 371
ab3bbdeb
YO
372If start_color is non-null then it will be inserted after the opening
373quote (if there is one) but before the escaped text. If end_color
374is non-null then it will be inserted after the escaped text but before
95b611b0 375any quotes or ellipses.
ab3bbdeb
YO
376
377Returns a pointer to the prettified text as held by dsv.
6cba11c8 378
ab3bbdeb
YO
379=cut
380*/
381
382char *
ddc5bc0f
YO
383Perl_pv_pretty( pTHX_ SV *dsv, char const * const str, const STRLEN count,
384 const STRLEN max, char const * const start_color, char const * const end_color,
ab3bbdeb
YO
385 const U32 flags )
386{
61f9802b 387 const U8 dq = (flags & PERL_PV_PRETTY_QUOTE) ? '"' : '%';
ab3bbdeb 388 STRLEN escaped;
7918f24d
NC
389
390 PERL_ARGS_ASSERT_PV_PRETTY;
391
881a015e
NC
392 if (!(flags & PERL_PV_PRETTY_NOCLEAR)) {
393 /* This won't alter the UTF-8 flag */
76f68e9b 394 sv_setpvs(dsv, "");
881a015e
NC
395 }
396
ab3bbdeb 397 if ( dq == '"' )
76f68e9b 398 sv_catpvs(dsv, "\"");
ab3bbdeb 399 else if ( flags & PERL_PV_PRETTY_LTGT )
76f68e9b 400 sv_catpvs(dsv, "<");
ab3bbdeb
YO
401
402 if ( start_color != NULL )
76f68e9b 403 sv_catpv(dsv, start_color);
ab3bbdeb
YO
404
405 pv_escape( dsv, str, count, max, &escaped, flags | PERL_PV_ESCAPE_NOCLEAR );
406
407 if ( end_color != NULL )
76f68e9b 408 sv_catpv(dsv, end_color);
ab3bbdeb
YO
409
410 if ( dq == '"' )
76f68e9b 411 sv_catpvs( dsv, "\"");
ab3bbdeb 412 else if ( flags & PERL_PV_PRETTY_LTGT )
76f68e9b 413 sv_catpvs(dsv, ">");
ab3bbdeb 414
95b611b0 415 if ( (flags & PERL_PV_PRETTY_ELLIPSES) && ( escaped < count ) )
76f68e9b 416 sv_catpvs(dsv, "...");
ab3bbdeb 417
3df15adc
YO
418 return SvPVX(dsv);
419}
420
421/*
422=for apidoc pv_display
423
3df15adc 424Similar to
3967c732 425
3df15adc
YO
426 pv_escape(dsv,pv,cur,pvlim,PERL_PV_ESCAPE_QUOTE);
427
428except that an additional "\0" will be appended to the string when
429len > cur and pv[cur] is "\0".
430
431Note that the final string may be up to 7 chars longer than pvlim.
432
433=cut
434*/
435
436char *
437Perl_pv_display(pTHX_ SV *dsv, const char *pv, STRLEN cur, STRLEN len, STRLEN pvlim)
438{
7918f24d
NC
439 PERL_ARGS_ASSERT_PV_DISPLAY;
440
ddc5bc0f 441 pv_pretty( dsv, pv, cur, pvlim, NULL, NULL, PERL_PV_PRETTY_DUMP);
3df15adc 442 if (len > cur && pv[cur] == '\0')
76f68e9b 443 sv_catpvs( dsv, "\\0");
e6abe6d8
JH
444 return SvPVX(dsv);
445}
446
447char *
864dbfa3 448Perl_sv_peek(pTHX_ SV *sv)
3967c732 449{
27da23d5 450 dVAR;
aec46f14 451 SV * const t = sv_newmortal();
3967c732 452 int unref = 0;
5357ca29 453 U32 type;
3967c732 454
76f68e9b 455 sv_setpvs(t, "");
3967c732
JD
456 retry:
457 if (!sv) {
458 sv_catpv(t, "VOID");
459 goto finish;
460 }
ad64d0ec 461 else if (sv == (const SV *)0x55555555 || SvTYPE(sv) == 'U') {
3967c732
JD
462 sv_catpv(t, "WILD");
463 goto finish;
464 }
7996736c 465 else if (sv == &PL_sv_undef || sv == &PL_sv_no || sv == &PL_sv_yes || sv == &PL_sv_placeholder) {
3967c732
JD
466 if (sv == &PL_sv_undef) {
467 sv_catpv(t, "SV_UNDEF");
468 if (!(SvFLAGS(sv) & (SVf_OK|SVf_OOK|SVs_OBJECT|
469 SVs_GMG|SVs_SMG|SVs_RMG)) &&
470 SvREADONLY(sv))
471 goto finish;
472 }
473 else if (sv == &PL_sv_no) {
474 sv_catpv(t, "SV_NO");
475 if (!(SvFLAGS(sv) & (SVf_ROK|SVf_OOK|SVs_OBJECT|
476 SVs_GMG|SVs_SMG|SVs_RMG)) &&
477 !(~SvFLAGS(sv) & (SVf_POK|SVf_NOK|SVf_READONLY|
478 SVp_POK|SVp_NOK)) &&
479 SvCUR(sv) == 0 &&
480 SvNVX(sv) == 0.0)
481 goto finish;
482 }
7996736c 483 else if (sv == &PL_sv_yes) {
3967c732
JD
484 sv_catpv(t, "SV_YES");
485 if (!(SvFLAGS(sv) & (SVf_ROK|SVf_OOK|SVs_OBJECT|
486 SVs_GMG|SVs_SMG|SVs_RMG)) &&
487 !(~SvFLAGS(sv) & (SVf_POK|SVf_NOK|SVf_READONLY|
488 SVp_POK|SVp_NOK)) &&
489 SvCUR(sv) == 1 &&
b15aece3 490 SvPVX_const(sv) && *SvPVX_const(sv) == '1' &&
3967c732
JD
491 SvNVX(sv) == 1.0)
492 goto finish;
7996736c
MHM
493 }
494 else {
495 sv_catpv(t, "SV_PLACEHOLDER");
496 if (!(SvFLAGS(sv) & (SVf_OK|SVf_OOK|SVs_OBJECT|
497 SVs_GMG|SVs_SMG|SVs_RMG)) &&
498 SvREADONLY(sv))
499 goto finish;
3967c732
JD
500 }
501 sv_catpv(t, ":");
502 }
503 else if (SvREFCNT(sv) == 0) {
504 sv_catpv(t, "(");
505 unref++;
506 }
a3b4c9c6
DM
507 else if (DEBUG_R_TEST_) {
508 int is_tmp = 0;
509 I32 ix;
510 /* is this SV on the tmps stack? */
511 for (ix=PL_tmps_ix; ix>=0; ix--) {
512 if (PL_tmps_stack[ix] == sv) {
513 is_tmp = 1;
514 break;
515 }
516 }
517 if (SvREFCNT(sv) > 1)
518 Perl_sv_catpvf(aTHX_ t, "<%"UVuf"%s>", (UV)SvREFCNT(sv),
519 is_tmp ? "T" : "");
520 else if (is_tmp)
521 sv_catpv(t, "<T>");
04932ac8
DM
522 }
523
3967c732
JD
524 if (SvROK(sv)) {
525 sv_catpv(t, "\\");
526 if (SvCUR(t) + unref > 10) {
b162af07 527 SvCUR_set(t, unref + 3);
3967c732
JD
528 *SvEND(t) = '\0';
529 sv_catpv(t, "...");
530 goto finish;
531 }
ad64d0ec 532 sv = SvRV(sv);
3967c732
JD
533 goto retry;
534 }
5357ca29
NC
535 type = SvTYPE(sv);
536 if (type == SVt_PVCV) {
537 Perl_sv_catpvf(aTHX_ t, "CV(%s)", CvGV(sv) ? GvNAME(CvGV(sv)) : "");
3967c732 538 goto finish;
5357ca29
NC
539 } else if (type < SVt_LAST) {
540 sv_catpv(t, svshorttypenames[type]);
3967c732 541
5357ca29
NC
542 if (type == SVt_NULL)
543 goto finish;
544 } else {
545 sv_catpv(t, "FREED");
3967c732 546 goto finish;
3967c732
JD
547 }
548
549 if (SvPOKp(sv)) {
b15aece3 550 if (!SvPVX_const(sv))
3967c732
JD
551 sv_catpv(t, "(null)");
552 else {
b9ac451d 553 SV * const tmp = newSVpvs("");
3967c732 554 sv_catpv(t, "(");
5115136b
DM
555 if (SvOOK(sv)) {
556 STRLEN delta;
557 SvOOK_offset(sv, delta);
558 Perl_sv_catpvf(aTHX_ t, "[%s]", pv_display(tmp, SvPVX_const(sv)-delta, delta, 0, 127));
559 }
b15aece3 560 Perl_sv_catpvf(aTHX_ t, "%s)", pv_display(tmp, SvPVX_const(sv), SvCUR(sv), SvLEN(sv), 127));
32639b87 561 if (SvUTF8(sv))
b2ff9928 562 Perl_sv_catpvf(aTHX_ t, " [UTF8 \"%s\"]",
e9569a7a 563 sv_uni_display(tmp, sv, 6 * SvCUR(sv),
c728cb41 564 UNI_DISPLAY_QQ));
3967c732
JD
565 SvREFCNT_dec(tmp);
566 }
567 }
568 else if (SvNOKp(sv)) {
e54dc35b 569 STORE_NUMERIC_LOCAL_SET_STANDARD();
1779d84d 570 Perl_sv_catpvf(aTHX_ t, "(%"NVgf")",SvNVX(sv));
e54dc35b 571 RESTORE_NUMERIC_LOCAL();
3967c732 572 }
57def98f 573 else if (SvIOKp(sv)) {
cf2093f6 574 if (SvIsUV(sv))
57def98f 575 Perl_sv_catpvf(aTHX_ t, "(%"UVuf")", (UV)SvUVX(sv));
cf2093f6 576 else
57def98f 577 Perl_sv_catpvf(aTHX_ t, "(%"IVdf")", (IV)SvIVX(sv));
25da4f38 578 }
3967c732
JD
579 else
580 sv_catpv(t, "()");
2ef28da1 581
3967c732 582 finish:
61f9802b
AL
583 while (unref--)
584 sv_catpv(t, ")");
59b714e2
RGS
585 if (PL_tainting && SvTAINTED(sv))
586 sv_catpv(t, " [tainted]");
8b6b16e7 587 return SvPV_nolen(t);
3967c732
JD
588}
589
590void
6867be6d 591Perl_do_pmop_dump(pTHX_ I32 level, PerlIO *file, const PMOP *pm)
3967c732
JD
592{
593 char ch;
594
7918f24d
NC
595 PERL_ARGS_ASSERT_DO_PMOP_DUMP;
596
3967c732 597 if (!pm) {
cea2e8a9 598 Perl_dump_indent(aTHX_ level, file, "{}\n");
3967c732
JD
599 return;
600 }
cea2e8a9 601 Perl_dump_indent(aTHX_ level, file, "{\n");
3967c732
JD
602 level++;
603 if (pm->op_pmflags & PMf_ONCE)
604 ch = '?';
605 else
606 ch = '/';
aaa362c4 607 if (PM_GETRE(pm))
cea2e8a9 608 Perl_dump_indent(aTHX_ level, file, "PMf_PRE %c%s%c%s\n",
220fc49f 609 ch, RX_PRECOMP(PM_GETRE(pm)), ch,
3967c732
JD
610 (pm->op_private & OPpRUNTIME) ? " (RUNTIME)" : "");
611 else
cea2e8a9 612 Perl_dump_indent(aTHX_ level, file, "PMf_PRE (RUNTIME)\n");
20e98b0f 613 if (pm->op_type != OP_PUSHRE && pm->op_pmreplrootu.op_pmreplroot) {
cea2e8a9 614 Perl_dump_indent(aTHX_ level, file, "PMf_REPL = ");
20e98b0f 615 op_dump(pm->op_pmreplrootu.op_pmreplroot);
3967c732 616 }
07bc277f 617 if (pm->op_pmflags || (PM_GETRE(pm) && RX_CHECK_SUBSTR(PM_GETRE(pm)))) {
4199688e 618 SV * const tmpsv = pm_description(pm);
b15aece3 619 Perl_dump_indent(aTHX_ level, file, "PMFLAGS = (%s)\n", SvCUR(tmpsv) ? SvPVX_const(tmpsv) + 1 : "");
3967c732
JD
620 SvREFCNT_dec(tmpsv);
621 }
622
cea2e8a9 623 Perl_dump_indent(aTHX_ level-1, file, "}\n");
3967c732
JD
624}
625
a0c2f4dd
NC
626const struct flag_to_name pmflags_flags_names[] = {
627 {PMf_CONST, ",CONST"},
628 {PMf_KEEP, ",KEEP"},
629 {PMf_GLOBAL, ",GLOBAL"},
630 {PMf_CONTINUE, ",CONTINUE"},
631 {PMf_RETAINT, ",RETAINT"},
632 {PMf_EVAL, ",EVAL"},
633 {PMf_NONDESTRUCT, ",NONDESTRUCT"},
634};
635
b9ac451d 636static SV *
4199688e
AL
637S_pm_description(pTHX_ const PMOP *pm)
638{
639 SV * const desc = newSVpvs("");
61f9802b 640 const REGEXP * const regex = PM_GETRE(pm);
4199688e
AL
641 const U32 pmflags = pm->op_pmflags;
642
7918f24d
NC
643 PERL_ARGS_ASSERT_PM_DESCRIPTION;
644
4199688e
AL
645 if (pmflags & PMf_ONCE)
646 sv_catpv(desc, ",ONCE");
c737faaf
YO
647#ifdef USE_ITHREADS
648 if (SvREADONLY(PL_regex_pad[pm->op_pmoffset]))
649 sv_catpv(desc, ":USED");
650#else
651 if (pmflags & PMf_USED)
652 sv_catpv(desc, ":USED");
653#endif
c737faaf 654
68d4833d 655 if (regex) {
07bc277f 656 if (RX_EXTFLAGS(regex) & RXf_TAINTED)
68d4833d 657 sv_catpv(desc, ",TAINTED");
07bc277f
NC
658 if (RX_CHECK_SUBSTR(regex)) {
659 if (!(RX_EXTFLAGS(regex) & RXf_NOSCAN))
68d4833d 660 sv_catpv(desc, ",SCANFIRST");
07bc277f 661 if (RX_EXTFLAGS(regex) & RXf_CHECK_ALL)
68d4833d
AB
662 sv_catpv(desc, ",ALL");
663 }
07bc277f 664 if (RX_EXTFLAGS(regex) & RXf_SKIPWHITE)
68d4833d 665 sv_catpv(desc, ",SKIPWHITE");
4199688e 666 }
68d4833d 667
a0c2f4dd 668 append_flags(desc, pmflags, pmflags_flags_names);
4199688e
AL
669 return desc;
670}
671
3967c732 672void
864dbfa3 673Perl_pmop_dump(pTHX_ PMOP *pm)
3967c732
JD
674{
675 do_pmop_dump(0, Perl_debug_log, pm);
79072805
LW
676}
677
2814eb74
PJ
678/* An op sequencer. We visit the ops in the order they're to execute. */
679
680STATIC void
0bd48802 681S_sequence(pTHX_ register const OP *o)
2814eb74 682{
27da23d5 683 dVAR;
c445ea15 684 const OP *oldop = NULL;
2814eb74 685
2814eb74
PJ
686 if (!o)
687 return;
688
3b721df9
NC
689#ifdef PERL_MAD
690 if (o->op_next == 0)
691 return;
692#endif
693
724e67cb
RGS
694 if (!Sequence)
695 Sequence = newHV();
2814eb74
PJ
696
697 for (; o; o = o->op_next) {
294b3b39
AL
698 STRLEN len;
699 SV * const op = newSVuv(PTR2UV(o));
700 const char * const key = SvPV_const(op, len);
701
2814eb74
PJ
702 if (hv_exists(Sequence, key, len))
703 break;
704
705 switch (o->op_type) {
706 case OP_STUB:
707 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
04fe65b0 708 (void)hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
2814eb74
PJ
709 break;
710 }
711 goto nothin;
712 case OP_NULL:
3b721df9
NC
713#ifdef PERL_MAD
714 if (o == o->op_next)
715 return;
716#endif
2814eb74
PJ
717 if (oldop && o->op_next)
718 continue;
719 break;
720 case OP_SCALAR:
721 case OP_LINESEQ:
722 case OP_SCOPE:
723 nothin:
724 if (oldop && o->op_next)
725 continue;
04fe65b0 726 (void)hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
2814eb74
PJ
727 break;
728
729 case OP_MAPWHILE:
730 case OP_GREPWHILE:
731 case OP_AND:
732 case OP_OR:
733 case OP_DOR:
734 case OP_ANDASSIGN:
735 case OP_ORASSIGN:
736 case OP_DORASSIGN:
737 case OP_COND_EXPR:
738 case OP_RANGE:
04fe65b0 739 (void)hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
294b3b39 740 sequence_tail(cLOGOPo->op_other);
2814eb74
PJ
741 break;
742
743 case OP_ENTERLOOP:
744 case OP_ENTERITER:
04fe65b0 745 (void)hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
294b3b39
AL
746 sequence_tail(cLOOPo->op_redoop);
747 sequence_tail(cLOOPo->op_nextop);
748 sequence_tail(cLOOPo->op_lastop);
2814eb74
PJ
749 break;
750
2814eb74 751 case OP_SUBST:
04fe65b0 752 (void)hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
29f2e912 753 sequence_tail(cPMOPo->op_pmstashstartu.op_pmreplstart);
2814eb74
PJ
754 break;
755
29f2e912
NC
756 case OP_QR:
757 case OP_MATCH:
2814eb74
PJ
758 case OP_HELEM:
759 break;
760
761 default:
04fe65b0 762 (void)hv_store(Sequence, key, len, newSVuv(++PL_op_seq), 0);
2814eb74
PJ
763 break;
764 }
765 oldop = o;
766 }
767}
768
294b3b39
AL
769static void
770S_sequence_tail(pTHX_ const OP *o)
771{
772 while (o && (o->op_type == OP_NULL))
773 o = o->op_next;
774 sequence(o);
775}
776
2814eb74 777STATIC UV
0bd48802 778S_sequence_num(pTHX_ const OP *o)
2814eb74 779{
27da23d5 780 dVAR;
2814eb74
PJ
781 SV *op,
782 **seq;
93524f2b 783 const char *key;
2814eb74
PJ
784 STRLEN len;
785 if (!o) return 0;
c0fd1b42 786 op = newSVuv(PTR2UV(o));
93524f2b 787 key = SvPV_const(op, len);
2814eb74
PJ
788 seq = hv_fetch(Sequence, key, len, 0);
789 return seq ? SvUV(*seq): 0;
790}
791
a0c2f4dd
NC
792const struct flag_to_name op_flags_names[] = {
793 {OPf_KIDS, ",KIDS"},
794 {OPf_PARENS, ",PARENS"},
a0c2f4dd
NC
795 {OPf_REF, ",REF"},
796 {OPf_MOD, ",MOD"},
65cccc5e 797 {OPf_STACKED, ",STACKED"},
a0c2f4dd
NC
798 {OPf_SPECIAL, ",SPECIAL"}
799};
800
ea9ad1f2 801const struct flag_to_name op_trans_names[] = {
65cccc5e
VP
802 {OPpTRANS_FROM_UTF, ",FROM_UTF"},
803 {OPpTRANS_TO_UTF, ",TO_UTF"},
804 {OPpTRANS_IDENTICAL, ",IDENTICAL"},
ea9ad1f2 805 {OPpTRANS_SQUASH, ",SQUASH"},
ea9ad1f2 806 {OPpTRANS_COMPLEMENT, ",COMPLEMENT"},
65cccc5e
VP
807 {OPpTRANS_GROWS, ",GROWS"},
808 {OPpTRANS_DELETE, ",DELETE"}
ea9ad1f2
NC
809};
810
811const struct flag_to_name op_entersub_names[] = {
ea9ad1f2
NC
812 {OPpENTERSUB_DB, ",DB"},
813 {OPpENTERSUB_HASTARG, ",HASTARG"},
65cccc5e
VP
814 {OPpENTERSUB_NOMOD, ",NOMOD"},
815 {OPpENTERSUB_AMPER, ",AMPER"},
ea9ad1f2 816 {OPpENTERSUB_NOPAREN, ",NOPAREN"},
65cccc5e 817 {OPpENTERSUB_INARGS, ",INARGS"}
ea9ad1f2
NC
818};
819
820const struct flag_to_name op_const_names[] = {
65cccc5e
VP
821 {OPpCONST_NOVER, ",NOVER"},
822 {OPpCONST_SHORTCIRCUIT, ",SHORTCIRCUIT"},
ea9ad1f2 823 {OPpCONST_STRICT, ",STRICT"},
65cccc5e 824 {OPpCONST_ENTERED, ",ENTERED"},
ea9ad1f2 825 {OPpCONST_ARYBASE, ",ARYBASE"},
65cccc5e
VP
826 {OPpCONST_BARE, ",BARE"},
827 {OPpCONST_WARNING, ",WARNING"}
ea9ad1f2
NC
828};
829
830const struct flag_to_name op_sort_names[] = {
831 {OPpSORT_NUMERIC, ",NUMERIC"},
832 {OPpSORT_INTEGER, ",INTEGER"},
65cccc5e
VP
833 {OPpSORT_REVERSE, ",REVERSE"},
834 {OPpSORT_INPLACE, ",INPLACE"},
835 {OPpSORT_DESCEND, ",DESCEND"},
836 {OPpSORT_QSORT, ",QSORT"},
837 {OPpSORT_STABLE, ",STABLE"}
ea9ad1f2
NC
838};
839
840const struct flag_to_name op_open_names[] = {
841 {OPpOPEN_IN_RAW, ",IN_RAW"},
842 {OPpOPEN_IN_CRLF, ",IN_CRLF"},
843 {OPpOPEN_OUT_RAW, ",OUT_RAW"},
844 {OPpOPEN_OUT_CRLF, ",OUT_CRLF"}
845};
846
261c990e
NC
847const struct flag_to_name op_exit_names[] = {
848 {OPpEXIT_VMSISH, ",EXIT_VMSISH"},
849 {OPpHUSH_VMSISH, ",HUSH_VMSISH"}
850};
851
852#define OP_PRIVATE_ONCE(op, flag, name) \
853 const struct flag_to_name CAT2(op, _names)[] = { \
854 {(flag), (name)} \
f58883a1 855 }
261c990e
NC
856
857OP_PRIVATE_ONCE(op_aassign, OPpASSIGN_COMMON, ",COMMON");
858OP_PRIVATE_ONCE(op_leavesub, OPpREFCOUNTED, ",REFCOUNTED");
859OP_PRIVATE_ONCE(op_sassign, OPpASSIGN_BACKWARDS, ",BACKWARDS");
860OP_PRIVATE_ONCE(op_repeat, OPpREPEAT_DOLIST, ",DOLIST");
65cccc5e 861OP_PRIVATE_ONCE(op_reverse, OPpREVERSE_INPLACE, ",INPLACE");
261c990e
NC
862OP_PRIVATE_ONCE(op_rv2cv, OPpLVAL_INTRO, ",INTRO");
863OP_PRIVATE_ONCE(op_flip, OPpFLIP_LINENUM, ",LINENUM");
864OP_PRIVATE_ONCE(op_gv, OPpEARLY_CV, ",EARLY_CV");
865OP_PRIVATE_ONCE(op_list, OPpLIST_GUESSED, ",GUESSED");
866OP_PRIVATE_ONCE(op_delete, OPpSLICE, ",SLICE");
867OP_PRIVATE_ONCE(op_exists, OPpEXISTS_SUB, ",EXISTS_SUB");
868OP_PRIVATE_ONCE(op_die, OPpHUSH_VMSISH, ",HUSH_VMSISH");
869
1fe3abee
NC
870struct op_private_by_op {
871 U16 op_type;
872 U16 len;
873 const struct flag_to_name *start;
874};
875
876const struct op_private_by_op op_private_names[] = {
261c990e
NC
877 {OP_LEAVESUB, C_ARRAY_LENGTH(op_leavesub_names), op_leavesub_names },
878 {OP_LEAVE, C_ARRAY_LENGTH(op_leavesub_names), op_leavesub_names },
879 {OP_LEAVESUBLV, C_ARRAY_LENGTH(op_leavesub_names), op_leavesub_names },
880 {OP_LEAVEWRITE, C_ARRAY_LENGTH(op_leavesub_names), op_leavesub_names },
881 {OP_AASSIGN, C_ARRAY_LENGTH(op_aassign_names), op_aassign_names },
882 {OP_DIE, C_ARRAY_LENGTH(op_die_names), op_die_names },
883 {OP_DELETE, C_ARRAY_LENGTH(op_delete_names), op_delete_names },
884 {OP_EXISTS, C_ARRAY_LENGTH(op_exists_names), op_exists_names },
885 {OP_EXIT, C_ARRAY_LENGTH(op_exit_names), op_exit_names },
886 {OP_FLIP, C_ARRAY_LENGTH(op_flip_names), op_flip_names },
887 {OP_FLOP, C_ARRAY_LENGTH(op_flip_names), op_flip_names },
888 {OP_GV, C_ARRAY_LENGTH(op_gv_names), op_gv_names },
889 {OP_LIST, C_ARRAY_LENGTH(op_list_names), op_list_names },
890 {OP_SASSIGN, C_ARRAY_LENGTH(op_sassign_names), op_sassign_names },
891 {OP_REPEAT, C_ARRAY_LENGTH(op_repeat_names), op_repeat_names },
892 {OP_RV2CV, C_ARRAY_LENGTH(op_rv2cv_names), op_rv2cv_names },
1fe3abee
NC
893 {OP_TRANS, C_ARRAY_LENGTH(op_trans_names), op_trans_names },
894 {OP_CONST, C_ARRAY_LENGTH(op_const_names), op_const_names },
895 {OP_SORT, C_ARRAY_LENGTH(op_sort_names), op_sort_names },
896 {OP_OPEN, C_ARRAY_LENGTH(op_open_names), op_open_names },
897 {OP_BACKTICK, C_ARRAY_LENGTH(op_open_names), op_open_names }
898};
899
900static bool
901S_op_private_to_names(pTHX_ SV *tmpsv, U32 optype, U32 op_private) {
902 const struct op_private_by_op *start = op_private_names;
903 const struct op_private_by_op *const end
904 = op_private_names + C_ARRAY_LENGTH(op_private_names);
905
906 /* This is a linear search, but no worse than the code that it replaced.
907 It's debugging code - size is more important than speed. */
908 do {
909 if (optype == start->op_type) {
910 S_append_flags(aTHX_ tmpsv, op_private, start->start,
911 start->start + start->len);
912 return TRUE;
913 }
914 } while (++start < end);
915 return FALSE;
916}
917
79072805 918void
6867be6d 919Perl_do_op_dump(pTHX_ I32 level, PerlIO *file, const OP *o)
79072805 920{
27da23d5 921 dVAR;
2814eb74 922 UV seq;
e15d5972
AL
923 const OPCODE optype = o->op_type;
924
7918f24d
NC
925 PERL_ARGS_ASSERT_DO_OP_DUMP;
926
0bd48802 927 sequence(o);
cea2e8a9 928 Perl_dump_indent(aTHX_ level, file, "{\n");
3967c732 929 level++;
0bd48802 930 seq = sequence_num(o);
2814eb74 931 if (seq)
f5992bc4 932 PerlIO_printf(file, "%-4"UVuf, seq);
93a17b20 933 else
3967c732 934 PerlIO_printf(file, " ");
c8db6e60
JH
935 PerlIO_printf(file,
936 "%*sTYPE = %s ===> ",
53e06cf0 937 (int)(PL_dumpindent*level-4), "", OP_NAME(o));
2814eb74 938 if (o->op_next)
f5992bc4 939 PerlIO_printf(file, seq ? "%"UVuf"\n" : "(%"UVuf")\n",
666ea192 940 sequence_num(o->op_next));
79072805 941 else
3967c732 942 PerlIO_printf(file, "DONE\n");
11343788 943 if (o->op_targ) {
e15d5972 944 if (optype == OP_NULL) {
cea2e8a9 945 Perl_dump_indent(aTHX_ level, file, " (was %s)\n", PL_op_name[o->op_targ]);
e15d5972 946 if (o->op_targ == OP_NEXTSTATE) {
ae7d165c 947 if (CopLINE(cCOPo))
f5992bc4 948 Perl_dump_indent(aTHX_ level, file, "LINE = %"UVuf"\n",
9d98dee5 949 (UV)CopLINE(cCOPo));
ae7d165c
PJ
950 if (CopSTASHPV(cCOPo))
951 Perl_dump_indent(aTHX_ level, file, "PACKAGE = \"%s\"\n",
952 CopSTASHPV(cCOPo));
4b65a919 953 if (CopLABEL(cCOPo))
ae7d165c 954 Perl_dump_indent(aTHX_ level, file, "LABEL = \"%s\"\n",
4b65a919 955 CopLABEL(cCOPo));
ae7d165c
PJ
956 }
957 }
8990e307 958 else
894356b3 959 Perl_dump_indent(aTHX_ level, file, "TARG = %ld\n", (long)o->op_targ);
8990e307 960 }
748a9306 961#ifdef DUMPADDR
57def98f 962 Perl_dump_indent(aTHX_ level, file, "ADDR = 0x%"UVxf" => 0x%"UVxf"\n", (UV)o, (UV)o->op_next);
79072805 963#endif
7e5d8ed2 964 if (o->op_flags || o->op_latefree || o->op_latefreed || o->op_attached) {
e15d5972 965 SV * const tmpsv = newSVpvs("");
5dc0d613 966 switch (o->op_flags & OPf_WANT) {
54310121 967 case OPf_WANT_VOID:
46fc3d4c 968 sv_catpv(tmpsv, ",VOID");
54310121
PP
969 break;
970 case OPf_WANT_SCALAR:
46fc3d4c 971 sv_catpv(tmpsv, ",SCALAR");
54310121
PP
972 break;
973 case OPf_WANT_LIST:
46fc3d4c 974 sv_catpv(tmpsv, ",LIST");
54310121
PP
975 break;
976 default:
46fc3d4c 977 sv_catpv(tmpsv, ",UNKNOWN");
54310121
PP
978 break;
979 }
a0c2f4dd 980 append_flags(tmpsv, o->op_flags, op_flags_names);
29522234
DM
981 if (o->op_latefree)
982 sv_catpv(tmpsv, ",LATEFREE");
983 if (o->op_latefreed)
984 sv_catpv(tmpsv, ",LATEFREED");
7e5d8ed2
DM
985 if (o->op_attached)
986 sv_catpv(tmpsv, ",ATTACHED");
b15aece3 987 Perl_dump_indent(aTHX_ level, file, "FLAGS = (%s)\n", SvCUR(tmpsv) ? SvPVX_const(tmpsv) + 1 : "");
46fc3d4c 988 SvREFCNT_dec(tmpsv);
79072805 989 }
11343788 990 if (o->op_private) {
e15d5972
AL
991 SV * const tmpsv = newSVpvs("");
992 if (PL_opargs[optype] & OA_TARGLEX) {
07447971
GS
993 if (o->op_private & OPpTARGET_MY)
994 sv_catpv(tmpsv, ",TARGET_MY");
995 }
e15d5972 996 else if (optype == OP_ENTERSUB ||
5c135d48
NC
997 optype == OP_RV2SV ||
998 optype == OP_GVSV ||
999 optype == OP_RV2AV ||
1000 optype == OP_RV2HV ||
1001 optype == OP_RV2GV ||
1002 optype == OP_AELEM ||
1003 optype == OP_HELEM )
85e6fe83 1004 {
e15d5972 1005 if (optype == OP_ENTERSUB) {
ea9ad1f2 1006 append_flags(tmpsv, o->op_private, op_entersub_names);
68dc0745 1007 }
bf91b999 1008 else {
d3011074 1009 switch (o->op_private & OPpDEREF) {
b9ac451d
AL
1010 case OPpDEREF_SV:
1011 sv_catpv(tmpsv, ",SV");
1012 break;
1013 case OPpDEREF_AV:
1014 sv_catpv(tmpsv, ",AV");
1015 break;
1016 case OPpDEREF_HV:
1017 sv_catpv(tmpsv, ",HV");
1018 break;
1019 }
bf91b999
SC
1020 if (o->op_private & OPpMAYBE_LVSUB)
1021 sv_catpv(tmpsv, ",MAYBE_LVSUB");
1022 }
0824d667
DM
1023
1024 if ((optype==OP_RV2SV || optype==OP_RV2AV || optype==OP_RV2HV)
1025 && (o->op_private & OPpDEREFed))
1026 sv_catpv(tmpsv, ",DEREFed");
1027
e15d5972 1028 if (optype == OP_AELEM || optype == OP_HELEM) {
5dc0d613 1029 if (o->op_private & OPpLVAL_DEFER)
46fc3d4c 1030 sv_catpv(tmpsv, ",LVAL_DEFER");
68dc0745
PP
1031 }
1032 else {
5dc0d613 1033 if (o->op_private & HINT_STRICT_REFS)
46fc3d4c 1034 sv_catpv(tmpsv, ",STRICT_REFS");
192587c2
GS
1035 if (o->op_private & OPpOUR_INTRO)
1036 sv_catpv(tmpsv, ",OUR_INTRO");
68dc0745 1037 }
8d063cd8 1038 }
1fe3abee
NC
1039 else if (S_op_private_to_names(aTHX_ tmpsv, optype, o->op_private)) {
1040 }
ef69c8fc 1041 else if (PL_check[optype] != Perl_ck_ftst) {
6ecf81d6 1042 if (OP_IS_FILETEST_ACCESS(o->op_type) && o->op_private & OPpFT_ACCESS)
fbb0b3b3
RGS
1043 sv_catpv(tmpsv, ",FT_ACCESS");
1044 if (o->op_private & OPpFT_STACKED)
1045 sv_catpv(tmpsv, ",FT_STACKED");
1af34c76 1046 }
11343788 1047 if (o->op_flags & OPf_MOD && o->op_private & OPpLVAL_INTRO)
46fc3d4c
PP
1048 sv_catpv(tmpsv, ",INTRO");
1049 if (SvCUR(tmpsv))
b15aece3 1050 Perl_dump_indent(aTHX_ level, file, "PRIVATE = (%s)\n", SvPVX_const(tmpsv) + 1);
46fc3d4c 1051 SvREFCNT_dec(tmpsv);
8d063cd8 1052 }
8d063cd8 1053
3b721df9
NC
1054#ifdef PERL_MAD
1055 if (PL_madskills && o->op_madprop) {
76f68e9b 1056 SV * const tmpsv = newSVpvs("");
3b721df9
NC
1057 MADPROP* mp = o->op_madprop;
1058 Perl_dump_indent(aTHX_ level, file, "MADPROPS = {\n");
1059 level++;
1060 while (mp) {
61f9802b 1061 const char tmp = mp->mad_key;
76f68e9b 1062 sv_setpvs(tmpsv,"'");
3b721df9
NC
1063 if (tmp)
1064 sv_catpvn(tmpsv, &tmp, 1);
1065 sv_catpv(tmpsv, "'=");
1066 switch (mp->mad_type) {
1067 case MAD_NULL:
1068 sv_catpv(tmpsv, "NULL");
1069 Perl_dump_indent(aTHX_ level, file, "%s\n", SvPVX(tmpsv));
1070 break;
1071 case MAD_PV:
1072 sv_catpv(tmpsv, "<");
1073 sv_catpvn(tmpsv, (char*)mp->mad_val, mp->mad_vlen);
1074 sv_catpv(tmpsv, ">");
1075 Perl_dump_indent(aTHX_ level, file, "%s\n", SvPVX(tmpsv));
1076 break;
1077 case MAD_OP:
1078 if ((OP*)mp->mad_val) {
1079 Perl_dump_indent(aTHX_ level, file, "%s\n", SvPVX(tmpsv));
1080 do_op_dump(level, file, (OP*)mp->mad_val);
1081 }
1082 break;
1083 default:
1084 sv_catpv(tmpsv, "(UNK)");
1085 Perl_dump_indent(aTHX_ level, file, "%s\n", SvPVX(tmpsv));
1086 break;
1087 }
1088 mp = mp->mad_next;
1089 }
1090 level--;
1091 Perl_dump_indent(aTHX_ level, file, "}\n");
1092
1093 SvREFCNT_dec(tmpsv);
1094 }
1095#endif
1096
e15d5972 1097 switch (optype) {
971a9dd3 1098 case OP_AELEMFAST:
93a17b20 1099 case OP_GVSV:
79072805 1100 case OP_GV:
971a9dd3 1101#ifdef USE_ITHREADS
c803eecc 1102 Perl_dump_indent(aTHX_ level, file, "PADIX = %" IVdf "\n", (IV)cPADOPo->op_padix);
971a9dd3 1103#else
1640e9f0 1104 if ( ! (o->op_flags & OPf_SPECIAL)) { /* not lexical */
38c076c7 1105 if (cSVOPo->op_sv) {
d4c19fe8 1106 SV * const tmpsv = newSV(0);
38c076c7
DM
1107 ENTER;
1108 SAVEFREESV(tmpsv);
3b721df9 1109#ifdef PERL_MAD
84021b46 1110 /* FIXME - is this making unwarranted assumptions about the
3b721df9
NC
1111 UTF-8 cleanliness of the dump file handle? */
1112 SvUTF8_on(tmpsv);
1113#endif
159b6efe 1114 gv_fullname3(tmpsv, MUTABLE_GV(cSVOPo->op_sv), NULL);
8b6b16e7 1115 Perl_dump_indent(aTHX_ level, file, "GV = %s\n",
d5263905 1116 SvPV_nolen_const(tmpsv));
38c076c7
DM
1117 LEAVE;
1118 }
1119 else
1120 Perl_dump_indent(aTHX_ level, file, "GV = NULL\n");
378cc40b 1121 }
971a9dd3 1122#endif
79072805
LW
1123 break;
1124 case OP_CONST:
996c9baa 1125 case OP_HINTSEVAL:
f5d5a27c 1126 case OP_METHOD_NAMED:
b6a15bc5
DM
1127#ifndef USE_ITHREADS
1128 /* with ITHREADS, consts are stored in the pad, and the right pad
1129 * may not be active here, so skip */
3848b962 1130 Perl_dump_indent(aTHX_ level, file, "SV = %s\n", SvPEEK(cSVOPo_sv));
b6a15bc5 1131#endif
79072805 1132 break;
93a17b20
LW
1133 case OP_NEXTSTATE:
1134 case OP_DBSTATE:
57843af0 1135 if (CopLINE(cCOPo))
f5992bc4 1136 Perl_dump_indent(aTHX_ level, file, "LINE = %"UVuf"\n",
9d98dee5 1137 (UV)CopLINE(cCOPo));
ed094faf
GS
1138 if (CopSTASHPV(cCOPo))
1139 Perl_dump_indent(aTHX_ level, file, "PACKAGE = \"%s\"\n",
1140 CopSTASHPV(cCOPo));
4b65a919 1141 if (CopLABEL(cCOPo))
ed094faf 1142 Perl_dump_indent(aTHX_ level, file, "LABEL = \"%s\"\n",
4b65a919 1143 CopLABEL(cCOPo));
79072805
LW
1144 break;
1145 case OP_ENTERLOOP:
cea2e8a9 1146 Perl_dump_indent(aTHX_ level, file, "REDO ===> ");
11343788 1147 if (cLOOPo->op_redoop)
f5992bc4 1148 PerlIO_printf(file, "%"UVuf"\n", sequence_num(cLOOPo->op_redoop));
79072805 1149 else
3967c732 1150 PerlIO_printf(file, "DONE\n");
cea2e8a9 1151 Perl_dump_indent(aTHX_ level, file, "NEXT ===> ");
11343788 1152 if (cLOOPo->op_nextop)
f5992bc4 1153 PerlIO_printf(file, "%"UVuf"\n", sequence_num(cLOOPo->op_nextop));
79072805 1154 else
3967c732 1155 PerlIO_printf(file, "DONE\n");
cea2e8a9 1156 Perl_dump_indent(aTHX_ level, file, "LAST ===> ");
11343788 1157 if (cLOOPo->op_lastop)
f5992bc4 1158 PerlIO_printf(file, "%"UVuf"\n", sequence_num(cLOOPo->op_lastop));
79072805 1159 else
3967c732 1160 PerlIO_printf(file, "DONE\n");
79072805
LW
1161 break;
1162 case OP_COND_EXPR:
1a67a97c 1163 case OP_RANGE:
a0d0e21e 1164 case OP_MAPWHILE:
79072805
LW
1165 case OP_GREPWHILE:
1166 case OP_OR:
1167 case OP_AND:
cea2e8a9 1168 Perl_dump_indent(aTHX_ level, file, "OTHER ===> ");
11343788 1169 if (cLOGOPo->op_other)
f5992bc4 1170 PerlIO_printf(file, "%"UVuf"\n", sequence_num(cLOGOPo->op_other));
79072805 1171 else
3967c732 1172 PerlIO_printf(file, "DONE\n");
79072805
LW
1173 break;
1174 case OP_PUSHRE:
1175 case OP_MATCH:
8782bef2 1176 case OP_QR:
79072805 1177 case OP_SUBST:
3967c732 1178 do_pmop_dump(level, file, cPMOPo);
79072805 1179 break;
7934575e
GS
1180 case OP_LEAVE:
1181 case OP_LEAVEEVAL:
1182 case OP_LEAVESUB:
1183 case OP_LEAVESUBLV:
1184 case OP_LEAVEWRITE:
1185 case OP_SCOPE:
1186 if (o->op_private & OPpREFCOUNTED)
1187 Perl_dump_indent(aTHX_ level, file, "REFCNT = %"UVuf"\n", (UV)o->op_targ);
1188 break;
a0d0e21e
LW
1189 default:
1190 break;
79072805 1191 }
11343788 1192 if (o->op_flags & OPf_KIDS) {
79072805 1193 OP *kid;
11343788 1194 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
3967c732 1195 do_op_dump(level, file, kid);
8d063cd8 1196 }
cea2e8a9 1197 Perl_dump_indent(aTHX_ level-1, file, "}\n");
3967c732
JD
1198}
1199
1200void
6867be6d 1201Perl_op_dump(pTHX_ const OP *o)
3967c732 1202{
7918f24d 1203 PERL_ARGS_ASSERT_OP_DUMP;
3967c732 1204 do_op_dump(0, Perl_debug_log, o);
8d063cd8
LW
1205}
1206
8adcabd8 1207void
864dbfa3 1208Perl_gv_dump(pTHX_ GV *gv)
378cc40b 1209{
79072805 1210 SV *sv;
378cc40b 1211
7918f24d
NC
1212 PERL_ARGS_ASSERT_GV_DUMP;
1213
79072805 1214 if (!gv) {
760ac839 1215 PerlIO_printf(Perl_debug_log, "{}\n");
378cc40b
LW
1216 return;
1217 }
8990e307 1218 sv = sv_newmortal();
760ac839 1219 PerlIO_printf(Perl_debug_log, "{\n");
bd61b366 1220 gv_fullname3(sv, gv, NULL);
b15aece3 1221 Perl_dump_indent(aTHX_ 1, Perl_debug_log, "GV_NAME = %s", SvPVX_const(sv));
79072805 1222 if (gv != GvEGV(gv)) {
bd61b366 1223 gv_efullname3(sv, GvEGV(gv), NULL);
b15aece3 1224 Perl_dump_indent(aTHX_ 1, Perl_debug_log, "-> %s", SvPVX_const(sv));
8adcabd8 1225 }
3967c732 1226 PerlIO_putc(Perl_debug_log, '\n');
cea2e8a9 1227 Perl_dump_indent(aTHX_ 0, Perl_debug_log, "}\n");
8d063cd8
LW
1228}
1229
14befaf4 1230
afe38520 1231/* map magic types to the symbolic names
14befaf4
DM
1232 * (with the PERL_MAGIC_ prefixed stripped)
1233 */
1234
27da23d5 1235static const struct { const char type; const char *name; } magic_names[] = {
52f49505 1236#include "mg_names.c"
516a5887 1237 /* this null string terminates the list */
b9ac451d 1238 { 0, NULL },
14befaf4
DM
1239};
1240
8adcabd8 1241void
6867be6d 1242Perl_do_magic_dump(pTHX_ I32 level, PerlIO *file, const MAGIC *mg, I32 nest, I32 maxnest, bool dumpops, STRLEN pvlim)
8d063cd8 1243{
7918f24d
NC
1244 PERL_ARGS_ASSERT_DO_MAGIC_DUMP;
1245
3967c732 1246 for (; mg; mg = mg->mg_moremagic) {
b900a521
JH
1247 Perl_dump_indent(aTHX_ level, file,
1248 " MAGIC = 0x%"UVxf"\n", PTR2UV(mg));
3967c732 1249 if (mg->mg_virtual) {
bfed75c6 1250 const MGVTBL * const v = mg->mg_virtual;
2d1f1fe5
NC
1251 if (v >= PL_magic_vtables
1252 && v < PL_magic_vtables + magic_vtable_max) {
1253 const U32 i = v - PL_magic_vtables;
1254 Perl_dump_indent(aTHX_ level, file, " MG_VIRTUAL = &PL_vtbl_%s\n", PL_magic_vtable_names[i]);
1255 }
3967c732 1256 else
b900a521 1257 Perl_dump_indent(aTHX_ level, file, " MG_VIRTUAL = 0x%"UVxf"\n", PTR2UV(v));
3967c732
JD
1258 }
1259 else
cea2e8a9 1260 Perl_dump_indent(aTHX_ level, file, " MG_VIRTUAL = 0\n");
8d063cd8 1261
3967c732 1262 if (mg->mg_private)
cea2e8a9 1263 Perl_dump_indent(aTHX_ level, file, " MG_PRIVATE = %d\n", mg->mg_private);
3967c732 1264
14befaf4
DM
1265 {
1266 int n;
c445ea15 1267 const char *name = NULL;
27da23d5 1268 for (n = 0; magic_names[n].name; n++) {
14befaf4
DM
1269 if (mg->mg_type == magic_names[n].type) {
1270 name = magic_names[n].name;
1271 break;
1272 }
1273 }
1274 if (name)
1275 Perl_dump_indent(aTHX_ level, file,
1276 " MG_TYPE = PERL_MAGIC_%s\n", name);
1277 else
1278 Perl_dump_indent(aTHX_ level, file,
1279 " MG_TYPE = UNKNOWN(\\%o)\n", mg->mg_type);
1280 }
3967c732
JD
1281
1282 if (mg->mg_flags) {
cea2e8a9 1283 Perl_dump_indent(aTHX_ level, file, " MG_FLAGS = 0x%02X\n", mg->mg_flags);
cb50f42d
YST
1284 if (mg->mg_type == PERL_MAGIC_envelem &&
1285 mg->mg_flags & MGf_TAINTEDDIR)
cea2e8a9 1286 Perl_dump_indent(aTHX_ level, file, " TAINTEDDIR\n");
4c02285a
FR
1287 if (mg->mg_type == PERL_MAGIC_regex_global &&
1288 mg->mg_flags & MGf_MINMATCH)
1289 Perl_dump_indent(aTHX_ level, file, " MINMATCH\n");
3967c732 1290 if (mg->mg_flags & MGf_REFCOUNTED)
cea2e8a9 1291 Perl_dump_indent(aTHX_ level, file, " REFCOUNTED\n");
3967c732 1292 if (mg->mg_flags & MGf_GSKIP)
cea2e8a9 1293 Perl_dump_indent(aTHX_ level, file, " GSKIP\n");
4c02285a
FR
1294 if (mg->mg_flags & MGf_COPY)
1295 Perl_dump_indent(aTHX_ level, file, " COPY\n");
1296 if (mg->mg_flags & MGf_DUP)
1297 Perl_dump_indent(aTHX_ level, file, " DUP\n");
1298 if (mg->mg_flags & MGf_LOCAL)
1299 Perl_dump_indent(aTHX_ level, file, " LOCAL\n");
3967c732
JD
1300 }
1301 if (mg->mg_obj) {
4c02285a 1302 Perl_dump_indent(aTHX_ level, file, " MG_OBJ = 0x%"UVxf"\n",
28d8d7f4
YO
1303 PTR2UV(mg->mg_obj));
1304 if (mg->mg_type == PERL_MAGIC_qr) {
07bc277f 1305 REGEXP* const re = (REGEXP *)mg->mg_obj;
61f9802b 1306 SV * const dsv = sv_newmortal();
866c78d1 1307 const char * const s
4c02285a 1308 = pv_pretty(dsv, RX_WRAPPED(re), RX_WRAPLEN(re),
28d8d7f4 1309 60, NULL, NULL,
95b611b0 1310 ( PERL_PV_PRETTY_QUOTE | PERL_PV_ESCAPE_RE | PERL_PV_PRETTY_ELLIPSES |
3c8556c3 1311 (RX_UTF8(re) ? PERL_PV_ESCAPE_UNI : 0))
28d8d7f4 1312 );
6483fb35
RGS
1313 Perl_dump_indent(aTHX_ level+1, file, " PAT = %s\n", s);
1314 Perl_dump_indent(aTHX_ level+1, file, " REFCNT = %"IVdf"\n",
07bc277f 1315 (IV)RX_REFCNT(re));
28d8d7f4
YO
1316 }
1317 if (mg->mg_flags & MGf_REFCOUNTED)
3967c732
JD
1318 do_sv_dump(level+2, file, mg->mg_obj, nest+1, maxnest, dumpops, pvlim); /* MG is already +1 */
1319 }
1320 if (mg->mg_len)
894356b3 1321 Perl_dump_indent(aTHX_ level, file, " MG_LEN = %ld\n", (long)mg->mg_len);
3967c732 1322 if (mg->mg_ptr) {
b900a521 1323 Perl_dump_indent(aTHX_ level, file, " MG_PTR = 0x%"UVxf, PTR2UV(mg->mg_ptr));
3967c732 1324 if (mg->mg_len >= 0) {
7e8c5dac 1325 if (mg->mg_type != PERL_MAGIC_utf8) {
61f9802b 1326 SV * const sv = newSVpvs("");
7e8c5dac
HS
1327 PerlIO_printf(file, " %s", pv_display(sv, mg->mg_ptr, mg->mg_len, 0, pvlim));
1328 SvREFCNT_dec(sv);
1329 }
3967c732
JD
1330 }
1331 else if (mg->mg_len == HEf_SVKEY) {
1332 PerlIO_puts(file, " => HEf_SVKEY\n");
ad64d0ec
NC
1333 do_sv_dump(level+2, file, MUTABLE_SV(((mg)->mg_ptr)), nest+1,
1334 maxnest, dumpops, pvlim); /* MG is already +1 */
3967c732
JD
1335 continue;
1336 }
866f9d6c 1337 else if (mg->mg_len == -1 && mg->mg_type == PERL_MAGIC_utf8);
3967c732 1338 else
866f9d6c
FC
1339 PerlIO_puts(
1340 file,
1341 " ???? - " __FILE__
1342 " does not know how to handle this MG_LEN"
1343 );
3967c732
JD
1344 PerlIO_putc(file, '\n');
1345 }
7e8c5dac 1346 if (mg->mg_type == PERL_MAGIC_utf8) {
61f9802b 1347 const STRLEN * const cache = (STRLEN *) mg->mg_ptr;
7e8c5dac
HS
1348 if (cache) {
1349 IV i;
1350 for (i = 0; i < PERL_MAGIC_UTF8_CACHESIZE; i++)
1351 Perl_dump_indent(aTHX_ level, file,
1352 " %2"IVdf": %"UVuf" -> %"UVuf"\n",
1353 i,
1354 (UV)cache[i * 2],
1355 (UV)cache[i * 2 + 1]);
1356 }
1357 }
378cc40b 1358 }
3967c732
JD
1359}
1360
1361void
6867be6d 1362Perl_magic_dump(pTHX_ const MAGIC *mg)
3967c732 1363{
b9ac451d 1364 do_magic_dump(0, Perl_debug_log, mg, 0, 0, FALSE, 0);
3967c732
JD
1365}
1366
1367void
e1ec3a88 1368Perl_do_hv_dump(pTHX_ I32 level, PerlIO *file, const char *name, HV *sv)
3967c732 1369{
bfcb3514 1370 const char *hvname;
7918f24d
NC
1371
1372 PERL_ARGS_ASSERT_DO_HV_DUMP;
1373
b900a521 1374 Perl_dump_indent(aTHX_ level, file, "%s = 0x%"UVxf, name, PTR2UV(sv));
bfcb3514 1375 if (sv && (hvname = HvNAME_get(sv)))
d7d51f4b
YO
1376 {
1377 /* we have to use pv_display and HvNAMELEN_get() so that we display the real package
1378 name which quite legally could contain insane things like tabs, newlines, nulls or
1379 other scary crap - this should produce sane results - except maybe for unicode package
1380 names - but we will wait for someone to file a bug on that - demerphq */
1381 SV * const tmpsv = newSVpvs("");
1382 PerlIO_printf(file, "\t%s\n", pv_display(tmpsv, hvname, HvNAMELEN_get(sv), 0, 1024));
1383 }
79072805 1384 else
3967c732
JD
1385 PerlIO_putc(file, '\n');
1386}
1387
1388void
e1ec3a88 1389Perl_do_gv_dump(pTHX_ I32 level, PerlIO *file, const char *name, GV *sv)
3967c732 1390{
7918f24d
NC
1391 PERL_ARGS_ASSERT_DO_GV_DUMP;
1392
b900a521 1393 Perl_dump_indent(aTHX_ level, file, "%s = 0x%"UVxf, name, PTR2UV(sv));
3967c732
JD
1394 if (sv && GvNAME(sv))
1395 PerlIO_printf(file, "\t\"%s\"\n", GvNAME(sv));
c90c0ff4 1396 else
3967c732
JD
1397 PerlIO_putc(file, '\n');
1398}
1399
1400void
e1ec3a88 1401Perl_do_gvgv_dump(pTHX_ I32 level, PerlIO *file, const char *name, GV *sv)
3967c732 1402{
7918f24d
NC
1403 PERL_ARGS_ASSERT_DO_GVGV_DUMP;
1404
b900a521 1405 Perl_dump_indent(aTHX_ level, file, "%s = 0x%"UVxf, name, PTR2UV(sv));
3967c732 1406 if (sv && GvNAME(sv)) {
bfcb3514 1407 const char *hvname;
3967c732 1408 PerlIO_printf(file, "\t\"");
bfcb3514
NC
1409 if (GvSTASH(sv) && (hvname = HvNAME_get(GvSTASH(sv))))
1410 PerlIO_printf(file, "%s\" :: \"", hvname);
3967c732 1411 PerlIO_printf(file, "%s\"\n", GvNAME(sv));
8d063cd8 1412 }
3967c732
JD
1413 else
1414 PerlIO_putc(file, '\n');
1415}
1416
a0c2f4dd
NC
1417const struct flag_to_name first_sv_flags_names[] = {
1418 {SVs_TEMP, "TEMP,"},
1419 {SVs_OBJECT, "OBJECT,"},
1420 {SVs_GMG, "GMG,"},
1421 {SVs_SMG, "SMG,"},
1422 {SVs_RMG, "RMG,"},
1423 {SVf_IOK, "IOK,"},
1424 {SVf_NOK, "NOK,"},
1425 {SVf_POK, "POK,"}
1426};
1427
1428const struct flag_to_name second_sv_flags_names[] = {
1429 {SVf_OOK, "OOK,"},
1430 {SVf_FAKE, "FAKE,"},
1431 {SVf_READONLY, "READONLY,"},
1432 {SVf_BREAK, "BREAK,"},
1433 {SVf_AMAGIC, "OVERLOAD,"},
1434 {SVp_IOK, "pIOK,"},
1435 {SVp_NOK, "pNOK,"},
1436 {SVp_POK, "pPOK,"}
1437};
1438
ae1f06a1
NC
1439const struct flag_to_name cv_flags_names[] = {
1440 {CVf_ANON, "ANON,"},
1441 {CVf_UNIQUE, "UNIQUE,"},
1442 {CVf_CLONE, "CLONE,"},
1443 {CVf_CLONED, "CLONED,"},
1444 {CVf_CONST, "CONST,"},
1445 {CVf_NODEBUG, "NODEBUG,"},
1446 {CVf_LVALUE, "LVALUE,"},
1447 {CVf_METHOD, "METHOD,"},
cfc1e951 1448 {CVf_WEAKOUTSIDE, "WEAKOUTSIDE,"},
31d45e0c
DM
1449 {CVf_CVGV_RC, "CVGV_RC,"},
1450 {CVf_ISXSUB, "ISXSUB,"}
ae1f06a1
NC
1451};
1452
1453const struct flag_to_name hv_flags_names[] = {
1454 {SVphv_SHAREKEYS, "SHAREKEYS,"},
1455 {SVphv_LAZYDEL, "LAZYDEL,"},
1456 {SVphv_HASKFLAGS, "HASKFLAGS,"},
1457 {SVphv_REHASH, "REHASH,"},
1458 {SVphv_CLONEABLE, "CLONEABLE,"}
1459};
1460
1461const struct flag_to_name gp_flags_names[] = {
1462 {GVf_INTRO, "INTRO,"},
1463 {GVf_MULTI, "MULTI,"},
1464 {GVf_ASSUMECV, "ASSUMECV,"},
1465 {GVf_IN_PAD, "IN_PAD,"}
1466};
1467
1468const struct flag_to_name gp_flags_imported_names[] = {
1469 {GVf_IMPORTED_SV, " SV"},
1470 {GVf_IMPORTED_AV, " AV"},
1471 {GVf_IMPORTED_HV, " HV"},
1472 {GVf_IMPORTED_CV, " CV"},
1473};
1474
d63e6659
DM
1475const struct flag_to_name regexp_flags_names[] = {
1476 {RXf_PMf_MULTILINE, "PMf_MULTILINE,"},
1477 {RXf_PMf_SINGLELINE, "PMf_SINGLELINE,"},
1478 {RXf_PMf_FOLD, "PMf_FOLD,"},
1479 {RXf_PMf_EXTENDED, "PMf_EXTENDED,"},
1480 {RXf_PMf_KEEPCOPY, "PMf_KEEPCOPY,"},
1481 {RXf_ANCH_BOL, "ANCH_BOL,"},
1482 {RXf_ANCH_MBOL, "ANCH_MBOL,"},
1483 {RXf_ANCH_SBOL, "ANCH_SBOL,"},
1484 {RXf_ANCH_GPOS, "ANCH_GPOS,"},
1485 {RXf_GPOS_SEEN, "GPOS_SEEN,"},
1486 {RXf_GPOS_FLOAT, "GPOS_FLOAT,"},
1487 {RXf_LOOKBEHIND_SEEN, "LOOKBEHIND_SEEN,"},
1488 {RXf_EVAL_SEEN, "EVAL_SEEN,"},
1489 {RXf_CANY_SEEN, "CANY_SEEN,"},
1490 {RXf_NOSCAN, "NOSCAN,"},
1491 {RXf_CHECK_ALL, "CHECK_ALL,"},
1492 {RXf_MATCH_UTF8, "MATCH_UTF8,"},
1493 {RXf_USE_INTUIT_NOML, "USE_INTUIT_NOML,"},
1494 {RXf_USE_INTUIT_ML, "USE_INTUIT_ML,"},
1495 {RXf_INTUIT_TAIL, "INTUIT_TAIL,"},
1496 {RXf_SPLIT, "SPLIT,"},
1497 {RXf_COPY_DONE, "COPY_DONE,"},
1498 {RXf_TAINTED_SEEN, "TAINTED_SEEN,"},
1499 {RXf_TAINTED, "TAINTED,"},
1500 {RXf_START_ONLY, "START_ONLY,"},
1501 {RXf_SKIPWHITE, "SKIPWHITE,"},
1502 {RXf_WHITE, "WHITE,"},
1503 {RXf_NULL, "NULL,"},
1504};
1505
3967c732 1506void
864dbfa3 1507Perl_do_sv_dump(pTHX_ I32 level, PerlIO *file, SV *sv, I32 nest, I32 maxnest, bool dumpops, STRLEN pvlim)
3967c732 1508{
97aff369 1509 dVAR;
cea89e20 1510 SV *d;
e1ec3a88 1511 const char *s;
3967c732
JD
1512 U32 flags;
1513 U32 type;
1514
7918f24d
NC
1515 PERL_ARGS_ASSERT_DO_SV_DUMP;
1516
3967c732 1517 if (!sv) {
cea2e8a9 1518 Perl_dump_indent(aTHX_ level, file, "SV = 0\n");
3967c732 1519 return;
378cc40b 1520 }
2ef28da1 1521
3967c732
JD
1522 flags = SvFLAGS(sv);
1523 type = SvTYPE(sv);
79072805 1524
e0bbf362
DM
1525 /* process general SV flags */
1526
cea89e20 1527 d = Perl_newSVpvf(aTHX_
57def98f 1528 "(0x%"UVxf") at 0x%"UVxf"\n%*s REFCNT = %"IVdf"\n%*s FLAGS = (",
56431972 1529 PTR2UV(SvANY(sv)), PTR2UV(sv),
894356b3
GS
1530 (int)(PL_dumpindent*level), "", (IV)SvREFCNT(sv),
1531 (int)(PL_dumpindent*level), "");
8d063cd8 1532
1979170b
NC
1533 if (!((flags & SVpad_NAME) == SVpad_NAME
1534 && (type == SVt_PVMG || type == SVt_PVNV))) {
e604303a
NC
1535 if (flags & SVs_PADSTALE) sv_catpv(d, "PADSTALE,");
1536 }
1979170b 1537 if (!((flags & SVpad_NAME) == SVpad_NAME && type == SVt_PVMG)) {
e604303a
NC
1538 if (flags & SVs_PADTMP) sv_catpv(d, "PADTMP,");
1539 if (flags & SVs_PADMY) sv_catpv(d, "PADMY,");
1540 }
a0c2f4dd 1541 append_flags(d, flags, first_sv_flags_names);
810b8aa5
GS
1542 if (flags & SVf_ROK) {
1543 sv_catpv(d, "ROK,");
1544 if (SvWEAKREF(sv)) sv_catpv(d, "WEAKREF,");
1545 }
a0c2f4dd 1546 append_flags(d, flags, second_sv_flags_names);
1ccdb730
NC
1547 if (flags & SVp_SCREAM && type != SVt_PVHV && !isGV_with_GP(sv)) {
1548 if (SvPCS_IMPORTED(sv))
1549 sv_catpv(d, "PCS_IMPORTED,");
1550 else
9660f481 1551 sv_catpv(d, "SCREAM,");
1ccdb730 1552 }
3967c732 1553
e0bbf362
DM
1554 /* process type-specific SV flags */
1555
3967c732
JD
1556 switch (type) {
1557 case SVt_PVCV:
1558 case SVt_PVFM:
ae1f06a1 1559 append_flags(d, CvFLAGS(sv), cv_flags_names);
3967c732
JD
1560 break;
1561 case SVt_PVHV:
ae1f06a1 1562 append_flags(d, flags, hv_flags_names);
3967c732 1563 break;
926fc7b6
DM
1564 case SVt_PVGV:
1565 case SVt_PVLV:
1566 if (isGV_with_GP(sv)) {
ae1f06a1 1567 append_flags(d, GvFLAGS(sv), gp_flags_names);
926fc7b6 1568 }
926fc7b6 1569 if (isGV_with_GP(sv) && GvIMPORTED(sv)) {
3967c732
JD
1570 sv_catpv(d, "IMPORT");
1571 if (GvIMPORTED(sv) == GVf_IMPORTED)
1572 sv_catpv(d, "ALL,");
1573 else {
1574 sv_catpv(d, "(");
ae1f06a1 1575 append_flags(d, GvFLAGS(sv), gp_flags_imported_names);
3967c732
JD
1576 sv_catpv(d, " ),");
1577 }
1578 }
addd1794 1579 /* FALL THROUGH */
25da4f38 1580 default:
e604303a 1581 evaled_or_uv:
25da4f38 1582 if (SvEVALED(sv)) sv_catpv(d, "EVALED,");
69c678eb 1583 if (SvIsUV(sv) && !(flags & SVf_ROK)) sv_catpv(d, "IsUV,");
25da4f38 1584 break;
addd1794 1585 case SVt_PVMG:
c13a5c80
NC
1586 if (SvTAIL(sv)) sv_catpv(d, "TAIL,");
1587 if (SvVALID(sv)) sv_catpv(d, "VALID,");
00b1698f 1588 if (SvPAD_TYPED(sv)) sv_catpv(d, "TYPED,");
e604303a 1589 if (SvPAD_OUR(sv)) sv_catpv(d, "OUR,");
2e94196c 1590 /* FALL THROUGH */
e604303a
NC
1591 case SVt_PVNV:
1592 if (SvPAD_STATE(sv)) sv_catpv(d, "STATE,");
1593 goto evaled_or_uv;
11ca45c0
NC
1594 case SVt_PVAV:
1595 break;
3967c732 1596 }
86f0d186
NC
1597 /* SVphv_SHAREKEYS is also 0x20000000 */
1598 if ((type != SVt_PVHV) && SvUTF8(sv))
9fe74ede 1599 sv_catpv(d, "UTF8");
3967c732 1600
b162af07
SP
1601 if (*(SvEND(d) - 1) == ',') {
1602 SvCUR_set(d, SvCUR(d) - 1);
1603 SvPVX(d)[SvCUR(d)] = '\0';
1604 }
3967c732 1605 sv_catpv(d, ")");
b15aece3 1606 s = SvPVX_const(d);
3967c732 1607
e0bbf362
DM
1608 /* dump initial SV details */
1609
fd0854ff 1610#ifdef DEBUG_LEAKING_SCALARS
cbe56f1d 1611 Perl_dump_indent(aTHX_ level, file,
cd676548 1612 "ALLOCATED at %s:%d %s %s (parent 0x%"UVxf"); serial %"UVuf"\n",
fd0854ff
DM
1613 sv->sv_debug_file ? sv->sv_debug_file : "(unknown)",
1614 sv->sv_debug_line,
1615 sv->sv_debug_inpad ? "for" : "by",
1616 sv->sv_debug_optype ? PL_op_name[sv->sv_debug_optype]: "(none)",
cd676548 1617 PTR2UV(sv->sv_debug_parent),
cbe56f1d
DM
1618 sv->sv_debug_serial
1619 );
fd0854ff 1620#endif
cea2e8a9 1621 Perl_dump_indent(aTHX_ level, file, "SV = ");
e0bbf362
DM
1622
1623 /* Dump SV type */
1624
5357ca29
NC
1625 if (type < SVt_LAST) {
1626 PerlIO_printf(file, "%s%s\n", svtypenames[type], s);
1627
1628 if (type == SVt_NULL) {
1629 SvREFCNT_dec(d);
1630 return;
1631 }
1632 } else {
faccc32b 1633 PerlIO_printf(file, "UNKNOWN(0x%"UVxf") %s\n", (UV)type, s);
cea89e20 1634 SvREFCNT_dec(d);
3967c732
JD
1635 return;
1636 }
e0bbf362
DM
1637
1638 /* Dump general SV fields */
1639
27bd069f 1640 if ((type >= SVt_PVIV && type != SVt_PVAV && type != SVt_PVHV
0a0c4b76
NC
1641 && type != SVt_PVCV && type != SVt_PVFM && type != SVt_PVIO
1642 && type != SVt_REGEXP && !isGV_with_GP(sv) && !SvVALID(sv))
4df7f6af 1643 || (type == SVt_IV && !SvROK(sv))) {
765f542d 1644 if (SvIsUV(sv)
f8c7b90f 1645#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
1646 || SvIsCOW(sv)
1647#endif
1648 )
57def98f 1649 Perl_dump_indent(aTHX_ level, file, " UV = %"UVuf, (UV)SvUVX(sv));
cf2093f6 1650 else
57def98f 1651 Perl_dump_indent(aTHX_ level, file, " IV = %"IVdf, (IV)SvIVX(sv));
f8c7b90f 1652#ifdef PERL_OLD_COPY_ON_WRITE
765f542d
NC
1653 if (SvIsCOW_shared_hash(sv))
1654 PerlIO_printf(file, " (HASH)");
1655 else if (SvIsCOW_normal(sv))
1656 PerlIO_printf(file, " (COW from 0x%"UVxf")", (UV)SvUVX(sv));
1657#endif
3967c732
JD
1658 PerlIO_putc(file, '\n');
1659 }
e0bbf362 1660
1979170b
NC
1661 if ((type == SVt_PVNV || type == SVt_PVMG)
1662 && (SvFLAGS(sv) & SVpad_NAME) == SVpad_NAME) {
0e4c4423
NC
1663 Perl_dump_indent(aTHX_ level, file, " COP_LOW = %"UVuf"\n",
1664 (UV) COP_SEQ_RANGE_LOW(sv));
1665 Perl_dump_indent(aTHX_ level, file, " COP_HIGH = %"UVuf"\n",
1666 (UV) COP_SEQ_RANGE_HIGH(sv));
1667 } else if ((type >= SVt_PVNV && type != SVt_PVAV && type != SVt_PVHV
08e44740 1668 && type != SVt_PVCV && type != SVt_PVFM && type != SVt_REGEXP
c0a413d1 1669 && type != SVt_PVIO && !isGV_with_GP(sv) && !SvVALID(sv))
0e4c4423 1670 || type == SVt_NV) {
e54dc35b 1671 STORE_NUMERIC_LOCAL_SET_STANDARD();
57def98f 1672 /* %Vg doesn't work? --jhi */
cf2093f6 1673#ifdef USE_LONG_DOUBLE
2d4389e4 1674 Perl_dump_indent(aTHX_ level, file, " NV = %.*" PERL_PRIgldbl "\n", LDBL_DIG, SvNVX(sv));
cf2093f6 1675#else
cea2e8a9 1676 Perl_dump_indent(aTHX_ level, file, " NV = %.*g\n", DBL_DIG, SvNVX(sv));
cf2093f6 1677#endif
e54dc35b 1678 RESTORE_NUMERIC_LOCAL();
3967c732 1679 }
e0bbf362 1680
3967c732 1681 if (SvROK(sv)) {
57def98f 1682 Perl_dump_indent(aTHX_ level, file, " RV = 0x%"UVxf"\n", PTR2UV(SvRV(sv)));
3967c732
JD
1683 if (nest < maxnest)
1684 do_sv_dump(level+1, file, SvRV(sv), nest+1, maxnest, dumpops, pvlim);
3967c732 1685 }
e0bbf362 1686
cea89e20
JH
1687 if (type < SVt_PV) {
1688 SvREFCNT_dec(d);
3967c732 1689 return;
cea89e20 1690 }
e0bbf362 1691
a49b46c6 1692 if ((type <= SVt_PVLV && !isGV_with_GP(sv)) || type == SVt_PVFM) {
b15aece3 1693 if (SvPVX_const(sv)) {
69240efd 1694 STRLEN delta;
7a4bba22 1695 if (SvOOK(sv)) {
69240efd 1696 SvOOK_offset(sv, delta);
7a4bba22 1697 Perl_dump_indent(aTHX_ level, file," OFFSET = %"UVuf"\n",
5186cc12 1698 (UV) delta);
69240efd
NC
1699 } else {
1700 delta = 0;
7a4bba22 1701 }
b15aece3 1702 Perl_dump_indent(aTHX_ level, file," PV = 0x%"UVxf" ", PTR2UV(SvPVX_const(sv)));
7a4bba22
NC
1703 if (SvOOK(sv)) {
1704 PerlIO_printf(file, "( %s . ) ",
1705 pv_display(d, SvPVX_const(sv) - delta, delta, 0,
1706 pvlim));
1707 }
b15aece3 1708 PerlIO_printf(file, "%s", pv_display(d, SvPVX_const(sv), SvCUR(sv), SvLEN(sv), pvlim));
e9569a7a
GG
1709 if (SvUTF8(sv)) /* the 6? \x{....} */
1710 PerlIO_printf(file, " [UTF8 \"%s\"]", sv_uni_display(d, sv, 6 * SvCUR(sv), UNI_DISPLAY_QQ));
e6abe6d8 1711 PerlIO_printf(file, "\n");
57def98f
JH
1712 Perl_dump_indent(aTHX_ level, file, " CUR = %"IVdf"\n", (IV)SvCUR(sv));
1713 Perl_dump_indent(aTHX_ level, file, " LEN = %"IVdf"\n", (IV)SvLEN(sv));
3967c732
JD
1714 }
1715 else
cea2e8a9 1716 Perl_dump_indent(aTHX_ level, file, " PV = 0\n");
3967c732 1717 }
e0bbf362 1718
3967c732 1719 if (type >= SVt_PVMG) {
0e4c4423 1720 if (type == SVt_PVMG && SvPAD_OUR(sv)) {
61f9802b 1721 HV * const ost = SvOURSTASH(sv);
38cbaf55
RGS
1722 if (ost)
1723 do_hv_dump(level, file, " OURSTASH", ost);
0e4c4423
NC
1724 } else {
1725 if (SvMAGIC(sv))
8530ff28 1726 do_magic_dump(level, file, SvMAGIC(sv), nest+1, maxnest, dumpops, pvlim);
0e4c4423 1727 }
3967c732
JD
1728 if (SvSTASH(sv))
1729 do_hv_dump(level, file, " STASH", SvSTASH(sv));
c13a5c80
NC
1730
1731 if ((type == SVt_PVMG || type == SVt_PVLV) && SvVALID(sv)) {
1732 Perl_dump_indent(aTHX_ level, file, " RARE = %u\n", (U8)BmRARE(sv));
1733 Perl_dump_indent(aTHX_ level, file, " PREVIOUS = %"UVuf"\n", (UV)BmPREVIOUS(sv));
1734 Perl_dump_indent(aTHX_ level, file, " USEFUL = %"IVdf"\n", (IV)BmUSEFUL(sv));
1735 }
3967c732 1736 }
e0bbf362
DM
1737
1738 /* Dump type-specific SV fields */
1739
3967c732 1740 switch (type) {
3967c732 1741 case SVt_PVAV:
57def98f 1742 Perl_dump_indent(aTHX_ level, file, " ARRAY = 0x%"UVxf, PTR2UV(AvARRAY(sv)));
3967c732 1743 if (AvARRAY(sv) != AvALLOC(sv)) {
57def98f
JH
1744 PerlIO_printf(file, " (offset=%"IVdf")\n", (IV)(AvARRAY(sv) - AvALLOC(sv)));
1745 Perl_dump_indent(aTHX_ level, file, " ALLOC = 0x%"UVxf"\n", PTR2UV(AvALLOC(sv)));
3967c732
JD
1746 }
1747 else
1748 PerlIO_putc(file, '\n');
57def98f
JH
1749 Perl_dump_indent(aTHX_ level, file, " FILL = %"IVdf"\n", (IV)AvFILLp(sv));
1750 Perl_dump_indent(aTHX_ level, file, " MAX = %"IVdf"\n", (IV)AvMAX(sv));
a3874608 1751 Perl_dump_indent(aTHX_ level, file, " ARYLEN = 0x%"UVxf"\n", SvMAGIC(sv) ? PTR2UV(AvARYLEN(sv)) : 0);
76f68e9b 1752 sv_setpvs(d, "");
11ca45c0
NC
1753 if (AvREAL(sv)) sv_catpv(d, ",REAL");
1754 if (AvREIFY(sv)) sv_catpv(d, ",REIFY");
b15aece3
SP
1755 Perl_dump_indent(aTHX_ level, file, " FLAGS = (%s)\n",
1756 SvCUR(d) ? SvPVX_const(d) + 1 : "");
502c6561 1757 if (nest < maxnest && av_len(MUTABLE_AV(sv)) >= 0) {
3967c732 1758 int count;
502c6561
NC
1759 for (count = 0; count <= av_len(MUTABLE_AV(sv)) && count < maxnest; count++) {
1760 SV** const elt = av_fetch(MUTABLE_AV(sv),count,0);
3967c732 1761
57def98f 1762 Perl_dump_indent(aTHX_ level + 1, file, "Elt No. %"IVdf"\n", (IV)count);
2ef28da1 1763 if (elt)
3967c732
JD
1764 do_sv_dump(level+1, file, *elt, nest+1, maxnest, dumpops, pvlim);
1765 }
1766 }
1767 break;
1768 case SVt_PVHV:
57def98f 1769 Perl_dump_indent(aTHX_ level, file, " ARRAY = 0x%"UVxf, PTR2UV(HvARRAY(sv)));
1b95d04f 1770 if (HvARRAY(sv) && HvUSEDKEYS(sv)) {
3967c732
JD
1771 /* Show distribution of HEs in the ARRAY */
1772 int freq[200];
bb7a0f54 1773#define FREQ_MAX ((int)(sizeof freq / sizeof freq[0] - 1))
3967c732
JD
1774 int i;
1775 int max = 0;
1b95d04f 1776 U32 pow2 = 2, keys = HvUSEDKEYS(sv);
65202027 1777 NV theoret, sum = 0;
3967c732
JD
1778
1779 PerlIO_printf(file, " (");
1780 Zero(freq, FREQ_MAX + 1, int);
eb160463 1781 for (i = 0; (STRLEN)i <= HvMAX(sv); i++) {
c445ea15
AL
1782 HE* h;
1783 int count = 0;
3967c732
JD
1784 for (h = HvARRAY(sv)[i]; h; h = HeNEXT(h))
1785 count++;
1786 if (count > FREQ_MAX)
1787 count = FREQ_MAX;
1788 freq[count]++;
1789 if (max < count)
1790 max = count;
1791 }
1792 for (i = 0; i <= max; i++) {
1793 if (freq[i]) {
1794 PerlIO_printf(file, "%d%s:%d", i,
1795 (i == FREQ_MAX) ? "+" : "",
1796 freq[i]);
1797 if (i != max)
1798 PerlIO_printf(file, ", ");
1799 }
1800 }
1801 PerlIO_putc(file, ')');
b8fa94d8
MG
1802 /* The "quality" of a hash is defined as the total number of
1803 comparisons needed to access every element once, relative
1804 to the expected number needed for a random hash.
1805
1806 The total number of comparisons is equal to the sum of
e76cd0fa
AMS
1807 the squares of the number of entries in each bucket.
1808 For a random hash of n keys into k buckets, the expected
b8fa94d8
MG
1809 value is
1810 n + n(n-1)/2k
1811 */
1812
3967c732
JD
1813 for (i = max; i > 0; i--) { /* Precision: count down. */
1814 sum += freq[i] * i * i;
1815 }
155aba94 1816 while ((keys = keys >> 1))
3967c732 1817 pow2 = pow2 << 1;
1b95d04f 1818 theoret = HvUSEDKEYS(sv);
b8fa94d8 1819 theoret += theoret * (theoret-1)/pow2;
3967c732 1820 PerlIO_putc(file, '\n');
6b4667fc 1821 Perl_dump_indent(aTHX_ level, file, " hash quality = %.1"NVff"%%", theoret/sum*100);
3967c732
JD
1822 }
1823 PerlIO_putc(file, '\n');
1b95d04f 1824 Perl_dump_indent(aTHX_ level, file, " KEYS = %"IVdf"\n", (IV)HvUSEDKEYS(sv));
57def98f
JH
1825 Perl_dump_indent(aTHX_ level, file, " FILL = %"IVdf"\n", (IV)HvFILL(sv));
1826 Perl_dump_indent(aTHX_ level, file, " MAX = %"IVdf"\n", (IV)HvMAX(sv));
bfcb3514
NC
1827 Perl_dump_indent(aTHX_ level, file, " RITER = %"IVdf"\n", (IV)HvRITER_get(sv));
1828 Perl_dump_indent(aTHX_ level, file, " EITER = 0x%"UVxf"\n", PTR2UV(HvEITER_get(sv)));
8d2f4536 1829 {
b9ac451d 1830 MAGIC * const mg = mg_find(sv, PERL_MAGIC_symtab);
8d2f4536
NC
1831 if (mg && mg->mg_obj) {
1832 Perl_dump_indent(aTHX_ level, file, " PMROOT = 0x%"UVxf"\n", PTR2UV(mg->mg_obj));
1833 }
1834 }
bfcb3514 1835 {
b9ac451d 1836 const char * const hvname = HvNAME_get(sv);
bfcb3514
NC
1837 if (hvname)
1838 Perl_dump_indent(aTHX_ level, file, " NAME = \"%s\"\n", hvname);
1839 }
86f55936 1840 if (SvOOK(sv)) {
ad64d0ec 1841 AV * const backrefs
85fbaab2 1842 = *Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(sv));
7d88e6c4 1843 struct mro_meta * const meta = HvAUX(sv)->xhv_mro_meta;
67e04715
FC
1844 if (HvAUX(sv)->xhv_name_count)
1845 Perl_dump_indent(aTHX_
7afc2217
FC
1846 level, file, " NAMECOUNT = %"IVdf"\n",
1847 (IV)HvAUX(sv)->xhv_name_count
67e04715 1848 );
15d9236d 1849 if (HvAUX(sv)->xhv_name_u.xhvnameu_name && HvENAME_HEK_NN(sv)) {
ec3405c8
NC
1850 const I32 count = HvAUX(sv)->xhv_name_count;
1851 if (count) {
1852 SV * const names = newSVpvs_flags("", SVs_TEMP);
1853 /* The starting point is the first element if count is
1854 positive and the second element if count is negative. */
1855 HEK *const *hekp = HvAUX(sv)->xhv_name_u.xhvnameu_names
1856 + (count < 0 ? 1 : 0);
1857 HEK *const *const endp = HvAUX(sv)->xhv_name_u.xhvnameu_names
1858 + (count < 0 ? -count : count);
1859 while (hekp < endp) {
67e04715
FC
1860 if (*hekp) {
1861 sv_catpvs(names, ", \"");
ec3405c8 1862 sv_catpvn(names, HEK_KEY(*hekp), HEK_LEN(*hekp));
67e04715 1863 sv_catpvs(names, "\"");
ec3405c8
NC
1864 } else {
1865 /* This should never happen. */
1866 sv_catpvs(names, ", (null)");
67e04715 1867 }
ec3405c8
NC
1868 ++hekp;
1869 }
67e04715
FC
1870 Perl_dump_indent(aTHX_
1871 level, file, " ENAME = %s\n", SvPV_nolen(names)+2
1872 );
1873 }
1874 else
1875 Perl_dump_indent(aTHX_
1876 level, file, " ENAME = \"%s\"\n", HvENAME_get(sv)
1877 );
1878 }
86f55936
NC
1879 if (backrefs) {
1880 Perl_dump_indent(aTHX_ level, file, " BACKREFS = 0x%"UVxf"\n",
1881 PTR2UV(backrefs));
ad64d0ec 1882 do_sv_dump(level+1, file, MUTABLE_SV(backrefs), nest+1, maxnest,
86f55936
NC
1883 dumpops, pvlim);
1884 }
7d88e6c4
NC
1885 if (meta) {
1886 /* FIXME - mro_algs kflags can signal a UTF-8 name. */
1887 Perl_dump_indent(aTHX_ level, file, " MRO_WHICH = \"%.*s\" (0x%"UVxf")\n",
1888 (int)meta->mro_which->length,
1889 meta->mro_which->name,
1890 PTR2UV(meta->mro_which));
1891 Perl_dump_indent(aTHX_ level, file, " CACHE_GEN = 0x%"UVxf"\n",
1892 (UV)meta->cache_gen);
1893 Perl_dump_indent(aTHX_ level, file, " PKG_GEN = 0x%"UVxf"\n",
1894 (UV)meta->pkg_gen);
1895 if (meta->mro_linear_all) {
1896 Perl_dump_indent(aTHX_ level, file, " MRO_LINEAR_ALL = 0x%"UVxf"\n",
1897 PTR2UV(meta->mro_linear_all));
1898 do_sv_dump(level+1, file, MUTABLE_SV(meta->mro_linear_all), nest+1, maxnest,
1899 dumpops, pvlim);
1900 }
1901 if (meta->mro_linear_current) {
1902 Perl_dump_indent(aTHX_ level, file, " MRO_LINEAR_CURRENT = 0x%"UVxf"\n",
1903 PTR2UV(meta->mro_linear_current));
1904 do_sv_dump(level+1, file, MUTABLE_SV(meta->mro_linear_current), nest+1, maxnest,
1905 dumpops, pvlim);
1906 }
1907 if (meta->mro_nextmethod) {
1908 Perl_dump_indent(aTHX_ level, file, " MRO_NEXTMETHOD = 0x%"UVxf"\n",
1909 PTR2UV(meta->mro_nextmethod));
1910 do_sv_dump(level+1, file, MUTABLE_SV(meta->mro_nextmethod), nest+1, maxnest,
1911 dumpops, pvlim);
1912 }
1913 if (meta->isa) {
1914 Perl_dump_indent(aTHX_ level, file, " ISA = 0x%"UVxf"\n",
1915 PTR2UV(meta->isa));
1916 do_sv_dump(level+1, file, MUTABLE_SV(meta->isa), nest+1, maxnest,
1917 dumpops, pvlim);
1918 }
1919 }
86f55936 1920 }
cbab3169
FC
1921 if (nest < maxnest && !HvEITER_get(sv)) { /* Try to preserve iterator */
1922 HE *he;
1923 HV * const hv = MUTABLE_HV(sv);
1924 int count = maxnest - nest;
1925
1926 hv_iterinit(hv);
1927 while ((he = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))
1928 && count--) {
1929 STRLEN len;
1930 const U32 hash = HeHASH(he);
1931 SV * const keysv = hv_iterkeysv(he);
1932 const char * const keypv = SvPV_const(keysv, len);
1933 SV * const elt = hv_iterval(hv, he);
1934
1935 Perl_dump_indent(aTHX_ level+1, file, "Elt %s ", pv_display(d, keypv, len, 0, pvlim));
1936 if (SvUTF8(keysv))
1937 PerlIO_printf(file, "[UTF8 \"%s\"] ", sv_uni_display(d, keysv, 6 * SvCUR(keysv), UNI_DISPLAY_QQ));
1938 if (HeKREHASH(he))
1939 PerlIO_printf(file, "[REHASH] ");
1940 PerlIO_printf(file, "HASH = 0x%"UVxf"\n", (UV)hash);
1941 do_sv_dump(level+1, file, elt, nest+1, maxnest, dumpops, pvlim);
3967c732 1942 }
cbab3169 1943 hv_iterinit(hv); /* Return to status quo */
3967c732
JD
1944 }
1945 break;
e0bbf362 1946
3967c732 1947 case SVt_PVCV:
cbf82dd0
NC
1948 if (SvPOK(sv)) {
1949 STRLEN len;
1950 const char *const proto = SvPV_const(sv, len);
1951 Perl_dump_indent(aTHX_ level, file, " PROTOTYPE = \"%.*s\"\n",
1952 (int) len, proto);
1953 }
3967c732
JD
1954 /* FALL THROUGH */
1955 case SVt_PVFM:
1956 do_hv_dump(level, file, " COMP_STASH", CvSTASH(sv));
d04ba589
NC
1957 if (!CvISXSUB(sv)) {
1958 if (CvSTART(sv)) {
1959 Perl_dump_indent(aTHX_ level, file,
1960 " START = 0x%"UVxf" ===> %"IVdf"\n",
1961 PTR2UV(CvSTART(sv)),
1962 (IV)sequence_num(CvSTART(sv)));
1963 }
1964 Perl_dump_indent(aTHX_ level, file, " ROOT = 0x%"UVxf"\n",
1965 PTR2UV(CvROOT(sv)));
1966 if (CvROOT(sv) && dumpops) {
1967 do_op_dump(level+1, file, CvROOT(sv));
1968 }
1969 } else {
126f53f3 1970 SV * const constant = cv_const_sv((const CV *)sv);
b1886099 1971
d04ba589 1972 Perl_dump_indent(aTHX_ level, file, " XSUB = 0x%"UVxf"\n", PTR2UV(CvXSUB(sv)));
b1886099
NC
1973
1974 if (constant) {
1975 Perl_dump_indent(aTHX_ level, file, " XSUBANY = 0x%"UVxf
1976 " (CONST SV)\n",
1977 PTR2UV(CvXSUBANY(sv).any_ptr));
1978 do_sv_dump(level+1, file, constant, nest+1, maxnest, dumpops,
1979 pvlim);
1980 } else {
1981 Perl_dump_indent(aTHX_ level, file, " XSUBANY = %"IVdf"\n",
1982 (IV)CvXSUBANY(sv).any_i32);
1983 }
1984 }
3967c732 1985 do_gvgv_dump(level, file, " GVGV::GV", CvGV(sv));
57843af0 1986 Perl_dump_indent(aTHX_ level, file, " FILE = \"%s\"\n", CvFILE(sv));
5129b2ca
NC
1987 if (type == SVt_PVCV)
1988 Perl_dump_indent(aTHX_ level, file, " DEPTH = %"IVdf"\n", (IV)CvDEPTH(sv));
894356b3 1989 Perl_dump_indent(aTHX_ level, file, " FLAGS = 0x%"UVxf"\n", (UV)CvFLAGS(sv));
a3985cdc 1990 Perl_dump_indent(aTHX_ level, file, " OUTSIDE_SEQ = %"UVuf"\n", (UV)CvOUTSIDE_SEQ(sv));
3967c732 1991 if (type == SVt_PVFM)
57def98f
JH
1992 Perl_dump_indent(aTHX_ level, file, " LINES = %"IVdf"\n", (IV)FmLINES(sv));
1993 Perl_dump_indent(aTHX_ level, file, " PADLIST = 0x%"UVxf"\n", PTR2UV(CvPADLIST(sv)));
dd2155a4
DM
1994 if (nest < maxnest) {
1995 do_dump_pad(level+1, file, CvPADLIST(sv), 0);
3967c732
JD
1996 }
1997 {
b9ac451d 1998 const CV * const outside = CvOUTSIDE(sv);
2ef28da1 1999 Perl_dump_indent(aTHX_ level, file, " OUTSIDE = 0x%"UVxf" (%s)\n",
57def98f 2000 PTR2UV(outside),
cf2093f6
JH
2001 (!outside ? "null"
2002 : CvANON(outside) ? "ANON"
2003 : (outside == PL_main_cv) ? "MAIN"
2004 : CvUNIQUE(outside) ? "UNIQUE"
2005 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3967c732
JD
2006 }
2007 if (nest < maxnest && (CvCLONE(sv) || CvCLONED(sv)))
ad64d0ec 2008 do_sv_dump(level+1, file, MUTABLE_SV(CvOUTSIDE(sv)), nest+1, maxnest, dumpops, pvlim);
3967c732 2009 break;
e0bbf362 2010
926fc7b6
DM
2011 case SVt_PVGV:
2012 case SVt_PVLV:
b9ac451d
AL
2013 if (type == SVt_PVLV) {
2014 Perl_dump_indent(aTHX_ level, file, " TYPE = %c\n", LvTYPE(sv));
2015 Perl_dump_indent(aTHX_ level, file, " TARGOFF = %"IVdf"\n", (IV)LvTARGOFF(sv));
2016 Perl_dump_indent(aTHX_ level, file, " TARGLEN = %"IVdf"\n", (IV)LvTARGLEN(sv));
2017 Perl_dump_indent(aTHX_ level, file, " TARG = 0x%"UVxf"\n", PTR2UV(LvTARG(sv)));
2018 if (LvTYPE(sv) != 't' && LvTYPE(sv) != 'T')
2019 do_sv_dump(level+1, file, LvTARG(sv), nest+1, maxnest,
2020 dumpops, pvlim);
2021 }
926fc7b6
DM
2022 if (!isGV_with_GP(sv))
2023 break;
cea2e8a9 2024 Perl_dump_indent(aTHX_ level, file, " NAME = \"%s\"\n", GvNAME(sv));
57def98f 2025 Perl_dump_indent(aTHX_ level, file, " NAMELEN = %"IVdf"\n", (IV)GvNAMELEN(sv));
3967c732 2026 do_hv_dump (level, file, " GvSTASH", GvSTASH(sv));
57def98f 2027 Perl_dump_indent(aTHX_ level, file, " GP = 0x%"UVxf"\n", PTR2UV(GvGP(sv)));
f472eb5c
GS
2028 if (!GvGP(sv))
2029 break;
57def98f
JH
2030 Perl_dump_indent(aTHX_ level, file, " SV = 0x%"UVxf"\n", PTR2UV(GvSV(sv)));
2031 Perl_dump_indent(aTHX_ level, file, " REFCNT = %"IVdf"\n", (IV)GvREFCNT(sv));
2032 Perl_dump_indent(aTHX_ level, file, " IO = 0x%"UVxf"\n", PTR2UV(GvIOp(sv)));
2033 Perl_dump_indent(aTHX_ level, file, " FORM = 0x%"UVxf" \n", PTR2UV(GvFORM(sv)));
2034 Perl_dump_indent(aTHX_ level, file, " AV = 0x%"UVxf"\n", PTR2UV(GvAV(sv)));
2035 Perl_dump_indent(aTHX_ level, file, " HV = 0x%"UVxf"\n", PTR2UV(GvHV(sv)));
2036 Perl_dump_indent(aTHX_ level, file, " CV = 0x%"UVxf"\n", PTR2UV(GvCV(sv)));
2037 Perl_dump_indent(aTHX_ level, file, " CVGEN = 0x%"UVxf"\n", (UV)GvCVGEN(sv));
57def98f 2038 Perl_dump_indent(aTHX_ level, file, " LINE = %"IVdf"\n", (IV)GvLINE(sv));
b195d487 2039 Perl_dump_indent(aTHX_ level, file, " FILE = \"%s\"\n", GvFILE(sv));
e39917cc 2040 Perl_dump_indent(aTHX_ level, file, " FLAGS = 0x%"UVxf"\n", (UV)GvFLAGS(sv));
3967c732
JD
2041 do_gv_dump (level, file, " EGV", GvEGV(sv));
2042 break;
2043 case SVt_PVIO:
57def98f
JH
2044 Perl_dump_indent(aTHX_ level, file, " IFP = 0x%"UVxf"\n", PTR2UV(IoIFP(sv)));
2045 Perl_dump_indent(aTHX_ level, file, " OFP = 0x%"UVxf"\n", PTR2UV(IoOFP(sv)));
2046 Perl_dump_indent(aTHX_ level, file, " DIRP = 0x%"UVxf"\n", PTR2UV(IoDIRP(sv)));
2047 Perl_dump_indent(aTHX_ level, file, " LINES = %"IVdf"\n", (IV)IoLINES(sv));
2048 Perl_dump_indent(aTHX_ level, file, " PAGE = %"IVdf"\n", (IV)IoPAGE(sv));
2049 Perl_dump_indent(aTHX_ level, file, " PAGE_LEN = %"IVdf"\n", (IV)IoPAGE_LEN(sv));
2050 Perl_dump_indent(aTHX_ level, file, " LINES_LEFT = %"IVdf"\n", (IV)IoLINES_LEFT(sv));
27533608 2051 if (IoTOP_NAME(sv))
cea2e8a9 2052 Perl_dump_indent(aTHX_ level, file, " TOP_NAME = \"%s\"\n", IoTOP_NAME(sv));
9ba1f565
NC
2053 if (!IoTOP_GV(sv) || SvTYPE(IoTOP_GV(sv)) == SVt_PVGV)
2054 do_gv_dump (level, file, " TOP_GV", IoTOP_GV(sv));
2055 else {
2056 Perl_dump_indent(aTHX_ level, file, " TOP_GV = 0x%"UVxf"\n",
2057 PTR2UV(IoTOP_GV(sv)));
ad64d0ec
NC
2058 do_sv_dump (level+1, file, MUTABLE_SV(IoTOP_GV(sv)), nest+1,
2059 maxnest, dumpops, pvlim);
9ba1f565
NC
2060 }
2061 /* Source filters hide things that are not GVs in these three, so let's
2062 be careful out there. */
27533608 2063 if (IoFMT_NAME(sv))
cea2e8a9 2064 Perl_dump_indent(aTHX_ level, file, " FMT_NAME = \"%s\"\n", IoFMT_NAME(sv));
9ba1f565
NC
2065 if (!IoFMT_GV(sv) || SvTYPE(IoFMT_GV(sv)) == SVt_PVGV)
2066 do_gv_dump (level, file, " FMT_GV", IoFMT_GV(sv));
2067 else {
2068 Perl_dump_indent(aTHX_ level, file, " FMT_GV = 0x%"UVxf"\n",
2069 PTR2UV(IoFMT_GV(sv)));
ad64d0ec
NC
2070 do_sv_dump (level+1, file, MUTABLE_SV(IoFMT_GV(sv)), nest+1,
2071 maxnest, dumpops, pvlim);
9ba1f565 2072 }
27533608 2073 if (IoBOTTOM_NAME(sv))
cea2e8a9 2074 Perl_dump_indent(aTHX_ level, file, " BOTTOM_NAME = \"%s\"\n", IoBOTTOM_NAME(sv));
9ba1f565
NC
2075 if (!IoBOTTOM_GV(sv) || SvTYPE(IoBOTTOM_GV(sv)) == SVt_PVGV)
2076 do_gv_dump (level, file, " BOTTOM_GV", IoBOTTOM_GV(sv));
2077 else {
2078 Perl_dump_indent(aTHX_ level, file, " BOTTOM_GV = 0x%"UVxf"\n",
2079 PTR2UV(IoBOTTOM_GV(sv)));
ad64d0ec
NC
2080 do_sv_dump (level+1, file, MUTABLE_SV(IoBOTTOM_GV(sv)), nest+1,
2081 maxnest, dumpops, pvlim);
9ba1f565 2082 }
27533608 2083 if (isPRINT(IoTYPE(sv)))
cea2e8a9 2084 Perl_dump_indent(aTHX_ level, file, " TYPE = '%c'\n", IoTYPE(sv));
27533608 2085 else
cea2e8a9 2086 Perl_dump_indent(aTHX_ level, file, " TYPE = '\\%o'\n", IoTYPE(sv));
57def98f 2087 Perl_dump_indent(aTHX_ level, file, " FLAGS = 0x%"UVxf"\n", (UV)IoFLAGS(sv));
3967c732 2088 break;
206ee256 2089 case SVt_REGEXP:
d63e6659
DM
2090 {
2091 struct regexp * const r = (struct regexp *)SvANY(sv);
2092 flags = RX_EXTFLAGS((REGEXP*)sv);
2093 sv_setpv(d,"");
2094 append_flags(d, flags, regexp_flags_names);
2095 if (*(SvEND(d) - 1) == ',') {
2096 SvCUR_set(d, SvCUR(d) - 1);
2097 SvPVX(d)[SvCUR(d)] = '\0';
2098 }
2099 Perl_dump_indent(aTHX_ level, file, " EXTFLAGS = 0x%"UVxf" (%s)\n",
2100 (UV)flags, SvPVX_const(d));
2101 Perl_dump_indent(aTHX_ level, file, " INTFLAGS = 0x%"UVxf"\n",
2102 (UV)(r->intflags));
2103 Perl_dump_indent(aTHX_ level, file, " NPARENS = %"UVuf"\n",
2104 (UV)(r->nparens));
2105 Perl_dump_indent(aTHX_ level, file, " LASTPAREN = %"UVuf"\n",
2106 (UV)(r->lastparen));
2107 Perl_dump_indent(aTHX_ level, file, " LASTCLOSEPAREN = %"UVuf"\n",
2108 (UV)(r->lastcloseparen));
2109 Perl_dump_indent(aTHX_ level, file, " MINLEN = %"IVdf"\n",
2110 (IV)(r->minlen));
2111 Perl_dump_indent(aTHX_ level, file, " MINLENRET = %"IVdf"\n",
2112 (IV)(r->minlenret));
2113 Perl_dump_indent(aTHX_ level, file, " GOFS = %"UVuf"\n",
2114 (UV)(r->gofs));
2115 Perl_dump_indent(aTHX_ level, file, " PRE_PREFIX = %"UVuf"\n",
2116 (UV)(r->pre_prefix));
2117 Perl_dump_indent(aTHX_ level, file, " SEEN_EVALS = %"UVuf"\n",
2118 (UV)(r->seen_evals));
2119 Perl_dump_indent(aTHX_ level, file, " SUBLEN = %"IVdf"\n",
2120 (IV)(r->sublen));
2121 if (r->subbeg)
2122 Perl_dump_indent(aTHX_ level, file, " SUBBEG = 0x%"UVxf" %s\n",
2123 PTR2UV(r->subbeg),
2124 pv_display(d, r->subbeg, r->sublen, 50, pvlim));
2125 else
2126 Perl_dump_indent(aTHX_ level, file, " SUBBEG = 0x0\n");
2127 Perl_dump_indent(aTHX_ level, file, " ENGINE = 0x%"UVxf"\n",
2128 PTR2UV(r->engine));
2129 Perl_dump_indent(aTHX_ level, file, " MOTHER_RE = 0x%"UVxf"\n",
2130 PTR2UV(r->mother_re));
2131 Perl_dump_indent(aTHX_ level, file, " PAREN_NAMES = 0x%"UVxf"\n",
2132 PTR2UV(r->paren_names));
2133 Perl_dump_indent(aTHX_ level, file, " SUBSTRS = 0x%"UVxf"\n",
2134 PTR2UV(r->substrs));
2135 Perl_dump_indent(aTHX_ level, file, " PPRIVATE = 0x%"UVxf"\n",
2136 PTR2UV(r->pprivate));
2137 Perl_dump_indent(aTHX_ level, file, " OFFS = 0x%"UVxf"\n",
2138 PTR2UV(r->offs));
2139#ifdef PERL_OLD_COPY_ON_WRITE
2140 Perl_dump_indent(aTHX_ level, file, " SAVED_COPY = 0x%"UVxf"\n",
2141 PTR2UV(r->saved_copy));
2142#endif
2143 }
206ee256 2144 break;
3967c732 2145 }
cea89e20 2146 SvREFCNT_dec(d);
3967c732
JD
2147}
2148
2149void
864dbfa3 2150Perl_sv_dump(pTHX_ SV *sv)
3967c732 2151{
97aff369 2152 dVAR;
7918f24d
NC
2153
2154 PERL_ARGS_ASSERT_SV_DUMP;
2155
d1029faa
JP
2156 if (SvROK(sv))
2157 do_sv_dump(0, Perl_debug_log, sv, 0, 4, 0, 0);
2158 else
2159 do_sv_dump(0, Perl_debug_log, sv, 0, 0, 0, 0);
8d063cd8 2160}
bd16a5f0
IZ
2161
2162int
2163Perl_runops_debug(pTHX)
2164{
97aff369 2165 dVAR;
bd16a5f0 2166 if (!PL_op) {
9b387841 2167 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING), "NULL OP IN RUN");
bd16a5f0
IZ
2168 return 0;
2169 }
2170
9f3673fb 2171 DEBUG_l(Perl_deb(aTHX_ "Entering new RUNOPS level\n"));
bd16a5f0 2172 do {
bd16a5f0 2173 if (PL_debug) {
b9ac451d 2174 if (PL_watchaddr && (*PL_watchaddr != PL_watchok))
bd16a5f0
IZ
2175 PerlIO_printf(Perl_debug_log,
2176 "WARNING: %"UVxf" changed from %"UVxf" to %"UVxf"\n",
2177 PTR2UV(PL_watchaddr), PTR2UV(PL_watchok),
2178 PTR2UV(*PL_watchaddr));
d6721266
DM
2179 if (DEBUG_s_TEST_) {
2180 if (DEBUG_v_TEST_) {
2181 PerlIO_printf(Perl_debug_log, "\n");
2182 deb_stack_all();
2183 }
2184 else
2185 debstack();
2186 }
2187
2188
bd16a5f0
IZ
2189 if (DEBUG_t_TEST_) debop(PL_op);
2190 if (DEBUG_P_TEST_) debprof(PL_op);
2191 }
16c91539 2192 } while ((PL_op = PL_op->op_ppaddr(aTHX)));
9f3673fb 2193 DEBUG_l(Perl_deb(aTHX_ "leaving RUNOPS level\n"));
bd16a5f0
IZ
2194
2195 TAINT_NOT;
2196 return 0;
2197}
2198
2199I32
6867be6d 2200Perl_debop(pTHX_ const OP *o)
bd16a5f0 2201{
97aff369 2202 dVAR;
7918f24d
NC
2203
2204 PERL_ARGS_ASSERT_DEBOP;
2205
1045810a
IZ
2206 if (CopSTASH_eq(PL_curcop, PL_debstash) && !DEBUG_J_TEST_)
2207 return 0;
2208
bd16a5f0
IZ
2209 Perl_deb(aTHX_ "%s", OP_NAME(o));
2210 switch (o->op_type) {
2211 case OP_CONST:
996c9baa 2212 case OP_HINTSEVAL:
6cefa69e 2213 /* With ITHREADS, consts are stored in the pad, and the right pad
7367e658 2214 * may not be active here, so check.
6cefa69e 2215 * Looks like only during compiling the pads are illegal.
7367e658 2216 */
6cefa69e
RU
2217#ifdef USE_ITHREADS
2218 if ((((SVOP*)o)->op_sv) || !IN_PERL_COMPILETIME)
2219#endif
7367e658 2220 PerlIO_printf(Perl_debug_log, "(%s)", SvPEEK(cSVOPo_sv));
bd16a5f0
IZ
2221 break;
2222 case OP_GVSV:
2223 case OP_GV:
2224 if (cGVOPo_gv) {
b9ac451d 2225 SV * const sv = newSV(0);
3b721df9 2226#ifdef PERL_MAD
84021b46 2227 /* FIXME - is this making unwarranted assumptions about the
3b721df9
NC
2228 UTF-8 cleanliness of the dump file handle? */
2229 SvUTF8_on(sv);
2230#endif
bd61b366 2231 gv_fullname3(sv, cGVOPo_gv, NULL);
93524f2b 2232 PerlIO_printf(Perl_debug_log, "(%s)", SvPV_nolen_const(sv));
bd16a5f0
IZ
2233 SvREFCNT_dec(sv);
2234 }
2235 else
2236 PerlIO_printf(Perl_debug_log, "(NULL)");
2237 break;
2238 case OP_PADSV:
2239 case OP_PADAV:
2240 case OP_PADHV:
a3b680e6 2241 {
bd16a5f0 2242 /* print the lexical's name */
b9ac451d 2243 CV * const cv = deb_curcv(cxstack_ix);
a3b680e6 2244 SV *sv;
bd16a5f0 2245 if (cv) {
b9ac451d 2246 AV * const padlist = CvPADLIST(cv);
502c6561 2247 AV * const comppad = MUTABLE_AV(*av_fetch(padlist, 0, FALSE));
bd16a5f0
IZ
2248 sv = *av_fetch(comppad, o->op_targ, FALSE);
2249 } else
a0714e2c 2250 sv = NULL;
bd16a5f0 2251 if (sv)
b9ac451d 2252 PerlIO_printf(Perl_debug_log, "(%s)", SvPV_nolen_const(sv));
bd16a5f0 2253 else
b9ac451d 2254 PerlIO_printf(Perl_debug_log, "[%"UVuf"]", (UV)o->op_targ);
a3b680e6 2255 }
bd16a5f0
IZ
2256 break;
2257 default:
091ab601 2258 break;
bd16a5f0
IZ
2259 }
2260 PerlIO_printf(Perl_debug_log, "\n");
2261 return 0;
2262}
2263
2264STATIC CV*
61f9802b 2265S_deb_curcv(pTHX_ const I32 ix)
bd16a5f0 2266{
97aff369 2267 dVAR;
b9ac451d 2268 const PERL_CONTEXT * const cx = &cxstack[ix];
bd16a5f0
IZ
2269 if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT)
2270 return cx->blk_sub.cv;
2271 else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
2272 return PL_compcv;
2273 else if (ix == 0 && PL_curstackinfo->si_type == PERLSI_MAIN)
2274 return PL_main_cv;
2275 else if (ix <= 0)
601f1833 2276 return NULL;
bd16a5f0
IZ
2277 else
2278 return deb_curcv(ix - 1);
2279}
2280
2281void
2282Perl_watch(pTHX_ char **addr)
2283{
97aff369 2284 dVAR;
7918f24d
NC
2285
2286 PERL_ARGS_ASSERT_WATCH;
2287
bd16a5f0
IZ
2288 PL_watchaddr = addr;
2289 PL_watchok = *addr;
2290 PerlIO_printf(Perl_debug_log, "WATCHING, %"UVxf" is currently %"UVxf"\n",
2291 PTR2UV(PL_watchaddr), PTR2UV(PL_watchok));
2292}
2293
2294STATIC void
e1ec3a88 2295S_debprof(pTHX_ const OP *o)
bd16a5f0 2296{
97aff369 2297 dVAR;
7918f24d
NC
2298
2299 PERL_ARGS_ASSERT_DEBPROF;
2300
61f9802b 2301 if (!DEBUG_J_TEST_ && CopSTASH_eq(PL_curcop, PL_debstash))
1045810a 2302 return;
bd16a5f0 2303 if (!PL_profiledata)
a02a5408 2304 Newxz(PL_profiledata, MAXO, U32);
bd16a5f0
IZ
2305 ++PL_profiledata[o->op_type];
2306}
2307
2308void
2309Perl_debprofdump(pTHX)
2310{
97aff369 2311 dVAR;
bd16a5f0
IZ
2312 unsigned i;
2313 if (!PL_profiledata)
2314 return;
2315 for (i = 0; i < MAXO; i++) {
2316 if (PL_profiledata[i])
2317 PerlIO_printf(Perl_debug_log,
2318 "%5lu %s\n", (unsigned long)PL_profiledata[i],
2319 PL_op_name[i]);
2320 }
2321}
66610fdd 2322
3b721df9
NC
2323#ifdef PERL_MAD
2324/*
2325 * XML variants of most of the above routines
2326 */
2327
4136a0f7 2328STATIC void
3b721df9
NC
2329S_xmldump_attr(pTHX_ I32 level, PerlIO *file, const char* pat, ...)
2330{
2331 va_list args;
7918f24d
NC
2332
2333 PERL_ARGS_ASSERT_XMLDUMP_ATTR;
2334
3b721df9
NC
2335 PerlIO_printf(file, "\n ");
2336 va_start(args, pat);
2337 xmldump_vindent(level, file, pat, &args);
2338 va_end(args);
2339}
2340
2341
2342void
2343Perl_xmldump_indent(pTHX_ I32 level, PerlIO *file, const char* pat, ...)
2344{
2345 va_list args;
7918f24d 2346 PERL_ARGS_ASSERT_XMLDUMP_INDENT;
3b721df9
NC
2347 va_start(args, pat);
2348 xmldump_vindent(level, file, pat, &args);
2349 va_end(args);
2350}
2351
2352void
2353Perl_xmldump_vindent(pTHX_ I32 level, PerlIO *file, const char* pat, va_list *args)
2354{
7918f24d
NC
2355 PERL_ARGS_ASSERT_XMLDUMP_VINDENT;
2356
3b721df9
NC
2357 PerlIO_printf(file, "%*s", (int)(level*PL_dumpindent), "");
2358 PerlIO_vprintf(file, pat, *args);
2359}
2360
2361void
2362Perl_xmldump_all(pTHX)
2363{
f0e3f042
CS
2364 xmldump_all_perl(FALSE);
2365}
2366
2367void
0190d5ef 2368Perl_xmldump_all_perl(pTHX_ bool justperl PERL_UNUSED_DECL)
f0e3f042 2369{
3b721df9
NC
2370 PerlIO_setlinebuf(PL_xmlfp);
2371 if (PL_main_root)
2372 op_xmldump(PL_main_root);
0190d5ef
CS
2373 /* someday we might call this, when it outputs XML: */
2374 /* xmldump_packsubs_perl(PL_defstash, justperl); */
3b721df9
NC
2375 if (PL_xmlfp != (PerlIO*)PerlIO_stdout())
2376 PerlIO_close(PL_xmlfp);
2377 PL_xmlfp = 0;
2378}
2379
2380void
2381Perl_xmldump_packsubs(pTHX_ const HV *stash)
2382{
28eb953d 2383 PERL_ARGS_ASSERT_XMLDUMP_PACKSUBS;
3ab0c9fa
NC
2384 xmldump_packsubs_perl(stash, FALSE);
2385}
2386
2387void
2388Perl_xmldump_packsubs_perl(pTHX_ const HV *stash, bool justperl)
2389{
3b721df9
NC
2390 I32 i;
2391 HE *entry;
2392
28eb953d 2393 PERL_ARGS_ASSERT_XMLDUMP_PACKSUBS_PERL;
7918f24d 2394
3b721df9
NC
2395 if (!HvARRAY(stash))
2396 return;
2397 for (i = 0; i <= (I32) HvMAX(stash); i++) {
2398 for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
159b6efe 2399 GV *gv = MUTABLE_GV(HeVAL(entry));
3b721df9
NC
2400 HV *hv;
2401 if (SvTYPE(gv) != SVt_PVGV || !GvGP(gv))
2402 continue;
2403 if (GvCVu(gv))
3ab0c9fa 2404 xmldump_sub_perl(gv, justperl);
3b721df9
NC
2405 if (GvFORM(gv))
2406 xmldump_form(gv);
2407 if (HeKEY(entry)[HeKLEN(entry)-1] == ':'
2408 && (hv = GvHV(gv)) && hv != PL_defstash)
3ab0c9fa 2409 xmldump_packsubs_perl(hv, justperl); /* nested package */
3b721df9
NC
2410 }
2411 }
2412}
2413
2414void
2415Perl_xmldump_sub(pTHX_ const GV *gv)
2416{
28eb953d 2417 PERL_ARGS_ASSERT_XMLDUMP_SUB;
f0e3f042
CS
2418 xmldump_sub_perl(gv, FALSE);
2419}
2420
2421void
2422Perl_xmldump_sub_perl(pTHX_ const GV *gv, bool justperl)
2423{
2424 SV * sv;
3b721df9 2425
28eb953d 2426 PERL_ARGS_ASSERT_XMLDUMP_SUB_PERL;
7918f24d 2427
f0e3f042
CS
2428 if (justperl && (CvISXSUB(GvCV(gv)) || !CvROOT(GvCV(gv))))
2429 return;
2430
2431 sv = sv_newmortal();
1a9a51d4 2432 gv_fullname3(sv, gv, NULL);
3b721df9
NC
2433 Perl_xmldump_indent(aTHX_ 0, PL_xmlfp, "\nSUB %s = ", SvPVX(sv));
2434 if (CvXSUB(GvCV(gv)))
2435 Perl_xmldump_indent(aTHX_ 0, PL_xmlfp, "(xsub 0x%"UVxf" %d)\n",
2436 PTR2UV(CvXSUB(GvCV(gv))),
2437 (int)CvXSUBANY(GvCV(gv)).any_i32);
2438 else if (CvROOT(GvCV(gv)))
2439 op_xmldump(CvROOT(GvCV(gv)));
2440 else
2441 Perl_xmldump_indent(aTHX_ 0, PL_xmlfp, "<undef>\n");
2442}
2443
2444void
2445Perl_xmldump_form(pTHX_ const GV *gv)
2446{
61f9802b 2447 SV * const sv = sv_newmortal();
3b721df9 2448
7918f24d
NC
2449 PERL_ARGS_ASSERT_XMLDUMP_FORM;
2450
1a9a51d4 2451 gv_fullname3(sv, gv, NULL);
3b721df9
NC
2452 Perl_xmldump_indent(aTHX_ 0, PL_xmlfp, "\nFORMAT %s = ", SvPVX(sv));
2453 if (CvROOT(GvFORM(gv)))
2454 op_xmldump(CvROOT(GvFORM(gv)));
2455 else
2456 Perl_xmldump_indent(aTHX_ 0, PL_xmlfp, "<undef>\n");
2457}
2458
2459void
2460Perl_xmldump_eval(pTHX)
2461{
2462 op_xmldump(PL_eval_root);
2463}
2464
2465char *
2466Perl_sv_catxmlsv(pTHX_ SV *dsv, SV *ssv)
2467{
7918f24d 2468 PERL_ARGS_ASSERT_SV_CATXMLSV;
3b721df9
NC
2469 return sv_catxmlpvn(dsv, SvPVX(ssv), SvCUR(ssv), SvUTF8(ssv));
2470}
2471
2472char *
9dcc53ea
Z
2473Perl_sv_catxmlpv(pTHX_ SV *dsv, const char *pv, int utf8)
2474{
2475 PERL_ARGS_ASSERT_SV_CATXMLPV;
2476 return sv_catxmlpvn(dsv, pv, strlen(pv), utf8);
2477}
2478
2479char *
20f84293 2480Perl_sv_catxmlpvn(pTHX_ SV *dsv, const char *pv, STRLEN len, int utf8)
3b721df9
NC
2481{
2482 unsigned int c;
61f9802b 2483 const char * const e = pv + len;
20f84293 2484 const char * const start = pv;
3b721df9
NC
2485 STRLEN dsvcur;
2486 STRLEN cl;
2487
7918f24d
NC
2488 PERL_ARGS_ASSERT_SV_CATXMLPVN;
2489
76f68e9b 2490 sv_catpvs(dsv,"");
3b721df9
NC
2491 dsvcur = SvCUR(dsv); /* in case we have to restart */
2492
2493 retry:
2494 while (pv < e) {
2495 if (utf8) {
2496 c = utf8_to_uvchr((U8*)pv, &cl);
2497 if (cl == 0) {
2498 SvCUR(dsv) = dsvcur;
2499 pv = start;
2500 utf8 = 0;
2501 goto retry;
2502 }
2503 }
2504 else
2505 c = (*pv & 255);
2506
2507 switch (c) {
2508 case 0x00:
2509 case 0x01:
2510 case 0x02:
2511 case 0x03:
2512 case 0x04:
2513 case 0x05:
2514 case 0x06:
2515 case 0x07:
2516 case 0x08:
2517 case 0x0b:
2518 case 0x0c:
2519 case 0x0e:
2520 case 0x0f:
2521 case 0x10:
2522 case 0x11:
2523 case 0x12:
2524 case 0x13:
2525 case 0x14:
2526 case 0x15:
2527 case 0x16:
2528 case 0x17:
2529 case 0x18:
2530 case 0x19:
2531 case 0x1a:
2532 case 0x1b:
2533 case 0x1c:
2534 case 0x1d:
2535 case 0x1e:
2536 case 0x1f:
2537 case 0x7f:
2538 case 0x80:
2539 case 0x81:
2540 case 0x82:
2541 case 0x83:
2542 case 0x84:
2543 case 0x86:
2544 case 0x87:
2545 case 0x88:
2546 case 0x89:
2547 case 0x90:
2548 case 0x91:
2549 case 0x92:
2550 case 0x93:
2551 case 0x94:
2552 case 0x95:
2553 case 0x96:
2554 case 0x97:
2555 case 0x98:
2556 case 0x99:
2557 case 0x9a:
2558 case 0x9b:
2559 case 0x9c:
2560 case 0x9d:
2561 case 0x9e:
2562 case 0x9f:
2563 Perl_sv_catpvf(aTHX_ dsv, "STUPIDXML(#x%X)", c);
2564 break;
2565 case '<':
f3a2811a 2566 sv_catpvs(dsv, "&lt;");
3b721df9
NC
2567 break;
2568 case '>':
f3a2811a 2569 sv_catpvs(dsv, "&gt;");
3b721df9
NC
2570 break;
2571 case '&':
f3a2811a 2572 sv_catpvs(dsv, "&amp;");
3b721df9
NC
2573 break;
2574 case '"':
49de0815 2575 sv_catpvs(dsv, "&#34;");
3b721df9
NC
2576 break;
2577 default:
2578 if (c < 0xD800) {
2579 if (c < 32 || c > 127) {
2580 Perl_sv_catpvf(aTHX_ dsv, "&#x%X;", c);
2581 }
2582 else {
5e7aa789
NC
2583 const char string = (char) c;
2584 sv_catpvn(dsv, &string, 1);
3b721df9
NC
2585 }
2586 break;
2587 }
2588 if ((c >= 0xD800 && c <= 0xDB7F) ||
2589 (c >= 0xDC00 && c <= 0xDFFF) ||
2590 (c >= 0xFFF0 && c <= 0xFFFF) ||
2591 c > 0x10ffff)
2592 Perl_sv_catpvf(aTHX_ dsv, "STUPIDXML(#x%X)", c);
2593 else
2594 Perl_sv_catpvf(aTHX_ dsv, "&#x%X;", c);
2595 }
2596
2597 if (utf8)
2598 pv += UTF8SKIP(pv);
2599 else
2600 pv++;
2601 }
2602
2603 return SvPVX(dsv);
2604}
2605
2606char *
2607Perl_sv_xmlpeek(pTHX_ SV *sv)
2608{
61f9802b 2609 SV * const t = sv_newmortal();
3b721df9
NC
2610 STRLEN n_a;
2611 int unref = 0;
2612
7918f24d
NC
2613 PERL_ARGS_ASSERT_SV_XMLPEEK;
2614
3b721df9 2615 sv_utf8_upgrade(t);
76f68e9b 2616 sv_setpvs(t, "");
3b721df9
NC
2617 /* retry: */
2618 if (!sv) {
2619 sv_catpv(t, "VOID=\"\"");
2620 goto finish;
2621 }
ad64d0ec 2622 else if (sv == (const SV *)0x55555555 || SvTYPE(sv) == 'U') {
3b721df9
NC
2623 sv_catpv(t, "WILD=\"\"");
2624 goto finish;
2625 }
2626 else if (sv == &PL_sv_undef || sv == &PL_sv_no || sv == &PL_sv_yes || sv == &PL_sv_placeholder) {
2627 if (sv == &PL_sv_undef) {
2628 sv_catpv(t, "SV_UNDEF=\"1\"");
2629 if (!(SvFLAGS(sv) & (SVf_OK|SVf_OOK|SVs_OBJECT|
2630 SVs_GMG|SVs_SMG|SVs_RMG)) &&
2631 SvREADONLY(sv))
2632 goto finish;
2633 }
2634 else if (sv == &PL_sv_no) {
2635 sv_catpv(t, "SV_NO=\"1\"");
2636 if (!(SvFLAGS(sv) & (SVf_ROK|SVf_OOK|SVs_OBJECT|
2637 SVs_GMG|SVs_SMG|SVs_RMG)) &&
2638 !(~SvFLAGS(sv) & (SVf_POK|SVf_NOK|SVf_READONLY|
2639 SVp_POK|SVp_NOK)) &&
2640 SvCUR(sv) == 0 &&
2641 SvNVX(sv) == 0.0)
2642 goto finish;
2643 }
2644 else if (sv == &PL_sv_yes) {
2645 sv_catpv(t, "SV_YES=\"1\"");
2646 if (!(SvFLAGS(sv) & (SVf_ROK|SVf_OOK|SVs_OBJECT|
2647 SVs_GMG|SVs_SMG|SVs_RMG)) &&
2648 !(~SvFLAGS(sv) & (SVf_POK|SVf_NOK|SVf_READONLY|
2649 SVp_POK|SVp_NOK)) &&
2650 SvCUR(sv) == 1 &&
2651 SvPVX(sv) && *SvPVX(sv) == '1' &&
2652 SvNVX(sv) == 1.0)
2653 goto finish;
2654 }
2655 else {
2656 sv_catpv(t, "SV_PLACEHOLDER=\"1\"");
2657 if (!(SvFLAGS(sv) & (SVf_OK|SVf_OOK|SVs_OBJECT|
2658 SVs_GMG|SVs_SMG|SVs_RMG)) &&
2659 SvREADONLY(sv))
2660 goto finish;
2661 }
2662 sv_catpv(t, " XXX=\"\" ");
2663 }
2664 else if (SvREFCNT(sv) == 0) {
2665 sv_catpv(t, " refcnt=\"0\"");
2666 unref++;
2667 }
2668 else if (DEBUG_R_TEST_) {
2669 int is_tmp = 0;
2670 I32 ix;
2671 /* is this SV on the tmps stack? */
2672 for (ix=PL_tmps_ix; ix>=0; ix--) {
2673 if (PL_tmps_stack[ix] == sv) {
2674 is_tmp = 1;
2675 break;
2676 }
2677 }
2678 if (SvREFCNT(sv) > 1)
2679 Perl_sv_catpvf(aTHX_ t, " DRT=\"<%"UVuf"%s>\"", (UV)SvREFCNT(sv),
2680 is_tmp ? "T" : "");
2681 else if (is_tmp)
2682 sv_catpv(t, " DRT=\"<T>\"");
2683 }
2684
2685 if (SvROK(sv)) {
2686 sv_catpv(t, " ROK=\"\"");
2687 }
2688 switch (SvTYPE(sv)) {
2689 default:
2690 sv_catpv(t, " FREED=\"1\"");
2691 goto finish;
2692
2693 case SVt_NULL:
2694 sv_catpv(t, " UNDEF=\"1\"");
2695 goto finish;
2696 case SVt_IV:
2697 sv_catpv(t, " IV=\"");
2698 break;
2699 case SVt_NV:
2700 sv_catpv(t, " NV=\"");
2701 break;
3b721df9
NC
2702 case SVt_PV:
2703 sv_catpv(t, " PV=\"");
2704 break;
2705 case SVt_PVIV:
2706 sv_catpv(t, " PVIV=\"");
2707 break;
2708 case SVt_PVNV:
2709 sv_catpv(t, " PVNV=\"");
2710 break;
2711 case SVt_PVMG:
2712 sv_catpv(t, " PVMG=\"");
2713 break;
2714 case SVt_PVLV:
2715 sv_catpv(t, " PVLV=\"");
2716 break;
2717 case SVt_PVAV:
2718 sv_catpv(t, " AV=\"");
2719 break;
2720 case SVt_PVHV:
2721 sv_catpv(t, " HV=\"");
2722 break;
2723 case SVt_PVCV:
2724 if (CvGV(sv))
2725 Perl_sv_catpvf(aTHX_ t, " CV=\"(%s)\"", GvNAME(CvGV(sv)));
2726 else
2727 sv_catpv(t, " CV=\"()\"");
2728 goto finish;
2729 case SVt_PVGV:
2730 sv_catpv(t, " GV=\"");
2731 break;
cecf5685
NC
2732 case SVt_BIND:
2733 sv_catpv(t, " BIND=\"");
3b721df9 2734 break;
d914baab 2735 case SVt_REGEXP:
8619e557 2736 sv_catpv(t, " REGEXP=\"");
4df7f6af 2737 break;
3b721df9
NC
2738 case SVt_PVFM:
2739 sv_catpv(t, " FM=\"");
2740 break;
2741 case SVt_PVIO:
2742 sv_catpv(t, " IO=\"");
2743 break;
2744 }
2745
2746 if (SvPOKp(sv)) {
2747 if (SvPVX(sv)) {
2748 sv_catxmlsv(t, sv);
2749 }
2750 }
2751 else if (SvNOKp(sv)) {
2752 STORE_NUMERIC_LOCAL_SET_STANDARD();
2753 Perl_sv_catpvf(aTHX_ t, "%"NVgf"",SvNVX(sv));
2754 RESTORE_NUMERIC_LOCAL();
2755 }
2756 else if (SvIOKp(sv)) {
2757 if (SvIsUV(sv))
2758 Perl_sv_catpvf(aTHX_ t, "%"UVuf"", (UV)SvUVX(sv));
2759 else
2760 Perl_sv_catpvf(aTHX_ t, "%"IVdf"", (IV)SvIVX(sv));
2761 }
2762 else
2763 sv_catpv(t, "");
2764 sv_catpv(t, "\"");
2765
2766 finish:
61f9802b
AL
2767 while (unref--)
2768 sv_catpv(t, ")");
3b721df9
NC
2769 return SvPV(t, n_a);
2770}
2771
2772void
2773Perl_do_pmop_xmldump(pTHX_ I32 level, PerlIO *file, const PMOP *pm)
2774{
7918f24d
NC
2775 PERL_ARGS_ASSERT_DO_PMOP_XMLDUMP;
2776
3b721df9
NC
2777 if (!pm) {
2778 Perl_xmldump_indent(aTHX_ level, file, "<pmop/>\n");
2779 return;
2780 }
2781 Perl_xmldump_indent(aTHX_ level, file, "<pmop \n");
2782 level++;
2783 if (PM_GETRE(pm)) {
d914baab 2784 REGEXP *const r = PM_GETRE(pm);
643e696a 2785 SV * const tmpsv = newSVpvn_utf8("", 0, TRUE);
ad64d0ec 2786 sv_catxmlsv(tmpsv, MUTABLE_SV(r));
3b721df9
NC
2787 Perl_xmldump_indent(aTHX_ level, file, "pre=\"%s\"\n",
2788 SvPVX(tmpsv));
2789 SvREFCNT_dec(tmpsv);
2790 Perl_xmldump_indent(aTHX_ level, file, "when=\"%s\"\n",
2791 (pm->op_private & OPpRUNTIME) ? "RUN" : "COMP");
2792 }
2793 else
2794 Perl_xmldump_indent(aTHX_ level, file, "pre=\"\" when=\"RUN\"\n");
d914baab 2795 if (pm->op_pmflags || (PM_GETRE(pm) && RX_CHECK_SUBSTR(PM_GETRE(pm)))) {
3df43ef7 2796 SV * const tmpsv = pm_description(pm);
3b721df9
NC
2797 Perl_xmldump_indent(aTHX_ level, file, "pmflags=\"%s\"\n", SvCUR(tmpsv) ? SvPVX(tmpsv) + 1 : "");
2798 SvREFCNT_dec(tmpsv);
2799 }
2800
2801 level--;
20e98b0f 2802 if (pm->op_type != OP_PUSHRE && pm->op_pmreplrootu.op_pmreplroot) {
3b721df9
NC
2803 Perl_xmldump_indent(aTHX_ level, file, ">\n");
2804 Perl_xmldump_indent(aTHX_ level+1, file, "<pm_repl>\n");
20e98b0f 2805 do_op_xmldump(level+2, file, pm->op_pmreplrootu.op_pmreplroot);
3b721df9
NC
2806 Perl_xmldump_indent(aTHX_ level+1, file, "</pm_repl>\n");
2807 Perl_xmldump_indent(aTHX_ level, file, "</pmop>\n");
2808 }
2809 else
2810 Perl_xmldump_indent(aTHX_ level, file, "/>\n");
2811}
2812
2813void
2814Perl_pmop_xmldump(pTHX_ const PMOP *pm)
2815{
2816 do_pmop_xmldump(0, PL_xmlfp, pm);
2817}
2818
2819void
2820Perl_do_op_xmldump(pTHX_ I32 level, PerlIO *file, const OP *o)
2821{
2822 UV seq;
2823 int contents = 0;
7918f24d
NC
2824
2825 PERL_ARGS_ASSERT_DO_OP_XMLDUMP;
2826
3b721df9
NC
2827 if (!o)
2828 return;
2829 sequence(o);
2830 seq = sequence_num(o);
2831 Perl_xmldump_indent(aTHX_ level, file,
2832 "<op_%s seq=\"%"UVuf" -> ",
2833 OP_NAME(o),
2834 seq);
2835 level++;
2836 if (o->op_next)
2837 PerlIO_printf(file, seq ? "%"UVuf"\"" : "(%"UVuf")\"",
2838 sequence_num(o->op_next));
2839 else
2840 PerlIO_printf(file, "DONE\"");
2841
2842 if (o->op_targ) {
2843 if (o->op_type == OP_NULL)
2844 {
2845 PerlIO_printf(file, " was=\"%s\"", PL_op_name[o->op_targ]);
2846 if (o->op_targ == OP_NEXTSTATE)
2847 {
2848 if (CopLINE(cCOPo))
f5992bc4 2849 PerlIO_printf(file, " line=\"%"UVuf"\"",
3b721df9
NC
2850 (UV)CopLINE(cCOPo));
2851 if (CopSTASHPV(cCOPo))
2852 PerlIO_printf(file, " package=\"%s\"",
2853 CopSTASHPV(cCOPo));
4b65a919 2854 if (CopLABEL(cCOPo))
3b721df9 2855 PerlIO_printf(file, " label=\"%s\"",
4b65a919 2856 CopLABEL(cCOPo));
3b721df9
NC
2857 }
2858 }
2859 else
2860 PerlIO_printf(file, " targ=\"%ld\"", (long)o->op_targ);
2861 }
2862#ifdef DUMPADDR
2863 PerlIO_printf(file, " addr=\"0x%"UVxf" => 0x%"UVxf"\"", (UV)o, (UV)o->op_next);
2864#endif
2865 if (o->op_flags) {
76f68e9b 2866 SV * const tmpsv = newSVpvs("");
3b721df9
NC
2867 switch (o->op_flags & OPf_WANT) {
2868 case OPf_WANT_VOID:
2869 sv_catpv(tmpsv, ",VOID");
2870 break;
2871 case OPf_WANT_SCALAR:
2872 sv_catpv(tmpsv, ",SCALAR");
2873 break;
2874 case OPf_WANT_LIST:
2875 sv_catpv(tmpsv, ",LIST");
2876 break;
2877 default:
2878 sv_catpv(tmpsv, ",UNKNOWN");
2879 break;
2880 }
2881 if (o->op_flags & OPf_KIDS)
2882 sv_catpv(tmpsv, ",KIDS");
2883 if (o->op_flags & OPf_PARENS)
2884 sv_catpv(tmpsv, ",PARENS");
2885 if (o->op_flags & OPf_STACKED)
2886 sv_catpv(tmpsv, ",STACKED");
2887 if (o->op_flags & OPf_REF)
2888 sv_catpv(tmpsv, ",REF");
2889 if (o->op_flags & OPf_MOD)
2890 sv_catpv(tmpsv, ",MOD");
2891 if (o->op_flags & OPf_SPECIAL)
2892 sv_catpv(tmpsv, ",SPECIAL");
2893 PerlIO_printf(file, " flags=\"%s\"", SvCUR(tmpsv) ? SvPVX(tmpsv) + 1 : "");
2894 SvREFCNT_dec(tmpsv);
2895 }
2896 if (o->op_private) {
76f68e9b 2897 SV * const tmpsv = newSVpvs("");
3b721df9
NC
2898 if (PL_opargs[o->op_type] & OA_TARGLEX) {
2899 if (o->op_private & OPpTARGET_MY)
2900 sv_catpv(tmpsv, ",TARGET_MY");
2901 }
2902 else if (o->op_type == OP_LEAVESUB ||
2903 o->op_type == OP_LEAVE ||
2904 o->op_type == OP_LEAVESUBLV ||
2905 o->op_type == OP_LEAVEWRITE) {
2906 if (o->op_private & OPpREFCOUNTED)
2907 sv_catpv(tmpsv, ",REFCOUNTED");
2908 }
2909 else if (o->op_type == OP_AASSIGN) {
2910 if (o->op_private & OPpASSIGN_COMMON)
2911 sv_catpv(tmpsv, ",COMMON");
2912 }
2913 else if (o->op_type == OP_SASSIGN) {
2914 if (o->op_private & OPpASSIGN_BACKWARDS)
2915 sv_catpv(tmpsv, ",BACKWARDS");
2916 }
2917 else if (o->op_type == OP_TRANS) {
2918 if (o->op_private & OPpTRANS_SQUASH)
2919 sv_catpv(tmpsv, ",SQUASH");
2920 if (o->op_private & OPpTRANS_DELETE)
2921 sv_catpv(tmpsv, ",DELETE");
2922 if (o->op_private & OPpTRANS_COMPLEMENT)
2923 sv_catpv(tmpsv, ",COMPLEMENT");
2924 if (o->op_private & OPpTRANS_IDENTICAL)
2925 sv_catpv(tmpsv, ",IDENTICAL");
2926 if (o->op_private & OPpTRANS_GROWS)
2927 sv_catpv(tmpsv, ",GROWS");
2928 }
2929 else if (o->op_type == OP_REPEAT) {
2930 if (o->op_private & OPpREPEAT_DOLIST)
2931 sv_catpv(tmpsv, ",DOLIST");
2932 }
2933 else if (o->op_type == OP_ENTERSUB ||
2934 o->op_type == OP_RV2SV ||
2935 o->op_type == OP_GVSV ||
2936 o->op_type == OP_RV2AV ||
2937 o->op_type == OP_RV2HV ||
2938 o->op_type == OP_RV2GV ||
2939 o->op_type == OP_AELEM ||
2940 o->op_type == OP_HELEM )
2941 {
2942 if (o->op_type == OP_ENTERSUB) {
2943 if (o->op_private & OPpENTERSUB_AMPER)
2944 sv_catpv(tmpsv, ",AMPER");
2945 if (o->op_private & OPpENTERSUB_DB)
2946 sv_catpv(tmpsv, ",DB");
2947 if (o->op_private & OPpENTERSUB_HASTARG)
2948 sv_catpv(tmpsv, ",HASTARG");
2949 if (o->op_private & OPpENTERSUB_NOPAREN)
2950 sv_catpv(tmpsv, ",NOPAREN");
2951 if (o->op_private & OPpENTERSUB_INARGS)
2952 sv_catpv(tmpsv, ",INARGS");
2953 if (o->op_private & OPpENTERSUB_NOMOD)
2954 sv_catpv(tmpsv, ",NOMOD");
2955 }
2956 else {
2957 switch (o->op_private & OPpDEREF) {
2958 case OPpDEREF_SV:
2959 sv_catpv(tmpsv, ",SV");
2960 break;
2961 case OPpDEREF_AV:
2962 sv_catpv(tmpsv, ",AV");
2963 break;
2964 case OPpDEREF_HV:
2965 sv_catpv(tmpsv, ",HV");
2966 break;
2967 }
2968 if (o->op_private & OPpMAYBE_LVSUB)
2969 sv_catpv(tmpsv, ",MAYBE_LVSUB");
2970 }
2971 if (o->op_type == OP_AELEM || o->op_type == OP_HELEM) {
2972 if (o->op_private & OPpLVAL_DEFER)
2973 sv_catpv(tmpsv, ",LVAL_DEFER");
2974 }
2975 else {
2976 if (o->op_private & HINT_STRICT_REFS)
2977 sv_catpv(tmpsv, ",STRICT_REFS");
2978 if (o->op_private & OPpOUR_INTRO)
2979 sv_catpv(tmpsv, ",OUR_INTRO");
2980 }
2981 }
2982 else if (o->op_type == OP_CONST) {
2983 if (o->op_private & OPpCONST_BARE)
2984 sv_catpv(tmpsv, ",BARE");
2985 if (o->op_private & OPpCONST_STRICT)
2986 sv_catpv(tmpsv, ",STRICT");
2987 if (o->op_private & OPpCONST_ARYBASE)
2988 sv_catpv(tmpsv, ",ARYBASE");
2989 if (o->op_private & OPpCONST_WARNING)
2990 sv_catpv(tmpsv, ",WARNING");
2991 if (o->op_private & OPpCONST_ENTERED)
2992 sv_catpv(tmpsv, ",ENTERED");
2993 }
2994 else if (o->op_type == OP_FLIP) {
2995 if (o->op_private & OPpFLIP_LINENUM)
2996 sv_catpv(tmpsv, ",LINENUM");
2997 }
2998 else if (o->op_type == OP_FLOP) {
2999 if (o->op_private & OPpFLIP_LINENUM)
3000 sv_catpv(tmpsv, ",LINENUM");
3001 }
3002 else if (o->op_type == OP_RV2CV) {
3003 if (o->op_private & OPpLVAL_INTRO)
3004 sv_catpv(tmpsv, ",INTRO");
3005 }
3006 else if (o->op_type == OP_GV) {
3007 if (o->op_private & OPpEARLY_CV)
3008 sv_catpv(tmpsv, ",EARLY_CV");
3009 }
3010 else if (o->op_type == OP_LIST) {
3011 if (o->op_private & OPpLIST_GUESSED)
3012 sv_catpv(tmpsv, ",GUESSED");
3013 }
3014 else if (o->op_type == OP_DELETE) {
3015 if (o->op_private & OPpSLICE)
3016 sv_catpv(tmpsv, ",SLICE");
3017 }
3018 else if (o->op_type == OP_EXISTS) {
3019 if (o->op_private & OPpEXISTS_SUB)
3020 sv_catpv(tmpsv, ",EXISTS_SUB");
3021 }
3022 else if (o->op_type == OP_SORT) {
3023 if (o->op_private & OPpSORT_NUMERIC)
3024 sv_catpv(tmpsv, ",NUMERIC");
3025 if (o->op_private & OPpSORT_INTEGER)
3026 sv_catpv(tmpsv, ",INTEGER");
3027 if (o->op_private & OPpSORT_REVERSE)
3028 sv_catpv(tmpsv, ",REVERSE");
3029 }
3b721df9
NC
3030 else if (o->op_type == OP_OPEN || o->op_type == OP_BACKTICK) {
3031 if (o->op_private & OPpOPEN_IN_RAW)
3032 sv_catpv(tmpsv, ",IN_RAW");
3033 if (o->op_private & OPpOPEN_IN_CRLF)
3034 sv_catpv(tmpsv, ",IN_CRLF");
3035 if (o->op_private & OPpOPEN_OUT_RAW)
3036 sv_catpv(tmpsv, ",OUT_RAW");
3037 if (o->op_private & OPpOPEN_OUT_CRLF)
3038 sv_catpv(tmpsv, ",OUT_CRLF");
3039 }
3040 else if (o->op_type == OP_EXIT) {
3041 if (o->op_private & OPpEXIT_VMSISH)
3042 sv_catpv(tmpsv, ",EXIT_VMSISH");
3043 if (o->op_private & OPpHUSH_VMSISH)
3044 sv_catpv(tmpsv, ",HUSH_VMSISH");
3045 }
3046 else if (o->op_type == OP_DIE) {
3047 if (o->op_private & OPpHUSH_VMSISH)
3048 sv_catpv(tmpsv, ",HUSH_VMSISH");
3049 }
ef69c8fc 3050 else if (PL_check[o->op_type] != Perl_ck_ftst) {
6ecf81d6 3051 if (OP_IS_FILETEST_ACCESS(o->op_type) && o->op_private & OPpFT_ACCESS)
3b721df9
NC
3052 sv_catpv(tmpsv, ",FT_ACCESS");
3053 if (o->op_private & OPpFT_STACKED)
3054 sv_catpv(tmpsv, ",FT_STACKED");
3055 }
3056 if (o->op_flags & OPf_MOD && o->op_private & OPpLVAL_INTRO)
3057 sv_catpv(tmpsv, ",INTRO");
3058 if (SvCUR(tmpsv))
3059 S_xmldump_attr(aTHX_ level, file, "private=\"%s\"", SvPVX(tmpsv) + 1);
3060 SvREFCNT_dec(tmpsv);
3061 }
3062
3063 switch (o->op_type) {
3064 case OP_AELEMFAST:
3065 if (o->op_flags & OPf_SPECIAL) {
3066 break;
3067 }
3068 case OP_GVSV:
3069 case OP_GV:
3070#ifdef USE_ITHREADS
3071 S_xmldump_attr(aTHX_ level, file, "padix=\"%" IVdf "\"", (IV)cPADOPo->op_padix);
3072#else
3073 if (cSVOPo->op_sv) {
d914baab
NC
3074 SV * const tmpsv1 = newSVpvn_utf8(NULL, 0, TRUE);
3075 SV * const tmpsv2 = newSVpvn_utf8("", 0, TRUE);
3b721df9
NC
3076 char *s;
3077 STRLEN len;
3078 ENTER;
3079 SAVEFREESV(tmpsv1);
3080 SAVEFREESV(tmpsv2);
159b6efe 3081 gv_fullname3(tmpsv1, MUTABLE_GV(cSVOPo->op_sv), NULL);
3b721df9
NC
3082 s = SvPV(tmpsv1,len);
3083 sv_catxmlpvn(tmpsv2, s, len, 1);
3084 S_xmldump_attr(aTHX_ level, file, "gv=\"%s\"", SvPV(tmpsv2, len));
3085 LEAVE;
3086 }
3087 else
3088 S_xmldump_attr(aTHX_ level, file, "gv=\"NULL\"");
3089#endif
3090 break;
3091 case OP_CONST:
996c9baa 3092 case OP_HINTSEVAL:
3b721df9
NC
3093 case OP_METHOD_NAMED:
3094#ifndef USE_ITHREADS
3095 /* with ITHREADS, consts are stored in the pad, and the right pad
3096 * may not be active here, so skip */
3097 S_xmldump_attr(aTHX_ level, file, "%s", sv_xmlpeek(cSVOPo_sv));
3098#endif
3099 break;
3100 case OP_ANONCODE:
3101 if (!contents) {
3102 contents = 1;
3103 PerlIO_printf(file, ">\n");
3104 }
3105 do_op_xmldump(level+1, file, CvROOT(cSVOPo_sv));
3106 break;
3b721df9
NC
3107 case OP_NEXTSTATE:
3108 case OP_DBSTATE:
3109 if (CopLINE(cCOPo))
f5992bc4 3110 S_xmldump_attr(aTHX_ level, file, "line=\"%"UVuf"\"",
3b721df9
NC
3111 (UV)CopLINE(cCOPo));
3112 if (CopSTASHPV(cCOPo))
3113 S_xmldump_attr(aTHX_ level, file, "package=\"%s\"",
3114 CopSTASHPV(cCOPo));
4b65a919 3115 if (CopLABEL(cCOPo))
3b721df9 3116 S_xmldump_attr(aTHX_ level, file, "label=\"%s\"",
4b65a919 3117 CopLABEL(cCOPo));
3b721df9
NC
3118 break;
3119 case OP_ENTERLOOP:
3120 S_xmldump_attr(aTHX_ level, file, "redo=\"");
3121 if (cLOOPo->op_redoop)
3122 PerlIO_printf(file, "%"UVuf"\"", sequence_num(cLOOPo->op_redoop));
3123 else
3124 PerlIO_printf(file, "DONE\"");
3125 S_xmldump_attr(aTHX_ level, file, "next=\"");
3126 if (cLOOPo->op_nextop)
3127 PerlIO_printf(file, "%"UVuf"\"", sequence_num(cLOOPo->op_nextop));
3128 else
3129 PerlIO_printf(file, "DONE\"");
3130 S_xmldump_attr(aTHX_ level, file, "last=\"");
3131 if (cLOOPo->op_lastop)
3132 PerlIO_printf(file, "%"UVuf"\"", sequence_num(cLOOPo->op_lastop));
3133 else
3134 PerlIO_printf(file, "DONE\"");
3135 break;
3136 case OP_COND_EXPR:
3137 case OP_RANGE:
3138 case OP_MAPWHILE:
3139 case OP_GREPWHILE:
3140 case OP_OR:
3141 case OP_AND:
3142 S_xmldump_attr(aTHX_ level, file, "other=\"");
3143 if (cLOGOPo->op_other)
3144 PerlIO_printf(file, "%"UVuf"\"", sequence_num(cLOGOPo->op_other));
3145 else
3146 PerlIO_printf(file, "DONE\"");
3147 break;
3148 case OP_LEAVE:
3149 case OP_LEAVEEVAL:
3150 case OP_LEAVESUB:
3151 case OP_LEAVESUBLV:
3152 case OP_LEAVEWRITE:
3153 case OP_SCOPE:
3154 if (o->op_private & OPpREFCOUNTED)
3155 S_xmldump_attr(aTHX_ level, file, "refcnt=\"%"UVuf"\"", (UV)o->op_targ);
3156 break;
3157 default:
3158 break;
3159 }
3160
3161 if (PL_madskills && o->op_madprop) {
fb2b694a 3162 char prevkey = '\0';
d914baab 3163 SV * const tmpsv = newSVpvn_utf8("", 0, TRUE);
20f84293 3164 const MADPROP* mp = o->op_madprop;
61f9802b 3165
3b721df9
NC
3166 if (!contents) {
3167 contents = 1;
3168 PerlIO_printf(file, ">\n");
3169 }
3170 Perl_xmldump_indent(aTHX_ level, file, "<madprops>\n");
3171 level++;
3172 while (mp) {
3173 char tmp = mp->mad_key;
76f68e9b 3174 sv_setpvs(tmpsv,"\"");
3b721df9
NC
3175 if (tmp)
3176 sv_catxmlpvn(tmpsv, &tmp, 1, 0);
fb2b694a
GG
3177 if ((tmp == '_') || (tmp == '#')) /* '_' '#' whitespace belong to the previous token. */
3178 sv_catxmlpvn(tmpsv, &prevkey, 1, 0);
3179 else
3180 prevkey = tmp;
3b721df9
NC
3181 sv_catpv(tmpsv, "\"");
3182 switch (mp->mad_type) {
3183 case MAD_NULL:
3184 sv_catpv(tmpsv, "NULL");
3185 Perl_xmldump_indent(aTHX_ level, file, "<mad_null key=%s/>\n", SvPVX(tmpsv));
3186 break;
3187 case MAD_PV:
3188 sv_catpv(tmpsv, " val=\"");
3189 sv_catxmlpvn(tmpsv, (char*)mp->mad_val, mp->mad_vlen,1);
3190 sv_catpv(tmpsv, "\"");
3191 Perl_xmldump_indent(aTHX_ level, file, "<mad_pv key=%s/>\n", SvPVX(tmpsv));
3192 break;
3193 case MAD_SV:
3194 sv_catpv(tmpsv, " val=\"");
ad64d0ec 3195 sv_catxmlsv(tmpsv, MUTABLE_SV(mp->mad_val));
3b721df9
NC
3196 sv_catpv(tmpsv, "\"");
3197 Perl_xmldump_indent(aTHX_ level, file, "<mad_sv key=%s/>\n", SvPVX(tmpsv));
3198 break;
3199 case MAD_OP:
3200 if ((OP*)mp->mad_val) {
3201 Perl_xmldump_indent(aTHX_ level, file, "<mad_op key=%s>\n", SvPVX(tmpsv));
3202 do_op_xmldump(level+1, file, (OP*)mp->mad_val);
3203 Perl_xmldump_indent(aTHX_ level, file, "</mad_op>\n");
3204 }
3205 break;
3206 default:
3207 Perl_xmldump_indent(aTHX_ level, file, "<mad_unk key=%s/>\n", SvPVX(tmpsv));
3208 break;
3209 }
3210 mp = mp->mad_next;
3211 }
3212 level--;
3213 Perl_xmldump_indent(aTHX_ level, file, "</madprops>\n");
3214
3215 SvREFCNT_dec(tmpsv);
3216 }
3217
3218 switch (o->op_type) {
3219 case OP_PUSHRE:
3220 case OP_MATCH:
3221 case OP_QR:
3222 case OP_SUBST:
3223 if (!contents) {
3224 contents = 1;
3225 PerlIO_printf(file, ">\n");
3226 }
3227 do_pmop_xmldump(level, file, cPMOPo);
3228 break;
3229 default:
3230 break;
3231 }
3232
3233 if (o->op_flags & OPf_KIDS) {
3234 OP *kid;
3235 if (!contents) {
3236 contents = 1;
3237 PerlIO_printf(file, ">\n");
3238 }
3239 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
3240 do_op_xmldump(level, file, kid);
3241 }
3242
3243 if (contents)
3244 Perl_xmldump_indent(aTHX_ level-1, file, "</op_%s>\n", OP_NAME(o));
3245 else
3246 PerlIO_printf(file, " />\n");
3247}
3248
3249void
3250Perl_op_xmldump(pTHX_ const OP *o)
3251{
7918f24d
NC
3252 PERL_ARGS_ASSERT_OP_XMLDUMP;
3253
3b721df9
NC
3254 do_op_xmldump(0, PL_xmlfp, o);
3255}
3256#endif
3257
66610fdd
RGS
3258/*
3259 * Local variables:
3260 * c-indentation-style: bsd
3261 * c-basic-offset: 4
3262 * indent-tabs-mode: t
3263 * End:
3264 *
37442d52
RGS
3265 * ex: set ts=8 sts=4 sw=4 noet:
3266 */