trace.h 11 KB

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