| 1 |
|
| #define PERL_NO_GET_CONTEXT /* we want efficiency */ |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| #include "ppport.h" |
| 6 |
|
| |
| 7 |
|
| #define PROBE_TYPE_NONE 0 |
| 8 |
|
| #define PROBE_TYPE_ONCE 1 |
| 9 |
|
| #define PROBE_TYPE_PERMANENT 2 |
| 10 |
|
| |
| 11 |
|
| #define PROBE_ACTION_LOOKUP 0 |
| 12 |
|
| #define PROBE_ACTION_REMOVE 1 |
| 13 |
|
| |
| 14 |
|
| #define PROBE_SETTINGS_TYPE_INDEX 0 |
| 15 |
|
| #define PROBE_SETTINGS_ARGS_INDEX 1 |
| 16 |
|
| |
| 17 |
|
| /* |
| 18 |
|
| * Use preprocessor macros for time-sensitive operations. |
| 19 |
|
| */ |
| 20 |
|
| #define probe_is_enabled() !!probe_enabled |
| 21 |
|
| |
| 22 |
|
| static Perl_ppaddr_t probe_nextstate_orig = 0; |
| 23 |
|
| static int probe_installed = 0; |
| 24 |
|
| static int probe_enabled = 0; |
| 25 |
|
| static HV* probe_hash = 0; |
| 26 |
|
| static SV* probe_trigger_cb = 0; |
| 27 |
|
| |
| 28 |
|
| static void probe_enable(void); |
| 29 |
|
| static void probe_disable(void); |
| 30 |
|
| static int probe_is_installed(void); |
| 31 |
|
| static void probe_install(pTHX); |
| 32 |
|
| static void probe_remove(pTHX); |
| 33 |
|
| |
| 34 |
|
| #define DEBUG 0 |
| 35 |
|
| |
| 36 |
|
| #define INFO(x) do { if (DEBUG > 0) dbg_printf x; } while (0) |
| 37 |
|
| #define TRACE(x) do { if (DEBUG > 1) dbg_printf x; } while (0) |
| 38 |
|
| |
| 39 |
0 |
| void dbg_printf(const char *fmt, ...) |
| 40 |
|
| { |
| 41 |
|
| va_list args; |
| 42 |
0 |
| va_start(args, fmt); |
| 43 |
0 |
| vfprintf(stderr, fmt, args); |
| 44 |
0 |
| va_end(args); |
| 45 |
0 |
| } |
| 46 |
|
| |
| 47 |
1000012 |
| static void probe_invoke_callback(pTHX_ const char* file, int line, SV* user_arg, SV* callback) |
| 48 |
|
| { |
| 49 |
|
| int count; |
| 50 |
|
| |
| 51 |
1000012 |
| dSP; |
| 52 |
|
| |
| 53 |
1000012 |
| ENTER; |
| 54 |
1000012 |
| SAVETMPS; |
| 55 |
|
| |
| 56 |
1000012 |
▶ | PUSHMARK (SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK (SP); |
0 |
1000012 |
1000012 |
|
| 57 |
1000012 |
▶ | EXTEND(SP, user_arg ? 3 : 2); |
Branch: 3 branches
| Branch | True | False | Total |
| EXTEND(SP, user_arg ? 3 : 2); |
1000003 |
9 |
1000012 |
| EXTEND(SP, user_arg ? 3 : 2); |
0 |
1000012 |
1000012 |
| EXTEND(SP, user_arg ? 3 : 2); |
0 |
0 |
0 |
|
| 58 |
1000012 |
▶ | XPUSHs(sv_2mortal(newSVpv(file, 0))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpv(file, 0))); |
0 |
1000012 |
1000012 |
|
| 59 |
1000012 |
▶ | XPUSHs(sv_2mortal(newSViv(line))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(line))); |
0 |
1000012 |
1000012 |
|
| 60 |
1000012 |
▶ | if (user_arg) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (user_arg) { |
1000003 |
9 |
1000012 |
|
| 61 |
1000003 |
▶ | XPUSHs(user_arg); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(user_arg); |
0 |
1000003 |
1000003 |
|
| 62 |
|
| } |
| 63 |
1000012 |
| PUTBACK; |
| 64 |
|
| |
| 65 |
1000012 |
| count = call_sv(callback, G_VOID|G_DISCARD); |
| 66 |
1000012 |
▶ | if (count != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count != 0) { |
0 |
1000012 |
1000012 |
|
| 67 |
0 |
| croak("probe trigger should have zero return values"); |
| 68 |
|
| } |
| 69 |
|
| |
| 70 |
1000012 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
1000012 |
0 |
1000012 |
|
| 71 |
1000012 |
| LEAVE; |
| 72 |
1000012 |
| } |
| 73 |
|
| |
| 74 |
4019589 |
| static bool probe_lookup(pTHX_ const char* file, int line, int action) |
| 75 |
|
| { |
| 76 |
4019589 |
| U32 klen = strlen(file); |
| 77 |
|
| char kstr[20]; |
| 78 |
4019589 |
| SV** rlines = 0; |
| 79 |
4019589 |
| HV* lines = 0; |
| 80 |
|
| |
| 81 |
4019589 |
| rlines = hv_fetch(probe_hash, file, klen, 0); |
| 82 |
4019589 |
▶ | if (rlines) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rlines) { |
4007120 |
12469 |
4019589 |
|
| 83 |
4007120 |
| lines = (HV*) SvRV(*rlines); |
| 84 |
|
| TRACE(("PROBE found entry for file [%s]: %p\n", file, lines)); |
| 85 |
|
| } else { |
| 86 |
12469 |
| return false; |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
4007120 |
| klen = sprintf(kstr, "%d", line); |
| 90 |
4007120 |
▶ | if (hv_exists(lines, kstr, klen)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists(lines, kstr, klen)) { |
1000021 |
3007099 |
4007120 |
|
| 91 |
1000021 |
▶ | if (action == PROBE_ACTION_REMOVE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (action == PROBE_ACTION_REMOVE) { |
9 |
1000012 |
1000021 |
|
| 92 |
|
| /* TODO: remove file name when last line for file was removed? */ |
| 93 |
9 |
| hv_delete(lines, kstr, klen, G_DISCARD); |
| 94 |
|
| INFO(("PROBE removed entry for line [%s]\n", kstr)); |
| 95 |
|
| } |
| 96 |
1000021 |
| return true; |
| 97 |
|
| } else { |
| 98 |
3007099 |
| return false; |
| 99 |
|
| } |
| 100 |
|
| |
| 101 |
|
| /* catch any mistakes */ |
| 102 |
|
| return false; |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
1000012 |
| static AV* probe_settings(pTHX_ const char* file, int line) |
| 106 |
|
| { |
| 107 |
1000012 |
| U32 klen = strlen(file); |
| 108 |
|
| char kstr[20]; |
| 109 |
1000012 |
| SV** rlines = 0; |
| 110 |
1000012 |
| HV* lines = 0; |
| 111 |
1000012 |
| SV** rsettings = 0; |
| 112 |
1000012 |
| AV* settings = 0; |
| 113 |
|
| |
| 114 |
1000012 |
| rlines = hv_fetch(probe_hash, file, klen, 0); |
| 115 |
1000012 |
▶ | if (!rlines) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rlines) { |
0 |
1000012 |
1000012 |
|
| 116 |
0 |
| return 0; |
| 117 |
|
| } |
| 118 |
1000012 |
| lines = (HV*) SvRV(*rlines); |
| 119 |
|
| |
| 120 |
1000012 |
| klen = sprintf(kstr, "%d", line); |
| 121 |
1000012 |
| rsettings = hv_fetch(lines, kstr, klen, 0); |
| 122 |
1000012 |
▶ | if (!rsettings) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rsettings) { |
0 |
1000012 |
1000012 |
|
| 123 |
0 |
| return 0; |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
1000012 |
▶ | if (!SvROK(*rsettings) || SvTYPE(SvRV(*rsettings)) != SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(*rsettings) || SvTYPE(SvRV(*rsettings)) != SVt_PVAV) { |
1000012 |
0 |
1000012 |
| if (!SvROK(*rsettings) || SvTYPE(SvRV(*rsettings)) != SVt_PVAV) { |
0 |
1000012 |
1000012 |
|
| 127 |
0 |
| croak("Devel::Probe settings must be an ARRAY ref"); |
| 128 |
|
| } |
| 129 |
|
| |
| 130 |
1000012 |
| settings = (AV*) SvRV(*rsettings); |
| 131 |
1000012 |
| return settings; |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
|
| /* |
| 135 |
|
| * This function will run for every single line in your Perl code. |
| 136 |
|
| * You would do well to make it as cheap as possible. |
| 137 |
|
| */ |
| 138 |
4372435 |
| static OP* probe_nextstate(pTHX) |
| 139 |
|
| { |
| 140 |
4372435 |
| OP* ret = probe_nextstate_orig(aTHX); |
| 141 |
|
| |
| 142 |
|
| do { |
| 143 |
4372435 |
| const char* file = 0; |
| 144 |
4372435 |
| int line = 0; |
| 145 |
4372435 |
| AV* settings = 0; |
| 146 |
4372435 |
| SV* user_callback_arg = 0; |
| 147 |
4372435 |
| int type = PROBE_TYPE_NONE; |
| 148 |
|
| |
| 149 |
4372435 |
▶ | if (!probe_is_enabled()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!probe_is_enabled()) { |
352855 |
4019580 |
4372435 |
|
| 150 |
352855 |
| break; |
| 151 |
|
| } |
| 152 |
|
| |
| 153 |
4019580 |
▶ | file = CopFILE(PL_curcop); |
Branch: 1 branch
| Branch | True | False | Total |
| file = CopFILE(PL_curcop); |
4019580 |
0 |
4019580 |
|
| 154 |
4019580 |
| line = CopLINE(PL_curcop); |
| 155 |
|
| TRACE(("PROBE check [%s] [%d]\n", file, line)); |
| 156 |
4019580 |
▶ | if (!probe_lookup(aTHX_ file, line, PROBE_ACTION_LOOKUP)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!probe_lookup(aTHX_ file, line, PROBE_ACTION_LOOKUP)) { |
3019568 |
1000012 |
4019580 |
|
| 157 |
3019568 |
| break; |
| 158 |
|
| } |
| 159 |
|
| |
| 160 |
1000012 |
| settings = probe_settings(aTHX_ file, line); |
| 161 |
1000012 |
▶ | if (!settings) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!settings) { |
0 |
1000012 |
1000012 |
|
| 162 |
0 |
| break; |
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
1000012 |
| type = SvIV(*(av_fetch(settings, PROBE_SETTINGS_TYPE_INDEX, 0))); |
| 166 |
1000012 |
▶ | if (av_top_index(settings) == PROBE_SETTINGS_ARGS_INDEX) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_top_index(settings) == PROBE_SETTINGS_ARGS_INDEX) { |
1000003 |
9 |
1000012 |
|
| 167 |
1000003 |
| user_callback_arg = *(av_fetch(settings, PROBE_SETTINGS_ARGS_INDEX, 0)); |
| 168 |
|
| } |
| 169 |
|
| |
| 170 |
|
| INFO(("PROBE triggered [%s] [%d] [%d]\n", file, line, type)); |
| 171 |
1000012 |
▶ | if (probe_trigger_cb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (probe_trigger_cb) { |
1000012 |
0 |
1000012 |
|
| 172 |
1000012 |
| probe_invoke_callback(aTHX_ file, line, user_callback_arg, probe_trigger_cb); |
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
1000012 |
▶ | if (type == PROBE_TYPE_ONCE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (type == PROBE_TYPE_ONCE) { |
9 |
1000003 |
1000012 |
|
| 176 |
9 |
| probe_lookup(aTHX_ file, line, PROBE_ACTION_REMOVE); |
| 177 |
|
| } |
| 178 |
|
| } while (0); |
| 179 |
|
| |
| 180 |
4372435 |
| return ret; |
| 181 |
|
| } |
| 182 |
|
| |
| 183 |
11006 |
| static void probe_enable(void) |
| 184 |
|
| { |
| 185 |
11006 |
▶ | if (probe_is_enabled()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (probe_is_enabled()) { |
1 |
11005 |
11006 |
|
| 186 |
1 |
| return; |
| 187 |
|
| } |
| 188 |
|
| INFO(("PROBE enabling\n")); |
| 189 |
11005 |
| probe_enabled = 1; |
| 190 |
|
| } |
| 191 |
|
| |
| 192 |
153022 |
| static void probe_clear(pTHX) |
| 193 |
|
| { |
| 194 |
153022 |
▶ | if (probe_hash) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (probe_hash) { |
153014 |
8 |
153022 |
|
| 195 |
153014 |
| hv_clear(probe_hash); |
| 196 |
|
| } else { |
| 197 |
8 |
| probe_hash = newHV(); |
| 198 |
|
| } |
| 199 |
|
| INFO(("PROBE cleared\n")); |
| 200 |
153022 |
| } |
| 201 |
|
| |
| 202 |
102011 |
| static void probe_reset(pTHX_ int installed) |
| 203 |
|
| { |
| 204 |
102011 |
| probe_installed = installed; |
| 205 |
102011 |
| probe_enabled = 0; |
| 206 |
102011 |
| probe_clear(aTHX); |
| 207 |
102011 |
▶ | if (probe_trigger_cb) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (probe_trigger_cb) { |
11001 |
91010 |
102011 |
|
| 208 |
11001 |
| SvREFCNT_dec(probe_trigger_cb); |
| 209 |
|
| } |
| 210 |
102011 |
| probe_trigger_cb = 0; |
| 211 |
102011 |
| } |
| 212 |
|
| |
| 213 |
10010 |
| static void probe_disable(void) |
| 214 |
|
| { |
| 215 |
10010 |
▶ | if (!probe_is_enabled()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!probe_is_enabled()) { |
9 |
10001 |
10010 |
|
| 216 |
9 |
| return; |
| 217 |
|
| } |
| 218 |
10001 |
| probe_enabled = 0; |
| 219 |
|
| INFO(("PROBE disabled\n")); |
| 220 |
|
| } |
| 221 |
|
| |
| 222 |
102021 |
| static int probe_is_installed(void) |
| 223 |
|
| { |
| 224 |
102021 |
| return probe_installed; |
| 225 |
|
| } |
| 226 |
|
| |
| 227 |
51012 |
| static void probe_install(pTHX) |
| 228 |
|
| { |
| 229 |
51012 |
▶ | if (probe_is_installed()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (probe_is_installed()) { |
3 |
51009 |
51012 |
|
| 230 |
3 |
| return; |
| 231 |
|
| } |
| 232 |
|
| |
| 233 |
|
| INFO(("PROBE installed, [%p] => [%p]\n", PL_ppaddr[OP_NEXTSTATE], probe_nextstate)); |
| 234 |
|
| |
| 235 |
51009 |
▶ | if (!probe_nextstate_orig) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!probe_nextstate_orig) { |
8 |
51001 |
51009 |
|
| 236 |
8 |
| probe_nextstate_orig = PL_ppaddr[OP_NEXTSTATE]; |
| 237 |
|
| } |
| 238 |
51009 |
| PL_ppaddr[OP_NEXTSTATE] = probe_nextstate; |
| 239 |
51009 |
| probe_reset(aTHX_ 1); |
| 240 |
51009 |
| probe_clear(aTHX); |
| 241 |
|
| } |
| 242 |
|
| |
| 243 |
51003 |
| static void probe_remove(pTHX) |
| 244 |
|
| { |
| 245 |
51003 |
▶ | if (!probe_is_installed()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!probe_is_installed()) { |
1 |
51002 |
51003 |
|
| 246 |
1 |
| return; |
| 247 |
|
| } |
| 248 |
|
| INFO(("PROBE removed, [%p] => [%p]\n", PL_ppaddr[OP_NEXTSTATE], probe_nextstate_orig)); |
| 249 |
51002 |
▶ | if (probe_nextstate_orig) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (probe_nextstate_orig) { |
51002 |
0 |
51002 |
|
| 250 |
51002 |
| PL_ppaddr[OP_NEXTSTATE] = probe_nextstate_orig; |
| 251 |
|
| } |
| 252 |
51002 |
| probe_reset(aTHX_ 0); |
| 253 |
|
| } |
| 254 |
|
| |
| 255 |
|
| MODULE = Devel::Probe PACKAGE = Devel::Probe |
| 256 |
|
| PROTOTYPES: DISABLE |
| 257 |
|
| |
| 258 |
|
| ################################################################# |
| 259 |
|
| |
| 260 |
|
| void |
| 261 |
|
| install() |
| 262 |
|
| CODE: |
| 263 |
51012 |
| probe_install(aTHX); |
| 264 |
|
| |
| 265 |
|
| void |
| 266 |
|
| remove() |
| 267 |
|
| CODE: |
| 268 |
51003 |
| probe_remove(aTHX); |
| 269 |
|
| |
| 270 |
|
| int |
| 271 |
|
| is_installed() |
| 272 |
|
| CODE: |
| 273 |
6 |
| RETVAL = probe_is_installed(); |
| 274 |
|
| OUTPUT: RETVAL |
| 275 |
|
| |
| 276 |
|
| void |
| 277 |
|
| enable() |
| 278 |
|
| CODE: |
| 279 |
11006 |
| probe_enable(); |
| 280 |
|
| |
| 281 |
|
| void |
| 282 |
|
| disable() |
| 283 |
|
| CODE: |
| 284 |
10008 |
| probe_disable(); |
| 285 |
|
| |
| 286 |
|
| int |
| 287 |
|
| is_enabled() |
| 288 |
|
| CODE: |
| 289 |
5 |
▶ | RETVAL = probe_is_enabled(); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = probe_is_enabled(); |
0 |
5 |
5 |
|
| 290 |
|
| OUTPUT: RETVAL |
| 291 |
|
| |
| 292 |
|
| void |
| 293 |
|
| clear() |
| 294 |
|
| CODE: |
| 295 |
2 |
| probe_disable(); |
| 296 |
2 |
| probe_clear(aTHX); |
| 297 |
|
| |
| 298 |
|
| HV * |
| 299 |
|
| _internal_probe_state() |
| 300 |
|
| CODE: |
| 301 |
21022 |
| RETVAL = probe_hash; |
| 302 |
|
| OUTPUT: RETVAL |
| 303 |
|
| |
| 304 |
|
| void |
| 305 |
|
| trigger(SV* callback) |
| 306 |
|
| CODE: |
| 307 |
11004 |
▶ | if (probe_trigger_cb == (SV*)NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (probe_trigger_cb == (SV*)NULL) { |
11004 |
0 |
11004 |
|
| 308 |
11004 |
| probe_trigger_cb = newSVsv(callback); |
| 309 |
|
| } else { |
| 310 |
0 |
▶ | SvSetSV(probe_trigger_cb, callback); |
Branch: 1 branch
| Branch | True | False | Total |
| SvSetSV(probe_trigger_cb, callback); |
0 |
0 |
0 |
|
| 311 |
|
| } |