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