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