trace_printk(9)
π 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 leavingtrace_printksscattered around in your code. (extra memory is used for special buffers that are allocated whentrace_printkis 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 π
- Kernel file
./include/linux/trace_printk.h trace_puts(9)
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
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms