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