Skip to content

Commit c3be656

Browse files
authored
Enable dynamic configuration for debugger-related products (#3476)
* Enable dynamic configuration for debugger-related products - Dynamic instrumentation - Code origins - Exception replay Signed-off-by: Bob Weinand <[email protected]> * Fixup libdatadog updates Signed-off-by: Bob Weinand <[email protected]> * Fix distinguishing modified vs unmodified Signed-off-by: Bob Weinand <[email protected]> * Update libdatadog once more --------- Signed-off-by: Bob Weinand <[email protected]>
1 parent 610ad43 commit c3be656

24 files changed

+466
-244
lines changed

Cargo.lock

Lines changed: 93 additions & 152 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ RUN_TESTS_CMD := DD_SERVICE= DD_ENV= REPORT_EXIT_STATUS=1 TEST_PHP_SRCDIR=$(PROJ
4848

4949
C_FILES = $(shell find components components-rs ext src/dogstatsd zend_abstract_interface -name '*.c' -o -name '*.h' | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' )
5050
TEST_FILES = $(shell find tests/ext -name '*.php*' -o -name '*.inc' -o -name '*.json' -o -name '*.yaml' -o -name 'CONFLICTS' | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' )
51-
RUST_FILES = $(BUILD_DIR)/Cargo.toml $(BUILD_DIR)/Cargo.lock $(shell find components-rs -name '*.c' -o -name '*.rs' -o -name 'Cargo.toml' | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' ) $(shell find libdatadog/{build-common,datadog-crashtracker,datadog-crashtracker-ffi,datadog-ipc,datadog-ipc-macros,datadog-live-debugger,datadog-live-debugger-ffi,datadog-remote-config,datadog-sidecar,datadog-sidecar-ffi,datadog-sidecar-macros,libdd-alloc,libdd-common,libdd-common-ffi,libdd-data-pipeline,libdd-ddsketch,libdd-dogstatsd-client,libdd-library-config,libdd-library-config-ffi,libdd-log,libdd-telemetry,libdd-telemetry-ffi,libdd-tinybytes,libdd-trace-*,spawn_worker,tools/{cc_utils,sidecar_mockgen},datadog-trace-*,Cargo.toml} \( -type l -o -type f \) \( -path "*/src*" -o -path "*/examples*" -o -path "*Cargo.toml" -o -path "*/build.rs" -o -path "*/tests/dataservice.rs" -o -path "*/tests/service_functional.rs" \) -not -path "*/datadog-ipc/build.rs" -not -path "*/datadog-sidecar-ffi/build.rs")
51+
RUST_FILES = $(BUILD_DIR)/Cargo.toml $(BUILD_DIR)/Cargo.lock $(shell find components-rs -name '*.c' -o -name '*.rs' -o -name 'Cargo.toml' | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' ) $(shell find libdatadog/{build-common,datadog-ipc,datadog-ipc-macros,datadog-live-debugger,datadog-live-debugger-ffi,datadog-remote-config,datadog-sidecar,datadog-sidecar-ffi,datadog-sidecar-macros,libdd-alloc,libdd-common,libdd-common-ffi,libdd-crashtracker,libdd-crashtracker-ffi,libdd-data-pipeline,libdd-ddsketch,libdd-dogstatsd-client,libdd-library-config,libdd-library-config-ffi,libdd-log,libdd-telemetry,libdd-telemetry-ffi,libdd-tinybytes,libdd-trace-*,spawn_worker,tools/{cc_utils,sidecar_mockgen},datadog-trace-*,Cargo.toml} \( -type l -o -type f \) \( -path "*/src*" -o -path "*/examples*" -o -path "*Cargo.toml" -o -path "*/build.rs" -o -path "*/tests/dataservice.rs" -o -path "*/tests/service_functional.rs" \) -not -path "*/datadog-ipc/build.rs" -not -path "*/datadog-sidecar-ffi/build.rs")
5252
ALL_OBJECT_FILES = $(C_FILES) $(RUST_FILES) $(BUILD_DIR)/Makefile
5353
TEST_OPCACHE_FILES = $(shell find tests/opcache -name '*.php*' -o -name '.gitkeep' | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' )
5454
TEST_STUB_FILES = $(shell find tests/ext -type d -name 'stubs' -exec find '{}' -type f \; | awk '{ printf "$(BUILD_DIR)/%s\n", $$1 }' )
@@ -436,8 +436,8 @@ generate_cbindgen: cbindgen_binary # Regenerate components-rs/ddtrace.h componen
436436
$(command rustup && echo run nightly --) cbindgen --crate datadog-sidecar-ffi \
437437
--config datadog-sidecar-ffi/cbindgen.toml \
438438
--output $(PROJECT_ROOT)/components-rs/sidecar.h; \
439-
$(command rustup && echo run nightly --) cbindgen --crate datadog-crashtracker-ffi \
440-
--config datadog-crashtracker-ffi/cbindgen.toml \
439+
$(command rustup && echo run nightly --) cbindgen --crate libdd-crashtracker-ffi \
440+
--config libdd-crashtracker-ffi/cbindgen.toml \
441441
--output $(PROJECT_ROOT)/components-rs/crashtracker.h; \
442442
$(command rustup && echo run nightly --) cbindgen --crate libdd-library-config-ffi \
443443
--config libdd-library-config-ffi/cbindgen.toml \

cbindgen.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ include_guard = "DDTRACE_PHP_H"
44
style = "both"
55
header = """
66
struct _zend_string;
7+
typedef struct _zend_string _zend_string;
78
"""
89

910
no_includes = true

components-rs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ datadog-sidecar = { path = "../libdatadog/datadog-sidecar" }
2020
datadog-sidecar-ffi = { path = "../libdatadog/datadog-sidecar-ffi" }
2121
libdd-tinybytes = { path = "../libdatadog/libdd-tinybytes" }
2222
libdd-trace-utils = { path = "../libdatadog/libdd-trace-utils" }
23-
datadog-crashtracker-ffi = { path = "../libdatadog/datadog-crashtracker-ffi", default-features = false, features = ["collector"] }
23+
libdd-crashtracker-ffi = { path = "../libdatadog/libdd-crashtracker-ffi", default-features = false, features = ["collector"] }
2424
libdd-library-config-ffi = { path = "../libdatadog/libdd-library-config-ffi", default-features = false }
2525
spawn_worker = { path = "../libdatadog/spawn_worker" }
2626
anyhow = { version = "1.0" }

components-rs/bytes.rs

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,45 @@ pub struct ZendString {
1616
pub val: [u8; 1],
1717
}
1818

19+
#[repr(transparent)]
20+
pub struct OwnedZendString(pub NonNull<ZendString>);
21+
22+
impl OwnedZendString {
23+
pub fn from_copy(mut ptr: NonNull<ZendString>) -> Self {
24+
unsafe { DDOG_ADDREF_ZEND_STRING.unwrap_unchecked()(ptr.as_mut()) };
25+
OwnedZendString(ptr)
26+
}
27+
}
28+
29+
impl Drop for OwnedZendString {
30+
fn drop(&mut self) {
31+
unsafe {
32+
DDOG_FREE_ZEND_STRING.unwrap_unchecked()(OwnedZendString(self.0));
33+
}
34+
}
35+
}
36+
37+
impl Clone for OwnedZendString {
38+
fn clone(&self) -> Self {
39+
OwnedZendString::from_copy(self.0)
40+
}
41+
}
42+
1943
static mut DDOG_ADDREF_ZEND_STRING: Option<extern "C" fn(&mut ZendString)> = None;
44+
static mut DDOG_INIT_ZEND_STRING: Option<extern "C" fn(CharSlice) -> OwnedZendString> = None;
45+
static mut DDOG_FREE_ZEND_STRING: Option<extern "C" fn(OwnedZendString)> = None;
2046

2147
static mut REFCOUNTED_CELL_VTABLE: Option<RefCountedCellVTable> = None;
2248

2349
#[no_mangle]
2450
pub unsafe extern "C" fn ddog_init_span_func(
25-
free_func: extern "C" fn(&mut ZendString),
51+
free_func: extern "C" fn(OwnedZendString),
2652
addref_func: extern "C" fn(&mut ZendString),
53+
init_func: extern "C" fn(CharSlice) -> OwnedZendString,
2754
) {
2855
DDOG_ADDREF_ZEND_STRING = Some(addref_func);
56+
DDOG_INIT_ZEND_STRING = Some(init_func);
57+
DDOG_FREE_ZEND_STRING = Some(free_func);
2958

3059
REFCOUNTED_CELL_VTABLE = Some(RefCountedCellVTable {
3160
clone,
@@ -38,6 +67,42 @@ pub unsafe extern "C" fn ddog_init_span_func(
3867
}
3968
}
4069

70+
pub fn u8_from_zend_string(str: &ZendString) -> &[u8] {
71+
unsafe { std::slice::from_raw_parts(str.val.as_ptr(), str.len) }
72+
}
73+
74+
impl AsRef<[u8]> for OwnedZendString {
75+
fn as_ref(&self) -> &[u8] {
76+
unsafe { self.0.as_ref() }.as_ref()
77+
}
78+
}
79+
80+
impl AsRef<[u8]> for ZendString {
81+
fn as_ref(&self) -> &[u8] {
82+
u8_from_zend_string(self)
83+
}
84+
}
85+
86+
impl Into<OwnedZendString> for &str {
87+
fn into(self) -> OwnedZendString {
88+
init_zend_string(self.as_bytes())
89+
}
90+
}
91+
92+
impl Into<OwnedZendString> for &[u8] {
93+
fn into(self) -> OwnedZendString {
94+
init_zend_string(self)
95+
}
96+
}
97+
98+
pub fn init_zend_string(str: &[u8]) -> OwnedZendString {
99+
unsafe { DDOG_INIT_ZEND_STRING.unwrap_unchecked()(CharSlice::from_bytes(str)) }
100+
}
101+
102+
pub unsafe fn dangling_zend_string() -> OwnedZendString {
103+
OwnedZendString(NonNull::dangling())
104+
}
105+
41106
fn convert_to_bytes(zend_str: &mut ZendString) -> Bytes {
42107
unsafe {
43108
DDOG_ADDREF_ZEND_STRING.unwrap_unchecked()(zend_str); // Increment the reference count to prevent double free
@@ -52,14 +117,6 @@ fn convert_to_bytes(zend_str: &mut ZendString) -> Bytes {
52117
}
53118
}
54119

55-
fn convert_literal_to_bytes(string: *const c_char) -> Bytes {
56-
unsafe {
57-
let cstring = CStr::from_ptr(string);
58-
59-
Bytes::from_static(cstring.to_bytes())
60-
}
61-
}
62-
63120
fn convert_zend_to_bytes_string(zend_str: &mut ZendString) -> BytesString {
64121
unsafe {
65122
match String::from_utf8_lossy(std::slice::from_raw_parts(

components-rs/common.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ typedef struct ddog_Vec_Tag_ParseResult {
257257
struct ddog_Error *error_message;
258258
} ddog_Vec_Tag_ParseResult;
259259

260+
typedef struct _zend_string _zend_string;
261+
262+
260263
#define ddog_LOG_ONCE (1 << 3)
261264

262265
#define ddog_MultiTargetFetcher_DEFAULT_CLIENTS_LIMIT 100
@@ -271,6 +274,13 @@ typedef enum ddog_ConfigurationOrigin {
271274
DDOG_CONFIGURATION_ORIGIN_FLEET_STABLE_CONFIG,
272275
} ddog_ConfigurationOrigin;
273276

277+
typedef enum ddog_DynamicConfigUpdateMode {
278+
DDOG_DYNAMIC_CONFIG_UPDATE_MODE_READ,
279+
DDOG_DYNAMIC_CONFIG_UPDATE_MODE_READ_WRITE,
280+
DDOG_DYNAMIC_CONFIG_UPDATE_MODE_WRITE,
281+
DDOG_DYNAMIC_CONFIG_UPDATE_MODE_RESTORE,
282+
} ddog_DynamicConfigUpdateMode;
283+
274284
typedef enum ddog_EvaluateAt {
275285
DDOG_EVALUATE_AT_ENTRY,
276286
DDOG_EVALUATE_AT_EXIT,
@@ -441,9 +451,11 @@ typedef struct ddog_Tag {
441451
const struct ddog_DslString *value;
442452
} ddog_Tag;
443453

444-
typedef struct ddog_Vec_CChar *(*ddog_DynamicConfigUpdate)(ddog_CharSlice config,
445-
ddog_CharSlice value,
446-
bool return_old);
454+
typedef struct _zend_string *ddog_OwnedZendString;
455+
456+
typedef struct _zend_string *(*ddog_DynamicConfigUpdate)(ddog_CharSlice config,
457+
ddog_OwnedZendString value,
458+
enum ddog_DynamicConfigUpdateMode mode);
447459

448460
typedef enum ddog_IntermediateValue_Tag {
449461
DDOG_INTERMEDIATE_VALUE_STRING,
@@ -750,6 +762,8 @@ typedef struct ddog_Vec_RemoteConfigCapabilities {
750762

751763
typedef struct ddog_Vec_RemoteConfigCapabilities ddog_VecRemoteConfigCapabilities;
752764

765+
#define ddog_DYANMIC_CONFIG_UPDATE_UNMODIFIED (_zend_string*)1
766+
753767
typedef struct ddog_DebuggerCapture ddog_DebuggerCapture;
754768
typedef struct ddog_DebuggerValue ddog_DebuggerValue;
755769

@@ -966,6 +980,12 @@ typedef struct ddog_AttributeAnyValueBytes ddog_AttributeAnyValueBytes;
966980
typedef struct ddog_AttributeArrayValueBytes ddog_AttributeArrayValueBytes;
967981

968982

983+
typedef enum ddog_DynamicInstrumentationConfigState {
984+
DDOG_DYNAMIC_INSTRUMENTATION_CONFIG_STATE_ENABLED,
985+
DDOG_DYNAMIC_INSTRUMENTATION_CONFIG_STATE_DISABLED,
986+
DDOG_DYNAMIC_INSTRUMENTATION_CONFIG_STATE_NOT_SET,
987+
} ddog_DynamicInstrumentationConfigState;
988+
969989
typedef struct ddog_AgentInfoReader ddog_AgentInfoReader;
970990

971991
typedef struct ddog_AgentRemoteConfigReader ddog_AgentRemoteConfigReader;

components-rs/ddtrace.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
struct _zend_string;
2-
3-
42
#ifndef DDTRACE_PHP_H
53
#define DDTRACE_PHP_H
64

@@ -82,7 +80,7 @@ bool ddog_remote_configs_service_env_change(struct ddog_RemoteConfigState *remot
8280

8381
bool ddog_remote_config_alter_dynamic_config(struct ddog_RemoteConfigState *remote_config,
8482
ddog_CharSlice config,
85-
ddog_CharSlice new_value);
83+
ddog_OwnedZendString new_value);
8684

8785
void ddog_setup_remote_config(ddog_DynamicConfigUpdate update_config,
8886
const struct ddog_LiveDebuggerSetup *setup);
@@ -183,8 +181,9 @@ ddog_MaybeError ddog_sidecar_telemetry_filter_flush(struct ddog_SidecarTransport
183181
ddog_CharSlice service,
184182
ddog_CharSlice env);
185183

186-
void ddog_init_span_func(void (*free_func)(struct _zend_string*),
187-
void (*addref_func)(struct _zend_string*));
184+
void ddog_init_span_func(void (*free_func)(ddog_OwnedZendString),
185+
void (*addref_func)(struct _zend_string*),
186+
ddog_OwnedZendString (*init_func)(ddog_CharSlice));
188187

189188
void ddog_set_span_service_zstr(ddog_SpanBytes *ptr, struct _zend_string *str);
190189

components-rs/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(internal_features)]
22
#![feature(allow_internal_unstable)]
33
#![feature(linkage)]
4+
#![allow(static_mut_refs)] // remove with move to Rust 2024 edition
45

56
pub mod log;
67
pub mod remote_config;
@@ -16,7 +17,7 @@ use std::ffi::c_char;
1617
use std::ptr::null_mut;
1718
use uuid::Uuid;
1819

19-
pub use datadog_crashtracker_ffi::*;
20+
pub use libdd_crashtracker_ffi::*;
2021
pub use libdd_library_config_ffi::*;
2122
pub use datadog_sidecar_ffi::*;
2223
use libdd_common::{parse_uri, Endpoint};

0 commit comments

Comments
 (0)