| 1 | /* deb.c |
| 2 | * |
| 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, |
| 4 | * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others |
| 5 | * |
| 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. |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * "Didst thou think that the eyes of the White Tower were blind? Nay, I |
| 13 | * have seen more than thou knowest, Gray Fool." --Denethor |
| 14 | */ |
| 15 | |
| 16 | /* |
| 17 | * This file contains various utilities for producing debugging output |
| 18 | * (mainly related to displaying the stack) |
| 19 | */ |
| 20 | |
| 21 | #include "EXTERN.h" |
| 22 | #define PERL_IN_DEB_C |
| 23 | #include "perl.h" |
| 24 | |
| 25 | #if defined(PERL_IMPLICIT_CONTEXT) |
| 26 | void |
| 27 | Perl_deb_nocontext(const char *pat, ...) |
| 28 | { |
| 29 | #ifdef DEBUGGING |
| 30 | dTHX; |
| 31 | va_list args; |
| 32 | va_start(args, pat); |
| 33 | vdeb(pat, &args); |
| 34 | va_end(args); |
| 35 | #else |
| 36 | PERL_UNUSED_ARG(pat); |
| 37 | #endif /* DEBUGGING */ |
| 38 | } |
| 39 | #endif |
| 40 | |
| 41 | void |
| 42 | Perl_deb(pTHX_ const char *pat, ...) |
| 43 | { |
| 44 | #ifdef DEBUGGING |
| 45 | va_list args; |
| 46 | va_start(args, pat); |
| 47 | vdeb(pat, &args); |
| 48 | va_end(args); |
| 49 | #else |
| 50 | PERL_UNUSED_CONTEXT; |
| 51 | PERL_UNUSED_ARG(pat); |
| 52 | #endif /* DEBUGGING */ |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | Perl_vdeb(pTHX_ const char *pat, va_list *args) |
| 57 | { |
| 58 | #ifdef DEBUGGING |
| 59 | dVAR; |
| 60 | const char* const file = PL_curcop ? OutCopFILE(PL_curcop) : "<null>"; |
| 61 | const char* const display_file = file ? file : "<free>"; |
| 62 | const long line = PL_curcop ? (long)CopLINE(PL_curcop) : 0; |
| 63 | |
| 64 | if (DEBUG_v_TEST) |
| 65 | PerlIO_printf(Perl_debug_log, "(%ld:%s:%ld)\t", |
| 66 | (long)PerlProc_getpid(), display_file, line); |
| 67 | else |
| 68 | PerlIO_printf(Perl_debug_log, "(%s:%ld)\t", display_file, line); |
| 69 | (void) PerlIO_vprintf(Perl_debug_log, pat, *args); |
| 70 | #else |
| 71 | PERL_UNUSED_CONTEXT; |
| 72 | PERL_UNUSED_ARG(pat); |
| 73 | PERL_UNUSED_ARG(args); |
| 74 | #endif /* DEBUGGING */ |
| 75 | } |
| 76 | |
| 77 | I32 |
| 78 | Perl_debstackptrs(pTHX) |
| 79 | { |
| 80 | #ifdef DEBUGGING |
| 81 | dVAR; |
| 82 | PerlIO_printf(Perl_debug_log, |
| 83 | "%8"UVxf" %8"UVxf" %8"IVdf" %8"IVdf" %8"IVdf"\n", |
| 84 | PTR2UV(PL_curstack), PTR2UV(PL_stack_base), |
| 85 | (IV)*PL_markstack_ptr, (IV)(PL_stack_sp-PL_stack_base), |
| 86 | (IV)(PL_stack_max-PL_stack_base)); |
| 87 | PerlIO_printf(Perl_debug_log, |
| 88 | "%8"UVxf" %8"UVxf" %8"UVuf" %8"UVuf" %8"UVuf"\n", |
| 89 | PTR2UV(PL_mainstack), PTR2UV(AvARRAY(PL_curstack)), |
| 90 | PTR2UV(PL_mainstack), PTR2UV(AvFILLp(PL_curstack)), |
| 91 | PTR2UV(AvMAX(PL_curstack))); |
| 92 | #endif /* DEBUGGING */ |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /* dump the contents of a particular stack |
| 98 | * Display stack_base[stack_min+1 .. stack_max], |
| 99 | * and display the marks whose offsets are contained in addresses |
| 100 | * PL_markstack[mark_min+1 .. mark_max] and whose values are in the range |
| 101 | * of the stack values being displayed |
| 102 | * |
| 103 | * Only displays top 30 max |
| 104 | */ |
| 105 | |
| 106 | STATIC void |
| 107 | S_deb_stack_n(pTHX_ SV** stack_base, I32 stack_min, I32 stack_max, |
| 108 | I32 mark_min, I32 mark_max) |
| 109 | { |
| 110 | #ifdef DEBUGGING |
| 111 | dVAR; |
| 112 | register I32 i = stack_max - 30; |
| 113 | const I32 *markscan = PL_markstack + mark_min; |
| 114 | if (i < stack_min) |
| 115 | i = stack_min; |
| 116 | |
| 117 | while (++markscan <= PL_markstack + mark_max) |
| 118 | if (*markscan >= i) |
| 119 | break; |
| 120 | |
| 121 | if (i > stack_min) |
| 122 | PerlIO_printf(Perl_debug_log, "... "); |
| 123 | |
| 124 | if (stack_base[0] != &PL_sv_undef || stack_max < 0) |
| 125 | PerlIO_printf(Perl_debug_log, " [STACK UNDERFLOW!!!]\n"); |
| 126 | do { |
| 127 | ++i; |
| 128 | if (markscan <= PL_markstack + mark_max && *markscan < i) { |
| 129 | do { |
| 130 | ++markscan; |
| 131 | PerlIO_putc(Perl_debug_log, '*'); |
| 132 | } |
| 133 | while (markscan <= PL_markstack + mark_max && *markscan < i); |
| 134 | PerlIO_printf(Perl_debug_log, " "); |
| 135 | } |
| 136 | if (i > stack_max) |
| 137 | break; |
| 138 | PerlIO_printf(Perl_debug_log, "%-4s ", SvPEEK(stack_base[i])); |
| 139 | } |
| 140 | while (1); |
| 141 | PerlIO_printf(Perl_debug_log, "\n"); |
| 142 | #else |
| 143 | PERL_UNUSED_CONTEXT; |
| 144 | PERL_UNUSED_ARG(stack_base); |
| 145 | PERL_UNUSED_ARG(stack_min); |
| 146 | PERL_UNUSED_ARG(stack_max); |
| 147 | PERL_UNUSED_ARG(mark_min); |
| 148 | PERL_UNUSED_ARG(mark_max); |
| 149 | #endif /* DEBUGGING */ |
| 150 | } |
| 151 | |
| 152 | |
| 153 | /* dump the current stack */ |
| 154 | |
| 155 | I32 |
| 156 | Perl_debstack(pTHX) |
| 157 | { |
| 158 | #ifndef SKIP_DEBUGGING |
| 159 | dVAR; |
| 160 | if (CopSTASH_eq(PL_curcop, PL_debstash) && !DEBUG_J_TEST_) |
| 161 | return 0; |
| 162 | |
| 163 | PerlIO_printf(Perl_debug_log, " => "); |
| 164 | deb_stack_n(PL_stack_base, |
| 165 | 0, |
| 166 | PL_stack_sp - PL_stack_base, |
| 167 | PL_curstackinfo->si_markoff, |
| 168 | PL_markstack_ptr - PL_markstack); |
| 169 | |
| 170 | |
| 171 | #endif /* SKIP_DEBUGGING */ |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | #ifdef DEBUGGING |
| 177 | static const char * const si_names[] = { |
| 178 | "UNKNOWN", |
| 179 | "UNDEF", |
| 180 | "MAIN", |
| 181 | "MAGIC", |
| 182 | "SORT", |
| 183 | "SIGNAL", |
| 184 | "OVERLOAD", |
| 185 | "DESTROY", |
| 186 | "WARNHOOK", |
| 187 | "DIEHOOK", |
| 188 | "REQUIRE" |
| 189 | }; |
| 190 | #endif |
| 191 | |
| 192 | /* display all stacks */ |
| 193 | |
| 194 | |
| 195 | void |
| 196 | Perl_deb_stack_all(pTHX) |
| 197 | { |
| 198 | #ifdef DEBUGGING |
| 199 | dVAR; |
| 200 | I32 si_ix; |
| 201 | const PERL_SI *si; |
| 202 | |
| 203 | /* rewind to start of chain */ |
| 204 | si = PL_curstackinfo; |
| 205 | while (si->si_prev) |
| 206 | si = si->si_prev; |
| 207 | |
| 208 | si_ix=0; |
| 209 | for (;;) |
| 210 | { |
| 211 | const size_t si_name_ix = si->si_type+1; /* -1 is a valid index */ |
| 212 | const char * const si_name = (si_name_ix >= sizeof(si_names)) ? "????" : si_names[si_name_ix]; |
| 213 | I32 ix; |
| 214 | PerlIO_printf(Perl_debug_log, "STACK %"IVdf": %s\n", |
| 215 | (IV)si_ix, si_name); |
| 216 | |
| 217 | for (ix=0; ix<=si->si_cxix; ix++) { |
| 218 | |
| 219 | const PERL_CONTEXT * const cx = &(si->si_cxstack[ix]); |
| 220 | PerlIO_printf(Perl_debug_log, |
| 221 | " CX %"IVdf": %-6s => ", |
| 222 | (IV)ix, PL_block_type[CxTYPE(cx)] |
| 223 | ); |
| 224 | /* substitution contexts don't save stack pointers etc) */ |
| 225 | if (CxTYPE(cx) == CXt_SUBST) |
| 226 | PerlIO_printf(Perl_debug_log, "\n"); |
| 227 | else { |
| 228 | |
| 229 | /* Find the the current context's stack range by searching |
| 230 | * forward for any higher contexts using this stack; failing |
| 231 | * that, it will be equal to the size of the stack for old |
| 232 | * stacks, or PL_stack_sp for the current stack |
| 233 | */ |
| 234 | |
| 235 | I32 i, stack_min, stack_max, mark_min, mark_max; |
| 236 | const PERL_CONTEXT *cx_n = NULL; |
| 237 | const PERL_SI *si_n; |
| 238 | |
| 239 | /* there's a separate stack per SI, so only search |
| 240 | * this one */ |
| 241 | |
| 242 | for (i=ix+1; i<=si->si_cxix; i++) { |
| 243 | if (CxTYPE(cx) == CXt_SUBST) |
| 244 | continue; |
| 245 | cx_n = &(si->si_cxstack[i]); |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | stack_min = cx->blk_oldsp; |
| 250 | |
| 251 | if (cx_n) { |
| 252 | stack_max = cx_n->blk_oldsp; |
| 253 | } |
| 254 | else if (si == PL_curstackinfo) { |
| 255 | stack_max = PL_stack_sp - AvARRAY(si->si_stack); |
| 256 | } |
| 257 | else { |
| 258 | stack_max = AvFILLp(si->si_stack); |
| 259 | } |
| 260 | |
| 261 | /* for the other stack types, there's only one stack |
| 262 | * shared between all SIs */ |
| 263 | |
| 264 | si_n = si; |
| 265 | i = ix; |
| 266 | cx_n = NULL; |
| 267 | for (;;) { |
| 268 | i++; |
| 269 | if (i > si_n->si_cxix) { |
| 270 | if (si_n == PL_curstackinfo) |
| 271 | break; |
| 272 | else { |
| 273 | si_n = si_n->si_next; |
| 274 | i = 0; |
| 275 | } |
| 276 | } |
| 277 | if (CxTYPE(&(si_n->si_cxstack[i])) == CXt_SUBST) |
| 278 | continue; |
| 279 | cx_n = &(si_n->si_cxstack[i]); |
| 280 | break; |
| 281 | } |
| 282 | |
| 283 | mark_min = cx->blk_oldmarksp; |
| 284 | if (cx_n) { |
| 285 | mark_max = cx_n->blk_oldmarksp; |
| 286 | } |
| 287 | else { |
| 288 | mark_max = PL_markstack_ptr - PL_markstack; |
| 289 | } |
| 290 | |
| 291 | deb_stack_n(AvARRAY(si->si_stack), |
| 292 | stack_min, stack_max, mark_min, mark_max); |
| 293 | |
| 294 | if (CxTYPE(cx) == CXt_EVAL || CxTYPE(cx) == CXt_SUB |
| 295 | || CxTYPE(cx) == CXt_FORMAT) |
| 296 | { |
| 297 | const OP * const retop = (CxTYPE(cx) == CXt_EVAL) |
| 298 | ? cx->blk_eval.retop : cx->blk_sub.retop; |
| 299 | |
| 300 | PerlIO_printf(Perl_debug_log, " retop=%s\n", |
| 301 | retop ? OP_NAME(retop) : "(null)" |
| 302 | ); |
| 303 | } |
| 304 | } |
| 305 | } /* next context */ |
| 306 | |
| 307 | |
| 308 | if (si == PL_curstackinfo) |
| 309 | break; |
| 310 | si = si->si_next; |
| 311 | si_ix++; |
| 312 | if (!si) |
| 313 | break; /* shouldn't happen, but just in case.. */ |
| 314 | } /* next stackinfo */ |
| 315 | |
| 316 | PerlIO_printf(Perl_debug_log, "\n"); |
| 317 | #else |
| 318 | PERL_UNUSED_CONTEXT; |
| 319 | #endif /* DEBUGGING */ |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Local variables: |
| 324 | * c-indentation-style: bsd |
| 325 | * c-basic-offset: 4 |
| 326 | * indent-tabs-mode: t |
| 327 | * End: |
| 328 | * |
| 329 | * ex: set ts=8 sts=4 sw=4 noet: |
| 330 | */ |