From b8a94775fed85851080ee2eb7483742e891633e7 Mon Sep 17 00:00:00 2001 From: The 8472 Date: Sat, 11 Dec 2021 15:18:39 +0100 Subject: [PATCH] How to printf-debug alloc/core --- src/SUMMARY.md | 5 +++ src/development/building-and-debugging.md | 38 +++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/development/building-and-debugging.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 16d15ba..252eb12 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -9,6 +9,11 @@ - [Membership](./membership.md) - [Reviewing](./reviewing.md) +--- + +- [Building and debugging libraries](./development/building-and-debugging.md) + + --- - [The feature lifecycle](./feature-lifecycle/summary.md) diff --git a/src/development/building-and-debugging.md b/src/development/building-and-debugging.md new file mode 100644 index 0000000..5b4f91a --- /dev/null +++ b/src/development/building-and-debugging.md @@ -0,0 +1,38 @@ + +# Building and Debugging the library crates + +Most of the [instructions from the rustc-dev-guide][rustc-guide] also apply to the standard library since +it is built with the same build system, so it is recommended to read it first. + +[rustc-guide]: https://rustc-dev-guide.rust-lang.org/building/how-to-build-and-run.html + +# Println-debugging alloc and core + +Since logging and IO APIs are not available in `alloc` and `core` advice meant for the rest of the compiler +is not applicable here. + +Instead one can either extract the code that should be tested to a normal crate and add debugging statements there or +on POSIX systems one can use the following hack: + +```rust +extern "C" { + fn dprintf(fd: i32, s: *const u8, ...); +} + +macro_rules! dbg_printf { + ($s:expr) => { + unsafe { dprintf(2, "%s\0".as_ptr(), $s as *const u8); } + } +} + +fn function_to_debug() { + let dbg_str = format!("debug: {}\n\0", "hello world"); + dbg_printf!(dbg_str.as_bytes().as_ptr()); +} +``` + +Then one can run a test which exercises the code to debug and show the error output via + +```shell,ignore +./x.py test library/alloc --test-args --test-args --no-capture +``` \ No newline at end of file