Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit 2b10860

Browse files
author
Jorge Aparicio
committed
use libc::mkfifo instead of the external mkfifo command
1 parent f58ef1c commit 2b10860

File tree

5 files changed

+233
-25
lines changed

5 files changed

+233
-25
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- `itmdump` no longer depends on the `mkfifo` command.
13+
1014
## v0.1.0 - 2016-10-03
1115

1216
### Added

Cargo.lock

Lines changed: 156 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ repository = "https://github.com/japaric/itm"
99
version = "0.1.0"
1010

1111
[dependencies]
12+
clap = "2.14.0"
13+
error-chain = "0.5.0"
14+
libc = "0.2.17"
1215
ref_slice = "1.1.0"

src/bin/itmdump.rs

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,84 @@
1+
extern crate clap;
2+
extern crate libc;
13
extern crate ref_slice;
24

5+
#[macro_use]
6+
extern crate error_chain;
7+
8+
use std::ffi::CString;
39
use std::fs::File;
410
use std::io::{Read, Write};
11+
use std::os::unix::ffi::OsStringExt;
512
use std::path::PathBuf;
6-
use std::process::Command;
713
use std::{env, fs, io, process};
814

15+
use clap::{App, Arg};
16+
use ref_slice::ref_slice_mut;
17+
18+
use errors::*;
19+
20+
mod errors {
21+
error_chain!();
22+
}
23+
924
fn main() {
10-
let pipe = &PathBuf::from(env::args_os()
11-
.skip(1)
12-
.next()
13-
.unwrap_or_else(|| exit("expected one argument: the path to the named pipe")));
25+
if let Err(e) = run() {
26+
let stderr = io::stderr();
27+
let mut stderr = stderr.lock();
28+
29+
writeln!(stderr, "{}", e).ok();
30+
31+
for e in e.iter().skip(1) {
32+
writeln!(stderr, "caused by: {}", e).ok();
33+
}
34+
35+
if show_backtrace() {
36+
if let Some(backtrace) = e.backtrace() {
37+
writeln!(stderr, "backtrace:").ok();
38+
writeln!(stderr, "{:?}", backtrace).ok();
39+
}
40+
}
41+
42+
process::exit(1)
43+
}
44+
}
45+
46+
fn run() -> Result<()> {
47+
let matches = App::new("itmdump")
48+
.version(env!("CARGO_PKG_VERSION"))
49+
.arg(Arg::with_name("PATH").help("Named pipe to use").required(true))
50+
.get_matches();
51+
52+
let pipe = PathBuf::from(matches.value_of("PATH").unwrap());
53+
let pipe_ = pipe.display();
1454

1555
if pipe.exists() {
16-
fs::remove_file(pipe)
17-
.unwrap_or_else(|_| exit(&format!("couldn't remove {}", pipe.display())));
56+
try!(fs::remove_file(&pipe)
57+
.chain_err(|| format!("couldn't remove {}", pipe_)));
1858
}
1959

20-
let output = Command::new("mkfifo").arg(pipe).output().unwrap_or_else(|_| {
21-
exit("`mkfifo` not found");
22-
});
60+
let cpipe = try!(CString::new(pipe.clone().into_os_string().into_vec())
61+
.chain_err(|| format!("error converting {} to a C string", pipe_)));
2362

24-
if !output.status.success() {
25-
exit(&String::from_utf8_lossy(&output.stderr))
63+
match unsafe { libc::mkfifo(cpipe.as_ptr(), 0o644) } {
64+
0 => {}
65+
e => {
66+
try!(Err(io::Error::from_raw_os_error(e)).chain_err(|| {
67+
format!("couldn't create a named pipe in {}", pipe_)
68+
}))
69+
}
2670
}
2771

28-
let mut stream = File::open(pipe)
29-
.unwrap_or_else(|_| exit(&format!("couldn't open {}", pipe.display())));
72+
let mut stream = try!(File::open(&pipe)
73+
.chain_err(|| format!("couldn't open {}", pipe_)));
3074

3175
let mut header = 0;
3276

3377
let (stdout, stderr) = (io::stdout(), io::stderr());
3478
let (mut stdout, mut stderr) = (stdout.lock(), stderr.lock());
3579
loop {
3680
if let Err(e) = (|| {
37-
try!(stream.read_exact(ref_slice::ref_slice_mut(&mut header)));
81+
try!(stream.read_exact(ref_slice_mut(&mut header)));
3882
let port = header >> 3;
3983

4084
// Ignore all the packets that don't come from the stimulus port 0
@@ -45,7 +89,7 @@ fn main() {
4589
match header & 0b111 {
4690
0b01 => {
4791
let mut payload = 0;
48-
try!(stream.read_exact(ref_slice::ref_slice_mut(&mut payload)));
92+
try!(stream.read_exact(ref_slice_mut(&mut payload)));
4993
stdout.write_all(&[payload])
5094
}
5195
0b10 => {
@@ -69,8 +113,6 @@ fn main() {
69113
}
70114
}
71115

72-
fn exit(msg: &str) -> ! {
73-
let stderr = io::stderr();
74-
writeln!(stderr.lock(), "{}", msg).ok();
75-
process::exit(1)
116+
fn show_backtrace() -> bool {
117+
env::var("RUST_BACKTRACE").as_ref().map(|s| &s[..]) == Ok("1")
76118
}

src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@
22
//!
33
//! [ITM]: http://infocenter.arm.com/help/topic/com.arm.doc.ddi0314h/Chdbicbg.html
44
//!
5-
//! Right now, this tool only handles instrumentation packets from the stimulus port 0.
5+
//! Right now, this tool only handles instrumentation packets from the stimulus
6+
//! port 0.
67
//!
78
//! # Usage
89
//!
910
//! ``` text
1011
//! $ itmdump /tmp/itm.fifo
1112
//! ```
1213
//!
13-
//! This will create a named pipe: `/tmp/itm.fifo`. Another application, e.g. OpenOCD, will have to
14-
//! be connected (open+write) to this pipe. Here's an example command for OpenOCD+GDB that does
15-
//! that. (But [read their documentation!]).
14+
//! This will create a named pipe: `/tmp/itm.fifo`. Another application, e.g.
15+
//! OpenOCD, will have to be connected (open+write) to this pipe. Here's an
16+
//! example command for OpenOCD+GDB that does that. (But
17+
//! [read their documentation!]).
1618
//!
1719
//! [read their documentation!]: http://openocd.org/doc/html/Architecture-and-Core-Commands.html
1820
//!
1921
//! ``` text
2022
//! (gdb) monitor tpiu config internal /tmp/itm.fifo uart off 8000000
2123
//! ```
2224
//!
23-
//! `itmdump` will read from the pipe, parse the packets and write the payload to `stdout`:
25+
//! `itmdump` will read from the pipe, parse the packets and write the payload
26+
//! to `stdout`:
2427
//!
2528
//! ``` text
2629
//! PANIC at 'Hello, world!', examples/panic.rs:13

0 commit comments

Comments
 (0)