| 1 | #if 0 |
|---|
| 2 | #define XCACHE_DEBUG |
|---|
| 3 | #endif |
|---|
| 4 | |
|---|
| 5 | #include "xc_coverager.h" |
|---|
| 6 | |
|---|
| 7 | #include <stdio.h> |
|---|
| 8 | #include "xcache.h" |
|---|
| 9 | #include "ext/standard/flock_compat.h" |
|---|
| 10 | #ifdef HAVE_SYS_FILE_H |
|---|
| 11 | # include <sys/file.h> |
|---|
| 12 | #endif |
|---|
| 13 | #include <sys/types.h> |
|---|
| 14 | #include <sys/stat.h> |
|---|
| 15 | #include <fcntl.h> |
|---|
| 16 | |
|---|
| 17 | #include "xcache/xc_extension.h" |
|---|
| 18 | #include "xcache/xc_ini.h" |
|---|
| 19 | #include "xcache/xc_utils.h" |
|---|
| 20 | #include "util/xc_stack.h" |
|---|
| 21 | #include "util/xc_trace.h" |
|---|
| 22 | #include "xcache_globals.h" |
|---|
| 23 | |
|---|
| 24 | #include "ext/standard/info.h" |
|---|
| 25 | #include "zend_compile.h" |
|---|
| 26 | |
|---|
| 27 | typedef HashTable *coverager_t; |
|---|
| 28 | #define PCOV_HEADER_MAGIC 0x564f4350 |
|---|
| 29 | |
|---|
| 30 | static char *xc_coveragedump_dir = NULL; |
|---|
| 31 | static zend_compile_file_t *old_compile_file = NULL; |
|---|
| 32 | |
|---|
| 33 | /* dumper */ |
|---|
| 34 | static void xc_destroy_coverage(void *pDest) /* {{{ */ |
|---|
| 35 | { |
|---|
| 36 | coverager_t cov = *(coverager_t*) pDest; |
|---|
| 37 | TRACE("destroy %p", cov); |
|---|
| 38 | zend_hash_destroy(cov); |
|---|
| 39 | efree(cov); |
|---|
| 40 | } |
|---|
| 41 | /* }}} */ |
|---|
| 42 | static void xcache_mkdirs_ex(char *root, int rootlen, char *path, int pathlen TSRMLS_DC) /* {{{ */ |
|---|
| 43 | { |
|---|
| 44 | char *fullpath; |
|---|
| 45 | struct stat st; |
|---|
| 46 | ALLOCA_FLAG(use_heap) |
|---|
| 47 | |
|---|
| 48 | TRACE("mkdirs %s %d %s %d", root, rootlen, path, pathlen); |
|---|
| 49 | fullpath = my_do_alloca(rootlen + pathlen + 1, use_heap); |
|---|
| 50 | memcpy(fullpath, root, rootlen); |
|---|
| 51 | memcpy(fullpath + rootlen, path, pathlen); |
|---|
| 52 | fullpath[rootlen + pathlen] = '\0'; |
|---|
| 53 | |
|---|
| 54 | if (stat(fullpath, &st) != 0) { |
|---|
| 55 | char *chr; |
|---|
| 56 | |
|---|
| 57 | chr = strrchr(path, PHP_DIR_SEPARATOR); |
|---|
| 58 | if (chr && chr != path) { |
|---|
| 59 | *chr = '\0'; |
|---|
| 60 | xcache_mkdirs_ex(root, rootlen, path, chr - path TSRMLS_CC); |
|---|
| 61 | *chr = PHP_DIR_SEPARATOR; |
|---|
| 62 | } |
|---|
| 63 | TRACE("mkdir %s", fullpath); |
|---|
| 64 | #if PHP_MAJOR_VERSION > 5 |
|---|
| 65 | php_stream_mkdir(fullpath, 0700, REPORT_ERRORS, NULL); |
|---|
| 66 | #else |
|---|
| 67 | mkdir(fullpath, 0700); |
|---|
| 68 | #endif |
|---|
| 69 | } |
|---|
| 70 | my_free_alloca(fullpath, use_heap); |
|---|
| 71 | } |
|---|
| 72 | /* }}} */ |
|---|
| 73 | static void xc_coverager_save_cov(char *srcfile, char *outfilename, coverager_t cov TSRMLS_DC) /* {{{ */ |
|---|
| 74 | { |
|---|
| 75 | long *buf = NULL, *p; |
|---|
| 76 | long covlines, *phits; |
|---|
| 77 | int fd = -1; |
|---|
| 78 | int size; |
|---|
| 79 | int newfile; |
|---|
| 80 | struct stat srcstat, outstat; |
|---|
| 81 | HashPosition pos; |
|---|
| 82 | char *contents = NULL; |
|---|
| 83 | long len; |
|---|
| 84 | |
|---|
| 85 | if (stat(srcfile, &srcstat) != 0) { |
|---|
| 86 | return; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | newfile = 0; |
|---|
| 90 | if (stat(outfilename, &outstat) != 0) { |
|---|
| 91 | newfile = 1; |
|---|
| 92 | } |
|---|
| 93 | else { |
|---|
| 94 | if (srcstat.st_mtime > outstat.st_mtime) { |
|---|
| 95 | newfile = 1; |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | fd = open(outfilename, O_RDWR | O_CREAT, 0600); |
|---|
| 100 | if (fd < 0) { |
|---|
| 101 | char *chr; |
|---|
| 102 | chr = strrchr(srcfile, PHP_DIR_SEPARATOR); |
|---|
| 103 | if (chr) { |
|---|
| 104 | *chr = '\0'; |
|---|
| 105 | xcache_mkdirs_ex(xc_coveragedump_dir, strlen(xc_coveragedump_dir), srcfile, chr - srcfile TSRMLS_CC); |
|---|
| 106 | *chr = PHP_DIR_SEPARATOR; |
|---|
| 107 | } |
|---|
| 108 | fd = open(outfilename, O_RDWR | O_CREAT, 0600); |
|---|
| 109 | if (fd < 0) { |
|---|
| 110 | goto bailout; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | if (flock(fd, LOCK_EX) != SUCCESS) { |
|---|
| 114 | goto bailout; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | if (newfile) { |
|---|
| 118 | TRACE("%s", "new file"); |
|---|
| 119 | } |
|---|
| 120 | else if (outstat.st_size) { |
|---|
| 121 | len = outstat.st_size; |
|---|
| 122 | contents = emalloc(len); |
|---|
| 123 | if (read(fd, (void *) contents, len) != len) { |
|---|
| 124 | goto bailout; |
|---|
| 125 | } |
|---|
| 126 | TRACE("oldsize %d", (int) len); |
|---|
| 127 | do { |
|---|
| 128 | p = (long *) contents; |
|---|
| 129 | len -= sizeof(long); |
|---|
| 130 | if (len < 0) { |
|---|
| 131 | break; |
|---|
| 132 | } |
|---|
| 133 | if (*p++ != PCOV_HEADER_MAGIC) { |
|---|
| 134 | TRACE("wrong magic in file %s", outfilename); |
|---|
| 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 >= (int) 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] == -1) { |
|---|
| 147 | /* OPTIMIZE: already marked */ |
|---|
| 148 | continue; |
|---|
| 149 | } |
|---|
| 150 | if (*phits != -1) { |
|---|
| 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 | *p++ = *phits; |
|---|
| 173 | if (*phits > 0) { |
|---|
| 174 | covlines ++; |
|---|
| 175 | } |
|---|
| 176 | zend_hash_move_forward_ex(cov, &pos); |
|---|
| 177 | } |
|---|
| 178 | p = buf + 1; |
|---|
| 179 | p[0] = 0; |
|---|
| 180 | p[1] = covlines; |
|---|
| 181 | |
|---|
| 182 | if (ftruncate(fd, 0) != 0) { |
|---|
| 183 | goto bailout; |
|---|
| 184 | } |
|---|
| 185 | lseek(fd, 0, SEEK_SET); |
|---|
| 186 | if (write(fd, (char *) buf, size) != size) { |
|---|
| 187 | goto bailout; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | bailout: |
|---|
| 191 | if (contents) efree(contents); |
|---|
| 192 | if (fd >= 0) close(fd); |
|---|
| 193 | if (buf) efree(buf); |
|---|
| 194 | } |
|---|
| 195 | /* }}} */ |
|---|
| 196 | |
|---|
| 197 | static void xc_coverager_initenv(TSRMLS_D) /* {{{ */ |
|---|
| 198 | { |
|---|
| 199 | if (!XG(coverages)) { |
|---|
| 200 | XG(coverages) = emalloc(sizeof(HashTable)); |
|---|
| 201 | zend_hash_init(XG(coverages), 0, NULL, xc_destroy_coverage, 0); |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | /* }}} */ |
|---|
| 205 | static void xc_coverager_clean(TSRMLS_D) /* {{{ */ |
|---|
| 206 | { |
|---|
| 207 | if (XG(coverages)) { |
|---|
| 208 | HashPosition pos; |
|---|
| 209 | coverager_t *pcov; |
|---|
| 210 | |
|---|
| 211 | zend_hash_internal_pointer_reset_ex(XG(coverages), &pos); |
|---|
| 212 | while (zend_hash_get_current_data_ex(XG(coverages), (void **) &pcov, &pos) == SUCCESS) { |
|---|
| 213 | long *phits; |
|---|
| 214 | coverager_t cov; |
|---|
| 215 | HashPosition pos2; |
|---|
| 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_start(TSRMLS_D) /* {{{ */ |
|---|
| 246 | { |
|---|
| 247 | XG(coverager_started) = 1; |
|---|
| 248 | } |
|---|
| 249 | /* }}} */ |
|---|
| 250 | static void xc_coverager_stop(TSRMLS_D) /* {{{ */ |
|---|
| 251 | { |
|---|
| 252 | XG(coverager_started) = 0; |
|---|
| 253 | } |
|---|
| 254 | /* }}} */ |
|---|
| 255 | |
|---|
| 256 | static PHP_RINIT_FUNCTION(xcache_coverager) /* {{{ */ |
|---|
| 257 | { |
|---|
| 258 | if (XG(coverager)) { |
|---|
| 259 | if (XG(coverager_autostart)) { |
|---|
| 260 | xc_coverager_start(TSRMLS_C); |
|---|
| 261 | } |
|---|
| 262 | #ifdef ZEND_COMPILE_EXTENDED_INFO |
|---|
| 263 | CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO; |
|---|
| 264 | #else |
|---|
| 265 | CG(extended_info) = 1; |
|---|
| 266 | #endif |
|---|
| 267 | } |
|---|
| 268 | else { |
|---|
| 269 | XG(coverager_started) = 0; |
|---|
| 270 | } |
|---|
| 271 | return SUCCESS; |
|---|
| 272 | } |
|---|
| 273 | /* }}} */ |
|---|
| 274 | static void xc_coverager_autodump(TSRMLS_D) /* {{{ */ |
|---|
| 275 | { |
|---|
| 276 | coverager_t *pcov; |
|---|
| 277 | zstr s; |
|---|
| 278 | char *outfilename; |
|---|
| 279 | int dumpdir_len, outfilelen, alloc_len = 0; |
|---|
| 280 | uint size; |
|---|
| 281 | HashPosition pos; |
|---|
| 282 | |
|---|
| 283 | if (XG(coverages) && xc_coveragedump_dir) { |
|---|
| 284 | dumpdir_len = strlen(xc_coveragedump_dir); |
|---|
| 285 | alloc_len = dumpdir_len + 1 + 128; |
|---|
| 286 | outfilename = emalloc(alloc_len); |
|---|
| 287 | strcpy(outfilename, xc_coveragedump_dir); |
|---|
| 288 | |
|---|
| 289 | zend_hash_internal_pointer_reset_ex(XG(coverages), &pos); |
|---|
| 290 | while (zend_hash_get_current_data_ex(XG(coverages), (void **) &pcov, &pos) == SUCCESS) { |
|---|
| 291 | zend_hash_get_current_key_ex(XG(coverages), &s, &size, NULL, 0, &pos); |
|---|
| 292 | outfilelen = dumpdir_len + size + 5; |
|---|
| 293 | if (alloc_len < outfilelen) { |
|---|
| 294 | alloc_len = outfilelen + 128; |
|---|
| 295 | outfilename = erealloc(outfilename, alloc_len); |
|---|
| 296 | } |
|---|
| 297 | strcpy(outfilename + dumpdir_len, ZSTR_S(s)); |
|---|
| 298 | strcpy(outfilename + dumpdir_len + size - 1, ".pcov"); |
|---|
| 299 | |
|---|
| 300 | TRACE("outfilename %s", outfilename); |
|---|
| 301 | xc_coverager_save_cov(ZSTR_S(s), outfilename, *pcov TSRMLS_CC); |
|---|
| 302 | zend_hash_move_forward_ex(XG(coverages), &pos); |
|---|
| 303 | } |
|---|
| 304 | efree(outfilename); |
|---|
| 305 | } |
|---|
| 306 | } |
|---|
| 307 | /* }}} */ |
|---|
| 308 | static void xc_coverager_dump(zval *return_value TSRMLS_DC) /* {{{ */ |
|---|
| 309 | { |
|---|
| 310 | coverager_t *pcov; |
|---|
| 311 | HashPosition pos; |
|---|
| 312 | |
|---|
| 313 | if (XG(coverages)) { |
|---|
| 314 | array_init(return_value); |
|---|
| 315 | |
|---|
| 316 | zend_hash_internal_pointer_reset_ex(XG(coverages), &pos); |
|---|
| 317 | while (zend_hash_get_current_data_ex(XG(coverages), (void **) &pcov, &pos) == SUCCESS) { |
|---|
| 318 | zval *lines; |
|---|
| 319 | long *phits; |
|---|
| 320 | coverager_t cov; |
|---|
| 321 | HashPosition pos2; |
|---|
| 322 | zstr filename; |
|---|
| 323 | uint size; |
|---|
| 324 | |
|---|
| 325 | cov = *pcov; |
|---|
| 326 | zend_hash_get_current_key_ex(XG(coverages), &filename, &size, NULL, 0, &pos); |
|---|
| 327 | |
|---|
| 328 | MAKE_STD_ZVAL(lines); |
|---|
| 329 | array_init(lines); |
|---|
| 330 | zend_hash_internal_pointer_reset_ex(cov, &pos2); |
|---|
| 331 | while (zend_hash_get_current_data_ex(cov, (void**)&phits, &pos2) == SUCCESS) { |
|---|
| 332 | long hits = *phits; |
|---|
| 333 | add_index_long(lines, pos2->h, hits >= 0 ? hits : 0); |
|---|
| 334 | zend_hash_move_forward_ex(cov, &pos2); |
|---|
| 335 | } |
|---|
| 336 | add_assoc_zval_ex(return_value, ZSTR_S(filename), strlen(ZSTR_S(filename)) + 1, lines); |
|---|
| 337 | |
|---|
| 338 | zend_hash_move_forward_ex(XG(coverages), &pos); |
|---|
| 339 | } |
|---|
| 340 | } |
|---|
| 341 | else { |
|---|
| 342 | RETVAL_NULL(); |
|---|
| 343 | } |
|---|
| 344 | } |
|---|
| 345 | /* }}} */ |
|---|
| 346 | static PHP_RSHUTDOWN_FUNCTION(xcache_coverager) /* {{{ */ |
|---|
| 347 | { |
|---|
| 348 | if (XG(coverager)) { |
|---|
| 349 | xc_coverager_autodump(TSRMLS_C); |
|---|
| 350 | xc_coverager_cleanup(TSRMLS_C); |
|---|
| 351 | } |
|---|
| 352 | return SUCCESS; |
|---|
| 353 | } |
|---|
| 354 | /* }}} */ |
|---|
| 355 | |
|---|
| 356 | /* helper func to store hits into coverages */ |
|---|
| 357 | static coverager_t xc_coverager_get(const char *filename TSRMLS_DC) /* {{{ */ |
|---|
| 358 | { |
|---|
| 359 | int len = strlen(filename) + 1; |
|---|
| 360 | coverager_t cov, *pcov; |
|---|
| 361 | |
|---|
| 362 | if (zend_u_hash_find(XG(coverages), IS_STRING, filename, len, (void **) &pcov) == SUCCESS) { |
|---|
| 363 | TRACE("got coverage %s %p", filename, *pcov); |
|---|
| 364 | return *pcov; |
|---|
| 365 | } |
|---|
| 366 | else { |
|---|
| 367 | cov = emalloc(sizeof(HashTable)); |
|---|
| 368 | zend_hash_init(cov, 0, NULL, NULL, 0); |
|---|
| 369 | zend_u_hash_add(XG(coverages), IS_STRING, filename, len, (void **) &cov, sizeof(cov), NULL); |
|---|
| 370 | TRACE("new coverage %s %p", filename, cov); |
|---|
| 371 | return cov; |
|---|
| 372 | } |
|---|
| 373 | } |
|---|
| 374 | /* }}} */ |
|---|
| 375 | static void xc_coverager_add_hits(HashTable *cov, long line, long hits TSRMLS_DC) /* {{{ */ |
|---|
| 376 | { |
|---|
| 377 | long *poldhits; |
|---|
| 378 | |
|---|
| 379 | if (line == 0) { |
|---|
| 380 | return; |
|---|
| 381 | } |
|---|
| 382 | if (zend_hash_index_find(cov, line, (void**)&poldhits) == SUCCESS) { |
|---|
| 383 | if (hits == -1) { |
|---|
| 384 | /* OPTIMIZE: -1 == init-ing, but it's already initized */ |
|---|
| 385 | return; |
|---|
| 386 | } |
|---|
| 387 | if (*poldhits != -1) { |
|---|
| 388 | hits += *poldhits; |
|---|
| 389 | } |
|---|
| 390 | } |
|---|
| 391 | zend_hash_index_update(cov, line, &hits, sizeof(hits), NULL); |
|---|
| 392 | } |
|---|
| 393 | /* }}} */ |
|---|
| 394 | |
|---|
| 395 | static int xc_coverager_get_op_array_size_no_tail(zend_op_array *op_array) /* {{{ */ |
|---|
| 396 | { |
|---|
| 397 | zend_uint last = op_array->last; |
|---|
| 398 | do { |
|---|
| 399 | next_op: |
|---|
| 400 | if (last == 0) { |
|---|
| 401 | break; |
|---|
| 402 | } |
|---|
| 403 | switch (op_array->opcodes[last - 1].opcode) { |
|---|
| 404 | #ifdef ZEND_HANDLE_EXCEPTION |
|---|
| 405 | case ZEND_HANDLE_EXCEPTION: |
|---|
| 406 | #endif |
|---|
| 407 | case ZEND_RETURN: |
|---|
| 408 | case ZEND_EXT_STMT: |
|---|
| 409 | --last; |
|---|
| 410 | goto next_op; |
|---|
| 411 | } |
|---|
| 412 | } while (0); |
|---|
| 413 | return last; |
|---|
| 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 = old_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 | static void xc_coverager_handle_ext_stmt(zend_op_array *op_array, zend_uchar op) /* {{{ */ |
|---|
| 470 | { |
|---|
| 471 | TSRMLS_FETCH(); |
|---|
| 472 | |
|---|
| 473 | if (XG(coverages) && XG(coverager_started)) { |
|---|
| 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 | /* user api */ |
|---|
| 484 | /* {{{ proto array xcache_coverager_decode(string data) |
|---|
| 485 | * decode specified data which is saved by auto dumper to array |
|---|
| 486 | */ |
|---|
| 487 | PHP_FUNCTION(xcache_coverager_decode) |
|---|
| 488 | { |
|---|
| 489 | char *str; |
|---|
| 490 | int len; |
|---|
| 491 | long *p; |
|---|
| 492 | |
|---|
| 493 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len) == FAILURE) { |
|---|
| 494 | return; |
|---|
| 495 | } |
|---|
| 496 | |
|---|
| 497 | array_init(return_value); |
|---|
| 498 | |
|---|
| 499 | p = (long*) str; |
|---|
| 500 | len -= sizeof(long); |
|---|
| 501 | if (len < 0) { |
|---|
| 502 | return; |
|---|
| 503 | } |
|---|
| 504 | if (*p++ != PCOV_HEADER_MAGIC) { |
|---|
| 505 | TRACE("%s", "wrong magic in xcache_coverager_decode"); |
|---|
| 506 | return; |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | for (; len >= (int) sizeof(long) * 2; len -= sizeof(long) * 2, p += 2) { |
|---|
| 510 | add_index_long(return_value, p[0], p[1] < 0 ? 0 : p[1]); |
|---|
| 511 | } |
|---|
| 512 | } |
|---|
| 513 | /* }}} */ |
|---|
| 514 | /* {{{ proto void xcache_coverager_start([bool clean = true]) |
|---|
| 515 | * starts coverager data collecting |
|---|
| 516 | */ |
|---|
| 517 | PHP_FUNCTION(xcache_coverager_start) |
|---|
| 518 | { |
|---|
| 519 | zend_bool clean = 1; |
|---|
| 520 | |
|---|
| 521 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) { |
|---|
| 522 | return; |
|---|
| 523 | } |
|---|
| 524 | |
|---|
| 525 | if (clean) { |
|---|
| 526 | xc_coverager_clean(TSRMLS_C); |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | if (XG(coverager)) { |
|---|
| 530 | xc_coverager_start(TSRMLS_C); |
|---|
| 531 | } |
|---|
| 532 | else { |
|---|
| 533 | php_error(E_WARNING, "You can only start coverager after you set 'xcache.coverager' to 'On' in ini"); |
|---|
| 534 | } |
|---|
| 535 | } |
|---|
| 536 | /* }}} */ |
|---|
| 537 | /* {{{ proto void xcache_coverager_stop([bool clean = false]) |
|---|
| 538 | * stop coverager data collecting |
|---|
| 539 | */ |
|---|
| 540 | PHP_FUNCTION(xcache_coverager_stop) |
|---|
| 541 | { |
|---|
| 542 | zend_bool clean = 0; |
|---|
| 543 | |
|---|
| 544 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) { |
|---|
| 545 | return; |
|---|
| 546 | } |
|---|
| 547 | |
|---|
| 548 | xc_coverager_stop(TSRMLS_C); |
|---|
| 549 | if (clean) { |
|---|
| 550 | xc_coverager_clean(TSRMLS_C); |
|---|
| 551 | } |
|---|
| 552 | } |
|---|
| 553 | /* }}} */ |
|---|
| 554 | /* {{{ proto array xcache_coverager_get([bool clean = false]) |
|---|
| 555 | * get coverager data collected |
|---|
| 556 | */ |
|---|
| 557 | PHP_FUNCTION(xcache_coverager_get) |
|---|
| 558 | { |
|---|
| 559 | zend_bool clean = 0; |
|---|
| 560 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) { |
|---|
| 561 | return; |
|---|
| 562 | } |
|---|
| 563 | |
|---|
| 564 | xc_coverager_dump(return_value TSRMLS_CC); |
|---|
| 565 | if (clean) { |
|---|
| 566 | xc_coverager_clean(TSRMLS_C); |
|---|
| 567 | } |
|---|
| 568 | } |
|---|
| 569 | /* }}} */ |
|---|
| 570 | static zend_function_entry xcache_coverager_functions[] = /* {{{ */ |
|---|
| 571 | { |
|---|
| 572 | PHP_FE(xcache_coverager_decode, NULL) |
|---|
| 573 | PHP_FE(xcache_coverager_start, NULL) |
|---|
| 574 | PHP_FE(xcache_coverager_stop, NULL) |
|---|
| 575 | PHP_FE(xcache_coverager_get, NULL) |
|---|
| 576 | PHP_FE_END |
|---|
| 577 | }; |
|---|
| 578 | /* }}} */ |
|---|
| 579 | |
|---|
| 580 | static int xc_coverager_zend_startup(zend_extension *extension) /* {{{ */ |
|---|
| 581 | { |
|---|
| 582 | old_compile_file = zend_compile_file; |
|---|
| 583 | zend_compile_file = xc_compile_file_for_coverage; |
|---|
| 584 | |
|---|
| 585 | return SUCCESS; |
|---|
| 586 | } |
|---|
| 587 | /* }}} */ |
|---|
| 588 | static void xc_coverager_zend_shutdown(zend_extension *extension) /* {{{ */ |
|---|
| 589 | { |
|---|
| 590 | /* empty */ |
|---|
| 591 | } |
|---|
| 592 | /* }}} */ |
|---|
| 593 | static void xc_statement_handler(zend_op_array *op_array) /* {{{ */ |
|---|
| 594 | { |
|---|
| 595 | xc_coverager_handle_ext_stmt(op_array, ZEND_EXT_STMT); |
|---|
| 596 | } |
|---|
| 597 | /* }}} */ |
|---|
| 598 | static void xc_fcall_begin_handler(zend_op_array *op_array) /* {{{ */ |
|---|
| 599 | { |
|---|
| 600 | #if 0 |
|---|
| 601 | xc_coverager_handle_ext_stmt(op_array, ZEND_EXT_FCALL_BEGIN); |
|---|
| 602 | #endif |
|---|
| 603 | } |
|---|
| 604 | /* }}} */ |
|---|
| 605 | static void xc_fcall_end_handler(zend_op_array *op_array) /* {{{ */ |
|---|
| 606 | { |
|---|
| 607 | #if 0 |
|---|
| 608 | xc_coverager_handle_ext_stmt(op_array, ZEND_EXT_FCALL_END); |
|---|
| 609 | #endif |
|---|
| 610 | } |
|---|
| 611 | /* }}} */ |
|---|
| 612 | /* {{{ zend extension definition structure */ |
|---|
| 613 | static zend_extension xc_coverager_zend_extension_entry = { |
|---|
| 614 | XCACHE_NAME " Coverager", |
|---|
| 615 | XCACHE_VERSION, |
|---|
| 616 | XCACHE_AUTHOR, |
|---|
| 617 | XCACHE_URL, |
|---|
| 618 | XCACHE_COPYRIGHT, |
|---|
| 619 | xc_coverager_zend_startup, |
|---|
| 620 | xc_coverager_zend_shutdown, |
|---|
| 621 | NULL, /* activate_func_t */ |
|---|
| 622 | NULL, /* deactivate_func_t */ |
|---|
| 623 | NULL, /* message_handler_func_t */ |
|---|
| 624 | NULL, /* statement_handler_func_t */ |
|---|
| 625 | xc_statement_handler, |
|---|
| 626 | xc_fcall_begin_handler, |
|---|
| 627 | xc_fcall_end_handler, |
|---|
| 628 | NULL, /* op_array_ctor_func_t */ |
|---|
| 629 | NULL, /* op_array_dtor_func_t */ |
|---|
| 630 | STANDARD_ZEND_EXTENSION_PROPERTIES |
|---|
| 631 | }; |
|---|
| 632 | /* }}} */ |
|---|
| 633 | /* {{{ PHP_INI */ |
|---|
| 634 | PHP_INI_BEGIN() |
|---|
| 635 | STD_PHP_INI_BOOLEAN("xcache.coverager", "0", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateBool, coverager, zend_xcache_globals, xcache_globals) |
|---|
| 636 | STD_PHP_INI_BOOLEAN("xcache.coverager_autostart", "1", PHP_INI_SYSTEM|PHP_INI_PERDIR, OnUpdateBool, coverager_autostart, zend_xcache_globals, xcache_globals) |
|---|
| 637 | PHP_INI_ENTRY1 ("xcache.coveragedump_directory", "", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL) |
|---|
| 638 | PHP_INI_END() |
|---|
| 639 | /* }}} */ |
|---|
| 640 | static PHP_MINFO_FUNCTION(xcache_coverager) /* {{{ */ |
|---|
| 641 | { |
|---|
| 642 | char *covdumpdir; |
|---|
| 643 | |
|---|
| 644 | php_info_print_table_start(); |
|---|
| 645 | php_info_print_table_row(2, "XCache Coverager Module", "enabled"); |
|---|
| 646 | if (cfg_get_string("xcache.coveragedump_directory", &covdumpdir) != SUCCESS || !covdumpdir[0]) { |
|---|
| 647 | covdumpdir = NULL; |
|---|
| 648 | } |
|---|
| 649 | php_info_print_table_row(2, "Coverage Started", XG(coverager_started) && covdumpdir ? "On" : "Off"); |
|---|
| 650 | php_info_print_table_end(); |
|---|
| 651 | |
|---|
| 652 | DISPLAY_INI_ENTRIES(); |
|---|
| 653 | } |
|---|
| 654 | /* }}} */ |
|---|
| 655 | static PHP_MINIT_FUNCTION(xcache_coverager) /* {{{ */ |
|---|
| 656 | { |
|---|
| 657 | REGISTER_INI_ENTRIES(); |
|---|
| 658 | |
|---|
| 659 | if (cfg_get_string("xcache.coveragedump_directory", &xc_coveragedump_dir) == SUCCESS && xc_coveragedump_dir) { |
|---|
| 660 | int len; |
|---|
| 661 | xc_coveragedump_dir = pestrdup(xc_coveragedump_dir, 1); |
|---|
| 662 | len = strlen(xc_coveragedump_dir); |
|---|
| 663 | if (len) { |
|---|
| 664 | if (xc_coveragedump_dir[len - 1] == '/') { |
|---|
| 665 | xc_coveragedump_dir[len - 1] = '\0'; |
|---|
| 666 | } |
|---|
| 667 | } |
|---|
| 668 | if (!strlen(xc_coveragedump_dir)) { |
|---|
| 669 | pefree(xc_coveragedump_dir, 1); |
|---|
| 670 | xc_coveragedump_dir = NULL; |
|---|
| 671 | } |
|---|
| 672 | } |
|---|
| 673 | |
|---|
| 674 | return xcache_zend_extension_add(&xc_coverager_zend_extension_entry, 0); |
|---|
| 675 | } |
|---|
| 676 | /* }}} */ |
|---|
| 677 | static PHP_MSHUTDOWN_FUNCTION(xcache_coverager) /* {{{ */ |
|---|
| 678 | { |
|---|
| 679 | if (old_compile_file && zend_compile_file == xc_compile_file_for_coverage) { |
|---|
| 680 | zend_compile_file = old_compile_file; |
|---|
| 681 | old_compile_file = NULL; |
|---|
| 682 | } |
|---|
| 683 | if (xc_coveragedump_dir) { |
|---|
| 684 | pefree(xc_coveragedump_dir, 1); |
|---|
| 685 | xc_coveragedump_dir = NULL; |
|---|
| 686 | } |
|---|
| 687 | UNREGISTER_INI_ENTRIES(); |
|---|
| 688 | return xcache_zend_extension_remove(&xc_coverager_zend_extension_entry); |
|---|
| 689 | } |
|---|
| 690 | /* }}} */ |
|---|
| 691 | |
|---|
| 692 | static zend_module_entry xcache_coverager_module_entry = { /* {{{ */ |
|---|
| 693 | STANDARD_MODULE_HEADER, |
|---|
| 694 | XCACHE_NAME " Coverager", |
|---|
| 695 | xcache_coverager_functions, |
|---|
| 696 | PHP_MINIT(xcache_coverager), |
|---|
| 697 | PHP_MSHUTDOWN(xcache_coverager), |
|---|
| 698 | PHP_RINIT(xcache_coverager), |
|---|
| 699 | PHP_RSHUTDOWN(xcache_coverager), |
|---|
| 700 | PHP_MINFO(xcache_coverager), |
|---|
| 701 | XCACHE_VERSION, |
|---|
| 702 | #ifdef PHP_GINIT |
|---|
| 703 | NO_MODULE_GLOBALS, |
|---|
| 704 | #endif |
|---|
| 705 | #ifdef ZEND_ENGINE_2 |
|---|
| 706 | NULL, |
|---|
| 707 | #else |
|---|
| 708 | NULL, |
|---|
| 709 | NULL, |
|---|
| 710 | #endif |
|---|
| 711 | STANDARD_MODULE_PROPERTIES_EX |
|---|
| 712 | }; |
|---|
| 713 | /* }}} */ |
|---|
| 714 | int xc_coverager_startup_module() /* {{{ */ |
|---|
| 715 | { |
|---|
| 716 | return zend_startup_module(&xcache_coverager_module_entry); |
|---|
| 717 | } |
|---|
| 718 | /* }}} */ |
|---|