Skip to content

Commit 0d7062b

Browse files
ZNeumannlavarou
authored andcommitted
unify handler overwrite into function
1 parent 94362eb commit 0d7062b

File tree

4 files changed

+38
-81
lines changed

4 files changed

+38
-81
lines changed

agent/php_observer.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,36 @@ static zend_observer_fcall_handlers nr_php_fcall_register_handlers(
126126
nr_php_observer_fcall_end;
127127
return handlers;
128128
}
129+
130+
bool nr_php_observer_is_registered(zend_function* func) {
131+
zend_observer_fcall_begin_handler *begin_handler;
132+
if (func == NULL) {
133+
return false;
134+
}
135+
begin_handler = (zend_observer_fcall_begin_handler *)&ZEND_OP_ARRAY_EXTENSION((&(func)->common), zend_observer_fcall_op_array_extension);
136+
// begin_handler will be NULL if the observer hasn't been installed yet.
137+
// *begin_Handler will be NULL if the function has not yet been called.
138+
return (begin_handler && *begin_handler);
139+
}
140+
141+
void nr_php_observer_overwrite_handlers(zend_function* func, nruserfn_t* wraprec) {
142+
if (nr_php_observer_is_registered(func)) {
143+
if (zend_observer_remove_begin_handler(func, NRINI(tt_detail) ?
144+
nr_php_observer_fcall_begin :
145+
nr_php_observer_empty_fcall_begin)) {
146+
zend_observer_add_begin_handler(func, wraprec->special_instrumentation_before ?
147+
(zend_observer_fcall_begin_handler)wraprec->special_instrumentation_before :
148+
nr_php_observer_fcall_begin);
149+
}
150+
if (zend_observer_remove_end_handler(func, NRINI(tt_detail) ?
151+
nr_php_observer_fcall_end :
152+
nr_php_observer_empty_fcall_end)) {
153+
zend_observer_add_end_handler(func, wraprec->special_instrumentation ?
154+
(zend_observer_fcall_end_handler)wraprec->special_instrumentation :
155+
nr_php_observer_fcall_end);
156+
}
157+
}
158+
}
129159
#endif
130160

131161

agent/php_observer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#if ZEND_MODULE_API_NO >= ZEND_8_0_X_API_NO /* PHP8+ */
1818

1919
#include "Zend/zend_observer.h"
20+
#include "php_user_instrument.h"
2021

2122
/*
2223
* Purpose: There are a few various places, aside from the php_execute_* family
@@ -77,6 +78,9 @@ void nr_php_observer_fcall_end(zend_execute_data* execute_data,
7778

7879

7980
#if ZEND_MODULE_API_NO >= ZEND_8_2_X_API_NO
81+
bool nr_php_observer_is_registered(zend_function* func);
82+
void nr_php_observer_overwrite_handlers(zend_function* func, nruserfn_t* wraprec);
83+
8084
void nr_php_observer_empty_fcall_begin(zend_execute_data* execute_data);
8185
void nr_php_observer_fcall_begin_instrumented(zend_execute_data* execute_data);
8286

agent/php_user_instrument.c

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,6 @@ nruserfn_t* nr_php_add_custom_tracer_named(const char* namestr,
452452
nruserfn_t* p;
453453
#if ZEND_MODULE_API_NO >= ZEND_8_2_X_API_NO
454454
zend_function* orig_func;
455-
zend_observer_fcall_begin_handler *begin_handler;
456455
#endif
457456

458457
wraprec = nr_php_user_wraprec_create_named(namestr, namestrlen);
@@ -494,28 +493,7 @@ nruserfn_t* nr_php_add_custom_tracer_named(const char* namestr,
494493
* module shutdown */
495494
nr_php_add_custom_tracer_common(wraprec);
496495
#if ZEND_MODULE_API_NO >= ZEND_8_2_X_API_NO
497-
if (orig_func) {
498-
// Before messing with our handlers, we must ensure that the observer fields of the function are initialized
499-
begin_handler = (zend_observer_fcall_begin_handler *)&ZEND_OP_ARRAY_EXTENSION((&(orig_func)->common), zend_observer_fcall_op_array_extension);
500-
// begin_handler will be NULL if the observer hasn't been installed yet.
501-
// *begin_Handler will be NULL if the function has not yet been called.
502-
if (begin_handler && *begin_handler) {
503-
if (zend_observer_remove_begin_handler(orig_func, NRINI(tt_detail) ?
504-
nr_php_observer_fcall_begin :
505-
nr_php_observer_empty_fcall_begin)) {
506-
zend_observer_add_begin_handler(orig_func, wraprec->special_instrumentation_before ?
507-
(zend_observer_fcall_begin_handler)wraprec->special_instrumentation_before :
508-
nr_php_observer_fcall_begin_instrumented);
509-
}
510-
if (zend_observer_remove_end_handler(orig_func, NRINI(tt_detail) ?
511-
nr_php_observer_fcall_end :
512-
nr_php_observer_empty_fcall_end)) {
513-
zend_observer_add_end_handler(orig_func, wraprec->special_instrumentation ?
514-
(zend_observer_fcall_end_handler)wraprec->special_instrumentation :
515-
nr_php_observer_fcall_end);
516-
}
517-
}
518-
}
496+
nr_php_observer_overwrite_handlers(orig_func, wraprec);
519497
#endif
520498

