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