Skip to content

Commit

Permalink
Introduce 'linux' config alias
Browse files Browse the repository at this point in the history
  • Loading branch information
d-e-s-o committed Oct 29, 2024
1 parent 91033a9 commit 619109e
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 39 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ codegen-units = 1
lto = false
codegen-units = 256

[build-dependencies]
cfg_aliases = "0.2.1"

[dependencies]
cpp_demangle = {version = "0.4", optional = true}
gimli = {version = "0.31", optional = true}
Expand Down
7 changes: 7 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use cfg_aliases::cfg_aliases;

fn main() {
cfg_aliases! {
linux: { target_os = "linux" },
}
}
6 changes: 3 additions & 3 deletions src/file_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct FileMeta {
inode: libc::ino_t,
size: libc::off_t,
mtime_sec: libc::time_t,
#[cfg(target_os = "linux")]
#[cfg(linux)]
mtime_nsec: i64,
}

Expand All @@ -32,7 +32,7 @@ impl From<&libc::stat> for FileMeta {
inode: other.st_ino as _,
size: other.st_size as _,
mtime_sec: other.st_mtime,
#[cfg(target_os = "linux")]
#[cfg(linux)]
mtime_nsec: other.st_mtime_nsec as _,
}
}
Expand Down Expand Up @@ -219,7 +219,7 @@ mod tests {

/// Check that our `FileCache` does not represent symbolic links
/// pointing to the same file as equal entries.
#[cfg(target_os = "linux")]
#[cfg(linux)]
#[test]
fn file_symlinks() {
use std::os::fd::AsRawFd as _;
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/bpf/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(not(target_os = "linux"))]
#[cfg(not(linux))]
compile_error!("BPF support is only present on Linux, please disable `bpf` feature");

mod btf;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
)),
allow(dead_code, unused_imports)
)]
#![cfg_attr(not(target_os = "linux"), allow(dead_code, unused_imports))]
#![cfg_attr(not(linux), allow(dead_code, unused_imports))]


