Refactoring to Sv*_set() macros - patch #4
[p5sagit/p5-mst-13.2.git] / dump.c
diff --git a/dump.c b/dump.c
index 533dd78..31a0e03 100644 (file)
--- a/dump.c
+++ b/dump.c
@@ -1,7 +1,7 @@
 /*    dump.c
  *
  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
- *    2000, 2001, 2002, 2003, 2004, by Larry Wall and others
+ *    2000, 2001, 2002, 2003, 2004, 2005, 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.
  * it has not been hard for me to read your mind and memory.'"
  */
 
+/* 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.
+ */
+
 #include "EXTERN.h"
 #define PERL_IN_DUMP_C
 #include "perl.h"
@@ -46,17 +53,17 @@ Perl_dump_all(pTHX)
 }
 
 void
-Perl_dump_packsubs(pTHX_ HV *stash)
+Perl_dump_packsubs(pTHX_ const HV *stash)
 {
     I32        i;
-    HE *entry;
 
     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);
-           HV *hv;
+            const GV *gv = (GV*)HeVAL(entry);
+            const HV *hv;
            if (SvTYPE(gv) != SVt_PVGV || !GvGP(gv))
                continue;
            if (GvCVu(gv))
@@ -71,7 +78,7 @@ Perl_dump_packsubs(pTHX_ HV *stash)
 }
 
 void
-Perl_dump_sub(pTHX_ GV *gv)
+Perl_dump_sub(pTHX_ const GV *gv)
 {
     SV *sv = sv_newmortal();
 
@@ -88,7 +95,7 @@ Perl_dump_sub(pTHX_ GV *gv)
 }
 
 void
-Perl_dump_form(pTHX_ GV *gv)
+Perl_dump_form(pTHX_ const GV *gv)
 {
     SV *sv = sv_newmortal();
 
@@ -107,15 +114,15 @@ Perl_dump_eval(pTHX)
 }
 
 char *
