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_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 | static PHP_RINIT_FUNCTION(xcache_coverager) /* {{{ */ |
---|
257 | { |
---|
258 | if (XG(coverager)) { |
---|
259 | xc_coverager_enable(TSRMLS_C); |
---|
260 | #ifdef ZEND_COMPILE_EXTENDED_INFO |
---|
261 | CG(compiler_options) |= ZEND_COMPILE_EXTENDED_INFO; |
---|
262 | #else |
---|
263 | CG(extended_info) = 1; |
---|
264 | #endif |
---|
265 | } |
---|
266 | else { |
---|
267 | XG(coverage_enabled) = 0; |
---|
268 | } |
---|
269 | return SUCCESS; |
---|
270 | } |
---|
271 | /* }}} */ |
---|
272 | static void xc_coverager_autodump(TSRMLS_D) /* {{{ */ |
---|
273 | { |
---|
274 | coverager_t *pcov; |
---|
275 | zstr s; |
---|
276 | char *outfilename; |
---|
277 | int dumpdir_len, outfilelen, alloc_len = 0; |
---|
278 | uint size; |
---|
279 | HashPosition pos; |
---|
280 | |
---|
281 | if (XG(coverages) && xc_coveragedump_dir) { |
---|
282 | dumpdir_len = strlen(xc_coveragedump_dir); |
---|
283 | alloc_len = dumpdir_len + 1 + 128; |
---|
284 | outfilename = emalloc(alloc_len); |
---|
285 | strcpy(outfilename, xc_coveragedump_dir); |
---|
286 | |
---|
287 | zend_hash_internal_pointer_reset_ex(XG(coverages), &pos); |
---|
288 | while (zend_hash_get_current_data_ex(XG(coverages), (void **) &pcov, &pos) == SUCCESS) { |
---|
289 | zend_hash_get_current_key_ex(XG(coverages), &s, &size, NULL, 0, &pos); |
---|
290 | outfilelen = dumpdir_len + size + 5; |
---|
291 | if (alloc_len < outfilelen) { |
---|
292 | alloc_len = outfilelen + 128; |
---|
293 | outfilename = erealloc(outfilename, alloc_len); |
---|
294 | } |
---|
295 | strcpy(outfilename + dumpdir_len, ZSTR_S(s)); |
---|
296 | strcpy(outfilename + dumpdir_len + size - 1, ".pcov"); |
---|
297 | |
---|
298 | TRACE("outfilename %s", outfilename); |
---|
299 | xc_coverager_save_cov(ZSTR_S(s), outfilename, *pcov TSRMLS_CC); |
---|
300 | zend_hash_move_forward_ex(XG(coverages), &pos); |
---|
301 | } |
---|
302 | efree(outfilename); |
---|
303 | } |
---|
304 | } |
---|
305 | /* }}} */ |
---|
306 | static void xc_coverager_dump(zval *return_value TSRMLS_DC) /* {{{ */ |
---|
307 | { |
---|
308 | coverager_t *pcov; |
---|
309 | HashPosition pos; |
---|
310 | |
---|
311 | if (XG(coverages)) { |
---|
312 | array_init(return_value); |
---|
313 | |
---|
314 | zend_hash_internal_pointer_reset_ex(XG(coverages), &pos); |
---|
315 | while (zend_hash_get_current_data_ex(XG(coverages), (void **) &pcov, &pos) == SUCCESS) { |
---|
316 | zval *lines; |
---|
317 | long *phits; |
---|
318 | coverager_t cov; |
---|
319 | HashPosition pos2; |
---|
320 | zstr filename; |
---|
321 | uint size; |
---|
322 | |
---|
323 | cov = *pcov; |
---|
324 | zend_hash_get_current_key_ex(XG(coverages), &filename, &size, NULL, 0, &pos); |
---|
325 | |
---|
326 | MAKE_STD_ZVAL(lines); |
---|
327 | array_init(lines); |
---|
328 | zend_hash_internal_pointer_reset_ex(cov, &pos2); |
---|
329 | while (zend_hash_get_current_data_ex(cov, (void**)&phits, &pos2) == SUCCESS) { |
---|
330 | long hits = *phits; |
---|
331 | add_index_long(lines, pos2->h, hits >= 0 ? hits : 0); |
---|
332 | zend_hash_move_forward_ex(cov, &pos2); |
---|
333 | } |
---|
334 | add_assoc_zval_ex(return_value, ZSTR_S(filename), strlen(ZSTR_S(filename)) + 1, lines); |
---|
335 | |
---|
336 | zend_hash_move_forward_ex(XG(coverages), &pos); |
---|
337 | } |
---|
338 | } |
---|
339 | else { |
---|
340 | RETVAL_NULL(); |
---|
341 | } |
---|
342 | } |
---|
343 | /* }}} */ |
---|
344 | static PHP_RSHUTDOWN_FUNCTION(xcache_coverager) /* {{{ */ |
---|
345 | { |
---|
346 | if (XG(coverager)) { |
---|
347 | xc_coverager_autodump(TSRMLS_C); |
---|
348 | xc_coverager_cleanup(TSRMLS_C); |
---|
349 | } |
---|
350 | return SUCCESS; |
---|
351 | } |
---|
352 | /* }}} */ |
---|
353 | |
---|
354 | /* helper func to store hits into coverages */ |
---|
355 | static coverager_t xc_coverager_get(const char *filename TSRMLS_DC) /* {{{ */ |
---|
356 | { |
---|
357 | int len = strlen(filename) + 1; |
---|
358 | coverager_t cov, *pcov; |
---|
359 | |
---|
360 | if (zend_u_hash_find(XG(coverages), IS_STRING, filename, len, (void **) &pcov) == SUCCESS) { |
---|
361 | TRACE("got coverage %s %p", filename, *pcov); |
---|
362 | return *pcov; |
---|
363 | } |
---|
364 | else { |
---|
365 | cov = emalloc(sizeof(HashTable)); |
---|
366 | zend_hash_init(cov, 0, NULL, NULL, 0); |
---|
367 | zend_u_hash_add(XG(coverages), IS_STRING, filename, len, (void **) &cov, sizeof(cov), NULL); |
---|
368 | TRACE("new coverage %s %p", filename, cov); |
---|
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 last = op_array->last; |
---|
396 | do { |
---|
397 | next_op: |
---|
398 | if (last == 0) { |
---|
399 | break; |
---|
400 | } |
---|
401 | switch (op_array->opcodes[last - 1].opcode) { |
---|
402 | #ifdef ZEND_HANDLE_EXCEPTION |
---|
403 | case ZEND_HANDLE_EXCEPTION: |
---|
404 | #endif |
---|
405 | case ZEND_RETURN: |
---|
406 | case ZEND_EXT_STMT: |
---|
407 | --last; |
---|
408 | goto next_op; |
---|
409 | } |
---|
410 | } while (0); |
---|
411 | return last; |
---|
412 | } |
---|
413 | /* }}} */ |
---|
414 | |
---|
415 | /* prefill */ |
---|
416 | static int xc_coverager_init_op_array(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
417 | { |
---|
418 | zend_uint size; |
---|
419 | coverager_t cov; |
---|
420 | zend_uint i; |
---|
421 | |
---|
422 | if (op_array->type != ZEND_USER_FUNCTION) { |
---|
423 | return 0; |
---|
424 | } |
---|
425 | |
---|
426 | size = xc_coverager_get_op_array_size_no_tail(op_array); |
---|
427 | cov = xc_coverager_get(op_array->filename TSRMLS_CC); |
---|
428 | for (i = 0; i < size; i ++) { |
---|
429 | switch (op_array->opcodes[i].opcode) { |
---|
430 | case ZEND_EXT_STMT: |
---|
431 | #if 0 |
---|
432 | case ZEND_EXT_FCALL_BEGIN: |
---|
433 | case ZEND_EXT_FCALL_END: |
---|
434 | #endif |
---|
435 | xc_coverager_add_hits(cov, op_array->opcodes[i].lineno, -1 TSRMLS_CC); |
---|
436 | break; |
---|
437 | } |
---|
438 | } |
---|
439 | return 0; |
---|
440 | } |
---|
441 | /* }}} */ |
---|
442 | static void xc_coverager_init_compile_result(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
443 | { |
---|
444 | xc_compile_result_t cr; |
---|
445 | |
---|
446 | xc_compile_result_init_cur(&cr, op_array TSRMLS_CC); |
---|
447 | xc_apply_op_array(&cr, (apply_func_t) xc_coverager_init_op_array TSRMLS_CC); |
---|
448 | xc_compile_result_free(&cr); |
---|
449 | } |
---|
450 | /* }}} */ |
---|
451 | static zend_op_array *xc_compile_file_for_coverage(zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
---|
452 | { |
---|
453 | zend_op_array *op_array; |
---|
454 | |
---|
455 | op_array = old_compile_file(h, type TSRMLS_CC); |
---|
456 | if (op_array) { |
---|
457 | if (XG(coverager)) { |
---|
458 | xc_coverager_initenv(TSRMLS_C); |
---|
459 | xc_coverager_init_compile_result(op_array TSRMLS_CC); |
---|
460 | } |
---|
461 | } |
---|
462 | return op_array; |
---|
463 | } |
---|
464 | /* }}} */ |
---|
465 | |
---|
466 | /* hits */ |
---|
467 | static void xc_coverager_handle_ext_stmt(zend_op_array *op_array, zend_uchar op) /* {{{ */ |
---|
468 | { |
---|
469 | TSRMLS_FETCH(); |
---|
470 | |
---|
471 | if (XG(coverages) && XG(coverage_enabled)) { |
---|
472 | int size = xc_coverager_get_op_array_size_no_tail(op_array); |
---|
473 | int oplineno = (*EG(opline_ptr)) - op_array->opcodes; |
---|
474 | if (oplineno < size) { |
---|
475 | xc_coverager_add_hits(xc_coverager_get(op_array->filename TSRMLS_CC), (*EG(opline_ptr))->lineno, 1 TSRMLS_CC); |
---|
476 | } |
---|
477 | } |
---|
478 | } |
---|
479 | /* }}} */ |
---|
480 | |
---|
481 | /* user api */ |
---|
482 | /* {{{ proto array xcache_coverager_decode(string data) |
---|
483 | * decode specified data which is saved by auto dumper to array |
---|
484 | */ |
---|
485 | PHP_FUNCTION(xcache_coverager_decode) |
---|
486 | { |
---|
487 | char *str; |
---|
488 | int len; |
---|
489 | long *p; |
---|
490 | |
---|
491 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len) == FAILURE) { |
---|
492 | return; |
---|
493 | } |
---|
494 | |
---|
495 | array_init(return_value); |
---|
496 | |
---|
497 | p = (long*) str; |
---|
498 | len -= sizeof(long); |
---|
499 | if (len < 0) { |
---|
500 | return; |
---|
501 | } |
---|
502 | if (*p++ != PCOV_HEADER_MAGIC) { |
---|
503 | TRACE("%s", "wrong magic in xcache_coverager_decode"); |
---|
504 | return; |
---|
505 | } |
---|
506 | |
---|
507 | for (; len >= (int) sizeof(long) * 2; len -= sizeof(long) * 2, p += 2) { |
---|
508 | add_index_long(return_value, p[0], p[1] < 0 ? 0 : p[1]); |
---|
509 | } |
---|
510 | } |
---|
511 | /* }}} */ |
---|
512 | /* {{{ proto void xcache_coverager_start([bool clean = true]) |
---|
513 | * starts coverager data collecting |
---|
514 | */ |
---|
515 | PHP_FUNCTION(xcache_coverager_start) |
---|
516 | { |
---|
517 | zend_bool clean = 1; |
---|
518 | |
---|
519 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) { |
---|
520 | return; |
---|
521 | } |
---|
522 | |
---|
523 | if (clean) { |
---|
524 | xc_coverager_clean(TSRMLS_C); |
---|
525 | } |
---|
526 | |
---|
527 | if (XG(coverager)) { |
---|
528 | xc_coverager_enable(TSRMLS_C); |
---|
529 | } |
---|
530 | else { |
---|
531 | php_error(E_WARNING, "You can only start coverager after you set 'xcache.coverager' to 'On' in ini"); |
---|
532 | } |
---|
533 | } |
---|
534 | /* }}} */ |
---|
535 | /* {{{ proto void xcache_coverager_stop([bool clean = false]) |
---|
536 | * stop coverager data collecting |
---|
537 | */ |
---|
538 | PHP_FUNCTION(xcache_coverager_stop) |
---|
539 | { |
---|
540 | zend_bool clean = 0; |
---|
541 | |
---|
542 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) { |
---|
543 | return; |
---|
544 | } |
---|
545 | |
---|
546 | xc_coverager_disable(TSRMLS_C); |
---|
547 | if (clean) { |
---|
548 | xc_coverager_clean(TSRMLS_C); |
---|
549 | } |
---|
550 | } |
---|
551 | /* }}} */ |
---|
552 | /* {{{ proto array xcache_coverager_get([bool clean = false]) |
---|
553 | * get coverager data collected |
---|
554 | */ |
---|
555 | PHP_FUNCTION(xcache_coverager_get) |
---|
556 | { |
---|
557 | zend_bool clean = 0; |
---|
558 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean) == FAILURE) { |
---|
559 | return; |
---|
560 | } |
---|
561 | |
---|
562 | xc_coverager_dump(return_value TSRMLS_CC); |
---|
563 | if (clean) { |
---|
564 | xc_coverager_clean(TSRMLS_C); |
---|
565 | } |
---|
566 | } |
---|
567 | /* }}} */ |
---|
568 | static zend_function_entry xcache_coverager_functions[] = /* {{{ */ |
---|
569 | { |
---|
570 | PHP_FE(xcache_coverager_decode, NULL) |
---|
571 | PHP_FE(xcache_coverager_start, NULL) |
---|
572 | PHP_FE(xcache_coverager_stop, NULL) |
---|
573 | PHP_FE(xcache_coverager_get, NULL) |
---|
574 | PHP_FE_END |
---|
575 | }; |
---|
576 | /* }}} */ |
---|
577 | |
---|
578 | static int xc_optimizer_zend_startup(zend_extension *extension) /* {{{ */ |
---|
579 | { |
---|
580 | return SUCCESS; |
---|
581 | } |
---|
582 | /* }}} */ |
---|
583 | static void xc_optimizer_zend_shutdown(zend_extension *extension) /* {{{ */ |
---|
584 | { |
---|
585 | /* empty */ |
---|
586 | } |
---|
587 | /* }}} */ |
---|
588 | static void xc_statement_handler(zend_op_array *op_array) /* {{{ */ |
---|
589 | { |
---|
590 | #ifdef HAVE_XCACHE_COVERAGER |
---|
591 | xc_coverager_handle_ext_stmt(op_array, ZEND_EXT_STMT); |
---|
592 | #endif |
---|
593 | } |
---|
594 | /* }}} */ |
---|
595 | static void xc_fcall_begin_handler(zend_op_array *op_array) /* {{{ */ |
---|
596 | { |
---|
597 | #if 0 |
---|
598 | xc_coverager_handle_ext_stmt(op_array, ZEND_EXT_FCALL_BEGIN); |
---|
599 | #endif |
---|
600 | } |
---|
601 | /* }}} */ |
---|
602 | static void xc_fcall_end_handler(zend_op_array *op_array) /* {{{ */ |
---|
603 | { |
---|
604 | #if 0 |
---|
605 | xc_coverager_handle_ext_stmt(op_array, ZEND_EXT_FCALL_END); |
---|
606 | #endif |
---|
607 | } |
---|
608 | /* }}} */ |
---|
609 | /* {{{ zend extension definition structure */ |
---|
610 | static zend_extension xc_coverager_zend_extension_entry = { |
---|
611 | XCACHE_NAME " Coverager", |
---|
612 | XCACHE_VERSION, |
---|
613 | XCACHE_AUTHOR, |
---|
614 | XCACHE_URL, |
---|
615 | XCACHE_COPYRIGHT, |
---|
616 | xc_optimizer_zend_startup, |
---|
617 | xc_optimizer_zend_shutdown, |
---|
618 | NULL, /* activate_func_t */ |
---|
619 | NULL, /* deactivate_func_t */ |
---|
620 | NULL, /* message_handler_func_t */ |
---|
621 | NULL, /* statement_handler_func_t */ |
---|
622 | xc_statement_handler, |
---|
623 | xc_fcall_begin_handler, |
---|
624 | xc_fcall_end_handler, |
---|
625 | NULL, /* op_array_ctor_func_t */ |
---|
626 | NULL, /* op_array_dtor_func_t */ |
---|
627 | STANDARD_ZEND_EXTENSION_PROPERTIES |
---|
628 | }; |
---|
629 | /* }}} */ |
---|
630 | /* {{{ PHP_INI */ |
---|
631 | PHP_INI_BEGIN() |
---|
632 | STD_PHP_INI_BOOLEAN("xcache.coverager" , "0", PHP_INI_ALL, OnUpdateBool, coverager, zend_xcache_globals, xcache_globals) |
---|
633 | PHP_INI_ENTRY1 ("xcache.coveragedump_directory", "", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL) |
---|
634 | PHP_INI_END() |
---|
635 | /* }}} */ |
---|
636 | static PHP_MINFO_FUNCTION(xcache_coverager) /* {{{ */ |
---|
637 | { |
---|
638 | char *covdumpdir; |
---|
639 | |
---|
640 | php_info_print_table_start(); |
---|
641 | php_info_print_table_row(2, "XCache Coverager Module", "enabled"); |
---|
642 | if (cfg_get_string("xcache.coveragedump_directory", &covdumpdir) != SUCCESS || !covdumpdir[0]) { |
---|
643 | covdumpdir = NULL; |
---|
644 | } |
---|
645 | php_info_print_table_row(2, "Coverage Auto Dumper", XG(coverager) && covdumpdir ? "enabled" : "disabled"); |
---|
646 | php_info_print_table_end(); |
---|
647 | |
---|
648 | DISPLAY_INI_ENTRIES(); |
---|
649 | } |
---|
650 | /* }}} */ |
---|
651 | static PHP_MINIT_FUNCTION(xcache_coverager) /* {{{ */ |
---|
652 | { |
---|
653 | old_compile_file = zend_compile_file; |
---|
654 | zend_compile_file = xc_compile_file_for_coverage; |
---|
655 | |
---|
656 | REGISTER_INI_ENTRIES(); |
---|
657 | |
---|
658 | if (cfg_get_string("xcache.coveragedump_directory", &xc_coveragedump_dir) == SUCCESS && xc_coveragedump_dir) { |
---|
659 | int len; |
---|
660 | xc_coveragedump_dir = pestrdup(xc_coveragedump_dir, 1); |
---|
661 | len = strlen(xc_coveragedump_dir); |
---|
662 | if (len) { |
---|
663 | if (xc_coveragedump_dir[len - 1] == '/') { |
---|
664 | xc_coveragedump_dir[len - 1] = '\0'; |
---|
665 | } |
---|
666 | } |
---|
667 | if (!strlen(xc_coveragedump_dir)) { |
---|
668 | pefree(xc_coveragedump_dir, 1); |
---|
669 | xc_coveragedump_dir = NULL; |
---|
670 | } |
---|
671 | } |
---|
672 | |
---|
673 | return xcache_zend_extension_add(&xc_coverager_zend_extension_entry, 0); |
---|
674 | } |
---|
675 | /* }}} */ |
---|
676 | static PHP_MSHUTDOWN_FUNCTION(xcache_coverager) /* {{{ */ |
---|
677 | { |
---|
678 | if (old_compile_file && zend_compile_file == xc_compile_file_for_coverage) { |
---|
679 | zend_compile_file = old_compile_file; |
---|
680 | old_compile_file = NULL; |
---|
681 | } |
---|
682 | if (xc_coveragedump_dir) { |
---|
683 | pefree(xc_coveragedump_dir, 1); |
---|
684 | xc_coveragedump_dir = NULL; |
---|
685 | } |
---|
686 | UNREGISTER_INI_ENTRIES(); |
---|
687 | return xcache_zend_extension_remove(&xc_coverager_zend_extension_entry); |
---|
688 | } |
---|
689 | /* }}} */ |
---|
690 | |
---|
691 | static zend_module_entry xcache_coverager_module_entry = { /* {{{ */ |
---|
692 | STANDARD_MODULE_HEADER, |
---|
693 | XCACHE_NAME " Coverager", |
---|
694 | xcache_coverager_functions, |
---|
695 | PHP_MINIT(xcache_coverager), |
---|
696 | PHP_MSHUTDOWN(xcache_coverager), |
---|
697 | PHP_RINIT(xcache_coverager), |
---|
698 | PHP_RSHUTDOWN(xcache_coverager), |
---|
699 | PHP_MINFO(xcache_coverager), |
---|
700 | XCACHE_VERSION, |
---|
701 | #ifdef PHP_GINIT |
---|
702 | NO_MODULE_GLOBALS, |
---|
703 | #endif |
---|
704 | #ifdef ZEND_ENGINE_2 |
---|
705 | NULL, |
---|
706 | #else |
---|
707 | NULL, |
---|
708 | NULL, |
---|
709 | #endif |
---|
710 | STANDARD_MODULE_PROPERTIES_EX |
---|
711 | }; |
---|
712 | /* }}} */ |
---|
713 | int xc_coverager_startup_module() /* {{{ */ |
---|
714 | { |
---|
715 | return zend_startup_module(&xcache_coverager_module_entry); |
---|
716 | } |
---|
717 | /* }}} */ |
---|