-
Notifications
You must be signed in to change notification settings - Fork 1
/
dump.c
2603 lines (2349 loc) · 89.1 KB
/
dump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* dump.c
*
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
* 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* 'You have talked long in your sleep, Frodo,' said Gandalf gently, 'and
* it has not been hard for me to read your mind and memory.'
*
* [p.220 of _The Lord of the Rings_, II/i: "Many Meetings"]
*/
/* This file contains utility routines to dump the contents of SV and OP
* structures, as used by command-line options like -Dt and -Dx, and
* by Devel::Peek.
*
* It also holds the debugging version of the runops function.
=head1 Display and Dump functions
*/
#include "EXTERN.h"
#define PERL_IN_DUMP_C
#include "perl.h"
#include "regcomp.h"
static const char* const svtypenames[SVt_LAST] = {
"NULL",
"IV",
"NV",
"PV",
"INVLIST",
"PVIV",
"PVNV",
"PVMG",
"REGEXP",
"PVGV",
"PVLV",
"PVAV",
"PVHV",
"PVCV",
"PVFM",
"PVIO"
};
static const char* const svshorttypenames[SVt_LAST] = {
"UNDEF",
"IV",
"NV",
"PV",
"INVLST",
"PVIV",
"PVNV",
"PVMG",
"REGEXP",
"GV",
"PVLV",
"AV",
"HV",
"CV",
"FM",
"IO"
};
struct flag_to_name {
U32 flag;
const char *name;
};
static void
S_append_flags(pTHX_ SV *sv, U32 flags, const struct flag_to_name *start,
const struct flag_to_name *const end)
{
do {
if (flags & start->flag)
sv_catpv(sv, start->name);
} while (++start < end);
}
#define append_flags(sv, f, flags) \
S_append_flags(aTHX_ (sv), (f), (flags), C_ARRAY_END(flags))
#define generic_pv_escape(sv,s,len,utf8) pv_escape( (sv), (s), (len), \
(len) * (4+UTF8_MAXBYTES) + 1, NULL, \
PERL_PV_ESCAPE_NONASCII | PERL_PV_ESCAPE_DWIM \
| ((utf8) ? PERL_PV_ESCAPE_UNI : 0) )
/*
=for apidoc pv_escape
Escapes at most the first "count" chars of pv and puts the results into
dsv such that the size of the escaped string will not exceed "max" chars
and will not contain any incomplete escape sequences. The number of bytes
escaped will be returned in the STRLEN *escaped parameter if it is not null.
When the dsv parameter is null no escaping actually occurs, but the number
of bytes that would be escaped were it not null will be calculated.
If flags contains PERL_PV_ESCAPE_QUOTE then any double quotes in the string
will also be escaped.
Normally the SV will be cleared before the escaped string is prepared,
but when PERL_PV_ESCAPE_NOCLEAR is set this will not occur.
If PERL_PV_ESCAPE_UNI is set then the input string is treated as UTF-8
if PERL_PV_ESCAPE_UNI_DETECT is set then the input string is scanned
using C<is_utf8_string()> to determine if it is UTF-8.
If PERL_PV_ESCAPE_ALL is set then all input chars will be output
using C<\x01F1> style escapes, otherwise if PERL_PV_ESCAPE_NONASCII is set, only
non-ASCII chars will be escaped using this style; otherwise, only chars above
255 will be so escaped; other non printable chars will use octal or
common escaped patterns like C<\n>.
Otherwise, if PERL_PV_ESCAPE_NOBACKSLASH
then all chars below 255 will be treated as printable and
will be output as literals.
If PERL_PV_ESCAPE_FIRSTCHAR is set then only the first char of the
string will be escaped, regardless of max. If the output is to be in hex,
then it will be returned as a plain hex
sequence. Thus the output will either be a single char,
an octal escape sequence, a special escape like C<\n> or a hex value.
If PERL_PV_ESCAPE_RE is set then the escape char used will be a '%' and
not a '\\'. This is because regexes very often contain backslashed
sequences, whereas '%' is not a particularly common character in patterns.
Returns a pointer to the escaped text as held by dsv.
=cut
*/
#define PV_ESCAPE_OCTBUFSIZE 32
char *
Perl_pv_escape( pTHX_ SV *dsv, char const * const str,
const STRLEN count, const STRLEN max,
STRLEN * const escaped, const U32 flags )
{
const char esc = (flags & PERL_PV_ESCAPE_RE) ? '%' : '\\';
const char dq = (flags & PERL_PV_ESCAPE_QUOTE) ? '"' : esc;
char octbuf[PV_ESCAPE_OCTBUFSIZE] = "%123456789ABCDF";
STRLEN wrote = 0; /* chars written so far */
STRLEN chsize = 0; /* size of data to be written */
STRLEN readsize = 1; /* size of data just read */
bool isuni= flags & PERL_PV_ESCAPE_UNI ? 1 : 0; /* is this UTF-8 */
const char *pv = str;
const char * const end = pv + count; /* end of string */
octbuf[0] = esc;
PERL_ARGS_ASSERT_PV_ESCAPE;
if (dsv && !(flags & PERL_PV_ESCAPE_NOCLEAR)) {
/* This won't alter the UTF-8 flag */
sv_setpvs(dsv, "");
}
if ((flags & PERL_PV_ESCAPE_UNI_DETECT) && is_utf8_string((U8*)pv, count))
isuni = 1;
for ( ; (pv < end && (!max || (wrote < max))) ; pv += readsize ) {
const UV u= (isuni) ? utf8_to_uvchr_buf((U8*)pv, (U8*) end, &readsize) : (U8)*pv;
const U8 c = (U8)u & 0xFF;
if ( ( u > 255 )
|| (flags & PERL_PV_ESCAPE_ALL)
|| (( ! isASCII(u) ) && (flags & (PERL_PV_ESCAPE_NONASCII|PERL_PV_ESCAPE_DWIM))))
{
if (flags & PERL_PV_ESCAPE_FIRSTCHAR)
chsize = my_snprintf( octbuf, PV_ESCAPE_OCTBUFSIZE,
"%"UVxf, u);
else
chsize = my_snprintf( octbuf, PV_ESCAPE_OCTBUFSIZE,
((flags & PERL_PV_ESCAPE_DWIM) && !isuni)
? "%cx%02"UVxf
: "%cx{%02"UVxf"}", esc, u);
} else if (flags & PERL_PV_ESCAPE_NOBACKSLASH) {
chsize = 1;
} else {
if ( (c == dq) || (c == esc) || !isPRINT(c) ) {
chsize = 2;
switch (c) {
case '\\' : /* FALLTHROUGH */
case '%' : if ( c == esc ) {
octbuf[1] = esc;
} else {
chsize = 1;
}
break;
case '\v' : octbuf[1] = 'v'; break;
case '\t' : octbuf[1] = 't'; break;
case '\r' : octbuf[1] = 'r'; break;
case '\n' : octbuf[1] = 'n'; break;
case '\f' : octbuf[1] = 'f'; break;
case '"' :
if ( dq == '"' )
octbuf[1] = '"';
else
chsize = 1;
break;
default:
if ( (flags & PERL_PV_ESCAPE_DWIM) && c != '\0' ) {
chsize = my_snprintf( octbuf, PV_ESCAPE_OCTBUFSIZE,
isuni ? "%cx{%02"UVxf"}" : "%cx%02"UVxf,
esc, u);
}
else if ( (pv+readsize < end) && isDIGIT((U8)*(pv+readsize)) )
chsize = my_snprintf( octbuf, PV_ESCAPE_OCTBUFSIZE,
"%c%03o", esc, c);
else
chsize = my_snprintf( octbuf, PV_ESCAPE_OCTBUFSIZE,
"%c%o", esc, c);
}
} else {
chsize = 1;
}
}
if ( max && (wrote + chsize > max) ) {
break;
} else if (chsize > 1) {
if (dsv)
sv_catpvn(dsv, octbuf, chsize);
wrote += chsize;
} else {
/* If PERL_PV_ESCAPE_NOBACKSLASH is set then non-ASCII bytes
can be appended raw to the dsv. If dsv happens to be
UTF-8 then we need catpvf to upgrade them for us.
Or add a new API call sv_catpvc(). Think about that name, and
how to keep it clear that it's unlike the s of catpvs, which is
really an array of octets, not a string. */
if (dsv)
Perl_sv_catpvf( aTHX_ dsv, "%c", c);
wrote++;
}
if ( flags & PERL_PV_ESCAPE_FIRSTCHAR )
break;
}
if (escaped != NULL)
*escaped= pv - str;
return dsv ? SvPVX(dsv) : NULL;
}
/*
=for apidoc pv_pretty
Converts a string into something presentable, handling escaping via
pv_escape() and supporting quoting and ellipses.
If the PERL_PV_PRETTY_QUOTE flag is set then the result will be
double quoted with any double quotes in the string escaped. Otherwise
if the PERL_PV_PRETTY_LTGT flag is set then the result be wrapped in
angle brackets.
If the PERL_PV_PRETTY_ELLIPSES flag is set and not all characters in
string were output then an ellipsis C<...> will be appended to the
string. Note that this happens AFTER it has been quoted.
If start_color is non-null then it will be inserted after the opening
quote (if there is one) but before the escaped text. If end_color
is non-null then it will be inserted after the escaped text but before
any quotes or ellipses.
Returns a pointer to the prettified text as held by dsv.
=cut
*/
char *
Perl_pv_pretty( pTHX_ SV *dsv, char const * const str, const STRLEN count,
const STRLEN max, char const * const start_color, char const * const end_color,
const U32 flags )
{
const U8 *quotes = (U8*)((flags & PERL_PV_PRETTY_QUOTE) ? "\"\"" :
(flags & PERL_PV_PRETTY_LTGT) ? "<>" : NULL);
STRLEN escaped;
STRLEN max_adjust= 0;
STRLEN orig_cur;
PERL_ARGS_ASSERT_PV_PRETTY;
if (!(flags & PERL_PV_PRETTY_NOCLEAR)) {
/* This won't alter the UTF-8 flag */
sv_setpvs(dsv, "");
}
orig_cur= SvCUR(dsv);
if ( quotes )
Perl_sv_catpvf(aTHX_ dsv, "%c", quotes[0]);
if ( start_color != NULL )
sv_catpv(dsv, start_color);
if ((flags & PERL_PV_PRETTY_EXACTSIZE)) {
if (quotes)
max_adjust += 2;
assert(max > max_adjust);
pv_escape( NULL, str, count, max - max_adjust, &escaped, flags );
if ( (flags & PERL_PV_PRETTY_ELLIPSES) && ( escaped < count ) )
max_adjust += 3;
assert(max > max_adjust);
}
pv_escape( dsv, str, count, max - max_adjust, &escaped, flags | PERL_PV_ESCAPE_NOCLEAR );
if ( end_color != NULL )
sv_catpv(dsv, end_color);
if ( quotes )
Perl_sv_catpvf(aTHX_ dsv, "%c", quotes[1]);
if ( (flags & PERL_PV_PRETTY_ELLIPSES) && ( escaped < count ) )
sv_catpvs(dsv, "...");
if ((flags & PERL_PV_PRETTY_EXACTSIZE)) {
while( SvCUR(dsv) - orig_cur < max )
sv_catpvs(dsv," ");
}
return SvPVX(dsv);
}
/*
=for apidoc pv_display
Similar to
pv_escape(dsv,pv,cur,pvlim,PERL_PV_ESCAPE_QUOTE);
except that an additional "\0" will be appended to the string when
len > cur and pv[cur] is "\0".
Note that the final string may be up to 7 chars longer than pvlim.
=cut
*/
char *
Perl_pv_display(pTHX_ SV *dsv, const char *pv, STRLEN cur, STRLEN len, STRLEN pvlim)
{
PERL_ARGS_ASSERT_PV_DISPLAY;
pv_pretty( dsv, pv, cur, pvlim, NULL, NULL, PERL_PV_PRETTY_DUMP);
if (len > cur && pv[cur] == '\0')
sv_catpvs( dsv, "\\0");
return SvPVX(dsv);
}
char *
Perl_sv_peek(pTHX_ SV *sv)
{
dVAR;
SV * const t = sv_newmortal();
int unref = 0;
U32 type;
sv_setpvs(t, "");
retry:
if (!sv) {
sv_catpv(t, "VOID");
goto finish;
}
else if (sv == (const SV *)0x55555555 || ((char)SvTYPE(sv)) == 'U') {
/* detect data corruption under memory poisoning */
sv_catpv(t, "WILD");
goto finish;
}
else if (sv == &PL_sv_undef || sv == &PL_sv_no || sv == &PL_sv_yes || sv == &PL_sv_placeholder) {
if (sv == &PL_sv_undef) {
sv_catpv(t, "SV_UNDEF");
if (!(SvFLAGS(sv) & (SVf_OK|SVf_OOK|SVs_OBJECT|
SVs_GMG|SVs_SMG|SVs_RMG)) &&
SvREADONLY(sv))
goto finish;
}
else if (sv == &PL_sv_no) {
sv_catpv(t, "SV_NO");
if (!(SvFLAGS(sv) & (SVf_ROK|SVf_OOK|SVs_OBJECT|
SVs_GMG|SVs_SMG|SVs_RMG)) &&
!(~SvFLAGS(sv) & (SVf_POK|SVf_NOK|SVf_READONLY|
SVp_POK|SVp_NOK)) &&
SvCUR(sv) == 0 &&
SvNVX(sv) == 0.0)
goto finish;
}
else if (sv == &PL_sv_yes) {
sv_catpv(t, "SV_YES");
if (!(SvFLAGS(sv) & (SVf_ROK|SVf_OOK|SVs_OBJECT|
SVs_GMG|SVs_SMG|SVs_RMG)) &&
!(~SvFLAGS(sv) & (SVf_POK|SVf_NOK|SVf_READONLY|
SVp_POK|SVp_NOK)) &&
SvCUR(sv) == 1 &&
SvPVX_const(sv) && *SvPVX_const(sv) == '1' &&
SvNVX(sv) == 1.0)
goto finish;
}
else {
sv_catpv(t, "SV_PLACEHOLDER");
if (!(SvFLAGS(sv) & (SVf_OK|SVf_OOK|SVs_OBJECT|
SVs_GMG|SVs_SMG|SVs_RMG)) &&
SvREADONLY(sv))
goto finish;
}
sv_catpv(t, ":");
}
else if (SvREFCNT(sv) == 0) {
sv_catpv(t, "(");
unref++;
}
else if (DEBUG_R_TEST_) {
int is_tmp = 0;
SSize_t ix;
/* is this SV on the tmps stack? */
for (ix=PL_tmps_ix; ix>=0; ix--) {
if (PL_tmps_stack[ix] == sv) {
is_tmp = 1;
break;
}
}
if (SvREFCNT(sv) > 1)
Perl_sv_catpvf(aTHX_ t, "<%"UVuf"%s>", (UV)SvREFCNT(sv),
is_tmp ? "T" : "");
else if (is_tmp)
sv_catpv(t, "<T>");
}
if (SvROK(sv)) {
sv_catpv(t, "\\");
if (SvCUR(t) + unref > 10) {
SvCUR_set(t, unref + 3);
*SvEND(t) = '\0';
sv_catpv(t, "...");
goto finish;
}
sv = SvRV(sv);
goto retry;
}
type = SvTYPE(sv);
if (type == SVt_PVCV) {
SV * const tmp = newSVpvs_flags("", SVs_TEMP);
GV* gvcv = CvGV(sv);
Perl_sv_catpvf(aTHX_ t, "CV(%s)", gvcv
? generic_pv_escape( tmp, GvNAME(gvcv), GvNAMELEN(gvcv), GvNAMEUTF8(gvcv))
: "");
goto finish;
} else if (type < SVt_LAST) {
sv_catpv(t, svshorttypenames[type]);
if (type == SVt_NULL)
goto finish;
} else {
sv_catpv(t, "FREED");
goto finish;
}
if (SvPOKp(sv)) {
if (!SvPVX_const(sv))
sv_catpv(t, "(null)");
else {
SV * const tmp = newSVpvs("");
sv_catpv(t, "(");
if (SvOOK(sv)) {
STRLEN delta;
SvOOK_offset(sv, delta);
Perl_sv_catpvf(aTHX_ t, "[%s]", pv_display(tmp, SvPVX_const(sv)-delta, delta, 0, 127));
}
Perl_sv_catpvf(aTHX_ t, "%s)", pv_display(tmp, SvPVX_const(sv), SvCUR(sv), SvLEN(sv), 127));
if (SvUTF8(sv))
Perl_sv_catpvf(aTHX_ t, " [UTF8 \"%s\"]",
sv_uni_display(tmp, sv, 6 * SvCUR(sv),
UNI_DISPLAY_QQ));
SvREFCNT_dec_NN(tmp);
}
}
else if (SvNOKp(sv)) {
STORE_LC_NUMERIC_UNDERLYING_SET_STANDARD();
Perl_sv_catpvf(aTHX_ t, "(%"NVgf")",SvNVX(sv));
RESTORE_LC_NUMERIC_UNDERLYING();
}
else if (SvIOKp(sv)) {
if (SvIsUV(sv))
Perl_sv_catpvf(aTHX_ t, "(%"UVuf")", (UV)SvUVX(sv));
else
Perl_sv_catpvf(aTHX_ t, "(%"IVdf")", (IV)SvIVX(sv));
}
else
sv_catpv(t, "()");
finish:
while (unref--)
sv_catpv(t, ")");
if (TAINTING_get && sv && SvTAINTED(sv))
sv_catpv(t, " [tainted]");
return SvPV_nolen(t);
}
/*
=head1 Debugging Utilities
*/
void
Perl_dump_indent(pTHX_ I32 level, PerlIO *file, const char* pat, ...)
{
va_list args;
PERL_ARGS_ASSERT_DUMP_INDENT;
va_start(args, pat);
dump_vindent(level, file, pat, &args);
va_end(args);
}
void
Perl_dump_vindent(pTHX_ I32 level, PerlIO *file, const char* pat, va_list *args)
{
PERL_ARGS_ASSERT_DUMP_VINDENT;
PerlIO_printf(file, "%*s", (int)(level*PL_dumpindent), "");
PerlIO_vprintf(file, pat, *args);
}
/*
=for apidoc dump_all
Dumps the entire optree of the current program starting at C<PL_main_root> to
C<STDERR>. Also dumps the optrees for all visible subroutines in
C<PL_defstash>.
=cut
*/
void
Perl_dump_all(pTHX)
{
dump_all_perl(FALSE);
}
void
Perl_dump_all_perl(pTHX_ bool justperl)
{
PerlIO_setlinebuf(Perl_debug_log);
if (PL_main_root)
op_dump(PL_main_root);
dump_packsubs_perl(PL_defstash, justperl);
}
/*
=for apidoc dump_packsubs
Dumps the optrees for all visible subroutines in C<stash>.
=cut
*/
void
Perl_dump_packsubs(pTHX_ const HV *stash)
{
PERL_ARGS_ASSERT_DUMP_PACKSUBS;
dump_packsubs_perl(stash, FALSE);
}
void
Perl_dump_packsubs_perl(pTHX_ const HV *stash, bool justperl)
{
I32 i;
PERL_ARGS_ASSERT_DUMP_PACKSUBS_PERL;
if (!HvARRAY(stash))
return;
for (i = 0; i <= (I32) HvMAX(stash); i++) {
const HE *entry;
for (entry = HvARRAY(stash)[i]; entry; entry = HeNEXT(entry)) {
GV * gv = (GV *)HeVAL(entry);
if (SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV)
/* unfake a fake GV */
(void)CvGV(SvRV(gv));
if (SvTYPE(gv) != SVt_PVGV || !GvGP(gv))
continue;
if (GvCVu(gv))
dump_sub_perl(gv, justperl);
if (GvFORM(gv))
dump_form(gv);
if (HeKEY(entry)[HeKLEN(entry)-1] == ':') {
const HV * const hv = GvHV(gv);
if (hv && (hv != PL_defstash))
dump_packsubs_perl(hv, justperl); /* nested package */
}
}
}
}
void
Perl_dump_sub(pTHX_ const GV *gv)
{
PERL_ARGS_ASSERT_DUMP_SUB;
dump_sub_perl(gv, FALSE);
}
void
Perl_dump_sub_perl(pTHX_ const GV *gv, bool justperl)
{
STRLEN len;
SV * const sv = newSVpvs_flags("", SVs_TEMP);
SV *tmpsv;
const char * name;
PERL_ARGS_ASSERT_DUMP_SUB_PERL;
if (justperl && (CvISXSUB(GvCV(gv)) || !CvROOT(GvCV(gv))))
return;
tmpsv = newSVpvs_flags("", SVs_TEMP);
gv_fullname3(sv, gv, NULL);
name = SvPV_const(sv, len);
Perl_dump_indent(aTHX_ 0, Perl_debug_log, "\nSUB %s = ",
generic_pv_escape(tmpsv, name, len, SvUTF8(sv)));
if (CvISXSUB(GvCV(gv)))
Perl_dump_indent(aTHX_ 0, Perl_debug_log, "(xsub 0x%"UVxf" %d)\n",
PTR2UV(CvXSUB(GvCV(gv))),
(int)CvXSUBANY(GvCV(gv)).any_i32);
else if (CvROOT(GvCV(gv)))
op_dump(CvROOT(GvCV(gv)));
else
Perl_dump_indent(aTHX_ 0, Perl_debug_log, "<undef>\n");
}
void
Perl_dump_form(pTHX_ const GV *gv)
{
SV * const sv = sv_newmortal();
PERL_ARGS_ASSERT_DUMP_FORM;
gv_fullname3(sv, gv, NULL);
Perl_dump_indent(aTHX_ 0, Perl_debug_log, "\nFORMAT %s = ", SvPVX_const(sv));
if (CvROOT(GvFORM(gv)))
op_dump(CvROOT(GvFORM(gv)));
else
Perl_dump_indent(aTHX_ 0, Perl_debug_log, "<undef>\n");
}
void
Perl_dump_eval(pTHX)
{
op_dump(PL_eval_root);
}
void
Perl_do_pmop_dump(pTHX_ I32 level, PerlIO *file, const PMOP *pm)
{
char ch;
PERL_ARGS_ASSERT_DO_PMOP_DUMP;
if (!pm) {
Perl_dump_indent(aTHX_ level, file, "{}\n");
return;
}
Perl_dump_indent(aTHX_ level, file, "{\n");
level++;
if (pm->op_pmflags & PMf_ONCE)
ch = '?';
else
ch = '/';
if (PM_GETRE(pm))
Perl_dump_indent(aTHX_ level, file, "PMf_PRE %c%.*s%c%s\n",
ch,(int)RX_PRELEN(PM_GETRE(pm)), RX_PRECOMP(PM_GETRE(pm)), ch,
(pm->op_private & OPpRUNTIME) ? " (RUNTIME)" : "");
else
Perl_dump_indent(aTHX_ level, file, "PMf_PRE (RUNTIME)\n");
if (pm->op_type != OP_PUSHRE && pm->op_pmreplrootu.op_pmreplroot) {
Perl_dump_indent(aTHX_ level, file, "PMf_REPL = ");
op_dump(pm->op_pmreplrootu.op_pmreplroot);
}
if (pm->op_code_list) {
if (pm->op_pmflags & PMf_CODELIST_PRIVATE) {
Perl_dump_indent(aTHX_ level, file, "CODE_LIST =\n");
do_op_dump(level, file, pm->op_code_list);
}
else
Perl_dump_indent(aTHX_ level, file, "CODE_LIST = 0x%"UVxf"\n",
PTR2UV(pm->op_code_list));
}
if (pm->op_pmflags || (PM_GETRE(pm) && RX_CHECK_SUBSTR(PM_GETRE(pm)))) {
SV * const tmpsv = pm_description(pm);
Perl_dump_indent(aTHX_ level, file, "PMFLAGS = (%s)\n", SvCUR(tmpsv) ? SvPVX_const(tmpsv) + 1 : "");
SvREFCNT_dec_NN(tmpsv);
}
Perl_dump_indent(aTHX_ level-1, file, "}\n");
}
const struct flag_to_name pmflags_flags_names[] = {
{PMf_CONST, ",CONST"},
{PMf_KEEP, ",KEEP"},
{PMf_GLOBAL, ",GLOBAL"},
{PMf_CONTINUE, ",CONTINUE"},
{PMf_RETAINT, ",RETAINT"},
{PMf_EVAL, ",EVAL"},
{PMf_NONDESTRUCT, ",NONDESTRUCT"},
{PMf_HAS_CV, ",HAS_CV"},
{PMf_CODELIST_PRIVATE, ",CODELIST_PRIVATE"},
{PMf_IS_QR, ",IS_QR"}
};
static SV *
S_pm_description(pTHX_ const PMOP *pm)
{
SV * const desc = newSVpvs("");
const REGEXP * const regex = PM_GETRE(pm);
const U32 pmflags = pm->op_pmflags;
PERL_ARGS_ASSERT_PM_DESCRIPTION;
if (pmflags & PMf_ONCE)
sv_catpv(desc, ",ONCE");
#ifdef USE_ITHREADS
if (SvREADONLY(PL_regex_pad[pm->op_pmoffset]))
sv_catpv(desc, ":USED");
#else
if (pmflags & PMf_USED)
sv_catpv(desc, ":USED");
#endif
if (regex) {
if (RX_ISTAINTED(regex))
sv_catpv(desc, ",TAINTED");
if (RX_CHECK_SUBSTR(regex)) {
if (!(RX_INTFLAGS(regex) & PREGf_NOSCAN))
sv_catpv(desc, ",SCANFIRST");
if (RX_EXTFLAGS(regex) & RXf_CHECK_ALL)
sv_catpv(desc, ",ALL");
}
if (RX_EXTFLAGS(regex) & RXf_SKIPWHITE)
sv_catpv(desc, ",SKIPWHITE");
}
append_flags(desc, pmflags, pmflags_flags_names);
return desc;
}
void
Perl_pmop_dump(pTHX_ PMOP *pm)
{
do_pmop_dump(0, Perl_debug_log, pm);
}
/* Return a unique integer to represent the address of op o.
* If it already exists in PL_op_sequence, just return it;
* otherwise add it.
* *** Note that this isn't thread-safe */
STATIC UV
S_sequence_num(pTHX_ const OP *o)
{
dVAR;
SV *op,
**seq;
const char *key;
STRLEN len;
if (!o)
return 0;
op = newSVuv(PTR2UV(o));
sv_2mortal(op);
key = SvPV_const(op, len);
if (!PL_op_sequence)
PL_op_sequence = newHV();
seq = hv_fetch(PL_op_sequence, key, len, 0);
if (seq)
return SvUV(*seq);
(void)hv_store(PL_op_sequence, key, len, newSVuv(++PL_op_seq), 0);
return PL_op_seq;
}
const struct flag_to_name op_flags_names[] = {
{OPf_KIDS, ",KIDS"},
{OPf_PARENS, ",PARENS"},
{OPf_REF, ",REF"},
{OPf_MOD, ",MOD"},
{OPf_STACKED, ",STACKED"},
{OPf_SPECIAL, ",SPECIAL"}
};
void
Perl_do_op_dump(pTHX_ I32 level, PerlIO *file, const OP *o)
{
UV seq;
const OPCODE optype = o->op_type;
PERL_ARGS_ASSERT_DO_OP_DUMP;
Perl_dump_indent(aTHX_ level, file, "{\n");
level++;
seq = sequence_num(o);
if (seq)
PerlIO_printf(file, "%-4"UVuf, seq);
else
PerlIO_printf(file, "????");
PerlIO_printf(file,
"%*sTYPE = %s ===> ",
(int)(PL_dumpindent*level-4), "", OP_NAME(o));
if (o->op_next)
PerlIO_printf(file,
o->op_type == OP_NULL ? "(%"UVuf")\n" : "%"UVuf"\n",
sequence_num(o->op_next));
else
PerlIO_printf(file, "NULL\n");
if (o->op_targ) {
if (optype == OP_NULL) {
Perl_dump_indent(aTHX_ level, file, " (was %s)\n", PL_op_name[o->op_targ]);
}
else
Perl_dump_indent(aTHX_ level, file, "TARG = %ld\n", (long)o->op_targ);
}
#ifdef DUMPADDR
Perl_dump_indent(aTHX_ level, file, "ADDR = 0x%"UVxf" => 0x%"UVxf"\n", (UV)o, (UV)o->op_next);
#endif
if (o->op_flags || o->op_slabbed || o->op_savefree || o->op_static) {
SV * const tmpsv = newSVpvs("");
switch (o->op_flags & OPf_WANT) {
case OPf_WANT_VOID:
sv_catpv(tmpsv, ",VOID");
break;
case OPf_WANT_SCALAR:
sv_catpv(tmpsv, ",SCALAR");
break;
case OPf_WANT_LIST:
sv_catpv(tmpsv, ",LIST");
break;
default:
sv_catpv(tmpsv, ",UNKNOWN");
break;
}
append_flags(tmpsv, o->op_flags, op_flags_names);
if (o->op_slabbed) sv_catpvs(tmpsv, ",SLABBED");
if (o->op_savefree) sv_catpvs(tmpsv, ",SAVEFREE");
if (o->op_static) sv_catpvs(tmpsv, ",STATIC");
if (o->op_folded) sv_catpvs(tmpsv, ",FOLDED");
if (o->op_moresib) sv_catpvs(tmpsv, ",MORESIB");
Perl_dump_indent(aTHX_ level, file, "FLAGS = (%s)\n",
SvCUR(tmpsv) ? SvPVX_const(tmpsv) + 1 : "");
}
if (o->op_private) {
U16 oppriv = o->op_private;
I16 op_ix = PL_op_private_bitdef_ix[o->op_type];
SV * tmpsv = NULL;
if (op_ix != -1) {
U16 stop = 0;
tmpsv = newSVpvs("");
for (; !stop; op_ix++) {
U16 entry = PL_op_private_bitdefs[op_ix];
U16 bit = (entry >> 2) & 7;
U16 ix = entry >> 5;
stop = (entry & 1);
if (entry & 2) {
/* bitfield */
I16 const *p = &PL_op_private_bitfields[ix];
U16 bitmin = (U16) *p++;
I16 label = *p++;
I16 enum_label;
U16 mask = 0;
U16 i;
U16 val;
for (i = bitmin; i<= bit; i++)
mask |= (1<<i);
bit = bitmin;
val = (oppriv & mask);
if ( label != -1
&& PL_op_private_labels[label] == '-'
&& PL_op_private_labels[label+1] == '\0'
)
/* display as raw number */
continue;
oppriv -= val;
val >>= bit;
enum_label = -1;
while (*p != -1) {
if (val == *p++) {
enum_label = *p;
break;
}
p++;
}
if (val == 0 && enum_label == -1)
/* don't display anonymous zero values */
continue;
sv_catpv(tmpsv, ",");
if (label != -1) {
sv_catpv(tmpsv, &PL_op_private_labels[label]);
sv_catpv(tmpsv, "=");
}
if (enum_label == -1)
Perl_sv_catpvf(aTHX_ tmpsv, "0x%"UVxf, (UV)val);
else
sv_catpv(tmpsv, &PL_op_private_labels[enum_label]);
}
else {
/* bit flag */
if ( oppriv & (1<<bit)
&& !(PL_op_private_labels[ix] == '-'
&& PL_op_private_labels[ix+1] == '\0'))
{
oppriv -= (1<<bit);
sv_catpv(tmpsv, ",");
sv_catpv(tmpsv, &PL_op_private_labels[ix]);
}
}
}
if (oppriv) {
sv_catpv(tmpsv, ",");
Perl_sv_catpvf(aTHX_ tmpsv, "0x%"UVxf, (UV)oppriv);
}
}
if (tmpsv && SvCUR(tmpsv)) {
Perl_dump_indent(aTHX_ level, file, "PRIVATE = (%s)\n", SvPVX_const(tmpsv) + 1);
} else
Perl_dump_indent(aTHX_ level, file, "PRIVATE = (0x%"UVxf")\n",
(UV)oppriv);
}
switch (optype) {
case OP_AELEMFAST:
case OP_GVSV:
case OP_GV:
#ifdef USE_ITHREADS
Perl_dump_indent(aTHX_ level, file, "PADIX = %" IVdf "\n", (IV)cPADOPo->op_padix);
#else
if ( ! (o->op_flags & OPf_SPECIAL)) { /* not lexical */
if (cSVOPo->op_sv) {
STRLEN len;
const char * name;
SV * const tmpsv = newSVpvs_flags("", SVs_TEMP);
SV * const tmpsv2 = newSVpvs_flags("", SVs_TEMP);
gv_fullname3(tmpsv, MUTABLE_GV(cSVOPo->op_sv), NULL);
name = SvPV_const(tmpsv, len);
Perl_dump_indent(aTHX_ level, file, "GV = %s\n",
generic_pv_escape( tmpsv2, name, len, SvUTF8(tmpsv)));
}
else
Perl_dump_indent(aTHX_ level, file, "GV = NULL\n");
}
#endif
break;
case OP_MULTIDEREF:
{
UNOP_AUX_item *items = cUNOP_AUXo->op_aux;
UV i, count = items[-1].uv;
Perl_dump_indent(aTHX_ level, file, "ARGS = \n");
for (i=0; i < count; i++)
Perl_dump_indent(aTHX_ level+1, file, "%"UVuf" => 0x%"UVxf"\n",
i, items[i].uv);
}
case OP_CONST:
case OP_HINTSEVAL:
case OP_METHOD_NAMED:
case OP_METHOD_SUPER:
case OP_METHOD_REDIR:
case OP_METHOD_REDIR_SUPER:
#ifndef USE_ITHREADS
/* with ITHREADS, consts are stored in the pad, and the right pad
* may not be active here, so skip */
Perl_dump_indent(aTHX_ level, file, "SV = %s\n", SvPEEK(cMETHOPx_meth(o)));
#endif
break;
case OP_NULL:
if (o->op_targ != OP_NEXTSTATE && o->op_targ != OP_DBSTATE)
break;
/* FALLTHROUGH */
case OP_NEXTSTATE:
case OP_DBSTATE:
if (CopLINE(cCOPo))
Perl_dump_indent(aTHX_ level, file, "LINE = %"UVuf"\n",
(UV)CopLINE(cCOPo));
if (CopSTASHPV(cCOPo)) {
SV* tmpsv = newSVpvs_flags("", SVs_TEMP);
HV *stash = CopSTASH(cCOPo);
const char * const hvname = HvNAME_get(stash);
Perl_dump_indent(aTHX_ level, file, "PACKAGE = \"%s\"\n",
generic_pv_escape(tmpsv, hvname,