trace.h 16 KB

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