Skip to content

Commit

Permalink
Enhance debug logging and refactor execution interception
Browse files Browse the repository at this point in the history
Increased the detail and scope of debug information during function execution. Refactored the interception process by handling the case when intercept info was not found, improving the overall flow and error handling. Removed some hard-coded function skipping, streamlining the process.
  • Loading branch information
koriym committed Jul 11, 2024
1 parent a725f85 commit 10b183e
Showing 1 changed file with 78 additions and 43 deletions.
121 changes: 78 additions & 43 deletions rayaop.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,68 +223,103 @@ void php_rayaop_free_intercept_info(zval *zv) {
*/

static void php_rayaop_execute_ex(zend_execute_data *execute_data) {
if (execute_data->func && execute_data->func->common.function_name) {
const char *fname = ZSTR_VAL(execute_data->func->common.function_name);
const char *class_name = execute_data->func->common.scope ? ZSTR_VAL(execute_data->func->common.scope->name) : NULL;

// Skip specific functions
if (class_name && strcmp(class_name, "PhpToken") == 0 && strcmp(fname, "tokenize") == 0) {
PHP_RAYAOP_DEBUG_PRINT("Skipping PhpToken::tokenize");
php_rayaop_original_execute_ex(execute_data);
return;
}

if (class_name && strcmp(class_name, "PhpParser\\Parser\\Php8") == 0 && strcmp(fname, "{closure}") == 0) {
PHP_RAYAOP_DEBUG_PRINT("Skipping PhpParser\\Parser\\Php8::{closure}");
php_rayaop_original_execute_ex(execute_data);
return;
}

if (class_name && strcmp(class_name, "PHPUnit\\Framework\\TestCase") == 0 && strcmp(fname, "startOutputBuffering") == 0) {
PHP_RAYAOP_DEBUG_PRINT("Skipping PHPUnit\\Framework\\TestCase::startOutputBuffering");
php_rayaop_original_execute_ex(execute_data);
return;
}
}

PHP_RAYAOP_DEBUG_PRINT("php_rayaop_execute_ex called");
PHP_RAYAOP_DEBUG_PRINT("php_rayaop_execute_ex called"); /* Output debug information */

// 実行深度を増加させ、ログに記録
RAYAOP_G(execution_depth)++;

PHP_RAYAOP_DEBUG_PRINT("Execution depth: %d", RAYAOP_G(execution_depth));

if (!php_rayaop_should_intercept(execute_data)) {
PHP_RAYAOP_DEBUG_PRINT("Interception not necessary, calling original execute_ex function");
php_rayaop_original_execute_ex(execute_data);

// Debug information about execute_data
if (execute_data == NULL) {
PHP_RAYAOP_DEBUG_PRINT("execute_data is NULL");
} else {
if (execute_data->func == NULL) {
PHP_RAYAOP_DEBUG_PRINT("execute_data->func is NULL");
} else {
if (execute_data->func->common.function_name == NULL) {
PHP_RAYAOP_DEBUG_PRINT("execute_data->func->common.function_name is NULL");
} else {
PHP_RAYAOP_DEBUG_PRINT("Function name: %s", ZSTR_VAL(execute_data->func->common.function_name));
}

if (execute_data->func->common.scope == NULL) {
PHP_RAYAOP_DEBUG_PRINT("execute_data->func->common.scope is NULL");
} else {
PHP_RAYAOP_DEBUG_PRINT("Class name: %s", ZSTR_VAL(execute_data->func->common.scope->name));
}
}
}

PHP_RAYAOP_DEBUG_PRINT("Calling original execute_ex function");

if (php_rayaop_original_execute_ex == NULL) {
PHP_RAYAOP_DEBUG_PRINT("php_rayaop_original_execute_ex is NULL");
}

php_rayaop_original_execute_ex(execute_data); /* Call the original execution function */
RAYAOP_G(execution_depth)--;
return;

return; /* End processing */
}

zend_function *current_function = execute_data->func;
zend_string *class_name = current_function->common.scope->name;
zend_string *method_name = current_function->common.function_name;
zend_function *current_function = execute_data->func; /* Get current function information */
zend_string *class_name = current_function->common.scope->name; /* Get class name */
zend_string *method_name = current_function->common.function_name; /* Get method name */

PHP_RAYAOP_DEBUG_PRINT("Function: %s::%s", ZSTR_VAL(class_name), ZSTR_VAL(method_name));

size_t key_len;
char *key = php_rayaop_generate_intercept_key(class_name, method_name, &key_len);
char *key = php_rayaop_generate_intercept_key(class_name, method_name, &key_len); /* Generate intercept key */

php_rayaop_intercept_info *info = php_rayaop_find_intercept_info(key, key_len);
php_rayaop_intercept_info *info = php_rayaop_find_intercept_info(key, key_len); /* Search for intercept information */

if (info) {
PHP_RAYAOP_DEBUG_PRINT("Found intercept info for key: %s", key);
if (RAYAOP_G(is_intercepting)) {
PHP_RAYAOP_DEBUG_PRINT("Already intercepting, skipping interception");
php_rayaop_original_execute_ex(execute_data);
efree(key);
return;
/* If intercept information is found */
PHP_RAYAOP_DEBUG_PRINT("Found intercept info for key: %s", key); /* Output debug information */
php_rayaop_execute_intercept(execute_data, info); /* Execute interception */
} else {
/* If intercept information is not found */
PHP_RAYAOP_DEBUG_PRINT("No intercept info found for key: %s", key); /* Output debug information */
PHP_RAYAOP_DEBUG_PRINT("Calling original execute_ex function");

// Debug information about execute_data before calling the original function
if (execute_data == NULL) {
PHP_RAYAOP_DEBUG_PRINT("execute_data is NULL");
} else {
if (execute_data->func == NULL) {
PHP_RAYAOP_DEBUG_PRINT("execute_data->func is NULL");
} else {
if (execute_data->func->common.function_name == NULL) {
PHP_RAYAOP_DEBUG_PRINT("execute_data->func->common.function_name is NULL");
} else {
PHP_RAYAOP_DEBUG_PRINT("Function name: %s", ZSTR_VAL(execute_data->func->common.function_name));
}

if (execute_data->func->common.scope == NULL) {
PHP_RAYAOP_DEBUG_PRINT("execute_data->func->common.scope is NULL");
} else {
PHP_RAYAOP_DEBUG_PRINT("Class name: %s", ZSTR_VAL(execute_data->func->common.scope->name));
}
}
}

RAYAOP_G(is_intercepting) = 1;
php_rayaop_execute_intercept(execute_data, info);
RAYAOP_G(is_intercepting) = 0;
} else {
PHP_RAYAOP_DEBUG_PRINT("No intercept info found for key: %s", key);
if (php_rayaop_original_execute_ex == NULL) {
PHP_RAYAOP_DEBUG_PRINT("php_rayaop_original_execute_ex is NULL");
}
PHP_RAYAOP_DEBUG_PRINT("Origilan method calling...");
if (execute_data == NULL || execute_data->func == NULL || php_rayaop_original_execute_ex == NULL) {
PHP_RAYAOP_DEBUG_PRINT("Invalid pointers detected");
// エラー処理
return;
}
PHP_RAYAOP_DEBUG_PRINT("Calling original execute_ex. execute_data: %p, func: %p, original_execute_ex: %p",
(void*)execute_data,
(void*)(execute_data ? execute_data->func : NULL),
(void*)php_rayaop_original_execute_ex);
php_rayaop_original_execute_ex(execute_data);
/* Call the original execution function */
PHP_RAYAOP_DEBUG_PRINT("Origilan method called sucsessfully!");
Expand Down

0 comments on commit 10b183e

Please sign in to comment.