Refactoring to Sv*_set() macros - patch #4
[p5sagit/p5-mst-13.2.git] / dump.c
diff --git a/dump.c b/dump.c
index 69fa933..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, 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,30 +114,28 @@ 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;
         }
-        if (isPRINT(*pv)) {
-            switch (*pv) {
-           case '\t': sv_catpvn(dsv, "\\t", 2);  break;
-           case '\n': sv_catpvn(dsv, "\\n", 2);  break;
-           case '\r': sv_catpvn(dsv, "\\r", 2);  break;
-           case '\f': sv_catpvn(dsv, "\\f", 2);  break;
-           case '"':  sv_catpvn(dsv, "\\\"", 2); break;
-           case '\\': sv_catpvn(dsv, "\\\\", 2); break;
-           default:   sv_catpvn(dsv, pv, 1);     break;
-            }
-        }
-       else {
-           if (cur && isDIGIT(*(pv+1)))
+       switch (*pv) {
+       case '\t': sv_catpvn(dsv, "\\t", 2);  break;
+       case '\n': sv_catpvn(dsv, "\\n", 2);  break;
+       case '\r': sv_catpvn(dsv, "\\r", 2);  break;
+       case '\f': sv_catpvn(dsv, "\\f", 2);  break;
+       case '"':  sv_catpvn(dsv, "\\\"", 2); break;
+       case '\\': sv_catpvn(dsv, "\\\\", 2); break;
+       default:
+           if (isPRINT(*pv))
+               sv_catpvn(dsv, pv, 1);
+           else if (cur && isDIGIT(*(pv+1)))
                Perl_sv_catpvf(aTHX_ dsv, "\\%03o", (U8)*pv);
            else
                Perl_sv_catpvf(aTHX_ dsv, "\\%o", (U8)*pv);
@@ -329,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;
 
@@ -397,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();
@@ -412,13 +417,13 @@ sequence(pTHX_ register OP *o)
     if (!o)
        return;
 
-    op = newSVuv((UV) o);
+    op = newSVuv(PTR2UV(o));
     key = SvPV(op, len);
     if (hv_exists(Sequence, key, len))
        return;
 
     for (; o; o = o->op_next) {
-       op = newSVuv((UV) o);
+       op = newSVuv(PTR2UV(o));
        key = SvPV(op, len);
        if (hv_exists(Sequence, key, len))
            break;
@@ -454,7 +459,7 @@ sequence(pTHX_ register OP *o)
        case OP_COND_EXPR:
        case OP_RANGE:
            hv_store(Sequence, key, len, newSVuv(++seq), 0);
-           for (l = cLOGOPo->op_other; l->op_type == OP_NULL; l = l->op_next)
+           for (l = cLOGOPo->op_other; l && l->op_type == OP_NULL; l = l->op_next)
                ;
            sequence(aTHX_ l);
            break;
@@ -462,13 +467,13 @@ sequence(pTHX_ register OP *o)
        case OP_ENTERLOOP:
        case OP_ENTERITER:
            hv_store(Sequence, key, len, newSVuv(++seq), 0);
-           for (l = cLOOPo->op_redoop; l->op_type == OP_NULL; l = l->op_next)
+           for (l = cLOOPo->op_redoop; l && l->op_type == OP_NULL; l = l->op_next)
                ;
            sequence(aTHX_ l);
-           for (l = cLOOPo->op_nextop; l->op_type == OP_NULL; l = l->op_next)
+           for (l = cLOOPo->op_nextop; l && l->op_type == OP_NULL; l = l->op_next)
                ;
            sequence(aTHX_ l);
-           for (l = cLOOPo->op_lastop; l->op_type == OP_NULL; l = l->op_next)
+           for (l = cLOOPo->op_lastop; l && l->op_type == OP_NULL; l = l->op_next)
                ;
            sequence(aTHX_ l);
            break;
@@ -477,7 +482,7 @@ sequence(pTHX_ register OP *o)
        case OP_MATCH:
        case OP_SUBST:
            hv_store(Sequence, key, len, newSVuv(++seq), 0);
-           for (l = cPMOPo->op_pmreplstart; l->op_type == OP_NULL; l = l->op_next)
+           for (l = cPMOPo->op_pmreplstart; l && l->op_type == OP_NULL; l = l->op_next)
                ;
            sequence(aTHX_ l);
            break;
@@ -494,21 +499,21 @@ sequence(pTHX_ register OP *o)
 }
 
 STATIC UV
-sequence_num(pTHX_ OP *o)
+sequence_num(pTHX_ const OP *o)
 {
     SV     *op,
           **seq;
     char   *key;
     STRLEN  len;
     if (!o) return 0;
-    op = newSVuv((UV) o);
+    op = newSVuv(PTR2UV(o));
     key = SvPV(op, len);
     seq = hv_fetch(Sequence, key, len, 0);
     return seq ? SvUV(*seq): 0;
 }
 
 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);
@@ -516,14 +521,15 @@ Perl_do_op_dump(pTHX_ I32 level, PerlIO *file, OP *o)
     level++;
     seq = sequence_num(aTHX_ o);
     if (seq)
-       PerlIO_printf(file, "%-4d", seq);
+       PerlIO_printf(file, "%-4"UVf, 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, seq ? "%d\n" : "(%d)\n", sequence_num(aTHX_ o->op_next));
+       PerlIO_printf(file, seq ? "%"UVf"\n" : "(%"UVf")\n",
+                               sequence_num(aTHX_ o->op_next));
     else
        PerlIO_printf(file, "DONE\n");
     if (o->op_targ) {
@@ -758,17 +764,19 @@ Perl_do_op_dump(pTHX_ I32 level, PerlIO *file, OP *o)
 #ifdef USE_ITHREADS
        Perl_dump_indent(aTHX_ level, file, "PADIX = %" IVdf "\n", (IV)cPADOPo->op_padix);
 #else
-       if (cSVOPo->op_sv) {
-           SV *tmpsv = NEWSV(0,0);
-           STRLEN n_a;
-           ENTER;
-           SAVEFREESV(tmpsv);
-           gv_fullname3(tmpsv, (GV*)cSVOPo->op_sv, Nullch);
-           Perl_dump_indent(aTHX_ level, file, "GV = %s\n", SvPV(tmpsv, n_a));
-           LEAVE;
+       if ( ! PL_op->op_flags & OPf_SPECIAL) { /* not lexical */
+           if (cSVOPo->op_sv) {
+               SV *tmpsv = NEWSV(0,0);
+               STRLEN n_a;
+               ENTER;
+               SAVEFREESV(tmpsv);
+               gv_fullname3(tmpsv, (GV*)cSVOPo->op_sv, Nullch);
+               Perl_dump_indent(aTHX_ level, file, "GV = %s\n", SvPV(tmpsv, n_a));
+               LEAVE;
+           }
+           else
+               Perl_dump_indent(aTHX_ level, file, "GV = NULL\n");
        }
-       else
-           Perl_dump_indent(aTHX_ level, file, "GV = NULL\n");
 #endif
        break;
     case OP_CONST:
@@ -795,17 +803,17 @@ Perl_do_op_dump(pTHX_ I32 level, PerlIO *file, OP *o)
     case OP_ENTERLOOP:
        Perl_dump_indent(aTHX_ level, file, "REDO ===> ");
        if (cLOOPo->op_redoop)
-           PerlIO_printf(file, "%d\n", sequence_num(aTHX_ cLOOPo->op_redoop));
+           PerlIO_printf(file, "%"UVf"\n", sequence_num(aTHX_ cLOOPo->op_redoop));
        else
            PerlIO_printf(file, "DONE\n");
        Perl_dump_indent(aTHX_ level, file, "NEXT ===> ");
        if (cLOOPo->op_nextop)
-           PerlIO_printf(file, "%d\n", sequence_num(aTHX_ cLOOPo->op_nextop));
+           PerlIO_printf(file, "%"UVf"\n", sequence_num(aTHX_ cLOOPo->op_nextop));
        else
            PerlIO_printf(file, "DONE\n");
        Perl_dump_indent(aTHX_ level, file, "LAST ===> ");
        if (cLOOPo->op_lastop)
-           PerlIO_printf(file, "%d\n", sequence_num(aTHX_ cLOOPo->op_lastop));
+           PerlIO_printf(file, "%"UVf"\n", sequence_num(aTHX_ cLOOPo->op_lastop));
        else
            PerlIO_printf(file, "DONE\n");
        break;
@@ -817,7 +825,7 @@ Perl_do_op_dump(pTHX_ I32 level, PerlIO *file, OP *o)
     case OP_AND:
        Perl_dump_indent(aTHX_ level, file, "OTHER ===> ");
        if (cLOGOPo->op_other)
-           PerlIO_printf(file, "%d\n", sequence_num(aTHX_ cLOGOPo->op_other));
+           PerlIO_printf(file, "%"UVf"\n", sequence_num(aTHX_ cLOGOPo->op_other));
        else
            PerlIO_printf(file, "DONE\n");
        break;
@@ -848,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);
 }
@@ -879,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(*)" },
@@ -924,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";
@@ -973,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;
@@ -1042,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))
@@ -1058,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))
@@ -1068,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)) {
@@ -1085,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;
 
@@ -1186,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) == ',')
@@ -1194,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:
@@ -1411,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);
 
@@ -1454,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"
@@ -1539,6 +1556,7 @@ Perl_runops_debug(pTHX)
        return 0;
     }
 
+    DEBUG_l(Perl_deb(aTHX_ "Entering new RUNOPS level\n"));
     do {
        PERL_ASYNC_CHECK();
        if (PL_debug) {
@@ -1561,15 +1579,15 @@ Perl_runops_debug(pTHX)
            if (DEBUG_P_TEST_) debprof(PL_op);
        }
     } while ((PL_op = CALL_FPTR(PL_op->op_ppaddr)(aTHX)));
+    DEBUG_l(Perl_deb(aTHX_ "leaving RUNOPS level\n"));
 
     TAINT_NOT;
     return 0;
 }
 
 I32
-Perl_debop(pTHX_ OP *o)
+Perl_debop(pTHX_ const OP *o)
 {
-    AV *padlist, *comppad;
     CV *cv;
     SV *sv;
 
@@ -1598,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;
@@ -1618,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))
@@ -1641,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;