trace_printk(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’– trace_printk(9) πŸ’–

okay so bestie, quick vibe check: this is a page from the Kernel API Manual, and it's all about trace_printk β€” literally printf formatting, but the output goes into the ftrace buffer instead of your console. πŸ¦‹


✨ NAME ✨

trace_printk β€” printf formatting in the ftrace buffer, and yes it's exactly as iconic as it sounds πŸŽ€


✨ SYNOPSIS ✨

trace_printk (fmt , ... );

🌸 ARGUMENTS 🌸

  • fmt β€” the printf format for printing. same energy as printf, different destination, iykyk πŸ’…
  • ... β€” variable arguments. the little extras that get formatted in 🫢

🌈 NOTE 🌈

so first thing, __trace_printk is an internal function for trace_printk, and the ip gets passed in via the trace_printk macro. lowkey important detail, don't forget it. ⭐

this function exists so a kernel developer can debug fast path sections that printk is just not appropriate for. like, printk could never, fr fr. by scattering various printk-like tracing around in the code, a developer can quickly see where problems are occurring. it's giving detective energy πŸ”βœ¨

⚠️ big one, bestie: this is intended as a debugging tool for the developer only. please refrain from leaving trace_printks scattered around in your code. (extra memory is used for special buffers that are allocated when trace_printk is used.) so like... clean up after yourself, no cap 😭

and here's a little optimization trick that's honestly kind of iconic. if there's only one argument, there's no need to scan the string for printf formats β€” trace_puts will suffice. πŸŽ€

but wait. how can we take advantage of using trace_puts when trace_printk has only one argument? by stringifying the args and checking the size, we can tell whether or not there are args. __stringify((__VA_ARGS__)) will turn into "() " with a size of 3 when there are no args, and anything else will be bigger. all we need to do is define a string to this, and then take its size and compare to 3. if it's bigger, use do_trace_printk; otherwise, optimize it to trace_puts. then just let gcc optimize the rest. 🌟

that's it. that's the whole trick. compiler does the rest of the work, main character energy πŸ‘‘


🌟 SEE ALSO 🌟


mwah πŸ’œ that's the whole page! it lives at ./include/linux, and this edition is dated October 2026, so like, bookmark it or whatever πŸ¦‹βœ¨

πŸ“¦ where this page comes from