Index: /trunk/coverager.c
===================================================================
--- /trunk/coverager.c	(revision 203)
+++ /trunk/coverager.c	(revision 204)
@@ -16,5 +16,5 @@
 #define PCOV_HEADER_MAGIC 0x564f4350
 
-char *xc_coveragedump_dir = NULL;
+static char *xc_coveragedump_dir = NULL;
 static zend_compile_file_t *origin_compile_file;
 
@@ -193,7 +193,8 @@
 }
 /* }}} */
-void xc_coverager_request_init(TSRMLS_D) /* {{{ */
-{
-	if (XG(coveragedumper)) {
+
+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);
@@ -201,5 +202,38 @@
 }
 /* }}} */
-void xc_coverager_request_shutdown(TSRMLS_D) /* {{{ */
+static void xc_coverager_clean(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(coveragedumper)) {
+		xc_coverager_enable(TSRMLS_C);
+		xc_coverager_initenv(TSRMLS_C);
+		CG(extended_info) = 1;
+	}
+	else {
+		XG(coverage_enabled) = 0;
+	}
+}
+/* }}} */
+static void xc_coverager_autodump(TSRMLS_D) /* {{{ */
 {
 	coverager_t *pcov;
@@ -208,9 +242,7 @@
 	int dumpdir_len, outfilelen, alloc_len = 0;
 	uint size;
-
-	if (!XG(coverages)) {
-		return;
-	}
-	if (XG(coveragedumper)) {	
+	HashPosition pos;
+
+	if (XG(coverages) && xc_coveragedump_dir) {	
 		dumpdir_len = strlen(xc_coveragedump_dir);
 		alloc_len = dumpdir_len + 1 + 128;
@@ -218,7 +250,7 @@
 		strcpy(outfilename, xc_coveragedump_dir);
 
-		zend_hash_internal_pointer_reset(XG(coverages));
-		while (zend_hash_get_current_data(XG(coverages), (void **) &pcov) == SUCCESS) {
-			zend_hash_get_current_key_ex(XG(coverages), &s, &size, NULL, 0, NULL);
+		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) {
@@ -233,12 +265,54 @@
 #endif
 			xc_coverager_save_cov(ZSTR_S(s), outfilename, *pcov TSRMLS_CC);
-			zend_hash_move_forward(XG(coverages));
+			zend_hash_move_forward_ex(XG(coverages), &pos);
 		}
 		efree(outfilename);
 	}
-
-	zend_hash_destroy(XG(coverages));
-	efree(XG(coverages));
-	XG(coverages) = NULL;
+}
+/* }}} */
+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(coveragedumper)) {
+		xc_coverager_autodump(TSRMLS_C);
+		xc_coverager_clean(TSRMLS_C);
+	}
 }
 /* }}} */
@@ -351,6 +425,9 @@
 
 	op_array = origin_compile_file(h, type TSRMLS_CC);
-	if (XG(coveragedumper) && XG(coverages) && op_array) {
-		xc_coverager_init_compile_result(op_array TSRMLS_CC);
+	if (op_array) {
+		if (XG(coveragedumper)) {
+			xc_coverager_initenv(TSRMLS_C);
+			xc_coverager_init_compile_result(op_array TSRMLS_CC);
+		}
 	}
 	return op_array;
@@ -363,5 +440,5 @@
 	TSRMLS_FETCH();
 
-	if (XG(coveragedumper) && XG(coverages)) {
+	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;
@@ -376,5 +453,8 @@
 int xc_coverager_init(int module_number TSRMLS_DC) /* {{{ */
 {
-	if (xc_coveragedump_dir) {
+	origin_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 = strlen(xc_coveragedump_dir);
 		if (len) {
@@ -383,10 +463,9 @@
 			}
 		}
-	}
-	if (xc_coveragedump_dir && xc_coveragedump_dir[0]) {
-		origin_compile_file = zend_compile_file;
-		zend_compile_file = xc_compile_file_for_coverage;
-		CG(extended_info) = 1;
-	}
+		if (!strlen(xc_coveragedump_dir)) {
+			xc_coveragedump_dir = NULL;
+		}
+	}
+
 	return SUCCESS;
 }
@@ -398,5 +477,4 @@
 	}
 	if (xc_coveragedump_dir) {
-		pefree(xc_coveragedump_dir, 1);
 		xc_coveragedump_dir = NULL;
 	}
@@ -405,5 +483,8 @@
 
 /* user api */
-PHP_FUNCTION(xcache_coverager_decode) /* {{{ */
+/* {{{ 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;
@@ -434,3 +515,58 @@
 }
 /* }}} */
-
+/* {{{ 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(coveragedumper)) {
+		xc_coverager_enable(TSRMLS_C);
+	}
+	else {
+		php_error(E_WARNING, "You can only start coverager after you set 'xcache.coveragedumper' 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);
+	}
+}
+/* }}} */
Index: /trunk/coverager.h
===================================================================
--- /trunk/coverager.h	(revision 203)
+++ /trunk/coverager.h	(revision 204)
@@ -1,6 +1,4 @@
 #include "php.h"
 #include "xcache.h"
-
-extern char *xc_coveragedump_dir;
 
 void xc_coverager_handle_ext_stmt(zend_op_array *op_array, zend_uchar op);
