Skip to content

Commit

Permalink
Merge pull request #38 from getsentry/feature/help-cargo-fmt
Browse files Browse the repository at this point in the history
Move pub mod out of cfg_if to support cargo fmt and run it
  • Loading branch information
fitzgen authored Jun 3, 2019
2 parents 910c4a4 + a631ffc commit 20999b4
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 101 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ repository = "gimli-rs/findshlibs"
[badges.travis-ci]
repository = "gimli-rs/findshlibs"

[build-dependencies]
cfg-if = "0.1.2"

[target.'cfg(target_os = "macos")'.build-dependencies]
bindgen = { version = "0.39.0", default-features = false }

[dependencies]
cfg-if = "0.1.2"
lazy_static = "1.0.0"
libc = "0.2.43"

Expand Down
48 changes: 19 additions & 29 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
#[macro_use]
extern crate cfg_if;

cfg_if! {
if #[cfg(target_os = "macos")] {
fn main() {
#[cfg(target_os = "macos")]
{
extern crate bindgen;

use std::env;
use std::path::PathBuf;

fn main() {
generate_macos_bindings();
}

fn generate_macos_bindings() {
let bindings = bindgen::Builder::default()
.header("./src/macos/bindings.h")
.whitelist_function("_dyld_.*")
.whitelist_type("mach_header.*")
.whitelist_type("load_command.*")
.whitelist_type("uuid_command.*")
.whitelist_type("segment_command.*")
.whitelist_var("MH_MAGIC.*")
.whitelist_var("LC_SEGMENT.*")
.whitelist_var("LC_UUID.*")
.generate()
.expect("Should generate macOS FFI bindings OK");
let bindings = bindgen::Builder::default()
.header("./src/macos/bindings.h")
.whitelist_function("_dyld_.*")
.whitelist_type("mach_header.*")
.whitelist_type("load_command.*")
.whitelist_type("uuid_command.*")
.whitelist_type("segment_command.*")
.whitelist_var("MH_MAGIC.*")
.whitelist_var("LC_SEGMENT.*")
.whitelist_var("LC_UUID.*")
.generate()
.expect("Should generate macOS FFI bindings OK");

let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("macos_bindings.rs"))
.expect("Should write macos_bindings.rs OK");
}
} else {
fn main() {}
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("macos_bindings.rs"))
.expect("Should write macos_bindings.rs OK");
}
}
50 changes: 17 additions & 33 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,56 +76,40 @@
//! [LUL]: http://searchfox.org/mozilla-central/rev/13148faaa91a1c823a7d68563d9995480e714979/tools/profiler/lul/LulMain.h#17-51
#![deny(missing_docs)]

#[macro_use]
extern crate cfg_if;

#[cfg(target_os = "macos")]
#[macro_use]
extern crate lazy_static;

#[cfg(target_os = "linux")]
extern crate libc;

#[cfg(target_os = "macos")]
pub mod macos;

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

use std::ffi::OsStr;
use std::fmt::{self, Debug};
use std::ptr;

pub mod unsupported;

cfg_if!(
if #[cfg(target_os = "linux")] {

pub mod linux;

/// The [`SharedLibrary` trait](./trait.SharedLibrary.html)
/// implementation for the target operating system.
pub type TargetSharedLibrary<'a> = linux::SharedLibrary<'a>;

/// An indicator if this platform is supported.
pub const TARGET_SUPPORTED: bool = true;

} else if #[cfg(target_os = "macos")] {

pub mod macos;

/// The [`SharedLibrary` trait](./trait.SharedLibrary.html)
/// implementation for the target operating system.
pub type TargetSharedLibrary<'a> = macos::SharedLibrary<'a>;

/// An indicator if this platform is supported.
pub const TARGET_SUPPORTED: bool = true;
#[cfg(target_os = "linux")]
use linux as native_mod;

} else {
#[cfg(target_os = "macos")]
use macos as native_mod;

/// The [`SharedLibrary` trait](./trait.SharedLibrary.html)
/// implementation for the target operating system.
pub type TargetSharedLibrary<'a> = unsupported::SharedLibrary<'a>;
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
use unsupported as native_mod;

/// An indicator if this platform is supported.
pub const TARGET_SUPPORTED: bool = false;
/// The [`SharedLibrary` trait](./trait.SharedLibrary.html)
/// implementation for the target operating system.
pub type TargetSharedLibrary<'a> = native_mod::SharedLibrary<'a>;

}
);
/// An indicator if this platform is supported.
pub const TARGET_SUPPORTED: bool = cfg!(any(target_os = "macos", target_os = "linux"));

