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