1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 #ifndef _LINUX_TRACEPOINT_H 3 #define _LINUX_TRACEPOINT_H 4 5 /* 6 * Kernel Tracepoint API. 7 * 8 * See Documentation/trace/tracepoints.rst. 9 * 10 * Copyright (C) 2008-2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> 11 * 12 * Heavily inspired from the Linux Kernel Markers. 13 */ 14 15 #include <linux/smp.h> 16 #include <linux/srcu.h> 17 #include <linux/errno.h> 18 #include <linux/types.h> 19 #include <linux/rcupdate.h> 20 #include <linux/tracepoint-defs.h> 21 #include <linux/static_call.h> 22 23 struct module; 24 struct tracepoint; 25 struct notifier_block; 26 27 struct trace_eval_map { 28 const char *system; 29 const char *eval_string; 30 unsigned long eval_value; 31 }; 32 33 #define TRACEPOINT_DEFAULT_PRIO 10 34 35 extern struct srcu_struct tracepoint_srcu; 36 37 extern int 38 tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data); 39 extern int 40 tracepoint_probe_register_prio(struct tracepoint *tp, void *probe, void *data, 41 int prio); 42 extern int 43 tracepoint_probe_register_prio_may_exist(struct tracepoint *tp, void *probe, void *data, 44 int prio); 45 extern int 46 tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data); 47 static inline int 48 tracepoint_probe_register_may_exist(struct tracepoint *tp, void *probe, 49 void *data) 50 { 51 return tracepoint_probe_register_prio_may_exist(tp, probe, data, 52 TRACEPOINT_DEFAULT_PRIO); 53 } 54 extern void 55 for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv), 56 void *priv); 57 58 #ifdef CONFIG_MODULES 59 struct tp_module { 60 struct list_head list; 61 struct module *mod; 62 }; 63 64 bool trace_module_has_bad_taint(struct module *mod); 65 extern int register_tracepoint_module_notifier(struct notifier_block *nb); 66 extern int unregister_tracepoint_module_notifier(struct notifier_block *nb); 67 void for_each_module_tracepoint(void (*fct)(struct tracepoint *, 68 struct module *, void *), 69 void *priv); 70 void for_each_tracepoint_in_module(struct module *, 71 void (*fct)(struct tracepoint *, 72 struct module *, void *), 73 void *priv); 74 #else 75 static inline bool trace_module_has_bad_taint(struct module *mod) 76 { 77 return false; 78 } 79 static inline 80 int register_tracepoint_module_notifier(struct notifier_block *nb) 81 { 82 return 0; 83 } 84 static inline 85 int unregister_tracepoint_module_notifier(struct notifier_block *nb) 86 { 87 return 0; 88 } 89 static inline 90 void for_each_module_tracepoint(void (*fct)(struct tracepoint *, 91 struct module *, void *), 92 void *priv) 93 { 94 } 95 static inline 96 void for_each_tracepoint_in_module(struct module *mod, 97 void (*fct)(struct tracepoint *, 98 struct module *, void *), 99 void *priv) 100 { 101 } 102 #endif /* CONFIG_MODULES */ 103 104 /* 105 * tracepoint_synchronize_unregister must be called between the last tracepoint 106 * probe unregistration and the end of module exit to make sure there is no 107 * caller executing a probe when it is freed. 108 */ 109 #ifdef CONFIG_TRACEPOINTS 110 static inline void tracepoint_synchronize_unregister(void) 111 { 112 synchronize_srcu(&tracepoint_srcu); 113 synchronize_rcu(); 114 } 115 #else 116 static inline void tracepoint_synchronize_unregister(void) 117 { } 118 #endif 119 120 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS 121 extern int syscall_regfunc(void); 122 extern void syscall_unregfunc(void); 123 #endif /* CONFIG_HAVE_SYSCALL_TRACEPOINTS */ 124 125 #ifndef PARAMS 126 #define PARAMS(args...) args 127 #endif 128 129 #define TRACE_DEFINE_ENUM(x) 130 #define TRACE_DEFINE_SIZEOF(x) 131 132 #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS 133 static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p) 134 { 135 return offset_to_ptr(p); 136 } 137 138 #define __TRACEPOINT_ENTRY(name) \ 139 asm(" .section \"__tracepoints_ptrs\", \"a\" \n" \ 140 " .balign 4 \n" \ 141 " .long __tracepoint_" #name " - . \n" \ 142 " .previous \n") 143 #else 144 static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p) 145 { 146 return *p; 147 } 148 149 #define __TRACEPOINT_ENTRY(name) \ 150 static tracepoint_ptr_t __tracepoint_ptr_##name __used \ 151 __section("__tracepoints_ptrs") = &__tracepoint_##name 152 #endif 153 154 #endif /* _LINUX_TRACEPOINT_H */ 155 156 /* 157 * Note: we keep the TRACE_EVENT and DECLARE_TRACE outside the include 158 * file ifdef protection. 159 * This is due to the way trace events work. If a file includes two 160 * trace event headers under one "CREATE_TRACE_POINTS" the first include 161 * will override the TRACE_EVENT and break the second include. 162 */ 163 164 #ifndef DECLARE_TRACE 165 166 #define TP_PROTO(args...) args 167 #define TP_ARGS(args...) args 168 #define TP_CONDITION(args...) args 169 170 /* 171 * Individual subsystem my have a separate configuration to 172 * enable their tracepoints. By default, this file will create 173 * the tracepoints if CONFIG_TRACEPOINTS is defined. If a subsystem 174 * wants to be able to disable its tracepoints from being created 175 * it can define NOTRACE before including the tracepoint headers. 176 */ 177 #if defined(CONFIG_TRACEPOINTS) && !defined(NOTRACE) 178 #define TRACEPOINTS_ENABLED 179 #endif 180 181 #ifdef TRACEPOINTS_ENABLED 182 183 #ifdef CONFIG_HAVE_STATIC_CALL 184 #define __DO_TRACE_CALL(name, args) \ 185 do { \ 186 struct tracepoint_func *it_func_ptr; \ 187 void *__data; \ 188 it_func_ptr = \ 189 rcu_dereference_raw((&__tracepoint_##name)->funcs); \ 190 if (it_func_ptr) { \ 191 __data = (it_func_ptr)->data; \ 192 static_call(tp_func_##name)(__data, args); \ 193 } \ 194 } while (0) 195 #else 196 #define __DO_TRACE_CALL(name, args) __traceiter_##name(NULL, args) 197 #endif /* CONFIG_HAVE_STATIC_CALL */ 198 199 /* 200 * ARCH_WANTS_NO_INSTR archs are expected to have sanitized entry and idle 201 * code that disallow any/all tracing/instrumentation when RCU isn't watching. 202 */ 203 #ifdef CONFIG_ARCH_WANTS_NO_INSTR 204 #define RCUIDLE_COND(rcuidle) (rcuidle) 205 #else 206 /* srcu can't be used from NMI */ 207 #define RCUIDLE_COND(rcuidle) (rcuidle && in_nmi()) 208 #endif 209 210 /* 211 * it_func[0] is never NULL because there is at least one element in the array 212 * when the array itself is non NULL. 213 */ 214 #define __DO_TRACE(name, args, cond, rcuidle) \ 215 do { \ 216 int __maybe_unused __idx = 0; \ 217 \ 218 if (!(cond)) \ 219 return; \ 220 \ 221 if (WARN_ONCE(RCUIDLE_COND(rcuidle), \ 222 "Bad RCU usage for tracepoint")) \ 223 return; \ 224 \ 225 /* keep srcu and sched-rcu usage consistent */ \ 226 preempt_disable_notrace(); \ 227 \ 228 /* \ 229 * For rcuidle callers, use srcu since sched-rcu \ 230 * doesn't work from the idle path. \ 231 */ \ 232 if (rcuidle) { \ 233 __idx = srcu_read_lock_notrace(&tracepoint_srcu);\ 234 ct_irq_enter_irqson(); \ 235 } \ 236 \ 237 __DO_TRACE_CALL(name, TP_ARGS(args)); \ 238 \ 239 if (rcuidle) { \ 240 ct_irq_exit_irqson(); \ 241 srcu_read_unlock_notrace(&tracepoint_srcu, __idx);\ 242 } \ 243 \ 244 preempt_enable_notrace(); \ 245 } while (0) 246 247 #ifndef MODULE 248 #define __DECLARE_TRACE_RCU(name, proto, args, cond) \ 249 static inline void trace_##name##_rcuidle(proto) \ 250 { \ 251 if (static_key_false(&__tracepoint_##name.key)) \ 252 __DO_TRACE(name, \ 253 TP_ARGS(args), \ 254 TP_CONDITION(cond), 1); \ 255 } 256 #else 257 #define __DECLARE_TRACE_RCU(name, proto, args, cond) 258 #endif 259 260 /* 261 * Make sure the alignment of the structure in the __tracepoints section will 262 * not add unwanted padding between the beginning of the section and the 263 * structure. Force alignment to the same alignment as the section start. 264 * 265 * When lockdep is enabled, we make sure to always test if RCU is 266 * "watching" regardless if the tracepoint is enabled or not. Tracepoints 267 * require RCU to be active, and it should always warn at the tracepoint 268 * site if it is not watching, as it will need to be active when the 269 * tracepoint is enabled. 270 */ 271 #define __DECLARE_TRACE(name, proto, args, cond, data_proto) \ 272 extern int __traceiter_##name(data_proto); \ 273 DECLARE_STATIC_CALL(tp_func_##name, __traceiter_##name); \ 274 extern struct tracepoint __tracepoint_##name; \ 275 static inline void trace_##name(proto) \ 276 { \ 277 if (static_key_false(&__tracepoint_##name.key)) \ 278 __DO_TRACE(name, \ 279 TP_ARGS(args), \ 280 TP_CONDITION(cond), 0); \ 281 if (IS_ENABLED(CONFIG_LOCKDEP) && (cond)) { \ 282 WARN_ONCE(!rcu_is_watching(), \ 283 "RCU not watching for tracepoint"); \ 284 } \ 285 } \ 286 __DECLARE_TRACE_RCU(name, PARAMS(proto), PARAMS(args), \ 287 PARAMS(cond)) \ 288 static inline int \ 289 register_trace_##name(void (*probe)(data_proto), void *data) \ 290 { \ 291 return tracepoint_probe_register(&__tracepoint_##name, \ 292 (void *)probe, data); \ 293 } \ 294 static inline int \ 295 register_trace_prio_##name(void (*probe)(data_proto), void *data,\ 296 int prio) \ 297 { \ 298 return tracepoint_probe_register_prio(&__tracepoint_##name, \ 299 (void *)probe, data, prio); \ 300 } \ 301 static inline int \ 302 unregister_trace_##name(void (*probe)(data_proto), void *data) \ 303 { \ 304 return tracepoint_probe_unregister(&__tracepoint_##name,\ 305 (void *)probe, data); \ 306 } \ 307 static inline void \ 308 check_trace_callback_type_##name(void (*cb)(data_proto)) \ 309 { \ 310 } \ 311 static inline bool \ 312 trace_##name##_enabled(void) \ 313 { \ 314 return static_key_false(&__tracepoint_##name.key); \ 315 } 316 317 /* 318 * We have no guarantee that gcc and the linker won't up-align the tracepoint 319 * structures, so we create an array of pointers that will be used for iteration 320 * on the tracepoints. 321 */ 322 #define DEFINE_TRACE_FN(_name, _reg, _unreg, proto, args) \ 323 static const char __tpstrtab_##_name[] \ 324 __section("__tracepoints_strings") = #_name; \ 325 extern struct static_call_key STATIC_CALL_KEY(tp_func_##_name); \ 326 int __traceiter_##_name(void *__data, proto); \ 327 void __probestub_##_name(void *__data, proto); \ 328 struct tracepoint __tracepoint_##_name __used \ 329 __section("__tracepoints") = { \ 330 .name = __tpstrtab_##_name, \ 331 .key = STATIC_KEY_INIT_FALSE, \ 332 .static_call_key = &STATIC_CALL_KEY(tp_func_##_name), \ 333 .static_call_tramp = STATIC_CALL_TRAMP_ADDR(tp_func_##_name), \ 334 .iterator = &__traceiter_##_name, \ 335 .probestub = &__probestub_##_name, \ 336 .regfunc = _reg, \ 337 .unregfunc = _unreg, \ 338 .funcs = NULL }; \ 339 __TRACEPOINT_ENTRY(_name); \ 340 int __traceiter_##_name(void *__data, proto) \ 341 { \ 342 struct tracepoint_func *it_func_ptr; \ 343 void *it_func; \ 344 \ 345 it_func_ptr = \ 346 rcu_dereference_raw((&__tracepoint_##_name)->funcs); \ 347 if (it_func_ptr) { \ 348 do { \ 349 it_func = READ_ONCE((it_func_ptr)->func); \ 350 __data = (it_func_ptr)->data; \ 351 ((void(*)(void *, proto))(it_func))(__data, args); \ 352 } while ((++it_func_ptr)->func); \ 353 } \ 354 return 0; \ 355 } \ 356 void __probestub_##_name(void *__data, proto) \ 357 { \ 358 } \ 359 DEFINE_STATIC_CALL(tp_func_##_name, __traceiter_##_name); 360 361 #define DEFINE_TRACE(name, proto, args) \ 362 DEFINE_TRACE_FN(name, NULL, NULL, PARAMS(proto), PARAMS(args)); 363 364 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name) \ 365 EXPORT_SYMBOL_GPL(__tracepoint_##name); \ 366 EXPORT_SYMBOL_GPL(__traceiter_##name); \ 367 EXPORT_STATIC_CALL_GPL(tp_func_##name) 368 #define EXPORT_TRACEPOINT_SYMBOL(name) \ 369 EXPORT_SYMBOL(__tracepoint_##name); \ 370 EXPORT_SYMBOL(__traceiter_##name); \ 371 EXPORT_STATIC_CALL(tp_func_##name) 372 373 374 #else /* !TRACEPOINTS_ENABLED */ 375 #define __DECLARE_TRACE(name, proto, args, cond, data_proto) \ 376 static inline void trace_##name(proto) \ 377 { } \ 378 static inline void trace_##name##_rcuidle(proto) \ 379 { } \ 380 static inline int \ 381 register_trace_##name(void (*probe)(data_proto), \ 382 void *data) \ 383 { \ 384 return -ENOSYS; \ 385 } \ 386 static inline int \ 387 unregister_trace_##name(void (*probe)(data_proto), \ 388 void *data) \ 389 { \ 390 return -ENOSYS; \ 391 } \ 392 static inline void check_trace_callback_type_##name(void (*cb)(data_proto)) \ 393 { \ 394 } \ 395 static inline bool \ 396 trace_##name##_enabled(void) \ 397 { \ 398 return false; \ 399 } 400 401 #define DEFINE_TRACE_FN(name, reg, unreg, proto, args) 402 #define DEFINE_TRACE(name, proto, args) 403 #define EXPORT_TRACEPOINT_SYMBOL_GPL(name) 404 #define EXPORT_TRACEPOINT_SYMBOL(name) 405 406 #endif /* TRACEPOINTS_ENABLED */ 407 408 #ifdef CONFIG_TRACING 409 /** 410 * tracepoint_string - register constant persistent string to trace system 411 * @str - a constant persistent string that will be referenced in tracepoints 412 * 413 * If constant strings are being used in tracepoints, it is faster and 414 * more efficient to just save the pointer to the string and reference 415 * that with a printf "%s" instead of saving the string in the ring buffer 416 * and wasting space and time. 417 * 418 * The problem with the above approach is that userspace tools that read 419 * the binary output of the trace buffers do not have access to the string. 420 * Instead they just show the address of the string which is not very 421 * useful to users. 422 * 423 * With tracepoint_string(), the string will be registered to the tracing 424 * system and exported to userspace via the debugfs/tracing/printk_formats 425 * file that maps the string address to the string text. This way userspace 426 * tools that read the binary buffers have a way to map the pointers to 427 * the ASCII strings they represent. 428 * 429 * The @str used must be a constant string and persistent as it would not 430 * make sense to show a string that no longer exists. But it is still fine 431 * to be used with modules, because when modules are unloaded, if they 432 * had tracepoints, the ring buffers are cleared too. As long as the string 433 * does not change during the life of the module, it is fine to use 434 * tracepoint_string() within a module. 435 */ 436 #define tracepoint_string(str) \ 437 ({ \ 438 static const char *___tp_str __tracepoint_string = str; \ 439 ___tp_str; \ 440 }) 441 #define __tracepoint_string __used __section("__tracepoint_str") 442 #else 443 /* 444 * tracepoint_string() is used to save the string address for userspace 445 * tracing tools. When tracing isn't configured, there's no need to save 446 * anything. 447 */ 448 # define tracepoint_string(str) str 449 # define __tracepoint_string 450 #endif 451 452 #define DECLARE_TRACE(name, proto, args) \ 453 __DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), \ 454 cpu_online(raw_smp_processor_id()), \ 455 PARAMS(void *__data, proto)) 456 457 #define DECLARE_TRACE_CONDITION(name, proto, args, cond) \ 458 __DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), \ 459 cpu_online(raw_smp_processor_id()) && (PARAMS(cond)), \ 460 PARAMS(void *__data, proto)) 461 462 #define TRACE_EVENT_FLAGS(event, flag) 463 464 #define TRACE_EVENT_PERF_PERM(event, expr...) 465 466 #endif /* DECLARE_TRACE */ 467 468 #ifndef TRACE_EVENT 469 /* 470 * For use with the TRACE_EVENT macro: 471 * 472 * We define a tracepoint, its arguments, its printk format 473 * and its 'fast binary record' layout. 474 * 475 * Firstly, name your tracepoint via TRACE_EVENT(name : the 476 * 'subsystem_event' notation is fine. 477 * 478 * Think about this whole construct as the 479 * 'trace_sched_switch() function' from now on. 480 * 481 * 482 * TRACE_EVENT(sched_switch, 483 * 484 * * 485 * * A function has a regular function arguments 486 * * prototype, declare it via TP_PROTO(): 487 * * 488 * 489 * TP_PROTO(struct rq *rq, struct task_struct *prev, 490 * struct task_struct *next), 491 * 492 * * 493 * * Define the call signature of the 'function'. 494 * * (Design sidenote: we use this instead of a 495 * * TP_PROTO1/TP_PROTO2/TP_PROTO3 ugliness.) 496 * * 497 * 498 * TP_ARGS(rq, prev, next), 499 * 500 * * 501 * * Fast binary tracing: define the trace record via 502 * * TP_STRUCT__entry(). You can think about it like a 503 * * regular C structure local variable definition. 504 * * 505 * * This is how the trace record is structured and will 506 * * be saved into the ring buffer. These are the fields 507 * * that will be exposed to user-space in 508 * * /sys/kernel/tracing/events/<*>/format. 509 * * 510 * * The declared 'local variable' is called '__entry' 511 * * 512 * * __field(pid_t, prev_pid) is equivalent to a standard declaration: 513 * * 514 * * pid_t prev_pid; 515 * * 516 * * __array(char, prev_comm, TASK_COMM_LEN) is equivalent to: 517 * * 518 * * char prev_comm[TASK_COMM_LEN]; 519 * * 520 * 521 * TP_STRUCT__entry( 522 * __array( char, prev_comm, TASK_COMM_LEN ) 523 * __field( pid_t, prev_pid ) 524 * __field( int, prev_prio ) 525 * __array( char, next_comm, TASK_COMM_LEN ) 526 * __field( pid_t, next_pid ) 527 * __field( int, next_prio ) 528 * ), 529 * 530 * * 531 * * Assign the entry into the trace record, by embedding 532 * * a full C statement block into TP_fast_assign(). You 533 * * can refer to the trace record as '__entry' - 534 * * otherwise you can put arbitrary C code in here. 535 * * 536 * * Note: this C code will execute every time a trace event 537 * * happens, on an active tracepoint. 538 * * 539 * 540 * TP_fast_assign( 541 * memcpy(__entry->next_comm, next->comm, TASK_COMM_LEN); 542 * __entry->prev_pid = prev->pid; 543 * __entry->prev_prio = prev->prio; 544 * memcpy(__entry->prev_comm, prev->comm, TASK_COMM_LEN); 545 * __entry->next_pid = next->pid; 546 * __entry->next_prio = next->prio; 547 * ), 548 * 549 * * 550 * * Formatted output of a trace record via TP_printk(). 551 * * This is how the tracepoint will appear under ftrace 552 * * plugins that make use of this tracepoint. 553 * * 554 * * (raw-binary tracing wont actually perform this step.) 555 * * 556 * 557 * TP_printk("task %s:%d [%d] ==> %s:%d [%d]", 558 * __entry->prev_comm, __entry->prev_pid, __entry->prev_prio, 559 * __entry->next_comm, __entry->next_pid, __entry->next_prio), 560 * 561 * ); 562 * 563 * This macro construct is thus used for the regular printk format 564 * tracing setup, it is used to construct a function pointer based 565 * tracepoint callback (this is used by programmatic plugins and 566 * can also by used by generic instrumentation like SystemTap), and 567 * it is also used to expose a structured trace record in 568 * /sys/kernel/tracing/events/. 569 * 570 * A set of (un)registration functions can be passed to the variant 571 * TRACE_EVENT_FN to perform any (un)registration work. 572 */ 573 574 #define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print) 575 #define DEFINE_EVENT(template, name, proto, args) \ 576 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args)) 577 #define DEFINE_EVENT_FN(template, name, proto, args, reg, unreg)\ 578 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args)) 579 #define DEFINE_EVENT_PRINT(template, name, proto, args, print) \ 580 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args)) 581 #define DEFINE_EVENT_CONDITION(template, name, proto, \ 582 args, cond) \ 583 DECLARE_TRACE_CONDITION(name, PARAMS(proto), \ 584 PARAMS(args), PARAMS(cond)) 585 586 #define TRACE_EVENT(name, proto, args, struct, assign, print) \ 587 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args)) 588 #define TRACE_EVENT_FN(name, proto, args, struct, \ 589 assign, print, reg, unreg) \ 590 DECLARE_TRACE(name, PARAMS(proto), PARAMS(args)) 591 #define TRACE_EVENT_FN_COND(name, proto, args, cond, struct, \ 592 assign, print, reg, unreg) \ 593 DECLARE_TRACE_CONDITION(name, PARAMS(proto), \ 594 PARAMS(args), PARAMS(cond)) 595 #define TRACE_EVENT_CONDITION(name, proto, args, cond, \ 596 struct, assign, print) \ 597 DECLARE_TRACE_CONDITION(name, PARAMS(proto), \ 598 PARAMS(args), PARAMS(cond)) 599 600 #define TRACE_EVENT_FLAGS(event, flag) 601 602 #define TRACE_EVENT_PERF_PERM(event, expr...) 603 604 #define DECLARE_EVENT_NOP(name, proto, args) \ 605 static inline void trace_##name(proto) \ 606 { } \ 607 static inline bool trace_##name##_enabled(void) \ 608 { \ 609 return false; \ 610 } 611 612 #define TRACE_EVENT_NOP(name, proto, args, struct, assign, print) \ 613 DECLARE_EVENT_NOP(name, PARAMS(proto), PARAMS(args)) 614 615 #define DECLARE_EVENT_CLASS_NOP(name, proto, args, tstruct, assign, print) 616 #define DEFINE_EVENT_NOP(template, name, proto, args) \ 617 DECLARE_EVENT_NOP(name, PARAMS(proto), PARAMS(args)) 618 619 #endif /* ifdef TRACE_EVENT (see note above) */ 620