trace.h 52 KB

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