trace.h 11 KB

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