| 1 | #if 0 |
|---|
| 2 | #define XCACHE_DEBUG |
|---|
| 3 | #endif |
|---|
| 4 | |
|---|
| 5 | #include <stdio.h> |
|---|
| 6 | #include "xcache.h" |
|---|
| 7 | #include "ext/standard/flock_compat.h" |
|---|
| 8 | #ifdef HAVE_SYS_FILE_H |
|---|
| 9 | # include <sys/file.h> |
|---|
| 10 | #endif |
|---|
| 11 | #include <sys/types.h> |
|---|
| 12 | #include <sys/stat.h> |
|---|
| 13 | #include <fcntl.h> |
|---|
| 14 | |
|---|
| 15 | #include "stack.h" |
|---|
| 16 | #include "xcache_globals.h" |
|---|
| 17 | #include "coverager.h" |
|---|
| 18 | #include "utils.h" |
|---|
| 19 | typedef HashTable *coverager_t; |
|---|
| 20 | #define PCOV_HEADER_MAGIC 0x564f4350 |
|---|
| 21 | |
|---|
| 22 | static char *xc_coveragedump_dir = NULL; |
|---|
| 23 | static zend_compile_file_t *old_compile_file = NULL; |
|---|
| 24 | |
|---|
| 25 | /* dumper */ |
|---|
| 26 | static void xc_destroy_coverage(void *pDest) /* {{{ */ |
|---|
| 27 | { |
|---|
| 28 | coverager_t cov = *(coverager_t*) pDest; |
|---|
| 29 | TRACE("destroy %p", cov); |
|---|
| 30 | zend_hash_destroy(cov); |
|---|
| 31 | efree(cov); |
|---|
| 32 | } |
|---|
| 33 | /* }}} */ |
|---|
| 34 | void xcache_mkdirs_ex(char *root, int rootlen, char *path, int pathlen TSRMLS_DC) /* {{{ */ |
|---|
| 35 | { |
|---|
| 36 | char *fullpath; |
|---|
| 37 | struct stat st; |
|---|
| 38 | ALLOCA_FLAG(use_heap) |
|---|
| 39 | |
|---|
| 40 | TRACE("mkdirs %s %d %s %d", root, rootlen, path, pathlen); |
|---|
| 41 | fullpath = my_do_alloca(rootlen + pathlen + 1, use_heap); |
|---|
| 42 | memcpy(fullpath, root, rootlen); |
|---|
| 43 | memcpy(fullpath + rootlen, path, pathlen); |
|---|
| 44 | fullpath[rootlen + pathlen] = '\0'; |
|---|
| 45 | |
|---|
| 46 | if (stat(fullpath, &st) != 0) { |
|---|
| 47 | char *chr; |
|---|
| 48 | |
|---|
| 49 | chr = strrchr(path, PHP_DIR_SEPARATOR); |
|---|
| 50 | if (chr && chr != path) { |
|---|
| 51 | *chr = '\0'; |
|---|
| 52 | xcache_mkdirs_ex(root, rootlen, path, chr - path TSRMLS_CC); |
|---|
| 53 | *chr = PHP_DIR_SEPARATOR; |
|---|
| 54 | } |
|---|
| 55 | TRACE("mkdir %s", fullpath); |
|---|
| 56 | #if PHP_MAJOR_VERSION > 5 |
|---|
| 57 | php_stream_mkdir(fullpath, 0700, REPORT_ERRORS, NULL); |
|---|
| 58 | #else |
|---|
| 59 | mkdir(fullpath, 0700); |
|---|
| 60 | #endif |
|---|
| 61 | } |
|---|
| 62 | my_free_alloca(fullpath, use_heap); |
|---|
| 63 | } |
|---|
| 64 | /* }}} */ |
|---|
| 65 | static void xc_coverager_save_cov(char *srcfile, char *outfilename, coverager_t cov TSRMLS_DC) /* {{{ */ |
|---|
| 66 | { |
|---|
| 67 | long *buf = NULL, *p; |
|---|
| 68 | long covlines, *phits; |
|---|
| 69 | int fd = -1; |
|---|
| 70 | int size; |
|---|
| 71 | int newfile; |
|---|
| 72 | struct stat srcstat, outstat; |
|---|
| 73 | HashPosition pos; |
|---|
| 74 | char *contents = NULL; |
|---|
| 75 | long len; |
|---|
| 76 | |
|---|
| 77 | if (stat(srcfile, &srcstat) != 0) { |
|---|
| 78 | return; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | newfile = 0; |
|---|
| 82 | if (stat(outfilename, &outstat) != 0) { |
|---|
| 83 | newfile = 1; |
|---|
| 84 | } |
|---|
| 85 | else { |
|---|
| 86 | if (srcstat.st_mtime > outstat.st_mtime) { |
|---|
| 87 | newfile = 1; |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | fd = open(outfilename, O_RDWR | O_CREAT, 0600); |
|---|
| 92 | if (fd < 0) { |
|---|
| 93 | char *chr; |
|---|
| 94 | chr = strrchr(srcfile, PHP_DIR_SEPARATOR); |
|---|
| 95 | if (chr) { |
|---|
| 96 | *chr = '\0'; |
|---|
| 97 | xcache_mkdirs_ex(xc_coveragedump_dir, strlen(xc_coveragedump_dir), srcfile, chr - srcfile TSRMLS_CC); |
|---|
| 98 | *chr = PHP_DIR_SEPARATOR; |
|---|
| 99 | } |
|---|
| 100 | fd = open(outfilename, O_RDWR | O_CREAT, 0600); |
|---|
| 101 | if (fd < 0) { |
|---|
| 102 | goto bailout; |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | if (flock(fd, LOCK_EX) != SUCCESS) { |
|---|
| 106 | goto bailout; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | if (newfile) { |
|---|
| 110 | TRACE("%s", "new file"); |
|---|
| 111 | } |
|---|
| 112 | else if (outstat.st_size) { |
|---|
| 113 | len = outstat.st_size; |
|---|
| 114 | contents = emalloc(len); |
|---|
| 115 | if (read(fd, (void *) contents, len) != len) { |
|---|
| 116 | goto bailout; |
|---|
| 117 | } |
|---|
| 118 | TRACE("oldsize %d", (int) len); |
|---|
| 119 | do { |
|---|
| 120 | p = (long *) contents; |
|---|
| 121 | len -= sizeof(long); |
|---|
| 122 | if (len < 0) { |
|---|
| 123 | break; |
|---|
| 124 | } |
|---|
| 125 | if (*p++ != PCOV_HEADER_MAGIC) { |
|---|
| 126 | TRACE("wrong magic in file %s", outfilename); |
|---|
| 127 | break; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | p += 2; /* skip covliens */ |
|---|
| 131 | len -= sizeof(long) * 2; |
|---|
| 132 | if (len < 0) { |
|---|
| 133 | break; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | for (; len >= sizeof(long) * 2; len -= sizeof(long) * 2, p += 2) { |
|---|
| 137 | if (zend_hash_index_find(cov, p[0], (void**)&phits) == SUCCESS) { |
|---|
| 138 | if (p[1] == -1) { |
|---|
| 139 | /* OPTIMIZE: already marked */ |
|---|
| 140 | continue; |
|---|
| 141 | } |
|---|
| 142 | if (*phits != -1) { |
|---|
| 143 | p[1] += *phits; |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | zend_hash_index_update(cov, p[0], &p[1], sizeof(p[1]), NULL); |
|---|
| 147 | } |
|---|
| 148 | } while (0); |
|---|
| 149 | efree(contents); |
|---|
| 150 | contents = NULL; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | /* serialize */ |
|---|
| 155 | size = (zend_hash_num_elements(cov) + 1) * sizeof(long) * 2 + sizeof(long); |
|---|
| 156 | p = buf = emalloc(size); |
|---|
| 157 | *p++ = PCOV_HEADER_MAGIC; |
|---|
| 158 | p += 2; /* for covlines */ |
|---|
| 159 | covlines = 0; |
|---|
| 160 | |
|---|
| 161 | zend_hash_internal_pointer_reset_ex(cov, &pos); |
|---|
| 162 | while (zend_hash_get_current_data_ex(cov, (void**)&phits, &pos) == SUCCESS) { |
|---|
| 163 | *p++ = pos->h; |
|---|
| 164 | *p++ = *phits; |
|---|
| 165 | if (*phits > 0) { |
|---|
| 166 | covlines ++; |
|---|
| 167 | } |
|---|
| 168 | zend_hash_move_forward_ex(cov, &pos); |
|---|
| 169 | } |
|---|
| 170 | p = buf + 1; |
|---|
| 171 | p[0] = 0; |
|---|
| 172 | p[1] = covlines; |
|---|
| 173 | |
|---|
| 174 | if (ftruncate(fd, 0) != 0) { |
|---|
| 175 | goto bailout; |
|---|
| 176 | } |
|---|
| 177 | lseek(fd, 0, SEEK_SET); |
|---|
| 178 | if (write(fd, (char *) buf, size) != size) { |
|---|
| 179 | goto bailout; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | bailout: |
|---|
| 183 | if (contents) efree(contents); |
|---|
| 184 | if (fd >= 0) close(fd); |
|---|
| 185 | if (buf) efree(buf); |
|---|
| 186 | } |
|---|
| 187 | /* }}} */ |
|---|
| 188 | |
|---|
| 189 | static void xc_coverager_initenv(TSRMLS_D) /* {{{ */ |
|---|
| 190 | { |
|---|
| 191 | if (!XG(coverages)) { |
|---|
| 192 | XG(coverages) = emalloc(sizeof(HashTable)); |
|---|
| 193 | zend_hash_init(XG(coverages), 0, NULL, xc_destroy_coverage, 0); |
|---|
| 194 | } |
|---|
| 195 | } |
|---|
| 196 | /* }}} */ |
|---|
| 197 | static void xc_coverager_clean(TSRMLS_D) /* {{{ */ |
|---|
| 198 | { |
|---|
| 199 | if (XG(coverages)) { |
|---|
| 200 | HashPosition pos; |
|---|
| 201 | coverager_t *pcov; |
|---|
| 202 | |
|---|
| 203 | zend_hash_internal_pointer_reset_ex(XG(coverages), &pos); |
|---|
| 204 | while (zend_hash_get_current_data_ex(XG(coverages), (void **) &pcov, &pos) == SUCCESS) { |
|---|
| 205 | long *phits; |
|---|
| 206 | coverager_t cov; |
|---|
| 207 | HashPosition pos2; |
|---|
| 208 | |
|---|
| 209 | cov = *pcov; |
|---|
| 210 | |
|---|
| 211 | zend_hash_internal_pointer_reset_ex(cov, &pos2); |
|---|
| 212 | while (zend_hash_get_current_data_ex(cov, (void**)&phits, &pos2) == SUCCESS) { |
|---|
| 213 | long hits = *phits; |
|---|
| 214 | |
|---|
| 215 | if (hits != -1) { |
|---|
| 216 | hits = -1; |
|---|
| 217 | zend_hash_index_update(cov, pos2->h, &hits, sizeof(hits), NULL); |
|---|
| 218 | } |
|---|
| 219 | zend_hash_move_forward_ex(cov, &pos2); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | zend_hash_move_forward_ex(XG(coverages), &pos); |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | /* }}} */ |
|---|
| 227 | static void xc_coverager_cleanup(TSRMLS_D) /* {{{ */ |
|---|
| 228 | { |
|---|
| 229 | if (XG(coverages)) { |
|---|
| 230 | zend_hash_destroy(XG(coverages)); |
|---|
| 231 | efree(XG(coverages)); |
|---|
| 232 | XG(coverages) = NULL; |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | /* }}} */ |
|---|
| 236 | |
|---|
| 237 | static void xc_coverager_enable(TSRMLS_D) /* {{{ */ |
|---|
| 238 | { |
|---|
| 239 | XG(coverage_enabled) = 1; |
|---|
| 240 | } |
|---|
| 241 | /* }}} */ |
|---|
| 242 | static void xc_coverager_disable(TSRMLS_D) /* {{{ */ |
|---|
| 243 | { |
|---|
| 244 | XG(coverage_enabled) = 0; |
|---|
| 245 | } |
|---|
| 246 | /* }}} */ |
|---|
| 247 | |
|---|
| 248 | void xc_coverager_request_init(TSRMLS_D) /* {{{ */ |
|---|
| 249 | { |
|---|
| 250 | if (XG(coverager)) { |
|---|
| 251 | xc_coverager_enable(TSRMLS_C); |
|---|
| 252 | #ifdef ZEND_COMPILE_EXTENDED_INFO |
|---|
| 253 | CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO; |
|---|
| 254 | #else |
|---|
| 255 | CG(extended_info) = 1; |
|---|
| 256 | #endif |
|---|
| 257 | } |
|---|
| 258 | else { |
|---|
| 259 | XG(coverage_enabled) = 0; |
|---|
| 260 | } |
|---|
| 261 | } |
|---|
| 262 | /* }}} */ |
|---|
| 263 | static void xc_coverager_autodump(TSRMLS_D) /* {{{ */ |
|---|
| 264 | { |
|---|
| 265 | coverager_t *pcov; |
|---|
| 266 | zstr s; |
|---|
| 267 | char *outfilename; |
|---|
| 268 | int dumpdir_len, outfilelen, alloc_len = 0; |
|---|
| 269 | uint size; |
|---|
| 270 | HashPosition pos; |
|---|
| 271 | |
|---|
| 272 | if (XG(coverages) && xc_coveragedump_dir) { |
|---|
| 273 | dumpdir_len = strlen(xc_coveragedump_dir); |
|---|
| 274 | alloc_len = dumpdir_len + 1 + 128; |
|---|
| 275 | outfilename = emalloc(alloc_len); |
|---|
| 276 | strcpy(outfilename, xc_coveragedump_dir); |
|---|
| 277 | |
|---|
| 278 | zend_hash_internal_pointer_reset_ex(XG(coverages), &pos); |
|---|
| 279 | while (zend_hash_get_current_data_ex(XG(coverages), (void **) &pcov, &pos) == SUCCESS) { |
|---|
| 280 | zend_hash_get_current_key_ex(XG(coverages), &s, &size, NULL, 0, &pos); |
|---|
| 281 | outfilelen = dumpdir_len + size + 5; |
|---|
| 282 | if (alloc_len < outfilelen) { |
|---|
| 283 | alloc_len = outfilelen + 128; |
|---|
| 284 | outfilename = erealloc(outfilename, alloc_len); |
|---|
| 285 | } |
|---|
| 286 | strcpy(outfilename + dumpdir_len, ZSTR_S(s)); |
|---|
| 287 | strcpy(outfilename + dumpdir_len + size - 1, ".pcov"); |
|---|
| 288 | |
|---|
| 289 | TRACE("outfilename %s", outfilename); |
|---|
| 290 | xc_coverager_save_cov(ZSTR_S(s), outfilename, *pcov TSRMLS_CC); |
|---|
| 291 | zend_hash_move_forward_ex(XG(coverages), &pos); |
|---|
| 292 | } |
|---|
| 293 | efree(outfilename); |
|---|
| 294 | } |
|---|
| 295 | } |
|---|
| 296 | /* }}} */ |
|---|
| 297 | static void xc_coverager_dump(zval *return_value TSRMLS_DC) /* {{{ */ |
|---|
| 298 | { |
|---|
| 299 | coverager_t *pcov; |
|---|
| 300 | HashPosition pos; |
|---|
| 301 | |
|---|
| 302 | if (XG(coverages)) { |
|---|
| 303 | array_init(return_value); |
|---|
| 304 | |
|---|
| 305 | zend_hash_internal_pointer_reset_ex(XG(coverages), &pos); |
|---|
| 306 | while (zend_hash_get_current_data_ex(XG(coverages), (void **) &pcov, &pos) == SUCCESS) { |
|---|
| 307 | zval *lines; |
|---|
| 308 | long *phits; |
|---|
| 309 | coverager_t cov; |
|---|
| 310 | HashPosition pos2; |
|---|
| 311 | zstr filename; |
|---|
| 312 | uint size; |
|---|
| 313 | |
|---|
| 314 | cov = *pcov; |
|---|
| 315 | zend_hash_get_current_key_ex(XG(coverages), &filename, &size, NULL, 0, &pos); |
|---|
| 316 | |
|---|
| 317 | MAKE_STD_ZVAL(lines); |
|---|
| 318 | array_init(lines); |
|---|
| 319 | zend_hash_internal_pointer_reset_ex(cov, &pos2); |
|---|
| 320 | while (zend_hash_get_current_data_ex(cov, (void**)&phits, &pos2) == SUCCESS) { |
|---|
| 321 | long hits = *phits; |
|---|
| 322 | add_index_long(lines, pos2->h, hits >= 0 ? hits : 0); |
|---|
| 323 | zend_hash_move_forward_ex(cov, &pos2); |
|---|
| 324 | } |
|---|
| 325 | add_assoc_zval_ex(return_value, ZSTR_S(filename), strlen(ZSTR_S(filename)) + 1, lines); |
|---|
| 326 | |
|---|
| 327 | zend_hash_move_forward_ex(XG(coverages), &pos); |
|---|
| 328 | } |
|---|
| 329 | } |
|---|
| 330 | else { |
|---|
| 331 | RETVAL_NULL(); |
|---|
| 332 | } |
|---|
| 333 | } |
|---|
| 334 | /* }}} */ |
|---|
| 335 | void xc_coverager_request_shutdown(TSRMLS_D) /* {{{ */ |
|---|
| 336 | { |
|---|
| 337 | if (XG(coverager)) { |
|---|
| 338 | xc_coverager_autodump(TSRMLS_C); |
|---|
| 339 | xc_coverager_cleanup(TSRMLS_C); |
|---|
| 340 | } |
|---|
| 341 | } |
|---|
| 342 | /* }}} */ |
|---|
| 343 | |
|---|
| 344 | /* helper func to store hits into coverages */ |
|---|
| 345 | static coverager_t xc_coverager_get(const char *filename TSRMLS_DC) /* {{{ */ |
|---|
| 346 | { |
|---|
| 347 | int len = strlen(filename) + 1; |
|---|
| 348 | coverager_t cov, *pcov; |
|---|
| 349 | |
|---|
| 350 | if (zend_u_hash_find(XG(coverages), IS_STRING, filename, len, (void **) &pcov) == SUCCESS) { |
|---|
| 351 | TRACE("got coverage %s %p", filename, *pcov); |
|---|
| 352 | return *pcov; |
|---|
| 353 | } |
|---|
| 354 | else { |
|---|
| 355 | cov = emalloc(sizeof(HashTable)); |
|---|
| 356 | zend_hash_init(cov, 0, NULL, NULL, 0); |
|---|
| 357 | zend_u_hash_add(XG(coverages), IS_STRING, filename, len, (void **) &cov, sizeof(cov), NULL); |
|---|
| 358 | TRACE("new coverage %s %p", filename, cov); |
|---|
| 359 | return cov; |
|---|
| 360 | } |
|---|
| 361 | } |
|---|
| 362 | /* }}} */ |
|---|
| 363 | static void xc_coverager_add_hits(HashTable *cov, long line, long hits TSRMLS_DC) /* {{{ */ |
|---|
| 364 | { |
|---|
| 365 | long *poldhits; |
|---|
| 366 | |
|---|
| 367 | if (line == 0) { |
|---|
| 368 | return; |
|---|
| 369 | } |
|---|
| 370 | if (zend_hash_index_find(cov, line, (void**)&poldhits) == SUCCESS) { |
|---|
| 371 | if (hits == -1) { |
|---|
| 372 | /* OPTIMIZE: -1 == init-ing, but it's already initized */ |
|---|
| 373 | return; |
|---|
| 374 | } |
|---|
| 375 | if (*poldhits != -1) { |
|---|
| 376 | hits += *poldhits; |
|---|
| 377 | } |
|---|
| 378 | } |
|---|
| 379 | zend_hash_index_update(cov, line, &hits, sizeof(hits), NULL); |
|---|
| 380 | } |
|---|
| 381 | /* }}} */ |
|---|
| 382 | |
|---|
| 383 | static int xc_coverager_get_op_array_size_no_tail(zend_op_array *op_array) /* {{{ */ |
|---|
| 384 | { |
|---|
| 385 | zend_uint last = op_array->last; |
|---|
| 386 | do { |
|---|
| 387 | next_op: |
|---|
| 388 | if (last == 0) { |
|---|
| 389 | break; |
|---|
| 390 | } |
|---|
| 391 | switch (op_array->opcodes[last - 1].opcode) { |
|---|
| 392 | #ifdef ZEND_HANDLE_EXCEPTION |
|---|
| 393 | case ZEND_HANDLE_EXCEPTION: |
|---|
| 394 | #endif |
|---|
| 395 | case ZEND_RETURN: |
|---|
| 396 | case ZEND_EXT_STMT: |
|---|
| 397 | --last; |
|---|
| 398 | goto next_op; |
|---|
| 399 | } |
|---|
| 400 | } while (0); |
|---|
| 401 | return last; |
|---|
| 402 | } |
|---|
| 403 | /* }}} */ |
|---|
| 404 | |
|---|
| 405 | /* prefill */ |
|---|
| 406 | static int xc_coverager_init_op_array(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 407 | { |
|---|
| 408 | zend_uint size; |
|---|
| 409 | coverager_t cov; |
|---|
| 410 | zend_uint i; |
|---|
| 411 | |
|---|
| 412 | if (op_array->type != ZEND_USER_FUNCTION) { |
|---|
| 413 | return 0; |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | size = xc_coverager_get_op_array_size_no_tail(op_array); |
|---|
| 417 | cov = xc_coverager_get(op_array->filename TSRMLS_CC); |
|---|
| 418 | for (i = 0; i < size; i ++) { |
|---|
| 419 | switch (op_array->opcodes[i].opcode) { |
|---|
| 420 | case ZEND_EXT_STMT: |
|---|
| 421 | #if 0 |
|---|
| 422 | case ZEND_EXT_FCALL_BEGIN: |
|---|
| 423 | case ZEND_EXT_FCALL_END: |
|---|
| 424 | #endif |
|---|
| 425 | xc_coverager_add_hits(cov, op_array->opcodes[i].lineno, -1 TSRMLS_CC); |
|---|
| 426 | break; |
|---|
| 427 | } |
|---|
| 428 | } |
|---|
| 429 | return 0; |
|---|
| 430 | } |
|---|
| 431 | /* }}} */ |
|---|
| 432 | static void xc_coverager_init_compile_result(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 433 | { |
|---|
| 434 | xc_compile_result_t cr; |
|---|
| 435 | |
|---|
| 436 | xc_compile_result_init_cur(&cr, op_array TSRMLS_CC); |
|---|
| 437 | xc_apply_op_array(&cr, (apply_func_t) xc_coverager_init_op_array TSRMLS_CC); |
|---|
| 438 | xc_compile_result_free(&cr); |
|---|
| 439 | } |
|---|
| 440 | /* }}} */ |
|---|
| 441 | static zend_op_array *xc_compile_file_for_coverage(zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
|---|
| 442 | { |
|---|
| 443 | zend_op_array *op_array; |
|---|
| 444 | |
|---|
| 445 | op_array = old_compile_file(h, type TSRMLS_CC); |
|---|
| 446 | if (op_array) { |
|---|
| 447 | if (XG(coverager)) { |
|---|
| 448 | xc_coverager_initenv(TSRMLS_C); |
|---|
| 449 | xc_coverager_init_compile_result(op_array TSRMLS_CC); |
|---|
| 450 | } |
|---|
| 451 | } |
|---|
| 452 | return op_array; |
|---|
| 453 | } |
|---|
| 454 | /* }}} */ |
|---|
| 455 | |
|---|
| 456 | /* hits */ |
|---|
| 457 | void xc_coverager_handle_ext_stmt(zend_op_array *op_array, zend_uchar op) /* {{{ */ |
|---|
| 458 | { |
|---|
| 459 | TSRMLS_FETCH(); |
|---|
| 460 | |
|---|
| 461 | if (XG(coverages) && XG(coverage_enabled)) { |
|---|
| 462 | int size = xc_coverager_get_op_array_size_no_tail(op_array); |
|---|
| 463 | int oplineno = (*EG(opline_ptr)) - op_array->opcodes; |
|---|
| 464 | if (oplineno < size) { |
|---|
| 465 | xc_coverager_add_hits(xc_coverager_get(op_array->filename TSRMLS_CC), (*EG(opline_ptr))->lineno, 1 TSRMLS_CC); |
|---|
| 466 | } |
|---|
| 467 | } |
|---|
| 468 | } |
|---|
| 469 | /* }}} */ |
|---|
| 470 | |
|---|
| 471 | /* MINIT/MSHUTDOWN */ |
|---|
| 472 | int xc_coverager_module_init(int module_number TSRMLS_DC) /* {{{ */ |
|---|
| 473 | { |
|---|
| 474 | old_compile_file = zend_compile_file; |
|---|
| 475 | zend_compile_file = xc_compile_file_for_coverage; |
|---|
| 476 | |
|---|
| 477 | if (cfg_get_string("xcache.coveragedump_directory", &xc_coveragedump_dir) == SUCCESS && xc_coveragedump_dir) { |
|---|
| 478 | int len; |
|---|
| 479 | xc_coveragedump_dir = pestrdup(xc_coveragedump_dir, 1); |
|---|
| 480 | len = strlen(xc_coveragedump_dir); |
|---|
| 481 | if (len) { |
|---|
| 482 | if (xc_coveragedump_dir[len - 1] == '/') { |
|---|
| 483 | xc_coveragedump_dir[len - 1] = '\0'; |
|---|
| 484 | } |
|---|
| 485 | } |
|---|
| 486 | if (!strlen(xc_coveragedump_dir)) { |
|---|
| 487 | pefree(xc_coveragedump_dir, 1); |
|---|
| 488 | xc_coveragedump_dir = NULL; |
|---|
| 489 | } |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | return SUCCESS; |
|---|
| 493 | } |
|---|
| 494 | /* }}} */ |
|---|
| 495 | void xc_coverager_module_shutdown() /* {{{ */ |
|---|
| 496 | { |
|---|
| 497 | if (old_compile_file == xc_compile_file_for_coverage) { |
|---|
| 498 | zend_compile_file = old_compile_file; |
|---|
| 499 | } |
|---|
| 500 | if (xc_coveragedump_dir) { |
|---|
| 501 | pefree(xc_coveragedump_dir, 1); |
|---|
| 502 | xc_coveragedump_dir = NULL; |
|---|
| 503 | } |
|---|
| 504 | } |
|---|
| 505 | /* }}} */ |
|---|
| 506 | |
|---|
| 507 | /* user api */ |
|---|
| 508 | /* {{{ proto array xcache_coverager_decode(string data) |
|---|
| 509 | * decode specified data which is saved by auto dumper to array |
|---|
| 510 | */ |
|---|
| 511 | PHP_FUNCTION(xcache_coverager_decode) |
|---|
| 512 | { |
|---|
| 513 | char *str; |
|---|
| 514 | int len; |
|---|
| 515 | long *p; |
|---|
| 516 | |
|---|
| 517 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len) == FAILURE) { |
|---|
| 518 | return; |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | array_init(return_value); |
|---|
| 522 | |
|---|
| 523 | p = (long*) str; |
|---|
| 524 | len -= sizeof(long); |
|---|
| 525 | if (len < 0) { |
|---|
| 526 | return; |
|---|
| 527 | } |
|---|
| 528 | if (*p++ != PCOV_HEADER_MAGIC) { |
|---|
| 529 | TRACE("%s", "wrong magic in xcache_coverager_decode"); |
|---|
| 530 | return; |
|---|
| 531 | } |
|---|
| 532 | |
|---|
| 533 | for (; len >= sizeof(long) * 2; len -= sizeof(long) * 2, p += 2) { |
|---|
| 534 | add_index_long(return_value, p[0], p[1] < 0 ? 0 : p[1]); |
|---|
| 535 | } |
|---|
| 536 | } |
|---|
| 537 | /* }}} */ |
|---|
| 538 | /* {{{ proto void xcache_coverager_start([bool clean = true]) |
|---|
| 539 | * starts coverager data collecting |
|---|
| 540 | */ |
|---|
| 541 | PHP_FUNCTION(xcache_coverager_start) |
|---|
| 542 | { |
|---|
| 543 | zend_bool clean = 1; |
|---|
| 544 | |
|---|
| 545 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) { |
|---|
| 546 | return; |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| 549 | if (clean) { |
|---|
| 550 | xc_coverager_clean(TSRMLS_C); |
|---|
| 551 | } |
|---|
| 552 | |
|---|
| 553 | if (XG(coverager)) { |
|---|
| 554 | xc_coverager_enable(TSRMLS_C); |
|---|
| 555 | } |
|---|
| 556 | else { |
|---|
| 557 | php_error(E_WARNING, "You can only start coverager after you set 'xcache.coverager' to 'On' in ini"); |
|---|
| 558 | } |
|---|
| 559 | } |
|---|
| 560 | /* }}} */ |
|---|
| 561 | /* {{{ proto void xcache_coverager_stop([bool clean = false]) |
|---|
| 562 | * stop coverager data collecting |
|---|
| 563 | */ |
|---|
| 564 | PHP_FUNCTION(xcache_coverager_stop) |
|---|
| 565 | { |
|---|
| 566 | zend_bool clean = 0; |
|---|
| 567 | |
|---|
| 568 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) { |
|---|
| 569 | return; |
|---|
| 570 | } |
|---|
| 571 | |
|---|
| 572 | xc_coverager_disable(TSRMLS_C); |
|---|
| 573 | if (clean) { |
|---|
| 574 | xc_coverager_clean(TSRMLS_C); |
|---|
| 575 | } |
|---|
| 576 | } |
|---|
| 577 | /* }}} */ |
|---|
| 578 | /* {{{ proto array xcache_coverager_get([bool clean = false]) |
|---|
| 579 | * get coverager data collected |
|---|
| 580 | */ |
|---|
| 581 | PHP_FUNCTION(xcache_coverager_get) |
|---|
| 582 | { |
|---|
| 583 | zend_bool clean = 0; |
|---|
| 584 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) { |
|---|
| 585 | return; |
|---|
| 586 | } |
|---|
| 587 | |
|---|
| 588 | xc_coverager_dump(return_value TSRMLS_CC); |
|---|
| 589 | if (clean) { |
|---|
| 590 | xc_coverager_clean(TSRMLS_C); |
|---|
| 591 | } |
|---|
| 592 | } |
|---|
| 593 | /* }}} */ |
|---|