trace.h 11 KB

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