#[cfg(feature = "nightly")]
Expand Down
4 changes: 2 additions & 2 deletions src/normalize/ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ fn vma_flags_to_perm(vma_flags: u64) -> Perm {
/// The caller is responsible for checking that the returned `MapsEntry`
/// actually covers the provided address. If it does not, it represents
/// the next known entry.
#[cfg(target_os = "linux")]
#[cfg(linux)]
pub(crate) fn query_procmap(
file: &File,
pid: Pid,
Expand Down Expand Up @@ -224,7 +224,7 @@ pub(crate) fn query_procmap(
Ok(Some(entry))
}

#[cfg(not(target_os = "linux"))]
#[cfg(not(linux))]
pub(crate) fn query_procmap(
_file: &File,
_pid: Pid,
Expand Down
2 changes: 1 addition & 1 deletion src/normalize/normalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ mod tests {
}

/// Check that we can normalize user addresses.
#[cfg(target_os = "linux")]
#[cfg(linux)]
// `libc` on Arm doesn't have `__errno_location`.
#[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))]
#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/symbolize/perf_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ mod tests {
}

/// Check that we can symbolize an address using a perf map.
#[cfg(target_os = "linux")]
#[cfg(linux)]
#[test]
#[ignore = "test requires python 3.12 or higher"]
fn symbolize_perf_map() {
Expand Down
16 changes: 8 additions & 8 deletions src/symbolize/symbolizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::symbolize::InlinedFn;
use crate::symbolize::Resolve;
use crate::symbolize::TranslateFileOffset;
use crate::util;
#[cfg(target_os = "linux")]
#[cfg(linux)]
use crate::util::uname_release;
use crate::util::Dbg;
#[cfg(feature = "tracing")]
Expand Down Expand Up @@ -891,7 +891,7 @@ impl Symbolizer {
Ok(resolver)
}

#[cfg(target_os = "linux")]
#[cfg(linux)]
fn create_kernel_resolver(&self, src: &Kernel) -> Result<KernelResolver> {
let Kernel {
kallsyms,
Expand Down Expand Up @@ -954,7 +954,7 @@ impl Symbolizer {
KernelResolver::new(ksym_resolver.cloned(), elf_resolver.cloned())
}

#[cfg(not(target_os = "linux"))]
#[cfg(not(linux))]
fn create_kernel_resolver(&self, _src: &Kernel) -> Result<KernelResolver> {
Err(Error::with_unsupported(
"kernel address symbolization is unsupported on operating systems other than Linux",
Expand Down Expand Up @@ -1375,7 +1375,7 @@ impl Default for Symbolizer {
mod tests {
use super::*;

#[cfg(all(target_os = "linux", feature = "nightly"))]
#[cfg(all(linux, feature = "nightly"))]
use test::Bencher;

use test_log::test;
Expand All @@ -1384,7 +1384,7 @@ mod tests {
use crate::symbolize;
use crate::symbolize::CodeInfo;
use crate::test_helper::find_the_answer_fn_in_zip;
#[cfg(target_os = "linux")]
#[cfg(linux)]
use crate::test_helper::with_bpf_symbolization_target_addrs;


Expand Down Expand Up @@ -1694,7 +1694,7 @@ mod tests {
}

/// Test symbolization of a kernel address inside a BPF program.
#[cfg(target_os = "linux")]
#[cfg(linux)]
#[test]
fn symbolize_kernel_bpf_program() {
with_bpf_symbolization_target_addrs(|handle_getpid, subprogram| {
Expand Down Expand Up @@ -1731,7 +1731,7 @@ mod tests {
}

/// Benchmark the symbolization of BPF program kernel addresses.
#[cfg(target_os = "linux")]
#[cfg(linux)]
#[cfg(feature = "nightly")]
#[bench]
fn bench_symbolize_kernel_bpf_uncached(b: &mut Bencher) {
Expand All @@ -1754,7 +1754,7 @@ mod tests {

/// Benchmark the symbolization of BPF program kernel addresses when
/// relevant data is readily cached.
#[cfg(target_os = "linux")]
#[cfg(linux)]
#[cfg(feature = "nightly")]
#[bench]
fn bench_symbolize_kernel_bpf_cached(b: &mut Bencher) {
Expand Down
4 changes: 2 additions & 2 deletions src/test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub(crate) fn find_the_answer_fn_in_zip(mmap: &Mmap) -> (inspect::SymInfo<'stati
}


#[cfg(target_os = "linux")]
#[cfg(linux)]
mod bpf {
use super::*;

Expand Down Expand Up @@ -166,5 +166,5 @@ mod bpf {
}
}

#[cfg(target_os = "linux")]
#[cfg(linux)]
pub(crate) use bpf::*;
6 changes: 3 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ pub fn stat(path: &Path) -> io::Result<libc::stat> {
}


#[cfg(target_os = "linux")]
#[cfg(linux)]
#[cfg(test)]
#[allow(clippy::absolute_paths)]
fn fstat(fd: std::os::unix::io::RawFd) -> io::Result<libc::stat> {
Expand All @@ -295,7 +295,7 @@ fn fstat(fd: std::os::unix::io::RawFd) -> io::Result<libc::stat> {
}


#[cfg(target_os = "linux")]
#[cfg(linux)]
pub(crate) fn uname_release() -> io::Result<CString> {
let mut dst = MaybeUninit::uninit();
let rc = unsafe { libc::uname(dst.as_mut_ptr()) };
Expand Down Expand Up @@ -741,7 +741,7 @@ mod tests {

/// Check that we can retrieve meta-data about a file using `stat`
/// and `fstat`.
#[cfg(target_os = "linux")]
#[cfg(linux)]
#[test]
fn file_stating() {
use std::os::fd::AsRawFd as _;
Expand Down
4 changes: 2 additions & 2 deletions tests/allocs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
clippy::let_and_return,
clippy::let_unit_value
)]
#![cfg_attr(not(target_os = "linux"), allow(dead_code, unused_imports))]
#![cfg_attr(not(linux), allow(dead_code, unused_imports))]

use std::alloc::GlobalAlloc;
use std::alloc::Layout;
Expand Down Expand Up @@ -56,7 +56,7 @@ unsafe impl GlobalAlloc for TracingAlloc {

/// Normalize addresses in the current process and print allocation
/// statistics.
#[cfg(target_os = "linux")]
#[cfg(linux)]
#[test]
fn normalize_process() {
let region = Region::new(&GLOBAL);
Expand Down
10 changes: 5 additions & 5 deletions tests/blazesym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
clippy::let_and_return,
clippy::let_unit_value
)]
#![cfg_attr(not(target_os = "linux"), allow(dead_code, unused_imports))]
#![cfg_attr(not(linux), allow(dead_code, unused_imports))]

use std::collections::HashMap;
use std::env;
Expand All @@ -16,7 +16,7 @@ use std::io::Read as _;
use std::io::Write as _;
use std::ops::ControlFlow;
use std::ops::Deref as _;
#[cfg(target_os = "linux")]
#[cfg(linux)]
use std::os::unix::ffi::OsStringExt as _;
use std::path::Path;
use std::process::Command;
Expand Down Expand Up @@ -699,7 +699,7 @@ fn symbolize_process() {

/// Check that we can symbolize an address in a process using a binary
/// located in a local mount namespace.
#[cfg(target_os = "linux")]
#[cfg(linux)]
#[test]
fn symbolize_process_in_mount_namespace() {
use libc::kill;
Expand Down Expand Up @@ -806,7 +806,7 @@ fn symbolize_process_with_custom_dispatch() {
}

/// Check that we can normalize addresses in an ELF shared object.
#[cfg(target_os = "linux")]
#[cfg(linux)]
#[test]
fn normalize_elf_addr() {
fn test(so: &str, map_files: bool) {
Expand Down Expand Up @@ -872,7 +872,7 @@ fn normalize_elf_addr() {


/// Check that we can enable/disable the reading of build IDs.
#[cfg(target_os = "linux")]
#[cfg(linux)]
#[test]
fn normalize_build_id_reading() {
fn test(read_build_ids: bool) {
Expand Down
4 changes: 2 additions & 2 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use libc::uid_t;


/// Run a function with a different effective user ID.
#[cfg(target_os = "linux")]
#[cfg(linux)]
pub fn as_user<F, R>(ruid: uid_t, euid: uid_t, f: F) -> R
where
F: FnOnce() -> R + UnwindSafe,
Expand Down Expand Up @@ -44,7 +44,7 @@ where
result.unwrap()
}

#[cfg(not(target_os = "linux"))]
#[cfg(not(linux))]
pub fn as_user<F, R>(ruid: uid_t, euid: uid_t, f: F) -> R
where
F: FnOnce() -> R + UnwindSafe,
Expand Down
6 changes: 3 additions & 3 deletions tests/permission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
clippy::let_and_return,
clippy::let_unit_value
)]
#![cfg_attr(not(target_os = "linux"), allow(dead_code, unused_imports))]
#![cfg_attr(not(linux), allow(dead_code, unused_imports))]

#[cfg(target_os = "linux")]
#[cfg(linux)]
mod common;

use std::fs::copy;
Expand Down Expand Up @@ -35,7 +35,7 @@ fn symbolize_no_permission_impl(path: &Path) {

/// Check that we fail symbolization as expected when we don't have the
/// permission to open the symbolization source.
#[cfg(target_os = "linux")]
#[cfg(linux)]
#[test]
fn symbolize_no_permission() {
use common::as_user;
Expand Down
10 changes: 5 additions & 5 deletions tests/permissionless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
clippy::let_and_return,
clippy::let_unit_value
)]
#![cfg_attr(not(target_os = "linux"), allow(dead_code, unused_imports))]
#![cfg_attr(not(linux), allow(dead_code, unused_imports))]

#[cfg(target_os = "linux")]
#[cfg(linux)]
mod common;

use std::io::Error;
Expand Down Expand Up @@ -75,7 +75,7 @@ fn normalize_permissionless_impl(pid: Pid, addr: Addr, test_lib: &Path) {
);
}

#[cfg(target_os = "linux")]
#[cfg(linux)]
fn run_test<F>(callback_fn: F)
where
F: FnOnce(Pid, u64, &Path) + UnwindSafe,
Expand Down Expand Up @@ -145,15 +145,15 @@ where

/// Check that we can symbolize an address in a process using only
/// symbolic paths.
#[cfg(target_os = "linux")]
#[cfg(linux)]
#[test]
fn symbolize_process_symbolic_paths() {
run_test(symbolize_permissionless_impl)
}

/// Check that we can normalize an address in a process using only
/// symbolic paths.
#[cfg(target_os = "linux")]
#[cfg(linux)]
#[test]
fn normalize_process_symbolic_paths() {
run_test(normalize_permissionless_impl)
Expand Down

0 comments on commit 619109e

Please sign in to comment.