Skip to content

How to printf-debug alloc/core #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2022
Merged

Conversation

the8472
Copy link
Member

@the8472 the8472 commented Dec 11, 2021

credit for the hack goes to @nnethercote

@nnethercote
Copy link

Credit also to @bjorn3 who gave me the idea and an earlier version of the code.

@nnethercote
Copy link

This is very useful info and I have been using this hack repeatedly since I learned about it.

I have a slightly modified version I've been using:

extern "C" { fn dprintf(fd: i32, s: *const u8, ...); }
macro_rules! d { ($s:expr) => { unsafe { dprintf(2, $s.as_ptr() as *const u8); } } }

fn function_to_debug() {
    // Note: trailing '\0' is critical!
    d!("hello world\n\0");
    d!(format!("debug: {}\n\0", "hello world"));
}

It's more concise, easier to copy and paste, and easier to comment out.

@nnethercote
Copy link

Another comment: if you use this in code that format! relies on you'll get infinite loops, which can be confusing. E.g. I had this problem when I had a d! call in the RawVec growth code path.

@the8472 the8472 force-pushed the printf-debugging branch from 6302699 to 8883586 Compare May 27, 2022 21:15
@the8472 the8472 force-pushed the printf-debugging branch from 8883586 to b8a9477 Compare May 27, 2022 22:41
@yaahc yaahc merged commit 4f10401 into rust-lang:master May 27, 2022
github-actions bot pushed a commit that referenced this pull request May 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants