Skip to contentSkip to conversation

rust: introduce sfile macro for succinct code tracing

PATCH Superseded Timur Tabi 4 participants 7 messages Jun 3, 2025 – Jun 7, 2025 Rust

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Timur Tabi

author
#0 lore raw

PATCH v2 1/1 rust: introduce sfile macro for succinct code tracing

Introduce the sfile (short file) macro that returns the stem of the current source file filename.

Rust provides a file!() macro that is similar to C's __FILE__ predefined macro. Unlike __FILE__, however, file!() returns a full path, which is klunky when used for debug traces such as

	pr_info!("{}:{}\n", file!(), line!());

sfile!() can be used in situations instead, to provide a more compact print. For example, if file!() returns "rust/kernel/print.rs", sfile!() returns just "print".

The macro avoids str::rfind() because currently, that function is not const. The compiler emits a call to memrchr, even when called on string literals. Instead, the macro implements its own versions of rfind(), allowing the compiler to generate the slice at compile time.

The macro also uses some unsafe functions in order to avoid indexing into a str, which apparently is not completely a const operation.

---
 rust/kernel/print.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
rust/kernel/print.rs +63 -0
index cf4714242e14..1fd1497af887 100644
@@ -414,3 +414,66 @@ macro_rules! pr_cont (
         $crate::print_macro!($crate::print::format_strings::CONT, true, $($arg)*)
     )
 );
+
+/// Returns just the base filename of the current file.  This differs from the
+/// built-in file!() macro, which returns the full path of the current file.
+/// Using the base filename gives you more succinct logging prints.
+///
+/// # Examples
+///
+/// ```
+/// pr_err!("{}:{}\n", sfile!(), line!());
+/// ```
+#[macro_export]
+macro_rules! sfile {
+    () => {{
+        const FILE: &str = file!();
+        assert!(FILE.is_ascii()); // .as_bytes() does not support non-ascii filenames
+
+        // Return the index of the last occurrence of `needle` in `haystack`,
+        // or zero if not found.  We can't use rfind() because it's not const (yet).
+        // Avoiding rfind() allows this macro to be evaluated at compile time.
+        const fn find_last_or_zero(haystack: &str, needle: char) -> usize {
+            let bytes = haystack.as_bytes();
+            let mut i = haystack.len();
+            while i > 0 {
+                i -= 1;
+                if bytes[i] == needle as u8 {
+                    return i;
+                }
+            }
+            0
+        }
+
+        // Return the index of the last occurrence of `needle` in `haystack`,
+        // or the length of the string if not found.
+        const fn find_last_or_len(haystack: &str, needle: char) -> usize {
+            let len = haystack.len();
+            let bytes = haystack.as_bytes();
+            let mut i = len;
+            while i > 0 {
+                i -= 1;
+                if bytes[i] == needle as u8 {
+                    return i;
+                }
+            }
+            len
+        }
+        let start = find_last_or_zero(FILE, '/') + 1;
+        let end = find_last_or_len(FILE, '.');
+
+        // We use these unsafe functions because apparently indexing into
+        // a string using normal Rust methods is not completely const operation.
+
+        let base_ptr: *const u8 = FILE.as_ptr();
+        // SAFETY: We know that `start` is <= the length of FILE, because FILE
+        // never ends in a slash.
+        let ptr: *const u8 = unsafe { base_ptr.add(start) };
+        // SAFETY: We also know that `end` < the length of FILE.
+        // If `end` < `start`, this will generate a compiler error.
+        let slice = unsafe { core::slice::from_raw_parts(ptr, end - start) };
+        // SAFETY: We know that the slice is valid UTF-8, because we checked
+        // that FILE is ASCII (via is_ascii() above).
+        unsafe { core::str::from_utf8_unchecked(slice) }
+    }};
+}
Signature · 1 line
2.43.0
#1 lore raw
Quoted 2 lines · Timur Tabi

+/// built-in file!() macro, which returns the full path of the current file.

This is missing a code span and it is still using a single sentence for the "short title".

