trace.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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/ftrace_event.h>
  14. #ifdef CONFIG_FTRACE_SYSCALLS
  15. #include <asm/unistd.h> /* For NR_SYSCALLS */
  16. #include <asm/syscall.h> /* some archs define it here */
  17. #endif
  18. enum trace_type {
  19. __TRACE_FIRST_TYPE = 0,
  20. TRACE_FN,
  21. TRACE_CTX,
  22. TRACE_WAKE,
  23. TRACE_STACK,
  24. TRACE_PRINT,
  25. TRACE_BPRINT,
  26. TRACE_MMIO_RW,
  27. TRACE_MMIO_MAP,
  28. TRACE_BRANCH,
  29. TRACE_GRAPH_RET,
  30. TRACE_GRAPH_ENT,
  31. TRACE_USER_STACK,
  32. TRACE_BLK,
  33. TRACE_BPUTS,
  34. __TRACE_LAST_TYPE,
  35. };
  36. #undef __field
  37. #define __field(type, item) type item;
  38. #undef __field_struct
  39. #define __field_struct(type, item) __field(type, item)
  40. #undef __field_desc
  41. #define __field_desc(type, container, item)
  42. #undef __array
  43. #define __array(type, item, size) type item[size];
  44. #undef __array_desc
  45. #define __array_desc(type, container, item, size)
  46. #undef __dynamic_array
  47. #define __dynamic_array(type, item) type item[];
  48. #undef F_STRUCT
  49. #define F_STRUCT(args...) args
  50. #undef FTRACE_ENTRY
  51. #define FTRACE_ENTRY(name, struct_name, id, tstruct, print, filter) \
  52. struct struct_name { \
  53. struct trace_entry ent; \
  54. tstruct \
  55. }
  56. #undef TP_ARGS
  57. #define TP_ARGS(args...) args
  58. #undef FTRACE_ENTRY_DUP
  59. #define FTRACE_ENTRY_DUP(name, name_struct, id, tstruct, printk, filter)
  60. #undef FTRACE_ENTRY_REG
  61. #define FTRACE_ENTRY_REG(name, struct_name, id, tstruct, print, \
  62. filter, regfn) \
  63. FTRACE_ENTRY(name, struct_name, id, PARAMS(tstruct), PARAMS(print), \
  64. filter)
  65. #include "trace_entries.h"
  66. /*
  67. * syscalls are special, and need special handling, this is why
  68. * they are not included in trace_entries.h
  69. */
  70. struct syscall_trace_enter {
  71. struct trace_entry ent;
  72. int nr;
  73. unsigned long args[];
  74. };
  75. struct syscall_trace_exit {
  76. struct trace_entry ent;
  77. int nr;
  78. long ret;
  79. };
  80. struct kprobe_trace_entry_head {
  81. struct trace_entry ent;
  82. unsigned long ip;
  83. };
  84. struct kretprobe_trace_entry_head {
  85. struct trace_entry ent;
  86. unsigned long func;
  87. unsigned long ret_ip;
  88. };
  89. /*
  90. * trace_flag_type is an enumeration that holds different
  91. * states when a trace occurs. These are:
  92. * IRQS_OFF - interrupts were disabled
  93. * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
  94. * NEED_RESCHED - reschedule is requested
  95. * HARDIRQ - inside an interrupt handler
  96. * SOFTIRQ - inside a softirq handler
  97. */
  98. enum trace_flag_type {
  99. TRACE_FLAG_IRQS_OFF = 0x01,
  100. TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
  101. TRACE_FLAG_NEED_RESCHED = 0x04,
  102. TRACE_FLAG_HARDIRQ = 0x08,
  103. TRACE_FLAG_SOFTIRQ = 0x10,
  104. };
  105. #define TRACE_BUF_SIZE 1024
  106. struct trace_array;
  107. /*
  108. * The CPU trace array - it consists of thousands of trace entries
  109. * plus some other descriptor data: (for example which task started
  110. * the trace, etc.)
  111. */
  112. struct trace_array_cpu {
  113. atomic_t disabled;
  114. void *buffer_page; /* ring buffer spare */
  115. unsigned long entries;
  116. unsigned long saved_latency;
  117. unsigned long critical_start;
  118. unsigned long critical_end;
  119. unsigned long critical_sequence;
  120. unsigned long nice;
  121. unsigned long policy;
  122. unsigned long rt_priority;
  123. unsigned long skipped_entries;
  124. cycle_t preempt_timestamp;
  125. pid_t pid;
  126. kuid_t uid;
  127. char comm[TASK_COMM_LEN];
  128. };
  129. struct tracer;
  130. struct trace_buffer {
  131. struct trace_array *tr;
  132. struct ring_buffer *buffer;
  133. struct trace_array_cpu __percpu *data;
  134. cycle_t time_start;
  135. int cpu;
  136. };
  137. /*
  138. * The trace array - an array of per-CPU trace arrays. This is the
  139. * highest level data structure that individual tracers deal with.
  140. * They have on/off state as well:
  141. */
  142. struct trace_array {
  143. struct list_head list;
  144. char *name;
  145. struct trace_buffer trace_buffer;
  146. #ifdef CONFIG_TRACER_MAX_TRACE
  147. /*
  148. * The max_buffer is used to snapshot the trace when a maximum
  149. * latency is reached, or when the user initiates a snapshot.
  150. * Some tracers will use this to store a maximum trace while
  151. * it continues examining live traces.
  152. *
  153. * The buffers for the max_buffer are set up the same as the trace_buffer
  154. * When a snapshot is taken, the buffer of the max_buffer is swapped
  155. * with the buffer of the trace_buffer and the buffers are reset for
  156. * the trace_buffer so the tracing can continue.
  157. */
  158. struct trace_buffer max_buffer;
  159. bool allocated_snapshot;
  160. #endif
  161. int buffer_disabled;
  162. #ifdef CONFIG_FTRACE_SYSCALLS
  163. int sys_refcount_enter;
  164. int sys_refcount_exit;
  165. DECLARE_BITMAP(enabled_enter_syscalls, NR_syscalls);
  166. DECLARE_BITMAP(enabled_exit_syscalls, NR_syscalls);
  167. #endif
  168. int stop_count;
  169. int clock_id;
  170. struct tracer *current_trace;
  171. unsigned int flags;
  172. raw_spinlock_t start_lock;
  173. struct dentry *dir;
  174. struct dentry *options;
  175. struct dentry *percpu_dir;
  176. struct dentry *event_dir;
  177. struct list_head systems;
  178. struct list_head events;
  179. cpumask_var_t tracing_cpumask; /* only trace on set CPUs */
  180. int ref;
  181. };
  182. enum {
  183. TRACE_ARRAY_FL_GLOBAL = (1 << 0)
  184. };
  185. extern struct list_head ftrace_trace_arrays;
  186. extern struct mutex trace_types_lock;
  187. extern int trace_array_get(struct trace_array *tr);
  188. extern void trace_array_put(struct trace_array *tr);
  189. /*
  190. * The global tracer (top) should be the first trace array added,
  191. * but we check the flag anyway.
  192. */
  193. static inline struct trace_array *top_trace_array(void)
  194. {
  195. struct trace_array *tr;
  196. tr = list_entry(ftrace_trace_arrays.prev,
  197. typeof(*tr), list);
  198. WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
  199. return tr;
  200. }
  201. #define FTRACE_CMP_TYPE(var, type) \
  202. __builtin_types_compatible_p(typeof(var), type *)
  203. #undef IF_ASSIGN
  204. #define IF_ASSIGN(var, entry, etype, id) \
  205. if (FTRACE_CMP_TYPE(var, etype)) { \
  206. var = (typeof(var))(entry); \
  207. WARN_ON(id && (entry)->type != id); \
  208. break; \
  209. }
  210. /* Will cause compile errors if type is not found. */
  211. extern void __ftrace_bad_type(void);
  212. /*
  213. * The trace_assign_type is a verifier that the entry type is
  214. * the same as the type being assigned. To add new types simply
  215. * add a line with the following format:
  216. *
  217. * IF_ASSIGN(var, ent, type, id);
  218. *
  219. * Where "type" is the trace type that includes the trace_entry
  220. * as the "ent" item. And "id" is the trace identifier that is
  221. * used in the trace_type enum.
  222. *
  223. * If the type can have more than one id, then use zero.
  224. */
  225. #define trace_assign_type(var, ent) \
  226. do { \
  227. IF_ASSIGN(var, ent, struct ftrace_entry, TRACE_FN); \
  228. IF_ASSIGN(var, ent, struct ctx_switch_entry, 0); \
  229. IF_ASSIGN(var, ent, struct stack_entry, TRACE_STACK); \
  230. IF_ASSIGN(var, ent, struct userstack_entry, TRACE_USER_STACK);\
  231. IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT); \
  232. IF_ASSIGN(var, ent, struct bprint_entry, TRACE_BPRINT); \
  233. IF_ASSIGN(var, ent, struct bputs_entry, TRACE_BPUTS); \
  234. IF_ASSIGN(var, ent, struct trace_mmiotrace_rw, \
  235. TRACE_MMIO_RW); \
  236. IF_ASSIGN(var, ent, struct trace_mmiotrace_map, \
  237. TRACE_MMIO_MAP); \
  238. IF_ASSIGN(var, ent, struct trace_branch, TRACE_BRANCH); \
  239. IF_ASSIGN(var, ent, struct ftrace_graph_ent_entry, \
  240. TRACE_GRAPH_ENT); \
  241. IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry, \
  242. TRACE_GRAPH_RET); \
  243. __ftrace_bad_type(); \
  244. } while (0)
  245. /*
  246. * An option specific to a tracer. This is a boolean value.
  247. * The bit is the bit index that sets its value on the
  248. * flags value in struct tracer_flags.
  249. */
  250. struct tracer_opt {
  251. const char *name; /* Will appear on the trace_options file */
  252. u32 bit; /* Mask assigned in val field in tracer_flags */
  253. };
  254. /*
  255. * The set of specific options for a tracer. Your tracer
  256. * have to set the initial value of the flags val.
  257. */
  258. struct tracer_flags {
  259. u32 val;
  260. struct tracer_opt *opts;
  261. };
  262. /* Makes more easy to define a tracer opt */
  263. #define TRACER_OPT(s, b) .name = #s, .bit = b
  264. /**
  265. * struct tracer - a specific tracer and its callbacks to interact with debugfs
  266. * @name: the name chosen to select it on the available_tracers file
  267. * @init: called when one switches to this tracer (echo name > current_tracer)
  268. * @reset: called when one switches to another tracer
  269. * @start: called when tracing is unpaused (echo 1 > tracing_enabled)
  270. * @stop: called when tracing is paused (echo 0 > tracing_enabled)
  271. * @open: called when the trace file is opened
  272. * @pipe_open: called when the trace_pipe file is opened
  273. * @wait_pipe: override how the user waits for traces on trace_pipe
  274. * @close: called when the trace file is released
  275. * @pipe_close: called when the trace_pipe file is released
  276. * @read: override the default read callback on trace_pipe
  277. * @splice_read: override the default splice_read callback on trace_pipe
  278. * @selftest: selftest to run on boot (see trace_selftest.c)
  279. * @print_headers: override the first lines that describe your columns
  280. * @print_line: callback that prints a trace
  281. * @set_flag: signals one of your private flags changed (trace_options file)
  282. * @flags: your private flags
  283. */
  284. struct tracer {
  285. const char *name;
  286. int (*init)(struct trace_array *tr);
  287. void (*reset)(struct trace_array *tr);
  288. void (*start)(struct trace_array *tr);
  289. void (*stop)(struct trace_array *tr);
  290. void (*open)(struct trace_iterator *iter);
  291. void (*pipe_open)(struct trace_iterator *iter);
  292. void (*wait_pipe)(struct trace_iterator *iter);
  293. void (*close)(struct trace_iterator *iter);
  294. void (*pipe_close)(struct trace_iterator *iter);
  295. ssize_t (*read)(struct trace_iterator *iter,
  296. struct file *filp, char __user *ubuf,
  297. size_t cnt, loff_t *ppos);
  298. ssize_t (*splice_read)(struct trace_iterator *iter,
  299. struct file *filp,
  300. loff_t *ppos,
  301. struct pipe_inode_info *pipe,
  302. size_t len,
  303. unsigned int flags);
  304. #ifdef CONFIG_FTRACE_STARTUP_TEST
  305. int (*selftest)(struct tracer *trace,
  306. struct trace_array *tr);
  307. #endif
  308. void (*print_header)(struct seq_file *m);
  309. enum print_line_t (*print_line)(struct trace_iterator *iter);
  310. /* If you handled the flag setting, return 0 */
  311. int (*set_flag)(u32 old_flags, u32 bit, int set);
  312. /* Return 0 if OK with change, else return non-zero */
  313. int (*flag_changed)(struct tracer *tracer,
  314. u32 mask, int set);
  315. struct tracer *next;
  316. struct tracer_flags *flags;
  317. bool print_max;
  318. bool enabled;
  319. #ifdef CONFIG_TRACER_MAX_TRACE
  320. bool use_max_tr;
  321. #endif
  322. };
  323. /* Only current can touch trace_recursion */
  324. /*
  325. * For function tracing recursion:
  326. * The order of these bits are important.
  327. *
  328. * When function tracing occurs, the following steps are made:
  329. * If arch does not support a ftrace feature:
  330. * call internal function (uses INTERNAL bits) which calls...
  331. * If callback is registered to the "global" list, the list
  332. * function is called and recursion checks the GLOBAL bits.
  333. * then this function calls...
  334. * The function callback, which can use the FTRACE bits to
  335. * check for recursion.
  336. *
  337. * Now if the arch does not suppport a feature, and it calls
  338. * the global list function which calls the ftrace callback
  339. * all three of these steps will do a recursion protection.
  340. * There's no reason to do one if the previous caller already
  341. * did. The recursion that we are protecting against will
  342. * go through the same steps again.
  343. *
  344. * To prevent the multiple recursion checks, if a recursion
  345. * bit is set that is higher than the MAX bit of the current
  346. * check, then we know that the check was made by the previous
  347. * caller, and we can skip the current check.
  348. */
  349. enum {
  350. TRACE_BUFFER_BIT,
  351. TRACE_BUFFER_NMI_BIT,
  352. TRACE_BUFFER_IRQ_BIT,
  353. TRACE_BUFFER_SIRQ_BIT,
  354. /* Start of function recursion bits */
  355. TRACE_FTRACE_BIT,
  356. TRACE_FTRACE_NMI_BIT,
  357. TRACE_FTRACE_IRQ_BIT,
  358. TRACE_FTRACE_SIRQ_BIT,
  359. /* GLOBAL_BITs must be greater than FTRACE_BITs */
  360. TRACE_GLOBAL_BIT,
  361. TRACE_GLOBAL_NMI_BIT,
  362. TRACE_GLOBAL_IRQ_BIT,
  363. TRACE_GLOBAL_SIRQ_BIT,
  364. /* INTERNAL_BITs must be greater than GLOBAL_BITs */
  365. TRACE_INTERNAL_BIT,
  366. TRACE_INTERNAL_NMI_BIT,
  367. TRACE_INTERNAL_IRQ_BIT,
  368. TRACE_INTERNAL_SIRQ_BIT,
  369. TRACE_CONTROL_BIT,
  370. /*
  371. * Abuse of the trace_recursion.
  372. * As we need a way to maintain state if we are tracing the function
  373. * graph in irq because we want to trace a particular function that
  374. * was called in irq context but we have irq tracing off. Since this
  375. * can only be modified by current, we can reuse trace_recursion.
  376. */
  377. TRACE_IRQ_BIT,
  378. };
  379. #define trace_recursion_set(bit) do { (current)->trace_recursion |= (1<<(bit)); } while (0)
  380. #define trace_recursion_clear(bit) do { (current)->trace_recursion &= ~(1<<(bit)); } while (0)
  381. #define trace_recursion_test(bit) ((current)->trace_recursion & (1<<(bit)))
  382. #define TRACE_CONTEXT_BITS 4
  383. #define TRACE_FTRACE_START TRACE_FTRACE_BIT
  384. #define TRACE_FTRACE_MAX ((1 << (TRACE_FTRACE_START + TRACE_CONTEXT_BITS)) - 1)
  385. #define TRACE_GLOBAL_START TRACE_GLOBAL_BIT
  386. #define TRACE_GLOBAL_MAX ((1 << (TRACE_GLOBAL_START + TRACE_CONTEXT_BITS)) - 1)
  387. #define TRACE_LIST_START TRACE_INTERNAL_BIT
  388. #define TRACE_LIST_MAX ((1 << (TRACE_LIST_START + TRACE_CONTEXT_BITS)) - 1)
  389. #define TRACE_CONTEXT_MASK TRACE_LIST_MAX
  390. static __always_inline int trace_get_context_bit(void)
  391. {
  392. int bit;
  393. if (in_interrupt()) {
  394. if (in_nmi())
  395. bit = 0;
  396. else if (in_irq())
  397. bit = 1;
  398. else
  399. bit = 2;
  400. } else
  401. bit = 3;
  402. return bit;
  403. }
  404. static __always_inline int trace_test_and_set_recursion(int start, int max)
  405. {
  406. unsigned int val = current->trace_recursion;
  407. int bit;
  408. /* A previous recursion check was made */
  409. if ((val & TRACE_CONTEXT_MASK) > max)
  410. return 0;
  411. bit = trace_get_context_bit() + start;
  412. if (unlikely(val & (1 << bit)))
  413. return -1;
  414. val |= 1 << bit;
  415. current->trace_recursion = val;
  416. barrier();
  417. return bit;
  418. }
  419. static __always_inline void trace_clear_recursion(int bit)
  420. {
  421. unsigned int val = current->trace_recursion;
  422. if (!bit)
  423. return;
  424. bit = 1 << bit;
  425. val &= ~bit;
  426. barrier();
  427. current->trace_recursion = val;
  428. }
  429. static inline struct ring_buffer_iter *
  430. trace_buffer_iter(struct trace_iterator *iter, int cpu)
  431. {
  432. if (iter->buffer_iter && iter->buffer_iter[cpu])
  433. return iter->buffer_iter[cpu];
  434. return NULL;
  435. }
  436. int tracer_init(struct tracer *t, struct trace_array *tr);
  437. int tracing_is_enabled(void);
  438. void tracing_reset(struct trace_buffer *buf, int cpu);
  439. void tracing_reset_online_cpus(struct trace_buffer *buf);
  440. void tracing_reset_current(int cpu);
  441. void tracing_reset_all_online_cpus(void);
  442. int tracing_open_generic(struct inode *inode, struct file *filp);
  443. struct dentry *trace_create_file(const char *name,
  444. umode_t mode,
  445. struct dentry *parent,
  446. void *data,
  447. const struct file_operations *fops);
  448. struct dentry *tracing_init_dentry_tr(struct trace_array *tr);
  449. struct dentry *tracing_init_dentry(void);
  450. struct ring_buffer_event;
  451. struct ring_buffer_event *
  452. trace_buffer_lock_reserve(struct ring_buffer *buffer,
  453. int type,
  454. unsigned long len,
  455. unsigned long flags,
  456. int pc);
  457. struct trace_entry *tracing_get_trace_entry(struct trace_array *tr,
  458. struct trace_array_cpu *data);
  459. struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
  460. int *ent_cpu, u64 *ent_ts);
  461. void __buffer_unlock_commit(struct ring_buffer *buffer,
  462. struct ring_buffer_event *event);
  463. int trace_empty(struct trace_iterator *iter);
  464. void *trace_find_next_entry_inc(struct trace_iterator *iter);
  465. void trace_init_global_iter(struct trace_iterator *iter);
  466. void tracing_iter_reset(struct trace_iterator *iter, int cpu);
  467. void poll_wait_pipe(struct trace_iterator *iter);
  468. void tracing_sched_switch_trace(struct trace_array *tr,
  469. struct task_struct *prev,
  470. struct task_struct *next,
  471. unsigned long flags, int pc);
  472. void tracing_sched_wakeup_trace(struct trace_array *tr,
  473. struct task_struct *wakee,
  474. struct task_struct *cur,
  475. unsigned long flags, int pc);
  476. void trace_function(struct trace_array *tr,
  477. unsigned long ip,
  478. unsigned long parent_ip,
  479. unsigned long flags, int pc);
  480. void trace_graph_function(struct trace_array *tr,
  481. unsigned long ip,
  482. unsigned long parent_ip,
  483. unsigned long flags, int pc);
  484. void trace_latency_header(struct seq_file *m);
  485. void trace_default_header(struct seq_file *m);
  486. void print_trace_header(struct seq_file *m, struct trace_iterator *iter);
  487. int trace_empty(struct trace_iterator *iter);
  488. void trace_graph_return(struct ftrace_graph_ret *trace);
  489. int trace_graph_entry(struct ftrace_graph_ent *trace);
  490. void set_graph_array(struct trace_array *tr);
  491. void tracing_start_cmdline_record(void);
  492. void tracing_stop_cmdline_record(void);
  493. void tracing_sched_switch_assign_trace(struct trace_array *tr);
  494. void tracing_stop_sched_switch_record(void);
  495. void tracing_start_sched_switch_record(void);
  496. int register_tracer(struct tracer *type);
  497. int is_tracing_stopped(void);
  498. extern cpumask_var_t __read_mostly tracing_buffer_mask;
  499. #define for_each_tracing_cpu(cpu) \
  500. for_each_cpu(cpu, tracing_buffer_mask)
  501. extern unsigned long nsecs_to_usecs(unsigned long nsecs);
  502. extern unsigned long tracing_thresh;
  503. #ifdef CONFIG_TRACER_MAX_TRACE
  504. extern unsigned long tracing_max_latency;
  505. void update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu);
  506. void update_max_tr_single(struct trace_array *tr,
  507. struct task_struct *tsk, int cpu);
  508. #endif /* CONFIG_TRACER_MAX_TRACE */
  509. #ifdef CONFIG_STACKTRACE
  510. void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
  511. int skip, int pc);
  512. void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
  513. int skip, int pc, struct pt_regs *regs);
  514. void ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags,
  515. int pc);
  516. void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
  517. int pc);
  518. #else
  519. static inline void ftrace_trace_stack(struct ring_buffer *buffer,
  520. unsigned long flags, int skip, int pc)
  521. {
  522. }
  523. static inline void ftrace_trace_stack_regs(struct ring_buffer *buffer,
  524. unsigned long flags, int skip,
  525. int pc, struct pt_regs *regs)
  526. {
  527. }
  528. static inline void ftrace_trace_userstack(struct ring_buffer *buffer,
  529. unsigned long flags, int pc)
  530. {
  531. }
  532. static inline void __trace_stack(struct trace_array *tr, unsigned long flags,
  533. int skip, int pc)
  534. {
  535. }
  536. #endif /* CONFIG_STACKTRACE */
  537. extern cycle_t ftrace_now(int cpu);
  538. extern void trace_find_cmdline(int pid, char comm[]);
  539. #ifdef CONFIG_DYNAMIC_FTRACE
  540. extern unsigned long ftrace_update_tot_cnt;
  541. #endif
  542. #define DYN_FTRACE_TEST_NAME trace_selftest_dynamic_test_func
  543. extern int DYN_FTRACE_TEST_NAME(void);
  544. #define DYN_FTRACE_TEST_NAME2 trace_selftest_dynamic_test_func2
  545. extern int DYN_FTRACE_TEST_NAME2(void);
  546. extern bool ring_buffer_expanded;
  547. extern bool tracing_selftest_disabled;
  548. DECLARE_PER_CPU(int, ftrace_cpu_disabled);
  549. #ifdef CONFIG_FTRACE_STARTUP_TEST
  550. extern int trace_selftest_startup_function(struct tracer *trace,
  551. struct trace_array *tr);
  552. extern int trace_selftest_startup_function_graph(struct tracer *trace,
  553. struct trace_array *tr);
  554. extern int trace_selftest_startup_irqsoff(struct tracer *trace,
  555. struct trace_array *tr);
  556. extern int trace_selftest_startup_preemptoff(struct tracer *trace,
  557. struct trace_array *tr);
  558. extern int trace_selftest_startup_preemptirqsoff(struct tracer *trace,
  559. struct trace_array *tr);
  560. extern int trace_selftest_startup_wakeup(struct tracer *trace,
  561. struct trace_array *tr);
  562. extern int trace_selftest_startup_nop(struct tracer *trace,
  563. struct trace_array *tr);
  564. extern int trace_selftest_startup_sched_switch(struct tracer *trace,
  565. struct trace_array *tr);
  566. extern int trace_selftest_startup_branch(struct tracer *trace,
  567. struct trace_array *tr);
  568. /*
  569. * Tracer data references selftest functions that only occur
  570. * on boot up. These can be __init functions. Thus, when selftests
  571. * are enabled, then the tracers need to reference __init functions.
  572. */
  573. #define __tracer_data __refdata
  574. #else
  575. /* Tracers are seldom changed. Optimize when selftests are disabled. */
  576. #define __tracer_data __read_mostly
  577. #endif /* CONFIG_FTRACE_STARTUP_TEST */
  578. extern void *head_page(struct trace_array_cpu *data);
  579. extern unsigned long long ns2usecs(cycle_t nsec);
  580. extern int
  581. trace_vbprintk(unsigned long ip, const char *fmt, va_list args);
  582. extern int
  583. trace_vprintk(unsigned long ip, const char *fmt, va_list args);
  584. extern int
  585. trace_array_vprintk(struct trace_array *tr,
  586. unsigned long ip, const char *fmt, va_list args);
  587. int trace_array_printk(struct trace_array *tr,
  588. unsigned long ip, const char *fmt, ...);
  589. int trace_array_printk_buf(struct ring_buffer *buffer,
  590. unsigned long ip, const char *fmt, ...);
  591. void trace_printk_seq(struct trace_seq *s);
  592. enum print_line_t print_trace_line(struct trace_iterator *iter);
  593. extern unsigned long trace_flags;
  594. /* Standard output formatting function used for function return traces */
  595. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  596. /* Flag options */
  597. #define TRACE_GRAPH_PRINT_OVERRUN 0x1
  598. #define TRACE_GRAPH_PRINT_CPU 0x2
  599. #define TRACE_GRAPH_PRINT_OVERHEAD 0x4
  600. #define TRACE_GRAPH_PRINT_PROC 0x8
  601. #define TRACE_GRAPH_PRINT_DURATION 0x10
  602. #define TRACE_GRAPH_PRINT_ABS_TIME 0x20
  603. extern enum print_line_t
  604. print_graph_function_flags(struct trace_iterator *iter, u32 flags);
  605. extern void print_graph_headers_flags(struct seq_file *s, u32 flags);
  606. extern enum print_line_t
  607. trace_print_graph_duration(unsigned long long duration, struct trace_seq *s);
  608. extern void graph_trace_open(struct trace_iterator *iter);
  609. extern void graph_trace_close(struct trace_iterator *iter);
  610. extern int __trace_graph_entry(struct trace_array *tr,
  611. struct ftrace_graph_ent *trace,
  612. unsigned long flags, int pc);
  613. extern void __trace_graph_return(struct trace_array *tr,
  614. struct ftrace_graph_ret *trace,
  615. unsigned long flags, int pc);
  616. #ifdef CONFIG_DYNAMIC_FTRACE
  617. /* TODO: make this variable */
  618. #define FTRACE_GRAPH_MAX_FUNCS 32
  619. extern int ftrace_graph_filter_enabled;
  620. extern int ftrace_graph_count;
  621. extern unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS];
  622. static inline int ftrace_graph_addr(unsigned long addr)
  623. {
  624. int i;
  625. if (!ftrace_graph_filter_enabled)
  626. return 1;
  627. for (i = 0; i < ftrace_graph_count; i++) {
  628. if (addr == ftrace_graph_funcs[i]) {
  629. /*
  630. * If no irqs are to be traced, but a set_graph_function
  631. * is set, and called by an interrupt handler, we still
  632. * want to trace it.
  633. */
  634. if (in_irq())
  635. trace_recursion_set(TRACE_IRQ_BIT);
  636. else
  637. trace_recursion_clear(TRACE_IRQ_BIT);
  638. return 1;
  639. }
  640. }
  641. return 0;
  642. }
  643. #else
  644. static inline int ftrace_graph_addr(unsigned long addr)
  645. {
  646. return 1;
  647. }
  648. #endif /* CONFIG_DYNAMIC_FTRACE */
  649. #else /* CONFIG_FUNCTION_GRAPH_TRACER */
  650. static inline enum print_line_t
  651. print_graph_function_flags(struct trace_iterator *iter, u32 flags)
  652. {
  653. return TRACE_TYPE_UNHANDLED;
  654. }
  655. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  656. extern struct list_head ftrace_pids;
  657. #ifdef CONFIG_FUNCTION_TRACER
  658. extern bool ftrace_filter_param __initdata;
  659. static inline int ftrace_trace_task(struct task_struct *task)
  660. {
  661. if (list_empty(&ftrace_pids))
  662. return 1;
  663. return test_tsk_trace_trace(task);
  664. }
  665. extern int ftrace_is_dead(void);
  666. #else
  667. static inline int ftrace_trace_task(struct task_struct *task)
  668. {
  669. return 1;
  670. }
  671. static inline int ftrace_is_dead(void) { return 0; }
  672. #endif
  673. int ftrace_event_is_function(struct ftrace_event_call *call);
  674. /*
  675. * struct trace_parser - servers for reading the user input separated by spaces
  676. * @cont: set if the input is not complete - no final space char was found
  677. * @buffer: holds the parsed user input
  678. * @idx: user input length
  679. * @size: buffer size
  680. */
  681. struct trace_parser {
  682. bool cont;
  683. char *buffer;
  684. unsigned idx;
  685. unsigned size;
  686. };
  687. static inline bool trace_parser_loaded(struct trace_parser *parser)
  688. {
  689. return (parser->idx != 0);
  690. }
  691. static inline bool trace_parser_cont(struct trace_parser *parser)
  692. {
  693. return parser->cont;
  694. }
  695. static inline void trace_parser_clear(struct trace_parser *parser)
  696. {
  697. parser->cont = false;
  698. parser->idx = 0;
  699. }
  700. extern int trace_parser_get_init(struct trace_parser *parser, int size);
  701. extern void trace_parser_put(struct trace_parser *parser);
  702. extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
  703. size_t cnt, loff_t *ppos);
  704. /*
  705. * trace_iterator_flags is an enumeration that defines bit
  706. * positions into trace_flags that controls the output.
  707. *
  708. * NOTE: These bits must match the trace_options array in
  709. * trace.c.
  710. */
  711. enum trace_iterator_flags {
  712. TRACE_ITER_PRINT_PARENT = 0x01,
  713. TRACE_ITER_SYM_OFFSET = 0x02,
  714. TRACE_ITER_SYM_ADDR = 0x04,
  715. TRACE_ITER_VERBOSE = 0x08,
  716. TRACE_ITER_RAW = 0x10,
  717. TRACE_ITER_HEX = 0x20,
  718. TRACE_ITER_BIN = 0x40,
  719. TRACE_ITER_BLOCK = 0x80,
  720. TRACE_ITER_STACKTRACE = 0x100,
  721. TRACE_ITER_PRINTK = 0x200,
  722. TRACE_ITER_PREEMPTONLY = 0x400,
  723. TRACE_ITER_BRANCH = 0x800,
  724. TRACE_ITER_ANNOTATE = 0x1000,
  725. TRACE_ITER_USERSTACKTRACE = 0x2000,
  726. TRACE_ITER_SYM_USEROBJ = 0x4000,
  727. TRACE_ITER_PRINTK_MSGONLY = 0x8000,
  728. TRACE_ITER_CONTEXT_INFO = 0x10000, /* Print pid/cpu/time */
  729. TRACE_ITER_LATENCY_FMT = 0x20000,
  730. TRACE_ITER_SLEEP_TIME = 0x40000,
  731. TRACE_ITER_GRAPH_TIME = 0x80000,
  732. TRACE_ITER_RECORD_CMD = 0x100000,
  733. TRACE_ITER_OVERWRITE = 0x200000,
  734. TRACE_ITER_STOP_ON_FREE = 0x400000,
  735. TRACE_ITER_IRQ_INFO = 0x800000,
  736. TRACE_ITER_MARKERS = 0x1000000,
  737. TRACE_ITER_FUNCTION = 0x2000000,
  738. };
  739. /*
  740. * TRACE_ITER_SYM_MASK masks the options in trace_flags that
  741. * control the output of kernel symbols.
  742. */
  743. #define TRACE_ITER_SYM_MASK \
  744. (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
  745. extern struct tracer nop_trace;
  746. #ifdef CONFIG_BRANCH_TRACER
  747. extern int enable_branch_tracing(struct trace_array *tr);
  748. extern void disable_branch_tracing(void);
  749. static inline int trace_branch_enable(struct trace_array *tr)
  750. {
  751. if (trace_flags & TRACE_ITER_BRANCH)
  752. return enable_branch_tracing(tr);
  753. return 0;
  754. }
  755. static inline void trace_branch_disable(void)
  756. {
  757. /* due to races, always disable */
  758. disable_branch_tracing();
  759. }
  760. #else
  761. static inline int trace_branch_enable(struct trace_array *tr)
  762. {
  763. return 0;
  764. }
  765. static inline void trace_branch_disable(void)
  766. {
  767. }
  768. #endif /* CONFIG_BRANCH_TRACER */
  769. /* set ring buffers to default size if not already done so */
  770. int tracing_update_buffers(void);
  771. struct ftrace_event_field {
  772. struct list_head link;
  773. const char *name;
  774. const char *type;
  775. int filter_type;
  776. int offset;
  777. int size;
  778. int is_signed;
  779. };
  780. struct event_filter {
  781. int n_preds; /* Number assigned */
  782. int a_preds; /* allocated */
  783. struct filter_pred *preds;
  784. struct filter_pred *root;
  785. char *filter_string;
  786. };
  787. struct event_subsystem {
  788. struct list_head list;
  789. const char *name;
  790. struct event_filter *filter;
  791. int ref_count;
  792. };
  793. struct ftrace_subsystem_dir {
  794. struct list_head list;
  795. struct event_subsystem *subsystem;
  796. struct trace_array *tr;
  797. struct dentry *entry;
  798. int ref_count;
  799. int nr_events;
  800. };
  801. #define FILTER_PRED_INVALID ((unsigned short)-1)
  802. #define FILTER_PRED_IS_RIGHT (1 << 15)
  803. #define FILTER_PRED_FOLD (1 << 15)
  804. /*
  805. * The max preds is the size of unsigned short with
  806. * two flags at the MSBs. One bit is used for both the IS_RIGHT
  807. * and FOLD flags. The other is reserved.
  808. *
  809. * 2^14 preds is way more than enough.
  810. */
  811. #define MAX_FILTER_PRED 16384
  812. struct filter_pred;
  813. struct regex;
  814. typedef int (*filter_pred_fn_t) (struct filter_pred *pred, void *event);
  815. typedef int (*regex_match_func)(char *str, struct regex *r, int len);
  816. enum regex_type {
  817. MATCH_FULL = 0,
  818. MATCH_FRONT_ONLY,
  819. MATCH_MIDDLE_ONLY,
  820. MATCH_END_ONLY,
  821. };
  822. struct regex {
  823. char pattern[MAX_FILTER_STR_VAL];
  824. int len;
  825. int field_len;
  826. regex_match_func match;
  827. };
  828. struct filter_pred {
  829. filter_pred_fn_t fn;
  830. u64 val;
  831. struct regex regex;
  832. unsigned short *ops;
  833. struct ftrace_event_field *field;
  834. int offset;
  835. int not;
  836. int op;
  837. unsigned short index;
  838. unsigned short parent;
  839. unsigned short left;
  840. unsigned short right;
  841. };
  842. extern enum regex_type
  843. filter_parse_regex(char *buff, int len, char **search, int *not);
  844. extern void print_event_filter(struct ftrace_event_call *call,
  845. struct trace_seq *s);
  846. extern int apply_event_filter(struct ftrace_event_call *call,
  847. char *filter_string);
  848. extern int apply_subsystem_event_filter(struct ftrace_subsystem_dir *dir,
  849. char *filter_string);
  850. extern void print_subsystem_event_filter(struct event_subsystem *system,
  851. struct trace_seq *s);
  852. extern int filter_assign_type(const char *type);
  853. struct ftrace_event_field *
  854. trace_find_event_field(struct ftrace_event_call *call, char *name);
  855. static inline int
  856. filter_check_discard(struct ftrace_event_call *call, void *rec,
  857. struct ring_buffer *buffer,
  858. struct ring_buffer_event *event)
  859. {
  860. if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
  861. !filter_match_preds(call->filter, rec)) {
  862. ring_buffer_discard_commit(buffer, event);
  863. return 1;
  864. }
  865. return 0;
  866. }
  867. extern void trace_event_enable_cmd_record(bool enable);
  868. extern int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr);
  869. extern int event_trace_del_tracer(struct trace_array *tr);
  870. extern struct mutex event_mutex;
  871. extern struct list_head ftrace_events;
  872. extern const char *__start___trace_bprintk_fmt[];
  873. extern const char *__stop___trace_bprintk_fmt[];
  874. extern const char *__start___tracepoint_str[];
  875. extern const char *__stop___tracepoint_str[];
  876. void trace_printk_init_buffers(void);
  877. void trace_printk_start_comm(void);
  878. int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set);
  879. int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled);
  880. /*
  881. * Normal trace_printk() and friends allocates special buffers
  882. * to do the manipulation, as well as saves the print formats
  883. * into sections to display. But the trace infrastructure wants
  884. * to use these without the added overhead at the price of being
  885. * a bit slower (used mainly for warnings, where we don't care
  886. * about performance). The internal_trace_puts() is for such
  887. * a purpose.
  888. */
  889. #define internal_trace_puts(str) __trace_puts(_THIS_IP_, str, strlen(str))
  890. #undef FTRACE_ENTRY
  891. #define FTRACE_ENTRY(call, struct_name, id, tstruct, print, filter) \
  892. extern struct ftrace_event_call \
  893. __attribute__((__aligned__(4))) event_##call;
  894. #undef FTRACE_ENTRY_DUP
  895. #define FTRACE_ENTRY_DUP(call, struct_name, id, tstruct, print, filter) \
  896. FTRACE_ENTRY(call, struct_name, id, PARAMS(tstruct), PARAMS(print), \
  897. filter)
  898. #include "trace_entries.h"
  899. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_FUNCTION_TRACER)
  900. int perf_ftrace_event_register(struct ftrace_event_call *call,
  901. enum trace_reg type, void *data);
  902. #else
  903. #define perf_ftrace_event_register NULL
  904. #endif
  905. #endif /* _LINUX_KERNEL_TRACE_H */