]> granicus.if.org Git - php/commitdiff
Add optional argument to debug_backtrace() and debug_print_backtrace() to limit the...
authorSebastian Bergmann <sebastian@php.net>
Sat, 9 Apr 2011 09:52:24 +0000 (09:52 +0000)
committerSebastian Bergmann <sebastian@php.net>
Sat, 9 Apr 2011 09:52:24 +0000 (09:52 +0000)
NEWS
Zend/tests/debug_backtrace_limit.phpt [new file with mode: 0644]
Zend/tests/debug_print_backtrace_limit.phpt [new file with mode: 0644]
Zend/zend_builtin_functions.c
Zend/zend_builtin_functions.h
Zend/zend_exceptions.c
ext/mysqlnd/mysqlnd_bt.c

diff --git a/NEWS b/NEWS
index 382b4d7294319fb983d02ee286436c4be3decb16..47d0f9d6742e52a1e4b83e19f8178bffcfcd1b65 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -51,6 +51,8 @@ PHP                                                                        NEWS
     (galaxy dot mipt at gmail dot com, Kalle)
   . Improved unix build system to allow building multiple PHP binary SAPIs and
     one SAPI module the same time. FR #53271, FR #52410. (Jani)
+  . Added optional argument to debug_backtrace() and debug_print_backtrace()
+    to limit the amount of stack frames returned. (Sebastian, Patrick)
 
 - Improved Zend Engine memory usage: (Dmitry)
   . Replaced zend_function.pass_rest_by_reference by 
diff --git a/Zend/tests/debug_backtrace_limit.phpt b/Zend/tests/debug_backtrace_limit.phpt
new file mode 100644 (file)
index 0000000..59e0401
--- /dev/null
@@ -0,0 +1,133 @@
+--TEST--
+debug_backtrace limit
+--FILE--
+<?php
+function a() {
+    b();
+}
+
+function b() {
+    c();
+}
+
+function c() {
+    print_r(debug_backtrace(0, 1));
+    print_r(debug_backtrace(0, 2));
+    print_r(debug_backtrace(0, 0));
+    print_r(debug_backtrace(0, 4));
+}
+
+a();
+?>
+--EXPECTF--
+Array
+(
+    [0] => Array
+        (
+            [file] => %s/debug_backtrace_limit.php
+            [line] => 7
+            [function] => c
+            [args] => Array
+                (
+                )
+
+        )
+
+)
+Array
+(
+    [0] => Array
+        (
+            [file] => %s/debug_backtrace_limit.php
+            [line] => 7
+            [function] => c
+            [args] => Array
+                (
+                )
+
+        )
+
+    [1] => Array
+        (
+            [file] => %s/debug_backtrace_limit.php
+            [line] => 3
+            [function] => b
+            [args] => Array
+                (
+                )
+
+        )
+
+)
+Array
+(
+    [0] => Array
+        (
+            [file] => %s/debug_backtrace_limit.php
+            [line] => 7
+            [function] => c
+            [args] => Array
+                (
+                )
+
+        )
+
+    [1] => Array
+        (
+            [file] => %s/debug_backtrace_limit.php
+            [line] => 3
+            [function] => b
+            [args] => Array
+                (
+                )
+
+        )
+
+    [2] => Array
+        (
+            [file] => %s/debug_backtrace_limit.php
+            [line] => 17
+            [function] => a
+            [args] => Array
+                (
+                )
+
+        )
+
+)
+Array
+(
+    [0] => Array
+        (
+            [file] => %s/debug_backtrace_limit.php
+            [line] => 7
+            [function] => c
+            [args] => Array
+                (
+                )
+
+        )
+
+    [1] => Array
+        (
+            [file] => %s/debug_backtrace_limit.php
+            [line] => 3
+            [function] => b
+            [args] => Array
+                (
+                )
+
+        )
+
+    [2] => Array
+        (
+            [file] => %s/debug_backtrace_limit.php
+            [line] => 17
+            [function] => a
+            [args] => Array
+                (
+                )
+
+        )
+
+)
diff --git a/Zend/tests/debug_print_backtrace_limit.phpt b/Zend/tests/debug_print_backtrace_limit.phpt
new file mode 100644 (file)
index 0000000..2bafcc7
--- /dev/null
@@ -0,0 +1,31 @@
+--TEST--
+debug_print_backtrace limit
+--FILE--
+<?php
+function a() {
+    b();
+}
+
+function b() {
+    c();
+}
+
+function c() {
+    debug_print_backtrace(0, 1);
+    debug_print_backtrace(0, 2);
+    debug_print_backtrace(0, 0);
+    debug_print_backtrace(0, 4);
+}
+
+a();
+?>
+--EXPECTF--
+#0  c() called at [%s/debug_print_backtrace_limit.php:7]
+#0  c() called at [%s/debug_print_backtrace_limit.php:7]
+#1  b() called at [%s/debug_print_backtrace_limit.php:3]
+#0  c() called at [%s/debug_print_backtrace_limit.php:7]
+#1  b() called at [%s/debug_print_backtrace_limit.php:3]
+#2  a() called at [%s/debug_print_backtrace_limit.php:17]
+#0  c() called at [%s/debug_print_backtrace_limit.php:7]
+#1  b() called at [%s/debug_print_backtrace_limit.php:3]
+#2  a() called at [%s/debug_print_backtrace_limit.php:17]
index c4f0fbc17bfda83aa47d4bb2b7273e3990e7bf4f..8b96f4729a16019d874c723736bf8d90dff5825c 100644 (file)
@@ -227,6 +227,11 @@ ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_INFO_EX(arginfo_debug_backtrace, 0, 0, 0)
        ZEND_ARG_INFO(0, options)
