Skip to content

Add configuration value to enable/disable stack trace logging #4281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ ZEND_INI_BEGIN()
#ifdef ZEND_SIGNALS
STD_ZEND_INI_BOOLEAN("zend.signal_check", "0", ZEND_INI_SYSTEM, OnUpdateBool, check, zend_signal_globals_t, zend_signal_globals)
#endif
STD_ZEND_INI_BOOLEAN("zend.log_exception_trace", "0", ZEND_INI_ALL, OnUpdateBool, log_exception_trace, zend_compiler_globals, compiler_globals)
ZEND_INI_END()

ZEND_API size_t zend_vspprintf(char **pbuf, size_t max_len, const char *format, va_list ap) /* {{{ */
Expand Down
37 changes: 26 additions & 11 deletions Zend/zend_exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,10 +664,11 @@ ZEND_METHOD(exception, __toString)
{
zval trace, *exception;
zend_class_entry *base_ce;
zend_string *str;
zend_string *str, *tmpstr;
zend_fcall_info fci;
zval rv, tmp;
zend_string *fname;
size_t str_len;

DEFAULT_0_PARAMS;

Expand Down Expand Up @@ -704,17 +705,31 @@ ZEND_METHOD(exception, __toString)
}

if (ZSTR_LEN(message) > 0) {
str = zend_strpprintf(0, "%s: %s in %s:" ZEND_LONG_FMT
"\nStack trace:\n%s%s%s",
ZSTR_VAL(Z_OBJCE_P(exception)->name), ZSTR_VAL(message), ZSTR_VAL(file), line,
(Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace)) ? Z_STRVAL(trace) : "#0 {main}\n",
ZSTR_LEN(prev_str) ? "\n\nNext " : "", ZSTR_VAL(prev_str));
str = zend_strpprintf(0, "%s: %s in %s:" ZEND_LONG_FMT,
ZSTR_VAL(Z_OBJCE_P(exception)->name), ZSTR_VAL(message), ZSTR_VAL(file), line);
} else {
str = zend_strpprintf(0, "%s in %s:" ZEND_LONG_FMT
"\nStack trace:\n%s%s%s",
ZSTR_VAL(Z_OBJCE_P(exception)->name), ZSTR_VAL(file), line,
(Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace)) ? Z_STRVAL(trace) : "#0 {main}\n",
ZSTR_LEN(prev_str) ? "\n\nNext " : "", ZSTR_VAL(prev_str));
str = zend_strpprintf(0, "%s in %s:" ZEND_LONG_FMT,
ZSTR_VAL(Z_OBJCE_P(exception)->name), ZSTR_VAL(file), line);
}

if (CG(log_exception_trace)) {
/* Add the strack trace */
str_len = ZSTR_LEN(str);
tmpstr = ZSTR_EMPTY_ALLOC();
tmpstr = zend_strpprintf(0, "\nStack trace:\n%s",
(Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace)) ? Z_STRVAL(trace) : "#0 {main}\n");
str = zend_string_extend(str, str_len + ZSTR_LEN(tmpstr), 0);
memcpy(ZSTR_VAL(str) + str_len, ZSTR_VAL(tmpstr), ZSTR_LEN(tmpstr));
zend_string_release_ex(tmpstr, 0);
}

if(ZSTR_LEN(prev_str) > 0) {
str_len = ZSTR_LEN(str);
tmpstr = ZSTR_EMPTY_ALLOC();
tmpstr = zend_strpprintf(0, "\n\nNext %s", ZSTR_VAL(prev_str));
str = zend_string_extend(str, str_len + ZSTR_LEN(tmpstr), 0);
memcpy(ZSTR_VAL(str) + str_len, ZSTR_VAL(tmpstr), ZSTR_LEN(tmpstr));
zend_string_release_ex(tmpstr, 0);
}

zend_string_release_ex(prev_str, 0);
Expand Down
2 changes: 2 additions & 0 deletions Zend/zend_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ struct _zend_compiler_globals {

HashTable *delayed_variance_obligations;
HashTable *delayed_autoloads;

zend_bool log_exception_trace;
};


Expand Down
5 changes: 5 additions & 0 deletions php.ini-development
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ zend.enable_gc = On
; Default: ""
;zend.script_encoding =

; Add stack traces to fatal errors and exceptions. This could potentially contain
; sensitive information and should not be enabled in a production environment.
; Default: Off
;zend.log_exception_trace = Off

;;;;;;;;;;;;;;;;;
; Miscellaneous ;
;;;;;;;;;;;;;;;;;
Expand Down
5 changes: 5 additions & 0 deletions php.ini-production
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ zend.enable_gc = On
; Default: ""
;zend.script_encoding =

; Add stack traces to fatal errors and exceptions. This could potentially contain
; sensitive information and should not be enabled in a production environment.
; Default: Off
;zend.log_exception_trace = Off

;;;;;;;;;;;;;;;;;
; Miscellaneous ;
;;;;;;;;;;;;;;;;;
Expand Down