Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

tracing: move tracing declarations from kernel.h to a dedicated header

Tracing is a half of the kernel.h in terms of LOCs, although it's a
self-consistent part. It is intended for quick debugging purposes and
isn't used by the normal tracing utilities.

Move it to a separate header. If someone needs to just throw a
trace_printk() in their driver, they will not have to pull all the heavy
tracing machinery.

This is a pure move.

Link: https://lkml.kernel.org/r/20260116042510.241009-7-ynorov@nvidia.com
Signed-off-by: Yury Norov <ynorov@nvidia.com>
Acked-by: Steven Rostedt <rostedt@goodmis.org>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Joel Fernandes <joelagnelf@nvidia.com>
Cc: Aaron Tomlin <atomlin@atomlin.com>
Cc: Andi Shyti <andi.shyti@linux.intel.com>
Cc: Christophe Leroy (CS GROUP) <chleroy@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Jani Nikula <jani.nikula@intel.com>
Cc: Petr Pavlu <petr.pavlu@suse.com>
Cc: Randy Dunlap <rdunlap@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Yury Norov and committed by
Andrew Morton
bec261fe 86e685ff

+205 -195
+1 -195
include/linux/kernel.h
··· 31 31 #include <linux/build_bug.h> 32 32 #include <linux/sprintf.h> 33 33 #include <linux/static_call_types.h> 34 - #include <linux/instruction_pointer.h> 34 + #include <linux/trace_printk.h> 35 35 #include <linux/util_macros.h> 36 36 #include <linux/wordpart.h> 37 37 ··· 188 188 SYSTEM_SUSPEND, 189 189 }; 190 190 extern enum system_states system_state; 191 - 192 - /* 193 - * General tracing related utility functions - trace_printk(), 194 - * tracing_on/tracing_off and tracing_start()/tracing_stop 195 - * 196 - * Use tracing_on/tracing_off when you want to quickly turn on or off 197 - * tracing. It simply enables or disables the recording of the trace events. 198 - * This also corresponds to the user space /sys/kernel/tracing/tracing_on 199 - * file, which gives a means for the kernel and userspace to interact. 200 - * Place a tracing_off() in the kernel where you want tracing to end. 201 - * From user space, examine the trace, and then echo 1 > tracing_on 202 - * to continue tracing. 203 - * 204 - * tracing_stop/tracing_start has slightly more overhead. It is used 205 - * by things like suspend to ram where disabling the recording of the 206 - * trace is not enough, but tracing must actually stop because things 207 - * like calling smp_processor_id() may crash the system. 208 - * 209 - * Most likely, you want to use tracing_on/tracing_off. 210 - */ 211 - 212 - enum ftrace_dump_mode { 213 - DUMP_NONE, 214 - DUMP_ALL, 215 - DUMP_ORIG, 216 - DUMP_PARAM, 217 - }; 218 - 219 - #ifdef CONFIG_TRACING 220 - void tracing_on(void); 221 - void tracing_off(void); 222 - int tracing_is_on(void); 223 - void tracing_snapshot(void); 224 - void tracing_snapshot_alloc(void); 225 - 226 - extern void tracing_start(void); 227 - extern void tracing_stop(void); 228 - 229 - static inline __printf(1, 2) 230 - void ____trace_printk_check_format(const char *fmt, ...) 231 - { 232 - } 233 - #define __trace_printk_check_format(fmt, args...) \ 234 - do { \ 235 - if (0) \ 236 - ____trace_printk_check_format(fmt, ##args); \ 237 - } while (0) 238 - 239 - /** 240 - * trace_printk - printf formatting in the ftrace buffer 241 - * @fmt: the printf format for printing 242 - * 243 - * Note: __trace_printk is an internal function for trace_printk() and 244 - * the @ip is passed in via the trace_printk() macro. 245 - * 246 - * This function allows a kernel developer to debug fast path sections 247 - * that printk is not appropriate for. By scattering in various 248 - * printk like tracing in the code, a developer can quickly see 249 - * where problems are occurring. 250 - * 251 - * This is intended as a debugging tool for the developer only. 252 - * Please refrain from leaving trace_printks scattered around in 253 - * your code. (Extra memory is used for special buffers that are 254 - * allocated when trace_printk() is used.) 255 - * 256 - * A little optimization trick is done here. If there's only one 257 - * argument, there's no need to scan the string for printf formats. 258 - * The trace_puts() will suffice. But how can we take advantage of 259 - * using trace_puts() when trace_printk() has only one argument? 260 - * By stringifying the args and checking the size we can tell 261 - * whether or not there are args. __stringify((__VA_ARGS__)) will 262 - * turn into "()\0" with a size of 3 when there are no args, anything 263 - * else will be bigger. All we need to do is define a string to this, 264 - * and then take its size and compare to 3. If it's bigger, use 265 - * do_trace_printk() otherwise, optimize it to trace_puts(). Then just 266 - * let gcc optimize the rest. 267 - */ 268 - 269 - #define trace_printk(fmt, ...) \ 270 - do { \ 271 - char _______STR[] = __stringify((__VA_ARGS__)); \ 272 - if (sizeof(_______STR) > 3) \ 273 - do_trace_printk(fmt, ##__VA_ARGS__); \ 274 - else \ 275 - trace_puts(fmt); \ 276 - } while (0) 277 - 278 - #define do_trace_printk(fmt, args...) \ 279 - do { \ 280 - static const char *trace_printk_fmt __used \ 281 - __section("__trace_printk_fmt") = \ 282 - __builtin_constant_p(fmt) ? fmt : NULL; \ 283 - \ 284 - __trace_printk_check_format(fmt, ##args); \ 285 - \ 286 - if (__builtin_constant_p(fmt)) \ 287 - __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ 288 - else \ 289 - __trace_printk(_THIS_IP_, fmt, ##args); \ 290 - } while (0) 291 - 292 - extern __printf(2, 3) 293 - int __trace_bprintk(unsigned long ip, const char *fmt, ...); 294 - 295 - extern __printf(2, 3) 296 - int __trace_printk(unsigned long ip, const char *fmt, ...); 297 - 298 - /** 299 - * trace_puts - write a string into the ftrace buffer 300 - * @str: the string to record 301 - * 302 - * Note: __trace_bputs is an internal function for trace_puts and 303 - * the @ip is passed in via the trace_puts macro. 304 - * 305 - * This is similar to trace_printk() but is made for those really fast 306 - * paths that a developer wants the least amount of "Heisenbug" effects, 307 - * where the processing of the print format is still too much. 308 - * 309 - * This function allows a kernel developer to debug fast path sections 310 - * that printk is not appropriate for. By scattering in various 311 - * printk like tracing in the code, a developer can quickly see 312 - * where problems are occurring. 313 - * 314 - * This is intended as a debugging tool for the developer only. 315 - * Please refrain from leaving trace_puts scattered around in 316 - * your code. (Extra memory is used for special buffers that are 317 - * allocated when trace_puts() is used.) 318 - * 319 - * Returns: 0 if nothing was written, positive # if string was. 320 - * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used) 321 - */ 322 - 323 - #define trace_puts(str) ({ \ 324 - static const char *trace_printk_fmt __used \ 325 - __section("__trace_printk_fmt") = \ 326 - __builtin_constant_p(str) ? str : NULL; \ 327 - \ 328 - if (__builtin_constant_p(str)) \ 329 - __trace_bputs(_THIS_IP_, trace_printk_fmt); \ 330 - else \ 331 - __trace_puts(_THIS_IP_, str); \ 332 - }) 333 - extern int __trace_bputs(unsigned long ip, const char *str); 334 - extern int __trace_puts(unsigned long ip, const char *str); 335 - 336 - extern void trace_dump_stack(int skip); 337 - 338 - /* 339 - * The double __builtin_constant_p is because gcc will give us an error 340 - * if we try to allocate the static variable to fmt if it is not a 341 - * constant. Even with the outer if statement. 342 - */ 343 - #define ftrace_vprintk(fmt, vargs) \ 344 - do { \ 345 - if (__builtin_constant_p(fmt)) { \ 346 - static const char *trace_printk_fmt __used \ 347 - __section("__trace_printk_fmt") = \ 348 - __builtin_constant_p(fmt) ? fmt : NULL; \ 349 - \ 350 - __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \ 351 - } else \ 352 - __ftrace_vprintk(_THIS_IP_, fmt, vargs); \ 353 - } while (0) 354 - 355 - extern __printf(2, 0) int 356 - __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap); 357 - 358 - extern __printf(2, 0) int 359 - __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap); 360 - 361 - extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode); 362 - #else 363 - static inline void tracing_start(void) { } 364 - static inline void tracing_stop(void) { } 365 - static inline void trace_dump_stack(int skip) { } 366 - 367 - static inline void tracing_on(void) { } 368 - static inline void tracing_off(void) { } 369 - static inline int tracing_is_on(void) { return 0; } 370 - static inline void tracing_snapshot(void) { } 371 - static inline void tracing_snapshot_alloc(void) { } 372 - 373 - static inline __printf(1, 2) 374 - int trace_printk(const char *fmt, ...) 375 - { 376 - return 0; 377 - } 378 - static __printf(1, 0) inline int 379 - ftrace_vprintk(const char *fmt, va_list ap) 380 - { 381 - return 0; 382 - } 383 - static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } 384 - #endif /* CONFIG_TRACING */ 385 191 386 192 /* Rebuild everything on CONFIG_DYNAMIC_FTRACE */ 387 193 #ifdef CONFIG_DYNAMIC_FTRACE
+204
include/linux/trace_printk.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #ifndef _LINUX_TRACE_PRINTK_H 3 + #define _LINUX_TRACE_PRINTK_H 4 + 5 + #include <linux/compiler_attributes.h> 6 + #include <linux/instruction_pointer.h> 7 + #include <linux/stddef.h> 8 + #include <linux/stringify.h> 9 + 10 + /* 11 + * General tracing related utility functions - trace_printk(), 12 + * tracing_on/tracing_off and tracing_start()/tracing_stop 13 + * 14 + * Use tracing_on/tracing_off when you want to quickly turn on or off 15 + * tracing. It simply enables or disables the recording of the trace events. 16 + * This also corresponds to the user space /sys/kernel/tracing/tracing_on 17 + * file, which gives a means for the kernel and userspace to interact. 18 + * Place a tracing_off() in the kernel where you want tracing to end. 19 + * From user space, examine the trace, and then echo 1 > tracing_on 20 + * to continue tracing. 21 + * 22 + * tracing_stop/tracing_start has slightly more overhead. It is used 23 + * by things like suspend to ram where disabling the recording of the 24 + * trace is not enough, but tracing must actually stop because things 25 + * like calling smp_processor_id() may crash the system. 26 + * 27 + * Most likely, you want to use tracing_on/tracing_off. 28 + */ 29 + 30 + enum ftrace_dump_mode { 31 + DUMP_NONE, 32 + DUMP_ALL, 33 + DUMP_ORIG, 34 + DUMP_PARAM, 35 + }; 36 + 37 + #ifdef CONFIG_TRACING 38 + void tracing_on(void); 39 + void tracing_off(void); 40 + int tracing_is_on(void); 41 + void tracing_snapshot(void); 42 + void tracing_snapshot_alloc(void); 43 + 44 + extern void tracing_start(void); 45 + extern void tracing_stop(void); 46 + 47 + static inline __printf(1, 2) 48 + void ____trace_printk_check_format(const char *fmt, ...) 49 + { 50 + } 51 + #define __trace_printk_check_format(fmt, args...) \ 52 + do { \ 53 + if (0) \ 54 + ____trace_printk_check_format(fmt, ##args); \ 55 + } while (0) 56 + 57 + /** 58 + * trace_printk - printf formatting in the ftrace buffer 59 + * @fmt: the printf format for printing 60 + * 61 + * Note: __trace_printk is an internal function for trace_printk() and 62 + * the @ip is passed in via the trace_printk() macro. 63 + * 64 + * This function allows a kernel developer to debug fast path sections 65 + * that printk is not appropriate for. By scattering in various 66 + * printk like tracing in the code, a developer can quickly see 67 + * where problems are occurring. 68 + * 69 + * This is intended as a debugging tool for the developer only. 70 + * Please refrain from leaving trace_printks scattered around in 71 + * your code. (Extra memory is used for special buffers that are 72 + * allocated when trace_printk() is used.) 73 + * 74 + * A little optimization trick is done here. If there's only one 75 + * argument, there's no need to scan the string for printf formats. 76 + * The trace_puts() will suffice. But how can we take advantage of 77 + * using trace_puts() when trace_printk() has only one argument? 78 + * By stringifying the args and checking the size we can tell 79 + * whether or not there are args. __stringify((__VA_ARGS__)) will 80 + * turn into "()\0" with a size of 3 when there are no args, anything 81 + * else will be bigger. All we need to do is define a string to this, 82 + * and then take its size and compare to 3. If it's bigger, use 83 + * do_trace_printk() otherwise, optimize it to trace_puts(). Then just 84 + * let gcc optimize the rest. 85 + */ 86 + 87 + #define trace_printk(fmt, ...) \ 88 + do { \ 89 + char _______STR[] = __stringify((__VA_ARGS__)); \ 90 + if (sizeof(_______STR) > 3) \ 91 + do_trace_printk(fmt, ##__VA_ARGS__); \ 92 + else \ 93 + trace_puts(fmt); \ 94 + } while (0) 95 + 96 + #define do_trace_printk(fmt, args...) \ 97 + do { \ 98 + static const char *trace_printk_fmt __used \ 99 + __section("__trace_printk_fmt") = \ 100 + __builtin_constant_p(fmt) ? fmt : NULL; \ 101 + \ 102 + __trace_printk_check_format(fmt, ##args); \ 103 + \ 104 + if (__builtin_constant_p(fmt)) \ 105 + __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ 106 + else \ 107 + __trace_printk(_THIS_IP_, fmt, ##args); \ 108 + } while (0) 109 + 110 + extern __printf(2, 3) 111 + int __trace_bprintk(unsigned long ip, const char *fmt, ...); 112 + 113 + extern __printf(2, 3) 114 + int __trace_printk(unsigned long ip, const char *fmt, ...); 115 + 116 + /** 117 + * trace_puts - write a string into the ftrace buffer 118 + * @str: the string to record 119 + * 120 + * Note: __trace_bputs is an internal function for trace_puts and 121 + * the @ip is passed in via the trace_puts macro. 122 + * 123 + * This is similar to trace_printk() but is made for those really fast 124 + * paths that a developer wants the least amount of "Heisenbug" effects, 125 + * where the processing of the print format is still too much. 126 + * 127 + * This function allows a kernel developer to debug fast path sections 128 + * that printk is not appropriate for. By scattering in various 129 + * printk like tracing in the code, a developer can quickly see 130 + * where problems are occurring. 131 + * 132 + * This is intended as a debugging tool for the developer only. 133 + * Please refrain from leaving trace_puts scattered around in 134 + * your code. (Extra memory is used for special buffers that are 135 + * allocated when trace_puts() is used.) 136 + * 137 + * Returns: 0 if nothing was written, positive # if string was. 138 + * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used) 139 + */ 140 + 141 + #define trace_puts(str) ({ \ 142 + static const char *trace_printk_fmt __used \ 143 + __section("__trace_printk_fmt") = \ 144 + __builtin_constant_p(str) ? str : NULL; \ 145 + \ 146 + if (__builtin_constant_p(str)) \ 147 + __trace_bputs(_THIS_IP_, trace_printk_fmt); \ 148 + else \ 149 + __trace_puts(_THIS_IP_, str); \ 150 + }) 151 + extern int __trace_bputs(unsigned long ip, const char *str); 152 + extern int __trace_puts(unsigned long ip, const char *str); 153 + 154 + extern void trace_dump_stack(int skip); 155 + 156 + /* 157 + * The double __builtin_constant_p is because gcc will give us an error 158 + * if we try to allocate the static variable to fmt if it is not a 159 + * constant. Even with the outer if statement. 160 + */ 161 + #define ftrace_vprintk(fmt, vargs) \ 162 + do { \ 163 + if (__builtin_constant_p(fmt)) { \ 164 + static const char *trace_printk_fmt __used \ 165 + __section("__trace_printk_fmt") = \ 166 + __builtin_constant_p(fmt) ? fmt : NULL; \ 167 + \ 168 + __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \ 169 + } else \ 170 + __ftrace_vprintk(_THIS_IP_, fmt, vargs); \ 171 + } while (0) 172 + 173 + extern __printf(2, 0) int 174 + __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap); 175 + 176 + extern __printf(2, 0) int 177 + __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap); 178 + 179 + extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode); 180 + #else 181 + static inline void tracing_start(void) { } 182 + static inline void tracing_stop(void) { } 183 + static inline void trace_dump_stack(int skip) { } 184 + 185 + static inline void tracing_on(void) { } 186 + static inline void tracing_off(void) { } 187 + static inline int tracing_is_on(void) { return 0; } 188 + static inline void tracing_snapshot(void) { } 189 + static inline void tracing_snapshot_alloc(void) { } 190 + 191 + static inline __printf(1, 2) 192 + int trace_printk(const char *fmt, ...) 193 + { 194 + return 0; 195 + } 196 + static __printf(1, 0) inline int 197 + ftrace_vprintk(const char *fmt, va_list ap) 198 + { 199 + return 0; 200 + } 201 + static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } 202 + #endif /* CONFIG_TRACING */ 203 + 204 + #endif