Skip to content

Commit e3d2964

Browse files
committed
fix transient wrapper creation
1 parent b2a5b53 commit e3d2964

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

agent/php_wrapper.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ nruserfn_t* nr_php_wrap_callable_before_after(
6464
nrspecialfn_t before_callback,
6565
nrspecialfn_t after_callback) {
6666
char* name = NULL;
67+
#if ZEND_MODULE_API_NO >= ZEND_8_2_X_API_NO
68+
zend_observer_fcall_begin_handler *begin_handler;
69+
#endif
6770

6871
/* creates a transient wraprec */
6972
nruserfn_t* wraprec = nr_php_add_custom_tracer_callable(callable TSRMLS_CC);
@@ -80,6 +83,32 @@ nruserfn_t* nr_php_wrap_callable_before_after(
8083
if (nrl_should_print(NRL_VERBOSEDEBUG, NRL_INSTRUMENT) && NULL != name) {
8184
nr_free(name);
8285
}
86+
#if ZEND_MODULE_API_NO >= ZEND_8_2_X_API_NO
87+
if (callable) {
88+
// Before messing with our handlers, we must ensure that the observer fields of the function are initialized
89+
begin_handler = (zend_observer_fcall_begin_handler *)&ZEND_OP_ARRAY_EXTENSION((&(callable)->common), zend_observer_fcall_op_array_extension);
90+
// begin_handler will be NULL if the observer hasn't been installed yet.
91+
// *begin_Handler will be NULL if the function has not yet been called.
92+
if (begin_handler && *begin_handler) {
93+
// It is okay to attempt to remove a handler that doesn't exist
94+
// TODO this could remove nr_php_observer_fcall_begin/end and then re-add it :)
95+
if (zend_observer_remove_begin_handler(callable, NRINI(tt_detail) ?
96+
nr_php_observer_fcall_begin :
97+
nr_php_observer_empty_fcall_begin)) {
98+
zend_observer_add_begin_handler(callable, wraprec->special_instrumentation_before ?
99+
(zend_observer_fcall_begin_handler)wraprec->special_instrumentation_before :
100+
nr_php_observer_fcall_begin);
101+
}
102+
if (zend_observer_remove_end_handler(callable, NRINI(tt_detail) ?
103+
nr_php_observer_fcall_end :
104+
nr_php_observer_empty_fcall_end)) {
105+
zend_observer_add_end_handler(callable, wraprec->special_instrumentation ?
106+
(zend_observer_fcall_end_handler)wraprec->special_instrumentation :
107+
nr_php_observer_fcall_end);
108+
}
109+
}
110+
}
111+
#endif
83112

84113
return wraprec;
85114
}

0 commit comments

Comments
 (0)