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