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