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