trace.h 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. #ifndef _LINUX_KERNEL_TRACE_H
  2. #define _LINUX_KERNEL_TRACE_H
  3. #include <linux/fs.h>
  4. #include <linux/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/tracepoint.h>
  10. #include <linux/ftrace.h>
  11. #include <linux/hw_breakpoint.h>
  12. #include <linux/trace_seq.h>
  13. #include <linux/ftrace_event.h>
  14. #ifdef CONFIG_FTRACE_SYSCALLS
  15. #include <asm/unistd.h> /* For NR_SYSCALLS */
  16. #include <asm/syscall.h> /* some archs define it here */
  17. #endif
  18. enum trace_type {
  19. __TRACE_FIRST_TYPE = 0,
  20. TRACE_FN,
  21. TRACE_CTX,
  22. TRACE_WAKE,
  23. TRACE_STACK,
  24. TRACE_PRINT,
  25. TRACE_BPRINT,
  26. TRACE_MMIO_RW,
  27. TRACE_MMIO_MAP,
  28. TRACE_BRANCH,
  29. TRACE_GRAPH_RET,
  30. TRACE_GRAPH_ENT,
  31. TRACE_USER_STACK,
  32. TRACE_BLK,
  33. TRACE_BPUTS,
  34. __TRACE_LAST_TYPE,
  35. };
  36. #undef __field
  37. #define __field(type, item) type item;
  38. #undef __field_struct
  39. #define __field_struct(type, item) __field(type, item)
  40. #undef __field_desc
  41. #define __field_desc(type, container, item)
  42. #undef __array
  43. #define __array(type, item, size) type item[size];
  44. #undef __array_desc
  45. #define __array_desc(type, container, item, size)
  46. #undef __dynamic_array
  47. #define __dynamic_array(type, item) type item[];
  48. #undef F_STRUCT
  49. #define F_STRUCT(args...) args
  50. #undef FTRACE_ENTRY
  51. #define FTRACE_ENTRY(name, struct_name, id, tstruct, print, filter) \
  52. struct struct_name { \
  53. struct trace_entry ent; \
  54. tstruct \
  55. }
  56. #undef TP_ARGS
  57. #define TP_ARGS(args...) args
  58. #undef FTRACE_ENTRY_DUP
  59. #define FTRACE_ENTRY_DUP(name, name_struct, id, tstruct, printk, filter)
  60. #undef FTRACE_ENTRY_REG
  61. #define FTRACE_ENTRY_REG(name, struct_name, id, tstruct, print, \
  62. filter, regfn) \
  63. FTRACE_ENTRY(name, struct_name, id, PARAMS(tstruct), PARAMS(print), \
  64. filter)
  65. #include "trace_entries.h"
  66. /*
  67. * syscalls are special, and need special handling, this is why
  68. * they are not included in trace_entries.h
  69. */
  70. struct syscall_trace_enter {
  71. struct trace_entry ent;
  72. int nr;
  73. unsigned long args[];
  74. };
  75. struct syscall_trace_exit {
  76. struct trace_entry ent;
  77. int nr;
  78. long ret;
  79. };
  80. struct kprobe_trace_entry_head {
  81. struct trace_entry ent;
  82. unsigned long ip;
  83. };
  84. struct kretprobe_trace_entry_head {
  85. struct trace_entry ent;
  86. unsigned long func;
  87. unsigned long ret_ip;
  88. };
  89. /*
  90. * trace_flag_type is an enumeration that holds different
  91. * states when a trace occurs. These are:
  92. * IRQS_OFF - interrupts were disabled
  93. * IRQS_NOSUPPORT - arch does not support irqs_disabled_flags
  94. * NEED_RESCHED - reschedule is requested
  95. * HARDIRQ - inside an interrupt handler
  96. * SOFTIRQ - inside a softirq handler
  97. */
  98. enum trace_flag_type {
  99. TRACE_FLAG_IRQS_OFF = 0x01,
  100. TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
  101. TRACE_FLAG_NEED_RESCHED = 0x04,
  102. TRACE_FLAG_HARDIRQ = 0x08,
  103. TRACE_FLAG_SOFTIRQ = 0x10,
  104. };
  105. #define TRACE_BUF_SIZE 1024
  106. struct trace_array;
  107. struct trace_cpu {
  108. struct trace_array *tr;
  109. struct dentry *dir;
  110. int cpu;
  111. };
  112. /*
  113. * The CPU trace array - it consists of thousands of trace entries
  114. * plus some other descriptor data: (for example which task started
  115. * the trace, etc.)
  116. */
  117. struct trace_array_cpu {
  118. struct trace_cpu trace_cpu;
  119. atomic_t disabled;
  120. void *buffer_page; /* ring buffer spare */
  121. unsigned long entries;
  122. unsigned long saved_latency;
  123. unsigned long critical_start;
  124. unsigned long critical_end;
  125. unsigned long critical_sequence;
  126. unsigned long nice;
  127. unsigned long policy;
  128. unsigned long rt_priority;
  129. unsigned long skipped_entries;
  130. cycle_t preempt_timestamp;
  131. pid_t pid;
  132. kuid_t uid;
  133. char comm[TASK_COMM_LEN];
  134. };
  135. struct tracer;
  136. struct trace_buffer {
  137. struct trace_array *tr;
  138. struct ring_buffer *buffer;
  139. struct trace_array_cpu __percpu *data;
  140. cycle_t time_start;
  141. int cpu;
  142. };
  143. /*
  144. * The trace array - an array of per-CPU trace arrays. This is the
  145. * highest level data structure that individual tracers deal with.
  146. * They have on/off state as well:
  147. */
  148. struct trace_array {
  149. struct list_head list;
  150. char *name;
  151. struct trace_buffer trace_buffer;
  152. #ifdef CONFIG_TRACER_MAX_TRACE
  153. /*
  154. * The max_buffer is used to snapshot the trace when a maximum
  155. * latency is reached, or when the user initiates a snapshot.
  156. * Some tracers will use this to store a maximum trace while
  157. * it continues examining live traces.
  158. *
  159. * The buffers for the max_buffer are set up the same as the trace_buffer
  160. * When a snapshot is taken, the buffer of the max_buffer is swapped
  161. * with the buffer of the trace_buffer and the buffers are reset for
  162. * the trace_buffer so the tracing can continue.
  163. */
  164. struct trace_buffer max_buffer;
  165. bool allocated_snapshot;
  166. #endif
  167. int buffer_disabled;
  168. struct trace_cpu trace_cpu; /* place holder */
  169. #ifdef CONFIG_FTRACE_SYSCALLS
  170. int sys_refcount_enter;
  171. int sys_refcount_exit;
  172. DECLARE_BITMAP(enabled_enter_syscalls, NR_syscalls);
  173. DECLARE_BITMAP(enabled_exit_syscalls, NR_syscalls);
  174. #endif
  175. int stop_count;
  176. int clock_id;
  177. struct tracer *current_trace;
  178. unsigned int flags;
  179. raw_spinlock_t start_lock;
  180. struct dentry *dir;
  181. struct dentry *options;
  182. struct dentry *percpu_dir;
  183. struct dentry *event_dir;
  184. struct list_head systems;
  185. struct list_head events;
  186. struct task_struct *waiter;
  187. int ref;
  188. };
  189. enum {
  190. TRACE_ARRAY_FL_GLOBAL = (1 << 0)
  191. };
  192. extern struct list_head ftrace_trace_arrays;
  193. extern struct mutex trace_types_lock;
  194. extern int trace_array_get(struct trace_array *tr);
  195. extern void trace_array_put(struct trace_array *tr);
  196. /*
  197. * The global tracer (top) should be the first trace array added,
  198. * but we check the flag anyway.
  199. */
  200. static inline struct trace_array *top_trace_array(void)
  201. {
  202. struct trace_array *tr;
  203. tr = list_entry(ftrace_trace_arrays.prev,
  204. typeof(*tr), list);
  205. WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
  206. return tr;
  207. }
  208. #define FTRACE_CMP_TYPE(var, type) \
  209. __builtin_types_compatible_p(typeof(var), type *)
  210. #undef IF_ASSIGN
  211. #define IF_ASSIGN(var, entry, etype, id) \
  212. if (FTRACE_CMP_TYPE(var, etype)) { \
  213. var = (typeof(var))(entry); \
  214. WARN_ON(id && (entry)->type != id); \
  215. break; \
  216. }
  217. /* Will cause compile errors if type is not found. */
  218. extern void __ftrace_bad_type(void);
  219. /*
  220. * The trace_assign_type is a verifier that the entry type is
  221. * the same as the type being assigned. To add new types simply
  222. * add a line with the following format:
  223. *
  224. * IF_ASSIGN(var, ent, type, id);
  225. *
  226. * Where "type" is the trace type that includes the trace_entry
  227. * as the "ent" item. And "id" is the trace identifier that is
  228. * used in the trace_type enum.
  229. *
  230. * If the type can have more than one id, then use zero.
  231. */
  232. #define trace_assign_type(var, ent) \
  233. do { \
  234. IF_ASSIGN(var, ent, struct ftrace_entry, TRACE_FN); \
  235. IF_ASSIGN(var, ent, struct ctx_switch_entry, 0); \
  236. IF_ASSIGN(var, ent, struct stack_entry, TRACE_STACK); \
  237. IF_ASSIGN(var, ent, struct userstack_entry, TRACE_USER_STACK);\
  238. IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT); \
  239. IF_ASSIGN(var, ent, struct bprint_entry, TRACE_BPRINT); \
  240. IF_ASSIGN(var, ent, struct bputs_entry, TRACE_BPUTS); \
  241. IF_ASSIGN(var, ent, struct trace_mmiotrace_rw, \
  242. TRACE_MMIO_RW); \
  243. IF_ASSIGN(var, ent, struct trace_mmiotrace_map, \
  244. TRACE_MMIO_MAP); \
  245. IF_ASSIGN(var, ent, struct trace_branch, TRACE_BRANCH); \
  246. IF_ASSIGN(var, ent, struct ftrace_graph_ent_entry, \
  247. TRACE_GRAPH_ENT); \
  248. IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry, \
  249. TRACE_GRAPH_RET); \
  250. __ftrace_bad_type(); \
  251. } while (0)
  252. /*
  253. * An option specific to a tracer. This is a boolean value.
  254. * The bit is the bit index that sets its value on the
  255. * flags value in struct tracer_flags.
  256. */
  257. struct tracer_opt {
  258. const char *name; /* Will appear on the trace_options file */
  259. u32 bit; /* Mask assigned in val field in tracer_flags */
  260. };
  261. /*
  262. * The set of specific options for a tracer. Your tracer
  263. * have to set the initial value of the flags val.
  264. */
  265. struct tracer_flags {
  266. u32 val;
  267. struct tracer_opt *opts;
  268. };
  269. /* Makes more easy to define a tracer opt */
  270. #define TRACER_OPT(s, b) .name = #s, .bit = b
  271. /**
  272. * struct tracer - a specific tracer and its callbacks to interact with debugfs
  273. * @name: the name chosen to select it on the available_tracers file
  274. * @init: called when one switches to this tracer (echo name > current_tracer)
  275. * @reset: called when one switches to another tracer
  276. * @start: called when tracing is unpaused (echo 1 > tracing_enabled)
  277. * @stop: called when tracing is paused (echo 0 > tracing_enabled)
  278. * @open: called when the trace file is opened
  279. * @pipe_open: called when the trace_pipe file is opened
  280. * @wait_pipe: override how the user waits for traces on trace_pipe
  281. * @close: called when the trace file is released
  282. * @pipe_close: called when the trace_pipe file is released
  283. * @read: override the default read callback on trace_pipe
  284. * @splice_read: override the default splice_read callback on trace_pipe
  285. * @selftest: selftest to run on boot (see trace_selftest.c)
  286. * @print_headers: override the first lines that describe your columns
  287. * @print_line: callback that prints a trace
  288. * @set_flag: signals one of your private flags changed (trace_options file)
  289. * @flags: your private flags
  290. */
  291. struct tracer {
  292. const char *name;
  293. int (*init)(struct trace_array *tr);
  294. void (*reset)(struct trace_array *tr);
  295. void (*start)(struct trace_array *tr);
  296. void (*stop)(struct trace_array *tr);
  297. void (*open)(struct trace_iterator *iter);
  298. void (*pipe_open)(struct trace_iterator *iter);
  299. void (*wait_pipe)(struct trace_iterator *iter);
  300. void (*close)(struct trace_iterator *iter);
  301. void (*pipe_close)(struct trace_iterator *iter);
  302. ssize_t (*read)(struct trace_iterator *iter,
  303. struct file *filp, char __user *ubuf,
  304. size_t cnt, loff_t *ppos);
  305. ssize_t (*splice_read)(struct trace_iterator *iter,
  306. struct file *filp,
  307. loff_t *ppos,
  308. struct pipe_inode_info *pipe,
  309. size_t len,
  310. unsigned int flags);
  311. #ifdef CONFIG_FTRACE_STARTUP_TEST
  312. int (*selftest)(struct tracer *trace,
  313. struct trace_array *tr);
  314. #endif
  315. void (*print_header)(struct seq_file *m);
  316. enum print_line_t (*print_line)(struct trace_iterator *iter);
  317. /* If you handled the flag setting, return 0 */
  318. int (*set_flag)(u32 old_flags, u32 bit, int set);
  319. /* Return 0 if OK with change, else return non-zero */
  320. int (*flag_changed)(struct tracer *tracer,
  321. u32 mask, int set);
  322. struct tracer *next;
  323. struct tracer_flags *flags;
  324. bool print_max;
  325. bool enabled;
  326. #ifdef CONFIG_TRACER_MAX_TRACE
  327. bool use_max_tr;
  328. #endif
  329. };
  330. /* Only current can touch trace_recursion */
  331. /*
  332. * For function tracing recursion:
  333. * The order of these bits are important.
  334. *
  335. * When function tracing occurs, the following steps are made:
  336. * If arch does not support a ftrace feature:
  337. * call internal function (uses INTERNAL bits) which calls...
  338. * If callback is registered to the "global" list, the list
  339. * function is called and recursion checks the GLOBAL bits.
  340. * then this function calls...
  341. * The function callback, which can use the FTRACE bits to
  342. * check for recursion.
  343. *
  344. * Now if the arch does not suppport a feature, and it calls
  345. * the global list function which calls the ftrace callback
  346. * all three of these steps will do a recursion protection.
  347. * There's no reason to do one if the previous caller already
  348. * did. The recursion that we are protecting against will
  349. * go through the same steps again.
  350. *
  351. * To prevent the multiple recursion checks, if a recursion
  352. * bit is set that is higher than the MAX bit of the current
  353. * check, then we know that the check was made by the previous
  354. * caller, and we can skip the current check.
  355. */
  356. enum {
  357. TRACE_BUFFER_BIT,
  358. TRACE_BUFFER_NMI_BIT,
  359. TRACE_BUFFER_IRQ_BIT,
  360. TRACE_BUFFER_SIRQ_BIT,
  361. /* Start of function recursion bits */
  362. TRACE_FTRACE_BIT,
  363. TRACE_FTRACE_NMI_BIT,
  364. TRACE_FTRACE_IRQ_BIT,
  365. TRACE_FTRACE_SIRQ_BIT,
  366. /* GLOBAL_BITs must be greater than FTRACE_BITs */
  367. TRACE_GLOBAL_BIT,
  368. TRACE_GLOBAL_NMI_BIT,
  369. TRACE_GLOBAL_IRQ_BIT,
  370. TRACE_GLOBAL_SIRQ_BIT,
  371. /* INTERNAL_BITs must be greater than GLOBAL_BITs */
  372. TRACE_INTERNAL_BIT,
  373. TRACE_INTERNAL_NMI_BIT,
  374. TRACE_INTERNAL_IRQ_BIT,
  375. TRACE_INTERNAL_SIRQ_BIT,
  376. TRACE_CONTROL_BIT,
  377. /*
  378. * Abuse of the trace_recursion.
  379. * As we need a way to maintain state if we are tracing the function
  380. * graph in irq because we want to trace a particular function that
  381. * was called in irq context but we have irq tracing off. Since this
  382. * can only be modified by current, we can reuse trace_recursion.
  383. */
  384. TRACE_IRQ_BIT,
  385. };
  386. #define trace_recursion_set(bit) do { (current)->trace_recursion |= (1<<(bit)); } while (0)
  387. #define trace_recursion_clear(bit) do { (current)->trace_recursion &= ~(1<<(bit)); } while (0)
  388. #define trace_recursion_test(bit) ((current)->trace_recursion & (1<<(bit)))
  389. #define TRACE_CONTEXT_BITS 4
  390. #define TRACE_FTRACE_START TRACE_FTRACE_BIT
  391. #define TRACE_FTRACE_MAX ((1 << (TRACE_FTRACE_START + TRACE_CONTEXT_BITS)) - 1)
  392. #define TRACE_GLOBAL_START TRACE_GLOBAL_BIT
  393. #define TRACE_GLOBAL_MAX ((1 << (TRACE_GLOBAL_START + TRACE_CONTEXT_BITS)) - 1)
  394. #define TRACE_LIST_START TRACE_INTERNAL_BIT
  395. #define TRACE_LIST_MAX ((1 << (TRACE_LIST_START + TRACE_CONTEXT_BITS)) - 1)
  396. #define TRACE_CONTEXT_MASK TRACE_LIST_MAX
  397. static __always_inline int trace_get_context_bit(void)
  398. {
  399. int bit;
  400. if (in_interrupt()) {
  401. if (in_nmi())
  402. bit = 0;
  403. else if (in_irq())
  404. bit = 1;
  405. else
  406. bit = 2;
  407. } else
  408. bit = 3;
  409. return bit;
  410. }
  411. static __always_inline int trace_test_and_set_recursion(int start, int max)
  412. {
  413. unsigned int val = current->trace_recursion;
  414. int bit;
  415. /* A previous recursion check was made */
  416. if ((val & TRACE_CONTEXT_MASK) > max)
  417. return 0;
  418. bit = trace_get_context_bit() + start;
  419. if (unlikely(val & (1 << bit)))
  420. return -1;
  421. val |= 1 << bit;
  422. current->trace_recursion = val;
  423. barrier();
  424. return bit;
  425. }
  426. static __always_inline void trace_clear_recursion(int bit)
  427. {
  428. unsigned int val = current->trace_recursion;
  429. if (!bit)
  430. return;
  431. bit = 1 << bit;
  432. val &= ~bit;
  433. barrier();
  434. current->trace_recursion = val;
  435. }
  436. static inline struct ring_buffer_iter *
  437. trace_buffer_iter(struct trace_iterator *iter, int cpu)
  438. {
  439. if (iter->buffer_iter && iter->buffer_iter[cpu])
  440. return iter->buffer_iter[cpu];
  441. return NULL;
  442. }
  443. int tracer_init(struct tracer *t, struct trace_array *tr);
  444. int tracing_is_enabled(void);
  445. void tracing_reset(struct trace_buffer *buf, int cpu);
  446. void tracing_reset_online_cpus(struct trace_buffer *buf);
  447. void tracing_reset_current(int cpu);
  448. void tracing_reset_all_online_cpus(void);
  449. int tracing_open_generic(struct inode *inode, struct file *filp);
  450. struct dentry *trace_create_file(const char *name,
  451. umode_t mode,
  452. struct dentry *parent,
  453. void *data,
  454. const struct file_operations *fops);
  455. struct dentry *tracing_init_dentry_tr(struct trace_array *tr);
  456. struct dentry *tracing_init_dentry(void);
  457. struct ring_buffer_event;
  458. struct ring_buffer_event *
  459. trace_buffer_lock_reserve(struct ring_buffer *buffer,
  460. int type,
  461. unsigned long len,
  462. unsigned long flags,
  463. int pc);
  464. struct trace_entry *tracing_get_trace_entry(struct trace_array *tr,
  465. struct trace_array_cpu *data);
  466. struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
  467. int *ent_cpu, u64 *ent_ts);
  468. void __buffer_unlock_commit(struct ring_buffer *buffer,
  469. struct ring_buffer_event *event);
  470. int trace_empty(struct trace_iterator *iter);
  471. void *trace_find_next_entry_inc(struct trace_iterator *iter);
  472. void trace_init_global_iter(struct trace_iterator *iter);
  473. void tracing_iter_reset(struct trace_iterator *iter, int cpu);
  474. void poll_wait_pipe(struct trace_iterator *iter);
  475. void tracing_sched_switch_trace(struct trace_array *tr,
  476. struct task_struct *prev,
  477. struct task_struct *next,
  478. unsigned long flags, int pc);
  479. void tracing_sched_wakeup_trace(struct trace_array *tr,
  480. struct task_struct *wakee,
  481. struct task_struct *cur,
  482. unsigned long flags, int pc);
  483. void trace_function(struct trace_array *tr,
  484. unsigned long ip,
  485. unsigned long parent_ip,
  486. unsigned long flags, int pc);
  487. void trace_graph_function(struct trace_array *tr,
  488. unsigned long ip,
  489. unsigned long parent_ip,
  490. unsigned long flags, int pc);
  491. void trace_latency_header(struct seq_file *m);
  492. void trace_default_header(struct seq_file *m);
  493. void print_trace_header(struct seq_file *m, struct trace_iterator *iter);
  494. int trace_empty(struct trace_iterator *iter);
  495. void trace_graph_return(struct ftrace_graph_ret *trace);
  496. int trace_graph_entry(struct ftrace_graph_ent *trace);
  497. void set_graph_array(struct trace_array *tr);
  498. void tracing_start_cmdline_record(void);
  499. void tracing_stop_cmdline_record(void);
  500. void tracing_sched_switch_assign_trace(struct trace_array *tr);
  501. void tracing_stop_sched_switch_record(void);
  502. void tracing_start_sched_switch_record(void);
  503. int register_tracer(struct tracer *type);
  504. int is_tracing_stopped(void);
  505. extern cpumask_var_t __read_mostly tracing_buffer_mask;
  506. #define for_each_tracing_cpu(cpu) \
  507. for_each_cpu(cpu, tracing_buffer_mask)
  508. extern unsigned long nsecs_to_usecs(unsigned long nsecs);
  509. extern unsigned long tracing_thresh;
  510. #ifdef CONFIG_TRACER_MAX_TRACE
  511. extern unsigned long tracing_max_latency;
  512. void update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu);
  513. void update_max_tr_single(struct trace_array *tr,
  514. struct task_struct *tsk, int cpu);
  515. #endif /* CONFIG_TRACER_MAX_TRACE */
  516. #ifdef CONFIG_STACKTRACE
  517. void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
  518. int skip, int pc);
  519. void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
  520. int skip, int pc, struct pt_regs *regs);
  521. void ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags,
  522. int pc);
  523. void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
  524. int pc);
  525. #else
  526. static inline void ftrace_trace_stack(struct ring_buffer *buffer,
  527. unsigned long flags, int skip, int pc)
  528. {
  529. }
  530. static inline void ftrace_trace_stack_regs(struct ring_buffer *buffer,
  531. unsigned long flags, int skip,
  532. int pc, struct pt_regs *regs)
  533. {
  534. }
  535. static inline void ftrace_trace_userstack(struct ring_buffer *buffer,
  536. unsigned long flags, int pc)
  537. {
  538. }
  539. static inline void __trace_stack(struct trace_array *tr, unsigned long flags,
  540. int skip, int pc)
  541. {
  542. }
  543. #endif /* CONFIG_STACKTRACE */
  544. extern cycle_t ftrace_now(int cpu);
  545. extern void trace_find_cmdline(int pid, char comm[]);
  546. #ifdef CONFIG_DYNAMIC_FTRACE
  547. extern unsigned long ftrace_update_tot_cnt;
  548. #endif
  549. #define DYN_FTRACE_TEST_NAME trace_selftest_dynamic_test_func
  550. extern int DYN_FTRACE_TEST_NAME(void);
  551. #define DYN_FTRACE_TEST_NAME2 trace_selftest_dynamic_test_func2
  552. extern int DYN_FTRACE_TEST_NAME2(void);
  553. extern bool ring_buffer_expanded;
  554. extern bool tracing_selftest_disabled;
  555. DECLARE_PER_CPU(int, ftrace_cpu_disabled);
  556. #ifdef CONFIG_FTRACE_STARTUP_TEST
  557. extern int trace_selftest_startup_function(struct tracer *trace,
  558. struct trace_array *tr);
  559. extern int trace_selftest_startup_function_graph(struct tracer *trace,
  560. struct trace_array *tr);
  561. extern int trace_selftest_startup_irqsoff(struct tracer *trace,
  562. struct trace_array *tr);
  563. extern int trace_selftest_startup_preemptoff(struct tracer *trace,
  564. struct trace_array *tr);
  565. extern int trace_selftest_startup_preemptirqsoff(struct tracer *trace,
  566. struct trace_array *tr);
  567. extern int trace_selftest_startup_wakeup(struct tracer *trace,
  568. struct trace_array *tr);
  569. extern int trace_selftest_startup_nop(struct tracer *trace,
  570. struct trace_array *tr);
  571. extern int trace_selftest_startup_sched_switch(struct tracer *trace,
  572. struct trace_array *tr);
  573. extern int trace_selftest_startup_branch(struct tracer *trace,
  574. struct trace_array *tr);
  575. #endif /* CONFIG_FTRACE_STARTUP_TEST */
  576. extern void *head_page(struct trace_array_cpu *data);
  577. extern unsigned long long ns2usecs(cycle_t nsec);
  578. extern int
  579. trace_vbprintk(unsigned long ip, const char *fmt, va_list args);
  580. extern int
  581. trace_vprintk(unsigned long ip, const char *fmt, va_list args);
  582. extern int
  583. trace_array_vprintk(struct trace_array *tr,
  584. unsigned long ip, const char *fmt, va_list args);
  585. int trace_array_printk(struct trace_array *tr,
  586. unsigned long ip, const char *fmt, ...);
  587. int trace_array_printk_buf(struct ring_buffer *buffer,
  588. unsigned long ip, const char *fmt, ...);
  589. void trace_printk_seq(struct trace_seq *s);
  590. enum print_line_t print_trace_line(struct trace_iterator *iter);
  591. extern unsigned long trace_flags;
  592. /* Standard output formatting function used for function return traces */
  593. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  594. /* Flag options */
  595. #define TRACE_GRAPH_PRINT_OVERRUN 0x1
  596. #define TRACE_GRAPH_PRINT_CPU 0x2
  597. #define TRACE_GRAPH_PRINT_OVERHEAD 0x4
  598. #define TRACE_GRAPH_PRINT_PROC 0x8
  599. #define TRACE_GRAPH_PRINT_DURATION 0x10
  600. #define TRACE_GRAPH_PRINT_ABS_TIME 0x20
  601. extern enum print_line_t
  602. print_graph_function_flags(struct trace_iterator *iter, u32 flags);
  603. extern void print_graph_headers_flags(struct seq_file *s, u32 flags);
  604. extern enum print_line_t
  605. trace_print_graph_duration(unsigned long long duration, struct trace_seq *s);
  606. extern void graph_trace_open(struct trace_iterator *iter);
  607. extern void graph_trace_close(struct trace_iterator *iter);
  608. extern int __trace_graph_entry(struct trace_array *tr,
  609. struct ftrace_graph_ent *trace,
  610. unsigned long flags, int pc);
  611. extern void __trace_graph_return(struct trace_array *tr,
  612. struct ftrace_graph_ret *trace,
  613. unsigned long flags, int pc);
  614. #ifdef CONFIG_DYNAMIC_FTRACE
  615. /* TODO: make this variable */
  616. #define FTRACE_GRAPH_MAX_FUNCS 32
  617. extern int ftrace_graph_filter_enabled;
  618. extern int ftrace_graph_count;
  619. extern unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS];
  620. static inline int ftrace_graph_addr(unsigned long addr)
  621. {
  622. int i;
  623. if (!ftrace_graph_filter_enabled)
  624. return 1;
  625. for (i = 0; i < ftrace_graph_count; i++) {
  626. if (addr == ftrace_graph_funcs[i]) {
  627. /*
  628. * If no irqs are to be traced, but a set_graph_function
  629. * is set, and called by an interrupt handler, we still
  630. * want to trace it.
  631. */
  632. if (in_irq())
  633. trace_recursion_set(TRACE_IRQ_BIT);
  634. else
  635. trace_recursion_clear(TRACE_IRQ_BIT);
  636. return 1;
  637. }
  638. }
  639. return 0;
  640. }
  641. #else
  642. static inline int ftrace_graph_addr(unsigned long addr)
  643. {
  644. return 1;
  645. }
  646. #endif /* CONFIG_DYNAMIC_FTRACE */
  647. #else /* CONFIG_FUNCTION_GRAPH_TRACER */
  648. static inline enum print_line_t
  649. print_graph_function_flags(struct trace_iterator *iter, u32 flags)
  650. {
  651. return TRACE_TYPE_UNHANDLED;
  652. }
  653. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  654. extern struct list_head ftrace_pids;
  655. #ifdef CONFIG_FUNCTION_TRACER
  656. extern bool ftrace_filter_param __initdata;
  657. static inline int ftrace_trace_task(struct task_struct *task)
  658. {
  659. if (list_empty(&ftrace_pids))
  660. return 1;
  661. return test_tsk_trace_trace(task);
  662. }
  663. extern int ftrace_is_dead(void);
  664. #else
  665. static inline int ftrace_trace_task(struct task_struct *task)
  666. {
  667. return 1;
  668. }
  669. static inline int ftrace_is_dead(void) { return 0; }
  670. #endif
  671. int ftrace_event_is_function(struct ftrace_event_call *call);
  672. /*
  673. * struct trace_parser - servers for reading the user input separated by spaces
  674. * @cont: set if the input is not complete - no final space char was found
  675. * @buffer: holds the parsed user input
  676. * @idx: user input length
  677. * @size: buffer size
  678. */
  679. struct trace_parser {
  680. bool cont;
  681. char *buffer;
  682. unsigned idx;
  683. unsigned size;
  684. };
  685. static inline bool trace_parser_loaded(struct trace_parser *parser)
  686. {
  687. return (parser->idx != 0);
  688. }
  689. static inline bool trace_parser_cont(struct trace_parser *parser)
  690. {
  691. return parser->cont;
  692. }
  693. static inline void trace_parser_clear(struct trace_parser *parser)
  694. {
  695. parser->cont = false;
  696. parser->idx = 0;
  697. }
  698. extern int trace_parser_get_init(struct trace_parser *parser, int size);
  699. extern void trace_parser_put(struct trace_parser *parser);
  700. extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
  701. size_t cnt, loff_t *ppos);
  702. /*
  703. * trace_iterator_flags is an enumeration that defines bit
  704. * positions into trace_flags that controls the output.
  705. *
  706. * NOTE: These bits must match the trace_options array in
  707. * trace.c.
  708. */
  709. enum trace_iterator_flags {
  710. TRACE_ITER_PRINT_PARENT = 0x01,
  711. TRACE_ITER_SYM_OFFSET = 0x02,
  712. TRACE_ITER_SYM_ADDR = 0x04,
  713. TRACE_ITER_VERBOSE = 0x08,
  714. TRACE_ITER_RAW = 0x10,
  715. TRACE_ITER_HEX = 0x20,
  716. TRACE_ITER_BIN = 0x40,
  717. TRACE_ITER_BLOCK = 0x80,
  718. TRACE_ITER_STACKTRACE = 0x100,
  719. TRACE_ITER_PRINTK = 0x200,
  720. TRACE_ITER_PREEMPTONLY = 0x400,
  721. TRACE_ITER_BRANCH = 0x800,
  722. TRACE_ITER_ANNOTATE = 0x1000,
  723. TRACE_ITER_USERSTACKTRACE = 0x2000,
  724. TRACE_ITER_SYM_USEROBJ = 0x4000,
  725. TRACE_ITER_PRINTK_MSGONLY = 0x8000,
  726. TRACE_ITER_CONTEXT_INFO = 0x10000, /* Print pid/cpu/time */
  727. TRACE_ITER_LATENCY_FMT = 0x20000,
  728. TRACE_ITER_SLEEP_TIME = 0x40000,
  729. TRACE_ITER_GRAPH_TIME = 0x80000,
  730. TRACE_ITER_RECORD_CMD = 0x100000,
  731. TRACE_ITER_OVERWRITE = 0x200000,
  732. TRACE_ITER_STOP_ON_FREE = 0x400000,
  733. TRACE_ITER_IRQ_INFO = 0x800000,
  734. TRACE_ITER_MARKERS = 0x1000000,
  735. TRACE_ITER_FUNCTION = 0x2000000,
  736. };
  737. /*
  738. * TRACE_ITER_SYM_MASK masks the options in trace_flags that
  739. * control the output of kernel symbols.
  740. */
  741. #define TRACE_ITER_SYM_MASK \
  742. (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
  743. extern struct tracer nop_trace;
  744. #ifdef CONFIG_BRANCH_TRACER
  745. extern int enable_branch_tracing(struct trace_array *tr);
  746. extern void disable_branch_tracing(void);
  747. static inline int trace_branch_enable(struct trace_array *tr)
  748. {
  749. if (trace_flags & TRACE_ITER_BRANCH)
  750. return enable_branch_tracing(tr);
  751. return 0;
  752. }
  753. static inline void trace_branch_disable(void)
  754. {
  755. /* due to races, always disable */
  756. disable_branch_tracing();
  757. }
  758. #else
  759. static inline int trace_branch_enable(struct trace_array *tr)
  760. {
  761. return 0;
  762. }
  763. static inline void trace_branch_disable(void)
  764. {
  765. }
  766. #endif /* CONFIG_BRANCH_TRACER */
  767. /* set ring buffers to default size if not already done so */
  768. int tracing_update_buffers(void);
  769. struct ftrace_event_field {
  770. struct list_head link;
  771. const char *name;
  772. const char *type;
  773. int filter_type;
  774. int offset;
  775. int size;
  776. int is_signed;
  777. };
  778. struct event_filter {
  779. int n_preds; /* Number assigned */
  780. int a_preds; /* allocated */
  781. struct filter_pred *preds;
  782. struct filter_pred *root;
  783. char *filter_string;
  784. };
  785. struct event_subsystem {
  786. struct list_head list;
  787. const char *name;
  788. struct event_filter *filter;
  789. int ref_count;
  790. };
  791. struct ftrace_subsystem_dir {
  792. struct list_head list;
  793. struct event_subsystem *subsystem;
  794. struct trace_array *tr;
  795. struct dentry *entry;
  796. int ref_count;
  797. int nr_events;
  798. };
  799. #define FILTER_PRED_INVALID ((unsigned short)-1)
  800. #define FILTER_PRED_IS_RIGHT (1 << 15)
  801. #define FILTER_PRED_FOLD (1 << 15)
  802. /*
  803. * The max preds is the size of unsigned short with
  804. * two flags at the MSBs. One bit is used for both the IS_RIGHT
  805. * and FOLD flags. The other is reserved.
  806. *
  807. * 2^14 preds is way more than enough.
  808. */
  809. #define MAX_FILTER_PRED 16384
  810. struct filter_pred;
  811. struct regex;
  812. typedef int (*filter_pred_fn_t) (struct filter_pred *pred, void *event);
  813. typedef int (*regex_match_func)(char *str, struct regex *r, int len);
  814. enum regex_type {
  815. MATCH_FULL = 0,
  816. MATCH_FRONT_ONLY,
  817. MATCH_MIDDLE_ONLY,
  818. MATCH_END_ONLY,
  819. };
  820. struct regex {
  821. char pattern[MAX_FILTER_STR_VAL];
  822. int len;
  823. int field_len;
  824. regex_match_func match;
  825. };
  826. struct filter_pred {
  827. filter_pred_fn_t fn;
  828. u64 val;
  829. struct regex regex;
  830. unsigned short *ops;
  831. struct ftrace_event_field *field;
  832. int offset;
  833. int not;
  834. int op;
  835. unsigned short index;
  836. unsigned short parent;
  837. unsigned short left;
  838. unsigned short right;
  839. };
  840. extern enum regex_type
  841. filter_parse_regex(char *buff, int len, char **search, int *not);
  842. extern void print_event_filter(struct ftrace_event_call *call,
  843. struct trace_seq *s);
  844. extern int apply_event_filter(struct ftrace_event_call *call,
  845. char *filter_string);
  846. extern int apply_subsystem_event_filter(struct ftrace_subsystem_dir *dir,
  847. char *filter_string);
  848. extern void print_subsystem_event_filter(struct event_subsystem *system,
  849. struct trace_seq *s);
  850. extern int filter_assign_type(const char *type);
  851. struct ftrace_event_field *
  852. trace_find_event_field(struct ftrace_event_call *call, char *name);
  853. static inline int
  854. filter_check_discard(struct ftrace_event_call *call, void *rec,
  855. struct ring_buffer *buffer,
  856. struct ring_buffer_event *event)
  857. {
  858. if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
  859. !filter_match_preds(call->filter, rec)) {
  860. ring_buffer_discard_commit(buffer, event);
  861. return 1;
  862. }
  863. return 0;
  864. }
  865. extern void trace_event_enable_cmd_record(bool enable);
  866. extern int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr);
  867. extern int event_trace_del_tracer(struct trace_array *tr);
  868. extern struct mutex event_mutex;
  869. extern struct list_head ftrace_events;
  870. extern const char *__start___trace_bprintk_fmt[];
  871. extern const char *__stop___trace_bprintk_fmt[];
  872. void trace_printk_init_buffers(void);
  873. void trace_printk_start_comm(void);
  874. int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set);
  875. int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled);
  876. /*
  877. * Normal trace_printk() and friends allocates special buffers
  878. * to do the manipulation, as well as saves the print formats
  879. * into sections to display. But the trace infrastructure wants
  880. * to use these without the added overhead at the price of being
  881. * a bit slower (used mainly for warnings, where we don't care
  882. * about performance). The internal_trace_puts() is for such
  883. * a purpose.
  884. */
  885. #define internal_trace_puts(str) __trace_puts(_THIS_IP_, str, strlen(str))
  886. #undef FTRACE_ENTRY
  887. #define FTRACE_ENTRY(call, struct_name, id, tstruct, print, filter) \
  888. extern struct ftrace_event_call \
  889. __attribute__((__aligned__(4))) event_##call;
  890. #undef FTRACE_ENTRY_DUP
  891. #define FTRACE_ENTRY_DUP(call, struct_name, id, tstruct, print, filter) \
  892. FTRACE_ENTRY(call, struct_name, id, PARAMS(tstruct), PARAMS(print), \
  893. filter)
  894. #include "trace_entries.h"
  895. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_FUNCTION_TRACER)
  896. int perf_ftrace_event_register(struct ftrace_event_call *call,
  897. enum trace_reg type, void *data);
  898. #else
  899. #define perf_ftrace_event_register NULL
  900. #endif
  901. #endif /* _LINUX_KERNEL_TRACE_H */