Skip to content

Rename zend_error_notify APIs to zend_observer_error* #6131

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 3 commits 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
10 changes: 5 additions & 5 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,19 @@ PHP 8.0 INTERNALS UPGRADE NOTES

u. Instead of overwriting zend_error_cb extensions with debugging, monitoring
use-cases catching Errors/Exceptions are strongly encouraged to use
the new error notification API instead.
the new error observer API instead.

Error notification callbacks are guaranteed to be called regardless of
Error observering callbacks are guaranteed to be called regardless of
the users error_reporting setting or userland error handler return values.

Register notification callbacks during MINIT of an extension:
Register observer callbacks during MINIT of an extension:

void my_error_notify_cb(int type,
void my_error_observer_cb(int type,
const char *error_filename,
uint32_t error_lineno,
zend_string *message) {
}
zend_register_error_notify_callback(my_error_notify_cb);
zend_observer_error_register(my_error_observer_cb);

v. The following APIs have been removed from the Zend Engine:
- zend_ts_hash_init_ex(), drop the last argument and use zend_ts_hash_init() instead
Expand Down
34 changes: 2 additions & 32 deletions Zend/zend.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ ZEND_TSRMLS_CACHE_DEFINE()
ZEND_API zend_utility_values zend_uv;
ZEND_API zend_bool zend_dtrace_enabled;

zend_llist zend_error_notify_callbacks;

/* version information */
static char *zend_version_info;
static uint32_t zend_version_info_length;
Expand Down Expand Up @@ -832,7 +830,6 @@ void zend_startup(zend_utility_functions *utility_functions) /* {{{ */

zend_startup_strtod();
zend_startup_extensions_mechanism();
zend_startup_error_notify_callbacks();

/* Set up utility functions and values */
zend_error_cb = utility_functions->error_function;
Expand Down Expand Up @@ -865,7 +862,7 @@ void zend_startup(zend_utility_functions *utility_functions) /* {{{ */
zend_execute_ex = dtrace_execute_ex;
zend_execute_internal = dtrace_execute_internal;

zend_register_error_notify_callback(dtrace_error_notify_cb);
zend_observer_error_register(dtrace_error_notify_cb);
} else {
zend_compile_file = compile_file;
zend_execute_ex = execute_ex;
Expand Down Expand Up @@ -1093,7 +1090,6 @@ void zend_shutdown(void) /* {{{ */
zend_hash_destroy(GLOBAL_AUTO_GLOBALS_TABLE);
free(GLOBAL_AUTO_GLOBALS_TABLE);

zend_shutdown_error_notify_callbacks();
zend_shutdown_extensions();
free(zend_version_info);

Expand Down Expand Up @@ -1329,7 +1325,7 @@ static ZEND_COLD void zend_error_impl(
}
}

zend_error_notify_all_callbacks(type, error_filename, error_lineno, message);
zend_observer_error_notify(type, error_filename, error_lineno, message);

/* if we don't have a user defined error handler */
if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF
Expand Down Expand Up @@ -1792,29 +1788,3 @@ ZEND_API void zend_map_ptr_extend(size_t last)
CG(map_ptr_last) = last;
}
}

void zend_startup_error_notify_callbacks(void)
{
zend_llist_init(&zend_error_notify_callbacks, sizeof(zend_error_notify_cb), NULL, 1);
}

void zend_shutdown_error_notify_callbacks(void)
{
zend_llist_destroy(&zend_error_notify_callbacks);
}

ZEND_API void zend_register_error_notify_callback(zend_error_notify_cb cb)
{
zend_llist_add_element(&zend_error_notify_callbacks, &cb);
}

void zend_error_notify_all_callbacks(int type, const char *error_filename, uint32_t error_lineno, zend_string *message)
{
zend_llist_element *element;
zend_error_notify_cb callback;

for (element = zend_error_notify_callbacks.head; element; element = element->next) {
callback = *(zend_error_notify_cb *) (element->data);
callback(type, error_filename, error_lineno, message);
}
}
9 changes: 0 additions & 9 deletions Zend/zend.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,6 @@ ZEND_API void zend_save_error_handling(zend_error_handling *current);
ZEND_API void zend_replace_error_handling(zend_error_handling_t error_handling, zend_class_entry *exception_class, zend_error_handling *current);
ZEND_API void zend_restore_error_handling(zend_error_handling *saved);

typedef void (*zend_error_notify_cb)(int type, const char *error_filename, uint32_t error_lineno, zend_string *message);

