Skip to content

Commit

Permalink
Merge pull request #36 from getsentry/feature/os-str
Browse files Browse the repository at this point in the history
Expose OsStr instead of CStr from API
  • Loading branch information
fitzgen authored Jun 3, 2019
2 parents a2a7f38 + b5647dd commit 910c4a4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion examples/list_segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
println!(
" {}: segment {}",
seg.actual_virtual_memory_address(shlib),
seg.name().to_string_lossy()
seg.name()
);
}
});
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//! for seg in shlib.segments() {
//! println!(" {}: segment {}",
//! seg.actual_virtual_memory_address(shlib),
//! seg.name().to_string_lossy());
//! seg.name());
//! }
//! });
//! }
Expand Down Expand Up @@ -86,7 +86,7 @@ extern crate lazy_static;
#[cfg(target_os = "linux")]
extern crate libc;

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

Expand Down Expand Up @@ -203,7 +203,7 @@ pub trait Segment: Sized + Debug {
type SharedLibrary: SharedLibrary<Segment = Self>;

/// Get this segment's name.
fn name(&self) -> &CStr;
fn name(&self) -> &str;

/// Returns `true` if this is a code segment.
#[inline]
Expand Down Expand Up @@ -310,7 +310,7 @@ pub trait SharedLibrary: Sized + Debug {
type SegmentIter: Debug + Iterator<Item = Self::Segment>;

/// Get the name of this shared library.
fn name(&self) -> &CStr;
fn name(&self) -> &OsStr;

/// Get the debug-id of this shared library if available.
fn id(&self) -> Option<SharedLibraryId>;
Expand Down
46 changes: 22 additions & 24 deletions src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use super::{Bias, IterationControl, SharedLibraryId, Svma};
use std::any::Any;
use std::borrow::Cow;
use std::env::current_exe;
use std::ffi::{CStr, CString};
use std::ffi::{CStr, CString, OsStr};
use std::os::unix::ffi::OsStrExt;
use std::fmt;
use std::isize;
use std::marker::PhantomData;
Expand Down Expand Up @@ -46,23 +47,23 @@ pub struct Segment<'a> {
impl<'a> SegmentTrait for Segment<'a> {
type SharedLibrary = ::linux::SharedLibrary<'a>;

fn name(&self) -> &CStr {
fn name(&self) -> &str {
unsafe {
match self.phdr.as_ref().unwrap().p_type {
libc::PT_NULL => CStr::from_ptr("NULL\0".as_ptr() as _),
libc::PT_LOAD => CStr::from_ptr("LOAD\0".as_ptr() as _),
libc::PT_DYNAMIC => CStr::from_ptr("DYNAMIC\0".as_ptr() as _),
libc::PT_INTERP => CStr::from_ptr("INTERP\0".as_ptr() as _),
libc::PT_NOTE => CStr::from_ptr("NOTE\0".as_ptr() as _),
libc::PT_SHLIB => CStr::from_ptr("SHLI\0".as_ptr() as _),
libc::PT_PHDR => CStr::from_ptr("PHDR\0".as_ptr() as _),
libc::PT_TLS => CStr::from_ptr("TLS\0".as_ptr() as _),
libc::PT_NUM => CStr::from_ptr("NUM\0".as_ptr() as _),
libc::PT_LOOS => CStr::from_ptr("LOOS\0".as_ptr() as _),
libc::PT_GNU_EH_FRAME => CStr::from_ptr("GNU_EH_FRAME\0".as_ptr() as _),
libc::PT_GNU_STACK => CStr::from_ptr("GNU_STACK\0".as_ptr() as _),
libc::PT_GNU_RELRO => CStr::from_ptr("GNU_RELRO\0".as_ptr() as _),
_ => CStr::from_ptr("(unknown segment type)\0".as_ptr() as _),
libc::PT_NULL => "NULL",
libc::PT_LOAD => "LOAD",
libc::PT_DYNAMIC => "DYNAMIC",
libc::PT_INTERP => "INTERP",
libc::PT_NOTE => "NOTE",
libc::PT_SHLIB => "SHLI",
libc::PT_PHDR => "PHDR",
libc::PT_TLS => "TLS",
libc::PT_NUM => "NUM",
libc::PT_LOOS => "LOOS",
libc::PT_GNU_EH_FRAME => "GNU_EH_FRAME",
libc::PT_GNU_STACK => "GNU_STACK",
libc::PT_GNU_RELRO => "GNU_RELRO",
_ => "(unknown segment type)",
}
}
}
Expand Down Expand Up @@ -202,8 +203,8 @@ impl<'a> SharedLibraryTrait for SharedLibrary<'a> {
type SegmentIter = SegmentIter<'a>;

#[inline]
fn name(&self) -> &CStr {
&*self.name
fn name(&self) -> &OsStr {
OsStr::from_bytes(self.name.to_bytes())
}

fn id(&self) -> Option<SharedLibraryId> {
Expand Down Expand Up @@ -376,11 +377,10 @@ mod tests {
#[test]
fn get_name() {
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
let mut names = vec![];
linux::SharedLibrary::each(|shlib| {
println!("{:?}", shlib);
let name = OsStr::from_bytes(shlib.name().to_bytes());
let name = shlib.name();
if name != OsStr::new("") {
names.push(name.to_str().unwrap().to_string());
}
Expand All @@ -392,13 +392,11 @@ mod tests {

#[test]
fn get_id() {
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
use std::path::Path;
use std::process::Command;

linux::SharedLibrary::each(|shlib| {
let name = OsStr::from_bytes(shlib.name().to_bytes());
let name = shlib.name();
let id = shlib.id();
if id.is_none() {
println!("no id found for {:?}", name);
Expand Down Expand Up @@ -433,7 +431,7 @@ mod tests {
for seg in shlib.segments() {
println!(" segment = {:?}", seg.name());

found_load |= seg.name().to_bytes() == b"LOAD";
found_load |= seg.name() == "LOAD";
}
assert!(found_load);
});
Expand Down
20 changes: 11 additions & 9 deletions src/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use super::{Bias, IterationControl, Svma, SharedLibraryId};
use super::Segment as SegmentTrait;
use super::SharedLibrary as SharedLibraryTrait;

use std::ffi::CStr;
use std::ffi::{CStr, OsStr};
use std::os::unix::ffi::OsStrExt;
use std::marker::PhantomData;
use std::sync::Mutex;
use std::usize;
Expand Down Expand Up @@ -36,16 +37,17 @@ impl<'a> SegmentTrait for Segment<'a> {
type SharedLibrary = ::macos::SharedLibrary<'a>;

#[inline]
fn name(&self) -> &CStr {
match *self {
fn name(&self) -> &str {
let cstr = match *self {
Segment::Segment32(seg) => unsafe { CStr::from_ptr(seg.segname.as_ptr()) },
Segment::Segment64(seg) => unsafe { CStr::from_ptr(seg.segname.as_ptr()) },
}
};
cstr.to_str().unwrap_or("(invalid segment name)")
}

#[inline]
fn is_code(&self) -> bool {
self.name().to_bytes() == b"__TEXT"
self.name().as_bytes() == b"__TEXT"
}

#[inline]
Expand Down Expand Up @@ -201,8 +203,8 @@ impl<'a> SharedLibraryTrait for SharedLibrary<'a> {
type SegmentIter = SegmentIter<'a>;

#[inline]
fn name(&self) -> &CStr {
self.name
fn name(&self) -> &OsStr {
OsStr::from_bytes(self.name.to_bytes())
}

fn id(&self) -> Option<SharedLibraryId> {
Expand Down Expand Up @@ -335,8 +337,8 @@ mod tests {
for seg in shlib.segments() {
println!(" segment = {:?}", seg.name());

found_text_or_pagezero |= seg.name().to_bytes() == b"__TEXT";
found_text_or_pagezero |= seg.name().to_bytes() == b"__PAGEZERO";
found_text_or_pagezero |= seg.name() == "__TEXT";
found_text_or_pagezero |= seg.name() == "__PAGEZERO";
}
assert!(found_text_or_pagezero);
});
Expand Down
6 changes: 3 additions & 3 deletions src/unsupported.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::Segment as SegmentTrait;
use super::SharedLibrary as SharedLibraryTrait;
use super::{Bias, IterationControl, SharedLibraryId, Svma};

use std::ffi::CStr;
use std::ffi::OsStr;
use std::marker::PhantomData;
use std::usize;

Expand All @@ -19,7 +19,7 @@ impl<'a> SegmentTrait for Segment<'a> {
type SharedLibrary = ::unsupported::SharedLibrary<'a>;

#[inline]
fn name(&self) -> &CStr {
fn name(&self) -> &str {
unreachable!()
}

Expand Down Expand Up @@ -60,7 +60,7 @@ impl<'a> SharedLibraryTrait for SharedLibrary<'a> {
type SegmentIter = SegmentIter<'a>;

#[inline]
fn name(&self) -> &CStr {
fn name(&self) -> &OsStr {
unreachable!()
}

Expand Down

0 comments on commit 910c4a4

Please sign in to comment.