Skip to content

Commit 2ba145c

Browse files
bors[bot]japaric
andcommitted
Merge #24
24: Logging with symbols r=therealprof a=japaric Obscure stuff with symbols; perfect fit for the nomicon, IMO. r? @rust-embedded/resources (anyone) Co-authored-by: Jorge Aparicio <[email protected]>
2 parents eb89516 + 53d1567 commit 2ba145c

37 files changed

+801
-9
lines changed

.travis.yml

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
language: rust
22

3-
addons:
4-
apt:
5-
packages:
6-
- qemu-system-arm
7-
83
matrix:
94
include:
105
- rust: beta
11-
12-
matrix:
13-
include:
146
- rust: nightly
157

168
install:
179
- bash ci/install.sh
18-
- export PATH="$PATH:$PWD/gcc/bin"
10+
- export PATH="$PATH:$PWD/gcc/bin:$PWD/qemu"
1911

2012
script:
2113
- bash ci/script.sh

ci/install.sh

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ main() {
1616

1717
curl -L https://developer.arm.com/-/media/Files/downloads/gnu-rm/7-2018q2/gcc-arm-none-eabi-7-2018-q2-update-linux.tar.bz2?revision=bc2c96c0-14b5-4bb4-9f18-bceb4050fee7?product=GNU%20Arm%20Embedded%20Toolchain,64-bit,,Linux,7-2018-q2-update | tar --strip-components=1 -C gcc -xj
1818

19+
mkdir qemu
20+
curl -L https://github.com/japaric/qemu-bin/raw/master/14.04/qemu-system-arm-2.12.0 > qemu/qemu-system-arm
21+
chmod +x qemu/qemu-system-arm
22+
1923
rustup component add llvm-tools-preview
2024

2125
curl -LSfs https://japaric.github.io/trust/install.sh | \

ci/logging/app/.cargo/config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[target.thumbv7m-none-eabi]
2+
runner = "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
3+
rustflags = ["-C", "link-arg=-Tlink.x"]
4+
5+
[build]
6+
target = "thumbv7m-none-eabi"

ci/logging/app/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
authors = ["Jorge Aparicio <[email protected]>"]
3+
edition = "2018"
4+
name = "app"
5+
version = "0.1.0"
6+
7+
[dependencies]
8+
cortex-m-semihosting = "0.3.1"
9+
rt = { path = "../rt" }
10+
11+
[profile.release]
12+
codegen-units = 1
13+
lto = true

ci/logging/app/dev.objdump

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
00001fe1 l .rodata 00000001 Goodbye
2+
00001fe0 l .rodata 00000001 Hello, world!

ci/logging/app/dev.out

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
0x1fe0
2+
0x1fe1

ci/logging/app/release.objdump

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
00000a7d l .rodata 00000001 Goodbye
2+
00000a7c l .rodata 00000001 Hello, world!

ci/logging/app/release.out

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
0xa7c
2+
0xa7d

ci/logging/app/src/main.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
use core::fmt::Write;
5+
use cortex_m_semihosting::{debug, hio};
6+
7+
use rt::entry;
8+
9+
entry!(main);
10+
11+
fn main() -> ! {
12+
let mut hstdout = hio::hstdout().unwrap();
13+
14+
#[export_name = "Hello, world!"]
15+
static A: u8 = 0;
16+
17+
writeln!(hstdout, "{:#x}", &A as *const u8 as usize);
18+
19+
#[export_name = "Goodbye"]
20+
static B: u8 = 0;
21+
22+
writeln!(hstdout, "{:#x}", &B as *const u8 as usize);
23+
24+
debug::exit(debug::EXIT_SUCCESS);
25+
26+
loop {}
27+
}

ci/logging/app2/.cargo/config

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[target.thumbv7m-none-eabi]
2+
runner = "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
3+
rustflags = [
4+
"-C", "link-arg=-Tlink.x",
5+
"-C", "link-arg=-Tlog.x", # <- NEW!
6+
]
7+
8+
[build]
9+
target = "thumbv7m-none-eabi"

ci/logging/app2/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
authors = ["Jorge Aparicio <[email protected]>"]
3+
edition = "2018"
4+
name = "app"
5+
version = "0.1.0"
6+
7+
[dependencies]
8+
cortex-m-semihosting = "0.3.1"
9+
rt = { path = "../rt" }
10+
11+
[profile.release]
12+
codegen-units = 1
13+
lto = true

ci/logging/app2/dev.objdump

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
00000001 l .log 00000001 Goodbye
2+
00000000 l .log 00000001 Hello, world!

ci/logging/app2/dev.out

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0001

ci/logging/app2/log.x

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SECTIONS
2+
{
3+
.log 0 (INFO) : {
4+
*(.log);
5+
}
6+
}

ci/logging/app2/src/main.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
use cortex_m_semihosting::{debug, hio};
5+
6+
use rt::entry;
7+
8+
entry!(main);
9+
10+
fn main() -> ! {
11+
let mut hstdout = hio::hstdout().unwrap();
12+
13+
#[export_name = "Hello, world!"]
14+
#[link_section = ".log"] // <- NEW!
15+
static A: u8 = 0;
16+
17+
let address = &A as *const u8 as usize as u8;
18+
hstdout.write_all(&[address]).unwrap(); // <- CHANGED!
19+
20+
#[export_name = "Goodbye"]
21+
#[link_section = ".log"] // <- NEW!
22+
static B: u8 = 0;
23+
24+
let address = &B as *const u8 as usize as u8;
25+
hstdout.write_all(&[address]).unwrap(); // <- CHANGED!
26+
27+
debug::exit(debug::EXIT_SUCCESS);
28+
29+
loop {}
30+
}

ci/logging/app3/.cargo

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../app2/.cargo

ci/logging/app3/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
authors = ["Jorge Aparicio <[email protected]>"]
3+
edition = "2018"
4+
name = "app"
5+
version = "0.1.0"
6+
7+
[dependencies]
8+
cortex-m-semihosting = "0.3.1"
9+
log = { path = "../log" }
10+
rt = { path = "../rt" }

ci/logging/app3/dev.objdump

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
00000001 l .log 00000001 Goodbye
2+
00000000 l .log 00000001 Hello, world!

ci/logging/app3/dev.out

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0001

ci/logging/app3/src/main.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
use cortex_m_semihosting::{
5+
debug,
6+
hio::{self, HStdout},
7+
};
8+
9+
use log::{log, Log};
10+
use rt::entry;
11+
12+
struct Logger {
13+
hstdout: HStdout,
14+
}
15+
16+
impl Log for Logger {
17+
type Error = ();
18+
19+
fn log(&mut self, address: u8) -> Result<(), ()> {
20+
self.hstdout.write_all(&[address])
21+
}
22+
}
23+
24+
entry!(main);
25+
26+
fn main() -> ! {
27+
let hstdout = hio::hstdout().unwrap();
28+
let mut logger = Logger { hstdout };
29+
30+
log!(logger, "Hello, world!");
31+
32+
log!(logger, "Goodbye");
33+
34+
debug::exit(debug::EXIT_SUCCESS);
35+
36+
loop {}
37+
}

ci/logging/app4/.cargo

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../app2/.cargo

ci/logging/app4/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
authors = ["Jorge Aparicio <[email protected]>"]
3+
edition = "2018"
4+
name = "app"
5+
version = "0.1.0"
6+
7+
[dependencies]
8+
cortex-m-semihosting = "0.3.1"
9+
log = { path = "../log2" }
10+
rt = { path = "../rt" }

ci/logging/app4/dev.objdump

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
00000000 l .log 00000001 Goodbye
2+
00000001 l .log 00000001 Hello, world!
3+
00000001 .log 00000000 __log_warning_start__

ci/logging/app4/dev.out

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0100

ci/logging/app4/src/main.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
use cortex_m_semihosting::{
5+
debug,
6+
hio::{self, HStdout},
7+
};
8+
9+
use log::{error, warn, Log};
10+
use rt::entry;
11+
12+
entry!(main);
13+
14+
fn main() -> ! {
15+
let hstdout = hio::hstdout().unwrap();
16+
let mut logger = Logger { hstdout };
17+
18+
warn!(logger, "Hello, world!"); // <- CHANGED!
19+
20+
error!(logger, "Goodbye"); // <- CHANGED!
21+
22+
debug::exit(debug::EXIT_SUCCESS);
23+
24+
loop {}
25+
}
26+
27+
struct Logger {
28+
hstdout: HStdout,
29+
}
30+
31+
impl Log for Logger {
32+
type Error = ();
33+
34+
fn log(&mut self, address: u8) -> Result<(), ()> {
35+
self.hstdout.write_all(&[address])
36+
}
37+
}

ci/logging/log/Cargo.toml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "log"
3+
version = "0.1.0"
4+
authors = ["Jorge Aparicio <[email protected]>"]
5+
edition = "2018"
6+
7+
[dependencies]

ci/logging/log/build.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::{env, error::Error, fs::File, io::Write, path::PathBuf};
2+
3+
fn main() -> Result<(), Box<Error>> {
4+
// Put the linker script somewhere the linker can find it
5+
let out = PathBuf::from(env::var("OUT_DIR")?);
6+
7+
File::create(out.join("log.x"))?.write_all(include_bytes!("log.x"))?;
8+
9+
println!("cargo:rustc-link-search={}", out.display());
10+
11+
Ok(())
12+
}

ci/logging/log/log.x

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SECTIONS
2+
{
3+
.log 0 (INFO) : {
4+
*(.log);
5+
}
6+
}

ci/logging/log/src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![no_std]
2+
3+
pub trait Log {
4+
type Error;
5+
6+
fn log(&mut self, address: u8) -> Result<(), Self::Error>;
7+
}
8+
9+
#[macro_export]
10+
macro_rules! log {
11+
($logger:expr, $string:expr) => {{
12+
#[export_name = $string]
13+
#[link_section = ".log"]
14+
static SYMBOL: u8 = 0;
15+
16+
$crate::Log::log(&mut $logger, &SYMBOL as *const u8 as usize as u8)
17+
}};
18+
}

ci/logging/log2/Cargo.toml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "log"
3+
version = "0.1.0"
4+
authors = ["Jorge Aparicio <[email protected]>"]
5+
edition = "2018"
6+
7+
[dependencies]

ci/logging/log2/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../log/build.rs

ci/logging/log2/log.x

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SECTIONS
2+
{
3+
.log 0 (INFO) : {
4+
*(.log.error);
5+
__log_warning_start__ = .;
6+
*(.log.warning);
7+
}
8+
}

ci/logging/log2/src/lib.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![no_std]
2+
3+
pub trait Log {
4+
type Error;
5+
6+
fn log(&mut self, address: u8) -> Result<(), Self::Error>;
7+
}
8+
9+
/// Logs messages at the ERROR log level
10+
#[macro_export]
11+
macro_rules! error {
12+
($logger:expr, $string:expr) => {{
13+
#[export_name = $string]
14+
#[link_section = ".log.error"] // <- CHANGED!
15+
static SYMBOL: u8 = 0;
16+
17+
$crate::Log::log(&mut $logger, &SYMBOL as *const u8 as usize as u8)
18+
}};
19+
}
20+
21+
/// Logs messages at the WARNING log level
22+
#[macro_export]
23+
macro_rules! warn {
24+
($logger:expr, $string:expr) => {{
25+
#[export_name = $string]
26+
#[link_section = ".log.warning"] // <- CHANGED!
27+
static SYMBOL: u8 = 0;
28+
29+
$crate::Log::log(&mut $logger, &SYMBOL as *const u8 as usize as u8)
30+
}};
31+
}

ci/logging/rt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../exceptions/rt

0 commit comments

Comments
 (0)