trace.h 30 KB

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