trace.h 14 KB

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