| 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 | ftruncate(fd, 0); |
|---|
| 175 | lseek(fd, 0, SEEK_SET); |
|---|
| 176 | write(fd, (char *) buf, size); |
|---|
| 177 | |
|---|
| 178 | bailout: |
|---|
| 179 | if (contents) efree(contents); |
|---|
| 180 | if (fd >= 0) close(fd); |
|---|
| 181 | if (buf) efree(buf); |
|---|
| 182 | } |
|---|
| 183 | /* }}} */ |
|---|
| 184 | |
|---|
| 185 | static void xc_coverager_initenv(TSRMLS_D) /* {{{ */ |
|---|
| 186 | { |
|---|
| 187 | if (!XG(coverages)) { |
|---|
| 188 | XG(coverages) = emalloc(sizeof(HashTable)); |
|---|
| 189 | zend_hash_init(XG(coverages), 0, NULL, xc_destroy_coverage, 0); |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | /* }}} */ |
|---|
| 193 | static void xc_coverager_clean(TSRMLS_D) /* {{{ */ |
|---|
| 194 | { |
|---|
| 195 | if (XG(coverages)) { |
|---|
| 196 | HashPosition pos; |
|---|
| 197 | coverager_t *pcov; |
|---|
| 198 | |
|---|
| 199 | zend_hash_internal_pointer_reset_ex(XG(coverages), &pos); |
|---|
| 200 | while (zend_hash_get_current_data_ex(XG(coverages), (void **) &pcov, &pos) == SUCCESS) { |
|---|
| 201 | long *phits; |
|---|
| 202 | coverager_t cov; |
|---|
| 203 | HashPosition pos2; |
|---|
| 204 | |
|---|
| 205 | cov = *pcov; |
|---|
| 206 | |
|---|
| 207 | zend_hash_internal_pointer_reset_ex(cov, &pos2); |
|---|
| 208 | while (zend_hash_get_current_data_ex(cov, (void**)&phits, &pos2) == SUCCESS) { |
|---|
| 209 | long hits = *phits; |
|---|
| 210 | |
|---|
| 211 | if (hits != -1) { |
|---|
| 212 | hits = -1; |
|---|
| 213 | zend_hash_index_update(cov, pos2->h, &hits, sizeof(hits), NULL); |
|---|
| 214 | } |
|---|
| 215 | zend_hash_move_forward_ex(cov, &pos2); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | zend_hash_move_forward_ex(XG(coverages), &pos); |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | /* }}} */ |
|---|
| 223 | static void xc_coverager_cleanup(TSRMLS_D) /* {{{ */ |
|---|
| 224 | { |
|---|
| 225 | if (XG(coverages)) { |
|---|
| 226 | zend_hash_destroy(XG(coverages)); |
|---|
| 227 | efree(XG(coverages)); |
|---|
| 228 | XG(coverages) = NULL; |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | /* }}} */ |
|---|
| 232 | |
|---|
| 233 | static void xc_coverager_enable(TSRMLS_D) /* {{{ */ |
|---|
| 234 | { |
|---|
| 235 | XG(coverage_enabled) = 1; |
|---|
| 236 | } |
|---|
| 237 | /* }}} */ |
|---|
| 238 | static void xc_coverager_disable(TSRMLS_D) /* {{{ */ |
|---|
| 239 | { |
|---|
| 240 | XG(coverage_enabled) = 0; |
|---|
| 241 | } |
|---|
| 242 | /* }}} */ |
|---|
| 243 | |
|---|
| 244 | void xc_coverager_request_init(TSRMLS_D) /* {{{ */ |
|---|
| 245 | { |
|---|
| 246 | if (XG(coverager)) { |
|---|
| 247 | xc_coverager_enable(TSRMLS_C); |
|---|
| 248 | CG(extended_info) = 1; |
|---|
| 249 | } |
|---|
| 250 | else { |
|---|
| 251 | XG(coverage_enabled) = 0; |
|---|
| 252 | } |
|---|
| 253 | } |
|---|
| 254 | /* }}} */ |
|---|
| 255 | static void xc_coverager_autodump(TSRMLS_D) /* {{{ */ |
|---|
| 256 | { |
|---|
| 257 | coverager_t *pcov; |
|---|
| 258 | zstr s; |
|---|
| 259 | char *outfilename; |
|---|
| 260 | int dumpdir_len, outfilelen, alloc_len = 0; |
|---|
| 261 | uint size; |
|---|
| 262 | HashPosition pos; |
|---|
| 263 | |
|---|
| 264 | if (XG(coverages) && xc_coveragedump_dir) { |
|---|
| 265 | dumpdir_len = strlen(xc_coveragedump_dir); |
|---|
| 266 | alloc_len = dumpdir_len + 1 + 128; |
|---|
| 267 | outfilename = emalloc(alloc_len); |
|---|
| 268 | strcpy(outfilename, xc_coveragedump_dir); |
|---|
| 269 | |
|---|
| 270 | zend_hash_internal_pointer_reset_ex(XG(coverages), &pos); |
|---|
| 271 | while (zend_hash_get_current_data_ex(XG(coverages), (void **) &pcov, &pos) == SUCCESS) { |
|---|
| 272 | zend_hash_get_current_key_ex(XG(coverages), &s, &size, NULL, 0, &pos); |
|---|
| 273 | outfilelen = dumpdir_len + size + 5; |
|---|
| 274 | if (alloc_len < outfilelen) { |
|---|
| 275 | alloc_len = outfilelen + 128; |
|---|
| 276 | outfilename = erealloc(outfilename, alloc_len); |
|---|
| 277 | } |
|---|
| 278 | strcpy(outfilename + dumpdir_len, ZSTR_S(s)); |
|---|
| 279 | strcpy(outfilename + dumpdir_len + size - 1, ".pcov"); |
|---|
| 280 | |
|---|
| 281 | TRACE("outfilename %s", outfilename); |
|---|
| 282 | xc_coverager_save_cov(ZSTR_S(s), outfilename, *pcov TSRMLS_CC); |
|---|
| 283 | zend_hash_move_forward_ex(XG(coverages), &pos); |
|---|
| 284 | } |
|---|
| 285 | efree(outfilename); |
|---|
| 286 | } |
|---|
| 287 | } |
|---|
| 288 | /* }}} */ |
|---|
| 289 | static void xc_coverager_dump(zval *return_value TSRMLS_DC) /* {{{ */ |
|---|
| 290 | { |
|---|
| 291 | coverager_t *pcov; |
|---|
| 292 | HashPosition pos; |
|---|
| 293 | |
|---|
| 294 | if (XG(coverages)) { |
|---|
| 295 | array_init(return_value); |
|---|
| 296 | |
|---|
| 297 | zend_hash_internal_pointer_reset_ex(XG(coverages), &pos); |
|---|
| 298 | while (zend_hash_get_current_data_ex(XG(coverages), (void **) &pcov, &pos) == SUCCESS) { |
|---|
| 299 | zval *lines; |
|---|
| 300 | long *phits; |
|---|
| 301 | coverager_t cov; |
|---|
| 302 | HashPosition pos2; |
|---|
| 303 | zstr filename; |
|---|
| 304 | uint size; |
|---|
| 305 | |
|---|
| 306 | cov = *pcov; |
|---|
| 307 | zend_hash_get_current_key_ex(XG(coverages), &filename, &size, NULL, 0, &pos); |
|---|
| 308 | |
|---|
| 309 | MAKE_STD_ZVAL(lines); |
|---|
| 310 | array_init(lines); |
|---|
| 311 | zend_hash_internal_pointer_reset_ex(cov, &pos2); |
|---|
| 312 | while (zend_hash_get_current_data_ex(cov, (void**)&phits, &pos2) == SUCCESS) { |
|---|
| 313 | long hits = *phits; |
|---|
| 314 | add_index_long(lines, pos2->h, hits >= 0 ? hits : 0); |
|---|
| 315 | zend_hash_move_forward_ex(cov, &pos2); |
|---|
| 316 | } |
|---|
| 317 | add_assoc_zval_ex(return_value, ZSTR_S(filename), strlen(ZSTR_S(filename)) + 1, lines); |
|---|
| 318 | |
|---|
| 319 | zend_hash_move_forward_ex(XG(coverages), &pos); |
|---|
| 320 | } |
|---|
| 321 | } |
|---|
| 322 | else { |
|---|
| 323 | RETVAL_NULL(); |
|---|
| 324 | } |
|---|
| 325 | } |
|---|
| 326 | /* }}} */ |
|---|
| 327 | void xc_coverager_request_shutdown(TSRMLS_D) /* {{{ */ |
|---|
| 328 | { |
|---|
| 329 | if (XG(coverager)) { |
|---|
| 330 | xc_coverager_autodump(TSRMLS_C); |
|---|
| 331 | xc_coverager_cleanup(TSRMLS_C); |
|---|
| 332 | } |
|---|
| 333 | } |
|---|
| 334 | /* }}} */ |
|---|
| 335 | |
|---|
| 336 | /* helper func to store hits into coverages */ |
|---|
| 337 | static coverager_t xc_coverager_get(char *filename TSRMLS_DC) /* {{{ */ |
|---|
| 338 | { |
|---|
| 339 | int len = strlen(filename) + 1; |
|---|
| 340 | coverager_t cov, *pcov; |
|---|
| 341 | |
|---|
| 342 | if (zend_hash_find(XG(coverages), filename, len, (void **) &pcov) == SUCCESS) { |
|---|
| 343 | TRACE("got coverage %s %p", filename, *pcov); |
|---|
| 344 | return *pcov; |
|---|
| 345 | } |
|---|
| 346 | else { |
|---|
| 347 | cov = emalloc(sizeof(HashTable)); |
|---|
| 348 | zend_hash_init(cov, 0, NULL, NULL, 0); |
|---|
| 349 | zend_hash_add(XG(coverages), filename, len, (void **) &cov, sizeof(cov), NULL); |
|---|
| 350 | TRACE("new coverage %s %p", filename, cov); |
|---|
| 351 | return cov; |
|---|
| 352 | } |
|---|
| 353 | } |
|---|
| 354 | /* }}} */ |
|---|
| 355 | static void xc_coverager_add_hits(HashTable *cov, long line, long hits TSRMLS_DC) /* {{{ */ |
|---|
| 356 | { |
|---|
| 357 | long *poldhits; |
|---|
| 358 | |
|---|
| 359 | if (line == 0) { |
|---|
| 360 | return; |
|---|
| 361 | } |
|---|
| 362 | if (zend_hash_index_find(cov, line, (void**)&poldhits) == SUCCESS) { |
|---|
| 363 | if (hits == -1) { |
|---|
| 364 | /* OPTIMIZE: -1 == init-ing, but it's already initized */ |
|---|
| 365 | return; |
|---|
| 366 | } |
|---|
| 367 | if (*poldhits != -1) { |
|---|
| 368 | hits += *poldhits; |
|---|
| 369 | } |
|---|
| 370 | } |
|---|
| 371 | zend_hash_index_update(cov, line, &hits, sizeof(hits), NULL); |
|---|
| 372 | } |
|---|
| 373 | /* }}} */ |
|---|
| 374 | |
|---|
| 375 | static int xc_coverager_get_op_array_size_no_tail(zend_op_array *op_array) /* {{{ */ |
|---|
| 376 | { |
|---|
| 377 | zend_uint size; |
|---|
| 378 | |
|---|
| 379 | size = op_array->size; |
|---|
| 380 | do { |
|---|
| 381 | next_op: |
|---|
| 382 | if (size == 0) { |
|---|
| 383 | break; |
|---|
| 384 | } |
|---|
| 385 | switch (op_array->opcodes[size - 1].opcode) { |
|---|
| 386 | #ifdef ZEND_HANDLE_EXCEPTION |
|---|
| 387 | case ZEND_HANDLE_EXCEPTION: |
|---|
| 388 | #endif |
|---|
| 389 | case ZEND_RETURN: |
|---|
| 390 | case ZEND_EXT_STMT: |
|---|
| 391 | size --; |
|---|
| 392 | goto next_op; |
|---|
| 393 | } |
|---|
| 394 | } while (0); |
|---|
| 395 | return size; |
|---|
| 396 | } |
|---|
| 397 | /* }}} */ |
|---|
| 398 | |
|---|
| 399 | /* prefill */ |
|---|
| 400 | static int xc_coverager_init_op_array(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 401 | { |
|---|
| 402 | zend_uint size; |
|---|
| 403 | coverager_t cov; |
|---|
| 404 | zend_uint i; |
|---|
| 405 | |
|---|
| 406 | if (op_array->type != ZEND_USER_FUNCTION) { |
|---|
| 407 | return 0; |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | size = xc_coverager_get_op_array_size_no_tail(op_array); |
|---|
| 411 | cov = xc_coverager_get(op_array->filename TSRMLS_CC); |
|---|
| 412 | for (i = 0; i < size; i ++) { |
|---|
| 413 | switch (op_array->opcodes[i].opcode) { |
|---|
| 414 | case ZEND_EXT_STMT: |
|---|
| 415 | #if 0 |
|---|
| 416 | case ZEND_EXT_FCALL_BEGIN: |
|---|
| 417 | case ZEND_EXT_FCALL_END: |
|---|
| 418 | #endif |
|---|
| 419 | xc_coverager_add_hits(cov, op_array->opcodes[i].lineno, -1 TSRMLS_CC); |
|---|
| 420 | break; |
|---|
| 421 | } |
|---|
| 422 | } |
|---|
| 423 | return 0; |
|---|
| 424 | } |
|---|
| 425 | /* }}} */ |
|---|
| 426 | static void xc_coverager_init_compile_result(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| 427 | { |
|---|
| 428 | xc_compile_result_t cr; |
|---|
| 429 | |
|---|
| 430 | xc_compile_result_init_cur(&cr, op_array TSRMLS_CC); |
|---|
| 431 | xc_apply_op_array(&cr, (apply_func_t) xc_coverager_init_op_array TSRMLS_CC); |
|---|
| 432 | xc_compile_result_free(&cr); |
|---|
| 433 | } |
|---|
| 434 | /* }}} */ |
|---|
| 435 | static zend_op_array *xc_compile_file_for_coverage(zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
|---|
| 436 | { |
|---|
| 437 | zend_op_array *op_array; |
|---|
| 438 | |
|---|
| 439 | op_array = old_compile_file(h, type TSRMLS_CC); |
|---|
| 440 | if (op_array) { |
|---|
| 441 | if (XG(coverager)) { |
|---|
| 442 | xc_coverager_initenv(TSRMLS_C); |
|---|
| 443 | xc_coverager_init_compile_result(op_array TSRMLS_CC); |
|---|
| 444 | } |
|---|
| 445 | } |
|---|
| 446 | return op_array; |
|---|
| 447 | } |
|---|
| 448 | /* }}} */ |
|---|
| 449 | |
|---|
| 450 | /* hits */ |
|---|
| 451 | void xc_coverager_handle_ext_stmt(zend_op_array *op_array, zend_uchar op) /* {{{ */ |
|---|
| 452 | { |
|---|
| 453 | TSRMLS_FETCH(); |
|---|
| 454 | |
|---|
| 455 | if (XG(coverages) && XG(coverage_enabled)) { |
|---|
| 456 | int size = xc_coverager_get_op_array_size_no_tail(op_array); |
|---|
| 457 | int oplineno = (*EG(opline_ptr)) - op_array->opcodes; |
|---|
| 458 | if (oplineno < size) { |
|---|
| 459 | xc_coverager_add_hits(xc_coverager_get(op_array->filename TSRMLS_CC), (*EG(opline_ptr))->lineno, 1 TSRMLS_CC); |
|---|
| 460 | } |
|---|
| 461 | } |
|---|
| 462 | } |
|---|
| 463 | /* }}} */ |
|---|
| 464 | |
|---|
| 465 | /* init/destroy */ |
|---|
| 466 | int xc_coverager_init(int module_number TSRMLS_DC) /* {{{ */ |
|---|
| 467 | { |
|---|
| 468 | old_compile_file = zend_compile_file; |
|---|
| 469 | zend_compile_file = xc_compile_file_for_coverage; |
|---|
| 470 | |
|---|
| 471 | if (cfg_get_string("xcache.coveragedump_directory", &xc_coveragedump_dir) == SUCCESS && xc_coveragedump_dir) { |
|---|
| 472 | xc_coveragedump_dir = pestrdup(xc_coveragedump_dir, 1); |
|---|
| 473 | int len = strlen(xc_coveragedump_dir); |
|---|
| 474 | if (len) { |
|---|
| 475 | if (xc_coveragedump_dir[len - 1] == '/') { |
|---|
| 476 | xc_coveragedump_dir[len - 1] = '\0'; |
|---|
| 477 | } |
|---|
| 478 | } |
|---|
| 479 | if (!strlen(xc_coveragedump_dir)) { |
|---|
| 480 | pefree(xc_coveragedump_dir, 1); |
|---|
| 481 | xc_coveragedump_dir = NULL; |
|---|
| 482 | } |
|---|
| 483 | } |
|---|
| 484 | |
|---|
| 485 | return SUCCESS; |
|---|
| 486 | } |
|---|
| 487 | /* }}} */ |
|---|
| 488 | void xc_coverager_destroy() /* {{{ */ |
|---|
| 489 | { |
|---|
| 490 | if (old_compile_file == xc_compile_file_for_coverage) { |
|---|
| 491 | zend_compile_file = old_compile_file; |
|---|
| 492 | } |
|---|
| 493 | if (xc_coveragedump_dir) { |
|---|
| 494 | pefree(xc_coveragedump_dir, 1); |
|---|
| 495 | xc_coveragedump_dir = NULL; |
|---|
| 496 | } |
|---|
| 497 | } |
|---|
| 498 | /* }}} */ |
|---|
| 499 | |
|---|
| 500 | /* user api */ |
|---|
| 501 | /* {{{ proto array xcache_coverager_decode(string data) |
|---|
| 502 | * decode specified data which is saved by auto dumper to array |
|---|
| 503 | */ |
|---|
| 504 | PHP_FUNCTION(xcache_coverager_decode) |
|---|
| 505 | { |
|---|
| 506 | char *str; |
|---|
| 507 | int len; |
|---|
| 508 | long *p; |
|---|
| 509 | |
|---|
| 510 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len) == FAILURE) { |
|---|
| 511 | return; |
|---|
| 512 | } |
|---|
| 513 | |
|---|
| 514 | array_init(return_value); |
|---|
| 515 | |
|---|
| 516 | p = (long*) str; |
|---|
| 517 | len -= sizeof(long); |
|---|
| 518 | if (len < 0) { |
|---|
| 519 | return; |
|---|
| 520 | } |
|---|
| 521 | if (*p++ != PCOV_HEADER_MAGIC) { |
|---|
| 522 | TRACE("%s", "wrong magic in xcache_coverager_decode"); |
|---|
| 523 | return; |
|---|
| 524 | } |
|---|
| 525 | |
|---|
| 526 | for (; len >= sizeof(long) * 2; len -= sizeof(long) * 2, p += 2) { |
|---|
| 527 | add_index_long(return_value, p[0], p[1] < 0 ? 0 : p[1]); |
|---|
| 528 | } |
|---|
| 529 | } |
|---|
| 530 | /* }}} */ |
|---|
| 531 | /* {{{ proto void xcache_coverager_start([bool clean = true]) |
|---|
| 532 | * starts coverager data collecting |
|---|
| 533 | */ |
|---|
| 534 | PHP_FUNCTION(xcache_coverager_start) |
|---|
| 535 | { |
|---|
| 536 | zend_bool clean = 1; |
|---|
| 537 | |
|---|
| 538 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) { |
|---|
| 539 | return; |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | if (clean) { |
|---|
| 543 | xc_coverager_clean(TSRMLS_C); |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | if (XG(coverager)) { |
|---|
| 547 | xc_coverager_enable(TSRMLS_C); |
|---|
| 548 | } |
|---|
| 549 | else { |
|---|
| 550 | php_error(E_WARNING, "You can only start coverager after you set 'xcache.coverager' to 'On' in ini"); |
|---|
| 551 | } |
|---|
| 552 | } |
|---|
| 553 | /* }}} */ |
|---|
| 554 | /* {{{ proto void xcache_coverager_stop([bool clean = false]) |
|---|
| 555 | * stop coverager data collecting |
|---|
| 556 | */ |
|---|
| 557 | PHP_FUNCTION(xcache_coverager_stop) |
|---|
| 558 | { |
|---|
| 559 | zend_bool clean = 0; |
|---|
| 560 | |
|---|
| 561 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) { |
|---|
| 562 | return; |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | xc_coverager_disable(TSRMLS_C); |
|---|
| 566 | if (clean) { |
|---|
| 567 | xc_coverager_clean(TSRMLS_C); |
|---|
| 568 | } |
|---|
| 569 | } |
|---|
| 570 | /* }}} */ |
|---|
| 571 | /* {{{ proto array xcache_coverager_get([bool clean = false]) |
|---|
| 572 | * get coverager data collected |
|---|
| 573 | */ |
|---|
| 574 | PHP_FUNCTION(xcache_coverager_get) |
|---|
| 575 | { |
|---|
| 576 | zend_bool clean = 0; |
|---|
| 577 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) { |
|---|
| 578 | return; |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | xc_coverager_dump(return_value TSRMLS_CC); |
|---|
| 582 | if (clean) { |
|---|
| 583 | xc_coverager_clean(TSRMLS_C); |
|---|
| 584 | } |
|---|
| 585 | } |
|---|
| 586 | /* }}} */ |
|---|