diff --git a/agent/Cargo.lock b/agent/Cargo.lock index 4ba96fec1d7..f0827b9ce84 100644 --- a/agent/Cargo.lock +++ b/agent/Cargo.lock @@ -4307,6 +4307,7 @@ dependencies = [ "ahash", "btf-rs", "cc", + "cfg-if", "env_logger 0.11.5", "gimli 0.31.0", "libc", diff --git a/agent/crates/trace-utils/Cargo.toml b/agent/crates/trace-utils/Cargo.toml index ca9a5fbb07d..cd0aa4b1caa 100644 --- a/agent/crates/trace-utils/Cargo.toml +++ b/agent/crates/trace-utils/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] ahash = "0.8" btf-rs = "1.1" +cfg-if = "1.0" gimli = "0.31" libc = "0.2" log = "0.4" diff --git a/agent/crates/trace-utils/src/lib.rs b/agent/crates/trace-utils/src/lib.rs index a47ff6110f5..772de1bf2bd 100644 --- a/agent/crates/trace-utils/src/lib.rs +++ b/agent/crates/trace-utils/src/lib.rs @@ -14,110 +14,114 @@ * limitations under the License. */ -pub mod btf; -pub mod error; -pub mod maps; -pub mod unwind; -pub(crate) mod utils; +cfg_if::cfg_if! { + if #[cfg(any(target_os = "linux", target_os = "android"))] { + pub mod btf; + pub mod error; + pub mod maps; + pub mod unwind; + pub(crate) mod utils; -use std::io::Write; + use std::io::Write; -use unwind::{python::PythonUnwindTable, UnwindTable}; + use unwind::{python::PythonUnwindTable, UnwindTable}; -#[no_mangle] -pub unsafe extern "C" fn unwind_table_create( - process_shard_list_map_fd: i32, - unwind_entry_shard_map_fd: i32, -) -> *mut UnwindTable { - let table = Box::new(UnwindTable::new( - process_shard_list_map_fd, - unwind_entry_shard_map_fd, - )); - Box::into_raw(table) -} + #[no_mangle] + pub unsafe extern "C" fn unwind_table_create( + process_shard_list_map_fd: i32, + unwind_entry_shard_map_fd: i32, + ) -> *mut UnwindTable { + let table = Box::new(UnwindTable::new( + process_shard_list_map_fd, + unwind_entry_shard_map_fd, + )); + Box::into_raw(table) + } -#[no_mangle] -pub unsafe extern "C" fn unwind_table_destroy(table: *mut UnwindTable) { - if table.is_null() { - return; - } - std::mem::drop(Box::from_raw(table)); -} + #[no_mangle] + pub unsafe extern "C" fn unwind_table_destroy(table: *mut UnwindTable) { + if table.is_null() { + return; + } + std::mem::drop(Box::from_raw(table)); + } -#[no_mangle] -pub unsafe extern "C" fn unwind_table_load(table: *mut UnwindTable, pid: u32) { - (*table).load(pid); -} + #[no_mangle] + pub unsafe extern "C" fn unwind_table_load(table: *mut UnwindTable, pid: u32) { + (*table).load(pid); + } -#[no_mangle] -pub unsafe extern "C" fn unwind_table_unload(table: *mut UnwindTable, pid: u32) { - (*table).unload(pid); -} + #[no_mangle] + pub unsafe extern "C" fn unwind_table_unload(table: *mut UnwindTable, pid: u32) { + (*table).unload(pid); + } -#[no_mangle] -pub unsafe extern "C" fn unwind_table_unload_all(table: *mut UnwindTable) { - (*table).unload_all(); -} + #[no_mangle] + pub unsafe extern "C" fn unwind_table_unload_all(table: *mut UnwindTable) { + (*table).unload_all(); + } -#[no_mangle] -pub unsafe extern "C" fn frame_pointer_heuristic_check(pid: u32) -> bool { - unwind::dwarf::frame_pointer_heuristic_check(pid) -} + #[no_mangle] + pub unsafe extern "C" fn frame_pointer_heuristic_check(pid: u32) -> bool { + unwind::dwarf::frame_pointer_heuristic_check(pid) + } -#[no_mangle] -pub unsafe extern "C" fn python_unwind_table_create( - unwind_info_map_fd: i32, - offsets_map_fd: i32, -) -> *mut PythonUnwindTable { - let table = Box::new(PythonUnwindTable::new(unwind_info_map_fd, offsets_map_fd)); - Box::into_raw(table) -} + #[no_mangle] + pub unsafe extern "C" fn python_unwind_table_create( + unwind_info_map_fd: i32, + offsets_map_fd: i32, + ) -> *mut PythonUnwindTable { + let table = Box::new(PythonUnwindTable::new(unwind_info_map_fd, offsets_map_fd)); + Box::into_raw(table) + } -#[no_mangle] -pub unsafe extern "C" fn python_unwind_table_destroy(table: *mut PythonUnwindTable) { - if !table.is_null() { - std::mem::drop(Box::from_raw(table)); - } -} + #[no_mangle] + pub unsafe extern "C" fn python_unwind_table_destroy(table: *mut PythonUnwindTable) { + if !table.is_null() { + std::mem::drop(Box::from_raw(table)); + } + } -#[no_mangle] -pub unsafe extern "C" fn python_unwind_table_load(table: *mut PythonUnwindTable, pid: u32) { - (*table).load(pid); -} + #[no_mangle] + pub unsafe extern "C" fn python_unwind_table_load(table: *mut PythonUnwindTable, pid: u32) { + (*table).load(pid); + } -#[no_mangle] -pub unsafe extern "C" fn python_unwind_table_unload(table: *mut PythonUnwindTable, pid: u32) { - (*table).unload(pid); -} + #[no_mangle] + pub unsafe extern "C" fn python_unwind_table_unload(table: *mut PythonUnwindTable, pid: u32) { + (*table).unload(pid); + } -// forwards rust demangle to C api -// The code is from: https://github.com/rust-lang/rustc-demangle/blob/main/crates/capi/src/lib.rs -#[no_mangle] -pub unsafe extern "C" fn rustc_demangle( - mangled: *const libc::c_char, - out: *mut libc::c_char, - out_size: usize, -) -> libc::c_int { - let mangled_str = match std::ffi::CStr::from_ptr(mangled).to_str() { - Ok(s) => s, - Err(_) => return 0, - }; - match rustc_demangle::try_demangle(mangled_str) { - Ok(demangle) => { - let mut out_slice = std::slice::from_raw_parts_mut(out as *mut u8, out_size); - match write!(out_slice, "{:#}\0", demangle) { - Ok(_) => return 1, + // forwards rust demangle to C api + // The code is from: https://github.com/rust-lang/rustc-demangle/blob/main/crates/capi/src/lib.rs + #[no_mangle] + pub unsafe extern "C" fn rustc_demangle( + mangled: *const libc::c_char, + out: *mut libc::c_char, + out_size: usize, + ) -> libc::c_int { + let mangled_str = match std::ffi::CStr::from_ptr(mangled).to_str() { + Ok(s) => s, + Err(_) => return 0, + }; + match rustc_demangle::try_demangle(mangled_str) { + Ok(demangle) => { + let mut out_slice = std::slice::from_raw_parts_mut(out as *mut u8, out_size); + match write!(out_slice, "{:#}\0", demangle) { + Ok(_) => return 1, + Err(_) => return 0, + } + } Err(_) => return 0, } } - Err(_) => return 0, - } -} -#[no_mangle] -pub unsafe extern "C" fn read_offset_of_stack_in_task_struct() -> i32 { - match btf::read_offset_of_stack_in_task_struct() { - Some(offset) => offset as i32, - None => -1, + #[no_mangle] + pub unsafe extern "C" fn read_offset_of_stack_in_task_struct() -> i32 { + match btf::read_offset_of_stack_in_task_struct() { + Some(offset) => offset as i32, + None => -1, + } + } } } diff --git a/agent/src/config/mod.rs b/agent/src/config/mod.rs index a925dd40905..b7a8476ab9e 100644 --- a/agent/src/config/mod.rs +++ b/agent/src/config/mod.rs @@ -17,13 +17,13 @@ mod config; pub mod handler; -#[cfg(any(target_os = "linux", target_os = "android"))] -pub use config::ApiResources; pub use config::{ AgentIdType, Config, ConfigError, DpdkSource, KubernetesPollerType, OracleConfig, PcapStream, - ProcessMatcher, PrometheusExtraLabels, UserConfig, K8S_CA_CRT_PATH, + PrometheusExtraLabels, UserConfig, K8S_CA_CRT_PATH, }; #[cfg(any(target_os = "linux", target_os = "android"))] +pub use config::{ApiResources, ProcessMatcher}; +#[cfg(any(target_os = "linux", target_os = "android"))] pub use handler::FlowAccess; pub use handler::{DispatcherConfig, FlowConfig, ModuleConfig, NpbConfig}; diff --git a/agent/src/dispatcher/base_dispatcher.rs b/agent/src/dispatcher/base_dispatcher.rs index fdb40b4bcf5..36af6465f69 100644 --- a/agent/src/dispatcher/base_dispatcher.rs +++ b/agent/src/dispatcher/base_dispatcher.rs @@ -403,15 +403,13 @@ impl BaseDispatcher { #[cfg(target_os = "windows")] impl InternalState { - pub(super) fn check_and_update_bpf(&mut self, _: &mut RecvEngine) { + pub(super) fn check_and_update_bpf(&mut self, engine: &mut RecvEngine) { if !self.need_update_bpf.swap(false, Ordering::Relaxed) { return; } let bpf_options = self.bpf_options.lock().unwrap(); - if let Err(e) = self - .engine - .set_bpf(vec![], &CString::new(bpf_options.get_bpf_syntax()).unwrap()) + if let Err(e) = engine.set_bpf(vec![], &CString::new(bpf_options.get_bpf_syntax()).unwrap()) { warn!("set_bpf failed: {}", e); } diff --git a/agent/src/dispatcher/mod.rs b/agent/src/dispatcher/mod.rs index e36f4e967a8..d56901954a8 100644 --- a/agent/src/dispatcher/mod.rs +++ b/agent/src/dispatcher/mod.rs @@ -347,6 +347,7 @@ impl DispatcherListener { Self::LocalPlus(l) => { l.on_tap_interface_change(interfaces, if_mac_source, agent_type, blacklist) } + #[cfg(target_os = "linux")] Self::LocalMultins(l) => { l.on_tap_interface_change(interfaces, if_mac_source, agent_type, blacklist) } @@ -1374,10 +1375,9 @@ impl DispatcherBuilder { const L2_MAC_ADDR_OFFSET: usize = 12; +#[cfg(any(target_os = "linux", target_os = "android"))] pub(crate) fn set_cpu_affinity(options: &Mutex) { - #[cfg(any(target_os = "linux", target_os = "android"))] let cpu_set = options.lock().unwrap().cpu_set; - #[cfg(any(target_os = "linux", target_os = "android"))] if cpu_set != CpuSet::new() { if let Err(e) = nix::sched::sched_setaffinity(nix::unistd::Pid::from_raw(0), &cpu_set) { warn!("CPU Affinity({:?}) bind error: {:?}.", &cpu_set, e); diff --git a/agent/src/monitor.rs b/agent/src/monitor.rs index fe793347cc6..00aa72cf90a 100644 --- a/agent/src/monitor.rs +++ b/agent/src/monitor.rs @@ -30,12 +30,11 @@ use sysinfo::NetworkExt; use sysinfo::{get_current_pid, Pid, ProcessExt, ProcessRefreshKind, System, SystemExt}; #[cfg(target_os = "linux")] -use crate::utils::environment::SocketInfo; +use crate::utils::{cgroups, environment::SocketInfo}; use crate::{ config::handler::EnvironmentAccess, error::{Error, Result}, utils::{ - cgroups, process::{get_current_sys_memory_percentage, get_file_and_size_sum}, stats::{ self, Collector, Countable, Counter, CounterType, CounterValue, RefCountable, diff --git a/agent/src/utils/guard.rs b/agent/src/utils/guard.rs index 09f995840fe..1cfbe99d223 100644 --- a/agent/src/utils/guard.rs +++ b/agent/src/utils/guard.rs @@ -16,12 +16,14 @@ use std::io::Read; use std::path::Path; +#[cfg(target_os = "linux")] +use std::time::Instant; use std::{ fs::{self, File}, string::String, sync::{Arc, Condvar, Mutex}, thread::{self, JoinHandle}, - time::{Duration, Instant, UNIX_EPOCH}, + time::{Duration, UNIX_EPOCH}, }; use arc_swap::access::Access;