BEGIN_EXTERN_C()
ZEND_API void zend_register_error_notify_callback(zend_error_notify_cb callback);
void zend_startup_error_notify_callbacks(void);
void zend_shutdown_error_notify_callbacks(void);
void zend_error_notify_all_callbacks(int type, const char *error_filename, uint32_t error_lineno, zend_string *message);
END_EXTERN_C()

#define DEBUG_BACKTRACE_PROVIDE_OBJECT (1<<0)
#define DEBUG_BACKTRACE_IGNORE_ARGS (1<<1)

Expand Down
5 changes: 3 additions & 2 deletions Zend/zend_exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "zend_dtrace.h"
#include "zend_smart_str.h"
#include "zend_exceptions_arginfo.h"
#include "zend_observer.h"

ZEND_API zend_class_entry *zend_ce_throwable;
ZEND_API zend_class_entry *zend_ce_exception;
Expand Down Expand Up @@ -900,7 +901,7 @@ static void zend_error_va(int type, const char *file, uint32_t lineno, const cha
va_list args;
va_start(args, format);
zend_string *message = zend_vstrpprintf(0, format, args);
zend_error_notify_all_callbacks(type, file, lineno, message);
zend_observer_error_notify(type, file, lineno, message);
zend_error_cb(type, file, lineno, message);
zend_string_release(message);
va_end(args);
Expand All @@ -923,7 +924,7 @@ ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *ex, int severit
zend_long line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
int type = (ce_exception == zend_ce_parse_error ? E_PARSE : E_COMPILE_ERROR) | E_DONT_BAIL;

zend_error_notify_all_callbacks(type, ZSTR_VAL(file), line, message);
zend_observer_error_notify(type, ZSTR_VAL(file), line, message);
zend_error_cb(type, ZSTR_VAL(file), line, message);

zend_string_release_ex(file, 0);
Expand Down
18 changes: 18 additions & 0 deletions Zend/zend_observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "zend_vm.h"

zend_llist zend_observers_fcall_list;
zend_llist zend_observer_error_callbacks;

int zend_observer_fcall_op_array_extension = -1;

ZEND_TLS zend_arena *fcall_handlers_arena = NULL;
Expand Down Expand Up @@ -58,6 +60,7 @@ ZEND_API void zend_observer_fcall_register(zend_observer_fcall_init init) {
// Called by engine before MINITs
ZEND_API void zend_observer_startup(void) {
zend_llist_init(&zend_observers_fcall_list, sizeof(zend_observer_fcall_init), NULL, 1);
zend_llist_init(&zend_observer_error_callbacks, sizeof(zend_observer_error_cb), NULL, 1);
}

ZEND_API void zend_observer_activate(void) {
Expand All @@ -74,6 +77,7 @@ ZEND_API void zend_observer_deactivate(void) {

ZEND_API void zend_observer_shutdown(void) {
zend_llist_destroy(&zend_observers_fcall_list);
zend_llist_destroy(&zend_observer_error_callbacks);
}

ZEND_API void zend_observer_fcall_install(zend_function *function) {
Expand Down Expand Up @@ -157,4 +161,18 @@ ZEND_API void zend_observe_fcall_end(
}
}

ZEND_API void zend_observer_error_register(zend_observer_error_cb cb)
{
zend_llist_add_element(&zend_observer_error_callbacks, &cb);
}

void zend_observer_error_notify(int type, const char *error_filename, uint32_t error_lineno, zend_string *message)
{
zend_llist_element *element;
zend_observer_error_cb callback;

for (element = zend_observer_error_callbacks.head; element; element = element->next) {
callback = *(zend_observer_error_cb *) (element->data);
callback(type, error_filename, error_lineno, message);
}
}
5 changes: 5 additions & 0 deletions Zend/zend_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ ZEND_API zend_always_inline void zend_observer_maybe_fcall_call_end(
}
}

typedef void (*zend_observer_error_cb)(int type, const char *error_filename, uint32_t error_lineno, zend_string *message);

ZEND_API void zend_observer_error_register(zend_observer_error_cb callback);
void zend_observer_error_notify(int type, const char *error_filename, uint32_t error_lineno, zend_string *message);

END_EXTERN_C()

#endif /* ZEND_OBSERVER_H */
2 changes: 1 addition & 1 deletion main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2045,7 +2045,7 @@ int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_mod
zend_update_current_locale();

#if ZEND_DEBUG
zend_register_error_notify_callback(report_zend_debug_error_notify_cb);
zend_observer_error_register(report_zend_debug_error_notify_cb);
#endif

#if HAVE_TZSET
Expand Down