trace.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #ifndef _LINUX_KERNEL_TRACE_H
  2. #define _LINUX_KERNEL_TRACE_H
  3. #include <linux/fs.h>
  4. #include <asm/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/ftrace.h>
  10. enum trace_type {
  11. __TRACE_FIRST_TYPE = 0,
  12. TRACE_FN,
  13. TRACE_CTX,
  14. TRACE_WAKE,
  15. TRACE_CONT,
  16. TRACE_STACK,
  17. TRACE_PRINT,
  18. TRACE_SPECIAL,
  19. TRACE_MMIO_RW,
  20. TRACE_MMIO_MAP,
  21. TRACE_BOOT,
  22. __TRACE_LAST_TYPE
  23. };
  24. /*
  25. * The trace entry - the most basic unit of tracing. This is what
  26. * is printed in the end as a single line in the trace output, such as:
  27. *
  28. * bash-15816 [01] 235.197585: idle_cpu <- irq_enter
  29. */
  30. struct trace_entry {
  31. unsigned char type;
  32. unsigned char cpu;
  33. unsigned char flags;
  34. unsigned char preempt_count;
  35. int pid;
  36. };
  37. /*
  38. * Function trace entry - function address and parent function addres:
  39. */
  40. struct ftrace_entry {
  41. struct trace_entry ent;
  42. unsigned long ip;
  43. unsigned long parent_ip;
  44. };
  45. extern struct tracer boot_tracer;
  46. /*
  47. * Context switch trace entry - which task (and prio) we switched from/to:
  48. */
  49. struct ctx_switch_entry {
  50. struct trace_entry ent;
  51. unsigned int prev_pid;
  52. unsigned char prev_prio;
  53. unsigned char prev_state;
  54. unsigned int next_pid;
  55. unsigned char next_prio;
  56. unsigned char next_state;
  57. unsigned int next_cpu;
  58. };
  59. /*
  60. * Special (free-form) trace entry:
  61. */
  62. struct special_entry {
  63. struct trace_entry ent;
  64. unsigned long arg1;
  65. unsigned long arg2;
  66. unsigned long arg3;
  67. };
  68. /*
  69. * Stack-trace entry:
  70. */
  71. #define FTRACE_STACK_ENTRIES 8
  72. struct stack_entry {
  73. struct trace_entry ent;
  74. unsigned long caller[FTRACE_STACK_ENTRIES];
  75. };
  76. /*
  77. * ftrace_printk entry:
  78. */
  79. struct print_entry {
  80. struct trace_entry ent;
  81. unsigned long ip;
  82. char buf[];
  83. };
  84. #define TRACE_OLD_SIZE 88
  85. struct trace_field_cont {
  86. unsigned char type;
  87. /* Temporary till we get rid of this completely */
  88. char buf[TRACE_OLD_SIZE - 1];
  89. };
  90. struct trace_mmiotrace_rw {
  91. struct trace_entry ent;
  92. struct mmiotrace_rw rw;
  93. };
  94. struct trace_mmiotrace_map {
  95. struct trace_entry ent;
  96. struct mmiotrace_map map;
  97. };
  98. struct trace_boot {
  99. struct trace_entry ent;
  100. struct boot_trace initcall;
  101. };
  102. /*
  103. * trace_flag_type is an enumeration that holds different
  104. * states when a trace occurs. These are:
  105. * IRQS_OFF - interrupts were disabled
  106. * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
  107. * NEED_RESCED - reschedule is requested
  108. * HARDIRQ - inside an interrupt handler
  109. * SOFTIRQ - inside a softirq handler
  110. * CONT - multiple entries hold the trace item
  111. */
  112. enum trace_flag_type {
  113. TRACE_FLAG_IRQS_OFF = 0x01,
  114. TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
  115. TRACE_FLAG_NEED_RESCHED = 0x04,
  116. TRACE_FLAG_HARDIRQ = 0x08,
  117. TRACE_FLAG_SOFTIRQ = 0x10,
  118. TRACE_FLAG_CONT = 0x20,
  119. };
  120. #define TRACE_BUF_SIZE 1024
  121. /*
  122. * The CPU trace array - it consists of thousands of trace entries
  123. * plus some other descriptor data: (for example which task started
  124. * the trace, etc.)
  125. */
  126. struct trace_array_cpu {
  127. atomic_t disabled;
  128. /* these fields get copied into max-trace: */
  129. unsigned long trace_idx;
  130. unsigned long overrun;
  131. unsigned long saved_latency;
  132. unsigned long critical_start;
  133. unsigned long critical_end;
  134. unsigned long critical_sequence;
  135. unsigned long nice;
  136. unsigned long policy;
  137. unsigned long rt_priority;
  138. cycle_t preempt_timestamp;
  139. pid_t pid;
  140. uid_t uid;
  141. char comm[TASK_COMM_LEN];
  142. };
  143. struct trace_iterator;
  144. /*
  145. * The trace array - an array of per-CPU trace arrays. This is the
  146. * highest level data structure that individual tracers deal with.
  147. * They have on/off state as well:
  148. */
  149. struct trace_array {
  150. struct ring_buffer *buffer;
  151. unsigned long entries;
  152. long ctrl;
  153. int cpu;
  154. cycle_t time_start;
  155. struct task_struct *waiter;
  156. struct trace_array_cpu *data[NR_CPUS];
  157. };
  158. #define FTRACE_CMP_TYPE(var, type) \
  159. __builtin_types_compatible_p(typeof(var), type *)
  160. #undef IF_ASSIGN
  161. #define IF_ASSIGN(var, entry, etype, id) \
  162. if (FTRACE_CMP_TYPE(var, etype)) { \
  163. var = (typeof(var))(entry); \
  164. WARN_ON(id && (entry)->type != id); \
  165. break; \
  166. }
  167. /* Will cause compile errors if type is not found. */
  168. extern void __ftrace_bad_type(void);
  169. /*
  170. * The trace_assign_type is a verifier that the entry type is
  171. * the same as the type being assigned. To add new types simply
  172. * add a line with the following format:
  173. *
  174. * IF_ASSIGN(var, ent, type, id);
  175. *
  176. * Where "type" is the trace type that includes the trace_entry
  177. * as the "ent" item. And "id" is the trace identifier that is
  178. * used in the trace_type enum.
  179. *
  180. * If the type can have more than one id, then use zero.
  181. */
  182. #define trace_assign_type(var, ent) \
  183. do { \
  184. IF_ASSIGN(var, ent, struct ftrace_entry, TRACE_FN); \
  185. IF_ASSIGN(var, ent, struct ctx_switch_entry, 0); \
  186. IF_ASSIGN(var, ent, struct trace_field_cont, TRACE_CONT); \
  187. IF_ASSIGN(var, ent, struct stack_entry, TRACE_STACK); \
  188. IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT); \
  189. IF_ASSIGN(var, ent, struct special_entry, 0); \
  190. IF_ASSIGN(var, ent, struct trace_mmiotrace_rw, \
  191. TRACE_MMIO_RW); \
  192. IF_ASSIGN(var, ent, struct trace_mmiotrace_map, \
  193. TRACE_MMIO_MAP); \
  194. IF_ASSIGN(var, ent, struct trace_boot, TRACE_BOOT); \
  195. __ftrace_bad_type(); \
  196. } while (0)
  197. /* Return values for print_line callback */
  198. enum print_line_t {
  199. TRACE_TYPE_PARTIAL_LINE = 0, /* Retry after flushing the seq */
  200. TRACE_TYPE_HANDLED = 1,
  201. TRACE_TYPE_UNHANDLED = 2 /* Relay to other output functions */
  202. };
  203. /*
  204. * A specific tracer, represented by methods that operate on a trace array:
  205. */
  206. struct tracer {
  207. const char *name;
  208. void (*init)(struct trace_array *tr);
  209. void (*reset)(struct trace_array *tr);
  210. void (*start)(struct trace_array *tr);
  211. void (*stop)(struct trace_array *tr);
  212. void (*open)(struct trace_iterator *iter);
  213. void (*pipe_open)(struct trace_iterator *iter);
  214. void (*close)(struct trace_iterator *iter);
  215. ssize_t (*read)(struct trace_iterator *iter,
  216. struct file *filp, char __user *ubuf,
  217. size_t cnt, loff_t *ppos);
  218. #ifdef CONFIG_FTRACE_STARTUP_TEST
  219. int (*selftest)(struct tracer *trace,
  220. struct trace_array *tr);
  221. #endif
  222. enum print_line_t (*print_line)(struct trace_iterator *iter);
  223. struct tracer *next;
  224. int print_max;
  225. };
  226. struct trace_seq {
  227. unsigned char buffer[PAGE_SIZE];
  228. unsigned int len;
  229. unsigned int readpos;
  230. };
  231. /*
  232. * Trace iterator - used by printout routines who present trace
  233. * results to users and which routines might sleep, etc:
  234. */
  235. struct trace_iterator {
  236. struct trace_array *tr;
  237. struct tracer *trace;
  238. void *private;
  239. struct ring_buffer_iter *buffer_iter[NR_CPUS];
  240. /* The below is zeroed out in pipe_read */
  241. struct trace_seq seq;
  242. struct trace_entry *ent;
  243. int cpu;
  244. u64 ts;
  245. unsigned long iter_flags;
  246. loff_t pos;
  247. long idx;
  248. };
  249. int tracing_is_enabled(void);
  250. void trace_wake_up(void);
  251. void tracing_reset(struct trace_array *tr, int cpu);
  252. int tracing_open_generic(struct inode *inode, struct file *filp);
  253. struct dentry *tracing_init_dentry(void);
  254. void init_tracer_sysprof_debugfs(struct dentry *d_tracer);
  255. struct trace_entry *tracing_get_trace_entry(struct trace_array *tr,
  256. struct trace_array_cpu *data);
  257. void tracing_generic_entry_update(struct trace_entry *entry,
  258. unsigned long flags,
  259. int pc);
  260. void ftrace(struct trace_array *tr,
  261. struct trace_array_cpu *data,
  262. unsigned long ip,
  263. unsigned long parent_ip,
  264. unsigned long flags, int pc);
  265. void tracing_sched_switch_trace(struct trace_array *tr,
  266. struct trace_array_cpu *data,
  267. struct task_struct *prev,
  268. struct task_struct *next,
  269. unsigned long flags, int pc);
  270. void tracing_record_cmdline(struct task_struct *tsk);
  271. void tracing_sched_wakeup_trace(struct trace_array *tr,
  272. struct trace_array_cpu *data,
  273. struct task_struct *wakee,
  274. struct task_struct *cur,
  275. unsigned long flags, int pc);
  276. void trace_special(struct trace_array *tr,
  277. struct trace_array_cpu *data,
  278. unsigned long arg1,
  279. unsigned long arg2,
  280. unsigned long arg3, int pc);
  281. void trace_function(struct trace_array *tr,
  282. struct trace_array_cpu *data,
  283. unsigned long ip,
  284. unsigned long parent_ip,
  285. unsigned long flags, int pc);
  286. void tracing_start_cmdline_record(void);
  287. void tracing_stop_cmdline_record(void);
  288. void tracing_sched_switch_assign_trace(struct trace_array *tr);
  289. void tracing_stop_sched_switch_record(void);
  290. void tracing_start_sched_switch_record(void);
  291. int register_tracer(struct tracer *type);
  292. void unregister_tracer(struct tracer *type);
  293. extern unsigned long nsecs_to_usecs(unsigned long nsecs);
  294. extern unsigned long tracing_max_latency;
  295. extern unsigned long tracing_thresh;
  296. void update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu);
  297. void update_max_tr_single(struct trace_array *tr,
  298. struct task_struct *tsk, int cpu);
  299. extern cycle_t ftrace_now(int cpu);
  300. #ifdef CONFIG_FUNCTION_TRACER
  301. void tracing_start_function_trace(void);
  302. void tracing_stop_function_trace(void);
  303. #else
  304. # define tracing_start_function_trace() do { } while (0)
  305. # define tracing_stop_function_trace() do { } while (0)
  306. #endif
  307. #ifdef CONFIG_CONTEXT_SWITCH_TRACER
  308. typedef void
  309. (*tracer_switch_func_t)(void *private,
  310. void *__rq,
  311. struct task_struct *prev,
  312. struct task_struct *next);
  313. struct tracer_switch_ops {
  314. tracer_switch_func_t func;
  315. void *private;
  316. struct tracer_switch_ops *next;
  317. };
  318. #endif /* CONFIG_CONTEXT_SWITCH_TRACER */
  319. #ifdef CONFIG_DYNAMIC_FTRACE
  320. extern unsigned long ftrace_update_tot_cnt;
  321. #define DYN_FTRACE_TEST_NAME trace_selftest_dynamic_test_func
  322. extern int DYN_FTRACE_TEST_NAME(void);
  323. #endif
  324. #ifdef CONFIG_FTRACE_STARTUP_TEST
  325. extern int trace_selftest_startup_function(struct tracer *trace,
  326. struct trace_array *tr);
  327. extern int trace_selftest_startup_irqsoff(struct tracer *trace,
  328. struct trace_array *tr);
  329. extern int trace_selftest_startup_preemptoff(struct tracer *trace,
  330. struct trace_array *tr);
  331. extern int trace_selftest_startup_preemptirqsoff(struct tracer *trace,
  332. struct trace_array *tr);
  333. extern int trace_selftest_startup_wakeup(struct tracer *trace,
  334. struct trace_array *tr);
  335. extern int trace_selftest_startup_nop(struct tracer *trace,
  336. struct trace_array *tr);
  337. extern int trace_selftest_startup_sched_switch(struct tracer *trace,
  338. struct trace_array *tr);
  339. extern int trace_selftest_startup_sysprof(struct tracer *trace,
  340. struct trace_array *tr);
  341. #endif /* CONFIG_FTRACE_STARTUP_TEST */
  342. extern void *head_page(struct trace_array_cpu *data);
  343. extern int trace_seq_printf(struct trace_seq *s, const char *fmt, ...);
  344. extern void trace_seq_print_cont(struct trace_seq *s,
  345. struct trace_iterator *iter);
  346. extern ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf,
  347. size_t cnt);
  348. extern long ns2usecs(cycle_t nsec);
  349. extern int trace_vprintk(unsigned long ip, const char *fmt, va_list args);
  350. extern unsigned long trace_flags;
  351. /*
  352. * trace_iterator_flags is an enumeration that defines bit
  353. * positions into trace_flags that controls the output.
  354. *
  355. * NOTE: These bits must match the trace_options array in
  356. * trace.c.
  357. */
  358. enum trace_iterator_flags {
  359. TRACE_ITER_PRINT_PARENT = 0x01,
  360. TRACE_ITER_SYM_OFFSET = 0x02,
  361. TRACE_ITER_SYM_ADDR = 0x04,
  362. TRACE_ITER_VERBOSE = 0x08,
  363. TRACE_ITER_RAW = 0x10,
  364. TRACE_ITER_HEX = 0x20,
  365. TRACE_ITER_BIN = 0x40,
  366. TRACE_ITER_BLOCK = 0x80,
  367. TRACE_ITER_STACKTRACE = 0x100,
  368. TRACE_ITER_SCHED_TREE = 0x200,
  369. TRACE_ITER_PRINTK = 0x400,
  370. TRACE_ITER_PREEMPTONLY = 0x800,
  371. };
  372. extern struct tracer nop_trace;
  373. /**
  374. * ftrace_preempt_disable - disable preemption scheduler safe
  375. *
  376. * When tracing can happen inside the scheduler, there exists
  377. * cases that the tracing might happen before the need_resched
  378. * flag is checked. If this happens and the tracer calls
  379. * preempt_enable (after a disable), a schedule might take place
  380. * causing an infinite recursion.
  381. *
  382. * To prevent this, we read the need_recshed flag before
  383. * disabling preemption. When we want to enable preemption we
  384. * check the flag, if it is set, then we call preempt_enable_no_resched.
  385. * Otherwise, we call preempt_enable.
  386. *
  387. * The rational for doing the above is that if need resched is set
  388. * and we have yet to reschedule, we are either in an atomic location
  389. * (where we do not need to check for scheduling) or we are inside
  390. * the scheduler and do not want to resched.
  391. */
  392. static inline int ftrace_preempt_disable(void)
  393. {
  394. int resched;
  395. resched = need_resched();
  396. preempt_disable_notrace();
  397. return resched;
  398. }
  399. /**
  400. * ftrace_preempt_enable - enable preemption scheduler safe
  401. * @resched: the return value from ftrace_preempt_disable
  402. *
  403. * This is a scheduler safe way to enable preemption and not miss
  404. * any preemption checks. The disabled saved the state of preemption.
  405. * If resched is set, then we were either inside an atomic or
  406. * are inside the scheduler (we would have already scheduled
  407. * otherwise). In this case, we do not want to call normal
  408. * preempt_enable, but preempt_enable_no_resched instead.
  409. */
  410. static inline void ftrace_preempt_enable(int resched)
  411. {
  412. if (resched)
  413. preempt_enable_no_resched_notrace();
  414. else
  415. preempt_enable_notrace();
  416. }
  417. #endif /* _LINUX_KERNEL_TRACE_H */