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