Skip to content

Commit 4f10401

Browse files
authored
Merge pull request #31 from the8472/printf-debugging
How to printf-debug alloc/core
2 parents bd69f0f + b8a9477 commit 4f10401

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/SUMMARY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
- [Membership](./membership.md)
1010
- [Reviewing](./reviewing.md)
1111

12+
---
13+
14+
- [Building and debugging libraries](./development/building-and-debugging.md)
15+
16+
1217
---
1318

1419
- [The feature lifecycle](./feature-lifecycle/summary.md)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
# Building and Debugging the library crates
3+
4+
Most of the [instructions from the rustc-dev-guide][rustc-guide] also apply to the standard library since
5+
it is built with the same build system, so it is recommended to read it first.
6+
7+
[rustc-guide]: https://rustc-dev-guide.rust-lang.org/building/how-to-build-and-run.html
8+
9+
# Println-debugging alloc and core
10+
11+
Since logging and IO APIs are not available in `alloc` and `core` advice meant for the rest of the compiler
12+
is not applicable here.
13+
14+
Instead one can either extract the code that should be tested to a normal crate and add debugging statements there or
15+
on POSIX systems one can use the following hack:
16+
17+
```rust
18+
extern "C" {
19+
fn dprintf(fd: i32, s: *const u8, ...);
20+
}
21+
22+
macro_rules! dbg_printf {
23+
($s:expr) => {
24+
unsafe { dprintf(2, "%s\0".as_ptr(), $s as *const u8); }
25+
}
26+
}
27+
28+
fn function_to_debug() {
29+
let dbg_str = format!("debug: {}\n\0", "hello world");
30+
dbg_printf!(dbg_str.as_bytes().as_ptr());
31+
}
32+
```
33+
34+
Then one can run a test which exercises the code to debug and show the error output via
35+
36+
```shell,ignore
37+
./x.py test library/alloc --test-args <test_name> --test-args --no-capture
38+
```

0 commit comments

Comments
 (0)