trace.h 16 KB

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