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