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