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