trace.h 16 KB

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