rust/kernel/print.rs macro_rules! pr_cont ( · line 426

Quoted 3 lines
rust/kernel/print.rs +3 -0
+/// ```
+/// pr_err!("{}:{}\n", sfile!(), line!());
+/// ```

Examples are built and tested, so please make sure they work -- this will likely fail to build, since sfile! is not imported nor in the prelude.

Quoted 1 line

+ assert!(FILE.is_ascii()); // .as_bytes() does not support non-ascii filenames

We generally avoid comments after a statement, instead we typically put them in the previous line.

Also: code span is missing, "ASCII", and we end them with periods.

rust/kernel/print.rs macro_rules! pr_cont ( · line 435

Quoted 3 lines
rust/kernel/print.rs +3 -0
+        // Return the index of the last occurrence of `needle` in `haystack`,
+        // or zero if not found.  We can't use rfind() because it's not const (yet).
+        // Avoiding rfind() allows this macro to be evaluated at compile time.

The first sentence should be ///, and the rest a comment. It also lacks code spans.

rust/kernel/print.rs macro_rules! pr_cont ( · line 466

Quoted 2 lines
rust/kernel/print.rs +2 -0
+        // We use these unsafe functions because apparently indexing into
+        // a string using normal Rust methods is not completely const operation.

Hmm... I am confused about what this is trying to say. rfind is not const, but that does not imply "indexing into a string" is not const (what "indexing into a string" means here?)

Also, we should try to avoid "apparently". And what does "not completely const" mean here?

By the way, please use the standard "[...]" for patch titles -- you can pass e.g. -v2 to Git for that. (And ideally please provide --base too).

Thanks!

Cheers,
Miguel

author
#2 lore raw
Quoted 20 lines · Miguel Ojeda
Quoted 2 lines · Timur Tabi

+/// built-in file!() macro, which returns the full path of the current file.

This is missing a code span and it is still using a single sentence for the "short title".

rust/kernel/print.rs macro_rules! pr_cont ( · line 426

Quoted 3 lines
rust/kernel/print.rs +3 -0
+/// ```
+/// pr_err!("{}:{}\n", sfile!(), line!());
+/// ```

Examples are built and tested, so please make sure they work -- this will likely fail to build, since sfile! is not imported nor in the prelude.

Quoted 1 line

+        assert!(FILE.is_ascii()); // .as_bytes() does not support non-ascii filenames

We generally avoid comments after a statement, instead we typically put them in the previous line.

Quoted 8 lines

Also: code span is missing, "ASCII", and we end them with periods.

Quoted 3 lines

+        // Return the index of the last occurrence of needle in haystack, +        // or zero if not found.  We can't use rfind() because it's not const (yet). +        // Avoiding rfind() allows this macro to be evaluated at compile time.

The first sentence should be ///, and the rest a comment. It also lacks code spans.

Ok, I think I got it this time.

Quoted 9 lines
Quoted 2 lines

+        // We use these unsafe functions because apparently indexing into +        // a string using normal Rust methods is not completely const operation.

Hmm... I am confused about what this is trying to say. rfind is not const, but that does not imply "indexing into a string" is not const (what "indexing into a string" means here?)

Also, we should try to avoid "apparently". And what does "not completely const" mean here?

Sorry, I didn't really understand your explanation, and I guess I failed to summarize it. I don't know what "not completely const" means, either.

I don't understand why, when I use "&FILE[start..]", this compiles as inline:

	pr_err!("{}:{}\n", sfile!(), line!());

but this doesn't:

	const SFILE: &'static str = sfile!();

It seems to me that the second line is "more const" than the first line, if that makes any sense.

What I was trying to say is that I need to use all those crazy unsafe calls so that the second line works, but I can get away with just "&FILE[start..]" if I only cared about the first line.

Quoted 3 lines

By the way, please use the standard "[...]" for patch titles -- you can pass e.g. -v2 to Git for that. (And ideally please provide --base too).

You mean you don't like [PATCH][V2]? I've been doing that for 20 years, and this is the first time anyone's complained.

#3 lore raw
Quoted 2 lines · Timur Tabi
Quoted 1 line · Miguel Ojeda

On Tue, Jun 3, 2025 at 9:55 PM Timur Tabi <[email protected]> wrote:

...>> By the way, please use the standard "[...]" for patch titles -- you

Quoted 2 lines
Quoted 1 line

can pass e.g. -v2 to Git for that. (And ideally please provide --base too).

--base is quite helpful, please don't overlook that, too.

Quoted 2 lines

You mean you don't like [PATCH][V2]? I've been doing that for 20 years, and this is the first time anyone's complained.

During those 20 years, the kernel community moved to a mostly git-based
workflow. And git's "-v2" is a neat little time saver that also ensures that your patchsets look "standard" to...pretty much everyone else here.

It's less about what you have been doing, and more about what others expect to see.

thanks,

Signature · 1 line
John Hubbard
#4 lore raw
In reply to Timur Tabi
Quoted 10 lines · Timur Tabi

I don't understand why, when I use "&FILE[start..]", this compiles as inline:

        pr_err!("{}:{}\n", sfile!(), line!());

but this doesn't:

        const SFILE: &'static str = sfile!();

It seems to me that the second line is "more const" than the first line, if that makes any sense.

The second call is in a const context, the first is just a normal line. So the second must be a constant expression, which is a big restriction on what you can write there, but the first doesn't require that.

Hmm... Perhaps you are thinking that the compiler (or optimizer) should know that it all resolves to a constant, and thus it should allow it anyway.

However, constant evaluation doesn't work like that, i.e. even if some expressions may happen to be able to be evaluated at compile-time, it doesn't make them constant expressions.

It is similar to how C++'s constexpr works, if you have done that. The languages keep expanding what can be done within const contexts over time, but it is not based on whether a given instance could be.

(One place where we do use "this particular case must be X" kind of logic is build_assert!).

Quoted 2 lines

You mean you don't like [PATCH][V2]? I've been doing that for 20 years, and this is the first time anyone's complained.

Sorry, I should have said "ideally" there too, since I intended it mostly as a suggestion like the one in parenthesis. Neither of those are blockers, of course, and we regularly take different styles of patches. The --base thing is more "important" (useful).

Cheers,
Miguel

reviewer
#5 lore raw
In reply to Timur Tabi
Quoted 18 lines · Timur Tabi

Introduce the sfile (short file) macro that returns the stem of the current source file filename.

Rust provides a file!() macro that is similar to C's __FILE__ predefined macro. Unlike __FILE__, however, file!() returns a full path, which is klunky when used for debug traces such as

	pr_info!("{}:{}\n", file!(), line!());

sfile!() can be used in situations instead, to provide a more compact print. For example, if file!() returns "rust/kernel/print.rs", sfile!() returns just "print".

The macro avoids str::rfind() because currently, that function is not const. The compiler emits a call to memrchr, even when called on string literals. Instead, the macro implements its own versions of rfind(), allowing the compiler to generate the slice at compile time.

Quoted 4 lines

The macro also uses some unsafe functions in order to avoid indexing into a str, which apparently is not completely a const operation.

What is the use-case for this macro?

Quoted 47 lines
 rust/kernel/print.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
rust/kernel/print.rs +36 -0
index cf4714242e14..1fd1497af887 100644
@@ -414,3 +414,66 @@ macro_rules! pr_cont (
         $crate::print_macro!($crate::print::format_strings::CONT, true, $($arg)*)
     )
 );
+
+/// Returns just the base filename of the current file.  This differs from the
+/// built-in file!() macro, which returns the full path of the current file.
+/// Using the base filename gives you more succinct logging prints.
+///
+/// # Examples
+///
+/// ```
+/// pr_err!("{}:{}\n", sfile!(), line!());
+/// ```
+#[macro_export]
+macro_rules! sfile {
+    () => {{
+        const FILE: &str = file!();
+        assert!(FILE.is_ascii()); // .as_bytes() does not support non-ascii filenames
+
+        // Return the index of the last occurrence of `needle` in `haystack`,
+        // or zero if not found.  We can't use rfind() because it's not const (yet).
+        // Avoiding rfind() allows this macro to be evaluated at compile time.
+        const fn find_last_or_zero(haystack: &str, needle: char) -> usize {
+            let bytes = haystack.as_bytes();
+            let mut i = haystack.len();
+            while i > 0 {
+                i -= 1;
+                if bytes[i] == needle as u8 {
+                    return i;
+                }
+            }
+            0
+        }
+
+        // Return the index of the last occurrence of `needle` in `haystack`,
+        // or the length of the string if not found.
+        const fn find_last_or_len(haystack: &str, needle: char) -> usize {
+            let len = haystack.len();
+            let bytes = haystack.as_bytes();

rust/kernel/print.rs macro_rules! pr_cont ( · line 462

Quoted 10 lines
rust/kernel/print.rs +10 -0
+            let mut i = len;
+            while i > 0 {
+                i -= 1;
+                if bytes[i] == needle as u8 {
+                    return i;
+                }
+            }
+            len
+        }
+        let start = find_last_or_zero(FILE, '/') + 1;

Is plus one really correct when this returns zero?

rust/kernel/print.rs macro_rules! pr_cont ( · line 463

Quoted 1 line
rust/kernel/print.rs +1 -0
+        let end = find_last_or_len(FILE, '.');

I'm thinking that you should only have one method here returning Option, and that the two calls should match on the Option.

let start = match find_last(FILE, '/') {
    Some(slash) => slash + 1,
    None => 0,
};
let end = match find_last(FILE, '.') {
    Some(dot) => dot,
    None => FILE.len(),
};
Quoted 4 lines
rust/kernel/print.rs +4 -0
+
+        // We use these unsafe functions because apparently indexing into
+        // a string using normal Rust methods is not completely const operation.
+

rust/kernel/print.rs macro_rules! pr_cont ( · line 477

Quoted 10 lines
rust/kernel/print.rs +10 -0
+        let base_ptr: *const u8 = FILE.as_ptr();
+        // SAFETY: We know that `start` is <= the length of FILE, because FILE
+        // never ends in a slash.
+        let ptr: *const u8 = unsafe { base_ptr.add(start) };
+        // SAFETY: We also know that `end` < the length of FILE.
+        // If `end` < `start`, this will generate a compiler error.
+        let slice = unsafe { core::slice::from_raw_parts(ptr, end - start) };
+        // SAFETY: We know that the slice is valid UTF-8, because we checked
+        // that FILE is ASCII (via is_ascii() above).
+        unsafe { core::str::from_utf8_unchecked(slice) }

Instead of this, I'm thinking you could have a const str slicing method. You can use is_char_boundary() asserts to get rid of the ASCII check.

Alice

author
#6 lore raw
Quoted 19 lines · Alice Ryhl
Quoted 18 lines · Timur Tabi

Introduce the sfile (short file) macro that returns the stem of the current source file filename.

Rust provides a file!() macro that is similar to C's __FILE__ predefined macro.  Unlike __FILE__, however, file!() returns a full path, which is klunky when used for debug traces such as

	pr_info!("{}:{}\n", file!(), line!());

sfile!() can be used in situations instead, to provide a more compact print.  For example, if file!() returns "rust/kernel/print.rs", sfile!() returns just "print".

The macro avoids str::rfind() because currently, that function is not const.  The compiler emits a call to memrchr, even when called on string literals.  Instead, the macro implements its own versions of rfind(), allowing the compiler to generate the slice at compile time.

Quoted 6 lines
Quoted 4 lines

The macro also uses some unsafe functions in order to avoid indexing into a str, which apparently is not completely a const operation.

What is the use-case for this macro?

That's been covered in other posts on this thread. Short answer: it's like dbg!() but less noisy.

Quoted 4 lines
Quoted 2 lines

+        let start = find_last_or_zero(FILE, '/') + 1;

Is plus one really correct when this returns zero?

Oh, you're right! I'll fix that in v3. Thanks!

Quoted 5 lines
Quoted 1 line

+        let end = find_last_or_len(FILE, '.');

I'm thinking that you should only have one method here returning Option, and that the two calls should match on the Option.

Quoted 8 lines

let start = match find_last(FILE, '/') {
    Some(slash) => slash + 1,
    None => 0,
};
let end = match find_last(FILE, '.') {
    Some(dot) => dot,
    None => FILE.len(),
};

Hmmmm... that seems like a good idea. I'll try it out, thanks.

Quoted 14 lines
Quoted 5 lines

+        // We use these unsafe functions because apparently indexing into +        // a string using normal Rust methods is not completely const operation. +
+        let base_ptr: *const u8 = FILE.as_ptr();
+        // SAFETY: We know that start is <= the length of FILE, because FILE

Quoted 8 lines

+        // never ends in a slash.
+        let ptr: *const u8 = unsafe { base_ptr.add(start) };
+        // SAFETY: We also know that end < the length of FILE. +        // If end < start, this will generate a compiler error. +        let slice = unsafe { core::slice::from_raw_parts(ptr, end - start) }; +        // SAFETY: We know that the slice is valid UTF-8, because we checked +        // that FILE is ASCII (via is_ascii() above).
+        unsafe { core::str::from_utf8_unchecked(slice) }

Quoted 2 lines

Instead of this, I'm thinking you could have a const str slicing method. You can use is_char_boundary() asserts to get rid of the ASCII check.

Um, can you spell that out a bit more? I had to use AI to come up with this current monstrosity, and I'm still not 100% sure how it works.