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