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