3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
4 * 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
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.
12 * 'Didst thou think that the eyes of the White Tower were blind? Nay,
13 * I have seen more than thou knowest, Grey Fool.' --Denethor
15 * [p.853 of _The Lord of the Rings_, V/vii: "The Pyre of Denethor"]
19 * This file contains various utilities for producing debugging output
20 * (mainly related to displaying the stack)
27 #if defined(MULTIPLICITY)
29 Perl_deb_nocontext(const char *pat, ...)
34 PERL_ARGS_ASSERT_DEB_NOCONTEXT;
40 #endif /* DEBUGGING */
46 =for apidoc_item deb_nocontext
49 When perl is compiled with C<-DDEBUGGING>, these each print to STDERR the
50 information given by the arguments, prefaced by the name of the file containing
51 the script causing the call, and the line number within that file.
53 If the C<v> (verbose) debugging option is in effect, the process id is also
56 C<deb> and C<deb_nocontext> differ only in that C<deb_nocontext> does not take
57 a thread context (C<aTHX>) parameter, so is used in situations where the caller
58 doesn't already have the thread context.
60 C<vdeb> is the same as C<deb> except C<args> are an encapsulated argument list.
66 Perl_deb(pTHX_ const char *pat, ...)
75 #endif /* DEBUGGING */
80 Perl_vdeb(pTHX_ const char *pat, va_list *args)
83 const char* const file = PL_curcop ? OutCopFILE(PL_curcop) : "<null>";
84 const char* const display_file = file ? file : "<free>";
85 line_t line = PL_curcop ? CopLINE(PL_curcop) : NOLINE;
89 PERL_ARGS_ASSERT_VDEB;
92 PerlIO_printf(Perl_debug_log, "(%ld:%s:%" LINE_Tf ")\t",
93 (long)PerlProc_getpid(), display_file, line);
95 PerlIO_printf(Perl_debug_log, "(%s:%" LINE_Tf ")\t",
97 (void) PerlIO_vprintf(Perl_debug_log, pat, *args);
100 PERL_UNUSED_ARG(pat);
101 PERL_UNUSED_ARG(args);
102 #endif /* DEBUGGING */
106 Perl_debstackptrs(pTHX) /* Currently unused in cpan and core */
109 PerlIO_printf(Perl_debug_log,
110 "%8" UVxf " %8" UVxf " %8" IVdf " %8" IVdf " %8" IVdf "\n",
111 PTR2UV(PL_curstack), PTR2UV(PL_stack_base),
112 (IV)*PL_markstack_ptr, (IV)(PL_stack_sp-PL_stack_base),
113 (IV)(PL_stack_max-PL_stack_base));
114 PerlIO_printf(Perl_debug_log,
115 "%8" UVxf " %8" UVxf " %8" UVuf " %8" UVuf " %8" UVuf "\n",
116 PTR2UV(PL_mainstack), PTR2UV(AvARRAY(PL_curstack)),
117 PTR2UV(PL_mainstack), PTR2UV(AvFILLp(PL_curstack)),
118 PTR2UV(AvMAX(PL_curstack)));
121 #endif /* DEBUGGING */
126 /* dump the contents of a particular stack
127 * Display stack_base[stack_min+1 .. stack_max],
128 * and display the marks whose offsets are contained in addresses
129 * PL_markstack[mark_min+1 .. mark_max] and whose values are in the range
130 * of the stack values being displayed
131 * On PERL_RC_STACK builds, nonrc_base indicates the lowest
132 * non-reference-counted stack element (or 0 if none or not such a build).
133 * Display a vertical bar at this position.
135 * Only displays top 30 max
139 S_deb_stack_n(pTHX_ SV** stack_base, SSize_t stack_min, SSize_t stack_max,
140 SSize_t mark_min, SSize_t mark_max, SSize_t nonrc_base)
143 SSize_t i = stack_max - 30;
144 const Stack_off_t *markscan = PL_markstack + mark_min;
146 PERL_ARGS_ASSERT_DEB_STACK_N;
151 while (++markscan <= PL_markstack + mark_max)
156 PerlIO_printf(Perl_debug_log, "... ");
158 if (stack_base[0] != &PL_sv_undef || stack_max < 0)
159 PerlIO_printf(Perl_debug_log, " [STACK UNDERFLOW!!!]\n");
162 if (markscan <= PL_markstack + mark_max && *markscan < i) {
165 (void)PerlIO_putc(Perl_debug_log, '*');
167 while (markscan <= PL_markstack + mark_max && *markscan < i);
168 PerlIO_printf(Perl_debug_log, " ");
173 PerlIO_printf(Perl_debug_log, "%-4s ", SvPEEK(stack_base[i]));
175 if (nonrc_base && nonrc_base == i + 1)
176 PerlIO_printf(Perl_debug_log, "| ");
179 PerlIO_printf(Perl_debug_log, "\n");
182 PERL_UNUSED_ARG(stack_base);
183 PERL_UNUSED_ARG(stack_min);
184 PERL_UNUSED_ARG(stack_max);
185 PERL_UNUSED_ARG(mark_min);
186 PERL_UNUSED_ARG(mark_max);
187 PERL_UNUSED_ARG(nonrc_base);
188 #endif /* DEBUGGING */
195 Dump the current stack
203 #ifndef SKIP_DEBUGGING
204 if (CopSTASH_eq(PL_curcop, PL_debstash) && !DEBUG_J_TEST_)
207 PerlIO_printf(Perl_debug_log, " => ");
208 S_deb_stack_n(aTHX_ PL_stack_base,
210 PL_stack_sp - PL_stack_base,
211 PL_curstackinfo->si_markoff,
212 PL_markstack_ptr - PL_markstack,
213 # ifdef PERL_RC_STACK
214 PL_curstackinfo->si_stack_nonrc_base
221 #endif /* SKIP_DEBUGGING */
227 static const char * const si_names[] = {
246 /* display all stacks */
250 Perl_deb_stack_all(pTHX)
256 /* rewind to start of chain */
257 si = PL_curstackinfo;
264 const size_t si_name_ix = si->si_type+1; /* -1 is a valid index */
265 const char * const si_name =
266 si_name_ix < C_ARRAY_LENGTH(si_names) ?
267 si_names[si_name_ix] : "????";
269 PerlIO_printf(Perl_debug_log, "STACK %" IVdf ": %s%s\n",
271 # ifdef PERL_RC_STACK
273 ? (si->si_stack_nonrc_base ? " (partial real)" : " (real)")
280 for (ix=0; ix<=si->si_cxix; ix++) {
282 const PERL_CONTEXT * const cx = &(si->si_cxstack[ix]);
283 PerlIO_printf(Perl_debug_log,
284 " CX %" IVdf ": %-6s => ",
285 (IV)ix, PL_block_type[CxTYPE(cx)]
287 /* substitution contexts don't save stack pointers etc) */
288 if (CxTYPE(cx) == CXt_SUBST)
289 PerlIO_printf(Perl_debug_log, "\n");
292 /* Find the current context's stack range by searching
293 * forward for any higher contexts using this stack; failing
294 * that, it will be equal to the size of the stack for old
295 * stacks, or PL_stack_sp for the current stack
298 I32 i, stack_min, stack_max, mark_min, mark_max;
299 const PERL_CONTEXT *cx_n = NULL;
302 /* there's a separate argument stack per SI, so only
305 for (i=ix+1; i<=si->si_cxix; i++) {
306 const PERL_CONTEXT *this_cx = &(si->si_cxstack[i]);
307 if (CxTYPE(this_cx) == CXt_SUBST)
313 stack_min = cx->blk_oldsp;
316 stack_max = cx_n->blk_oldsp;
318 else if (si == PL_curstackinfo) {
319 stack_max = PL_stack_sp - AvARRAY(si->si_stack);
322 stack_max = AvFILLp(si->si_stack);
325 /* for the markstack, there's only one stack shared
333 if (i > si_n->si_cxix) {
334 if (si_n == PL_curstackinfo)
337 si_n = si_n->si_next;
341 if (CxTYPE(&(si_n->si_cxstack[i])) == CXt_SUBST)
343 if (si_n->si_cxix >= 0)
344 cx_n = &(si_n->si_cxstack[i]);
350 mark_min = cx->blk_oldmarksp;
352 mark_max = cx_n->blk_oldmarksp;
355 mark_max = PL_markstack_ptr - PL_markstack;
358 S_deb_stack_n(aTHX_ AvARRAY(si->si_stack),
359 stack_min, stack_max, mark_min, mark_max,
360 # ifdef PERL_RC_STACK
361 si->si_stack_nonrc_base
367 if (CxTYPE(cx) == CXt_EVAL || CxTYPE(cx) == CXt_SUB
368 || CxTYPE(cx) == CXt_FORMAT)
370 const OP * const retop = cx->blk_sub.retop;
372 PerlIO_printf(Perl_debug_log, " retop=%s\n",
373 retop ? OP_NAME(retop) : "(null)"
380 if (si == PL_curstackinfo)
385 break; /* shouldn't happen, but just in case.. */
386 } /* next stackinfo */
388 PerlIO_printf(Perl_debug_log, "\n");
391 #endif /* DEBUGGING */
395 * ex: set ts=8 sts=4 sw=4 et: