Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b92869e

Browse files
committedJul 7, 2023
Experiment with a bufreader
1 parent f90ee25 commit b92869e

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed
 

‎src/symbolize/gimli/libs_dl_iterate_phdr.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ pub(super) fn native_libraries() -> Vec<Library> {
1818
}
1919

2020
fn infer_current_exe(base_addr: usize) -> OsString {
21-
if let Ok(entries) = super::parse_running_mmaps::parse_maps() {
21+
if let Some(entries) = super::parse_running_mmaps::parse_maps() {
2222
let opt_path = entries
23-
.iter()
23+
.filter_map(|e| e.ok())
2424
.find(|e| e.ip_matches(base_addr) && e.pathname().len() > 0)
25-
.map(|e| e.pathname())
26-
.cloned();
25+
.map(|e| e.pathname().clone());
2726
if let Some(path) = opt_path {
2827
return path;
2928
}

‎src/symbolize/gimli/parse_running_mmaps_unix.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// general purpose, but it hasn't been tested elsewhere.
44

55
use super::mystd::fs::File;
6-
use super::mystd::io::Read;
6+
use super::mystd::io::{self, BufRead, BufReader, ErrorKind};
77
use super::mystd::str::FromStr;
8-
use super::{OsString, String, Vec};
8+
use super::OsString;
99

1010
#[derive(PartialEq, Eq, Debug)]
1111
pub(super) struct MapsEntry {
@@ -53,19 +53,17 @@ pub(super) struct MapsEntry {
5353
pathname: OsString,
5454
}
5555

56-
pub(super) fn parse_maps() -> Result<Vec<MapsEntry>, &'static str> {
57-
let failed_io_err = "couldn't read /proc/self/maps";
58-
let mut v = Vec::new();
59-
let mut proc_self_maps = File::open("/proc/self/maps").map_err(|_| failed_io_err)?;
60-
let mut buf = String::new();
61-
let _bytes_read = proc_self_maps
62-
.read_to_string(&mut buf)
63-
.map_err(|_| failed_io_err)?;
64-
for line in buf.lines() {
65-
v.push(line.parse()?);
66-
}
67-
68-
Ok(v)
56+
pub(super) fn parse_maps() -> Option<impl Iterator<Item = Result<MapsEntry, io::Error>>> {
57+
let proc_self_maps = match File::open("/proc/self/maps") {
58+
Ok(f) => f,
59+
Err(_) => return None,
60+
};
61+
let buf_read = BufReader::new(proc_self_maps);
62+
Some(
63+
buf_read
64+
.lines()
65+
.map(|res| res.and_then(|s| s.parse().map_err(|e| io::Error::from(e)))),
66+
)
6967
}
7068

7169
impl MapsEntry {
@@ -81,15 +79,15 @@ impl MapsEntry {
8179
}
8280

8381
impl FromStr for MapsEntry {
84-
type Err = &'static str;
82+
type Err = io::ErrorKind;
8583

8684
// Format: address perms offset dev inode pathname
8785
// e.g.: "ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]"
8886
// e.g.: "7f5985f46000-7f5985f48000 rw-p 00039000 103:06 76021795 /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2"
8987
// e.g.: "35b1a21000-35b1a22000 rw-p 00000000 00:00 0"
9088
fn from_str(s: &str) -> Result<Self, Self::Err> {
91-
let missing_field = "failed to find all map fields";
92-
let parse_err = "failed to parse all map fields";
89+
let missing_field = ErrorKind::NotFound;
90+
let parse_err = ErrorKind::InvalidData;
9391
let mut parts = s.split_ascii_whitespace();
9492
let range_str = parts.next().ok_or(missing_field)?;
9593
let perms_str = parts.next().ok_or(missing_field)?;

0 commit comments

Comments
 (0)