Skip to content

Commit

Permalink
fix: Windows compile and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rvql committed Feb 20, 2025
1 parent 3a12b03 commit e152fef
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 100 deletions.
1 change: 1 addition & 0 deletions agent/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions agent/crates/trace-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
180 changes: 92 additions & 88 deletions agent/crates/trace-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
}
6 changes: 3 additions & 3 deletions agent/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
6 changes: 2 additions & 4 deletions agent/src/dispatcher/base_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions agent/src/dispatcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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<Options>) {
#[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);
Expand Down
3 changes: 1 addition & 2 deletions agent/src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion agent/src/utils/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e152fef

Please sign in to comment.