trace.h 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674
  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. #else
  747. static inline int ftrace_trace_task(struct trace_array *tr)
  748. {
  749. return 1;
  750. }
  751. static inline int ftrace_is_dead(void) { return 0; }
  752. static inline int
  753. ftrace_create_function_files(struct trace_array *tr,
  754. struct dentry *parent)
  755. {
  756. return 0;
  757. }
  758. static inline void ftrace_destroy_function_files(struct trace_array *tr) { }
  759. static inline __init void
  760. ftrace_init_global_array_ops(struct trace_array *tr) { }
  761. static inline void ftrace_reset_array_ops(struct trace_array *tr) { }
  762. static inline void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d) { }
  763. static inline void ftrace_init_tracefs_toplevel(struct trace_array *tr, struct dentry *d) { }
  764. /* ftace_func_t type is not defined, use macro instead of static inline */
  765. #define ftrace_init_array_ops(tr, func) do { } while (0)
  766. #endif /* CONFIG_FUNCTION_TRACER */
  767. #if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE)
  768. void ftrace_create_filter_files(struct ftrace_ops *ops,
  769. struct dentry *parent);
  770. void ftrace_destroy_filter_files(struct ftrace_ops *ops);
  771. #else
  772. /*
  773. * The ops parameter passed in is usually undefined.
  774. * This must be a macro.
  775. */
  776. #define ftrace_create_filter_files(ops, parent) do { } while (0)
  777. #define ftrace_destroy_filter_files(ops) do { } while (0)
  778. #endif /* CONFIG_FUNCTION_TRACER && CONFIG_DYNAMIC_FTRACE */
  779. bool ftrace_event_is_function(struct trace_event_call *call);
  780. /*
  781. * struct trace_parser - servers for reading the user input separated by spaces
  782. * @cont: set if the input is not complete - no final space char was found
  783. * @buffer: holds the parsed user input
  784. * @idx: user input length
  785. * @size: buffer size
  786. */
  787. struct trace_parser {
  788. bool cont;
  789. char *buffer;
  790. unsigned idx;
  791. unsigned size;
  792. };
  793. static inline bool trace_parser_loaded(struct trace_parser *parser)
  794. {
  795. return (parser->idx != 0);
  796. }
  797. static inline bool trace_parser_cont(struct trace_parser *parser)
  798. {
  799. return parser->cont;
  800. }
  801. static inline void trace_parser_clear(struct trace_parser *parser)
  802. {
  803. parser->cont = false;
  804. parser->idx = 0;
  805. }
  806. extern int trace_parser_get_init(struct trace_parser *parser, int size);
  807. extern void trace_parser_put(struct trace_parser *parser);
  808. extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
  809. size_t cnt, loff_t *ppos);
  810. /*
  811. * Only create function graph options if function graph is configured.
  812. */
  813. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  814. # define FGRAPH_FLAGS \
  815. C(DISPLAY_GRAPH, "display-graph"),
  816. #else
  817. # define FGRAPH_FLAGS
  818. #endif
  819. #ifdef CONFIG_BRANCH_TRACER
  820. # define BRANCH_FLAGS \
  821. C(BRANCH, "branch"),
  822. #else
  823. # define BRANCH_FLAGS
  824. #endif
  825. #ifdef CONFIG_FUNCTION_TRACER
  826. # define FUNCTION_FLAGS \
  827. C(FUNCTION, "function-trace"),
  828. # define FUNCTION_DEFAULT_FLAGS TRACE_ITER_FUNCTION
  829. #else
  830. # define FUNCTION_FLAGS
  831. # define FUNCTION_DEFAULT_FLAGS 0UL
  832. #endif
  833. #ifdef CONFIG_STACKTRACE
  834. # define STACK_FLAGS \
  835. C(STACKTRACE, "stacktrace"),
  836. #else
  837. # define STACK_FLAGS
  838. #endif
  839. /*
  840. * trace_iterator_flags is an enumeration that defines bit
  841. * positions into trace_flags that controls the output.
  842. *
  843. * NOTE: These bits must match the trace_options array in
  844. * trace.c (this macro guarantees it).
  845. */
  846. #define TRACE_FLAGS \
  847. C(PRINT_PARENT, "print-parent"), \
  848. C(SYM_OFFSET, "sym-offset"), \
  849. C(SYM_ADDR, "sym-addr"), \
  850. C(VERBOSE, "verbose"), \
  851. C(RAW, "raw"), \
  852. C(HEX, "hex"), \
  853. C(BIN, "bin"), \
  854. C(BLOCK, "block"), \
  855. C(PRINTK, "trace_printk"), \
  856. C(ANNOTATE, "annotate"), \
  857. C(USERSTACKTRACE, "userstacktrace"), \
  858. C(SYM_USEROBJ, "sym-userobj"), \
  859. C(PRINTK_MSGONLY, "printk-msg-only"), \
  860. C(CONTEXT_INFO, "context-info"), /* Print pid/cpu/time */ \
  861. C(LATENCY_FMT, "latency-format"), \
  862. C(RECORD_CMD, "record-cmd"), \
  863. C(OVERWRITE, "overwrite"), \
  864. C(STOP_ON_FREE, "disable_on_free"), \
  865. C(IRQ_INFO, "irq-info"), \
  866. C(MARKERS, "markers"), \
  867. C(EVENT_FORK, "event-fork"), \
  868. FUNCTION_FLAGS \
  869. FGRAPH_FLAGS \
  870. STACK_FLAGS \
  871. BRANCH_FLAGS
  872. /*
  873. * By defining C, we can make TRACE_FLAGS a list of bit names
  874. * that will define the bits for the flag masks.
  875. */
  876. #undef C
  877. #define C(a, b) TRACE_ITER_##a##_BIT
  878. enum trace_iterator_bits {
  879. TRACE_FLAGS
  880. /* Make sure we don't go more than we have bits for */
  881. TRACE_ITER_LAST_BIT
  882. };
  883. /*
  884. * By redefining C, we can make TRACE_FLAGS a list of masks that
  885. * use the bits as defined above.
  886. */
  887. #undef C
  888. #define C(a, b) TRACE_ITER_##a = (1 << TRACE_ITER_##a##_BIT)
  889. enum trace_iterator_flags { TRACE_FLAGS };
  890. /*
  891. * TRACE_ITER_SYM_MASK masks the options in trace_flags that
  892. * control the output of kernel symbols.
  893. */
  894. #define TRACE_ITER_SYM_MASK \
  895. (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
  896. extern struct tracer nop_trace;
  897. #ifdef CONFIG_BRANCH_TRACER
  898. extern int enable_branch_tracing(struct trace_array *tr);
  899. extern void disable_branch_tracing(void);
  900. static inline int trace_branch_enable(struct trace_array *tr)
  901. {
  902. if (tr->trace_flags & TRACE_ITER_BRANCH)
  903. return enable_branch_tracing(tr);
  904. return 0;
  905. }
  906. static inline void trace_branch_disable(void)
  907. {
  908. /* due to races, always disable */
  909. disable_branch_tracing();
  910. }
  911. #else
  912. static inline int trace_branch_enable(struct trace_array *tr)
  913. {
  914. return 0;
  915. }
  916. static inline void trace_branch_disable(void)
  917. {
  918. }
  919. #endif /* CONFIG_BRANCH_TRACER */
  920. /* set ring buffers to default size if not already done so */
  921. int tracing_update_buffers(void);
  922. struct ftrace_event_field {
  923. struct list_head link;
  924. const char *name;
  925. const char *type;
  926. int filter_type;
  927. int offset;
  928. int size;
  929. int is_signed;
  930. };
  931. struct event_filter {
  932. int n_preds; /* Number assigned */
  933. int a_preds; /* allocated */
  934. struct filter_pred *preds;
  935. struct filter_pred *root;
  936. char *filter_string;
  937. };
  938. struct event_subsystem {
  939. struct list_head list;
  940. const char *name;
  941. struct event_filter *filter;
  942. int ref_count;
  943. };
  944. struct trace_subsystem_dir {
  945. struct list_head list;
  946. struct event_subsystem *subsystem;
  947. struct trace_array *tr;
  948. struct dentry *entry;
  949. int ref_count;
  950. int nr_events;
  951. };
  952. extern int call_filter_check_discard(struct trace_event_call *call, void *rec,
  953. struct ring_buffer *buffer,
  954. struct ring_buffer_event *event);
  955. void trace_buffer_unlock_commit_regs(struct trace_array *tr,
  956. struct ring_buffer *buffer,
  957. struct ring_buffer_event *event,
  958. unsigned long flags, int pc,
  959. struct pt_regs *regs);
  960. static inline void trace_buffer_unlock_commit(struct trace_array *tr,
  961. struct ring_buffer *buffer,
  962. struct ring_buffer_event *event,
  963. unsigned long flags, int pc)
  964. {
  965. trace_buffer_unlock_commit_regs(tr, buffer, event, flags, pc, NULL);
  966. }
  967. DECLARE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
  968. DECLARE_PER_CPU(int, trace_buffered_event_cnt);
  969. void trace_buffered_event_disable(void);
  970. void trace_buffered_event_enable(void);
  971. static inline void
  972. __trace_event_discard_commit(struct ring_buffer *buffer,
  973. struct ring_buffer_event *event)
  974. {
  975. if (this_cpu_read(trace_buffered_event) == event) {
  976. /* Simply release the temp buffer */
  977. this_cpu_dec(trace_buffered_event_cnt);
  978. return;
  979. }
  980. ring_buffer_discard_commit(buffer, event);
  981. }
  982. /*
  983. * Helper function for event_trigger_unlock_commit{_regs}().
  984. * If there are event triggers attached to this event that requires
  985. * filtering against its fields, then they wil be called as the
  986. * entry already holds the field information of the current event.
  987. *
  988. * It also checks if the event should be discarded or not.
  989. * It is to be discarded if the event is soft disabled and the
  990. * event was only recorded to process triggers, or if the event
  991. * filter is active and this event did not match the filters.
  992. *
  993. * Returns true if the event is discarded, false otherwise.
  994. */
  995. static inline bool
  996. __event_trigger_test_discard(struct trace_event_file *file,
  997. struct ring_buffer *buffer,
  998. struct ring_buffer_event *event,
  999. void *entry,
  1000. enum event_trigger_type *tt)
  1001. {
  1002. unsigned long eflags = file->flags;
  1003. if (eflags & EVENT_FILE_FL_TRIGGER_COND)
  1004. *tt = event_triggers_call(file, entry);
  1005. if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags) ||
  1006. (unlikely(file->flags & EVENT_FILE_FL_FILTERED) &&
  1007. !filter_match_preds(file->filter, entry))) {
  1008. __trace_event_discard_commit(buffer, event);
  1009. return true;
  1010. }
  1011. return false;
  1012. }
  1013. /**
  1014. * event_trigger_unlock_commit - handle triggers and finish event commit
  1015. * @file: The file pointer assoctiated to the event
  1016. * @buffer: The ring buffer that the event is being written to
  1017. * @event: The event meta data in the ring buffer
  1018. * @entry: The event itself
  1019. * @irq_flags: The state of the interrupts at the start of the event
  1020. * @pc: The state of the preempt count at the start of the event.
  1021. *
  1022. * This is a helper function to handle triggers that require data
  1023. * from the event itself. It also tests the event against filters and
  1024. * if the event is soft disabled and should be discarded.
  1025. */
  1026. static inline void
  1027. event_trigger_unlock_commit(struct trace_event_file *file,
  1028. struct ring_buffer *buffer,
  1029. struct ring_buffer_event *event,
  1030. void *entry, unsigned long irq_flags, int pc)
  1031. {
  1032. enum event_trigger_type tt = ETT_NONE;
  1033. if (!__event_trigger_test_discard(file, buffer, event, entry, &tt))
  1034. trace_buffer_unlock_commit(file->tr, buffer, event, irq_flags, pc);
  1035. if (tt)
  1036. event_triggers_post_call(file, tt, entry);
  1037. }
  1038. /**
  1039. * event_trigger_unlock_commit_regs - handle triggers and finish event commit
  1040. * @file: The file pointer assoctiated to the event
  1041. * @buffer: The ring buffer that the event is being written to
  1042. * @event: The event meta data in the ring buffer
  1043. * @entry: The event itself
  1044. * @irq_flags: The state of the interrupts at the start of the event
  1045. * @pc: The state of the preempt count at the start of the event.
  1046. *
  1047. * This is a helper function to handle triggers that require data
  1048. * from the event itself. It also tests the event against filters and
  1049. * if the event is soft disabled and should be discarded.
  1050. *
  1051. * Same as event_trigger_unlock_commit() but calls
  1052. * trace_buffer_unlock_commit_regs() instead of trace_buffer_unlock_commit().
  1053. */
  1054. static inline void
  1055. event_trigger_unlock_commit_regs(struct trace_event_file *file,
  1056. struct ring_buffer *buffer,
  1057. struct ring_buffer_event *event,
  1058. void *entry, unsigned long irq_flags, int pc,
  1059. struct pt_regs *regs)
  1060. {
  1061. enum event_trigger_type tt = ETT_NONE;
  1062. if (!__event_trigger_test_discard(file, buffer, event, entry, &tt))
  1063. trace_buffer_unlock_commit_regs(file->tr, buffer, event,
  1064. irq_flags, pc, regs);
  1065. if (tt)
  1066. event_triggers_post_call(file, tt, entry);
  1067. }
  1068. #define FILTER_PRED_INVALID ((unsigned short)-1)
  1069. #define FILTER_PRED_IS_RIGHT (1 << 15)
  1070. #define FILTER_PRED_FOLD (1 << 15)
  1071. /*
  1072. * The max preds is the size of unsigned short with
  1073. * two flags at the MSBs. One bit is used for both the IS_RIGHT
  1074. * and FOLD flags. The other is reserved.
  1075. *
  1076. * 2^14 preds is way more than enough.
  1077. */
  1078. #define MAX_FILTER_PRED 16384
  1079. struct filter_pred;
  1080. struct regex;
  1081. typedef int (*filter_pred_fn_t) (struct filter_pred *pred, void *event);
  1082. typedef int (*regex_match_func)(char *str, struct regex *r, int len);
  1083. enum regex_type {
  1084. MATCH_FULL = 0,
  1085. MATCH_FRONT_ONLY,
  1086. MATCH_MIDDLE_ONLY,
  1087. MATCH_END_ONLY,
  1088. };
  1089. struct regex {
  1090. char pattern[MAX_FILTER_STR_VAL];
  1091. int len;
  1092. int field_len;
  1093. regex_match_func match;
  1094. };
  1095. struct filter_pred {
  1096. filter_pred_fn_t fn;
  1097. u64 val;
  1098. struct regex regex;
  1099. unsigned short *ops;
  1100. struct ftrace_event_field *field;
  1101. int offset;
  1102. int not;
  1103. int op;
  1104. unsigned short index;
  1105. unsigned short parent;
  1106. unsigned short left;
  1107. unsigned short right;
  1108. };
  1109. static inline bool is_string_field(struct ftrace_event_field *field)
  1110. {
  1111. return field->filter_type == FILTER_DYN_STRING ||
  1112. field->filter_type == FILTER_STATIC_STRING ||
  1113. field->filter_type == FILTER_PTR_STRING;
  1114. }
  1115. static inline bool is_function_field(struct ftrace_event_field *field)
  1116. {
  1117. return field->filter_type == FILTER_TRACE_FN;
  1118. }
  1119. extern enum regex_type
  1120. filter_parse_regex(char *buff, int len, char **search, int *not);
  1121. extern void print_event_filter(struct trace_event_file *file,
  1122. struct trace_seq *s);
  1123. extern int apply_event_filter(struct trace_event_file *file,
  1124. char *filter_string);
  1125. extern int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
  1126. char *filter_string);
  1127. extern void print_subsystem_event_filter(struct event_subsystem *system,
  1128. struct trace_seq *s);
  1129. extern int filter_assign_type(const char *type);
  1130. extern int create_event_filter(struct trace_event_call *call,
  1131. char *filter_str, bool set_str,
  1132. struct event_filter **filterp);
  1133. extern void free_event_filter(struct event_filter *filter);
  1134. struct ftrace_event_field *
  1135. trace_find_event_field(struct trace_event_call *call, char *name);
  1136. extern void trace_event_enable_cmd_record(bool enable);
  1137. extern int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr);
  1138. extern int event_trace_del_tracer(struct trace_array *tr);
  1139. extern struct trace_event_file *find_event_file(struct trace_array *tr,
  1140. const char *system,
  1141. const char *event);
  1142. static inline void *event_file_data(struct file *filp)
  1143. {
  1144. return ACCESS_ONCE(file_inode(filp)->i_private);
  1145. }
  1146. extern struct mutex event_mutex;
  1147. extern struct list_head ftrace_events;
  1148. extern const struct file_operations event_trigger_fops;
  1149. extern const struct file_operations event_hist_fops;
  1150. #ifdef CONFIG_HIST_TRIGGERS
  1151. extern int register_trigger_hist_cmd(void);
  1152. extern int register_trigger_hist_enable_disable_cmds(void);
  1153. #else
  1154. static inline int register_trigger_hist_cmd(void) { return 0; }
  1155. static inline int register_trigger_hist_enable_disable_cmds(void) { return 0; }
  1156. #endif
  1157. extern int register_trigger_cmds(void);
  1158. extern void clear_event_triggers(struct trace_array *tr);
  1159. struct event_trigger_data {
  1160. unsigned long count;
  1161. int ref;
  1162. struct event_trigger_ops *ops;
  1163. struct event_command *cmd_ops;
  1164. struct event_filter __rcu *filter;
  1165. char *filter_str;
  1166. void *private_data;
  1167. bool paused;
  1168. bool paused_tmp;
  1169. struct list_head list;
  1170. char *name;
  1171. struct list_head named_list;
  1172. struct event_trigger_data *named_data;
  1173. };
  1174. /* Avoid typos */
  1175. #define ENABLE_EVENT_STR "enable_event"
  1176. #define DISABLE_EVENT_STR "disable_event"
  1177. #define ENABLE_HIST_STR "enable_hist"
  1178. #define DISABLE_HIST_STR "disable_hist"
  1179. struct enable_trigger_data {
  1180. struct trace_event_file *file;
  1181. bool enable;
  1182. bool hist;
  1183. };
  1184. extern int event_enable_trigger_print(struct seq_file *m,
  1185. struct event_trigger_ops *ops,
  1186. struct event_trigger_data *data);
  1187. extern void event_enable_trigger_free(struct event_trigger_ops *ops,
  1188. struct event_trigger_data *data);
  1189. extern int event_enable_trigger_func(struct event_command *cmd_ops,
  1190. struct trace_event_file *file,
  1191. char *glob, char *cmd, char *param);
  1192. extern int event_enable_register_trigger(char *glob,
  1193. struct event_trigger_ops *ops,
  1194. struct event_trigger_data *data,
  1195. struct trace_event_file *file);
  1196. extern void event_enable_unregister_trigger(char *glob,
  1197. struct event_trigger_ops *ops,
  1198. struct event_trigger_data *test,
  1199. struct trace_event_file *file);
  1200. extern void trigger_data_free(struct event_trigger_data *data);
  1201. extern int event_trigger_init(struct event_trigger_ops *ops,
  1202. struct event_trigger_data *data);
  1203. extern int trace_event_trigger_enable_disable(struct trace_event_file *file,
  1204. int trigger_enable);
  1205. extern void update_cond_flag(struct trace_event_file *file);
  1206. extern void unregister_trigger(char *glob, struct event_trigger_ops *ops,
  1207. struct event_trigger_data *test,
  1208. struct trace_event_file *file);
  1209. extern int set_trigger_filter(char *filter_str,
  1210. struct event_trigger_data *trigger_data,
  1211. struct trace_event_file *file);
  1212. extern struct event_trigger_data *find_named_trigger(const char *name);
  1213. extern bool is_named_trigger(struct event_trigger_data *test);
  1214. extern int save_named_trigger(const char *name,
  1215. struct event_trigger_data *data);
  1216. extern void del_named_trigger(struct event_trigger_data *data);
  1217. extern void pause_named_trigger(struct event_trigger_data *data);
  1218. extern void unpause_named_trigger(struct event_trigger_data *data);
  1219. extern void set_named_trigger_data(struct event_trigger_data *data,
  1220. struct event_trigger_data *named_data);
  1221. extern int register_event_command(struct event_command *cmd);
  1222. extern int unregister_event_command(struct event_command *cmd);
  1223. extern int register_trigger_hist_enable_disable_cmds(void);
  1224. /**
  1225. * struct event_trigger_ops - callbacks for trace event triggers
  1226. *
  1227. * The methods in this structure provide per-event trigger hooks for
  1228. * various trigger operations.
  1229. *
  1230. * All the methods below, except for @init() and @free(), must be
  1231. * implemented.
  1232. *
  1233. * @func: The trigger 'probe' function called when the triggering
  1234. * event occurs. The data passed into this callback is the data
  1235. * that was supplied to the event_command @reg() function that
  1236. * registered the trigger (see struct event_command) along with
  1237. * the trace record, rec.
  1238. *
  1239. * @init: An optional initialization function called for the trigger
  1240. * when the trigger is registered (via the event_command reg()
  1241. * function). This can be used to perform per-trigger
  1242. * initialization such as incrementing a per-trigger reference
  1243. * count, for instance. This is usually implemented by the
  1244. * generic utility function @event_trigger_init() (see
  1245. * trace_event_triggers.c).
  1246. *
  1247. * @free: An optional de-initialization function called for the
  1248. * trigger when the trigger is unregistered (via the
  1249. * event_command @reg() function). This can be used to perform
  1250. * per-trigger de-initialization such as decrementing a
  1251. * per-trigger reference count and freeing corresponding trigger
  1252. * data, for instance. This is usually implemented by the
  1253. * generic utility function @event_trigger_free() (see
  1254. * trace_event_triggers.c).
  1255. *
  1256. * @print: The callback function invoked to have the trigger print
  1257. * itself. This is usually implemented by a wrapper function
  1258. * that calls the generic utility function @event_trigger_print()
  1259. * (see trace_event_triggers.c).
  1260. */
  1261. struct event_trigger_ops {
  1262. void (*func)(struct event_trigger_data *data,
  1263. void *rec);
  1264. int (*init)(struct event_trigger_ops *ops,
  1265. struct event_trigger_data *data);
  1266. void (*free)(struct event_trigger_ops *ops,
  1267. struct event_trigger_data *data);
  1268. int (*print)(struct seq_file *m,
  1269. struct event_trigger_ops *ops,
  1270. struct event_trigger_data *data);
  1271. };
  1272. /**
  1273. * struct event_command - callbacks and data members for event commands
  1274. *
  1275. * Event commands are invoked by users by writing the command name
  1276. * into the 'trigger' file associated with a trace event. The
  1277. * parameters associated with a specific invocation of an event
  1278. * command are used to create an event trigger instance, which is
  1279. * added to the list of trigger instances associated with that trace
  1280. * event. When the event is hit, the set of triggers associated with
  1281. * that event is invoked.
  1282. *
  1283. * The data members in this structure provide per-event command data
  1284. * for various event commands.
  1285. *
  1286. * All the data members below, except for @post_trigger, must be set
  1287. * for each event command.
  1288. *
  1289. * @name: The unique name that identifies the event command. This is
  1290. * the name used when setting triggers via trigger files.
  1291. *
  1292. * @trigger_type: A unique id that identifies the event command
  1293. * 'type'. This value has two purposes, the first to ensure that
  1294. * only one trigger of the same type can be set at a given time
  1295. * for a particular event e.g. it doesn't make sense to have both
  1296. * a traceon and traceoff trigger attached to a single event at
  1297. * the same time, so traceon and traceoff have the same type
  1298. * though they have different names. The @trigger_type value is
  1299. * also used as a bit value for deferring the actual trigger
  1300. * action until after the current event is finished. Some
  1301. * commands need to do this if they themselves log to the trace
  1302. * buffer (see the @post_trigger() member below). @trigger_type
  1303. * values are defined by adding new values to the trigger_type
  1304. * enum in include/linux/trace_events.h.
  1305. *
  1306. * @flags: See the enum event_command_flags below.
  1307. *
  1308. * All the methods below, except for @set_filter() and @unreg_all(),
  1309. * must be implemented.
  1310. *
  1311. * @func: The callback function responsible for parsing and
  1312. * registering the trigger written to the 'trigger' file by the
  1313. * user. It allocates the trigger instance and registers it with
  1314. * the appropriate trace event. It makes use of the other
  1315. * event_command callback functions to orchestrate this, and is
  1316. * usually implemented by the generic utility function
  1317. * @event_trigger_callback() (see trace_event_triggers.c).
  1318. *
  1319. * @reg: Adds the trigger to the list of triggers associated with the
  1320. * event, and enables the event trigger itself, after
  1321. * initializing it (via the event_trigger_ops @init() function).
  1322. * This is also where commands can use the @trigger_type value to
  1323. * make the decision as to whether or not multiple instances of
  1324. * the trigger should be allowed. This is usually implemented by
  1325. * the generic utility function @register_trigger() (see
  1326. * trace_event_triggers.c).
  1327. *
  1328. * @unreg: Removes the trigger from the list of triggers associated
  1329. * with the event, and disables the event trigger itself, after
  1330. * initializing it (via the event_trigger_ops @free() function).
  1331. * This is usually implemented by the generic utility function
  1332. * @unregister_trigger() (see trace_event_triggers.c).
  1333. *
  1334. * @unreg_all: An optional function called to remove all the triggers
  1335. * from the list of triggers associated with the event. Called
  1336. * when a trigger file is opened in truncate mode.
  1337. *
  1338. * @set_filter: An optional function called to parse and set a filter
  1339. * for the trigger. If no @set_filter() method is set for the
  1340. * event command, filters set by the user for the command will be
  1341. * ignored. This is usually implemented by the generic utility
  1342. * function @set_trigger_filter() (see trace_event_triggers.c).
  1343. *
  1344. * @get_trigger_ops: The callback function invoked to retrieve the
  1345. * event_trigger_ops implementation associated with the command.
  1346. */
  1347. struct event_command {
  1348. struct list_head list;
  1349. char *name;
  1350. enum event_trigger_type trigger_type;
  1351. int flags;
  1352. int (*func)(struct event_command *cmd_ops,
  1353. struct trace_event_file *file,
  1354. char *glob, char *cmd, char *params);
  1355. int (*reg)(char *glob,
  1356. struct event_trigger_ops *ops,
  1357. struct event_trigger_data *data,
  1358. struct trace_event_file *file);
  1359. void (*unreg)(char *glob,
  1360. struct event_trigger_ops *ops,
  1361. struct event_trigger_data *data,
  1362. struct trace_event_file *file);
  1363. void (*unreg_all)(struct trace_event_file *file);
  1364. int (*set_filter)(char *filter_str,
  1365. struct event_trigger_data *data,
  1366. struct trace_event_file *file);
  1367. struct event_trigger_ops *(*get_trigger_ops)(char *cmd, char *param);
  1368. };
  1369. /**
  1370. * enum event_command_flags - flags for struct event_command
  1371. *
  1372. * @POST_TRIGGER: A flag that says whether or not this command needs
  1373. * to have its action delayed until after the current event has
  1374. * been closed. Some triggers need to avoid being invoked while
  1375. * an event is currently in the process of being logged, since
  1376. * the trigger may itself log data into the trace buffer. Thus
  1377. * we make sure the current event is committed before invoking
  1378. * those triggers. To do that, the trigger invocation is split
  1379. * in two - the first part checks the filter using the current
  1380. * trace record; if a command has the @post_trigger flag set, it
  1381. * sets a bit for itself in the return value, otherwise it
  1382. * directly invokes the trigger. Once all commands have been
  1383. * either invoked or set their return flag, the current record is
  1384. * either committed or discarded. At that point, if any commands
  1385. * have deferred their triggers, those commands are finally
  1386. * invoked following the close of the current event. In other
  1387. * words, if the event_trigger_ops @func() probe implementation
  1388. * itself logs to the trace buffer, this flag should be set,
  1389. * otherwise it can be left unspecified.
  1390. *
  1391. * @NEEDS_REC: A flag that says whether or not this command needs
  1392. * access to the trace record in order to perform its function,
  1393. * regardless of whether or not it has a filter associated with
  1394. * it (filters make a trigger require access to the trace record
  1395. * but are not always present).
  1396. */
  1397. enum event_command_flags {
  1398. EVENT_CMD_FL_POST_TRIGGER = 1,
  1399. EVENT_CMD_FL_NEEDS_REC = 2,
  1400. };
  1401. static inline bool event_command_post_trigger(struct event_command *cmd_ops)
  1402. {
  1403. return cmd_ops->flags & EVENT_CMD_FL_POST_TRIGGER;
  1404. }
  1405. static inline bool event_command_needs_rec(struct event_command *cmd_ops)
  1406. {
  1407. return cmd_ops->flags & EVENT_CMD_FL_NEEDS_REC;
  1408. }
  1409. extern int trace_event_enable_disable(struct trace_event_file *file,
  1410. int enable, int soft_disable);
  1411. extern int tracing_alloc_snapshot(void);
  1412. extern const char *__start___trace_bprintk_fmt[];
  1413. extern const char *__stop___trace_bprintk_fmt[];
  1414. extern const char *__start___tracepoint_str[];
  1415. extern const char *__stop___tracepoint_str[];
  1416. void trace_printk_control(bool enabled);
  1417. void trace_printk_init_buffers(void);
  1418. void trace_printk_start_comm(void);
  1419. int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set);
  1420. int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled);
  1421. /*
  1422. * Normal trace_printk() and friends allocates special buffers
  1423. * to do the manipulation, as well as saves the print formats
  1424. * into sections to display. But the trace infrastructure wants
  1425. * to use these without the added overhead at the price of being
  1426. * a bit slower (used mainly for warnings, where we don't care
  1427. * about performance). The internal_trace_puts() is for such
  1428. * a purpose.
  1429. */
  1430. #define internal_trace_puts(str) __trace_puts(_THIS_IP_, str, strlen(str))
  1431. #undef FTRACE_ENTRY
  1432. #define FTRACE_ENTRY(call, struct_name, id, tstruct, print, filter) \
  1433. extern struct trace_event_call \
  1434. __aligned(4) event_##call;
  1435. #undef FTRACE_ENTRY_DUP
  1436. #define FTRACE_ENTRY_DUP(call, struct_name, id, tstruct, print, filter) \
  1437. FTRACE_ENTRY(call, struct_name, id, PARAMS(tstruct), PARAMS(print), \
  1438. filter)
  1439. #undef FTRACE_ENTRY_PACKED
  1440. #define FTRACE_ENTRY_PACKED(call, struct_name, id, tstruct, print, filter) \
  1441. FTRACE_ENTRY(call, struct_name, id, PARAMS(tstruct), PARAMS(print), \
  1442. filter)
  1443. #include "trace_entries.h"
  1444. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_FUNCTION_TRACER)
  1445. int perf_ftrace_event_register(struct trace_event_call *call,
  1446. enum trace_reg type, void *data);
  1447. #else
  1448. #define perf_ftrace_event_register NULL
  1449. #endif
  1450. #ifdef CONFIG_FTRACE_SYSCALLS
  1451. void init_ftrace_syscalls(void);
  1452. const char *get_syscall_name(int syscall);
  1453. #else
  1454. static inline void init_ftrace_syscalls(void) { }
  1455. static inline const char *get_syscall_name(int syscall)
  1456. {
  1457. return NULL;
  1458. }
  1459. #endif
  1460. #ifdef CONFIG_EVENT_TRACING
  1461. void trace_event_init(void);
  1462. void trace_event_enum_update(struct trace_enum_map **map, int len);
  1463. #else
  1464. static inline void __init trace_event_init(void) { }
  1465. static inline void trace_event_enum_update(struct trace_enum_map **map, int len) { }
  1466. #endif
  1467. extern struct trace_iterator *tracepoint_print_iter;
  1468. #endif /* _LINUX_KERNEL_TRACE_H */