trace.h 30 KB

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