| [543] | 1 | #if 0 |
|---|
| 2 | #define XCACHE_DEBUG |
|---|
| 3 | #endif |
|---|
| 4 | |
|---|
| [1] | 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] | 11 | #include <sys/types.h> |
|---|
| 12 | #include <sys/stat.h> |
|---|
| 13 | #include <fcntl.h> |
|---|
| 14 | |
|---|
| [1] | 15 | #include "stack.h" |
|---|
| 16 | #include "xcache_globals.h" |
|---|
| [27] | 17 | #include "coverager.h" |
|---|
| [1] | 18 | #include "utils.h" |
|---|
| [27] | 19 | typedef HashTable *coverager_t; |
|---|
| [1] | 20 | #define PCOV_HEADER_MAGIC 0x564f4350 |
|---|
| 21 | |
|---|
| [204] | 22 | static char *xc_coveragedump_dir = NULL; |
|---|
| [404] | 23 | static zend_compile_file_t *old_compile_file = NULL; |
|---|
| [1] | 24 | |
|---|
| 25 | /* dumper */ |
|---|
| 26 | static void xc_destroy_coverage(void *pDest) /* {{{ */ |
|---|
| 27 | { |
|---|
| [27] | 28 | coverager_t cov = *(coverager_t*) pDest; |
|---|
| [305] | 29 | TRACE("destroy %p", cov); |
|---|
| [1] | 30 | zend_hash_destroy(cov); |
|---|
| 31 | efree(cov); |
|---|
| 32 | } |
|---|
| 33 | /* }}} */ |
|---|
| [13] | 34 | void xcache_mkdirs_ex(char *root, int rootlen, char *path, int pathlen TSRMLS_DC) /* {{{ */ |
|---|
| [1] | 35 | { |
|---|
| 36 | char *fullpath; |
|---|
| 37 | struct stat st; |
|---|
| [485] | 38 | ALLOCA_FLAG(use_heap) |
|---|
| [1] | 39 | |
|---|
| [305] | 40 | TRACE("mkdirs %s %d %s %d", root, rootlen, path, pathlen); |
|---|
| [485] | 41 | fullpath = my_do_alloca(rootlen + pathlen + 1, use_heap); |
|---|
| [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 | } |
|---|
| [305] | 55 | TRACE("mkdir %s", fullpath); |
|---|
| [51] | 56 | #if PHP_MAJOR_VERSION > 5 |
|---|
| [1] | 57 | php_stream_mkdir(fullpath, 0700, REPORT_ERRORS, NULL); |
|---|
| [16] | 58 | #else |
|---|
| 59 | mkdir(fullpath, 0700); |
|---|
| 60 | #endif |
|---|
| [1] | 61 | } |
|---|
| [485] | 62 | my_free_alloca(fullpath, use_heap); |
|---|
| [1] | 63 | } |
|---|
| 64 | /* }}} */ |
|---|
| [27] | 65 | static void xc_coverager_save_cov(char *srcfile, char *outfilename, coverager_t cov TSRMLS_DC) /* {{{ */ |
|---|
| [1] | 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) { |
|---|
| [305] | 110 | TRACE("%s", "new file"); |
|---|
| [1] | 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 | } |
|---|
| [305] | 118 | TRACE("oldsize %d", (int) len); |
|---|
| [1] | 119 | do { |
|---|
| 120 | p = (long *) contents; |
|---|
| 121 | len -= sizeof(long); |
|---|
| 122 | if (len < 0) { |
|---|
| 123 | break; |
|---|
| 124 | } |
|---|
| 125 | if (*p++ != PCOV_HEADER_MAGIC) { |
|---|
| [305] | 126 | TRACE("wrong magic in file %s", outfilename); |
|---|
| [1] | 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) { |
|---|
| [212] | 138 | if (p[1] == -1) { |
|---|
| [208] | 139 | /* OPTIMIZE: already marked */ |
|---|
| [1] | 140 | continue; |
|---|
| 141 | } |
|---|
| [212] | 142 | if (*phits != -1) { |
|---|
| 143 | p[1] += *phits; |
|---|
| 144 | } |
|---|
| [1] | 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; |
|---|
| [208] | 164 | *p++ = *phits; |
|---|
| 165 | if (*phits > 0) { |
|---|
| [1] | 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 | /* }}} */ |
|---|
| [204] | 184 | |
|---|
| 185 | static void xc_coverager_initenv(TSRMLS_D) /* {{{ */ |
|---|
| [1] | 186 | { |
|---|
| [204] | 187 | if (!XG(coverages)) { |
|---|
| [1] | 188 | XG(coverages) = emalloc(sizeof(HashTable)); |
|---|
| 189 | zend_hash_init(XG(coverages), 0, NULL, xc_destroy_coverage, 0); |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | /* }}} */ |
|---|
| [204] | 193 | static void xc_coverager_clean(TSRMLS_D) /* {{{ */ |
|---|
| [1] | 194 | { |
|---|
| [204] | 195 | if (XG(coverages)) { |
|---|
| [208] | 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 | |
|---|
| [212] | 211 | if (hits != -1) { |
|---|
| 212 | hits = -1; |
|---|
| [208] | 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)) { |
|---|
| [204] | 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 | { |
|---|
| [206] | 246 | if (XG(coverager)) { |
|---|
| [204] | 247 | xc_coverager_enable(TSRMLS_C); |
|---|
| [548] | 248 | #ifdef ZEND_COMPILE_EXTENDED_INFO |
|---|
| 249 | CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO; |
|---|
| 250 | #else |
|---|
| [204] | 251 | CG(extended_info) = 1; |
|---|
| [548] | 252 | #endif |
|---|
| [204] | 253 | } |
|---|
| 254 | else { |
|---|
| 255 | XG(coverage_enabled) = 0; |
|---|
| 256 | } |
|---|
| 257 | } |
|---|
| 258 | /* }}} */ |
|---|
| 259 | static void xc_coverager_autodump(TSRMLS_D) /* {{{ */ |
|---|
| 260 | { |
|---|
| [27] | 261 | coverager_t *pcov; |
|---|
| [1] | 262 | zstr s; |
|---|
| 263 | char *outfilename; |
|---|
| [90] | 264 | int dumpdir_len, outfilelen, alloc_len = 0; |
|---|
| 265 | uint size; |
|---|
| [204] | 266 | HashPosition pos; |
|---|
| [1] | 267 | |
|---|
| [204] | 268 | if (XG(coverages) && xc_coveragedump_dir) { |
|---|
| [1] | 269 | dumpdir_len = strlen(xc_coveragedump_dir); |
|---|
| 270 | alloc_len = dumpdir_len + 1 + 128; |
|---|
| 271 | outfilename = emalloc(alloc_len); |
|---|
| 272 | strcpy(outfilename, xc_coveragedump_dir); |
|---|
| 273 | |
|---|
| [204] | 274 | zend_hash_internal_pointer_reset_ex(XG(coverages), &pos); |
|---|
| 275 | while (zend_hash_get_current_data_ex(XG(coverages), (void **) &pcov, &pos) == SUCCESS) { |
|---|
| 276 | zend_hash_get_current_key_ex(XG(coverages), &s, &size, NULL, 0, &pos); |
|---|
| [1] | 277 | outfilelen = dumpdir_len + size + 5; |
|---|
| 278 | if (alloc_len < outfilelen) { |
|---|
| 279 | alloc_len = outfilelen + 128; |
|---|
| 280 | outfilename = erealloc(outfilename, alloc_len); |
|---|
| 281 | } |
|---|
| 282 | strcpy(outfilename + dumpdir_len, ZSTR_S(s)); |
|---|
| 283 | strcpy(outfilename + dumpdir_len + size - 1, ".pcov"); |
|---|
| 284 | |
|---|
| [305] | 285 | TRACE("outfilename %s", outfilename); |
|---|
| [27] | 286 | xc_coverager_save_cov(ZSTR_S(s), outfilename, *pcov TSRMLS_CC); |
|---|
| [204] | 287 | zend_hash_move_forward_ex(XG(coverages), &pos); |
|---|
| [1] | 288 | } |
|---|
| [176] | 289 | efree(outfilename); |
|---|
| [1] | 290 | } |
|---|
| [204] | 291 | } |
|---|
| 292 | /* }}} */ |
|---|
| 293 | static void xc_coverager_dump(zval *return_value TSRMLS_DC) /* {{{ */ |
|---|
| 294 | { |
|---|
| 295 | coverager_t *pcov; |
|---|
| 296 | HashPosition pos; |
|---|
| [1] | 297 | |
|---|
| [204] | 298 | if (XG(coverages)) { |
|---|
| 299 | array_init(return_value); |
|---|
| 300 | |
|---|
| 301 | zend_hash_internal_pointer_reset_ex(XG(coverages), &pos); |
|---|
| 302 | while (zend_hash_get_current_data_ex(XG(coverages), (void **) &pcov, &pos) == SUCCESS) { |
|---|
| 303 | zval *lines; |
|---|
| 304 | long *phits; |
|---|
| 305 | coverager_t cov; |
|---|
| 306 | HashPosition pos2; |
|---|
| 307 | zstr filename; |
|---|
| 308 | uint size; |
|---|
| 309 | |
|---|
| 310 | cov = *pcov; |
|---|
| 311 | zend_hash_get_current_key_ex(XG(coverages), &filename, &size, NULL, 0, &pos); |
|---|
| 312 | |
|---|
| 313 | MAKE_STD_ZVAL(lines); |
|---|
| 314 | array_init(lines); |
|---|
| 315 | zend_hash_internal_pointer_reset_ex(cov, &pos2); |
|---|
| 316 | while (zend_hash_get_current_data_ex(cov, (void**)&phits, &pos2) == SUCCESS) { |
|---|
| 317 | long hits = *phits; |
|---|
| 318 | add_index_long(lines, pos2->h, hits >= 0 ? hits : 0); |
|---|
| 319 | zend_hash_move_forward_ex(cov, &pos2); |
|---|
| 320 | } |
|---|
| 321 | add_assoc_zval_ex(return_value, ZSTR_S(filename), strlen(ZSTR_S(filename)) + 1, lines); |
|---|
| 322 | |
|---|
| 323 | zend_hash_move_forward_ex(XG(coverages), &pos); |
|---|
| 324 | } |
|---|
| 325 | } |
|---|
| 326 | else { |
|---|
| 327 | RETVAL_NULL(); |
|---|
| 328 | } |
|---|
| [1] | 329 | } |
|---|
| 330 | /* }}} */ |
|---|
| [204] | 331 | void xc_coverager_request_shutdown(TSRMLS_D) /* {{{ */ |
|---|
| 332 | { |
|---|
| [206] | 333 | if (XG(coverager)) { |
|---|
| [204] | 334 | xc_coverager_autodump(TSRMLS_C); |
|---|
| [208] | 335 | xc_coverager_cleanup(TSRMLS_C); |
|---|
| [204] | 336 | } |
|---|
| 337 | } |
|---|
| 338 | /* }}} */ |
|---|
| [1] | 339 | |
|---|
| 340 | /* helper func to store hits into coverages */ |
|---|
| [27] | 341 | static coverager_t xc_coverager_get(char *filename TSRMLS_DC) /* {{{ */ |
|---|
| [1] | 342 | { |
|---|
| 343 | int len = strlen(filename) + 1; |
|---|
| [27] | 344 | coverager_t cov, *pcov; |
|---|
| [1] | 345 | |
|---|
| 346 | if (zend_hash_find(XG(coverages), filename, len, (void **) &pcov) == SUCCESS) { |
|---|
| [305] | 347 | TRACE("got coverage %s %p", filename, *pcov); |
|---|
| [1] | 348 | return *pcov; |
|---|
| 349 | } |
|---|
| 350 | else { |
|---|
| 351 | cov = emalloc(sizeof(HashTable)); |
|---|
| 352 | zend_hash_init(cov, 0, NULL, NULL, 0); |
|---|
| 353 | zend_hash_add(XG(coverages), filename, len, (void **) &cov, sizeof(cov), NULL); |
|---|
| [305] | 354 | TRACE("new coverage %s %p", filename, cov); |
|---|
| [1] | 355 | return cov; |
|---|
| 356 | } |
|---|
| 357 | } |
|---|
| 358 | /* }}} */ |
|---|
| [27] | 359 | static void xc_coverager_add_hits(HashTable *cov, long line, long hits TSRMLS_DC) /* {{{ */ |
|---|
| [1] | 360 | { |
|---|
| 361 | long *poldhits; |
|---|
| 362 | |
|---|
| 363 | if (line == 0) { |
|---|
| 364 | return; |
|---|
| 365 | } |
|---|
| 366 | if (zend_hash_index_find(cov, line, (void**)&poldhits) == SUCCESS) { |
|---|
| [212] | 367 | if (hits == -1) { |
|---|
| 368 | /* OPTIMIZE: -1 == init-ing, but it's already initized */ |
|---|
| [1] | 369 | return; |
|---|
| 370 | } |
|---|
| [212] | 371 | if (*poldhits != -1) { |
|---|
| 372 | hits += *poldhits; |
|---|
| 373 | } |
|---|
| [1] | 374 | } |
|---|
| 375 | zend_hash_index_update(cov, line, &hits, sizeof(hits), NULL); |
|---|
| 376 | } |
|---|
| 377 | /* }}} */ |
|---|
| 378 | |
|---|
| [27] | 379 | static int xc_coverager_get_op_array_size_no_tail(zend_op_array *op_array) /* {{{ */ |
|---|
| [1] | 380 | { |
|---|
| 381 | zend_uint size; |
|---|
| 382 | |
|---|
| 383 | size = op_array->size; |
|---|
| [228] | 384 | do { |
|---|
| 385 | next_op: |
|---|
| 386 | if (size == 0) { |
|---|
| 387 | break; |
|---|
| 388 | } |
|---|
| 389 | switch (op_array->opcodes[size - 1].opcode) { |
|---|
| 390 | #ifdef ZEND_HANDLE_EXCEPTION |
|---|
| 391 | case ZEND_HANDLE_EXCEPTION: |
|---|
| [1] | 392 | #endif |
|---|
| [228] | 393 | case ZEND_RETURN: |
|---|
| 394 | case ZEND_EXT_STMT: |
|---|
| [1] | 395 | size --; |
|---|
| [228] | 396 | goto next_op; |
|---|
| 397 | } |
|---|
| 398 | } while (0); |
|---|
| [1] | 399 | return size; |
|---|
| 400 | } |
|---|
| 401 | /* }}} */ |
|---|
| 402 | |
|---|
| 403 | /* prefill */ |
|---|
| [27] | 404 | static int xc_coverager_init_op_array(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| [1] | 405 | { |
|---|
| 406 | zend_uint size; |
|---|
| [27] | 407 | coverager_t cov; |
|---|
| [11] | 408 | zend_uint i; |
|---|
| [1] | 409 | |
|---|
| 410 | if (op_array->type != ZEND_USER_FUNCTION) { |
|---|
| 411 | return 0; |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| [27] | 414 | size = xc_coverager_get_op_array_size_no_tail(op_array); |
|---|
| 415 | cov = xc_coverager_get(op_array->filename TSRMLS_CC); |
|---|
| [11] | 416 | for (i = 0; i < size; i ++) { |
|---|
| [1] | 417 | switch (op_array->opcodes[i].opcode) { |
|---|
| 418 | case ZEND_EXT_STMT: |
|---|
| 419 | #if 0 |
|---|
| 420 | case ZEND_EXT_FCALL_BEGIN: |
|---|
| 421 | case ZEND_EXT_FCALL_END: |
|---|
| 422 | #endif |
|---|
| [212] | 423 | xc_coverager_add_hits(cov, op_array->opcodes[i].lineno, -1 TSRMLS_CC); |
|---|
| [1] | 424 | break; |
|---|
| 425 | } |
|---|
| 426 | } |
|---|
| 427 | return 0; |
|---|
| 428 | } |
|---|
| 429 | /* }}} */ |
|---|
| [27] | 430 | static void xc_coverager_init_compile_result(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
|---|
| [1] | 431 | { |
|---|
| 432 | xc_compile_result_t cr; |
|---|
| 433 | |
|---|
| 434 | xc_compile_result_init_cur(&cr, op_array TSRMLS_CC); |
|---|
| [27] | 435 | xc_apply_op_array(&cr, (apply_func_t) xc_coverager_init_op_array TSRMLS_CC); |
|---|
| [1] | 436 | xc_compile_result_free(&cr); |
|---|
| 437 | } |
|---|
| 438 | /* }}} */ |
|---|
| 439 | static zend_op_array *xc_compile_file_for_coverage(zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
|---|
| 440 | { |
|---|
| 441 | zend_op_array *op_array; |
|---|
| 442 | |
|---|
| [402] | 443 | op_array = old_compile_file(h, type TSRMLS_CC); |
|---|
| [204] | 444 | if (op_array) { |
|---|
| [206] | 445 | if (XG(coverager)) { |
|---|
| [204] | 446 | xc_coverager_initenv(TSRMLS_C); |
|---|
| 447 | xc_coverager_init_compile_result(op_array TSRMLS_CC); |
|---|
| 448 | } |
|---|
| [1] | 449 | } |
|---|
| 450 | return op_array; |
|---|
| 451 | } |
|---|
| 452 | /* }}} */ |
|---|
| 453 | |
|---|
| 454 | /* hits */ |
|---|
| [27] | 455 | void xc_coverager_handle_ext_stmt(zend_op_array *op_array, zend_uchar op) /* {{{ */ |
|---|
| [1] | 456 | { |
|---|
| [11] | 457 | TSRMLS_FETCH(); |
|---|
| 458 | |
|---|
| [204] | 459 | if (XG(coverages) && XG(coverage_enabled)) { |
|---|
| [27] | 460 | int size = xc_coverager_get_op_array_size_no_tail(op_array); |
|---|
| [1] | 461 | int oplineno = (*EG(opline_ptr)) - op_array->opcodes; |
|---|
| 462 | if (oplineno < size) { |
|---|
| [27] | 463 | xc_coverager_add_hits(xc_coverager_get(op_array->filename TSRMLS_CC), (*EG(opline_ptr))->lineno, 1 TSRMLS_CC); |
|---|
| [1] | 464 | } |
|---|
| 465 | } |
|---|
| 466 | } |
|---|
| 467 | /* }}} */ |
|---|
| 468 | |
|---|
| 469 | /* init/destroy */ |
|---|
| [27] | 470 | int xc_coverager_init(int module_number TSRMLS_DC) /* {{{ */ |
|---|
| [1] | 471 | { |
|---|
| [402] | 472 | old_compile_file = zend_compile_file; |
|---|
| [204] | 473 | zend_compile_file = xc_compile_file_for_coverage; |
|---|
| 474 | |
|---|
| 475 | if (cfg_get_string("xcache.coveragedump_directory", &xc_coveragedump_dir) == SUCCESS && xc_coveragedump_dir) { |
|---|
| [524] | 476 | xc_coveragedump_dir = pestrdup(xc_coveragedump_dir, 1); |
|---|
| [1] | 477 | int len = strlen(xc_coveragedump_dir); |
|---|
| 478 | if (len) { |
|---|
| 479 | if (xc_coveragedump_dir[len - 1] == '/') { |
|---|
| 480 | xc_coveragedump_dir[len - 1] = '\0'; |
|---|
| 481 | } |
|---|
| 482 | } |
|---|
| [204] | 483 | if (!strlen(xc_coveragedump_dir)) { |
|---|
| [524] | 484 | pefree(xc_coveragedump_dir, 1); |
|---|
| [204] | 485 | xc_coveragedump_dir = NULL; |
|---|
| 486 | } |
|---|
| [1] | 487 | } |
|---|
| [204] | 488 | |
|---|
| [1] | 489 | return SUCCESS; |
|---|
| 490 | } |
|---|
| 491 | /* }}} */ |
|---|
| [27] | 492 | void xc_coverager_destroy() /* {{{ */ |
|---|
| [1] | 493 | { |
|---|
| [402] | 494 | if (old_compile_file == xc_compile_file_for_coverage) { |
|---|
| 495 | zend_compile_file = old_compile_file; |
|---|
| [1] | 496 | } |
|---|
| 497 | if (xc_coveragedump_dir) { |
|---|
| [524] | 498 | pefree(xc_coveragedump_dir, 1); |
|---|
| [1] | 499 | xc_coveragedump_dir = NULL; |
|---|
| 500 | } |
|---|
| 501 | } |
|---|
| 502 | /* }}} */ |
|---|
| 503 | |
|---|
| 504 | /* user api */ |
|---|
| [204] | 505 | /* {{{ proto array xcache_coverager_decode(string data) |
|---|
| 506 | * decode specified data which is saved by auto dumper to array |
|---|
| 507 | */ |
|---|
| 508 | PHP_FUNCTION(xcache_coverager_decode) |
|---|
| [1] | 509 | { |
|---|
| 510 | char *str; |
|---|
| 511 | int len; |
|---|
| 512 | long *p; |
|---|
| 513 | |
|---|
| 514 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len) == FAILURE) { |
|---|
| 515 | return; |
|---|
| 516 | } |
|---|
| 517 | |
|---|
| 518 | array_init(return_value); |
|---|
| 519 | |
|---|
| 520 | p = (long*) str; |
|---|
| 521 | len -= sizeof(long); |
|---|
| 522 | if (len < 0) { |
|---|
| 523 | return; |
|---|
| 524 | } |
|---|
| 525 | if (*p++ != PCOV_HEADER_MAGIC) { |
|---|
| [305] | 526 | TRACE("%s", "wrong magic in xcache_coverager_decode"); |
|---|
| [1] | 527 | return; |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | for (; len >= sizeof(long) * 2; len -= sizeof(long) * 2, p += 2) { |
|---|
| [462] | 531 | add_index_long(return_value, p[0], p[1] < 0 ? 0 : p[1]); |
|---|
| [1] | 532 | } |
|---|
| 533 | } |
|---|
| 534 | /* }}} */ |
|---|
| [204] | 535 | /* {{{ proto void xcache_coverager_start([bool clean = true]) |
|---|
| 536 | * starts coverager data collecting |
|---|
| 537 | */ |
|---|
| 538 | PHP_FUNCTION(xcache_coverager_start) |
|---|
| 539 | { |
|---|
| 540 | zend_bool clean = 1; |
|---|
| [1] | 541 | |
|---|
| [204] | 542 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) { |
|---|
| 543 | return; |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | if (clean) { |
|---|
| 547 | xc_coverager_clean(TSRMLS_C); |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| [206] | 550 | if (XG(coverager)) { |
|---|
| [204] | 551 | xc_coverager_enable(TSRMLS_C); |
|---|
| 552 | } |
|---|
| 553 | else { |
|---|
| [206] | 554 | php_error(E_WARNING, "You can only start coverager after you set 'xcache.coverager' to 'On' in ini"); |
|---|
| [204] | 555 | } |
|---|
| 556 | } |
|---|
| 557 | /* }}} */ |
|---|
| 558 | /* {{{ proto void xcache_coverager_stop([bool clean = false]) |
|---|
| 559 | * stop coverager data collecting |
|---|
| 560 | */ |
|---|
| 561 | PHP_FUNCTION(xcache_coverager_stop) |
|---|
| 562 | { |
|---|
| 563 | zend_bool clean = 0; |
|---|
| 564 | |
|---|
| 565 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) { |
|---|
| 566 | return; |
|---|
| 567 | } |
|---|
| 568 | |
|---|
| 569 | xc_coverager_disable(TSRMLS_C); |
|---|
| 570 | if (clean) { |
|---|
| 571 | xc_coverager_clean(TSRMLS_C); |
|---|
| 572 | } |
|---|
| 573 | } |
|---|
| 574 | /* }}} */ |
|---|
| 575 | /* {{{ proto array xcache_coverager_get([bool clean = false]) |
|---|
| 576 | * get coverager data collected |
|---|
| 577 | */ |
|---|
| 578 | PHP_FUNCTION(xcache_coverager_get) |
|---|
| 579 | { |
|---|
| 580 | zend_bool clean = 0; |
|---|
| 581 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) { |
|---|
| 582 | return; |
|---|
| 583 | } |
|---|
| 584 | |
|---|
| 585 | xc_coverager_dump(return_value TSRMLS_CC); |
|---|
| 586 | if (clean) { |
|---|
| 587 | xc_coverager_clean(TSRMLS_C); |
|---|
| 588 | } |
|---|
| 589 | } |
|---|
| 590 | /* }}} */ |
|---|