-
Notifications
You must be signed in to change notification settings - Fork 560
/
dump.c
3572 lines (3151 loc) · 118 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.
=for apidoc_section $display
*/
#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",
"PVOBJ",
};
static const char* const svshorttypenames[SVt_LAST] = {
"UNDEF",
"IV",
"NV",
"PV",
"INVLST",
"PVIV",
"PVNV",
"PVMG",
"REGEXP",
"GV",
"PVLV",
"AV",
"HV",
"CV",
"FM",
"IO",
"OBJ",
};
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) )
#define _pv_display_for_dump(dsv, pv, cur, len, pvlim) \
_pv_display_flags(aTHX_ dsv, pv, cur, len, pvlim, PERL_PV_ESCAPE_DWIM_ALL_HEX)
/*
=for apidoc pv_escape
Escapes at most the first C<count> chars of C<pv> and puts the results into
C<dsv> such that the size of the escaped string will not exceed C<max> chars
and will not contain any incomplete escape sequences. The number of bytes
escaped will be returned in the C<STRLEN *escaped> parameter if it is not null.
When the C<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 C<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 C<PERL_PV_ESCAPE_NOCLEAR> is set this will not occur.
If C<PERL_PV_ESCAPE_UNI> is set then the input string is treated as UTF-8.
If C<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 C<PERL_PV_ESCAPE_ALL> is set then all input chars will be output
using C<\x01F1> style escapes, otherwise if C<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 C<PERL_PV_ESCAPE_NOBACKSLASH> then all chars below 255 will be
treated as printable and will be output as literals. The
C<PERL_PV_ESCAPE_NON_WC> modifies the previous rules to cause word
chars, unicode or otherwise, to be output as literals, note this uses
the *unicode* rules for deciding on word characters.
If C<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 C<PERL_PV_ESCAPE_RE> is set then the escape char used will be a
C<"%"> and not a C<"\\">. This is because regexes very often contain
backslashed sequences, whereas C<"%"> is not a particularly common
character in patterns.
Returns a pointer to the escaped text as held by C<dsv>.
=for apidoc Amnh||PERL_PV_ESCAPE_ALL
=for apidoc Amnh||PERL_PV_ESCAPE_FIRSTCHAR
=for apidoc Amnh||PERL_PV_ESCAPE_NOBACKSLASH
=for apidoc Amnh||PERL_PV_ESCAPE_NOCLEAR
=for apidoc Amnh||PERL_PV_ESCAPE_NONASCII
=for apidoc Amnh||PERL_PV_ESCAPE_QUOTE
=for apidoc Amnh||PERL_PV_ESCAPE_RE
=for apidoc Amnh||PERL_PV_ESCAPE_UNI
=for apidoc Amnh||PERL_PV_ESCAPE_UNI_DETECT
=for apidoc Amnh||PERL_PV_ESCAPE_NON_WC
=cut
Unused or not for public use
=for apidoc Cmnh||PERL_PV_PRETTY_REGPROP
=for apidoc Cmnh||PERL_PV_PRETTY_DUMP
=for apidoc Cmnh||PERL_PV_PRETTY_NOCLEAR
=cut
*/
#define PV_ESCAPE_OCTBUFSIZE 32
#define PV_BYTE_HEX_UC "x%02" UVXf
#define PV_BYTE_HEX_LC "x%02" UVxf
char *
Perl_pv_escape( pTHX_ SV *dsv, char const * const str,
const STRLEN count, STRLEN max,
STRLEN * const escaped, U32 flags )
{
bool use_uc_hex = false;
if (flags & PERL_PV_ESCAPE_DWIM_ALL_HEX) {
use_uc_hex = true;
flags |= PERL_PV_ESCAPE_DWIM;
}
const char esc = (flags & PERL_PV_ESCAPE_RE) ? '%' : '\\';
const char dq = (flags & PERL_PV_ESCAPE_QUOTE) ? '"' : esc;
const char *qs;
const char *qe;
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)
? TRUE : FALSE; /* is this UTF-8 */
const char *pv = str;
const char * const end = pv + count; /* end of string */
const char *restart = NULL;
STRLEN extra_len = 0;
STRLEN tail = 0;
if ((flags & PERL_PV_ESCAPE_TRUNC_MIDDLE) && max > 3) {
if (flags & PERL_PV_ESCAPE_QUOTE) {
qs = qe = "\"";
extra_len = 5;
} else if (flags & PERL_PV_PRETTY_LTGT) {
qs = "<";
qe = ">";
extra_len = 5;
} else {
qs = qe = "";
extra_len = 3;
}
tail = max / 2;
restart = isuni ? (char *)utf8_hop_back((U8*)end,-tail,(U8*)pv) : end - tail;
if (restart > pv) {
max -= tail;
} else {
tail = 0;
restart = NULL;
}
}
else {
qs = qe = "";
}
octbuf[0] = esc;
PERL_ARGS_ASSERT_PV_ESCAPE;
if (dsv && !(flags & PERL_PV_ESCAPE_NOCLEAR)) {
/* This won't alter the UTF-8 flag */
SvPVCLEAR(dsv);
}
if ((flags & PERL_PV_ESCAPE_UNI_DETECT) && is_utf8_string((U8*)pv, count))
isuni = 1;
for ( ; pv < end ; pv += readsize ) {
const UV u= (isuni) ? utf8_to_uvchr_buf((U8*)pv, (U8*) end, &readsize) : (U8)*pv;
const U8 c = (U8)u;
const char *source_buf = octbuf;
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
if ((flags & PERL_PV_ESCAPE_NON_WC) && isWORDCHAR_uvchr(u)) {
chsize = readsize;
source_buf = pv;
}
else
chsize = my_snprintf( octbuf, PV_ESCAPE_OCTBUFSIZE,
((flags & PERL_PV_ESCAPE_DWIM) && !isuni)
? ( use_uc_hex ? ("%c" PV_BYTE_HEX_UC) : ("%c" PV_BYTE_HEX_LC) )
: "%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_ALL_HEX) || ((flags & PERL_PV_ESCAPE_DWIM) && c != '\0') ) {
chsize = my_snprintf( octbuf, PV_ESCAPE_OCTBUFSIZE,
isuni ? "%cx{%02" UVxf "}" : ( use_uc_hex ? ("%c" PV_BYTE_HEX_UC) : ("%c" PV_BYTE_HEX_LC) ),
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)) {
if (restart) {
/* this only happens with PERL_PV_ESCAPE_TRUNC_MIDDLE */
if (dsv)
Perl_sv_catpvf( aTHX_ dsv,"%s...%s", qe, qs);
wrote += extra_len;
pv = restart;
max = tail;
wrote = tail = 0;
restart = NULL;
} else {
break;
}
} else if (chsize > 1) {
if (dsv)
sv_catpvn(dsv, source_buf, 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
C<pv_escape()> and supporting quoting and ellipses.
If the C<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 C<PERL_PV_PRETTY_LTGT> flag is set then the result be wrapped in
angle brackets.
If the C<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 C<start_color> is non-null then it will be inserted after the opening
quote (if there is one) but before the escaped text. If C<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 C<dsv>.
=for apidoc Amnh||PERL_PV_PRETTY_QUOTE
=for apidoc Amnh||PERL_PV_PRETTY_LTGT
=for apidoc Amnh||PERL_PV_PRETTY_ELLIPSES
=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 */
SvPVCLEAR(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);
}
STATIC char *
_pv_display_flags(pTHX_ SV *dsv, const char *pv, STRLEN cur, STRLEN len, STRLEN pvlim, I32 pretty_flags)
{
PERL_ARGS_ASSERT_PV_DISPLAY;
pv_pretty( dsv, pv, cur, pvlim, NULL, NULL, PERL_PV_PRETTY_DUMP | pretty_flags );
if (len > cur && pv[cur] == '\0')
sv_catpvs( dsv, "\\0");
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)
{
return _pv_display_flags(aTHX_ dsv, pv, cur, len, pvlim, 0);
}
/*
=for apidoc sv_peek
Implements C<SvPEEK>
=cut
*/
char *
Perl_sv_peek(pTHX_ SV *sv)
{
SV * const t = sv_newmortal();
int unref = 0;
U32 type;
SvPVCLEAR(t);
retry:
if (!sv) {
sv_catpvs(t, "VOID");
goto finish;
}
else if (sv == (const SV *)0x55555555 || ((char)SvTYPE(sv)) == 'U') {
/* detect data corruption under memory poisoning */
sv_catpvs(t, "WILD");
goto finish;
}
else if ( sv == &PL_sv_undef || sv == &PL_sv_no || sv == &PL_sv_yes
|| sv == &PL_sv_zero || sv == &PL_sv_placeholder)
{
if (sv == &PL_sv_undef) {
sv_catpvs(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_catpvs(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_catpvs(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 if (sv == &PL_sv_zero) {
sv_catpvs(t, "SV_ZERO");
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) == '0' &&
SvNVX(sv) == 0.0)
goto finish;
}
else {
sv_catpvs(t, "SV_PLACEHOLDER");
if (!(SvFLAGS(sv) & (SVf_OK|SVf_OOK|SVs_OBJECT|
SVs_GMG|SVs_SMG|SVs_RMG)) &&
SvREADONLY(sv))
goto finish;
}
sv_catpvs(t, ":");
}
else if (SvREFCNT(sv) == 0) {
sv_catpvs(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 (is_tmp || SvREFCNT(sv) > 1 || SvPADTMP(sv)) {
Perl_sv_catpvf(aTHX_ t, "<");
if (SvREFCNT(sv) > 1)
Perl_sv_catpvf(aTHX_ t, "%" UVuf, (UV)SvREFCNT(sv));
if (SvPADTMP(sv))
Perl_sv_catpvf(aTHX_ t, "%s", "P");
if (is_tmp)
Perl_sv_catpvf(aTHX_ t, "%s", SvTEMP(t) ? "T" : "t");
Perl_sv_catpvf(aTHX_ t, ">");
}
}
if (SvROK(sv)) {
sv_catpvs(t, "\\");
if (SvCUR(t) + unref > 10) {
SvCUR_set(t, unref + 3);
*SvEND(t) = '\0';
sv_catpvs(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_catpvs(t, "FREED");
goto finish;
}
if (SvPOKp(sv)) {
if (!SvPVX_const(sv))
sv_catpvs(t, "(null)");
else {
SV * const tmp = newSVpvs("");
sv_catpvs(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)) {
DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
STORE_LC_NUMERIC_SET_STANDARD();
Perl_sv_catpvf(aTHX_ t, "(%" NVgf ")",SvNVX(sv));
RESTORE_LC_NUMERIC();
}
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_catpvs(t, "()");
finish:
while (unref--)
sv_catpvs(t, ")");
if (TAINTING_get && sv && SvTAINTED(sv))
sv_catpvs(t, " [tainted]");
return SvPV_nolen(t);
}
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);
}
/* Like Perl_dump_indent(), but specifically for ops: adds a vertical bar
* for each indent level as appropriate.
*
* bar contains bits indicating which indent columns should have a
* vertical bar displayed. Bit 0 is the RH-most column. If there are more
* levels than bits in bar, then the first few indents are displayed
* without a bar.
*
* The start of a new op is signalled by passing a value for level which
* has been negated and offset by 1 (so that level 0 is passed as -1 and
* can thus be distinguished from -0); in this case, emit a suitably
* indented blank line, then on the next line, display the op's sequence
* number, and make the final indent an '+----'.
*
* e.g.
*
* | FOO # level = 1, bar = 0b1
* | | # level =-2-1, bar = 0b11
* 1234 | +---BAR
* | BAZ # level = 2, bar = 0b10
*/
static void
S_opdump_indent(pTHX_ const OP *o, I32 level, UV bar, PerlIO *file,
const char* pat, ...)
{
va_list args;
bool newop = (level < 0);
va_start(args, pat);
/* start displaying a new op? */
if (newop) {
UV seq = sequence_num(o);
level = -level - 1;
/* output preceding blank line */
PerlIO_puts(file, " ");
for (I32 i = level-1; i >= 0; i--)
PerlIO_puts(file, ( i == 0
|| (i < UVSIZE*8 && (bar & ((UV)1 << i)))
)
? "| " : " ");
PerlIO_puts(file, "\n");
/* output sequence number */
if (seq)
PerlIO_printf(file, "%-4" UVuf " ", seq);
else
PerlIO_puts(file, "???? ");
}
else
PerlIO_puts(file, " ");
for (I32 i = level-1; i >= 0; i--)
PerlIO_puts(file,
(i == 0 && newop) ? "+--"
: (bar & (1 << i)) ? "| "
: " ");
PerlIO_vprintf(file, pat, args);
va_end(args);
}
struct Perl_OpDumpContext {
I32 level;
UV bar;
PerlIO *file;
bool indent_needed;
};
static void
S_opdump_print(pTHX_ struct Perl_OpDumpContext *ctx, SV *msg)
{
STRLEN msglen;
const char *msgpv = SvPV(msg, msglen);
while(msglen) {
if(ctx->indent_needed) {
PerlIO_puts(ctx->file, " ");
for (I32 i = ctx->level-1; i >= 0; i--)
PerlIO_puts(ctx->file,
(ctx->bar & (1 << i)) ? "| " : " ");
}
const char *eol_at = strchr(msgpv, '\n');
if(eol_at) {
STRLEN partlen = eol_at - msgpv + 1;
PerlIO_write(ctx->file, msgpv, partlen);
ctx->indent_needed = true;
msgpv += partlen;
msglen -= partlen;
}
else {
PerlIO_write(ctx->file, msgpv, msglen);
ctx->indent_needed = false;
msglen = 0;
}
}
}
/*
=for apidoc_section $debugging
=for apidoc opdump_printf
Prints formatted output to C<STDERR> according to the pattern and subsequent
arguments, in the style of C<printf()> et.al. This should only be called by
a function invoked by the C<xop_dump> field of a custom operator, where the
C<ctx> opaque structure pointer should be passed in from the argument given
to the C<xop_dump> callback.
This function handles indentation after linefeeds, so message strings passed
in should not account for it themselves. Multiple lines may be passed to this
function at once, or a single line may be split across multiple calls.
=cut
*/
void
Perl_opdump_printf(pTHX_ struct Perl_OpDumpContext *ctx, const char *pat, ...)
{
va_list args;
PERL_ARGS_ASSERT_OPDUMP_PRINTF;
va_start(args, pat);
SV *msg_sv = sv_2mortal(vnewSVpvf(pat, &args));
S_opdump_print(aTHX_ ctx, msg_sv);
va_end(args);
}
/* display a link field (e.g. op_next) in the format
* ====> sequence_number [opname 0x123456]
*/
static void
S_opdump_link(pTHX_ const OP *base, const OP *o, PerlIO *file)
{
PerlIO_puts(file, " ===> ");
if (o == base)
PerlIO_puts(file, "[SELF]\n");
else if (o)
PerlIO_printf(file, "%" UVuf " [%s 0x%" UVxf "]\n",
sequence_num(o), OP_NAME(o), PTR2UV(o));
else
PerlIO_puts(file, "[0x0]\n");
}
/*
=for apidoc_section $debugging
=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 (!HvTOTALKEYS(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)
{
CV *cv;
PERL_ARGS_ASSERT_DUMP_SUB_PERL;
cv = isGV_with_GP(gv) ? GvCV(gv) : CV_FROM_REF((SV*)gv);
if (justperl && (CvISXSUB(cv) || !CvROOT(cv)))
return;
if (isGV_with_GP(gv)) {
SV * const namesv = newSVpvs_flags("", SVs_TEMP);
SV *escsv = newSVpvs_flags("", SVs_TEMP);
const char *namepv;
STRLEN namelen;
gv_fullname3(namesv, gv, NULL);
namepv = SvPV_const(namesv, namelen);
Perl_dump_indent(aTHX_ 0, Perl_debug_log, "\nSUB %s = ",
generic_pv_escape(escsv, namepv, namelen, SvUTF8(namesv)));
} else {
Perl_dump_indent(aTHX_ 0, Perl_debug_log, "\nSUB = ");
}
if (CvISXSUB(cv))
Perl_dump_indent(aTHX_ 0, Perl_debug_log, "(xsub 0x%" UVxf " %d)\n",
PTR2UV(CvXSUB(cv)),
(int)CvXSUBANY(cv).any_i32);
else if (CvROOT(cv))
op_dump(CvROOT(cv));
else
Perl_dump_indent(aTHX_ 0, Perl_debug_log, "<undef>\n");
}
/*
=for apidoc dump_form
Dumps the contents of the format contained in the GV C<gv> to C<STDERR>, or a
message that one doesn't exist.
=cut
*/
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);
}
/* returns a temp SV displaying the name of a GV. Handles the case where
* a GV is in fact a ref to a CV */
static SV *
S_gv_display(pTHX_ GV *gv)
{
SV * const name = newSVpvs_flags("", SVs_TEMP);
if (gv) {
SV * const raw = newSVpvs_flags("", SVs_TEMP);
STRLEN len;
const char * rawpv;
if (isGV_with_GP(gv))
gv_fullname3(raw, gv, NULL);
else {
Perl_sv_catpvf(aTHX_ raw, "cv ref: %s",
SvPV_nolen_const(cv_name(CV_FROM_REF((SV*)gv), name, 0)));
}
rawpv = SvPV_const(raw, len);
generic_pv_escape(name, rawpv, len, SvUTF8(raw));
}
else
sv_catpvs(name, "(NULL)");
return name;
}
/* forward decl */
static void
S_do_op_dump_bar(pTHX_ I32 level, UV bar, PerlIO *file, const OP *o);
static void
S_do_pmop_dump_bar(pTHX_ I32 level, UV bar, PerlIO *file, const PMOP *pm)
{
UV kidbar;
if (!pm)
return;
kidbar = ((bar << 1) | cBOOL(pm->op_flags & OPf_KIDS)) << 1;
if (PM_GETRE(pm)) {
char ch = (pm->op_pmflags & PMf_ONCE) ? '?' : '/';
S_opdump_indent(aTHX_ (OP*)pm, level, bar, file, "PMf_PRE %c%.*s%c\n",
ch,(int)RX_PRELEN(PM_GETRE(pm)), RX_PRECOMP(PM_GETRE(pm)), ch);
}
else
S_opdump_indent(aTHX_ (OP*)pm, level, bar, file, "PMf_PRE (RUNTIME)\n");
if (pm->op_pmflags || PM_GETRE(pm)) {
SV * const tmpsv = pm_description(pm);
S_opdump_indent(aTHX_ (OP*)pm, level, bar, file, "PMFLAGS = (%s)\n",
SvCUR(tmpsv) ? SvPVX_const(tmpsv) + 1 : "");
SvREFCNT_dec_NN(tmpsv);
}
if (pm->op_type == OP_SPLIT)
S_opdump_indent(aTHX_ (OP*)pm, level, bar, file,
"TARGOFF/GV = 0x%" UVxf "\n",
PTR2UV(pm->op_pmreplrootu.op_pmtargetgv));
else {