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