+       ZEND_ARG_INFO(0, limit)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_debug_print_backtrace, 0, 0, 0)
+       ZEND_ARG_INFO(0, options)
 ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_INFO_EX(arginfo_extension_loaded, 0, 0, 1)
@@ -290,7 +295,7 @@ static const zend_function_entry builtin_functions[] = { /* {{{ */
        ZEND_FE(get_extension_funcs,            arginfo_extension_loaded)
        ZEND_FE(get_defined_constants,          arginfo_get_defined_constants)
        ZEND_FE(debug_backtrace,                        arginfo_debug_backtrace)
-       ZEND_FE(debug_print_backtrace,          arginfo_debug_backtrace)
+       ZEND_FE(debug_print_backtrace,          arginfo_debug_print_backtrace)
 #if ZEND_DEBUG
        ZEND_FE(zend_test_func,         NULL)
 #ifdef ZTS
@@ -2047,11 +2052,11 @@ void debug_print_backtrace_args(zval *arg_array TSRMLS_DC)
        }
 }
 
-/* {{{ proto void debug_print_backtrace([int options]) */
+/* {{{ proto void debug_print_backtrace([int options[, int limit]]) */
 ZEND_FUNCTION(debug_print_backtrace)
 {
        zend_execute_data *ptr, *skip;
-       int lineno;
+       int lineno, frameno = 0;
        char *function_name;
        char *filename;
        const char *class_name = NULL;
@@ -2060,8 +2065,9 @@ ZEND_FUNCTION(debug_print_backtrace)
        zval *arg_array = NULL;
        int indent = 0;
        long options = 0;
+       long limit = 0;
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &options) == FAILURE) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &options, &limit) == FAILURE) {
                return;
        }
 
@@ -2070,9 +2076,10 @@ ZEND_FUNCTION(debug_print_backtrace)
        /* skip debug_backtrace() */
        ptr = ptr->prev_execute_data;
 
-       while (ptr) {
+       while (ptr && (limit == 0 || frameno < limit)) {
                const char *free_class_name = NULL;
 
+               frameno++;
                class_name = call_type = NULL;   
                arg_array = NULL;
 
@@ -2207,10 +2214,10 @@ ZEND_FUNCTION(debug_print_backtrace)
 
 /* }}} */
 
-ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options TSRMLS_DC)
+ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options, int limit TSRMLS_DC)
 {
        zend_execute_data *ptr, *skip;
-       int lineno;
+       int lineno, frameno = 0;
        char *function_name;
        char *filename;
        char *class_name;
@@ -2231,7 +2238,8 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int
 
        array_init(return_value);
 
-       while (ptr) {
+       while (ptr && (limit == 0 || frameno < limit)) {
+               frameno++;
                MAKE_STD_ZVAL(stack_frame);
                array_init(stack_frame);
 
@@ -2369,17 +2377,18 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int
 /* }}} */
 
 
-/* {{{ proto array debug_backtrace([int options])
+/* {{{ proto array debug_backtrace([int options[, int limit]])
    Return backtrace as array */
 ZEND_FUNCTION(debug_backtrace)
 {
        long options = DEBUG_BACKTRACE_PROVIDE_OBJECT;
+       long limit = 0;
        
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &options) == FAILURE) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &options, &limit) == FAILURE) {
                return;
        }
 
-       zend_fetch_debug_backtrace(return_value, 1, options TSRMLS_CC);
+       zend_fetch_debug_backtrace(return_value, 1, options, limit TSRMLS_CC);
 }
 /* }}} */
 
index cb7699654731debe99436afbacece18356431338..32c33ba1581b5e8ea00c55a765e12e3aeed6074b 100644 (file)
@@ -25,7 +25,7 @@
 int zend_startup_builtin_functions(TSRMLS_D);
 
 BEGIN_EXTERN_C()
-ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options TSRMLS_DC);
+ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options, int limit TSRMLS_DC);
 END_EXTERN_C()
 
 #endif /* ZEND_BUILTIN_FUNCTIONS_H */
index 0865815c95cc057b2fc7a372918b59c89004e4e7..8db4c7633d7da981777bc3348aaa92d4e4247169 100644 (file)
@@ -158,7 +158,7 @@ static zend_object_value zend_default_exception_new_ex(zend_class_entry *class_t
        ALLOC_ZVAL(trace);
        Z_UNSET_ISREF_P(trace);
        Z_SET_REFCOUNT_P(trace, 0);
-       zend_fetch_debug_backtrace(trace, skip_top_traces, 0 TSRMLS_CC);
+       zend_fetch_debug_backtrace(trace, skip_top_traces, 0, 0 TSRMLS_CC);
 
        zend_update_property_string(default_exception_ce, &obj, "file", sizeof("file")-1, zend_get_executed_filename(TSRMLS_C) TSRMLS_CC);
        zend_update_property_long(default_exception_ce, &obj, "line", sizeof("line")-1, zend_get_executed_lineno(TSRMLS_C) TSRMLS_CC);
index 4863012409b8830f1dff726a9d3ec9b7d85c9112..536782063da0549cf9251972ad072d8d1d376942 100644 (file)
@@ -454,7 +454,7 @@ PHPAPI char * mysqlnd_get_backtrace(uint max_levels, size_t * length TSRMLS_DC)
        }
 
        MAKE_STD_ZVAL(trace);
-       zend_fetch_debug_backtrace(trace, 0, 0 TSRMLS_CC);
+       zend_fetch_debug_backtrace(trace, 0, 0, 0 TSRMLS_CC);
 
        zend_hash_apply_with_arguments(Z_ARRVAL_P(trace) TSRMLS_CC, (apply_func_args_t)mysqlnd_build_trace_string, 4, &max_levels, str, len, &num);
        zval_ptr_dtor(&trace);