@@ -10,2 +8,5 @@
 void xc_coverager_request_shutdown(TSRMLS_D);
 PHP_FUNCTION(xcache_coverager_decode);
+PHP_FUNCTION(xcache_coverager_start);
+PHP_FUNCTION(xcache_coverager_stop);
+PHP_FUNCTION(xcache_coverager_get);
Index: /trunk/xcache-zh-gb2312.ini
===================================================================
--- /trunk/xcache-zh-gb2312.ini	(revision 203)
+++ /trunk/xcache-zh-gb2312.ini	(revision 204)
@@ -66,7 +66,9 @@
 
 [xcache.coverager]
-; ÇëÈ·±£±¾Ä¿Â¼ÄÜ±» coverage viewer ½Å±¾¶ÁÈ¡ (×¢Òâ open_basedir)
-xcache.coveragedump_directory = "/tmp/pcov/"
 
 ; Èç¹û xcache.coveragedump_directory ÉèÖÃÎª¿ÕÔò±¾ÉèÖÃ×Ô¶¯Îª Off
-xcache.coveragedumper         = Off
+xcache.coverager =          Off
+
+; ÇëÈ·±£±¾Ä¿Â¼ÄÜ±» coverage viewer ½Å±¾¶ÁÈ¡ (×¢Òâ open_basedir)
+; ÒÀÀµÓÚ xcache.coverager=On
+xcache.coveragedump_directory = ""
Index: /trunk/xcache.c
===================================================================
--- /trunk/xcache.c	(revision 203)
+++ /trunk/xcache.c	(revision 204)
@@ -2026,4 +2026,7 @@
 #ifdef HAVE_XCACHE_COVERAGER
 	PHP_FE(xcache_coverager_decode,  NULL)
+	PHP_FE(xcache_coverager_start,   NULL)
+	PHP_FE(xcache_coverager_stop,    NULL)
+	PHP_FE(xcache_coverager_get,     NULL)
 #endif
 	PHP_FE(xcache_get_special_value, NULL)
@@ -2158,6 +2161,6 @@
 	STD_PHP_INI_BOOLEAN("xcache.var_ttl",                "0", PHP_INI_ALL,    OnUpdateLong,        var_ttl,           zend_xcache_globals, xcache_globals)
 #ifdef HAVE_XCACHE_COVERAGER
-	PHP_INI_ENTRY1     ("xcache.coveragedump_directory", "/tmp/pcov/", PHP_INI_SYSTEM, xc_OnUpdateString,   &xc_coveragedump_dir)
-	STD_PHP_INI_BOOLEAN("xcache.coveragedumper" ,                 "0", PHP_INI_ALL,    OnUpdateBool,        coveragedumper,    zend_xcache_globals, xcache_globals)
+	STD_PHP_INI_BOOLEAN("xcache.coverager"      ,        "0", PHP_INI_ALL,    OnUpdateBool,        coverager,         zend_xcache_globals, xcache_globals)
+	PHP_INI_ENTRY1     ("xcache.coveragedump_directory",  "", PHP_INI_SYSTEM, xc_OnUpdateDummy,    NULL)
 #endif
 PHP_INI_END()
@@ -2170,4 +2173,5 @@
 	int left, len;
 	xc_shm_scheme_t *scheme;
+	char *covdumpdir;
 
 	php_info_print_table_start();
@@ -2207,5 +2211,8 @@
 
 #ifdef HAVE_XCACHE_COVERAGER
-	php_info_print_table_row(2, "Coverage Dumper", XG(coveragedumper) && xc_coveragedump_dir && xc_coveragedump_dir[0] ? "enabled" : "disabled");
+	if (cfg_get_string("xcache.coveragedump_directory", &covdumpdir) != SUCCESS || !covdumpdir[0]) {
+		covdumpdir = NULL;
+	}
+	php_info_print_table_row(2, "Coverage Auto Dumper", XG(coverager) && covdumpdir ? "enabled" : "disabled");
 #endif
 	php_info_print_table_end();
Index: /trunk/xcache.ini
===================================================================
--- /trunk/xcache.ini	(revision 203)
+++ /trunk/xcache.ini	(revision 204)
@@ -64,8 +64,10 @@
 
 [xcache.coverager]
+; per request settings
+; enable coverage data collecting for xcache.coveragedump_directory and xcache_coverager_start/stop/get/clean() functions (will hurt executing performance)
+xcache.coverager =          Off
+
 ; ini only settings
-; make sure it's readable (care open_basedir) coverage viewer script
-xcache.coveragedump_directory = "/tmp/pcov/"
-
-; per request settings, will be auto disabled if xcache.coveragedump_directory is not set
-xcache.coveragedumper         = Off
+; make sure it's readable (care open_basedir) by coverage viewer script
+; requires xcache.coverager=On
+xcache.coveragedump_directory = ""
Index: /trunk/xcache_globals.h
===================================================================
--- /trunk/xcache_globals.h	(revision 203)
+++ /trunk/xcache_globals.h	(revision 204)
@@ -7,5 +7,6 @@
 #endif
 #ifdef HAVE_XCACHE_COVERAGER
-	zend_bool coveragedumper;
+	zend_bool coverager;
+	zend_bool coverage_enabled;
 	HashTable *coverages;  /* coverages[file][line] = times */
 #endif
