trace.h 16 KB

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