This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regexec.c: Change MiXeD cAsE formal macro parameters
[perl5.git] / deb.c
1 /*    deb.c
2  *
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
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,
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"]
16  */
17
18 /*
19  * This file contains various utilities for producing debugging output
20  * (mainly related to displaying the stack)
21  */
22
23 #include "EXTERN.h"
24 #define PERL_IN_DEB_C
25 #include "perl.h"
26
27 #if defined(PERL_IMPLICIT_CONTEXT)
28 void
29 Perl_deb_nocontext(const char *pat, ...)
30 {
31 #ifdef DEBUGGING
32     dTHX;
33     va_list args;
34     PERL_ARGS_ASSERT_DEB_NOCONTEXT;
35     va_start(args, pat);
36     vdeb(pat, &args);
37     va_end(args);
38 #else
39     PERL_UNUSED_ARG(pat);
40 #endif /* DEBUGGING */
41 }
42 #endif
43
44 void
45 Perl_deb(pTHX_ const char *pat, ...)
46 {
47     va_list args;
48     PERL_ARGS_ASSERT_DEB;
49     va_start(args, pat);
50 #ifdef DEBUGGING
51     vdeb(pat, &args);
52 #else
53     PERL_UNUSED_CONTEXT;
54 #endif /* DEBUGGING */
55     va_end(args);
56 }
57
58 void
59 Perl_vdeb(pTHX_ const char *pat, va_list *args)
60 {
61 #ifdef DEBUGGING
62     dVAR;
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;
66
67     PERL_ARGS_ASSERT_VDEB;
68
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);
74     (void) PerlIO_vprintf(Perl_debug_log, pat, *args);
75 #else
76     PERL_UNUSED_CONTEXT;
77     PERL_UNUSED_ARG(pat);
78     PERL_UNUSED_ARG(args);
79 #endif /* DEBUGGING */
80 }
81
82 I32
83 Perl_debstackptrs(pTHX)
84 {
85 #ifdef DEBUGGING
86     dVAR;
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)));
97 #else
98     PERL_UNUSED_CONTEXT;
99 #endif /* DEBUGGING */
100     return 0;
101 }
102
103
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  */
112
113 STATIC void
114 S_deb_stack_n(pTHX_ SV** stack_base, I32 stack_min, I32 stack_max,
115         I32 mark_min, I32 mark_max)
116 {
117 #ifdef DEBUGGING
118     dVAR;
119     I32 i = stack_max - 30;
120     const I32 *markscan = PL_markstack + mark_min;
121
122     PERL_ARGS_ASSERT_DEB_STACK_N;
123
124     if (i < stack_min)
125         i = stack_min;
126     
127     while (++markscan <= PL_markstack + mark_max)
128         if (*markscan >= i)
129             break;
130
131     if (i > stack_min)
132         PerlIO_printf(Perl_debug_log, "... ");
133
134     if (stack_base[0] != &PL_sv_undef || stack_max < 0)
135         PerlIO_printf(Perl_debug_log, " [STACK UNDERFLOW!!!]\n");
136     do {
137         ++i;
138         if (markscan <= PL_markstack + mark_max && *markscan < i) {
139             do {
140                 ++markscan;
141                 PerlIO_putc(Perl_debug_log, '*');
142             }
143             while (markscan <= PL_markstack + mark_max && *markscan < i);
144             PerlIO_printf(Perl_debug_log, "  ");
145         }
146         if (i > stack_max)
147             break;
148         PerlIO_printf(Perl_debug_log, "%-4s  ", SvPEEK(stack_base[i]));
149     }
150     while (1);
151     PerlIO_printf(Perl_debug_log, "\n");
152 #else
153     PERL_UNUSED_CONTEXT;
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);
159 #endif /* DEBUGGING */
160 }
161
162
163 /* dump the current stack */
164
165 I32
166 Perl_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
180 #endif /* SKIP_DEBUGGING */
181     return 0;
182 }
183
184
185 #ifdef DEBUGGING
186 static const char * const si_names[] = {
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
204 void
205 Perl_deb_stack_all(pTHX)
206 {
207 #ifdef DEBUGGING
208     dVAR;
209     I32 si_ix;
210     const PERL_SI *si;
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     {
220         const size_t si_name_ix = si->si_type+1; /* -1 is a valid index */
221         const char * const si_name =
222             si_name_ix < C_ARRAY_LENGTH(si_names) ?
223             si_names[si_name_ix] : "????";
224         I32 ix;
225         PerlIO_printf(Perl_debug_log, "STACK %"IVdf": %s\n",
226                                                 (IV)si_ix, si_name);
227
228         for (ix=0; ix<=si->si_cxix; ix++) {
229
230             const PERL_CONTEXT * const cx = &(si->si_cxstack[ix]);
231             PerlIO_printf(Perl_debug_log,
232                     "  CX %"IVdf": %-6s => ",
233                     (IV)ix, PL_block_type[CxTYPE(cx)]
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;
247                 const PERL_CONTEXT *cx_n = NULL;
248                 const PERL_SI *si_n;
249
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;
277                 cx_n = NULL;
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;
295                 if (cx_n) {
296                     mark_max  = cx_n->blk_oldmarksp;
297                 }
298                 else {
299                     mark_max = PL_markstack_ptr - PL_markstack;
300                 }
301
302                 deb_stack_n(AvARRAY(si->si_stack),
303                         stack_min, stack_max, mark_min, mark_max);
304
305                 if (CxTYPE(cx) == CXt_EVAL || CxTYPE(cx) == CXt_SUB
306                         || CxTYPE(cx) == CXt_FORMAT)
307                 {
308                     const OP * const retop = cx->blk_sub.retop;
309
310                     PerlIO_printf(Perl_debug_log, "  retop=%s\n",
311                             retop ? OP_NAME(retop) : "(null)"
312                     );
313                 }
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");
327 #else
328     PERL_UNUSED_CONTEXT;
329 #endif /* DEBUGGING */
330 }
331
332 /*
333  * Local variables:
334  * c-indentation-style: bsd
335  * c-basic-offset: 4
336  * indent-tabs-mode: nil
337  * End:
338  *
339  * ex: set ts=8 sts=4 sw=4 et:
340  */