| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| #define NEED_PL_parser_GLOBAL |
| 5 |
|
| #define NEED_newRV_noinc_GLOBAL |
| 6 |
|
| #define NEED_sv_2pv_flags_GLOBAL |
| 7 |
|
| #include "ppport.h" |
| 8 |
|
| |
| 9 |
|
| #include "hook_op_check.h" |
| 10 |
|
| #include "hook_op_ppaddr.h" |
| 11 |
|
| |
| 12 |
|
| #ifndef CvISXSUB |
| 13 |
|
| # define CvISXSUB(cv) CvXSUB(cv) |
| 14 |
|
| #endif |
| 15 |
|
| |
| 16 |
|
| static int trycatch_debug = 0; |
| 17 |
|
| |
| 18 |
|
| STATIC I32 |
| 19 |
0 |
| dump_cxstack() |
| 20 |
|
| { |
| 21 |
|
| I32 i; |
| 22 |
0 |
▶ | for (i = cxstack_ix; i >= 0; i--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = cxstack_ix; i >= 0; i--) { |
0 |
0 |
0 |
|
| 23 |
0 |
| register const PERL_CONTEXT * const cx = cxstack+i; |
| 24 |
0 |
| switch (CxTYPE(cx)) { |
| 25 |
0 |
| default: |
| 26 |
0 |
| continue; |
| 27 |
0 |
| case CXt_EVAL: |
| 28 |
0 |
| printf("***\n* eval stack %d: WA: %d\n", (int)i, cx->blk_gimme); |
| 29 |
|
| /* sv_dump((SV*)cx->blk_eval.cv); */ |
| 30 |
0 |
| break; |
| 31 |
0 |
| case CXt_SUB: |
| 32 |
0 |
| printf("***\n* cx stack %d: WA: %d\n", (int)i, cx->blk_gimme); |
| 33 |
0 |
| sv_dump((SV*)cx->blk_sub.cv); |
| 34 |
0 |
| break; |
| 35 |
|
| } |
| 36 |
|
| } |
| 37 |
0 |
| return i; |
| 38 |
|
| } |
| 39 |
|
| |
| 40 |
|
| /* Return the (array)context of the first subroutine context up the Cx stack */ |
| 41 |
66 |
| int get_sub_context() |
| 42 |
|
| { |
| 43 |
|
| I32 i; |
| 44 |
234 |
▶ | for (i = cxstack_ix; i >= 0; i--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = cxstack_ix; i >= 0; i--) { |
227 |
7 |
234 |
|
| 45 |
227 |
| register const PERL_CONTEXT * const cx = cxstack+i; |
| 46 |
227 |
▶ | switch (CxTYPE(cx)) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (CxTYPE(cx)) { |
168 |
59 |
227 |
|
| 47 |
168 |
| default: |
| 48 |
168 |
| continue; |
| 49 |
59 |
| case CXt_SUB: |
| 50 |
59 |
| return cx->blk_gimme; |
| 51 |
|
| } |
| 52 |
|
| } |
| 53 |
7 |
| return G_VOID; |
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
|
| |
| 57 |
|
| /* the implementation of 'return' op inside try blocks. */ |
| 58 |
|
| STATIC OP* |
| 59 |
22 |
| try_return (pTHX_ OP *op, void *user_data) { |
| 60 |
22 |
| dSP; |
| 61 |
|
| SV* ctx; |
| 62 |
|
| CV *unwind; |
| 63 |
|
| |
| 64 |
|
| PERL_UNUSED_VAR(op); |
| 65 |
|
| PERL_UNUSED_VAR(user_data); |
| 66 |
|
| |
| 67 |
22 |
| ctx = get_sv("TryCatch::CTX", 0); |
| 68 |
22 |
▶ | if (ctx) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx) { |
0 |
22 |
22 |
|
| 69 |
0 |
▶ | XPUSHs( ctx ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( ctx ); |
0 |
0 |
0 |
|
| 70 |
0 |
| PUTBACK; |
| 71 |
0 |
▶ | if (trycatch_debug & 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (trycatch_debug & 2) { |
0 |
0 |
0 |
|
| 72 |
0 |
| printf("have a $CTX of %d\n", SvIV(ctx)); |
| 73 |
|
| } |
| 74 |
|
| } else { |
| 75 |
22 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
22 |
22 |
|
| 76 |
22 |
| PUTBACK; |
| 77 |
|
| |
| 78 |
22 |
| call_pv("Scope::Upper::SUB", G_SCALAR); |
| 79 |
22 |
▶ | if (trycatch_debug & 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (trycatch_debug & 2) { |
0 |
22 |
22 |
|
| 80 |
0 |
| printf("No ctx, making it up\n"); |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
22 |
| SPAGAIN; |
| 84 |
|
| } |
| 85 |
|
| |
| 86 |
22 |
▶ | if (trycatch_debug & 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (trycatch_debug & 2) { |
0 |
22 |
22 |
|
| 87 |
0 |
| printf("unwinding to %d\n", (int)SvIV(*sp)); |
| 88 |
|
| |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
|
| |
| 92 |
|
| /* Can't use call_sv et al. since it resets PL_op. */ |
| 93 |
|
| /* call_pv("Scope::Upper::unwind", G_VOID); */ |
| 94 |
|
| |
| 95 |
22 |
| unwind = get_cv("Scope::Upper::unwind", 0); |
| 96 |
22 |
▶ | XPUSHs( (SV*)unwind); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( (SV*)unwind); |
0 |
22 |
22 |
|
| 97 |
22 |
| PUTBACK; |
| 98 |
|
| |
| 99 |
|
| /* pp_entersub gets the XSUB arguments from @_ if there are any. |
| 100 |
|
| * Bypass this as we pushed the arguments directly on the stack. */ |
| 101 |
|
| |
| 102 |
22 |
▶ | if (CvISXSUB(unwind)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (CvISXSUB(unwind)) |
22 |
0 |
22 |
|
| 103 |
22 |
| AvFILLp(GvAV(PL_defgv)) = -1; |
| 104 |
|
| |
| 105 |
22 |
| return CALL_FPTR(PL_ppaddr[OP_ENTERSUB])(aTHX); |
| 106 |
|
| } |
| 107 |
|
| |
| 108 |
|
| /* The implementation of wantarray op/keyword inside try blocks. */ |
| 109 |
|
| STATIC OP* |
| 110 |
6 |
| try_wantarray( pTHX_ OP *op, void *user_data ) { |
| 111 |
|
| dVAR; |
| 112 |
6 |
| dSP; |
| 113 |
6 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
6 |
6 |
|
| 114 |
|
| |
| 115 |
|
| PERL_UNUSED_VAR(op); |
| 116 |
|
| PERL_UNUSED_VAR(user_data); |
| 117 |
|
| |
| 118 |
|
| /* We want the context from the closest subroutine, not from the closest |
| 119 |
|
| * block |
| 120 |
|
| */ |
| 121 |
6 |
| switch ( get_sub_context() ) { |
| 122 |
2 |
| case G_ARRAY: |
| 123 |
2 |
| RETPUSHYES; |
| 124 |
2 |
| case G_SCALAR: |
| 125 |
2 |
| RETPUSHNO; |
| 126 |
2 |
| default: |
| 127 |
2 |
| RETPUSHUNDEF; |
| 128 |
|
| } |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
|
| |
| 132 |
|
| /* After the scope has been created, fix up the context of the C<eval {}> block */ |
| 133 |
|
| STATIC OP* |
| 134 |
60 |
| try_after_entertry(pTHX_ OP *op, void *user_data) { |
| 135 |
60 |
| PERL_CONTEXT * cx = cxstack+cxstack_ix; |
| 136 |
60 |
| cx->blk_gimme = get_sub_context(); |
| 137 |
60 |
| return op; |
| 138 |
|
| } |
| 139 |
|
| |
| 140 |
|
| |
| 141 |
|
| |
| 142 |
|
| STATIC OP* |
| 143 |
38615 |
| hook_if_correct_file( pTHX_ OP *op, void* user_data ) { |
| 144 |
|
| SV* eval_is_try; |
| 145 |
|
| |
| 146 |
38615 |
| const char* wanted_file = SvPV_nolen( (SV*)user_data ); |
| 147 |
38615 |
▶ | const char* cur_file = CopFILE( &PL_compiling ); |
Branch: 1 branch
| Branch | True | False | Total |
| const char* cur_file = CopFILE( &PL_compiling ); |
38615 |
0 |
38615 |
|
| 148 |
38615 |
▶ | if ( strcmp(wanted_file, cur_file) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( strcmp(wanted_file, cur_file) ) { |
38094 |
521 |
38615 |
|
| 149 |
38094 |
▶ | if ( trycatch_debug & 4 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( trycatch_debug & 4 ) |
0 |
38094 |
38094 |
|
| 150 |
0 |
| Perl_warn( aTHX_ "Not hooking OP %s since its not in '%s'", PL_op_name[op->op_type], wanted_file ); |
| 151 |
38094 |
| return op; |
| 152 |
|
| } |
| 153 |
521 |
▶ | if (trycatch_debug & 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (trycatch_debug & 4) { |
0 |
521 |
521 |
|
| 154 |
0 |
| Perl_warn(aTHX_ "hooking OP %s", PL_op_name[op->op_type]); |
| 155 |
|
| } |
| 156 |
|
| |
| 157 |
521 |
| switch (op->op_type) { |
| 158 |
3 |
| case OP_WANTARRAY: |
| 159 |
3 |
| hook_op_ppaddr(op, try_wantarray, NULL); |
| 160 |
3 |
| break; |
| 161 |
|
| |
| 162 |
146 |
| case OP_RETURN: |
| 163 |
146 |
| hook_op_ppaddr(op, try_return, NULL); |
| 164 |
146 |
| break; |
| 165 |
|
| |
| 166 |
|
| #if (PERL_BCDVERSION < 0x5011000) |
| 167 |
|
| case OP_ENTEREVAL: |
| 168 |
|
| /* Do nothing if its still an entereval */ |
| 169 |
|
| break; |
| 170 |
|
| #endif |
| 171 |
|
| |
| 172 |
186 |
| case OP_LEAVETRY: |
| 173 |
|
| /* eval {} starts off as an OP_ENTEREVAL, and then the PL_check[OP_ENTEREVAL] |
| 174 |
|
| returns a newly created ENTERTRY (and LEAVETRY) ops without calling the |
| 175 |
|
| PL_check for these new ops into OP_ENTERTRY. How ever versions prior to perl |
| 176 |
|
| 5.10.1 didn't call the PL_check for these new ops */ |
| 177 |
186 |
| hook_if_correct_file( aTHX_ ((LISTOP*)op)->op_first, user_data ); |
| 178 |
186 |
| break; |
| 179 |
|
| |
| 180 |
186 |
| case OP_ENTERTRY: |
| 181 |
186 |
| eval_is_try = get_sv("TryCatch::NEXT_EVAL_IS_TRY", 0); |
| 182 |
186 |
▶ | if ( eval_is_try && SvOK( eval_is_try ) && SvTRUE( eval_is_try ) ) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ( eval_is_try && SvOK( eval_is_try ) && SvTRUE( eval_is_try ) ) { |
186 |
0 |
186 |
| if ( eval_is_try && SvOK( eval_is_try ) && SvTRUE( eval_is_try ) ) { |
186 |
0 |
186 |
| if ( eval_is_try && SvOK( eval_is_try ) && SvTRUE( eval_is_try ) ) { |
36 |
150 |
186 |
|
| 183 |
|
| /* We've hooked a try block, so reset the flag */ |
| 184 |
36 |
| SvIV_set( eval_is_try, 0 ); |
| 185 |
36 |
| hook_op_ppaddr_around( op, NULL, try_after_entertry, NULL ); |
| 186 |
|
| } |
| 187 |
186 |
| break; |
| 188 |
|
| |
| 189 |
0 |
| default: |
| 190 |
0 |
| fprintf(stderr, "Try Catch Internal Error: Unknown op %d: %s\n", op->op_type, PL_op_name[op->op_type]); |
| 191 |
0 |
| abort(); |
| 192 |
|
| } |
| 193 |
521 |
| return op; |
| 194 |
|
| } |
| 195 |
|
| |
| 196 |
|
| |
| 197 |
|
| /* Hook all the *_check functions we need. Return an arrayref of: |
| 198 |
|
| * |
| 199 |
|
| * [ current_file_name, op_id, hook_id, op_id, hook_id, ... ] |
| 200 |
|
| */ |
| 201 |
|
| SV* |
| 202 |
71 |
| xs_install_op_checks() { |
| 203 |
71 |
| SV *sv_curfile = newSV( 0 ); |
| 204 |
71 |
| AV* av = newAV(); |
| 205 |
|
| |
| 206 |
|
| /* Get the filename we install check op hooks into. Need this so that we |
| 207 |
|
| don't hook ops if a require Other::Module happens in a try block. */ |
| 208 |
71 |
▶ | char* file = CopFILE(&PL_compiling); |
Branch: 1 branch
| Branch | True | False | Total |
| char* file = CopFILE(&PL_compiling); |
71 |
0 |
71 |
|
| 209 |
71 |
| STRLEN len = strlen(file); |
| 210 |
|
| |
| 211 |
71 |
▶ | (void)SvUPGRADE(sv_curfile,SVt_PVNV); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)SvUPGRADE(sv_curfile,SVt_PVNV); |
71 |
0 |
71 |
|
| 212 |
|
| |
| 213 |
71 |
| sv_setpvn(sv_curfile,file,len); |
| 214 |
71 |
| av_push(av, sv_curfile); |
| 215 |
|
| |
| 216 |
|
| #define do_hook(op) \ |
| 217 |
|
| av_push(av, newSVuv( (op) ) ); \ |
| 218 |
|
| av_push(av, newSVuv( hook_op_check( op, hook_if_correct_file, sv_curfile ) ) ); \ |
| 219 |
|
| |
| 220 |
|
| /* This replace return with an unwird */ |
| 221 |
71 |
| do_hook( OP_RETURN ); |
| 222 |
|
| /* This fixes 'wantarray' keyword */ |
| 223 |
71 |
| do_hook( OP_WANTARRAY ); |
| 224 |
|
| /* And this gives the right context to C<return foo()> in a try block */ |
| 225 |
71 |
| do_hook( OP_ENTERTRY ); |
| 226 |
|
| |
| 227 |
|
| #if (PERL_BCDVERSION < 0x5011000) |
| 228 |
|
| /* Prior to 5.10.1(?) the ENTERTRY starts out as an ENTEREVAL and doesn't get |
| 229 |
|
| * PL_checked, so we need to hook ENTEREVAL (string eval) too and see if the |
| 230 |
|
| * type got changed. */ |
| 231 |
|
| do_hook( OP_ENTEREVAL ); |
| 232 |
|
| #endif |
| 233 |
|
| |
| 234 |
|
| #undef do_hook |
| 235 |
|
| |
| 236 |
|
| /* Get an array ref form the array, return that. This keeps the sv_curfile alive */ |
| 237 |
71 |
| return newRV_noinc( (SV*) av ); |
| 238 |
|
| } |
| 239 |
|
| |
| 240 |
|
| |
| 241 |
|
| MODULE = TryCatch PACKAGE = TryCatch::XS |
| 242 |
|
| |
| 243 |
|
| PROTOTYPES: DISABLE |
| 244 |
|
| |
| 245 |
|
| void |
| 246 |
|
| install_op_checks() |
| 247 |
|
| CODE: |
| 248 |
71 |
| ST(0) = xs_install_op_checks(); |
| 249 |
71 |
| XSRETURN(1); |
| 250 |
|
| |
| 251 |
|
| void |
| 252 |
|
| uninstall_op_checks( aref ) |
| 253 |
|
| SV* aref; |
| 254 |
|
| PREINIT: |
| 255 |
|
| AV* av; |
| 256 |
|
| SV *op, *id; |
| 257 |
|
| CODE: |
| 258 |
0 |
▶ | if ( !SvROK(aref) && SvTYPE(SvRV(aref)) != SVt_PVAV ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ( !SvROK(aref) && SvTYPE(SvRV(aref)) != SVt_PVAV ) { |
0 |
0 |
0 |
| if ( !SvROK(aref) && SvTYPE(SvRV(aref)) != SVt_PVAV ) { |
0 |
0 |
0 |
|
| 259 |
0 |
| Perl_croak(aTHX_ "ArrayRef expected"); |
| 260 |
|
| } |
| 261 |
0 |
| av = (AV*)(SvRV(aref)); |
| 262 |
|
| /* throw away cur_file */ |
| 263 |
0 |
| av_shift(av); |
| 264 |
0 |
▶ | while (av_len(av) != -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (av_len(av) != -1) { |
0 |
0 |
0 |
|
| 265 |
0 |
| op = av_shift(av); |
| 266 |
0 |
| id = av_shift(av); |
| 267 |
0 |
| hook_op_check_remove( SvUV(op), SvUV(id) ); |
| 268 |
|
| } |
| 269 |
|
| OUTPUT: |
| 270 |
|
| |
| 271 |
|
| void dump_stack() |
| 272 |
|
| CODE: |
| 273 |
0 |
| dump_cxstack(); |
| 274 |
|
| OUTPUT: |
| 275 |
|
| |
| 276 |
|
| void set_linestr_offset(int offset) |
| 277 |
|
| CODE: |
| 278 |
0 |
▶ | char* linestr = SvPVX(PL_linestr); |
Branch: 1 branch
| Branch | True | False | Total |
| char* linestr = SvPVX(PL_linestr); |
0 |
0 |
0 |
|
| 279 |
0 |
▶ | PL_bufptr = linestr + offset; |
Branch: 1 branch
| Branch | True | False | Total |
| PL_bufptr = linestr + offset; |
0 |
0 |
0 |
|
| 280 |
|
| |
| 281 |
|
| BOOT: |
| 282 |
|
| { |
| 283 |
10 |
| char *debug = getenv ("TRYCATCH_DEBUG"); |
| 284 |
|
| /* Debug meanings: |
| 285 |
|
| 1 - line string changes (from the .pm) |
| 286 |
|
| 2 - Debug unwid contexts |
| 287 |
|
| 4 - debug op hooking |
| 288 |
|
| */ |
| 289 |
10 |
▶ | if (debug && (trycatch_debug = atoi(debug)) ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (debug && (trycatch_debug = atoi(debug)) ) { |
0 |
10 |
10 |
| if (debug && (trycatch_debug = atoi(debug)) ) { |
0 |
0 |
0 |
|
| 290 |
0 |
| fprintf(stderr, "TryCatch XS debug enabled: %d\n", trycatch_debug); |
| 291 |
|
| } |
| 292 |
|
| } |