-Perl_pv_display(pTHX_ SV *dsv, char *pv, STRLEN cur, STRLEN len, STRLEN pvlim)
+Perl_pv_display(pTHX_ SV *dsv, const char *pv, STRLEN cur, STRLEN len, STRLEN pvlim)
 {
-    int truncated = 0;
-    int nul_terminated = len > cur && pv[cur] == '\0';
+    const bool nul_terminated = len > cur && pv[cur] == '\0';
+    bool truncated = 0;
 
     sv_setpvn(dsv, "\"", 1);
     for (; cur--; pv++) {
        if (pvlim && SvCUR(dsv) >= pvlim) {
-            truncated++;
+            truncated = 1;
            break;
         }
        switch (*pv) {
@@ -327,7 +334,7 @@ Perl_sv_peek(pTHX_ SV *sv)
 }
 
 void
-Perl_do_pmop_dump(pTHX_ I32 level, PerlIO *file, PMOP *pm)
+Perl_do_pmop_dump(pTHX_ I32 level, PerlIO *file, const PMOP *pm)
 {
     char ch;
 
@@ -395,14 +402,14 @@ Perl_pmop_dump(pTHX_ PMOP *pm)
 /* An op sequencer.  We visit the ops in the order they're to execute. */
 
 STATIC void
-sequence(pTHX_ register OP *o)
+sequence(pTHX_ register const OP *o)
 {
     SV      *op;
     char    *key;
     STRLEN   len;
     static   UV seq;
-    OP      *oldop = 0,
-            *l;
+    const OP *oldop = 0;
+    OP      *l;
 
     if (!Sequence)
        Sequence = newHV();
@@ -492,7 +499,7 @@ sequence(pTHX_ register OP *o)
 }
 
 STATIC UV
-sequence_num(pTHX_ OP *o)
+sequence_num(pTHX_ const OP *o)
 {
     SV     *op,
           **seq;
@@ -506,7 +513,7 @@ sequence_num(pTHX_ OP *o)
 }
 
 void
-Perl_do_op_dump(pTHX_ I32 level, PerlIO *file, OP *o)
+Perl_do_op_dump(pTHX_ I32 level, PerlIO *file, const OP *o)
 {
     UV      seq;
     sequence(aTHX_ o);
@@ -849,7 +856,7 @@ Perl_do_op_dump(pTHX_ I32 level, PerlIO *file, OP *o)
 }
 
 void
-Perl_op_dump(pTHX_ OP *o)
+Perl_op_dump(pTHX_ const OP *o)
 {
     do_op_dump(0, Perl_debug_log, o);
 }
@@ -880,7 +887,7 @@ Perl_gv_dump(pTHX_ GV *gv)
  * (with the PERL_MAGIC_ prefixed stripped)
  */
 
-static struct { char type; char *name; } magic_names[] = {
+static struct { const char type; const char *name; } magic_names[] = {
        { PERL_MAGIC_sv,             "sv(\\0)" },
        { PERL_MAGIC_arylen,         "arylen(#)" },
        { PERL_MAGIC_glob,           "glob(*)" },
@@ -925,14 +932,14 @@ static struct { char type; char *name; } magic_names[] = {
 };
 
 void
-Perl_do_magic_dump(pTHX_ I32 level, PerlIO *file, MAGIC *mg, I32 nest, I32 maxnest, bool dumpops, STRLEN pvlim)
+Perl_do_magic_dump(pTHX_ I32 level, PerlIO *file, const MAGIC *mg, I32 nest, I32 maxnest, bool dumpops, STRLEN pvlim)
 {
     for (; mg; mg = mg->mg_moremagic) {
        Perl_dump_indent(aTHX_ level, file,
                         "  MAGIC = 0x%"UVxf"\n", PTR2UV(mg));
        if (mg->mg_virtual) {
-            MGVTBL *v = mg->mg_virtual;
-           char *s = 0;
+            const MGVTBL * const v = mg->mg_virtual;
+           const char *s = 0;
            if      (v == &PL_vtbl_sv)         s = "sv";
             else if (v == &PL_vtbl_env)        s = "env";
             else if (v == &PL_vtbl_envelem)    s = "envelem";
@@ -974,7 +981,7 @@ Perl_do_magic_dump(pTHX_ I32 level, PerlIO *file, MAGIC *mg, I32 nest, I32 maxne
 
        {
            int n;
-           char *name = 0;
+           const char *name = 0;
            for (n=0; magic_names[n].name; n++) {
                if (mg->mg_type == magic_names[n].type) {
                    name = magic_names[n].name;
@@ -1043,13 +1050,13 @@ Perl_do_magic_dump(pTHX_ I32 level, PerlIO *file, MAGIC *mg, I32 nest, I32 maxne
 }
 
 void
-Perl_magic_dump(pTHX_ MAGIC *mg)
+Perl_magic_dump(pTHX_ const MAGIC *mg)
 {
     do_magic_dump(0, Perl_debug_log, mg, 0, 0, 0, 0);
 }
 
 void
-Perl_do_hv_dump(pTHX_ I32 level, PerlIO *file, char *name, HV *sv)
+Perl_do_hv_dump(pTHX_ I32 level, PerlIO *file, const char *name, HV *sv)
 {
     Perl_dump_indent(aTHX_ level, file, "%s = 0x%"UVxf, name, PTR2UV(sv));
     if (sv && HvNAME(sv))
@@ -1059,7 +1066,7 @@ Perl_do_hv_dump(pTHX_ I32 level, PerlIO *file, char *name, HV *sv)
 }
 
 void
-Perl_do_gv_dump(pTHX_ I32 level, PerlIO *file, char *name, GV *sv)
+Perl_do_gv_dump(pTHX_ I32 level, PerlIO *file, const char *name, GV *sv)
 {
     Perl_dump_indent(aTHX_ level, file, "%s = 0x%"UVxf, name, PTR2UV(sv));
     if (sv && GvNAME(sv))
@@ -1069,7 +1076,7 @@ Perl_do_gv_dump(pTHX_ I32 level, PerlIO *file, char *name, GV *sv)
 }
 
 void
-Perl_do_gvgv_dump(pTHX_ I32 level, PerlIO *file, char *name, GV *sv)
+Perl_do_gvgv_dump(pTHX_ I32 level, PerlIO *file, const char *name, GV *sv)
 {
     Perl_dump_indent(aTHX_ level, file, "%s = 0x%"UVxf, name, PTR2UV(sv));
     if (sv && GvNAME(sv)) {
@@ -1086,7 +1093,7 @@ void
 Perl_do_sv_dump(pTHX_ I32 level, PerlIO *file, SV *sv, I32 nest, I32 maxnest, bool dumpops, STRLEN pvlim)
 {
     SV *d;
-    char *s;
+    const char *s;
     U32 flags;
     U32 type;
 
@@ -1187,7 +1194,8 @@ Perl_do_sv_dump(pTHX_ I32 level, PerlIO *file, SV *sv, I32 nest, I32 maxnest, bo
                                sv_catpv(d, "TYPED,");
        break;
     }
-    if ((SvPOK(sv) || SvPOKp(sv)) && SvUTF8(sv))
+    /* SVphv_SHAREKEYS is also 0x20000000 */
+    if ((type != SVt_PVHV) && SvUTF8(sv))
         sv_catpv(d, "UTF8");
 
     if (*(SvEND(d) - 1) == ',')
@@ -1195,6 +1203,14 @@ Perl_do_sv_dump(pTHX_ I32 level, PerlIO *file, SV *sv, I32 nest, I32 maxnest, bo
     sv_catpv(d, ")");
     s = SvPVX(d);
 
+#ifdef DEBUG_LEAKING_SCALARS
+    Perl_dump_indent(aTHX_ level, file, "ALLOCATED at %s:%d %s %s%s\n",
+       sv->sv_debug_file ? sv->sv_debug_file : "(unknown)",
+       sv->sv_debug_line,
+       sv->sv_debug_inpad ? "for" : "by",
+       sv->sv_debug_optype ? PL_op_name[sv->sv_debug_optype]: "(none)",
+       sv->sv_debug_cloned ? " (cloned)" : "");
+#endif
     Perl_dump_indent(aTHX_ level, file, "SV = ");
     switch (type) {
     case SVt_NULL:
@@ -1412,7 +1428,7 @@ Perl_do_sv_dump(pTHX_ I32 level, PerlIO *file, SV *sv, I32 nest, I32 maxnest, bo
            while ((he = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))
                    && count--) {
                SV *elt, *keysv;
-               char *keypv;
+                const char *keypv;
                STRLEN len;
                U32 hash = HeHASH(he);
 
@@ -1455,7 +1471,7 @@ Perl_do_sv_dump(pTHX_ I32 level, PerlIO *file, SV *sv, I32 nest, I32 maxnest, bo
            do_dump_pad(level+1, file, CvPADLIST(sv), 0);
        }
        {
-           CV *outside = CvOUTSIDE(sv);
+            const CV *outside = CvOUTSIDE(sv);
            Perl_dump_indent(aTHX_ level, file, "  OUTSIDE = 0x%"UVxf" (%s)\n",
                        PTR2UV(outside),
                        (!outside ? "null"
@@ -1570,9 +1586,8 @@ Perl_runops_debug(pTHX)
 }
 
 I32
-Perl_debop(pTHX_ OP *o)
+Perl_debop(pTHX_ const OP *o)
 {
-    AV *padlist, *comppad;
     CV *cv;
     SV *sv;
 
@@ -1601,8 +1616,8 @@ Perl_debop(pTHX_ OP *o)
        /* print the lexical's name */
         cv = deb_curcv(cxstack_ix);
         if (cv) {
-            padlist = CvPADLIST(cv);
-            comppad = (AV*)(*av_fetch(padlist, 0, FALSE));
+            AV *padlist = CvPADLIST(cv);
+            AV *comppad = (AV*)(*av_fetch(padlist, 0, FALSE));
             sv = *av_fetch(comppad, o->op_targ, FALSE);
         } else
             sv = Nullsv;
@@ -1621,7 +1636,7 @@ Perl_debop(pTHX_ OP *o)
 STATIC CV*
 S_deb_curcv(pTHX_ I32 ix)
 {
-    PERL_CONTEXT *cx = &cxstack[ix];
+    const PERL_CONTEXT *cx = &cxstack[ix];
     if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT)
         return cx->blk_sub.cv;
     else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
@@ -1644,7 +1659,7 @@ Perl_watch(pTHX_ char **addr)
 }
 
 STATIC void
-S_debprof(pTHX_ OP *o)
+S_debprof(pTHX_ const OP *o)
 {
     if (CopSTASH_eq(PL_curcop, PL_debstash) && !DEBUG_J_TEST_)
        return;