svn: /pecl/scream/trunk/ php_scream.h scream.c tests/001.phpt
| From: | Gopal Vijayaraghavan | Date: | Mon, 09 Jan 2012 11:14:01 +0000 |
| Subject: | svn: /pecl/scream/trunk/ php_scream.h scream.c tests/001.phpt | ||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-17440@lists.php.net to get a copy of this message | ||
gopalv Mon, 09 Jan 2012 11:14:01 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=321948
Log:
add scream.memorydump to scream
Changed paths:
U pecl/scream/trunk/php_scream.h
U pecl/scream/trunk/scream.c
U pecl/scream/trunk/tests/001.phpt
Modified: pecl/scream/trunk/php_scream.h
===================================================================
--- pecl/scream/trunk/php_scream.h 2012-01-09 10:10:32 UTC (rev 321947)
+++ pecl/scream/trunk/php_scream.h 2012-01-09 11:14:01 UTC (rev 321948)
@@ -48,6 +48,10 @@
ZEND_BEGIN_MODULE_GLOBALS(scream)
/* configuration parameters */
zend_bool enabled; /* defaults to false */
+ zend_bool silence; /* defaults to false */
+ zend_bool memory; /* defaults to false */
+ char *coredir;
+ char *gdb;
ZEND_END_MODULE_GLOBALS(scream)
/* }}} */
Modified: pecl/scream/trunk/scream.c
===================================================================
--- pecl/scream/trunk/scream.c 2012-01-09 10:10:32 UTC (rev 321947)
+++ pecl/scream/trunk/scream.c 2012-01-09 11:14:01 UTC (rev 321948)
@@ -32,13 +32,21 @@
/* {{{ php_scream_init_globals */
static void php_scream_init_globals(zend_scream_globals* scream_globals TSRMLS_DC)
{
- scream_globals->enabled = 1;
+ scream_globals->enabled = 0;
+ scream_globals->silence = 0;
+ scream_globals->memory = 0;
+ scream_globals->gdb = NULL;
+ scream_globals->coredir = NULL;
}
/* }}} */
/* {{{ ini entries */
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("scream.enabled", "0", PHP_INI_ALL, OnUpdateBool, enabled,
zend_scream_globals, scream_globals)
+ STD_PHP_INI_BOOLEAN("scream.silence", "1", PHP_INI_ALL, OnUpdateBool, silence,
zend_scream_globals, scream_globals)
+ STD_PHP_INI_BOOLEAN("scream.memorydump", "0", PHP_INI_ALL, OnUpdateBool,
memory, zend_scream_globals, scream_globals)
+ STD_PHP_INI_BOOLEAN("scream.gdb", "/usr/bin/gdb", PHP_INI_ALL, OnUpdateString,
gdb, zend_scream_globals, scream_globals)
+ STD_PHP_INI_BOOLEAN("scream.coredir", "/tmp", PHP_INI_ALL, OnUpdateString,
coredir, zend_scream_globals, scream_globals)
PHP_INI_END()
/* }}} */
@@ -47,9 +55,102 @@
#endif
/* {{{ zend hooks */
+
+/* Pointer to the original execute function */
+static ZEND_DLEXPORT void (*_zend_execute) (zend_op_array *ops TSRMLS_DC) = NULL;
+
+/* Pointer to the original execute_internal function */
+static ZEND_DLEXPORT void (*_zend_execute_internal) (zend_execute_data *data, int ret TSRMLS_DC) =
NULL;
+
+static void scream_core_dump ()
+{
+ char gdbpath[MAXPATHLEN+1]="/tmp/scream.gdb.XXXXXX";
+ int fd = mkstemp(gdbpath);
+ FILE *gdbfile = NULL;
+ int child;
+ char command[MAXPATHLEN*3+1]={0,};
+
+ if(fd == -1)
+ {
+ perror("gdb macro create");
+ return; /* silent fail */
+ }
+ gdbfile = fdopen(fd, "w");
+ if(gdbfile == NULL)
+ {
+ perror("gdb macro create");
+ return;
+ }
+ fprintf(gdbfile, "at %d\n", getpid());
+ fprintf(gdbfile, "bt\n");
+ fprintf(gdbfile, "gc %s/%d.core\n", SCREAM_G(coredir), getpid());
+ fprintf(gdbfile, "quit");
+ fclose(gdbfile);
+
+ snprintf(command, sizeof(command), "%s --batch-silent -x %s 2> /dev/null 1>
/dev/null", SCREAM_G(gdb), gdbpath);
+ if(system(command) == 0)
+ {
+ fprintf(stderr, "PHP OOM detected, dumped core to %d.core\n", getpid());
+ }
+ else
+ {
+ fprintf(stderr, "Failed to run gdb command %s -x %s\n", SCREAM_G(gdb), gdbpath);
+ }
+}
+
+ZEND_DLEXPORT void scream_execute (zend_op_array *ops TSRMLS_DC)
+{
+ size_t before, after, limit;
+
+ limit = PG(memory_limit);
+ before = zend_memory_usage(0 TSRMLS_CC);
+
+ if(before < 0.9*limit)
+ {
+ /* fast path */
+ _zend_execute(ops TSRMLS_CC);
+ after = zend_memory_usage(0 TSRMLS_CC);
+ }
+ else
+ {
+ zend_try
+ {
+ _zend_execute(ops TSRMLS_CC);
+ after = zend_memory_usage(0 TSRMLS_CC);
+ }
+ zend_catch
+ {
+ /* generate a core dump */
+ scream_core_dump();
+ /* rethrow */
+ zend_bailout();
+ }
+ zend_end_try();
+ }
+}
+
+ZEND_DLEXPORT void scream_execute_internal(zend_execute_data *execute_data, int ret TSRMLS_DC)
+{
+ size_t before, after, limit;
+
+ limit = PG(memory_limit);
+ before = zend_memory_usage(0 TSRMLS_CC);
+ if(before < 0.9*limit)
+ {
+ /* fast path */
+ _zend_execute_internal(execute_data, ret TSRMLS_CC);
+ after = zend_memory_usage(0 TSRMLS_CC);
+ }
+ else
+ {
+ _zend_execute_internal(execute_data, ret TSRMLS_CC);
+ after = zend_memory_usage(0 TSRMLS_CC);
+ }
+}
+
static int scream_op_ZEND_SILENCE(ZEND_OPCODE_HANDLER_ARGS)
{
- if(SCREAM_G(enabled))
+ if(SCREAM_G(silence))
{
execute_data->opline++;
return ZEND_USER_OPCODE_CONTINUE;
@@ -61,13 +162,28 @@
{
if((zend_set_user_opcode_handler(ZEND_BEGIN_SILENCE,
scream_op_ZEND_SILENCE) == FAILURE) ||
- (zend_set_user_opcode_handler(ZEND_END_SILENCE,
+ (zend_set_user_opcode_handler(ZEND_END_SILENCE,
scream_op_ZEND_SILENCE) == FAILURE))
{
zend_error(E_NOTICE, "cannot install silence opcode override");
- SCREAM_G(enabled) = 0;
+ SCREAM_G(silence) = 0;
}
+
+ if(SCREAM_G(memory))
+ {
+ /* Replace zend_execute with our proxy */
+ _zend_execute = zend_execute;
+ zend_execute = scream_execute;
+
+ if(zend_execute_internal)
+ {
+ /* Replace zend_execute_internal with our proxy */
+ _zend_execute_internal = zend_execute_internal;
+ zend_execute_internal = scream_execute_internal;
+ }
+ }
}
+
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
@@ -77,9 +193,13 @@
ZEND_INIT_MODULE_GLOBALS(scream, php_scream_init_globals, NULL);
REGISTER_INI_ENTRIES();
-
- scream_zend_init(TSRMLS_C);
+ if(SCREAM_G(enabled))
+ {
+ /* if the ini enables the extension, the default mode is to turn on the scream.silence = On; */
+ scream_zend_init(TSRMLS_C);
+ }
+
return SUCCESS;
}
/* }}} */
Modified: pecl/scream/trunk/tests/001.phpt
===================================================================
--- pecl/scream/trunk/tests/001.phpt 2012-01-09 10:10:32 UTC (rev 321947)
+++ pecl/scream/trunk/tests/001.phpt 2012-01-09 11:14:01 UTC (rev 321948)
@@ -7,18 +7,17 @@
}
?>
--INI--
-scream.enabled=0
+scream.enabled=1
+scream.silence=0
error_reporting=E_ALL|E_STRICT
--FILE--
<?php
$foo = @$bar1;
-ini_set('scream.enabled', 1);
+ini_set('scream.silence', 1);
$foo = @$bar2;
-ini_set('scream.enabled', 0);
+ini_set('scream.silence', 0);
$foo = @$bar3;
-
-echo "--DONE--";
?>
--EXPECTF--
-Notice: Undefined variable: bar2 in %s on line %d
+Notice: Undefined variable: bar2 in %s on line 4
--DONE--