macro_rules! simple_newtypes {
(
Expand Down
16 changes: 6 additions & 10 deletions src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,22 @@ use std::any::Any;
use std::borrow::Cow;
use std::env::current_exe;
use std::ffi::{CStr, CString, OsStr};
use std::os::unix::ffi::OsStrExt;
use std::fmt;
use std::isize;
use std::marker::PhantomData;
use std::mem;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::ffi::OsStringExt;
use std::panic;
use std::slice;

use libc;

cfg_if! {
if #[cfg(target_pointer_width = "32")] {
type Phdr = libc::Elf32_Phdr;
} else if #[cfg(target_pointer_width = "64")] {
type Phdr = libc::Elf64_Phdr;
} else {
// Unsupported.
}
}
#[cfg(target_pointer_width = "32")]
type Phdr = libc::Elf32_Phdr;

#[cfg(target_pointer_width = "64")]
type Phdr = libc::Elf64_Phdr;

const NT_GNU_BUILD_ID: u32 = 3;

Expand Down
55 changes: 30 additions & 25 deletions src/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
//! trait](../trait.SharedLibrary.html).
#![allow(clippy::cast_ptr_alignment)]

use super::{Bias, IterationControl, Svma, SharedLibraryId};
use super::Segment as SegmentTrait;
use super::SharedLibrary as SharedLibraryTrait;
use super::{Bias, IterationControl, SharedLibraryId, Svma};

use std::ffi::{CStr, OsStr};
use std::os::unix::ffi::OsStrExt;
use std::marker::PhantomData;
use std::os::unix::ffi::OsStrExt;
use std::sync::Mutex;
use std::usize;

Expand Down Expand Up @@ -147,12 +147,10 @@ enum MachType {

impl MachType {
unsafe fn from_header_ptr(header: *const bindings::mach_header) -> Option<MachType> {
header.as_ref().and_then(|header| {
match header.magic {
bindings::MH_MAGIC => Some(MachType::Mach32),
bindings::MH_MAGIC_64 => Some(MachType::Mach64),
_ => None,
}
header.as_ref().and_then(|header| match header.magic {
bindings::MH_MAGIC => Some(MachType::Mach32),
bindings::MH_MAGIC_64 => Some(MachType::Mach64),
_ => None,
})
}
}
Expand All @@ -165,12 +163,11 @@ enum MachHeader<'a> {

impl<'a> MachHeader<'a> {
unsafe fn from_header_ptr(header: *const bindings::mach_header) -> Option<MachHeader<'a>> {
MachType::from_header_ptr(header).and_then(|ty| {
match ty {
MachType::Mach32 => header.as_ref().map(MachHeader::Header32),
MachType::Mach64 => (header as *const bindings::mach_header_64)
.as_ref().map(MachHeader::Header64),
}
MachType::from_header_ptr(header).and_then(|ty| match ty {
MachType::Mach32 => header.as_ref().map(MachHeader::Header32),
MachType::Mach64 => (header as *const bindings::mach_header_64)
.as_ref()
.map(MachHeader::Header64),
})
}
}
Expand Down Expand Up @@ -242,8 +239,9 @@ impl<'a> SharedLibraryTrait for SharedLibrary<'a> {
}

fn each<F, C>(mut f: F)
where F: FnMut(&Self) -> C,
C: Into<IterationControl>
where
F: FnMut(&Self) -> C,
C: Into<IterationControl>,
{
// Make sure we have exclusive access to dyld so that (hopefully) no one
// else adds or removes shared libraries while we are iterating them.
Expand All @@ -253,16 +251,22 @@ impl<'a> SharedLibraryTrait for SharedLibrary<'a> {

for image_idx in 0..count {
let (header, slide, name) = unsafe {
(bindings::_dyld_get_image_header(image_idx),
bindings::_dyld_get_image_vmaddr_slide(image_idx),
bindings::_dyld_get_image_name(image_idx))
(
bindings::_dyld_get_image_header(image_idx),
bindings::_dyld_get_image_vmaddr_slide(image_idx),
bindings::_dyld_get_image_name(image_idx),
)
};

if let Some(header) = unsafe { MachHeader::from_header_ptr(header) } {
assert!(slide != 0,
"If we have a header pointer, slide should be valid");
assert!(!name.is_null(),
"If we have a header pointer, name should be valid");
assert!(
slide != 0,
"If we have a header pointer, slide should be valid"
);
assert!(
!name.is_null(),
"If we have a header pointer, name should be valid"
);

let name = unsafe { CStr::from_ptr(name) };
let shlib = SharedLibrary::new(header, slide, name);
Expand All @@ -278,14 +282,15 @@ impl<'a> SharedLibraryTrait for SharedLibrary<'a> {

#[cfg(test)]
mod tests {
use super::super::{IterationControl, Segment, SharedLibrary};
use macos;
use super::super::{IterationControl, SharedLibrary, Segment};

#[test]
fn have_libdyld() {
let mut found_dyld = false;
macos::SharedLibrary::each(|shlib| {
found_dyld |= shlib.name
found_dyld |= shlib
.name
.to_bytes()
.split(|c| *c == b'.' || *c == b'/')
.any(|s| s == b"libdyld");
Expand Down

0 comments on commit 20999b4

Please sign in to comment.