521499
return wraprec; /* return the new wraprec */

agent/php_wrapper.c

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ nruserfn_t* nr_php_wrap_callable_before_after(
8383
zend_function* callable,
8484
nrspecialfn_t before_callback,
8585
nrspecialfn_t after_callback) {
86-
char* name = NULL;
87-
#if ZEND_MODULE_API_NO >= ZEND_8_2_X_API_NO
88-
zend_observer_fcall_begin_handler *begin_handler;
89-
#endif
86+
char* name = NULL;
9087

9188
/* creates a transient wraprec */
9289
nruserfn_t* wraprec = nr_php_add_custom_tracer_callable(callable TSRMLS_CC);
@@ -104,33 +101,7 @@ nruserfn_t* nr_php_wrap_callable_before_after(
104101
nr_free(name);
105102
}
106103
#if ZEND_MODULE_API_NO >= ZEND_8_2_X_API_NO
107-
if (callable) {
108-
// Before messing with our handlers, we must ensure that the observer fields of the function are initialized
109-
begin_handler = (zend_observer_fcall_begin_handler *)&ZEND_OP_ARRAY_EXTENSION((&(callable)->common), zend_observer_fcall_op_array_extension);
110-
// begin_handler will be NULL if the observer hasn't been installed yet.
111-
// *begin_Handler will be NULL if the function has not yet been called.
112-
if (begin_handler && *begin_handler) {
113-
name = nr_php_function_debug_name(callable);
114-
php_printf("AHHHHHHH %s\n", name);
115-
nr_free(name);
116-
// It is okay to attempt to remove a handler that doesn't exist
117-
// TODO this could remove nr_php_observer_fcall_begin/end and then re-add it :)
118-
if (zend_observer_remove_begin_handler(callable, NRINI(tt_detail) ?
119-
nr_php_observer_fcall_begin :
120-
nr_php_observer_empty_fcall_begin)) {
121-
zend_observer_add_begin_handler(callable, wraprec->special_instrumentation_before ?
122-
(zend_observer_fcall_begin_handler)wraprec->special_instrumentation_before :
123-
nr_php_observer_fcall_begin);
124-
}
125-
if (zend_observer_remove_end_handler(callable, NRINI(tt_detail) ?
126-
nr_php_observer_fcall_end :
127-
nr_php_observer_empty_fcall_end)) {
128-
zend_observer_add_end_handler(callable, wraprec->special_instrumentation ?
129-
(zend_observer_fcall_end_handler)wraprec->special_instrumentation :
130-
nr_php_observer_fcall_end);
131-
}
132-
}
133-
}
104+
nr_php_observer_overwrite_handlers(callable, wraprec);
134105
#endif
135106

136107
return wraprec;
@@ -172,9 +143,6 @@ nruserfn_t* nr_php_wrap_callable(zend_function* callable,
172143
nrspecialfn_t callback TSRMLS_DC) {
173144
/* creates a transient wraprec */
174145
nruserfn_t* wraprec = nr_php_add_custom_tracer_callable(callable TSRMLS_CC);
175-
#if ZEND_MODULE_API_NO >= ZEND_8_2_X_API_NO
176-
zend_observer_fcall_begin_handler *begin_handler;
177-
#endif
178146

179147
if (wraprec && callback) {
180148
if ((NULL != wraprec->special_instrumentation)
@@ -186,30 +154,7 @@ nruserfn_t* nr_php_wrap_callable(zend_function* callable,
186154
} else {
187155
wraprec->special_instrumentation = callback;
188156
#if ZEND_MODULE_API_NO >= ZEND_8_2_X_API_NO
189-
if (callable) {
190-
// Before messing with our handlers, we must ensure that the observer fields of the function are initialized
191-
begin_handler = (zend_observer_fcall_begin_handler *)&ZEND_OP_ARRAY_EXTENSION((&(callable)->common), zend_observer_fcall_op_array_extension);
192-
// begin_handler will be NULL if the observer hasn't been installed yet.
193-
// *begin_Handler will be NULL if the function has not yet been called.
194-
if (begin_handler && *begin_handler) {
195-
// It is okay to attempt to remove a handler that doesn't exist
196-
// TODO this could remove nr_php_observer_fcall_begin/end and then re-add it :)
197-
if (zend_observer_remove_begin_handler(callable, NRINI(tt_detail) ?
198-
nr_php_observer_fcall_begin :
199-
nr_php_observer_empty_fcall_begin)) {
200-
zend_observer_add_begin_handler(callable, wraprec->special_instrumentation_before ?
201-
(zend_observer_fcall_begin_handler)wraprec->special_instrumentation_before :
202-
nr_php_observer_fcall_begin);
203-
}
204-
if (zend_observer_remove_end_handler(callable, NRINI(tt_detail) ?
205-
nr_php_observer_fcall_end :
206-
nr_php_observer_empty_fcall_end)) {
207-
zend_observer_add_end_handler(callable, wraprec->special_instrumentation ?
208-
(zend_observer_fcall_end_handler)wraprec->special_instrumentation :
209-
nr_php_observer_fcall_end);
210-
}
211-
}
212-
}
157+
nr_php_observer_overwrite_handlers(callable, wraprec);
213158
#endif
214159
}
215160
}

0 commit comments

Comments
 (0)