This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
PVFMs don't need CvDEPTH, and PVCVs don't use SvIVX, so moving
[perl5.git] / ext / Devel / Peek / Peek.xs
CommitLineData
c5be433b 1#define PERL_NO_GET_CONTEXT
3967c732
JD
2#include "EXTERN.h"
3#include "perl.h"
4#include "XSUB.h"
5
f681a178 6static bool
bd16a5f0
IZ
7_runops_debug(int flag)
8{
9 dTHX;
7698c435 10 const bool d = PL_runops == MEMBER_TO_FPTR(Perl_runops_debug);
bd16a5f0
IZ
11
12 if (flag >= 0)
13 PL_runops
14 = MEMBER_TO_FPTR(flag ? Perl_runops_debug : Perl_runops_standard);
15 return d;
16}
17
f681a178 18static SV *
cea2e8a9 19DeadCode(pTHX)
3967c732 20{
8ecf7187
GS
21#ifdef PURIFY
22 return Nullsv;
23#else
3967c732 24 SV* sva;
9c5ffd7c 25 SV* sv;
3967c732
JD
26 SV* ret = newRV_noinc((SV*)newAV());
27 register SV* svend;
28 int tm = 0, tref = 0, ts = 0, ta = 0, tas = 0;
29
30 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
31 svend = &sva[SvREFCNT(sva)];
32 for (sv = sva + 1; sv < svend; ++sv) {
33 if (SvTYPE(sv) == SVt_PVCV) {
34 CV *cv = (CV*)sv;
35 AV* padlist = CvPADLIST(cv), *argav;
36 SV** svp;
37 SV** pad;
38 int i = 0, j, levelm, totm = 0, levelref, totref = 0;
39 int levels, tots = 0, levela, tota = 0, levelas, totas = 0;
40 int dumpit = 0;
41
aed2304a 42 if (CvISXSUB(sv)) {
3967c732
JD
43 continue; /* XSUB */
44 }
45 if (!CvGV(sv)) {
46 continue; /* file-level scope. */
47 }
48 if (!CvROOT(cv)) {
bf49b057 49 /* PerlIO_printf(Perl_debug_log, " no root?!\n"); */
3967c732
JD
50 continue; /* autoloading stub. */
51 }
bf49b057 52 do_gvgv_dump(0, Perl_debug_log, "GVGV::GV", CvGV(sv));
3967c732 53 if (CvDEPTH(cv)) {
bf49b057 54 PerlIO_printf(Perl_debug_log, " busy\n");
3967c732
JD
55 continue;
56 }
57 svp = AvARRAY(padlist);
58 while (++i <= AvFILL(padlist)) { /* Depth. */
59 SV **args;
60
61 pad = AvARRAY((AV*)svp[i]);
62 argav = (AV*)pad[0];
63 if (!argav || (SV*)argav == &PL_sv_undef) {
bf49b057 64 PerlIO_printf(Perl_debug_log, " closure-template\n");
3967c732
JD
65 continue;
66 }
67 args = AvARRAY(argav);
68 levelm = levels = levelref = levelas = 0;
69 levela = sizeof(SV*) * (AvMAX(argav) + 1);
70 if (AvREAL(argav)) {
71 for (j = 0; j < AvFILL(argav); j++) {
72 if (SvROK(args[j])) {
bf49b057 73 PerlIO_printf(Perl_debug_log, " ref in args!\n");
3967c732
JD
74 levelref++;
75 }
76 /* else if (SvPOK(args[j]) && SvPVX(args[j])) { */
77 else if (SvTYPE(args[j]) >= SVt_PV && SvLEN(args[j])) {
78 levelas += SvLEN(args[j])/SvREFCNT(args[j]);
79 }
80 }
81 }
82 for (j = 1; j < AvFILL((AV*)svp[1]); j++) { /* Vars. */
83 if (SvROK(pad[j])) {
84 levelref++;
bf49b057 85 do_sv_dump(0, Perl_debug_log, pad[j], 0, 4, 0, 0);
3967c732
JD
86 dumpit = 1;
87 }
88 /* else if (SvPOK(pad[j]) && SvPVX(pad[j])) { */
89 else if (SvTYPE(pad[j]) >= SVt_PVAV) {
90 if (!SvPADMY(pad[j])) {
91 levelref++;
bf49b057 92 do_sv_dump(0, Perl_debug_log, pad[j], 0, 4, 0, 0);
3967c732
JD
93 dumpit = 1;
94 }
95 }
96 else if (SvTYPE(pad[j]) >= SVt_PV && SvLEN(pad[j])) {
3967c732
JD
97 levels++;
98 levelm += SvLEN(pad[j])/SvREFCNT(pad[j]);
99 /* Dump(pad[j],4); */
100 }
101 }
bf49b057 102 PerlIO_printf(Perl_debug_log, " level %i: refs: %i, strings: %i in %i,\targsarray: %i, argsstrings: %i\n",
3967c732
JD
103 i, levelref, levelm, levels, levela, levelas);
104 totm += levelm;
105 tota += levela;
106 totas += levelas;
107 tots += levels;
108 totref += levelref;
109 if (dumpit)
bf49b057 110 do_sv_dump(0, Perl_debug_log, (SV*)cv, 0, 2, 0, 0);
3967c732
JD
111 }
112 if (AvFILL(padlist) > 1) {
bf49b057 113 PerlIO_printf(Perl_debug_log, " total: refs: %i, strings: %i in %i,\targsarrays: %i, argsstrings: %i\n",
3967c732
JD
114 totref, totm, tots, tota, totas);
115 }
116 tref += totref;
117 tm += totm;
118 ts += tots;
119 ta += tota;
120 tas += totas;
121 }
122 }
123 }
bf49b057 124 PerlIO_printf(Perl_debug_log, "total: refs: %i, strings: %i in %i\targsarray: %i, argsstrings: %i\n", tref, tm, ts, ta, tas);
3967c732
JD
125
126 return ret;
3967c732 127#endif /* !PURIFY */
8ecf7187 128}
3967c732 129
7e84e004
SR
130#if (defined(PERL_DEBUGGING_MSTATS) || defined(DEBUGGING_MSTATS)) \
131 && (defined(MYMALLOC) && !defined(PLAIN_MALLOC))
3967c732
JD
132# define mstat(str) dump_mstats(str)
133#else
134# define mstat(str) \
bf49b057 135 PerlIO_printf(Perl_debug_log, "%s: perl not compiled with DEBUGGING_MSTATS\n",str);
3967c732
JD
136#endif
137
7e84e004
SR
138#if (defined(PERL_DEBUGGING_MSTATS) || defined(DEBUGGING_MSTATS)) \
139 && (defined(MYMALLOC) && !defined(PLAIN_MALLOC))
d1424c31
IZ
140
141/* Very coarse overestimate, 2-per-power-of-2, one more to determine NBUCKETS. */
142# define _NBUCKETS (2*8*IVSIZE+1)
143
144struct mstats_buffer
145{
146 perl_mstats_t buffer;
147 UV buf[_NBUCKETS*4];
148};
149
f681a178 150static void
d1424c31
IZ
151_fill_mstats(struct mstats_buffer *b, int level)
152{
c024d977 153 dTHX;
d1424c31
IZ
154 b->buffer.nfree = b->buf;
155 b->buffer.ntotal = b->buf + _NBUCKETS;
156 b->buffer.bucket_mem_size = b->buf + 2*_NBUCKETS;
157 b->buffer.bucket_available_size = b->buf + 3*_NBUCKETS;
158 Zero(b->buf, (level ? 4*_NBUCKETS: 2*_NBUCKETS), unsigned long);
159 get_mstats(&(b->buffer), _NBUCKETS, level);
160}
161
f681a178 162static void
d1424c31
IZ
163fill_mstats(SV *sv, int level)
164{
c024d977 165 dTHX;
d1424c31
IZ
166
167 if (SvREADONLY(sv))
168 croak("Cannot modify a readonly value");
169 SvGROW(sv, sizeof(struct mstats_buffer)+1);
170 _fill_mstats((struct mstats_buffer*)SvPVX(sv),level);
171 SvCUR_set(sv, sizeof(struct mstats_buffer));
172 *SvEND(sv) = '\0';
173 SvPOK_only(sv);
174}
175
f681a178 176static void
7698c435 177_mstats_to_hv(HV *hv, const struct mstats_buffer *b, int level)
d1424c31 178{
c024d977 179 dTHX;
d1424c31
IZ
180 SV **svp;
181 int type;
182
183 svp = hv_fetch(hv, "topbucket", 9, 1);
184 sv_setiv(*svp, b->buffer.topbucket);
185
186 svp = hv_fetch(hv, "topbucket_ev", 12, 1);
187 sv_setiv(*svp, b->buffer.topbucket_ev);
188
189 svp = hv_fetch(hv, "topbucket_odd", 13, 1);
190 sv_setiv(*svp, b->buffer.topbucket_odd);
191
192 svp = hv_fetch(hv, "totfree", 7, 1);
193 sv_setiv(*svp, b->buffer.totfree);
194
195 svp = hv_fetch(hv, "total", 5, 1);
196 sv_setiv(*svp, b->buffer.total);
197
198 svp = hv_fetch(hv, "total_chain", 11, 1);
199 sv_setiv(*svp, b->buffer.total_chain);
200
201 svp = hv_fetch(hv, "total_sbrk", 10, 1);
202 sv_setiv(*svp, b->buffer.total_sbrk);
203
204 svp = hv_fetch(hv, "sbrks", 5, 1);
205 sv_setiv(*svp, b->buffer.sbrks);
206
207 svp = hv_fetch(hv, "sbrk_good", 9, 1);
208 sv_setiv(*svp, b->buffer.sbrk_good);
209
210 svp = hv_fetch(hv, "sbrk_slack", 10, 1);
211 sv_setiv(*svp, b->buffer.sbrk_slack);
212
213 svp = hv_fetch(hv, "start_slack", 11, 1);
214 sv_setiv(*svp, b->buffer.start_slack);
215
216 svp = hv_fetch(hv, "sbrked_remains", 14, 1);
217 sv_setiv(*svp, b->buffer.sbrked_remains);
218
219 svp = hv_fetch(hv, "minbucket", 9, 1);
220 sv_setiv(*svp, b->buffer.minbucket);
221
222 svp = hv_fetch(hv, "nbuckets", 8, 1);
223 sv_setiv(*svp, b->buffer.nbuckets);
224
225 if (_NBUCKETS < b->buffer.nbuckets)
226 warn("FIXME: internal mstats buffer too short");
227
228 for (type = 0; type < (level ? 4 : 2); type++) {
d05c1ba0 229 UV *p = 0, *p1 = 0;
d1424c31
IZ
230 AV *av;
231 int i;
232 static const char *types[4] = {
233 "free", "used", "mem_size", "available_size"
234 };
235
236 svp = hv_fetch(hv, types[type], strlen(types[type]), 1);
237
238 if (SvOK(*svp) && !(SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVAV))
239 croak("Unexpected value for the key '%s' in the mstats hash", types[type]);
240 if (!SvOK(*svp)) {
241 av = newAV();
d05c1ba0 242 (void)SvUPGRADE(*svp, SVt_RV);
b162af07 243 SvRV_set(*svp, (SV*)av);
d1424c31
IZ
244 SvROK_on(*svp);
245 } else
246 av = (AV*)SvRV(*svp);
247
248 av_extend(av, b->buffer.nbuckets - 1);
249 /* XXXX What is the official way to reduce the size of the array? */
250 switch (type) {
251 case 0:
252 p = b->buffer.nfree;
253 break;
254 case 1:
255 p = b->buffer.ntotal;
256 p1 = b->buffer.nfree;
257 break;
258 case 2:
259 p = b->buffer.bucket_mem_size;
260 break;
261 case 3:
262 p = b->buffer.bucket_available_size;
263 break;
264 }
265 for (i = 0; i < b->buffer.nbuckets; i++) {
266 svp = av_fetch(av, i, 1);
267 if (type == 1)
268 sv_setiv(*svp, p[i]-p1[i]);
269 else
270 sv_setuv(*svp, p[i]);
271 }
272 }
273}
f681a178
AT
274
275static void
d1424c31
IZ
276mstats_fillhash(SV *sv, int level)
277{
278 struct mstats_buffer buf;
279
280 if (!(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV))
281 croak("Not a hash reference");
282 _fill_mstats(&buf, level);
283 _mstats_to_hv((HV *)SvRV(sv), &buf, level);
284}
f681a178
AT
285
286static void
d1424c31
IZ
287mstats2hash(SV *sv, SV *rv, int level)
288{
289 if (!(SvROK(rv) && SvTYPE(SvRV(rv)) == SVt_PVHV))
290 croak("Not a hash reference");
291 if (!SvPOK(sv))
292 croak("Undefined value when expecting mstats buffer");
293 if (SvCUR(sv) != sizeof(struct mstats_buffer))
294 croak("Wrong size for a value with a mstats buffer");
295 _mstats_to_hv((HV *)SvRV(rv), (struct mstats_buffer*)SvPVX(sv), level);
296}
297#else /* !( defined(PERL_DEBUGGING_MSTATS) || defined(DEBUGGING_MSTATS) \ ) */
f681a178 298static void
d1424c31
IZ
299fill_mstats(SV *sv, int level)
300{
301 croak("Cannot report mstats without Perl malloc");
302}
f681a178
AT
303
304static void
d1424c31
IZ
305mstats_fillhash(SV *sv, int level)
306{
307 croak("Cannot report mstats without Perl malloc");
308}
f681a178
AT
309
310static void
d1424c31
IZ
311mstats2hash(SV *sv, SV *rv, int level)
312{
313 croak("Cannot report mstats without Perl malloc");
314}
315#endif /* defined(PERL_DEBUGGING_MSTATS) || defined(DEBUGGING_MSTATS)... */
316
83ee9e09
GS
317#define _CvGV(cv) \
318 (SvROK(cv) && (SvTYPE(SvRV(cv))==SVt_PVCV) \
15bcf759 319 ? SvREFCNT_inc(CvGV((CV*)SvRV(cv))) : &PL_sv_undef)
83ee9e09 320
3967c732
JD
321MODULE = Devel::Peek PACKAGE = Devel::Peek
322
323void
324mstat(str="Devel::Peek::mstat: ")
325char *str
326
327void
d1424c31
IZ
328fill_mstats(SV *sv, int level = 0)
329
330void
331mstats_fillhash(SV *sv, int level = 0)
332 PROTOTYPE: \%;$
333
334void
335mstats2hash(SV *sv, SV *rv, int level = 0)
336 PROTOTYPE: $\%;$
337
338void
3967c732
JD
339Dump(sv,lim=4)
340SV * sv
341I32 lim
342PPCODE:
343{
344 SV *pv_lim_sv = perl_get_sv("Devel::Peek::pv_limit", FALSE);
7698c435 345 const STRLEN pv_lim = pv_lim_sv ? SvIV(pv_lim_sv) : 0;
3967c732 346 SV *dumpop = perl_get_sv("Devel::Peek::dump_ops", FALSE);
7698c435 347 const I32 save_dumpindent = PL_dumpindent;
3967c732 348 PL_dumpindent = 2;
eb160463
GS
349 do_sv_dump(0, Perl_debug_log, sv, 0, lim,
350 (bool)(dumpop && SvTRUE(dumpop)), pv_lim);
3967c732
JD
351 PL_dumpindent = save_dumpindent;
352}
353
354void
355DumpArray(lim,...)
356I32 lim
357PPCODE:
358{
359 long i;
360 SV *pv_lim_sv = perl_get_sv("Devel::Peek::pv_limit", FALSE);
7698c435 361 const STRLEN pv_lim = pv_lim_sv ? SvIV(pv_lim_sv) : 0;
3967c732 362 SV *dumpop = perl_get_sv("Devel::Peek::dump_ops", FALSE);
7698c435 363 const I32 save_dumpindent = PL_dumpindent;
3967c732
JD
364 PL_dumpindent = 2;
365
366 for (i=1; i<items; i++) {
7b0972df 367 PerlIO_printf(Perl_debug_log, "Elt No. %ld 0x%"UVxf"\n", i - 1, PTR2UV(ST(i)));
eb160463
GS
368 do_sv_dump(0, Perl_debug_log, ST(i), 0, lim,
369 (bool)(dumpop && SvTRUE(dumpop)), pv_lim);
3967c732
JD
370 }
371 PL_dumpindent = save_dumpindent;
372}
373
374void
375DumpProg()
376PPCODE:
377{
d2560b70 378 warn("dumpindent is %d", (int)PL_dumpindent);
3967c732
JD
379 if (PL_main_root)
380 op_dump(PL_main_root);
381}
382
383I32
384SvREFCNT(sv)
385SV * sv
386
387# PPCODE needed since otherwise sv_2mortal is inserted that will kill the value.
388
389SV *
390SvREFCNT_inc(sv)
391SV * sv
392PPCODE:
393{
394 RETVAL = SvREFCNT_inc(sv);
395 PUSHs(RETVAL);
396}
397
398# PPCODE needed since by default it is void
399
8063af02 400void
3967c732
JD
401SvREFCNT_dec(sv)
402SV * sv
403PPCODE:
404{
405 SvREFCNT_dec(sv);
406 PUSHs(sv);
407}
408
409SV *
410DeadCode()
cea2e8a9
GS
411CODE:
412 RETVAL = DeadCode(aTHX);
413OUTPUT:
414 RETVAL
83ee9e09
GS
415
416MODULE = Devel::Peek PACKAGE = Devel::Peek PREFIX = _
417
418SV *
419_CvGV(cv)
420 SV *cv
bd16a5f0
IZ
421
422bool
423_runops_debug(int flag = -1)