trace.h 29 KB

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