Skip to content

Commit

Permalink
use ZEND_OP_ARRAY_EXTENSION to store wraprec
Browse files Browse the repository at this point in the history
  • Loading branch information
lavarou committed Jan 31, 2025
1 parent 6891952 commit 0ac2a0b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
13 changes: 10 additions & 3 deletions agent/php_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@
* conditions wherever possible.
*/

static inline nruserfn_t* nr_php_get_wraprec_from_op_array_extension(const char* fn, zend_function* zf) {
nruserfn_t* wraprec = (nruserfn_t*)ZEND_OP_ARRAY_EXTENSION(&zf->op_array, NR_PHP_PROCESS_GLOBALS(op_array_extension_handle));
nrl_verbosedebug(NRL_AGENT, "%s from %s, op_array_extension=%p, wraprec=%p, wraprec->pid=%d", __func__, fn, ZEND_OP_ARRAY_EXTENSION(&zf->op_array, NR_PHP_PROCESS_GLOBALS(op_array_extension_handle)), wraprec, wraprec->pid);
return wraprec;
}

static void nr_php_show_exec_return(NR_EXECUTE_PROTO TSRMLS_DC);
static int nr_php_show_exec_indentation(TSRMLS_D);
static void nr_php_show_exec(NR_EXECUTE_PROTO TSRMLS_DC);
Expand Down Expand Up @@ -589,6 +595,7 @@ static void nr_php_show_exec(NR_EXECUTE_PROTO TSRMLS_DC) {
char argstr[NR_EXECUTE_DEBUG_STRBUFSZ];
const char* filename = nr_php_op_array_file_name(NR_OP_ARRAY);
const char* function_name = nr_php_op_array_function_name(NR_OP_ARRAY);
nruserfn_t* wr = nr_php_get_wraprec_from_op_array_extension(__func__, execute_data->func);

argstr[0] = '\0';

Expand All @@ -612,7 +619,7 @@ static void nr_php_show_exec(NR_EXECUTE_PROTO TSRMLS_DC) {
#if ZEND_MODULE_API_NO < ZEND_7_4_X_API_NO
nr_php_op_array_get_wraprec(NR_OP_ARRAY TSRMLS_CC) ? " *" : "",
#else
nr_php_get_wraprec(execute_data->func) ? " *" : "",
wr ? " *" : "",
#endif
NRP_FILENAME(filename), NR_OP_ARRAY->line_start);
} else if (NR_OP_ARRAY->function_name) {
Expand All @@ -633,7 +640,7 @@ static void nr_php_show_exec(NR_EXECUTE_PROTO TSRMLS_DC) {
#if ZEND_MODULE_API_NO < ZEND_7_4_X_API_NO
nr_php_op_array_get_wraprec(NR_OP_ARRAY TSRMLS_CC) ? " *" : "",
#else
nr_php_get_wraprec(execute_data->func) ? " *" : "",
wr ? " *" : "",
#endif
NRP_FILENAME(filename), NR_OP_ARRAY->line_start);
} else if (NR_OP_ARRAY->filename) {
Expand Down Expand Up @@ -1930,7 +1937,7 @@ static void nr_php_instrument_func_begin(NR_EXECUTE_PROTO) {
*/
nr_php_observer_attempt_call_cufa_handler(NR_EXECUTE_ORIG_ARGS);
}
wraprec = nr_php_get_wraprec(execute_data->func);
wraprec = nr_php_get_wraprec_from_op_array_extension(__func__, execute_data->func);

segment = nr_segment_start(NRPRG(txn), NULL, NULL);

Expand Down
4 changes: 1 addition & 3 deletions agent/php_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ typedef struct _nrphpglobals_t {
* variable with the key `NEW_RELIC_LABELS` */
#if ZEND_MODULE_API_NO >= ZEND_8_1_X_API_NO /* PHP 8.1+ */
zend_long zend_offset; /* Zend extension offset */
zend_long
zend_op_array_offset; /* Zend extension op_array to modify reserved */
#else
int zend_offset; /* Zend extension offset */
int zend_op_array_offset; /* Zend extension op_array to modify reserved */
#endif
int op_array_extension_handle; /* Zend op_array extension handle to attach agent's data to function */
int done_instrumentation; /* Set to true if we have installed instrumentation
handlers */
nrtime_t expensive_min; /* newrelic.special.expensive_node_min */
Expand Down
5 changes: 5 additions & 0 deletions agent/php_minit.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,11 @@ PHP_MINIT_FUNCTION(newrelic) {
nrl_debug(NRL_INIT, "MINIT processing done");
#if ZEND_MODULE_API_NO >= ZEND_8_0_X_API_NO /* PHP 7.4+ */
NR_PHP_PROCESS_GLOBALS(zend_offset) = zend_get_resource_handle(dummy);
#if ZEND_MODULE_API_NO >= ZEND_8_4_X_API_NO /* PHP 7.4+ */
NR_PHP_PROCESS_GLOBALS(op_array_extension_handle) = zend_get_internal_function_extension_handle("newrelic");
#else
NR_PHP_PROCESS_GLOBALS(op_array_extension_handle) = zend_get_op_array_extension_handle("newrelic");
#endif
#else
NR_PHP_PROCESS_GLOBALS(zend_offset) = zend_get_resource_handle(&dummy);
#endif
Expand Down
14 changes: 14 additions & 0 deletions agent/php_observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,27 @@
static zend_observer_fcall_handlers nr_php_fcall_register_handlers(
zend_execute_data* execute_data) {
zend_observer_fcall_handlers handlers = {NULL, NULL};
nruserfn_t* wr;
if (NULL == execute_data) {
return handlers;
}
if ((NULL == execute_data->func)
|| (ZEND_INTERNAL_FUNCTION == execute_data->func->type)) {
return handlers;
}

if (!OP_ARRAY_IS_A_FILE(NR_OP_ARRAY)) {
nrl_verbosedebug(NRL_AGENT, "Registering Observer API handlers for user function %s",
NRSAFESTR(nr_php_op_array_function_name(NR_OP_ARRAY)));
} else {
nrl_verbosedebug(NRL_AGENT, "Registering Observer API handlers for file %s",
NRSAFESTR(nr_php_op_array_file_name(NR_OP_ARRAY)));
}

wr = nr_php_get_wraprec(execute_data->func);
// store the wraprec in the op_array extension for the duration of the request for later lookup
ZEND_OP_ARRAY_EXTENSION(&execute_data->func->op_array, NR_PHP_PROCESS_GLOBALS(op_array_extension_handle)) = wr;

handlers.begin = nr_php_observer_fcall_begin;
handlers.end = nr_php_observer_fcall_end;
return handlers;
Expand Down
8 changes: 7 additions & 1 deletion agent/php_user_instrument.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ static nr_php_wraprec_hashmap_t* user_function_wrappers;
static inline void nr_php_wraprec_lookup_set(nruserfn_t* wr,
zend_function* zf) {
nr_php_wraprec_hashmap_update(user_function_wrappers, zf, wr);
// store the wraprec in the op_array extension for the duration of the request for later lookup
// for situation when wraprec is added after first execution of the function
ZEND_OP_ARRAY_EXTENSION(&zf->op_array, NR_PHP_PROCESS_GLOBALS(op_array_extension_handle)) = wr;

}
static inline nruserfn_t* nr_php_wraprec_lookup_get(zend_function* zf) {
nruserfn_t* wraprec = NULL;
Expand Down Expand Up @@ -291,7 +295,9 @@ static void nr_php_wrap_user_function_internal(nruserfn_t* wraprec TSRMLS_DC) {
}

static nruserfn_t* nr_php_user_wraprec_create(void) {
return (nruserfn_t*)nr_zalloc(sizeof(nruserfn_t));
nruserfn_t* wr = (nruserfn_t*)nr_zalloc(sizeof(nruserfn_t));
wr->pid = nr_getpid();
return wr;
}

static nruserfn_t* nr_php_user_wraprec_create_named(const char* full_name,
Expand Down
1 change: 1 addition & 0 deletions agent/php_user_instrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ typedef struct _nruserfn_t {
#if ZEND_MODULE_API_NO >= ZEND_7_4_X_API_NO
char* wordpress_plugin_theme;
#endif
int pid; /* pid of a process that created this wraprec */
} nruserfn_t;

extern nruserfn_t* nr_wrapped_user_functions; /* a singly linked list */
Expand Down

0 comments on commit 0ac2a0b

Please sign in to comment.