Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat join cut get caller #64

Merged
merged 3 commits into from
May 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(php): pinpoint_get_caller_arg
- pinpoint_get_caller_arg and phpt
eeliu committed May 30, 2024
commit 8918483dc658f876667da7b1384bb809214afb2e
1 change: 1 addition & 0 deletions src/PHP/php_pinpoint_php.h
Original file line number Diff line number Diff line change
@@ -81,6 +81,7 @@ PHP_FUNCTION(_pinpoint_add_clue);
PHP_FUNCTION(_pinpoint_add_clues);
PHP_FUNCTION(_pinpoint_unique_id);
PHP_FUNCTION(pinpoint_get_this);
PHP_FUNCTION(pinpoint_get_caller_arg);
PHP_FUNCTION(_pinpoint_trace_limit);
PHP_FUNCTION(_pinpoint_drop_trace);
PHP_FUNCTION(_pinpoint_start_time);
71 changes: 71 additions & 0 deletions src/PHP/pinpoint_php.cpp
Original file line number Diff line number Diff line change
@@ -125,6 +125,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_add_timestamp, 0, 0, 0)
ZEND_ARG_INFO(0, timestamp)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_add_arg_index, 0, 0, 0)
ZEND_ARG_INFO(0, index)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_add_id, 0, 0, 0)
ZEND_ARG_INFO(0, nodeid)
ZEND_END_ARG_INFO()
@@ -146,6 +150,7 @@ const zend_function_entry pinpoint_php_functions[] = {
PHP_FE(_pinpoint_end_trace, arginfo_add_id)
PHP_FE(_pinpoint_unique_id, arginfo_none)
PHP_FE(pinpoint_get_this, arginfo_none)
PHP_FE(pinpoint_get_caller_arg,arginfo_add_arg_index)
PHP_FE(pinpoint_status, arginfo_none)
// PHP__FE(pinpoint_get_func_ref_args, arginfo_none)
PHP_FE(_pinpoint_drop_trace, arginfo_add_id)
@@ -271,6 +276,72 @@ PHP_FUNCTION(_pinpoint_set_context) {
RETURN_TRUE;
}

// ref from ZEND_FUNCTION(func_get_arg)
PHP_FUNCTION(pinpoint_get_caller_arg) {
uint32_t arg_count, first_extra_arg;
zval *arg;
zend_long requested_offset;
zend_execute_data *ex;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &requested_offset) ==
FAILURE) {
return;
}

if (requested_offset < 0) {
zend_error(
E_WARNING,
"pinpoint_get_caller_arg(): The argument number should be >= 0");
RETURN_FALSE;
}

// changes
// ex = EX(prev_execute_data)
ex = EX(prev_execute_data)->prev_execute_data;
if (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE) {
zend_error(E_WARNING, "pinpoint_get_caller_arg(): Called from the global "
"scope - no function context");
RETURN_FALSE;
}
#if PHP_MAJOR_VERSION == 8 && PHP_MINOR_VERSION >= 2
if (zend_forbid_dynamic_call() == FAILURE) {
RETURN_THROWS();
}
#else
if (zend_forbid_dynamic_call("pinpoint_get_caller_arg()") == FAILURE) {
RETURN_FALSE;
}
#endif
arg_count = ZEND_CALL_NUM_ARGS(ex);

if ((zend_ulong)requested_offset >= arg_count) {
zend_error(E_WARNING,
"pinpoint_get_caller_arg(): Argument " ZEND_LONG_FMT
" not passed to function",
requested_offset);
RETURN_FALSE;
}

first_extra_arg = ex->func->op_array.num_args;
if ((zend_ulong)requested_offset >= first_extra_arg &&
(ZEND_CALL_NUM_ARGS(ex) > first_extra_arg)) {
arg = ZEND_CALL_VAR_NUM(ex, ex->func->op_array.last_var +
ex->func->op_array.T) +
(requested_offset - first_extra_arg);
} else {
arg = ZEND_CALL_ARG(ex, requested_offset + 1);
}
if (EXPECTED(!Z_ISUNDEF_P(arg))) {

#if PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION <= 2
ZVAL_DEREF(arg);
ZVAL_COPY(return_value, arg);
#else
ZVAL_COPY_DEREF(return_value, arg);
#endif
}
}

PHP_FUNCTION(_pinpoint_get_context) {
long _id = -1;
std::string key;
122 changes: 0 additions & 122 deletions src/PHP/pinpoint_php_api.php

This file was deleted.

49 changes: 49 additions & 0 deletions tests/pinpoint_get_caller_arg.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--TEST--
pinpoint_php test pinpoint_get_caller_arg
--SKIPIF--
<?php if (!extension_loaded("pinpoint_php")) print "skip"; ?>
--INI--
pinpoint_php.DebugReport=true
--FILE--
<?php

_pinpoint_join_cut(
["curl_exec"],
function ($a) {
},
function ($ret) {
$ch = pinpoint_get_caller_arg(0);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "status_code $code \n";
},
function ($e) {
}
);

echo "case: curl_init() \n";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://httpbin.org/anything");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'user_header:"xxxx"',
'user_abc:2133'
]);
$response = curl_exec($ch);

$j_res = json_decode($response, true);

$error = curl_error($ch);
echo "error: $error\n";
curl_close($ch);

--EXPECTF--
[pinpoint] [%d] [%d]try to interceptor function=curl_exec
[pinpoint] [%d] [%d]added interceptor on `function`: curl_exec success
case: curl_init()
[pinpoint] [%d] [%d]pinpoint_interceptor_handler_entry: handle func/method:curl_exec
[pinpoint] [%d] [%d] call_callback_function on_before return type(1) zval
[pinpoint] [%d] [%d]replace_ex_caller_parameters return value must be `an array`
status_code 200
error:
[pinpoint] [%d] [%d]start free interceptor: curl_exec