| 1 |
|
| #include "tracecollector.h" |
| 2 |
|
| #include "tracefile.h" |
| 3 |
|
| |
| 4 |
|
| using namespace devel::statprofiler; |
| 5 |
|
| |
| 6 |
|
| extern MGVTBL Devel_StatProfiler_eval_idx_vtbl; |
| 7 |
|
| |
| 8 |
|
| |
| 9 |
|
| // needs to be kept in sync with S_dopoptosub in pp_ctl.c |
| 10 |
|
| STATIC I32 |
| 11 |
228422 |
| S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock) |
| 12 |
|
| { |
| 13 |
|
| dVAR; |
| 14 |
|
| I32 i; |
| 15 |
|
| |
| 16 |
360858 |
▶ | for (i = startingblock; i >= 0; i--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = startingblock; i >= 0; i--) { |
335364 |
25494 |
360858 |
|
| 17 |
335364 |
| const PERL_CONTEXT * const cx = &cxstk[i]; |
| 18 |
335364 |
| switch (CxTYPE(cx)) { |
| 19 |
132436 |
| default: |
| 20 |
132436 |
| continue; |
| 21 |
159285 |
| case CXt_SUB: |
| 22 |
|
| /* in sub foo { /(?{...})/ }, foo ends up on the CX stack |
| 23 |
|
| * twice; the first for the normal foo() call, and the second |
| 24 |
|
| * for a faked up re-entry into the sub to execute the |
| 25 |
|
| * code block. Hide this faked entry from the world. */ |
| 26 |
|
| #if PERL_VERSION >= 18 |
| 27 |
159285 |
▶ | if (cx->cx_type & CXp_SUB_RE_FAKE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cx->cx_type & CXp_SUB_RE_FAKE) |
0 |
159285 |
159285 |
|
| 28 |
0 |
| continue; |
| 29 |
|
| #endif |
| 30 |
|
| case CXt_EVAL: |
| 31 |
|
| case CXt_FORMAT: |
| 32 |
|
| DEBUG_l( Perl_deb(aTHX_ "(dopoptosub_at(): found sub at cx=%ld)\n", (long)i)); |
| 33 |
202928 |
| return i; |
| 34 |
|
| } |
| 35 |
|
| } |
| 36 |
25494 |
| return i; |
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
|
| |
| 40 |
|
| static void |
| 41 |
52 |
| save_eval_once(pTHX_ TraceFileWriter &trace, const PERL_CONTEXT *frame) { |
| 42 |
52 |
| SV *eval_text = frame->blk_eval.cur_text; |
| 43 |
52 |
| EvalCollected *collected = get_or_attach_evalcollected(aTHX_ eval_text, false); // overwrite not OK |
| 44 |
|
| |
| 45 |
52 |
▶ | if (!collected->saved) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!collected->saved) { |
34 |
18 |
52 |
|
| 46 |
34 |
| trace.add_eval_source(eval_text, collected->evalseq); |
| 47 |
34 |
| collected->saved = true; |
| 48 |
|
| } |
| 49 |
52 |
| } |
| 50 |
|
| |
| 51 |
|
| |
| 52 |
|
| // needs to be kept in sync with Perl_caller_cx in op.c |
| 53 |
|
| void |
| 54 |
24243 |
| devel::statprofiler::collect_trace(pTHX_ TraceFileWriter &trace, int depth, bool eval_source) |
| 55 |
|
| { |
| 56 |
24243 |
| I32 cxix = S_dopoptosub_at(aTHX_ cxstack, cxstack_ix); |
| 57 |
24243 |
| const PERL_CONTEXT *ccstack = cxstack; |
| 58 |
24243 |
| const PERL_SI *top_si = PL_curstackinfo; |
| 59 |
24243 |
▶ | CV *db_sub = PL_DBsub ? GvCV(PL_DBsub) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| CV *db_sub = PL_DBsub ? GvCV(PL_DBsub) : NULL; |
24182 |
61 |
24243 |
|
| 60 |
24243 |
| COP *line = PL_curcop; |
| 61 |
24243 |
| bool previous_is_eval_block = false; |
| 62 |
|
| |
| 63 |
|
| for (;;) { |
| 64 |
|
| // skip over auxiliary stacks pushed by PUSHSTACKi |
| 65 |
228422 |
▶ | while (cxix < 0 && top_si->si_type != PERLSI_MAIN) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (cxix < 0 && top_si->si_type != PERLSI_MAIN) { |
25494 |
202928 |
228422 |
| while (cxix < 0 && top_si->si_type != PERLSI_MAIN) { |
1256 |
24238 |
25494 |
|
| 66 |
1256 |
| top_si = top_si->si_prev; |
| 67 |
1256 |
| ccstack = top_si->si_cxstack; |
| 68 |
1256 |
| cxix = S_dopoptosub_at(aTHX_ ccstack, top_si->si_cxix); |
| 69 |
|
| } |
| 70 |
227166 |
▶ | if (cxix < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cxix < 0) |
24238 |
202928 |
227166 |
|
| 71 |
24238 |
| break; |
| 72 |
|
| // do not report the automatic calls to &DB::sub |
| 73 |
202928 |
▶ | if (!(db_sub && cxix >= 0 && |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(db_sub && cxix >= 0 && |
0 |
202928 |
202928 |
| if (!(db_sub && cxix >= 0 && |
0 |
0 |
0 |
|
| 74 |
0 |
▶ | ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))) { |
Branch: 1 branch
| Branch | True | False | Total |
| ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))) { |
0 |
0 |
0 |
|
| 75 |
202928 |
| const PERL_CONTEXT *sub = &ccstack[cxix]; |
| 76 |
202928 |
| const PERL_CONTEXT *caller = sub; |
| 77 |
|
| |
| 78 |
202928 |
▶ | if (db_sub) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (db_sub) { |
0 |
202928 |
202928 |
|
| 79 |
|
| // when there is a &DB::sub, we need its call site to get |
| 80 |
|
| // the correct file/line information |
| 81 |
0 |
| I32 dbcxix = S_dopoptosub_at(aTHX_ ccstack, cxix - 1); |
| 82 |
|
| |
| 83 |
0 |
▶ | if (dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == db_sub) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == db_sub) { |
0 |
0 |
0 |
| if (dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == db_sub) { |
0 |
0 |
0 |
|
| 84 |
0 |
| caller = &ccstack[dbcxix]; |
| 85 |
0 |
| cxix = dbcxix; |
| 86 |
|
| } |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
|
| // when called between an entersub and the following nextstate, |
| 90 |
|
| // ignore the set-up but-not-entered-yet stack frame |
| 91 |
|
| // also ignore the call frame set up for BEGIN blocks |
| 92 |
202928 |
| int cxt = CxTYPE(sub); |
| 93 |
202928 |
| bool is_eval_block = CxTRYBLOCK(sub); |
| 94 |
|
| // check the stack frame has been entered |
| 95 |
202928 |
▶ | if ((line != caller->blk_oldcop || |
Branch: 2 branches
| Branch | True | False | Total |
| if ((line != caller->blk_oldcop || |
16114 |
186814 |
202928 |
| if ((line != caller->blk_oldcop || |
3912 |
12202 |
16114 |
|
| 96 |
|
| // but make sure it's not a recursive sub |
| 97 |
3912 |
| cxt != CXt_SUB || |
| 98 |
3912 |
▶ | sub->blk_sub.cv != NULL && |
Branch: 1 branch
| Branch | True | False | Total |
| sub->blk_sub.cv != NULL && |
3912 |
0 |
3912 |
|
| 99 |
202928 |
▶ | PL_op != CvSTART(sub->blk_sub.cv)) && |
Branch: 2 branches
| Branch | True | False | Total |
| PL_op != CvSTART(sub->blk_sub.cv)) && |
3896 |
16 |
3912 |
| PL_op != CvSTART(sub->blk_sub.cv)) && |
191821 |
11091 |
202912 |
|
| 100 |
|
| // ignore stack frames set up by BEGIN blocks |
| 101 |
191821 |
| line != &PL_compiling && |
| 102 |
|
| // ignore eval BLOCK |
| 103 |
191821 |
▶ | !is_eval_block) { |
Branch: 1 branch
| Branch | True | False | Total |
| !is_eval_block) { |
160626 |
31195 |
191821 |
|
| 104 |
160626 |
▶ | if (cxt != CXt_EVAL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cxt != CXt_EVAL) { |
159269 |
1357 |
160626 |
|
| 105 |
159269 |
| trace.add_frame(FRAME_SUB, sub->blk_sub.cv, NULL, line); |
| 106 |
1357 |
▶ | } else if (CxOLD_OP_TYPE(sub) != OP_ENTEREVAL) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (CxOLD_OP_TYPE(sub) != OP_ENTEREVAL) { |
1226 |
131 |
1357 |
|
| 107 |
1226 |
| trace.add_frame(FRAME_MAIN, NULL, NULL, line); |
| 108 |
|
| } else { |
| 109 |
131 |
▶ | if (eval_source) |
Branch: 1 branch
| Branch | True | False | Total |
| if (eval_source) |
52 |
79 |
131 |
|
| 110 |
|
| // this branch should always be entered with an |
| 111 |
|
| // EvalCollector attached to the source |
| 112 |
52 |
| save_eval_once(aTHX_ trace, sub); |
| 113 |
|
| |
| 114 |
131 |
| trace.add_frame(FRAME_EVAL, NULL, NULL, line); |
| 115 |
|
| } |
| 116 |
|
| } |
| 117 |
|
| else { |
| 118 |
42302 |
| ++depth; |
| 119 |
|
| |
| 120 |
|
| // BEGIN block in an eval "" |
| 121 |
42302 |
▶ | if (cxt == CXt_EVAL && CxOLD_OP_TYPE(sub) == OP_ENTEREVAL) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (cxt == CXt_EVAL && CxOLD_OP_TYPE(sub) == OP_ENTEREVAL) { |
42286 |
16 |
42302 |
| if (cxt == CXt_EVAL && CxOLD_OP_TYPE(sub) == OP_ENTEREVAL) { |
5 |
42281 |
42286 |
|
| 122 |
5 |
▶ | if (eval_source) |
Branch: 1 branch
| Branch | True | False | Total |
| if (eval_source) |
0 |
5 |
5 |
|
| 123 |
0 |
| save_eval_once(aTHX_ trace, sub); |
| 124 |
|
| } |
| 125 |
|
| } |
| 126 |
202928 |
▶ | if (!is_eval_block) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_eval_block) |
160738 |
42190 |
202928 |
|
| 127 |
160738 |
| line = caller->blk_oldcop; |
| 128 |
|
| |
| 129 |
202928 |
| previous_is_eval_block = is_eval_block; |
| 130 |
202928 |
▶ | if (!--depth) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!--depth) |
5 |
202923 |
202928 |
|
| 131 |
5 |
| break; |
| 132 |
|
| } |
| 133 |
202923 |
| cxix = S_dopoptosub_at(aTHX_ ccstack, cxix - 1); |
| 134 |
202923 |
| } |
| 135 |
|
| |
| 136 |
|
| // report main, but ignore the stack frame set up for BEGIN blocks |
| 137 |
24243 |
▶ | if (depth && line != &PL_compiling) |
Branch: 2 branches
| Branch | True | False | Total |
| if (depth && line != &PL_compiling) |
24238 |
5 |
24243 |
| if (depth && line != &PL_compiling) |
13243 |
10995 |
24238 |
|
| 138 |
13243 |
| trace.add_frame(FRAME_MAIN, NULL, NULL, line); |
| 139 |
24243 |
| } |
| 140 |
|
| |
| 141 |
|
| EvalCollected * |
| 142 |
1398 |
| devel::statprofiler::get_or_attach_evalcollected(pTHX_ SV *eval_text, bool overwrite_if_needed) |
| 143 |
|
| { |
| 144 |
1398 |
▶ | MAGIC *marker = SvMAGICAL(eval_text) ? mg_findext(eval_text, PERL_MAGIC_ext, &Devel_StatProfiler_eval_idx_vtbl) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| MAGIC *marker = SvMAGICAL(eval_text) ? mg_findext(eval_text, PERL_MAGIC_ext, &Devel_StatProfiler_eval_idx_vtbl) : NULL; |
52 |
1346 |
1398 |
|
| 145 |
|
| EvalCollected *collected; |
| 146 |
|
| |
| 147 |
1398 |
▶ | if (!marker) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!marker) { |
1346 |
52 |
1398 |
|
| 148 |
1346 |
| EvalCollected data(PL_breakable_sub_gen, PL_evalseq); |
| 149 |
|
| |
| 150 |
1346 |
▶ | marker = sv_magicext(eval_text, NULL, PERL_MAGIC_ext, |
Branch: 1 branch
| Branch | True | False | Total |
| marker = sv_magicext(eval_text, NULL, PERL_MAGIC_ext, |
1346 |
0 |
1346 |
|
| 151 |
|
| &Devel_StatProfiler_eval_idx_vtbl, |
| 152 |
|
| (const char *)&data, sizeof(data)); |
| 153 |
1346 |
| collected = (EvalCollected *) marker->mg_ptr; |
| 154 |
|
| } else { |
| 155 |
52 |
| collected = (EvalCollected *) marker->mg_ptr; |
| 156 |
|
| |
| 157 |
|
| // this is going to break in case of recursive evals that are |
| 158 |
|
| // eval()ing the same SV. I'm going to ignore the issue. |
| 159 |
52 |
▶ | if (collected->evalseq != PL_evalseq && overwrite_if_needed) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (collected->evalseq != PL_evalseq && overwrite_if_needed) { |
3 |
49 |
52 |
| if (collected->evalseq != PL_evalseq && overwrite_if_needed) { |
0 |
3 |
3 |
|
| 160 |
0 |
| collected->sub_gen = PL_breakable_sub_gen; |
| 161 |
0 |
| collected->evalseq = PL_evalseq; |
| 162 |
0 |
| collected->saved = false; |
| 163 |
|
| } |
| 164 |
|
| } |
| 165 |
|
| |
| 166 |
1398 |
| return collected; |
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
|
| MGVTBL Devel_StatProfiler_eval_idx_vtbl; |