Cover.xs

filter: Statementstmt 82.0% 119 / 145 Branchbran 54.6% 59 / 108 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.3% 178 / 253 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Test2/EventFacet/Coverage.pm ↑ Summary
1 #include <EXTERN.h>
2 #include <perl.h>
3 #include <XSUB.h>
4 #include <stdio.h>
5  
6 static GV *sub_to_gv(pTHX_ SV *sv);
7 Perl_ppaddr_t orig_subhandler;
8 Perl_ppaddr_t orig_openhandler;
9 Perl_ppaddr_t orig_sysopenhandler;
10  
11 // If we do not use threads we will make this global
12 // The performance impact of fetching it each time is significant, so avoid it
13 // if we can.
14 #ifdef USE_ITHREADS
15 #define fetch_report HV *report = get_hv("Test2::Plugin::Cover::REPORT", GV_ADDMULTI);
16 #define fetch_opens AV *opens = get_av("Test2::Plugin::Cover::OPENS", GV_ADDMULTI);
17 #define fetch_from SV *from = get_sv("Test2::Plugin::Cover::FROM", 0);
18 #define fetch_root SV *root = get_sv("Test2::Plugin::Cover::ROOT", 0);
19 #define fetch_enabled SV *enabled = get_sv("Test2::Plugin::Cover::ENABLED", 0);
20 #define fetch_trace_opens SV *trace_opens = get_sv("Test2::Plugin::Cover::TRACE_OPENS", 0);
21 #else
22 HV *report;
23 AV *opens;
24 // Cache the GVs, not the SVs, so local() (which swaps the SV in the glob)
25 // still resolves to the live value.
26 GV *from_gv;
27 GV *root_gv;
28 GV *enabled_gv;
29 GV *trace_opens_gv;
30 #define fetch_report NOOP
31 #define fetch_opens NOOP
32 #define fetch_from SV *from = GvSV(from_gv);
33 #define fetch_root SV *root = GvSV(root_gv);
34 #define fetch_enabled SV *enabled = GvSV(enabled_gv);
35 #define fetch_trace_opens SV *trace_opens = GvSV(trace_opens_gv);
36 #endif
37  
38 720367 void add_entry(char *fname, STRLEN fnamelen, char *sname, STRLEN snamelen) {
39 fetch_report;
40 720367 HV *file = NULL;
41 720367 SV **existing_file = hv_fetch(report, fname, fnamelen, 0);
42 720367 ▶ if (existing_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing_file) { 720199 168 720367
43 720199 file = (HV *)SvRV(*existing_file);
44 }
45 else {
46 168 file = newHV();
47 168 hv_store(report, fname, fnamelen, newRV_noinc((SV *)file), 0);
48 }
49  
50 720367 HV *sub = NULL;
51 720367 SV **existing_sub = hv_fetch(file, sname, snamelen, 0);
52 720367 ▶ if (existing_sub) {
Branch: 1 branch
BranchTrueFalseTotal
if (existing_sub) { 720140 227 720367
53 720140 sub = (HV *)SvRV(*existing_sub);
54 }
55 else {
56 227 sub = newHV();
57 227 hv_store(file, sname, snamelen, newRV_noinc((SV *)sub), 0);
58 }
59  
60 720367 fetch_from;
61 720367 ▶ if (!(from && SvOK(from))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(from && SvOK(from))) { 720367 0 720367
if (!(from && SvOK(from))) { 0 720367 720367
62 0 ▶ if (!hv_exists(sub, "*", 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists(sub, "*", 1)) { 0 0 0
63 0 from = newSVpv("*", 1);
64 0 hv_store_ent(sub, from, from, 0);
65 }
66 }
67 720367 ▶ else if (!hv_exists_ent(sub, from, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!hv_exists_ent(sub, from, 0)) { 252 720115 720367
68 252 from = sv_mortalcopy(from);
69 252 SvREFCNT_inc(from);
70 252 hv_store_ent(sub, from, from, 0);
71 }
72  
73 720367 return;
74 }
75  
76 594809 static OP* my_subhandler(pTHX) {
77 594809 dSP;
78 // Grab the sub SV before running the original handler, an XS call may
79 // overwrite this stack slot with a return value.
80 594809 SV *sub_sv = *SP;
81 594809 OP* out = orig_subhandler(aTHX);
82  
83 594800 fetch_enabled;
84 594800 ▶ if (!SvTRUE(enabled)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvTRUE(enabled)) { 31647 563153 594800
85 31647 return out;
86 }
87  
88 563153 ▶ if (out != NULL && (out->op_type == OP_NEXTSTATE || out->op_type == OP_DBSTATE)) {
Branch: 3 branches
BranchTrueFalseTotal
if (out != NULL && (out->op_type == OP_NEXTSTATE || out->op_type == OP_DBSTATE)) { 561369 1784 563153
if (out != NULL && (out->op_type == OP_NEXTSTATE || out->op_type == OP_DBSTATE)) { 1073 560296 561369
if (out != NULL && (out->op_type == OP_NEXTSTATE || out->op_type == OP_DBSTATE)) { 0 1073 1073
89 560296 ▶ char *fname = CopFILE(cCOPx(out));
Branch: 1 branch
BranchTrueFalseTotal
char *fname = CopFILE(cCOPx(out)); 560296 0 560296
90 560296 STRLEN namelen = strlen(fname);
91  
92 // Check for absolute paths and reject them. This is a very
93 // unix-oriented optimization.
94 560296 ▶ if (!strncmp(fname, "/", 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!strncmp(fname, "/", 1)) { 40145 520151 560296
95 40145 fetch_root;
96  
97 40145 ▶ if (root != NULL && SvPOK(root)) {
Branch: 2 branches
BranchTrueFalseTotal
if (root != NULL && SvPOK(root)) { 40145 0 40145
if (root != NULL && SvPOK(root)) { 40145 0 40145
98 STRLEN len;
99 40145 char *rt = NULL;
100 40145 rt = SvPV(root, len);
101  
102 48184 ▶ if (namelen < len) return out;
Branch: 1 branch
BranchTrueFalseTotal
if (namelen < len) return out; 31906 8239 40145
103  
104 8239 ▶ if (strncmp(fname, rt, len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(fname, rt, len)) { 8039 200 8239
105 8039 return out;
106 }
107 }
108 }
109  
110 520351 char *subname = NULL;
111 520351 STRLEN sublen = 0;
112  
113 520351 GV *my_gv = sub_to_gv(aTHX_ sub_sv);
114 520351 ▶ if (my_gv != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (my_gv != NULL) { 520351 0 520351
115 520351 subname = GvNAME(my_gv);
116 520351 sublen = strlen(subname);
117 }
118 else {
119 0 subname = "*";
120 0 sublen = 1;
121 }
122  
123 520351 add_entry(fname, namelen, subname, sublen);
124 }
125  
126 523208 return out;
127 }
128  
129 // Copied and modified from Devel::NYTProf
130 520351 static GV *sub_to_gv(pTHX_ SV *sv) {
131 520351 CV *cv = NULL;
132  
133 /* copied from top of perl's pp_entersub */
134 /* modified to return either CV or else a GV */
135 /* or a NULL in cases that pp_entersub would croak */
136 520351 switch (SvTYPE(sv)) {
137 26 default:
138 26 ▶ if (!SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) { 0 26 26
139 0 char *sym = NULL;
140  
141 0 ▶ if (sv == &PL_sv_yes) { /* unfound import, ignore */
Branch: 1 branch
BranchTrueFalseTotal
if (sv == &PL_sv_yes) { /* unfound import, ignore */ 0 0 0
142 0 return NULL;
143 }
144 0 ▶ if (SvGMAGICAL(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(sv)) { 0 0 0
145 // pp_entersub already ran get-magic; use the cached value.
146 // Calling mg_get() here would re-run FETCH (arbitrary perl
147 // code) from inside the op hook.
148 0 ▶ if (SvROK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) 0 0 0
149 0 goto got_rv;
150 0 ▶ sym = SvPOKp(sv) ? SvPVX(sv) : Nullch;
Branch: 1 branch
BranchTrueFalseTotal
sym = SvPOKp(sv) ? SvPVX(sv) : Nullch; 0 0 0
151 }
152 // else {
153 // This causes the warnings from issue #2 https://github.com/Test-More/Test2-Plugin-Cover/issues/2
154 //sym = SvPV_nolen(sv);
155 // }
156  
157 0 ▶ if (!sym)
Branch: 1 branch
BranchTrueFalseTotal
if (!sym) 0 0 0
158 0 return NULL;
159 0 ▶ if (PL_op->op_private & HINT_STRICT_REFS)
Branch: 1 branch
BranchTrueFalseTotal
if (PL_op->op_private & HINT_STRICT_REFS) 0 0 0
160 0 return NULL;
161 0 cv = get_cv(sym, TRUE);
162 0 break;
163 }
164 26 got_rv:
165 // No amagic (&{} overload) dereference here, pp_entersub already
166 // did it and re-invoking it would run arbitrary perl code from
167 // inside the op hook. Non-CV refs just fall through to NULL.
168 26 cv = (CV*)SvRV(sv);
169 26 ▶ if (SvTYPE(cv) == SVt_PVCV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(cv) == SVt_PVCV) 26 0 26
170 26 break;
171  
172 /* FALL THROUGH */
173 case SVt_PVHV:
174 case SVt_PVAV:
175 0 return NULL;
176  
177 321 case SVt_PVCV:
178 321 cv = (CV*)sv;
179 321 break;
180  
181 520004 case SVt_PVGV:
182 520004 ▶ if (!(isGV_with_GP(sv) && (cv = GvCVu((GV*)sv)))) {
Branch: 5 branches
BranchTrueFalseTotal
if (!(isGV_with_GP(sv) && (cv = GvCVu((GV*)sv)))) { 520004 0 520004
if (!(isGV_with_GP(sv) && (cv = GvCVu((GV*)sv)))) { 0 520004 520004
if (!(isGV_with_GP(sv) && (cv = GvCVu((GV*)sv)))) { 0 0 0
if (!(isGV_with_GP(sv) && (cv = GvCVu((GV*)sv)))) { 520004 0 520004
if (!(isGV_with_GP(sv) && (cv = GvCVu((GV*)sv)))) { 0 520004 520004
183 0 HV *stash = NULL;
184 0 GV *gv = NULL;
185 0 cv = sv_2cv(sv, &stash, &gv, FALSE);
186  
187 0 ▶ if (gv) {
Branch: 1 branch
BranchTrueFalseTotal
if (gv) { 0 0 0
188 0 return gv;
189 }
190 }
191  
192 520004 ▶ if (!cv) { /* would autoload in this situation */
Branch: 1 branch
BranchTrueFalseTotal
if (!cv) { /* would autoload in this situation */ 0 520004 520004
193 0 return NULL;
194 }
195  
196 520004 break;
197 }
198  
199 520351 ▶ if (cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv) { 520351 0 520351
200 520351 GV *out = CvGV(cv);
201 520351 ▶ if (out && isGV_with_GP(out)) {
Branch: 4 branches
BranchTrueFalseTotal
if (out && isGV_with_GP(out)) { 520351 0 520351
if (out && isGV_with_GP(out)) { 520351 0 520351
if (out && isGV_with_GP(out)) { 0 520351 520351
if (out && isGV_with_GP(out)) { 0 0 0
202 520351 return out;
203 }
204 }
205  
206 0 return NULL;
207 }
208  
209 200035 void _sv_file_handler(SV *filename) {
210 400066 ▶ if (filename == NULL) return;
Branch: 1 branch
BranchTrueFalseTotal
if (filename == NULL) return; 0 200035 200035
211 200035 ▶ if (!SvPOKp(filename)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvPOKp(filename)) return; 19 200016 200035
212  
213 200016 STRLEN namelen = 0;
214 200016 char *fname = SvPV(filename, namelen);
215  
216 200016 add_entry(fname, namelen, "<>", 2);
217  
218 200016 fetch_trace_opens;
219 200016 ▶ if (!SvTRUE(trace_opens)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvTRUE(trace_opens)) return; 200012 4 200016
220  
221 4 const PERL_CONTEXT* cx = cxstack;
222 4 AV* row = newAV();
223  
224 4 av_push(row, newSVpvn(fname, namelen));
225 4 ▶ av_push(row, newSVpv(OutCopFILE(PL_curcop), 0));
Branch: 1 branch
BranchTrueFalseTotal
av_push(row, newSVpv(OutCopFILE(PL_curcop), 0)); 4 0 4
226 4 av_push(row, newSViv(CopLINE(PL_curcop)));
227  
228 4 ▶ SV* package = newSVpv(CopSTASHPV(PL_curcop), 0);
Branch: 7 branches
BranchTrueFalseTotal
SV* package = newSVpv(CopSTASHPV(PL_curcop), 0); 4 0 4
SV* package = newSVpv(CopSTASHPV(PL_curcop), 0); 4 0 4
SV* package = newSVpv(CopSTASHPV(PL_curcop), 0); 4 0 4
SV* package = newSVpv(CopSTASHPV(PL_curcop), 0); 0 4 4
SV* package = newSVpv(CopSTASHPV(PL_curcop), 0); 0 0 0
SV* package = newSVpv(CopSTASHPV(PL_curcop), 0); 4 0 4
SV* package = newSVpv(CopSTASHPV(PL_curcop), 0); 0 4 4
229 4 av_push(row, package);
230  
231 fetch_opens;
232 4 av_push(opens, newRV_noinc((SV *)row));
233 }
234  
235 100049 static OP* my_openhandler(pTHX) {
236 100049 dSP;
237  
238 100049 fetch_enabled;
239 100049 ▶ if (SvTRUE(enabled)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(enabled)) { 100030 19 100049
240 100030 SV **mark = PL_stack_base + TOPMARK;
241 100030 I32 items = (I32)(sp - mark);
242  
243 // Only grab for 2-arg or 3-arg form
244 100030 ▶ if (items == 2 || items == 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (items == 2 || items == 3) { 100024 6 100030
if (items == 2 || items == 3) { 100023 1 100024
245 100029 _sv_file_handler(TOPs);
246 }
247 }
248  
249 100049 return orig_openhandler(aTHX);
250 }
251  
252 100007 static OP* my_sysopenhandler(pTHX) {
253 100007 dSP;
254  
255 100007 fetch_enabled;
256 100007 ▶ if (SvTRUE(enabled)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(enabled)) { 100006 1 100007
257 // pp_sysopen is fixed-arity, its pushmark is nulled at compile time,
258 // so TOPMARK belongs to an enclosing op and cannot be used to find
259 // the args (doing so read arbitrary stack slots and could segv).
260 // Index from the stack top instead: gv, filename, mode, [perms].
261 100006 I32 args = MAXARG;
262 100006 ▶ if (args >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (args >= 3) { 100006 0 100006
263 100006 _sv_file_handler(*(sp - (args - 2)));
264 }
265 }
266  
267 100007 return orig_sysopenhandler(aTHX);
268 }
269  
270 MODULE = Test2::Plugin::Cover PACKAGE = Test2::Plugin::Cover
271  
272 PROTOTYPES: ENABLE
273  
274 BOOT:
275 {
276 // If the module is reloaded (%INC cleared + re-required) BOOT runs
277 // again; without this guard the second run captures our own handlers
278 // as "orig" and the next sub call recurses infinitely (segfault).
279 static int booted = 0;
280 16 ▶ if (!booted) {
Branch: 1 branch
BranchTrueFalseTotal
if (!booted) { 15 1 16
281 15 booted = 1;
282  
283 //Initialize the global files HV, but only if we are not a threaded perl
284 #ifndef USE_ITHREADS
285 15 report = get_hv("Test2::Plugin::Cover::REPORT", GV_ADDMULTI);
286 15 SvREFCNT_inc(report);
287 15 opens = get_av("Test2::Plugin::Cover::OPENS", GV_ADDMULTI);
288 15 SvREFCNT_inc(opens);
289  
290 15 from_gv = gv_fetchpv("Test2::Plugin::Cover::FROM", GV_ADDMULTI, SVt_PV);
291 15 root_gv = gv_fetchpv("Test2::Plugin::Cover::ROOT", GV_ADDMULTI, SVt_PV);
292 15 enabled_gv = gv_fetchpv("Test2::Plugin::Cover::ENABLED", GV_ADDMULTI, SVt_PV);
293 15 trace_opens_gv = gv_fetchpv("Test2::Plugin::Cover::TRACE_OPENS", GV_ADDMULTI, SVt_PV);
294 15 SvREFCNT_inc((SV *)from_gv);
295 15 SvREFCNT_inc((SV *)root_gv);
296 15 SvREFCNT_inc((SV *)enabled_gv);
297 15 SvREFCNT_inc((SV *)trace_opens_gv);
298 #endif
299  
300 15 orig_subhandler = PL_ppaddr[OP_ENTERSUB];
301 15 PL_ppaddr[OP_ENTERSUB] = my_subhandler;
302  
303 15 orig_openhandler = PL_ppaddr[OP_OPEN];
304 15 PL_ppaddr[OP_OPEN] = my_openhandler;
305  
306 15 orig_sysopenhandler = PL_ppaddr[OP_SYSOPEN];
307 15 PL_ppaddr[OP_SYSOPEN] = my_sysopenhandler;
308 }
309 }
« blib/lib/Test2/EventFacet/Coverage.pm ↑ Summary