This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add a reference to books.perl.org.
[perl5.git] / deb.c
1 /*    deb.c
2  *
3  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
4  *    2000, 2001, 2002, 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 #endif /* DEBUGGING */
48 }
49
50 void
51 Perl_vdeb(pTHX_ const char *pat, va_list *args)
52 {
53 #ifdef DEBUGGING
54     char* file = OutCopFILE(PL_curcop);
55
56     PerlIO_printf(Perl_debug_log, "(%s:%ld)\t", (file ? file : "<free>"),
57                   (long)CopLINE(PL_curcop));
58     (void) PerlIO_vprintf(Perl_debug_log, pat, *args);
59 #endif /* DEBUGGING */
60 }
61
62 I32
63 Perl_debstackptrs(pTHX)
64 {
65 #ifdef DEBUGGING
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)));
76 #endif /* DEBUGGING */
77     return 0;
78 }
79
80
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  */
89
90 STATIC void
91 S_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;
96     I32 *markscan = PL_markstack + mark_min;
97     if (i < stack_min)
98         i = stack_min;
99     
100     while (++markscan <= PL_markstack + mark_max)
101         if (*markscan >= i)
102             break;
103
104     if (i > stack_min)
105         PerlIO_printf(Perl_debug_log, "... ");
106
107     if (stack_base[0] != &PL_sv_undef || stack_max < 0)
108         PerlIO_printf(Perl_debug_log, " [STACK UNDERFLOW!!!]\n");
109     do {
110         ++i;
111         if (markscan <= PL_markstack + mark_max && *markscan < i) {
112             do {
113                 ++markscan;
114                 PerlIO_putc(Perl_debug_log, '*');
115             }
116             while (markscan <= PL_markstack + mark_max && *markscan < i);
117             PerlIO_printf(Perl_debug_log, "  ");
118         }
119         if (i > stack_max)
120             break;
121         PerlIO_printf(Perl_debug_log, "%-4s  ", SvPEEK(stack_base[i]));
122     }
123     while (1);
124     PerlIO_printf(Perl_debug_log, "\n");
125 #endif /* DEBUGGING */
126 }
127
128
129 /* dump the current stack */
130
131 I32
132 Perl_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
146 #endif /* SKIP_DEBUGGING */
147     return 0;
148 }
149
150
151 #ifdef DEBUGGING
152 static char * si_names[] = {
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
170 void
171 Perl_deb_stack_all(pTHX)
172 {
173 #ifdef DEBUGGING
174     I32          ix, si_ix;
175     PERL_SI      *si;
176     PERL_CONTEXT *cx;
177
178     /* rewind to start of chain */
179     si = PL_curstackinfo;
180     while (si->si_prev)
181         si = si->si_prev;
182
183     si_ix=0;
184     for (;;)
185     {
186         char *si_name;
187         int si_name_ix = si->si_type+1; /* -1 is a valid index */
188         if (si_name_ix>= sizeof(si_names))
189             si_name = "????";
190         else
191             si_name = si_names[si_name_ix];
192         PerlIO_printf(Perl_debug_log, "STACK %"IVdf": %s\n",
193                                                 (IV)si_ix, si_name);
194
195         for (ix=0; ix<=si->si_cxix; ix++) {
196
197             cx = &(si->si_cxstack[ix]);
198             PerlIO_printf(Perl_debug_log,
199                     "  CX %"IVdf": %-6s => ",
200                     (IV)ix, PL_block_type[CxTYPE(cx)]
201             );
202             /* substitution contexts don't save stack pointers etc) */
203             if (CxTYPE(cx) == CXt_SUBST)
204                 PerlIO_printf(Perl_debug_log, "\n");
205             else {
206
207                 /* Find the the current context's stack range by searching
208                  * forward for any higher contexts using this stack; failing
209                  * that, it will be equal to the size of the stack for old
210                  * stacks, or PL_stack_sp for the current stack
211                  */
212
213                 I32 i, stack_min, stack_max, mark_min, mark_max;
214                 PERL_CONTEXT *cx_n;
215                 PERL_SI      *si_n;
216                 OP           *retop;
217
218                 cx_n = Null(PERL_CONTEXT*);
219
220                 /* there's a separate stack per SI, so only search
221                  * this one */
222
223                 for (i=ix+1; i<=si->si_cxix; i++) {
224                     if (CxTYPE(cx) == CXt_SUBST)
225                         continue;
226                     cx_n = &(si->si_cxstack[i]);
227                     break;
228                 }
229
230                 stack_min = cx->blk_oldsp;
231
232                 if (cx_n) {
233                     stack_max = cx_n->blk_oldsp;
234                 }
235                 else if (si == PL_curstackinfo) {
236                     stack_max = PL_stack_sp - AvARRAY(si->si_stack);
237                 }
238                 else {
239                     stack_max = AvFILLp(si->si_stack);
240                 }
241
242                 /* for the other stack types, there's only one stack
243                  * shared between all SIs */
244
245                 si_n = si;
246                 i = ix;
247                 cx_n = Null(PERL_CONTEXT*);
248                 for (;;) {
249                     i++;
250                     if (i > si_n->si_cxix) {
251                         if (si_n == PL_curstackinfo)
252                             break;
253                         else {
254                             si_n = si_n->si_next;
255                             i = 0;
256                         }
257                     }
258                     if (CxTYPE(&(si_n->si_cxstack[i])) == CXt_SUBST)
259                         continue;
260                     cx_n = &(si_n->si_cxstack[i]);
261                     break;
262                 }
263
264                 mark_min  = cx->blk_oldmarksp;
265                 if (cx_n) {
266                     mark_max  = cx_n->blk_oldmarksp;
267                 }
268                 else {
269                     mark_max = PL_markstack_ptr - PL_markstack;
270                 }
271
272                 deb_stack_n(AvARRAY(si->si_stack),
273                         stack_min, stack_max, mark_min, mark_max);
274
275                 if (CxTYPE(cx) == CXt_EVAL || CxTYPE(cx) == CXt_SUB
276                         || CxTYPE(cx) == CXt_FORMAT)
277                 {
278                     retop = (CxTYPE(cx) == CXt_EVAL)
279                             ? cx->blk_eval.retop : cx->blk_sub.retop;
280
281                     PerlIO_printf(Perl_debug_log, "  retop=%s\n",
282                             retop ? OP_NAME(retop) : "(null)"
283                     );
284                 }
285             }
286         } /* next context */
287
288
289         if (si == PL_curstackinfo)
290             break;
291         si = si->si_next;
292         si_ix++;
293         if (!si)
294             break; /* shouldn't happen, but just in case.. */
295     } /* next stackinfo */
296
297     PerlIO_printf(Perl_debug_log, "\n");
298 #endif /* DEBUGGING */
299 }
300
301