Skip to content

Commit afa2b3c

Browse files
authored
Merge pull request #2361 from DataDog/glopes/exec-integration
Add exec integration
2 parents 9f4cb76 + f4edf58 commit afa2b3c

31 files changed

+1879
-8
lines changed

bridge/_files_integrations.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
__DIR__ . '/../src/Integrations/Integrations/AMQP/AMQPIntegration.php',
1212
__DIR__ . '/../src/Integrations/Integrations/CakePHP/CakePHPIntegration.php',
1313
__DIR__ . '/../src/Integrations/Integrations/CodeIgniter/V2/CodeIgniterIntegration.php',
14+
__DIR__ . '/../src/Integrations/Integrations/Exec/ExecIntegration.php',
1415
__DIR__ . '/../src/Integrations/Integrations/Drupal/DrupalIntegration.php',
1516
__DIR__ . '/../src/Integrations/Integrations/Web/WebIntegration.php',
1617
__DIR__ . '/../src/Integrations/Integrations/IntegrationsLoader.php',

config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ if test "$PHP_DDTRACE" != "no"; then
142142
ext/handlers_exception.c \
143143
ext/handlers_internal.c \
144144
ext/handlers_pcntl.c \
145+
ext/integrations/exec_integration.c \
145146
ext/integrations/integrations.c \
146147
ext/ip_extraction.c \
147148
ext/logging.c \

ext/ddtrace.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "excluded_modules.h"
4848
#include "handlers_http.h"
4949
#include "handlers_internal.h"
50+
#include "integrations/exec_integration.h"
5051
#include "integrations/integrations.h"
5152
#include "ip_extraction.h"
5253
#include "logging.h"
@@ -1100,6 +1101,10 @@ static PHP_RSHUTDOWN_FUNCTION(ddtrace) {
11001101

11011102
zend_hash_destroy(&DDTRACE_G(traced_spans));
11021103

1104+
// this needs to be done before dropping the spans
1105+
// run unconditionally because ddtrace may've been disabled mid-request
1106+
ddtrace_exec_handlers_rshutdown();
1107+
11031108
if (get_DD_TRACE_ENABLED()) {
11041109
dd_force_shutdown_tracing();
11051110
} else if (!DDTRACE_G(disable)) {

ext/handlers_internal.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "arrays.h"
44
#include "ddtrace.h"
5+
#include "integrations/exec_integration.h"
56

67
void ddtrace_free_unregistered_class(zend_class_entry *ce) {
78
#if PHP_VERSION_ID >= 80100
@@ -108,6 +109,11 @@ static void dd_install_internal_handlers(void) {
108109
dd_install_internal_function("curl_exec");
109110
dd_install_internal_function("pcntl_fork");
110111
dd_install_internal_function("pcntl_rfork");
112+
dd_install_internal_function("DDTrace\\Integrations\\Exec\\register_stream");
113+
dd_install_internal_function("DDTrace\\Integrations\\Exec\\proc_assoc_span");
114+
dd_install_internal_function("DDTrace\\Integrations\\Exec\\proc_get_span");
115+
dd_install_internal_function("DDTrace\\Integrations\\Exec\\proc_get_pid");
116+
dd_install_internal_function("DDTrace\\Integrations\\Exec\\test_rshutdown");
111117
}
112118
#endif
113119

@@ -121,7 +127,7 @@ void ddtrace_exception_handlers_rinit(void);
121127

122128
void ddtrace_curl_handlers_rshutdown(void);
123129

124-
void ddtrace_internal_handlers_startup(void) {
130+
void ddtrace_internal_handlers_startup() {
125131
// On PHP 8.0 zend_execute_internal is not executed in JIT. Manually ensure internal hooks are executed.
126132
#if PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80200
127133
#if PHP_VERSION_ID >= 80100
@@ -139,6 +145,8 @@ void ddtrace_internal_handlers_startup(void) {
139145
ddtrace_pcntl_handlers_startup();
140146
// exception handlers have to run otherwise wrapping will fail horribly
141147
ddtrace_exception_handlers_startup();
148+
149+
ddtrace_exec_handlers_startup();
142150
}
143151

144152
void ddtrace_internal_handlers_shutdown(void) {
@@ -150,11 +158,18 @@ void ddtrace_internal_handlers_shutdown(void) {
150158
#if PHP_VERSION_ID < 80000
151159
ddtrace_curl_handlers_shutdown();
152160
#endif
161+
162+
ddtrace_exec_handlers_shutdown();
153163
}
154164

155165
void ddtrace_internal_handlers_rinit(void) {
156166
ddtrace_curl_handlers_rinit();
157167
ddtrace_exception_handlers_rinit();
168+
ddtrace_exec_handlers_rinit();
158169
}
159170

160-
void ddtrace_internal_handlers_rshutdown(void) { ddtrace_curl_handlers_rshutdown(); }
171+
void ddtrace_internal_handlers_rshutdown(void) {
172+
ddtrace_curl_handlers_rshutdown();
173+
// called earlier in zm_deactivate_ddtrace
174+
// ddtrace_exec_handlers_rshutdown();
175+
}

ext/hook/uhook.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ typedef struct {
5858
zval *retval_ptr;
5959
ddtrace_span_data *span;
6060
ddtrace_span_stack *prior_stack;
61+
bool returns_reference;
6162
} dd_hook_data;
6263

6364
typedef struct {
@@ -225,6 +226,7 @@ static bool dd_uhook_begin(zend_ulong invocation, zend_execute_data *execute_dat
225226
}
226227

227228
dyn->hook_data = (dd_hook_data *)dd_hook_data_create(ddtrace_hook_data_ce);
229+
dyn->hook_data->returns_reference = execute_data->func->common.fn_flags & ZEND_ACC_RETURN_REFERENCE;
228230
dyn->hook_data->vm_stack_top = EG(vm_stack_top);
229231

230232
dyn->hook_data->invocation = invocation;
@@ -775,6 +777,12 @@ ZEND_METHOD(DDTrace_HookData, overrideReturnValue) {
775777
RETURN_FALSE;
776778
}
777779

780+
if (hookData->returns_reference) {
781+
ZVAL_MAKE_REF(retval);
782+
} else {
783+
ZVAL_DEREF(retval);
784+
}
785+
778786
zval_ptr_dtor(hookData->retval_ptr);
779787
ZVAL_COPY(hookData->retval_ptr, retval);
780788

0 commit comments

Comments
 (0)