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