trace.h 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  1. #ifndef _LINUX_KERNEL_TRACE_H
  2. #define _LINUX_KERNEL_TRACE_H
  3. #include <linux/fs.h>
  4. #include <linux/atomic.h>
  5. #include <linux/sched.h>
  6. #include <linux/clocksource.h>
  7. #include <linux/ring_buffer.h>
  8. #include <linux/mmiotrace.h>
  9. #include <linux/tracepoint.h>
  10. #include <linux/ftrace.h>
  11. #include <linux/hw_breakpoint.h>
  12. #include <linux/trace_seq.h>
  13. #include <linux/trace_events.h>
  14. #include <linux/compiler.h>
  15. #include <linux/trace_seq.h>
  16. #ifdef CONFIG_FTRACE_SYSCALLS
  17. #include <asm/unistd.h> /* For NR_SYSCALLS */
  18. #include <asm/syscall.h> /* some archs define it here */
  19. #endif
  20. enum trace_type {
  21. __TRACE_FIRST_TYPE = 0,
  22. TRACE_FN,
  23. TRACE_CTX,
  24. TRACE_WAKE,
  25. TRACE_STACK,
  26. TRACE_PRINT,
  27. TRACE_BPRINT,
  28. TRACE_MMIO_RW,
  29. TRACE_MMIO_MAP,
  30. TRACE_BRANCH,
  31. TRACE_GRAPH_RET,
  32. TRACE_GRAPH_ENT,
  33. TRACE_USER_STACK,
  34. TRACE_BLK,
  35. TRACE_BPUTS,
  36. __TRACE_LAST_TYPE,
  37. };
  38. #undef __field
  39. #define __field(type, item) type item;
  40. #undef __field_struct
  41. #define __field_struct(type, item) __field(type, item)
  42. #undef __field_desc
  43. #define __field_desc(type, container, item)
  44. #undef __array
  45. #define __array(type, item, size) type item[size];
  46. #undef __array_desc
  47. #define __array_desc(type, container, item, size)
  48. #undef __dynamic_array
  49. #define __dynamic_array(type, item) type item[];
  50. #undef F_STRUCT
  51. #define F_STRUCT(args...) args
  52. #undef FTRACE_ENTRY
  53. #define FTRACE_ENTRY(name, struct_name, id, tstruct, print, filter) \
  54. struct struct_name { \
  55. struct trace_entry ent; \
  56. tstruct \
  57. }
  58. #undef FTRACE_ENTRY_DUP
  59. #define FTRACE_ENTRY_DUP(name, name_struct, id, tstruct, printk, filter)
  60. #undef FTRACE_ENTRY_REG
  61. #define FTRACE_ENTRY_REG(name, struct_name, id, tstruct, print, \
  62. filter, regfn) \
  63. FTRACE_ENTRY(name, struct_name, id, PARAMS(tstruct), PARAMS(print), \
  64. filter)
  65. #undef FTRACE_ENTRY_PACKED
  66. #define FTRACE_ENTRY_PACKED(name, struct_name, id, tstruct, print, \
  67. filter) \
  68. FTRACE_ENTRY(name, struct_name, id, PARAMS(tstruct), PARAMS(print), \
  69. filter) __packed
  70. #include "trace_entries.h"
  71. /*
  72. * syscalls are special, and need special handling, this is why
  73. * they are not included in trace_entries.h
  74. */
  75. struct syscall_trace_enter {
  76. struct trace_entry ent;
  77. int nr;
  78. unsigned long args[];
  79. };
  80. struct syscall_trace_exit {
  81. struct trace_entry ent;
  82. int nr;
  83. long ret;
  84. };
  85. struct kprobe_trace_entry_head {
  86. struct trace_entry ent;
  87. unsigned long ip;
  88. };
  89. struct kretprobe_trace_entry_head {
  90. struct trace_entry ent;
  91. unsigned long func;
  92. unsigned long ret_ip;
  93. };
  94. /*
  95. * trace_flag_type is an enumeration that holds different
  96. * states when a trace occurs. These are:
  97. * IRQS_OFF - interrupts were disabled
  98. * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
  99. * NEED_RESCHED - reschedule is requested
  100. * HARDIRQ - inside an interrupt handler
  101. * SOFTIRQ - inside a softirq handler
  102. */
  103. enum trace_flag_type {
  104. TRACE_FLAG_IRQS_OFF = 0x01,
  105. TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
  106. TRACE_FLAG_NEED_RESCHED = 0x04,
  107. TRACE_FLAG_HARDIRQ = 0x08,
  108. TRACE_FLAG_SOFTIRQ = 0x10,
  109. TRACE_FLAG_PREEMPT_RESCHED = 0x20,
  110. TRACE_FLAG_NMI = 0x40,
  111. };
  112. #define TRACE_BUF_SIZE 1024
  113. struct trace_array;
  114. /*
  115. * The CPU trace array - it consists of thousands of trace entries
  116. * plus some other descriptor data: (for example which task started
  117. * the trace, etc.)
  118. */
  119. struct trace_array_cpu {
  120. atomic_t disabled;
  121. void *buffer_page; /* ring buffer spare */
  122. unsigned long entries;
  123. unsigned long saved_latency;
  124. unsigned long critical_start;
  125. unsigned long critical_end;
  126. unsigned long critical_sequence;
  127. unsigned long nice;
  128. unsigned long policy;
  129. unsigned long rt_priority;
  130. unsigned long skipped_entries;
  131. cycle_t preempt_timestamp;
  132. pid_t pid;
  133. kuid_t uid;
  134. char comm[TASK_COMM_LEN];
  135. bool ignore_pid;
  136. #ifdef CONFIG_FUNCTION_TRACER
  137. bool ftrace_ignore_pid;
  138. #endif
  139. };
  140. struct tracer;
  141. struct trace_option_dentry;
  142. struct trace_buffer {
  143. struct trace_array *tr;
  144. struct ring_buffer *buffer;
  145. struct trace_array_cpu __percpu *data;
  146. cycle_t time_start;
  147. int cpu;
  148. };
  149. #define TRACE_FLAGS_MAX_SIZE 32
  150. struct trace_options {
  151. struct tracer *tracer;
  152. struct trace_option_dentry *topts;
  153. };
  154. struct trace_pid_list {
  155. int pid_max;
  156. unsigned long *pids;
  157. };
  158. /*
  159. * The trace array - an array of per-CPU trace arrays. This is the
  160. * highest level data structure that individual tracers deal with.
  161. * They have on/off state as well:
  162. */
  163. struct trace_array {
  164. struct list_head list;
  165. char *name;
  166. struct trace_buffer trace_buffer;
  167. #ifdef CONFIG_TRACER_MAX_TRACE
  168. /*
  169. * The max_buffer is used to snapshot the trace when a maximum
  170. * latency is reached, or when the user initiates a snapshot.
  171. * Some tracers will use this to store a maximum trace while
  172. * it continues examining live traces.
  173. *
  174. * The buffers for the max_buffer are set up the same as the trace_buffer
  175. * When a snapshot is taken, the buffer of the max_buffer is swapped
  176. * with the buffer of the trace_buffer and the buffers are reset for
  177. * the trace_buffer so the tracing can continue.
  178. */
  179. struct trace_buffer max_buffer;
  180. bool allocated_snapshot;
  181. unsigned long max_latency;
  182. #endif
  183. struct trace_pid_list __rcu *filtered_pids;
  184. /*
  185. * max_lock is used to protect the swapping of buffers
  186. * when taking a max snapshot. The buffers themselves are
  187. * protected by per_cpu spinlocks. But the action of the swap
  188. * needs its own lock.
  189. *
  190. * This is defined as a arch_spinlock_t in order to help
  191. * with performance when lockdep debugging is enabled.
  192. *
  193. * It is also used in other places outside the update_max_tr
  194. * so it needs to be defined outside of the
  195. * CONFIG_TRACER_MAX_TRACE.
  196. */
  197. arch_spinlock_t max_lock;
  198. int buffer_disabled;
  199. #ifdef CONFIG_FTRACE_SYSCALLS
  200. int sys_refcount_enter;
  201. int sys_refcount_exit;
  202. struct trace_event_file __rcu *enter_syscall_files[NR_syscalls];
  203. struct trace_event_file __rcu *exit_syscall_files[NR_syscalls];
  204. #endif
  205. int stop_count;
  206. int clock_id;
  207. int nr_topts;
  208. struct tracer *current_trace;
  209. unsigned int trace_flags;
  210. unsigned char trace_flags_index[TRACE_FLAGS_MAX_SIZE];
  211. unsigned int flags;
  212. raw_spinlock_t start_lock;
  213. struct dentry *dir;
  214. struct dentry *options;
  215. struct dentry *percpu_dir;
  216. struct dentry *event_dir;
  217. struct trace_options *topts;
  218. struct list_head systems;
  219. struct list_head events;
  220. cpumask_var_t tracing_cpumask; /* only trace on set CPUs */
  221. int ref;
  222. #ifdef CONFIG_FUNCTION_TRACER
  223. struct ftrace_ops *ops;
  224. struct trace_pid_list __rcu *function_pids;
  225. /* function tracing enabled */
  226. int function_enabled;
  227. #endif
  228. };
  229. enum {
  230. TRACE_ARRAY_FL_GLOBAL = (1 << 0)
  231. };
  232. extern struct list_head ftrace_trace_arrays;
  233. extern struct mutex trace_types_lock;
  234. extern int trace_array_get(struct trace_array *tr);
  235. extern void trace_array_put(struct trace_array *tr);
  236. /*
  237. * The global tracer (top) should be the first trace array added,
  238. * but we check the flag anyway.
  239. */
  240. static inline struct trace_array *top_trace_array(void)
  241. {
  242. struct trace_array *tr;
  243. if (list_empty(&ftrace_trace_arrays))
  244. return NULL;
  245. tr = list_entry(ftrace_trace_arrays.prev,
  246. typeof(*tr), list);
  247. WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
  248. return tr;
  249. }
  250. #define FTRACE_CMP_TYPE(var, type) \
  251. __builtin_types_compatible_p(typeof(var), type *)
  252. #undef IF_ASSIGN
  253. #define IF_ASSIGN(var, entry, etype, id) \
  254. if (FTRACE_CMP_TYPE(var, etype)) { \
  255. var = (typeof(var))(entry); \
  256. WARN_ON(id && (entry)->type != id); \
  257. break; \
  258. }
  259. /* Will cause compile errors if type is not found. */
  260. extern void __ftrace_bad_type(void);
  261. /*
  262. * The trace_assign_type is a verifier that the entry type is
  263. * the same as the type being assigned. To add new types simply
  264. * add a line with the following format:
  265. *
  266. * IF_ASSIGN(var, ent, type, id);
  267. *
  268. * Where "type" is the trace type that includes the trace_entry
  269. * as the "ent" item. And "id" is the trace identifier that is
  270. * used in the trace_type enum.
  271. *
  272. * If the type can have more than one id, then use zero.
  273. */
  274. #define trace_assign_type(var, ent) \
  275. do { \
  276. IF_ASSIGN(var, ent, struct ftrace_entry, TRACE_FN); \
  277. IF_ASSIGN(var, ent, struct ctx_switch_entry, 0); \
  278. IF_ASSIGN(var, ent, struct stack_entry, TRACE_STACK); \
  279. IF_ASSIGN(var, ent, struct userstack_entry, TRACE_USER_STACK);\
  280. IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT); \
  281. IF_ASSIGN(var, ent, struct bprint_entry, TRACE_BPRINT); \
  282. IF_ASSIGN(var, ent, struct bputs_entry, TRACE_BPUTS); \
  283. IF_ASSIGN(var, ent, struct trace_mmiotrace_rw, \
  284. TRACE_MMIO_RW); \
  285. IF_ASSIGN(var, ent, struct trace_mmiotrace_map, \
  286. TRACE_MMIO_MAP); \
  287. IF_ASSIGN(var, ent, struct trace_branch, TRACE_BRANCH); \
  288. IF_ASSIGN(var, ent, struct ftrace_graph_ent_entry, \
  289. TRACE_GRAPH_ENT); \
  290. IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry, \
  291. TRACE_GRAPH_RET); \
  292. __ftrace_bad_type(); \
  293. } while (0)
  294. /*
  295. * An option specific to a tracer. This is a boolean value.
  296. * The bit is the bit index that sets its value on the
  297. * flags value in struct tracer_flags.
  298. */
  299. struct tracer_opt {
  300. const char *name; /* Will appear on the trace_options file */
  301. u32 bit; /* Mask assigned in val field in tracer_flags */
  302. };
  303. /*
  304. * The set of specific options for a tracer. Your tracer
  305. * have to set the initial value of the flags val.
  306. */
  307. struct tracer_flags {
  308. u32 val;
  309. struct tracer_opt *opts;
  310. struct tracer *trace;
  311. };
  312. /* Makes more easy to define a tracer opt */
  313. #define TRACER_OPT(s, b) .name = #s, .bit = b
  314. struct trace_option_dentry {
  315. struct tracer_opt *opt;
  316. struct tracer_flags *flags;
  317. struct trace_array *tr;
  318. struct dentry *entry;
  319. };
  320. /**
  321. * struct tracer - a specific tracer and its callbacks to interact with tracefs
  322. * @name: the name chosen to select it on the available_tracers file
  323. * @init: called when one switches to this tracer (echo name > current_tracer)
  324. * @reset: called when one switches to another tracer
  325. * @start: called when tracing is unpaused (echo 1 > tracing_on)
  326. * @stop: called when tracing is paused (echo 0 > tracing_on)
  327. * @update_thresh: called when tracing_thresh is updated
  328. * @open: called when the trace file is opened
  329. * @pipe_open: called when the trace_pipe file is opened
  330. * @close: called when the trace file is released
  331. * @pipe_close: called when the trace_pipe file is released
  332. * @read: override the default read callback on trace_pipe
  333. * @splice_read: override the default splice_read callback on trace_pipe
  334. * @selftest: selftest to run on boot (see trace_selftest.c)
  335. * @print_headers: override the first lines that describe your columns
  336. * @print_line: callback that prints a trace
  337. * @set_flag: signals one of your private flags changed (trace_options file)
  338. * @flags: your private flags
  339. */
  340. struct tracer {
  341. const char *name;
  342. int (*init)(struct trace_array *tr);
  343. void (*reset)(struct trace_array *tr);
  344. void (*start)(struct trace_array *tr);
  345. void (*stop)(struct trace_array *tr);
  346. int (*update_thresh)(struct trace_array *tr);
  347. void (*open)(struct trace_iterator *iter);
  348. void (*pipe_open)(struct trace_iterator *iter);
  349. void (*close)(struct trace_iterator *iter);
  350. void (*pipe_close)(struct trace_iterator *iter);
  351. ssize_t (*read)(struct trace_iterator *iter,
  352. struct file *filp, char __user *ubuf,
  353. size_t cnt, loff_t *ppos);
  354. ssize_t (*splice_read)(struct trace_iterator *iter,
  355. struct file *filp,
  356. loff_t *ppos,
  357. struct pipe_inode_info *pipe,
  358. size_t len,
  359. unsigned int flags);
  360. #ifdef CONFIG_FTRACE_STARTUP_TEST
  361. int (*selftest)(struct tracer *trace,
  362. struct trace_array *tr);
  363. #endif
  364. void (*print_header)(struct seq_file *m);
  365. enum print_line_t (*print_line)(struct trace_iterator *iter);
  366. /* If you handled the flag setting, return 0 */
  367. int (*set_flag)(struct trace_array *tr,
  368. u32 old_flags, u32 bit, int set);
  369. /* Return 0 if OK with change, else return non-zero */
  370. int (*flag_changed)(struct trace_array *tr,
  371. u32 mask, int set);
  372. struct tracer *next;
  373. struct tracer_flags *flags;
  374. int enabled;
  375. int ref;
  376. bool print_max;
  377. bool allow_instances;
  378. #ifdef CONFIG_TRACER_MAX_TRACE
  379. bool use_max_tr;
  380. #endif
  381. };
  382. /* Only current can touch trace_recursion */
  383. /*
  384. * For function tracing recursion:
  385. * The order of these bits are important.
  386. *
  387. * When function tracing occurs, the following steps are made:
  388. * If arch does not support a ftrace feature:
  389. * call internal function (uses INTERNAL bits) which calls...
  390. * If callback is registered to the "global" list, the list
  391. * function is called and recursion checks the GLOBAL bits.
  392. * then this function calls...
  393. * The function callback, which can use the FTRACE bits to
  394. * check for recursion.
  395. *
  396. * Now if the arch does not suppport a feature, and it calls
  397. * the global list function which calls the ftrace callback
  398. * all three of these steps will do a recursion protection.
  399. * There's no reason to do one if the previous caller already
  400. * did. The recursion that we are protecting against will
  401. * go through the same steps again.
  402. *
  403. * To prevent the multiple recursion checks, if a recursion
  404. * bit is set that is higher than the MAX bit of the current
  405. * check, then we know that the check was made by the previous
  406. * caller, and we can skip the current check.
  407. */
  408. enum {
  409. TRACE_BUFFER_BIT,
  410. TRACE_BUFFER_NMI_BIT,
  411. TRACE_BUFFER_IRQ_BIT,
  412. TRACE_BUFFER_SIRQ_BIT,
  413. /* Start of function recursion bits */
  414. TRACE_FTRACE_BIT,
  415. TRACE_FTRACE_NMI_BIT,
  416. TRACE_FTRACE_IRQ_BIT,
  417. TRACE_FTRACE_SIRQ_BIT,
  418. /* INTERNAL_BITs must be greater than FTRACE_BITs */
  419. TRACE_INTERNAL_BIT,
  420. TRACE_INTERNAL_NMI_BIT,
  421. TRACE_INTERNAL_IRQ_BIT,
  422. TRACE_INTERNAL_SIRQ_BIT,
  423. TRACE_BRANCH_BIT,
  424. /*
  425. * Abuse of the trace_recursion.
  426. * As we need a way to maintain state if we are tracing the function
  427. * graph in irq because we want to trace a particular function that
  428. * was called in irq context but we have irq tracing off. Since this
  429. * can only be modified by current, we can reuse trace_recursion.
  430. */
  431. TRACE_IRQ_BIT,
  432. };
  433. #define trace_recursion_set(bit) do { (current)->trace_recursion |= (1<<(bit)); } while (0)
  434. #define trace_recursion_clear(bit) do { (current)->trace_recursion &= ~(1<<(bit)); } while (0)
  435. #define trace_recursion_test(bit) ((current)->trace_recursion & (1<<(bit)))
  436. #define TRACE_CONTEXT_BITS 4
  437. #define TRACE_FTRACE_START TRACE_FTRACE_BIT
  438. #define TRACE_FTRACE_MAX ((1 << (TRACE_FTRACE_START + TRACE_CONTEXT_BITS)) - 1)
  439. #define TRACE_LIST_START TRACE_INTERNAL_BIT
  440. #define TRACE_LIST_MAX ((1 << (TRACE_LIST_START + TRACE_CONTEXT_BITS)) - 1)
  441. #define TRACE_CONTEXT_MASK TRACE_LIST_MAX
  442. static __always_inline int trace_get_context_bit(void)
  443. {
  444. int bit;
  445. if (in_interrupt()) {
  446. if (in_nmi())
  447. bit = 0;
  448. else if (in_irq())
  449. bit = 1;
  450. else
  451. bit = 2;
  452. } else
  453. bit = 3;
  454. return bit;
  455. }
  456. static __always_inline int trace_test_and_set_recursion(int start, int max)
  457. {
  458. unsigned int val = current->trace_recursion;
  459. int bit;
  460. /* A previous recursion check was made */
  461. if ((val & TRACE_CONTEXT_MASK) > max)
  462. return 0;
  463. bit = trace_get_context_bit() + start;
  464. if (unlikely(val & (1 << bit)))
  465. return -1;
  466. val |= 1 << bit;
  467. current->trace_recursion = val;
  468. barrier();
  469. return bit;
  470. }
  471. static __always_inline void trace_clear_recursion(int bit)
  472. {
  473. unsigned int val = current->trace_recursion;
  474. if (!bit)
  475. return;
  476. bit = 1 << bit;
  477. val &= ~bit;
  478. barrier();
  479. current->trace_recursion = val;
  480. }
  481. static inline struct ring_buffer_iter *
  482. trace_buffer_iter(struct trace_iterator *iter, int cpu)
  483. {
  484. if (iter->buffer_iter && iter->buffer_iter[cpu])
  485. return iter->buffer_iter[cpu];
  486. return NULL;
  487. }
  488. int tracer_init(struct tracer *t, struct trace_array *tr);
  489. int tracing_is_enabled(void);
  490. void tracing_reset(struct trace_buffer *buf, int cpu);
  491. void tracing_reset_online_cpus(struct trace_buffer *buf);
  492. void tracing_reset_current(int cpu);
  493. void tracing_reset_all_online_cpus(void);
  494. int tracing_open_generic(struct inode *inode, struct file *filp);
  495. bool tracing_is_disabled(void);
  496. struct dentry *trace_create_file(const char *name,
  497. umode_t mode,
  498. struct dentry *parent,
  499. void *data,
  500. const struct file_operations *fops);
  501. struct dentry *tracing_init_dentry(void);
  502. struct ring_buffer_event;
  503. struct ring_buffer_event *
  504. trace_buffer_lock_reserve(struct ring_buffer *buffer,
  505. int type,
  506. unsigned long len,
  507. unsigned long flags,
  508. int pc);
  509. struct trace_entry *tracing_get_trace_entry(struct trace_array *tr,
  510. struct trace_array_cpu *data);
  511. struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
  512. int *ent_cpu, u64 *ent_ts);
  513. void __buffer_unlock_commit(struct ring_buffer *buffer,
  514. struct ring_buffer_event *event);
  515. int trace_empty(struct trace_iterator *iter);
  516. void *trace_find_next_entry_inc(struct trace_iterator *iter);
  517. void trace_init_global_iter(struct trace_iterator *iter);
  518. void tracing_iter_reset(struct trace_iterator *iter, int cpu);
  519. void trace_function(struct trace_array *tr,
  520. unsigned long ip,
  521. unsigned long parent_ip,
  522. unsigned long flags, int pc);
  523. void trace_graph_function(struct trace_array *tr,
  524. unsigned long ip,
  525. unsigned long parent_ip,
  526. unsigned long flags, int pc);
  527. void trace_latency_header(struct seq_file *m);
  528. void trace_default_header(struct seq_file *m);
  529. void print_trace_header(struct seq_file *m, struct trace_iterator *iter);
  530. int trace_empty(struct trace_iterator *iter);
  531. void trace_graph_return(struct ftrace_graph_ret *trace);
  532. int trace_graph_entry(struct ftrace_graph_ent *trace);
  533. void set_graph_array(struct trace_array *tr);
  534. void tracing_start_cmdline_record(void);
  535. void tracing_stop_cmdline_record(void);
  536. int register_tracer(struct tracer *type);
  537. int is_tracing_stopped(void);
  538. loff_t tracing_lseek(struct file *file, loff_t offset, int whence);
  539. extern cpumask_var_t __read_mostly tracing_buffer_mask;
  540. #define for_each_tracing_cpu(cpu) \
  541. for_each_cpu(cpu, tracing_buffer_mask)
  542. extern unsigned long nsecs_to_usecs(unsigned long nsecs);
  543. extern unsigned long tracing_thresh;
  544. /* PID filtering */
  545. extern int pid_max;
  546. bool trace_find_filtered_pid(struct trace_pid_list *filtered_pids,
  547. pid_t search_pid);
  548. bool trace_ignore_this_task(struct trace_pid_list *filtered_pids,
  549. struct task_struct *task);
  550. void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
  551. struct task_struct *self,
  552. struct task_struct *task);
  553. void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos);
  554. void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos);
  555. int trace_pid_show(struct seq_file *m, void *v);
  556. void trace_free_pid_list(struct trace_pid_list *pid_list);
  557. int trace_pid_write(struct trace_pid_list *filtered_pids,
  558. struct trace_pid_list **new_pid_list,
  559. const char __user *ubuf, size_t cnt);
  560. #ifdef CONFIG_TRACER_MAX_TRACE
  561. void update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu);
  562. void update_max_tr_single(struct trace_array *tr,
  563. struct task_struct *tsk, int cpu);
  564. #endif /* CONFIG_TRACER_MAX_TRACE */
  565. #ifdef CONFIG_STACKTRACE
  566. void ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags,
  567. int pc);
  568. void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
  569. int pc);
  570. #else
  571. static inline void ftrace_trace_userstack(struct ring_buffer *buffer,
  572. unsigned long flags, int pc)
  573. {
  574. }
  575. static inline void __trace_stack(struct trace_array *tr, unsigned long flags,
  576. int skip, int pc)
  577. {
  578. }
  579. #endif /* CONFIG_STACKTRACE */
  580. extern cycle_t ftrace_now(int cpu);
  581. extern void trace_find_cmdline(int pid, char comm[]);
  582. extern void trace_event_follow_fork(struct trace_array *tr, bool enable);
  583. #ifdef CONFIG_DYNAMIC_FTRACE
  584. extern unsigned long ftrace_update_tot_cnt;
  585. #endif
  586. #define DYN_FTRACE_TEST_NAME trace_selftest_dynamic_test_func
  587. extern int DYN_FTRACE_TEST_NAME(void);
  588. #define DYN_FTRACE_TEST_NAME2 trace_selftest_dynamic_test_func2
  589. extern int DYN_FTRACE_TEST_NAME2(void);
  590. extern bool ring_buffer_expanded;
  591. extern bool tracing_selftest_disabled;
  592. #ifdef CONFIG_FTRACE_STARTUP_TEST
  593. extern int trace_selftest_startup_function(struct tracer *trace,
  594. struct trace_array *tr);
  595. extern int trace_selftest_startup_function_graph(struct tracer *trace,
  596. struct trace_array *tr);
  597. extern int trace_selftest_startup_irqsoff(struct tracer *trace,
  598. struct trace_array *tr);
  599. extern int trace_selftest_startup_preemptoff(struct tracer *trace,
  600. struct trace_array *tr);
  601. extern int trace_selftest_startup_preemptirqsoff(struct tracer *trace,
  602. struct trace_array *tr);
  603. extern int trace_selftest_startup_wakeup(struct tracer *trace,
  604. struct trace_array *tr);
  605. extern int trace_selftest_startup_nop(struct tracer *trace,
  606. struct trace_array *tr);
  607. extern int trace_selftest_startup_sched_switch(struct tracer *trace,
  608. struct trace_array *tr);
  609. extern int trace_selftest_startup_branch(struct tracer *trace,
  610. struct trace_array *tr);
  611. /*
  612. * Tracer data references selftest functions that only occur
  613. * on boot up. These can be __init functions. Thus, when selftests
  614. * are enabled, then the tracers need to reference __init functions.
  615. */
  616. #define __tracer_data __refdata
  617. #else
  618. /* Tracers are seldom changed. Optimize when selftests are disabled. */
  619. #define __tracer_data __read_mostly
  620. #endif /* CONFIG_FTRACE_STARTUP_TEST */
  621. extern void *head_page(struct trace_array_cpu *data);
  622. extern unsigned long long ns2usecs(cycle_t nsec);
  623. extern int
  624. trace_vbprintk(unsigned long ip, const char *fmt, va_list args);
  625. extern int
  626. trace_vprintk(unsigned long ip, const char *fmt, va_list args);
  627. extern int
  628. trace_array_vprintk(struct trace_array *tr,
  629. unsigned long ip, const char *fmt, va_list args);
  630. int trace_array_printk(struct trace_array *tr,
  631. unsigned long ip, const char *fmt, ...);
  632. int trace_array_printk_buf(struct ring_buffer *buffer,
  633. unsigned long ip, const char *fmt, ...);
  634. void trace_printk_seq(struct trace_seq *s);
  635. enum print_line_t print_trace_line(struct trace_iterator *iter);
  636. extern char trace_find_mark(unsigned long long duration);
  637. /* Standard output formatting function used for function return traces */
  638. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  639. /* Flag options */
  640. #define TRACE_GRAPH_PRINT_OVERRUN 0x1
  641. #define TRACE_GRAPH_PRINT_CPU 0x2
  642. #define TRACE_GRAPH_PRINT_OVERHEAD 0x4
  643. #define TRACE_GRAPH_PRINT_PROC 0x8
  644. #define TRACE_GRAPH_PRINT_DURATION 0x10
  645. #define TRACE_GRAPH_PRINT_ABS_TIME 0x20
  646. #define TRACE_GRAPH_PRINT_IRQS 0x40
  647. #define TRACE_GRAPH_PRINT_TAIL 0x80
  648. #define TRACE_GRAPH_SLEEP_TIME 0x100
  649. #define TRACE_GRAPH_GRAPH_TIME 0x200
  650. #define TRACE_GRAPH_PRINT_FILL_SHIFT 28
  651. #define TRACE_GRAPH_PRINT_FILL_MASK (0x3 << TRACE_GRAPH_PRINT_FILL_SHIFT)
  652. extern void ftrace_graph_sleep_time_control(bool enable);
  653. extern void ftrace_graph_graph_time_control(bool enable);
  654. extern enum print_line_t
  655. print_graph_function_flags(struct trace_iterator *iter, u32 flags);
  656. extern void print_graph_headers_flags(struct seq_file *s, u32 flags);
  657. extern void
  658. trace_print_graph_duration(unsigned long long duration, struct trace_seq *s);
  659. extern void graph_trace_open(struct trace_iterator *iter);
  660. extern void graph_trace_close(struct trace_iterator *iter);
  661. extern int __trace_graph_entry(struct trace_array *tr,
  662. struct ftrace_graph_ent *trace,
  663. unsigned long flags, int pc);
  664. extern void __trace_graph_return(struct trace_array *tr,
  665. struct ftrace_graph_ret *trace,
  666. unsigned long flags, int pc);
  667. #ifdef CONFIG_DYNAMIC_FTRACE
  668. /* TODO: make this variable */
  669. #define FTRACE_GRAPH_MAX_FUNCS 32
  670. extern int ftrace_graph_count;
  671. extern unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS];
  672. extern int ftrace_graph_notrace_count;
  673. extern unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS];
  674. static inline int ftrace_graph_addr(unsigned long addr)
  675. {
  676. int i;
  677. if (!ftrace_graph_count)
  678. return 1;
  679. for (i = 0; i < ftrace_graph_count; i++) {
  680. if (addr == ftrace_graph_funcs[i]) {
  681. /*
  682. * If no irqs are to be traced, but a set_graph_function
  683. * is set, and called by an interrupt handler, we still
  684. * want to trace it.
  685. */
  686. if (in_irq())
  687. trace_recursion_set(TRACE_IRQ_BIT);
  688. else
  689. trace_recursion_clear(TRACE_IRQ_BIT);
  690. return 1;
  691. }
  692. }
  693. return 0;
  694. }
  695. static inline int ftrace_graph_notrace_addr(unsigned long addr)
  696. {
  697. int i;
  698. if (!ftrace_graph_notrace_count)
  699. return 0;
  700. for (i = 0; i < ftrace_graph_notrace_count; i++) {
  701. if (addr == ftrace_graph_notrace_funcs[i])
  702. return 1;
  703. }
  704. return 0;
  705. }
  706. #else
  707. static inline int ftrace_graph_addr(unsigned long addr)
  708. {
  709. return 1;
  710. }
  711. static inline int ftrace_graph_notrace_addr(unsigned long addr)
  712. {
  713. return 0;
  714. }
  715. #endif /* CONFIG_DYNAMIC_FTRACE */
  716. #else /* CONFIG_FUNCTION_GRAPH_TRACER */
  717. static inline enum print_line_t
  718. print_graph_function_flags(struct trace_iterator *iter, u32 flags)
  719. {
  720. return TRACE_TYPE_UNHANDLED;
  721. }
  722. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  723. extern struct list_head ftrace_pids;
  724. #ifdef CONFIG_FUNCTION_TRACER
  725. extern bool ftrace_filter_param __initdata;
  726. static inline int ftrace_trace_task(struct trace_array *tr)
  727. {
  728. return !this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid);
  729. }
  730. extern int ftrace_is_dead(void);
  731. int ftrace_create_function_files(struct trace_array *tr,
  732. struct dentry *parent);
  733. void ftrace_destroy_function_files(struct trace_array *tr);
  734. void ftrace_init_global_array_ops(struct trace_array *tr);
  735. void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func);
  736. void ftrace_reset_array_ops(struct trace_array *tr);
  737. int using_ftrace_ops_list_func(void);
  738. void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer);
  739. void ftrace_init_tracefs_toplevel(struct trace_array *tr,
  740. struct dentry *d_tracer);
  741. #else
  742. static inline int ftrace_trace_task(struct trace_array *tr)
  743. {
  744. return 1;
  745. }
  746. static inline int ftrace_is_dead(void) { return 0; }
  747. static inline int
  748. ftrace_create_function_files(struct trace_array *tr,
  749. struct dentry *parent)
  750. {
  751. return 0;
  752. }
  753. static inline void ftrace_destroy_function_files(struct trace_array *tr) { }
  754. static inline __init void
  755. ftrace_init_global_array_ops(struct trace_array *tr) { }
  756. static inline void ftrace_reset_array_ops(struct trace_array *tr) { }
  757. static inline void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d) { }
  758. static inline void ftrace_init_tracefs_toplevel(struct trace_array *tr, struct dentry *d) { }
  759. /* ftace_func_t type is not defined, use macro instead of static inline */
  760. #define ftrace_init_array_ops(tr, func) do { } while (0)
  761. #endif /* CONFIG_FUNCTION_TRACER */
  762. #if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE)
  763. void ftrace_create_filter_files(struct ftrace_ops *ops,
  764. struct dentry *parent);
  765. void ftrace_destroy_filter_files(struct ftrace_ops *ops);
  766. #else
  767. /*
  768. * The ops parameter passed in is usually undefined.
  769. * This must be a macro.
  770. */
  771. #define ftrace_create_filter_files(ops, parent) do { } while (0)
  772. #define ftrace_destroy_filter_files(ops) do { } while (0)
  773. #endif /* CONFIG_FUNCTION_TRACER && CONFIG_DYNAMIC_FTRACE */
  774. bool ftrace_event_is_function(struct trace_event_call *call);
  775. /*
  776. * struct trace_parser - servers for reading the user input separated by spaces
  777. * @cont: set if the input is not complete - no final space char was found
  778. * @buffer: holds the parsed user input
  779. * @idx: user input length
  780. * @size: buffer size
  781. */
  782. struct trace_parser {
  783. bool cont;
  784. char *buffer;
  785. unsigned idx;
  786. unsigned size;
  787. };
  788. static inline bool trace_parser_loaded(struct trace_parser *parser)
  789. {
  790. return (parser->idx != 0);
  791. }
  792. static inline bool trace_parser_cont(struct trace_parser *parser)
  793. {
  794. return parser->cont;
  795. }
  796. static inline void trace_parser_clear(struct trace_parser *parser)
  797. {
  798. parser->cont = false;
  799. parser->idx = 0;
  800. }
  801. extern int trace_parser_get_init(struct trace_parser *parser, int size);
  802. extern void trace_parser_put(struct trace_parser *parser);
  803. extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
  804. size_t cnt, loff_t *ppos);
  805. /*
  806. * Only create function graph options if function graph is configured.
  807. */
  808. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  809. # define FGRAPH_FLAGS \
  810. C(DISPLAY_GRAPH, "display-graph"),
  811. #else
  812. # define FGRAPH_FLAGS
  813. #endif
  814. #ifdef CONFIG_BRANCH_TRACER
  815. # define BRANCH_FLAGS \
  816. C(BRANCH, "branch"),
  817. #else
  818. # define BRANCH_FLAGS
  819. #endif
  820. #ifdef CONFIG_FUNCTION_TRACER
  821. # define FUNCTION_FLAGS \
  822. C(FUNCTION, "function-trace"),
  823. # define FUNCTION_DEFAULT_FLAGS TRACE_ITER_FUNCTION
  824. #else
  825. # define FUNCTION_FLAGS
  826. # define FUNCTION_DEFAULT_FLAGS 0UL
  827. #endif
  828. #ifdef CONFIG_STACKTRACE
  829. # define STACK_FLAGS \
  830. C(STACKTRACE, "stacktrace"),
  831. #else
  832. # define STACK_FLAGS
  833. #endif
  834. /*
  835. * trace_iterator_flags is an enumeration that defines bit
  836. * positions into trace_flags that controls the output.
  837. *
  838. * NOTE: These bits must match the trace_options array in
  839. * trace.c (this macro guarantees it).
  840. */
  841. #define TRACE_FLAGS \
  842. C(PRINT_PARENT, "print-parent"), \
  843. C(SYM_OFFSET, "sym-offset"), \
  844. C(SYM_ADDR, "sym-addr"), \
  845. C(VERBOSE, "verbose"), \
  846. C(RAW, "raw"), \
  847. C(HEX, "hex"), \
  848. C(BIN, "bin"), \
  849. C(BLOCK, "block"), \
  850. C(PRINTK, "trace_printk"), \
  851. C(ANNOTATE, "annotate"), \
  852. C(USERSTACKTRACE, "userstacktrace"), \
  853. C(SYM_USEROBJ, "sym-userobj"), \
  854. C(PRINTK_MSGONLY, "printk-msg-only"), \
  855. C(CONTEXT_INFO, "context-info"), /* Print pid/cpu/time */ \
  856. C(LATENCY_FMT, "latency-format"), \
  857. C(RECORD_CMD, "record-cmd"), \
  858. C(OVERWRITE, "overwrite"), \
  859. C(STOP_ON_FREE, "disable_on_free"), \
  860. C(IRQ_INFO, "irq-info"), \
  861. C(MARKERS, "markers"), \
  862. C(EVENT_FORK, "event-fork"), \
  863. FUNCTION_FLAGS \
  864. FGRAPH_FLAGS \
  865. STACK_FLAGS \
  866. BRANCH_FLAGS
  867. /*
  868. * By defining C, we can make TRACE_FLAGS a list of bit names
  869. * that will define the bits for the flag masks.
  870. */
  871. #undef C
  872. #define C(a, b) TRACE_ITER_##a##_BIT
  873. enum trace_iterator_bits {
  874. TRACE_FLAGS
  875. /* Make sure we don't go more than we have bits for */
  876. TRACE_ITER_LAST_BIT
  877. };
  878. /*
  879. * By redefining C, we can make TRACE_FLAGS a list of masks that
  880. * use the bits as defined above.
  881. */
  882. #undef C
  883. #define C(a, b) TRACE_ITER_##a = (1 << TRACE_ITER_##a##_BIT)
  884. enum trace_iterator_flags { TRACE_FLAGS };
  885. /*
  886. * TRACE_ITER_SYM_MASK masks the options in trace_flags that
  887. * control the output of kernel symbols.
  888. */
  889. #define TRACE_ITER_SYM_MASK \
  890. (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
  891. extern struct tracer nop_trace;
  892. #ifdef CONFIG_BRANCH_TRACER
  893. extern int enable_branch_tracing(struct trace_array *tr);
  894. extern void disable_branch_tracing(void);
  895. static inline int trace_branch_enable(struct trace_array *tr)
  896. {
  897. if (tr->trace_flags & TRACE_ITER_BRANCH)
  898. return enable_branch_tracing(tr);
  899. return 0;
  900. }
  901. static inline void trace_branch_disable(void)
  902. {
  903. /* due to races, always disable */
  904. disable_branch_tracing();
  905. }
  906. #else
  907. static inline int trace_branch_enable(struct trace_array *tr)
  908. {
  909. return 0;
  910. }
  911. static inline void trace_branch_disable(void)
  912. {
  913. }
  914. #endif /* CONFIG_BRANCH_TRACER */
  915. /* set ring buffers to default size if not already done so */
  916. int tracing_update_buffers(void);
  917. struct ftrace_event_field {
  918. struct list_head link;
  919. const char *name;
  920. const char *type;
  921. int filter_type;
  922. int offset;
  923. int size;
  924. int is_signed;
  925. };
  926. struct event_filter {
  927. int n_preds; /* Number assigned */
  928. int a_preds; /* allocated */
  929. struct filter_pred *preds;
  930. struct filter_pred *root;
  931. char *filter_string;
  932. };
  933. struct event_subsystem {
  934. struct list_head list;
  935. const char *name;
  936. struct event_filter *filter;
  937. int ref_count;
  938. };
  939. struct trace_subsystem_dir {
  940. struct list_head list;
  941. struct event_subsystem *subsystem;
  942. struct trace_array *tr;
  943. struct dentry *entry;
  944. int ref_count;
  945. int nr_events;
  946. };
  947. extern int call_filter_check_discard(struct trace_event_call *call, void *rec,
  948. struct ring_buffer *buffer,
  949. struct ring_buffer_event *event);
  950. void trace_buffer_unlock_commit_regs(struct trace_array *tr,
  951. struct ring_buffer *buffer,
  952. struct ring_buffer_event *event,
  953. unsigned long flags, int pc,
  954. struct pt_regs *regs);
  955. static inline void trace_buffer_unlock_commit(struct trace_array *tr,
  956. struct ring_buffer *buffer,
  957. struct ring_buffer_event *event,
  958. unsigned long flags, int pc)
  959. {
  960. trace_buffer_unlock_commit_regs(tr, buffer, event, flags, pc, NULL);
  961. }
  962. DECLARE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
  963. DECLARE_PER_CPU(int, trace_buffered_event_cnt);
  964. void trace_buffered_event_disable(void);
  965. void trace_buffered_event_enable(void);
  966. static inline void
  967. __trace_event_discard_commit(struct ring_buffer *buffer,
  968. struct ring_buffer_event *event)
  969. {
  970. if (this_cpu_read(trace_buffered_event) == event) {
  971. /* Simply release the temp buffer */
  972. this_cpu_dec(trace_buffered_event_cnt);
  973. return;
  974. }
  975. ring_buffer_discard_commit(buffer, event);
  976. }
  977. /*
  978. * Helper function for event_trigger_unlock_commit{_regs}().
  979. * If there are event triggers attached to this event that requires
  980. * filtering against its fields, then they wil be called as the
  981. * entry already holds the field information of the current event.
  982. *
  983. * It also checks if the event should be discarded or not.
  984. * It is to be discarded if the event is soft disabled and the
  985. * event was only recorded to process triggers, or if the event
  986. * filter is active and this event did not match the filters.
  987. *
  988. * Returns true if the event is discarded, false otherwise.
  989. */
  990. static inline bool
  991. __event_trigger_test_discard(struct trace_event_file *file,
  992. struct ring_buffer *buffer,
  993. struct ring_buffer_event *event,
  994. void *entry,
  995. enum event_trigger_type *tt)
  996. {
  997. unsigned long eflags = file->flags;
  998. if (eflags & EVENT_FILE_FL_TRIGGER_COND)
  999. *tt = event_triggers_call(file, entry);
  1000. if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags) ||
  1001. (unlikely(file->flags & EVENT_FILE_FL_FILTERED) &&
  1002. !filter_match_preds(file->filter, entry))) {
  1003. __trace_event_discard_commit(buffer, event);
  1004. return true;
  1005. }
  1006. return false;
  1007. }
  1008. /**
  1009. * event_trigger_unlock_commit - handle triggers and finish event commit
  1010. * @file: The file pointer assoctiated to the event
  1011. * @buffer: The ring buffer that the event is being written to
  1012. * @event: The event meta data in the ring buffer
  1013. * @entry: The event itself
  1014. * @irq_flags: The state of the interrupts at the start of the event
  1015. * @pc: The state of the preempt count at the start of the event.
  1016. *
  1017. * This is a helper function to handle triggers that require data
  1018. * from the event itself. It also tests the event against filters and
  1019. * if the event is soft disabled and should be discarded.
  1020. */
  1021. static inline void
  1022. event_trigger_unlock_commit(struct trace_event_file *file,
  1023. struct ring_buffer *buffer,
  1024. struct ring_buffer_event *event,
  1025. void *entry, unsigned long irq_flags, int pc)
  1026. {
  1027. enum event_trigger_type tt = ETT_NONE;
  1028. if (!__event_trigger_test_discard(file, buffer, event, entry, &tt))
  1029. trace_buffer_unlock_commit(file->tr, buffer, event, irq_flags, pc);
  1030. if (tt)
  1031. event_triggers_post_call(file, tt, entry);
  1032. }
  1033. /**
  1034. * event_trigger_unlock_commit_regs - handle triggers and finish event commit
  1035. * @file: The file pointer assoctiated to the event
  1036. * @buffer: The ring buffer that the event is being written to
  1037. * @event: The event meta data in the ring buffer
  1038. * @entry: The event itself
  1039. * @irq_flags: The state of the interrupts at the start of the event
  1040. * @pc: The state of the preempt count at the start of the event.
  1041. *
  1042. * This is a helper function to handle triggers that require data
  1043. * from the event itself. It also tests the event against filters and
  1044. * if the event is soft disabled and should be discarded.
  1045. *
  1046. * Same as event_trigger_unlock_commit() but calls
  1047. * trace_buffer_unlock_commit_regs() instead of trace_buffer_unlock_commit().
  1048. */
  1049. static inline void
  1050. event_trigger_unlock_commit_regs(struct trace_event_file *file,
  1051. struct ring_buffer *buffer,
  1052. struct ring_buffer_event *event,
  1053. void *entry, unsigned long irq_flags, int pc,
  1054. struct pt_regs *regs)
  1055. {
  1056. enum event_trigger_type tt = ETT_NONE;
  1057. if (!__event_trigger_test_discard(file, buffer, event, entry, &tt))
  1058. trace_buffer_unlock_commit_regs(file->tr, buffer, event,
  1059. irq_flags, pc, regs);
  1060. if (tt)
  1061. event_triggers_post_call(file, tt, entry);
  1062. }
  1063. #define FILTER_PRED_INVALID ((unsigned short)-1)
  1064. #define FILTER_PRED_IS_RIGHT (1 << 15)
  1065. #define FILTER_PRED_FOLD (1 << 15)
  1066. /*
  1067. * The max preds is the size of unsigned short with
  1068. * two flags at the MSBs. One bit is used for both the IS_RIGHT
  1069. * and FOLD flags. The other is reserved.
  1070. *
  1071. * 2^14 preds is way more than enough.
  1072. */
  1073. #define MAX_FILTER_PRED 16384
  1074. struct filter_pred;
  1075. struct regex;
  1076. typedef int (*filter_pred_fn_t) (struct filter_pred *pred, void *event);
  1077. typedef int (*regex_match_func)(char *str, struct regex *r, int len);
  1078. enum regex_type {
  1079. MATCH_FULL = 0,
  1080. MATCH_FRONT_ONLY,
  1081. MATCH_MIDDLE_ONLY,
  1082. MATCH_END_ONLY,
  1083. };
  1084. struct regex {
  1085. char pattern[MAX_FILTER_STR_VAL];
  1086. int len;
  1087. int field_len;
  1088. regex_match_func match;
  1089. };
  1090. struct filter_pred {
  1091. filter_pred_fn_t fn;
  1092. u64 val;
  1093. struct regex regex;
  1094. unsigned short *ops;
  1095. struct ftrace_event_field *field;
  1096. int offset;
  1097. int not;
  1098. int op;
  1099. unsigned short index;
  1100. unsigned short parent;
  1101. unsigned short left;
  1102. unsigned short right;
  1103. };
  1104. static inline bool is_string_field(struct ftrace_event_field *field)
  1105. {
  1106. return field->filter_type == FILTER_DYN_STRING ||
  1107. field->filter_type == FILTER_STATIC_STRING ||
  1108. field->filter_type == FILTER_PTR_STRING;
  1109. }
  1110. static inline bool is_function_field(struct ftrace_event_field *field)
  1111. {
  1112. return field->filter_type == FILTER_TRACE_FN;
  1113. }
  1114. extern enum regex_type
  1115. filter_parse_regex(char *buff, int len, char **search, int *not);
  1116. extern void print_event_filter(struct trace_event_file *file,
  1117. struct trace_seq *s);
  1118. extern int apply_event_filter(struct trace_event_file *file,
  1119. char *filter_string);
  1120. extern int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
  1121. char *filter_string);
  1122. extern void print_subsystem_event_filter(struct event_subsystem *system,
  1123. struct trace_seq *s);
  1124. extern int filter_assign_type(const char *type);
  1125. extern int create_event_filter(struct trace_event_call *call,
  1126. char *filter_str, bool set_str,
  1127. struct event_filter **filterp);
  1128. extern void free_event_filter(struct event_filter *filter);
  1129. struct ftrace_event_field *
  1130. trace_find_event_field(struct trace_event_call *call, char *name);
  1131. extern void trace_event_enable_cmd_record(bool enable);
  1132. extern int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr);
  1133. extern int event_trace_del_tracer(struct trace_array *tr);
  1134. extern struct trace_event_file *find_event_file(struct trace_array *tr,
  1135. const char *system,
  1136. const char *event);
  1137. static inline void *event_file_data(struct file *filp)
  1138. {
  1139. return ACCESS_ONCE(file_inode(filp)->i_private);
  1140. }
  1141. extern struct mutex event_mutex;
  1142. extern struct list_head ftrace_events;
  1143. extern const struct file_operations event_trigger_fops;
  1144. extern const struct file_operations event_hist_fops;
  1145. #ifdef CONFIG_HIST_TRIGGERS
  1146. extern int register_trigger_hist_cmd(void);
  1147. extern int register_trigger_hist_enable_disable_cmds(void);
  1148. #else
  1149. static inline int register_trigger_hist_cmd(void) { return 0; }
  1150. static inline int register_trigger_hist_enable_disable_cmds(void) { return 0; }
  1151. #endif
  1152. extern int register_trigger_cmds(void);
  1153. extern void clear_event_triggers(struct trace_array *tr);
  1154. struct event_trigger_data {
  1155. unsigned long count;
  1156. int ref;
  1157. struct event_trigger_ops *ops;
  1158. struct event_command *cmd_ops;
  1159. struct event_filter __rcu *filter;
  1160. char *filter_str;
  1161. void *private_data;
  1162. bool paused;
  1163. bool paused_tmp;
  1164. struct list_head list;
  1165. char *name;
  1166. struct list_head named_list;
  1167. struct event_trigger_data *named_data;
  1168. };
  1169. /* Avoid typos */
  1170. #define ENABLE_EVENT_STR "enable_event"
  1171. #define DISABLE_EVENT_STR "disable_event"
  1172. #define ENABLE_HIST_STR "enable_hist"
  1173. #define DISABLE_HIST_STR "disable_hist"
  1174. struct enable_trigger_data {
  1175. struct trace_event_file *file;
  1176. bool enable;
  1177. bool hist;
  1178. };
  1179. extern int event_enable_trigger_print(struct seq_file *m,
  1180. struct event_trigger_ops *ops,
  1181. struct event_trigger_data *data);
  1182. extern void event_enable_trigger_free(struct event_trigger_ops *ops,
  1183. struct event_trigger_data *data);
  1184. extern int event_enable_trigger_func(struct event_command *cmd_ops,
  1185. struct trace_event_file *file,
  1186. char *glob, char *cmd, char *param);
  1187. extern int event_enable_register_trigger(char *glob,
  1188. struct event_trigger_ops *ops,
  1189. struct event_trigger_data *data,
  1190. struct trace_event_file *file);
  1191. extern void event_enable_unregister_trigger(char *glob,
  1192. struct event_trigger_ops *ops,
  1193. struct event_trigger_data *test,
  1194. struct trace_event_file *file);
  1195. extern void trigger_data_free(struct event_trigger_data *data);
  1196. extern int event_trigger_init(struct event_trigger_ops *ops,
  1197. struct event_trigger_data *data);
  1198. extern int trace_event_trigger_enable_disable(struct trace_event_file *file,
  1199. int trigger_enable);
  1200. extern void update_cond_flag(struct trace_event_file *file);
  1201. extern void unregister_trigger(char *glob, struct event_trigger_ops *ops,
  1202. struct event_trigger_data *test,
  1203. struct trace_event_file *file);
  1204. extern int set_trigger_filter(char *filter_str,
  1205. struct event_trigger_data *trigger_data,
  1206. struct trace_event_file *file);
  1207. extern struct event_trigger_data *find_named_trigger(const char *name);
  1208. extern bool is_named_trigger(struct event_trigger_data *test);
  1209. extern int save_named_trigger(const char *name,
  1210. struct event_trigger_data *data);
  1211. extern void del_named_trigger(struct event_trigger_data *data);
  1212. extern void pause_named_trigger(struct event_trigger_data *data);
  1213. extern void unpause_named_trigger(struct event_trigger_data *data);
  1214. extern void set_named_trigger_data(struct event_trigger_data *data,
  1215. struct event_trigger_data *named_data);
  1216. extern int register_event_command(struct event_command *cmd);
  1217. extern int unregister_event_command(struct event_command *cmd);
  1218. extern int register_trigger_hist_enable_disable_cmds(void);
  1219. /**
  1220. * struct event_trigger_ops - callbacks for trace event triggers
  1221. *
  1222. * The methods in this structure provide per-event trigger hooks for
  1223. * various trigger operations.
  1224. *
  1225. * All the methods below, except for @init() and @free(), must be
  1226. * implemented.
  1227. *
  1228. * @func: The trigger 'probe' function called when the triggering
  1229. * event occurs. The data passed into this callback is the data
  1230. * that was supplied to the event_command @reg() function that
  1231. * registered the trigger (see struct event_command) along with
  1232. * the trace record, rec.
  1233. *
  1234. * @init: An optional initialization function called for the trigger
  1235. * when the trigger is registered (via the event_command reg()
  1236. * function). This can be used to perform per-trigger
  1237. * initialization such as incrementing a per-trigger reference
  1238. * count, for instance. This is usually implemented by the
  1239. * generic utility function @event_trigger_init() (see
  1240. * trace_event_triggers.c).
  1241. *
  1242. * @free: An optional de-initialization function called for the
  1243. * trigger when the trigger is unregistered (via the
  1244. * event_command @reg() function). This can be used to perform
  1245. * per-trigger de-initialization such as decrementing a
  1246. * per-trigger reference count and freeing corresponding trigger
  1247. * data, for instance. This is usually implemented by the
  1248. * generic utility function @event_trigger_free() (see
  1249. * trace_event_triggers.c).
  1250. *
  1251. * @print: The callback function invoked to have the trigger print
  1252. * itself. This is usually implemented by a wrapper function
  1253. * that calls the generic utility function @event_trigger_print()
  1254. * (see trace_event_triggers.c).
  1255. */
  1256. struct event_trigger_ops {
  1257. void (*func)(struct event_trigger_data *data,
  1258. void *rec);
  1259. int (*init)(struct event_trigger_ops *ops,
  1260. struct event_trigger_data *data);
  1261. void (*free)(struct event_trigger_ops *ops,
  1262. struct event_trigger_data *data);
  1263. int (*print)(struct seq_file *m,
  1264. struct event_trigger_ops *ops,
  1265. struct event_trigger_data *data);
  1266. };
  1267. /**
  1268. * struct event_command - callbacks and data members for event commands
  1269. *
  1270. * Event commands are invoked by users by writing the command name
  1271. * into the 'trigger' file associated with a trace event. The
  1272. * parameters associated with a specific invocation of an event
  1273. * command are used to create an event trigger instance, which is
  1274. * added to the list of trigger instances associated with that trace
  1275. * event. When the event is hit, the set of triggers associated with
  1276. * that event is invoked.
  1277. *
  1278. * The data members in this structure provide per-event command data
  1279. * for various event commands.
  1280. *
  1281. * All the data members below, except for @post_trigger, must be set
  1282. * for each event command.
  1283. *
  1284. * @name: The unique name that identifies the event command. This is
  1285. * the name used when setting triggers via trigger files.
  1286. *
  1287. * @trigger_type: A unique id that identifies the event command
  1288. * 'type'. This value has two purposes, the first to ensure that
  1289. * only one trigger of the same type can be set at a given time
  1290. * for a particular event e.g. it doesn't make sense to have both
  1291. * a traceon and traceoff trigger attached to a single event at
  1292. * the same time, so traceon and traceoff have the same type
  1293. * though they have different names. The @trigger_type value is
  1294. * also used as a bit value for deferring the actual trigger
  1295. * action until after the current event is finished. Some
  1296. * commands need to do this if they themselves log to the trace
  1297. * buffer (see the @post_trigger() member below). @trigger_type
  1298. * values are defined by adding new values to the trigger_type
  1299. * enum in include/linux/trace_events.h.
  1300. *
  1301. * @flags: See the enum event_command_flags below.
  1302. *
  1303. * All the methods below, except for @set_filter() and @unreg_all(),
  1304. * must be implemented.
  1305. *
  1306. * @func: The callback function responsible for parsing and
  1307. * registering the trigger written to the 'trigger' file by the
  1308. * user. It allocates the trigger instance and registers it with
  1309. * the appropriate trace event. It makes use of the other
  1310. * event_command callback functions to orchestrate this, and is
  1311. * usually implemented by the generic utility function
  1312. * @event_trigger_callback() (see trace_event_triggers.c).
  1313. *
  1314. * @reg: Adds the trigger to the list of triggers associated with the
  1315. * event, and enables the event trigger itself, after
  1316. * initializing it (via the event_trigger_ops @init() function).
  1317. * This is also where commands can use the @trigger_type value to
  1318. * make the decision as to whether or not multiple instances of
  1319. * the trigger should be allowed. This is usually implemented by
  1320. * the generic utility function @register_trigger() (see
  1321. * trace_event_triggers.c).
  1322. *
  1323. * @unreg: Removes the trigger from the list of triggers associated
  1324. * with the event, and disables the event trigger itself, after
  1325. * initializing it (via the event_trigger_ops @free() function).
  1326. * This is usually implemented by the generic utility function
  1327. * @unregister_trigger() (see trace_event_triggers.c).
  1328. *
  1329. * @unreg_all: An optional function called to remove all the triggers
  1330. * from the list of triggers associated with the event. Called
  1331. * when a trigger file is opened in truncate mode.
  1332. *
  1333. * @set_filter: An optional function called to parse and set a filter
  1334. * for the trigger. If no @set_filter() method is set for the
  1335. * event command, filters set by the user for the command will be
  1336. * ignored. This is usually implemented by the generic utility
  1337. * function @set_trigger_filter() (see trace_event_triggers.c).
  1338. *
  1339. * @get_trigger_ops: The callback function invoked to retrieve the
  1340. * event_trigger_ops implementation associated with the command.
  1341. */
  1342. struct event_command {
  1343. struct list_head list;
  1344. char *name;
  1345. enum event_trigger_type trigger_type;
  1346. int flags;
  1347. int (*func)(struct event_command *cmd_ops,
  1348. struct trace_event_file *file,
  1349. char *glob, char *cmd, char *params);
  1350. int (*reg)(char *glob,
  1351. struct event_trigger_ops *ops,
  1352. struct event_trigger_data *data,
  1353. struct trace_event_file *file);
  1354. void (*unreg)(char *glob,
  1355. struct event_trigger_ops *ops,
  1356. struct event_trigger_data *data,
  1357. struct trace_event_file *file);
  1358. void (*unreg_all)(struct trace_event_file *file);
  1359. int (*set_filter)(char *filter_str,
  1360. struct event_trigger_data *data,
  1361. struct trace_event_file *file);
  1362. struct event_trigger_ops *(*get_trigger_ops)(char *cmd, char *param);
  1363. };
  1364. /**
  1365. * enum event_command_flags - flags for struct event_command
  1366. *
  1367. * @POST_TRIGGER: A flag that says whether or not this command needs
  1368. * to have its action delayed until after the current event has
  1369. * been closed. Some triggers need to avoid being invoked while
  1370. * an event is currently in the process of being logged, since
  1371. * the trigger may itself log data into the trace buffer. Thus
  1372. * we make sure the current event is committed before invoking
  1373. * those triggers. To do that, the trigger invocation is split
  1374. * in two - the first part checks the filter using the current
  1375. * trace record; if a command has the @post_trigger flag set, it
  1376. * sets a bit for itself in the return value, otherwise it
  1377. * directly invokes the trigger. Once all commands have been
  1378. * either invoked or set their return flag, the current record is
  1379. * either committed or discarded. At that point, if any commands
  1380. * have deferred their triggers, those commands are finally
  1381. * invoked following the close of the current event. In other
  1382. * words, if the event_trigger_ops @func() probe implementation
  1383. * itself logs to the trace buffer, this flag should be set,
  1384. * otherwise it can be left unspecified.
  1385. *
  1386. * @NEEDS_REC: A flag that says whether or not this command needs
  1387. * access to the trace record in order to perform its function,
  1388. * regardless of whether or not it has a filter associated with
  1389. * it (filters make a trigger require access to the trace record
  1390. * but are not always present).
  1391. */
  1392. enum event_command_flags {
  1393. EVENT_CMD_FL_POST_TRIGGER = 1,
  1394. EVENT_CMD_FL_NEEDS_REC = 2,
  1395. };
  1396. static inline bool event_command_post_trigger(struct event_command *cmd_ops)
  1397. {
  1398. return cmd_ops->flags & EVENT_CMD_FL_POST_TRIGGER;
  1399. }
  1400. static inline bool event_command_needs_rec(struct event_command *cmd_ops)
  1401. {
  1402. return cmd_ops->flags & EVENT_CMD_FL_NEEDS_REC;
  1403. }
  1404. extern int trace_event_enable_disable(struct trace_event_file *file,
  1405. int enable, int soft_disable);
  1406. extern int tracing_alloc_snapshot(void);
  1407. extern const char *__start___trace_bprintk_fmt[];
  1408. extern const char *__stop___trace_bprintk_fmt[];
  1409. extern const char *__start___tracepoint_str[];
  1410. extern const char *__stop___tracepoint_str[];
  1411. void trace_printk_control(bool enabled);
  1412. void trace_printk_init_buffers(void);
  1413. void trace_printk_start_comm(void);
  1414. int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set);
  1415. int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled);
  1416. /*
  1417. * Normal trace_printk() and friends allocates special buffers
  1418. * to do the manipulation, as well as saves the print formats
  1419. * into sections to display. But the trace infrastructure wants
  1420. * to use these without the added overhead at the price of being
  1421. * a bit slower (used mainly for warnings, where we don't care
  1422. * about performance). The internal_trace_puts() is for such
  1423. * a purpose.
  1424. */
  1425. #define internal_trace_puts(str) __trace_puts(_THIS_IP_, str, strlen(str))
  1426. #undef FTRACE_ENTRY
  1427. #define FTRACE_ENTRY(call, struct_name, id, tstruct, print, filter) \
  1428. extern struct trace_event_call \
  1429. __aligned(4) event_##call;
  1430. #undef FTRACE_ENTRY_DUP
  1431. #define FTRACE_ENTRY_DUP(call, struct_name, id, tstruct, print, filter) \
  1432. FTRACE_ENTRY(call, struct_name, id, PARAMS(tstruct), PARAMS(print), \
  1433. filter)
  1434. #undef FTRACE_ENTRY_PACKED
  1435. #define FTRACE_ENTRY_PACKED(call, struct_name, id, tstruct, print, filter) \
  1436. FTRACE_ENTRY(call, struct_name, id, PARAMS(tstruct), PARAMS(print), \
  1437. filter)
  1438. #include "trace_entries.h"
  1439. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_FUNCTION_TRACER)
  1440. int perf_ftrace_event_register(struct trace_event_call *call,
  1441. enum trace_reg type, void *data);
  1442. #else
  1443. #define perf_ftrace_event_register NULL
  1444. #endif
  1445. #ifdef CONFIG_FTRACE_SYSCALLS
  1446. void init_ftrace_syscalls(void);
  1447. const char *get_syscall_name(int syscall);
  1448. #else
  1449. static inline void init_ftrace_syscalls(void) { }
  1450. static inline const char *get_syscall_name(int syscall)
  1451. {
  1452. return NULL;
  1453. }
  1454. #endif
  1455. #ifdef CONFIG_EVENT_TRACING
  1456. void trace_event_init(void);
  1457. void trace_event_enum_update(struct trace_enum_map **map, int len);
  1458. #else
  1459. static inline void __init trace_event_init(void) { }
  1460. static inline void trace_event_enum_update(struct trace_enum_map **map, int len) { }
  1461. #endif
  1462. extern struct trace_iterator *tracepoint_print_iter;
  1463. #endif /* _LINUX_KERNEL_TRACE_H */