@@ -64,6 +64,9 @@ nruserfn_t* nr_php_wrap_callable_before_after(
64
64
nrspecialfn_t before_callback ,
65
65
nrspecialfn_t after_callback ) {
66
66
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
67
70
68
71
/* creates a transient wraprec */
69
72
nruserfn_t * wraprec = nr_php_add_custom_tracer_callable (callable TSRMLS_CC );
@@ -80,6 +83,32 @@ nruserfn_t* nr_php_wrap_callable_before_after(
80
83
if (nrl_should_print (NRL_VERBOSEDEBUG , NRL_INSTRUMENT ) && NULL != name ) {
81
84
nr_free (name );
82
85
}
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
83
112
84
113
return wraprec ;
85
114
}
0 commit comments