]> granicus.if.org Git - php/commitdiff
improve uncaught exception handler
authorkrakjoe <joe.watkins@live.co.uk>
Mon, 18 Nov 2013 17:00:26 +0000 (17:00 +0000)
committerkrakjoe <joe.watkins@live.co.uk>
Mon, 18 Nov 2013 17:00:26 +0000 (17:00 +0000)
phpdbg_prompt.c
test.php

index e6d4a0ca73908df9572b5aac69b56a44e5a7d0d7..88d03dc06fbd3cf04361a43e7797d610a56f93cd 100644 (file)
@@ -390,6 +390,56 @@ static PHPDBG_COMMAND(leave) /* {{{ */
        return PHPDBG_LEAVE;
 } /* }}} */
 
+static inline void phpdbg_handle_exception(TSRMLS_D) /* }}} */
+{
+       zend_fcall_info fci;
+
+       zval fname, 
+                *trace, 
+                exception;
+
+       /* get filename and linenumber before unsetting exception */
+       const char *filename = zend_get_executed_filename(TSRMLS_C);
+       zend_uint lineno = zend_get_executed_lineno(TSRMLS_C);
+
+       /* copy exception */
+       exception = *EG(exception);
+       zval_copy_ctor(&exception);
+       EG(exception) = NULL;
+
+       phpdbg_error(
+               "Uncaught %s !", 
+               Z_OBJCE(exception)->name);
+
+       /* call __toString */
+       ZVAL_STRINGL(&fname, "__tostring", sizeof("__tostring")-1, 1);
+       fci.size = sizeof(fci);
+       fci.function_table = &Z_OBJCE(exception)->function_table;
+       fci.function_name = &fname;
+       fci.symbol_table = NULL;
+       fci.object_ptr = &exception;
+       fci.retval_ptr_ptr = &trace;
+       fci.param_count = 0;
+       fci.params = NULL;
+       fci.no_separation = 1;
+       zend_call_function(&fci, NULL TSRMLS_CC);
+       
+       if (trace) {
+               phpdbg_writeln(
+                       "Uncaught %s", Z_STRVAL_P(trace));
+               /* remember to dtor trace */
+               zval_ptr_dtor(&trace);
+       }
+       
+       /* output useful information about address */
+       phpdbg_writeln(
+               "Stacked entered at %p in %s on line %lu",
+               EG(active_op_array)->opcodes, filename, lineno);
+       
+       zval_dtor(&fname);
+       zval_dtor(&exception);
+} /* }}} */
+
 static PHPDBG_COMMAND(run) /* {{{ */
 {
        if (EG(in_execution)) {
@@ -441,16 +491,7 @@ static PHPDBG_COMMAND(run) /* {{{ */
                } zend_end_try();
 
                if (EG(exception)) {
-                       phpdbg_error("Uncaught Exception !");
-                       /*
-                       * @TODO(anyone) something better !!
-                       */
-                       zend_print_zval_r(
-                               EG(exception), 0 TSRMLS_CC);
-
-                       /* make sure this is dtor'd and reset */
-                       zval_ptr_dtor(&EG(exception));
-                       EG(exception) = NULL;
+                       phpdbg_handle_exception(TSRMLS_C);
                }
 
                EG(active_op_array) = orig_op_array;
index 2266d157d5b8ffe66a875e003ad9643afa2bb701..7e3a96c87d51d77b3ddb03d869ebd8b819adf7b4 100644 (file)
--- a/test.php
+++ b/test.php
@@ -1,4 +1,15 @@
 <?php
+
+function outer() {
+       mine();
+}
+
+function mine() {
+       throw new Exception();
+}
+
+outer();
+
 if (isset($include))
        include (sprintf("%s/web-bootstrap.php", dirname(__FILE__)));