trace.h 53 KB

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