| /* |
| * ring buffer based function tracer |
| * |
| * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com> |
| * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com> |
| * |
| * Originally taken from the RT patch by: |
| * Arnaldo Carvalho de Melo <acme@redhat.com> |
| * |
| * Based on code from the latency_tracer, that is: |
| * Copyright (C) 2004-2006 Ingo Molnar |
| * Copyright (C) 2004 Nadia Yvette Chambers |
| */ |
| #include <linux/ring_buffer.h> |
| #include <generated/utsrelease.h> |
| #include <linux/stacktrace.h> |
| #include <linux/writeback.h> |
| #include <linux/kallsyms.h> |
| #include <linux/seq_file.h> |
| #include <linux/notifier.h> |
| #include <linux/irqflags.h> |
| #include <linux/debugfs.h> |
| #include <linux/tracefs.h> |
| #include <linux/pagemap.h> |
| #include <linux/hardirq.h> |
| #include <linux/linkage.h> |
| #include <linux/uaccess.h> |
| #include <linux/vmalloc.h> |
| #include <linux/ftrace.h> |
| #include <linux/module.h> |
| #include <linux/percpu.h> |
| #include <linux/splice.h> |
| #include <linux/kdebug.h> |
| #include <linux/string.h> |
| #include <linux/mount.h> |
| #include <linux/rwsem.h> |
| #include <linux/slab.h> |
| #include <linux/ctype.h> |
| #include <linux/init.h> |
| #include <linux/poll.h> |
| #include <linux/nmi.h> |
| #include <linux/fs.h> |
| #include <linux/trace.h> |
| #include <linux/sched/rt.h> |
| |
| #include "trace.h" |
| #include "trace_output.h" |
| |
| /* |
| * On boot up, the ring buffer is set to the minimum size, so that |
| * we do not waste memory on systems that are not using tracing. |
| */ |
| bool ring_buffer_expanded; |
| |
| /* |
| * We need to change this state when a selftest is running. |
| * A selftest will lurk into the ring-buffer to count the |
| * entries inserted during the selftest although some concurrent |
| * insertions into the ring-buffer such as trace_printk could occurred |
| * at the same time, giving false positive or negative results. |
| */ |
| static bool __read_mostly tracing_selftest_running; |
| |
| /* |
| * If a tracer is running, we do not want to run SELFTEST. |
| */ |
| bool __read_mostly tracing_selftest_disabled; |
| |
| /* Pipe tracepoints to printk */ |
| struct trace_iterator *tracepoint_print_iter; |
| int tracepoint_printk; |
| static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key); |
| |
| /* For tracers that don't implement custom flags */ |
| static struct tracer_opt dummy_tracer_opt[] = { |
| { } |
| }; |
| |
| static int |
| dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set) |
| { |
| return 0; |
| } |
| |
| /* |
| * To prevent the comm cache from being overwritten when no |
| * tracing is active, only save the comm when a trace event |
| * occurred. |
| */ |
| static DEFINE_PER_CPU(bool, trace_taskinfo_save); |
| |
| /* |
| * Kill all tracing for good (never come back). |
| * It is initialized to 1 but will turn to zero if the initialization |
| * of the tracer is successful. But that is the only place that sets |
| * this back to zero. |
| */ |
| static int tracing_disabled = 1; |
| |
| cpumask_var_t __read_mostly tracing_buffer_mask; |
| |
| /* |
| * ftrace_dump_on_oops - variable to dump ftrace buffer on oops |
| * |
| * If there is an oops (or kernel panic) and the ftrace_dump_on_oops |
| * is set, then ftrace_dump is called. This will output the contents |
| * of the ftrace buffers to the console. This is very useful for |
| * capturing traces that lead to crashes and outputing it to a |
| * serial console. |
| * |
| * It is default off, but you can enable it with either specifying |
| * "ftrace_dump_on_oops" in the kernel command line, or setting |
| * /proc/sys/kernel/ftrace_dump_on_oops |
| * Set 1 if you want to dump buffers of all CPUs |
| * Set 2 if you want to dump the buffer of the CPU that triggered oops |
| */ |
| |
| enum ftrace_dump_mode ftrace_dump_on_oops; |
| |
| /* When set, tracing will stop when a WARN*() is hit */ |
| int __disable_trace_on_warning; |
| |
| #ifdef CONFIG_TRACE_EVAL_MAP_FILE |
| /* Map of enums to their values, for "eval_map" file */ |
| struct trace_eval_map_head { |
| struct module *mod; |
| unsigned long length; |
| }; |
| |
| union trace_eval_map_item; |
| |
| struct trace_eval_map_tail { |
| /* |
| * "end" is first and points to NULL as it must be different |
| * than "mod" or "eval_string" |
| */ |
| union trace_eval_map_item *next; |
| const char *end; /* points to NULL */ |
| }; |
| |
| static DEFINE_MUTEX(trace_eval_mutex); |
| |
| /* |
| * The trace_eval_maps are saved in an array with two extra elements, |
| * one at the beginning, and one at the end. The beginning item contains |
| * the count of the saved maps (head.length), and the module they |
| * belong to if not built in (head.mod). The ending item contains a |
| * pointer to the next array of saved eval_map items. |
| */ |
| union trace_eval_map_item { |
| struct trace_eval_map map; |
| struct trace_eval_map_head head; |
| struct trace_eval_map_tail tail; |
| }; |
| |
| static union trace_eval_map_item *trace_eval_maps; |
| #endif /* CONFIG_TRACE_EVAL_MAP_FILE */ |
| |
| static int tracing_set_tracer(struct trace_array *tr, const char *buf); |
| |
| #define MAX_TRACER_SIZE 100 |
| static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata; |
| static char *default_bootup_tracer; |
| |
| static bool allocate_snapshot; |
| |
| static int __init set_cmdline_ftrace(char *str) |
| { |
| strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE); |
| default_bootup_tracer = bootup_tracer_buf; |
| /* We are using ftrace early, expand it */ |
| ring_buffer_expanded = true; |
| return 1; |
| } |
| __setup("ftrace=", set_cmdline_ftrace); |
| |
| static int __init set_ftrace_dump_on_oops(char *str) |
| { |
| if (*str++ != '=' || !*str) { |
| ftrace_dump_on_oops = DUMP_ALL; |
| return 1; |
| } |
| |
| if (!strcmp("orig_cpu", str)) { |
| ftrace_dump_on_oops = DUMP_ORIG; |
| return 1; |
| } |
| |
| return 0; |
| } |
| __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops); |
| |
| static int __init stop_trace_on_warning(char *str) |
| { |
| if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0)) |
| __disable_trace_on_warning = 1; |
| return 1; |
| } |
| __setup("traceoff_on_warning", stop_trace_on_warning); |
| |
| static int __init boot_alloc_snapshot(char *str) |
| { |
| allocate_snapshot = true; |
| /* We also need the main ring buffer expanded */ |
| ring_buffer_expanded = true; |
| return 1; |
| } |
| __setup("alloc_snapshot", boot_alloc_snapshot); |
| |
| |
| static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata; |
| |
| static int __init set_trace_boot_options(char *str) |
| { |
| strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE); |
| return 0; |
| } |
| __setup("trace_options=", set_trace_boot_options); |
| |
| static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata; |
| static char *trace_boot_clock __initdata; |
| |
| static int __init set_trace_boot_clock(char *str) |
| { |
| strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE); |
| trace_boot_clock = trace_boot_clock_buf; |
| return 0; |
| } |
| __setup("trace_clock=", set_trace_boot_clock); |
| |
| static int __init set_tracepoint_printk(char *str) |
| { |
| if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0)) |
| tracepoint_printk = 1; |
| return 1; |
| } |
| __setup("tp_printk", set_tracepoint_printk); |
| |
| unsigned long long ns2usecs(u64 nsec) |
| { |
| nsec += 500; |
| do_div(nsec, 1000); |
| return nsec; |
| } |
| |
| /* trace_flags holds trace_options default values */ |
| #define TRACE_DEFAULT_FLAGS \ |
| (FUNCTION_DEFAULT_FLAGS | \ |
| TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK | \ |
| TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | \ |
| TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE | \ |
| TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS) |
| |
| /* trace_options that are only supported by global_trace */ |
| #define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK | \ |
| TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD) |
| |
| /* trace_flags that are default zero for instances */ |
| #define ZEROED_TRACE_FLAGS \ |
| (TRACE_ITER_EVENT_FORK | TRACE_ITER_FUNC_FORK) |
| |
| /* |
| * The global_trace is the descriptor that holds the top-level tracing |
| * buffers for the live tracing. |
| */ |
| static struct trace_array global_trace = { |
| .trace_flags = TRACE_DEFAULT_FLAGS, |
| }; |
| |
| LIST_HEAD(ftrace_trace_arrays); |
| |
| int trace_array_get(struct trace_array *this_tr) |
| { |
| struct trace_array *tr; |
| int ret = -ENODEV; |
| |
| mutex_lock(&trace_types_lock); |
| list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| if (tr == this_tr) { |
| tr->ref++; |
| ret = 0; |
| break; |
| } |
| } |
| mutex_unlock(&trace_types_lock); |
| |
| return ret; |
| } |
| |
| static void __trace_array_put(struct trace_array *this_tr) |
| { |
| WARN_ON(!this_tr->ref); |
| this_tr->ref--; |
| } |
| |
| void trace_array_put(struct trace_array *this_tr) |
| { |
| mutex_lock(&trace_types_lock); |
| __trace_array_put(this_tr); |
| mutex_unlock(&trace_types_lock); |
| } |
| |
| int call_filter_check_discard(struct trace_event_call *call, void *rec, |
| struct ring_buffer *buffer, |
| struct ring_buffer_event *event) |
| { |
| if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) && |
| !filter_match_preds(call->filter, rec)) { |
| __trace_event_discard_commit(buffer, event); |
| return 1; |
| } |
| |
| return 0; |
| } |
| |
| void trace_free_pid_list(struct trace_pid_list *pid_list) |
| { |
| vfree(pid_list->pids); |
| kfree(pid_list); |
| } |
| |
| /** |
| * trace_find_filtered_pid - check if a pid exists in a filtered_pid list |
| * @filtered_pids: The list of pids to check |
| * @search_pid: The PID to find in @filtered_pids |
| * |
| * Returns true if @search_pid is fonud in @filtered_pids, and false otherwis. |
| */ |
| bool |
| trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid) |
| { |
| /* |
| * If pid_max changed after filtered_pids was created, we |
| * by default ignore all pids greater than the previous pid_max. |
| */ |
| if (search_pid >= filtered_pids->pid_max) |
| return false; |
| |
| return test_bit(search_pid, filtered_pids->pids); |
| } |
| |
| /** |
| * trace_ignore_this_task - should a task be ignored for tracing |
| * @filtered_pids: The list of pids to check |
| * @task: The task that should be ignored if not filtered |
| * |
| * Checks if @task should be traced or not from @filtered_pids. |
| * Returns true if @task should *NOT* be traced. |
| * Returns false if @task should be traced. |
| */ |
| bool |
| trace_ignore_this_task(struct trace_pid_list *filtered_pids, struct task_struct *task) |
| { |
| /* |
| * Return false, because if filtered_pids does not exist, |
| * all pids are good to trace. |
| */ |
| if (!filtered_pids) |
| return false; |
| |
| return !trace_find_filtered_pid(filtered_pids, task->pid); |
| } |
| |
| /** |
| * trace_pid_filter_add_remove - Add or remove a task from a pid_list |
| * @pid_list: The list to modify |
| * @self: The current task for fork or NULL for exit |
| * @task: The task to add or remove |
| * |
| * If adding a task, if @self is defined, the task is only added if @self |
| * is also included in @pid_list. This happens on fork and tasks should |
| * only be added when the parent is listed. If @self is NULL, then the |
| * @task pid will be removed from the list, which would happen on exit |
| * of a task. |
| */ |
| void trace_filter_add_remove_task(struct trace_pid_list *pid_list, |
| struct task_struct *self, |
| struct task_struct *task) |
| { |
| if (!pid_list) |
| return; |
| |
| /* For forks, we only add if the forking task is listed */ |
| if (self) { |
| if (!trace_find_filtered_pid(pid_list, self->pid)) |
| return; |
| } |
| |
| /* Sorry, but we don't support pid_max changing after setting */ |
| if (task->pid >= pid_list->pid_max) |
| return; |
| |
| /* "self" is set for forks, and NULL for exits */ |
| if (self) |
| set_bit(task->pid, pid_list->pids); |
| else |
| clear_bit(task->pid, pid_list->pids); |
| } |
| |
| /** |
| * trace_pid_next - Used for seq_file to get to the next pid of a pid_list |
| * @pid_list: The pid list to show |
| * @v: The last pid that was shown (+1 the actual pid to let zero be displayed) |
| * @pos: The position of the file |
| * |
| * This is used by the seq_file "next" operation to iterate the pids |
| * listed in a trace_pid_list structure. |
| * |
| * Returns the pid+1 as we want to display pid of zero, but NULL would |
| * stop the iteration. |
| */ |
| void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos) |
| { |
| unsigned long pid = (unsigned long)v; |
| |
| (*pos)++; |
| |
| /* pid already is +1 of the actual prevous bit */ |
| pid = find_next_bit(pid_list->pids, pid_list->pid_max, pid); |
| |
| /* Return pid + 1 to allow zero to be represented */ |
| if (pid < pid_list->pid_max) |
| return (void *)(pid + 1); |
| |
| return NULL; |
| } |
| |
| /** |
| * trace_pid_start - Used for seq_file to start reading pid lists |
| * @pid_list: The pid list to show |
| * @pos: The position of the file |
| * |
| * This is used by seq_file "start" operation to start the iteration |
| * of listing pids. |
| * |
| * Returns the pid+1 as we want to display pid of zero, but NULL would |
| * stop the iteration. |
| */ |
| void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos) |
| { |
| unsigned long pid; |
| loff_t l = 0; |
| |
| pid = find_first_bit(pid_list->pids, pid_list->pid_max); |
| if (pid >= pid_list->pid_max) |
| return NULL; |
| |
| /* Return pid + 1 so that zero can be the exit value */ |
| for (pid++; pid && l < *pos; |
| pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l)) |
| ; |
| return (void *)pid; |
| } |
| |
| /** |
| * trace_pid_show - show the current pid in seq_file processing |
| * @m: The seq_file structure to write into |
| * @v: A void pointer of the pid (+1) value to display |
| * |
| * Can be directly used by seq_file operations to display the current |
| * pid value. |
| */ |
| int trace_pid_show(struct seq_file *m, void *v) |
| { |
| unsigned long pid = (unsigned long)v - 1; |
| |
| seq_printf(m, "%lu\n", pid); |
| return 0; |
| } |
| |
| /* 128 should be much more than enough */ |
| #define PID_BUF_SIZE 127 |
| |
| int trace_pid_write(struct trace_pid_list *filtered_pids, |
| struct trace_pid_list **new_pid_list, |
| const char __user *ubuf, size_t cnt) |
| { |
| struct trace_pid_list *pid_list; |
| struct trace_parser parser; |
| unsigned long val; |
| int nr_pids = 0; |
| ssize_t read = 0; |
| ssize_t ret = 0; |
| loff_t pos; |
| pid_t pid; |
| |
| if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1)) |
| return -ENOMEM; |
| |
| /* |
| * Always recreate a new array. The write is an all or nothing |
| * operation. Always create a new array when adding new pids by |
| * the user. If the operation fails, then the current list is |
| * not modified. |
| */ |
| pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL); |
| if (!pid_list) |
| return -ENOMEM; |
| |
| pid_list->pid_max = READ_ONCE(pid_max); |
| |
| /* Only truncating will shrink pid_max */ |
| if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max) |
| pid_list->pid_max = filtered_pids->pid_max; |
| |
| pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3); |
| if (!pid_list->pids) { |
| kfree(pid_list); |
| return -ENOMEM; |
| } |
| |
| if (filtered_pids) { |
| /* copy the current bits to the new max */ |
| for_each_set_bit(pid, filtered_pids->pids, |
| filtered_pids->pid_max) { |
| set_bit(pid, pid_list->pids); |
| nr_pids++; |
| } |
| } |
| |
| while (cnt > 0) { |
| |
| pos = 0; |
| |
| ret = trace_get_user(&parser, ubuf, cnt, &pos); |
| if (ret < 0 || !trace_parser_loaded(&parser)) |
| break; |
| |
| read += ret; |
| ubuf += ret; |
| cnt -= ret; |
| |
| parser.buffer[parser.idx] = 0; |
| |
| ret = -EINVAL; |
| if (kstrtoul(parser.buffer, 0, &val)) |
| break; |
| if (val >= pid_list->pid_max) |
| break; |
| |
| pid = (pid_t)val; |
| |
| set_bit(pid, pid_list->pids); |
| nr_pids++; |
| |
| trace_parser_clear(&parser); |
| ret = 0; |
| } |
| trace_parser_put(&parser); |
| |
| if (ret < 0) { |
| trace_free_pid_list(pid_list); |
| return ret; |
| } |
| |
| if (!nr_pids) { |
| /* Cleared the list of pids */ |
| trace_free_pid_list(pid_list); |
| read = ret; |
| pid_list = NULL; |
| } |
| |
| *new_pid_list = pid_list; |
| |
| return read; |
| } |
| |
| static u64 buffer_ftrace_now(struct trace_buffer *buf, int cpu) |
| { |
| u64 ts; |
| |
| /* Early boot up does not have a buffer yet */ |
| if (!buf->buffer) |
| return trace_clock_local(); |
| |
| ts = ring_buffer_time_stamp(buf->buffer, cpu); |
| ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts); |
| |
| return ts; |
| } |
| |
| u64 ftrace_now(int cpu) |
| { |
| return buffer_ftrace_now(&global_trace.trace_buffer, cpu); |
| } |
| |
| /** |
| * tracing_is_enabled - Show if global_trace has been disabled |
| * |
| * Shows if the global trace has been enabled or not. It uses the |
| * mirror flag "buffer_disabled" to be used in fast paths such as for |
| * the irqsoff tracer. But it may be inaccurate due to races. If you |
| * need to know the accurate state, use tracing_is_on() which is a little |
| * slower, but accurate. |
| */ |
| int tracing_is_enabled(void) |
| { |
| /* |
| * For quick access (irqsoff uses this in fast path), just |
| * return the mirror variable of the state of the ring buffer. |
| * It's a little racy, but we don't really care. |
| */ |
| smp_rmb(); |
| return !global_trace.buffer_disabled; |
| } |
| |
| /* |
| * trace_buf_size is the size in bytes that is allocated |
| * for a buffer. Note, the number of bytes is always rounded |
| * to page size. |
| * |
| * This number is purposely set to a low number of 16384. |
| * If the dump on oops happens, it will be much appreciated |
| * to not have to wait for all that output. Anyway this can be |
| * boot time and run time configurable. |
| */ |
| #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */ |
| |
| static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT; |
| |
| /* trace_types holds a link list of available tracers. */ |
| static struct tracer *trace_types __read_mostly; |
| |
| /* |
| * trace_types_lock is used to protect the trace_types list. |
| */ |
| DEFINE_MUTEX(trace_types_lock); |
| |
| /* |
| * serialize the access of the ring buffer |
| * |
| * ring buffer serializes readers, but it is low level protection. |
| * The validity of the events (which returns by ring_buffer_peek() ..etc) |
| * are not protected by ring buffer. |
| * |
| * The content of events may become garbage if we allow other process consumes |
| * these events concurrently: |
| * A) the page of the consumed events may become a normal page |
| * (not reader page) in ring buffer, and this page will be rewrited |
| * by events producer. |
| * B) The page of the consumed events may become a page for splice_read, |
| * and this page will be returned to system. |
| * |
| * These primitives allow multi process access to different cpu ring buffer |
| * concurrently. |
| * |
| * These primitives don't distinguish read-only and read-consume access. |
| * Multi read-only access are also serialized. |
| */ |
| |
| #ifdef CONFIG_SMP |
| static DECLARE_RWSEM(all_cpu_access_lock); |
| static DEFINE_PER_CPU(struct mutex, cpu_access_lock); |
| |
| static inline void trace_access_lock(int cpu) |
| { |
| if (cpu == RING_BUFFER_ALL_CPUS) { |
| /* gain it for accessing the whole ring buffer. */ |
| down_write(&all_cpu_access_lock); |
| } else { |
| /* gain it for accessing a cpu ring buffer. */ |
| |
| /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */ |
| down_read(&all_cpu_access_lock); |
| |
| /* Secondly block other access to this @cpu ring buffer. */ |
| mutex_lock(&per_cpu(cpu_access_lock, cpu)); |
| } |
| } |
| |
| static inline void trace_access_unlock(int cpu) |
| { |
| if (cpu == RING_BUFFER_ALL_CPUS) { |
| up_write(&all_cpu_access_lock); |
| } else { |
| mutex_unlock(&per_cpu(cpu_access_lock, cpu)); |
| up_read(&all_cpu_access_lock); |
| } |
| } |
| |
| static inline void trace_access_lock_init(void) |
| { |
| int cpu; |
| |
| for_each_possible_cpu(cpu) |
| mutex_init(&per_cpu(cpu_access_lock, cpu)); |
| } |
| |
| #else |
| |
| static DEFINE_MUTEX(access_lock); |
| |
| static inline void trace_access_lock(int cpu) |
| { |
| (void)cpu; |
| mutex_lock(&access_lock); |
| } |
| |
| static inline void trace_access_unlock(int cpu) |
| { |
| (void)cpu; |
| mutex_unlock(&access_lock); |
| } |
| |
| static inline void trace_access_lock_init(void) |
| { |
| } |
| |
| #endif |
| |
| #ifdef CONFIG_STACKTRACE |
| static void __ftrace_trace_stack(struct ring_buffer *buffer, |
| unsigned long flags, |
| int skip, int pc, struct pt_regs *regs); |
| static inline void ftrace_trace_stack(struct trace_array *tr, |
| struct ring_buffer *buffer, |
| unsigned long flags, |
| int skip, int pc, struct pt_regs *regs); |
| |
| #else |
| static inline void __ftrace_trace_stack(struct ring_buffer *buffer, |
| unsigned long flags, |
| int skip, int pc, struct pt_regs *regs) |
| { |
| } |
| static inline void ftrace_trace_stack(struct trace_array *tr, |
| struct ring_buffer *buffer, |
| unsigned long flags, |
| int skip, int pc, struct pt_regs *regs) |
| { |
| } |
| |
| #endif |
| |
| static __always_inline void |
| trace_event_setup(struct ring_buffer_event *event, |
| int type, unsigned long flags, int pc) |
| { |
| struct trace_entry *ent = ring_buffer_event_data(event); |
| |
| tracing_generic_entry_update(ent, flags, pc); |
| ent->type = type; |
| } |
| |
| static __always_inline struct ring_buffer_event * |
| __trace_buffer_lock_reserve(struct ring_buffer *buffer, |
| int type, |
| unsigned long len, |
| unsigned long flags, int pc) |
| { |
| struct ring_buffer_event *event; |
| |
| event = ring_buffer_lock_reserve(buffer, len); |
| if (event != NULL) |
| trace_event_setup(event, type, flags, pc); |
| |
| return event; |
| } |
| |
| void tracer_tracing_on(struct trace_array *tr) |
| { |
| if (tr->trace_buffer.buffer) |
| ring_buffer_record_on(tr->trace_buffer.buffer); |
| /* |
| * This flag is looked at when buffers haven't been allocated |
| * yet, or by some tracers (like irqsoff), that just want to |
| * know if the ring buffer has been disabled, but it can handle |
| * races of where it gets disabled but we still do a record. |
| * As the check is in the fast path of the tracers, it is more |
| * important to be fast than accurate. |
| */ |
| tr->buffer_disabled = 0; |
| /* Make the flag seen by readers */ |
| smp_wmb(); |
| } |
| |
| /** |
| * tracing_on - enable tracing buffers |
| * |
| * This function enables tracing buffers that may have been |
| * disabled with tracing_off. |
| */ |
| void tracing_on(void) |
| { |
| tracer_tracing_on(&global_trace); |
| } |
| EXPORT_SYMBOL_GPL(tracing_on); |
| |
| |
| static __always_inline void |
| __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event) |
| { |
| __this_cpu_write(trace_taskinfo_save, true); |
| |
| /* If this is the temp buffer, we need to commit fully */ |
| if (this_cpu_read(trace_buffered_event) == event) { |
| /* Length is in event->array[0] */ |
| ring_buffer_write(buffer, event->array[0], &event->array[1]); |
| /* Release the temp buffer */ |
| this_cpu_dec(trace_buffered_event_cnt); |
| } else |
| ring_buffer_unlock_commit(buffer, event); |
| } |
| |
| /** |
| * __trace_puts - write a constant string into the trace buffer. |
| * @ip: The address of the caller |
| * @str: The constant string to write |
| * @size: The size of the string. |
| */ |
| int __trace_puts(unsigned long ip, const char *str, int size) |
| { |
| struct ring_buffer_event *event; |
| struct ring_buffer *buffer; |
| struct print_entry *entry; |
| unsigned long irq_flags; |
| int alloc; |
| int pc; |
| |
| if (!(global_trace.trace_flags & TRACE_ITER_PRINTK)) |
| return 0; |
| |
| pc = preempt_count(); |
| |
| if (unlikely(tracing_selftest_running || tracing_disabled)) |
| return 0; |
| |
| alloc = sizeof(*entry) + size + 2; /* possible \n added */ |
| |
| local_save_flags(irq_flags); |
| buffer = global_trace.trace_buffer.buffer; |
| event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc, |
| irq_flags, pc); |
| if (!event) |
| return 0; |
| |
| entry = ring_buffer_event_data(event); |
| entry->ip = ip; |
| |
| memcpy(&entry->buf, str, size); |
| |
| /* Add a newline if necessary */ |
| if (entry->buf[size - 1] != '\n') { |
| entry->buf[size] = '\n'; |
| entry->buf[size + 1] = '\0'; |
| } else |
| entry->buf[size] = '\0'; |
| |
| __buffer_unlock_commit(buffer, event); |
| ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL); |
| |
| return size; |
| } |
| EXPORT_SYMBOL_GPL(__trace_puts); |
| |
| /** |
| * __trace_bputs - write the pointer to a constant string into trace buffer |
| * @ip: The address of the caller |
| * @str: The constant string to write to the buffer to |
| */ |
| int __trace_bputs(unsigned long ip, const char *str) |
| { |
| struct ring_buffer_event *event; |
| struct ring_buffer *buffer; |
| struct bputs_entry *entry; |
| unsigned long irq_flags; |
| int size = sizeof(struct bputs_entry); |
| int pc; |
| |
| if (!(global_trace.trace_flags & TRACE_ITER_PRINTK)) |
| return 0; |
| |
| pc = preempt_count(); |
| |
| if (unlikely(tracing_selftest_running || tracing_disabled)) |
| return 0; |
| |
| local_save_flags(irq_flags); |
| buffer = global_trace.trace_buffer.buffer; |
| event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size, |
| irq_flags, pc); |
| if (!event) |
| return 0; |
| |
| entry = ring_buffer_event_data(event); |
| entry->ip = ip; |
| entry->str = str; |
| |
| __buffer_unlock_commit(buffer, event); |
| ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL); |
| |
| return 1; |
| } |
| EXPORT_SYMBOL_GPL(__trace_bputs); |
| |
| #ifdef CONFIG_TRACER_SNAPSHOT |
| static void tracing_snapshot_instance(struct trace_array *tr) |
| { |
| struct tracer *tracer = tr->current_trace; |
| unsigned long flags; |
| |
| if (in_nmi()) { |
| internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n"); |
| internal_trace_puts("*** snapshot is being ignored ***\n"); |
| return; |
| } |
| |
| if (!tr->allocated_snapshot) { |
| internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n"); |
| internal_trace_puts("*** stopping trace here! ***\n"); |
| tracing_off(); |
| return; |
| } |
| |
| /* Note, snapshot can not be used when the tracer uses it */ |
| if (tracer->use_max_tr) { |
| internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n"); |
| internal_trace_puts("*** Can not use snapshot (sorry) ***\n"); |
| return; |
| } |
| |
| local_irq_save(flags); |
| update_max_tr(tr, current, smp_processor_id()); |
| local_irq_restore(flags); |
| } |
| |
| /** |
| * trace_snapshot - take a snapshot of the current buffer. |
| * |
| * This causes a swap between the snapshot buffer and the current live |
| * tracing buffer. You can use this to take snapshots of the live |
| * trace when some condition is triggered, but continue to trace. |
| * |
| * Note, make sure to allocate the snapshot with either |
| * a tracing_snapshot_alloc(), or by doing it manually |
| * with: echo 1 > /sys/kernel/debug/tracing/snapshot |
| * |
| * If the snapshot buffer is not allocated, it will stop tracing. |
| * Basically making a permanent snapshot. |
| */ |
| void tracing_snapshot(void) |
| { |
| struct trace_array *tr = &global_trace; |
| |
| tracing_snapshot_instance(tr); |
| } |
| EXPORT_SYMBOL_GPL(tracing_snapshot); |
| |
| static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf, |
| struct trace_buffer *size_buf, int cpu_id); |
| static void set_buffer_entries(struct trace_buffer *buf, unsigned long val); |
| |
| static int alloc_snapshot(struct trace_array *tr) |
| { |
| int ret; |
| |
| if (!tr->allocated_snapshot) { |
| |
| /* allocate spare buffer */ |
| ret = resize_buffer_duplicate_size(&tr->max_buffer, |
| &tr->trace_buffer, RING_BUFFER_ALL_CPUS); |
| if (ret < 0) |
| return ret; |
| |
| tr->allocated_snapshot = true; |
| } |
| |
| return 0; |
| } |
| |
| static void free_snapshot(struct trace_array *tr) |
| { |
| /* |
| * We don't free the ring buffer. instead, resize it because |
| * The max_tr ring buffer has some state (e.g. ring->clock) and |
| * we want preserve it. |
| */ |
| ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS); |
| set_buffer_entries(&tr->max_buffer, 1); |
| tracing_reset_online_cpus(&tr->max_buffer); |
| tr->allocated_snapshot = false; |
| } |
| |
| /** |
| * tracing_alloc_snapshot - allocate snapshot buffer. |
| * |
| * This only allocates the snapshot buffer if it isn't already |
| * allocated - it doesn't also take a snapshot. |
| * |
| * This is meant to be used in cases where the snapshot buffer needs |
| * to be set up for events that can't sleep but need to be able to |
| * trigger a snapshot. |
| */ |
| int tracing_alloc_snapshot(void) |
| { |
| struct trace_array *tr = &global_trace; |
| int ret; |
| |
| ret = alloc_snapshot(tr); |
| WARN_ON(ret < 0); |
| |
| return ret; |
| } |
| EXPORT_SYMBOL_GPL(tracing_alloc_snapshot); |
| |
| /** |
| * trace_snapshot_alloc - allocate and take a snapshot of the current buffer. |
| * |
| * This is similar to trace_snapshot(), but it will allocate the |
| * snapshot buffer if it isn't already allocated. Use this only |
| * where it is safe to sleep, as the allocation may sleep. |
| * |
| * This causes a swap between the snapshot buffer and the current live |
| * tracing buffer. You can use this to take snapshots of the live |
| * trace when some condition is triggered, but continue to trace. |
| */ |
| void tracing_snapshot_alloc(void) |
| { |
| int ret; |
| |
| ret = tracing_alloc_snapshot(); |
| if (ret < 0) |
| return; |
| |
| tracing_snapshot(); |
| } |
| EXPORT_SYMBOL_GPL(tracing_snapshot_alloc); |
| #else |
| void tracing_snapshot(void) |
| { |
| WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used"); |
| } |
| EXPORT_SYMBOL_GPL(tracing_snapshot); |
| int tracing_alloc_snapshot(void) |
| { |
| WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used"); |
| return -ENODEV; |
| } |
| EXPORT_SYMBOL_GPL(tracing_alloc_snapshot); |
| void tracing_snapshot_alloc(void) |
| { |
| /* Give warning */ |
| tracing_snapshot(); |
| } |
| EXPORT_SYMBOL_GPL(tracing_snapshot_alloc); |
| #endif /* CONFIG_TRACER_SNAPSHOT */ |
| |
| void tracer_tracing_off(struct trace_array *tr) |
| { |
| if (tr->trace_buffer.buffer) |
| ring_buffer_record_off(tr->trace_buffer.buffer); |
| /* |
| * This flag is looked at when buffers haven't been allocated |
| * yet, or by some tracers (like irqsoff), that just want to |
| * know if the ring buffer has been disabled, but it can handle |
| * races of where it gets disabled but we still do a record. |
| * As the check is in the fast path of the tracers, it is more |
| * important to be fast than accurate. |
| */ |
| tr->buffer_disabled = 1; |
| /* Make the flag seen by readers */ |
| smp_wmb(); |
| } |
| |
| /** |
| * tracing_off - turn off tracing buffers |
| * |
| * This function stops the tracing buffers from recording data. |
| * It does not disable any overhead the tracers themselves may |
| * be causing. This function simply causes all recording to |
| * the ring buffers to fail. |
| */ |
| void tracing_off(void) |
| { |
| tracer_tracing_off(&global_trace); |
| } |
| EXPORT_SYMBOL_GPL(tracing_off); |
| |
| void disable_trace_on_warning(void) |
| { |
| if (__disable_trace_on_warning) |
| tracing_off(); |
| } |
| |
| /** |
| * tracer_tracing_is_on - show real state of ring buffer enabled |
| * @tr : the trace array to know if ring buffer is enabled |
| * |
| * Shows real state of the ring buffer if it is enabled or not. |
| */ |
| int tracer_tracing_is_on(struct trace_array *tr) |
| { |
| if (tr->trace_buffer.buffer) |
| return ring_buffer_record_is_on(tr->trace_buffer.buffer); |
| return !tr->buffer_disabled; |
| } |
| |
| /** |
| * tracing_is_on - show state of ring buffers enabled |
| */ |
| int tracing_is_on(void) |
| { |
| return tracer_tracing_is_on(&global_trace); |
| } |
| EXPORT_SYMBOL_GPL(tracing_is_on); |
| |
| static int __init set_buf_size(char *str) |
| { |
| unsigned long buf_size; |
| |
| if (!str) |
| return 0; |
| buf_size = memparse(str, &str); |
| /* nr_entries can not be zero */ |
| if (buf_size == 0) |
| return 0; |
| trace_buf_size = buf_size; |
| return 1; |
| } |
| __setup("trace_buf_size=", set_buf_size); |
| |
| static int __init set_tracing_thresh(char *str) |
| { |
| unsigned long threshold; |
| int ret; |
| |
| if (!str) |
| return 0; |
| ret = kstrtoul(str, 0, &threshold); |
| if (ret < 0) |
| return 0; |
| tracing_thresh = threshold * 1000; |
| return 1; |
| } |
| __setup("tracing_thresh=", set_tracing_thresh); |
| |
| unsigned long nsecs_to_usecs(unsigned long nsecs) |
| { |
| return nsecs / 1000; |
| } |
| |
| /* |
| * TRACE_FLAGS is defined as a tuple matching bit masks with strings. |
| * It uses C(a, b) where 'a' is the eval (enum) name and 'b' is the string that |
| * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list |
| * of strings in the order that the evals (enum) were defined. |
| */ |
| #undef C |
| #define C(a, b) b |
| |
| /* These must match the bit postions in trace_iterator_flags */ |
| static const char *trace_options[] = { |
| TRACE_FLAGS |
| NULL |
| }; |
| |
| static struct { |
| u64 (*func)(void); |
| const char *name; |
| int in_ns; /* is this clock in nanoseconds? */ |
| } trace_clocks[] = { |
| { trace_clock_local, "local", 1 }, |
| { trace_clock_global, "global", 1 }, |
| { trace_clock_counter, "counter", 0 }, |
| { trace_clock_jiffies, "uptime", 0 }, |
| { trace_clock, "perf", 1 }, |
| { ktime_get_mono_fast_ns, "mono", 1 }, |
| { ktime_get_raw_fast_ns, "mono_raw", 1 }, |
| { ktime_get_boot_fast_ns, "boot", 1 }, |
| ARCH_TRACE_CLOCKS |
| }; |
| |
| /* |
| * trace_parser_get_init - gets the buffer for trace parser |
| */ |
| int trace_parser_get_init(struct trace_parser *parser, int size) |
| { |
| memset(parser, 0, sizeof(*parser)); |
| |
| parser->buffer = kmalloc(size, GFP_KERNEL); |
| if (!parser->buffer) |
| return 1; |
| |
| parser->size = size; |
| return 0; |
| } |
| |
| /* |
| * trace_parser_put - frees the buffer for trace parser |
| */ |
| void trace_parser_put(struct trace_parser *parser) |
| { |
| kfree(parser->buffer); |
| parser->buffer = NULL; |
| } |
| |
| /* |
| * trace_get_user - reads the user input string separated by space |
| * (matched by isspace(ch)) |
| * |
| * For each string found the 'struct trace_parser' is updated, |
| * and the function returns. |
| * |
| * Returns number of bytes read. |
| * |
| * See kernel/trace/trace.h for 'struct trace_parser' details. |
| */ |
| int trace_get_user(struct trace_parser *parser, const char __user *ubuf, |
| size_t cnt, loff_t *ppos) |
| { |
| char ch; |
| size_t read = 0; |
| ssize_t ret; |
| |
| if (!*ppos) |
| trace_parser_clear(parser); |
| |
| ret = get_user(ch, ubuf++); |
| if (ret) |
| goto out; |
| |
| read++; |
| cnt--; |
| |
| /* |
| * The parser is not finished with the last write, |
| * continue reading the user input without skipping spaces. |
| */ |
| if (!parser->cont) { |
| /* skip white space */ |
| while (cnt && isspace(ch)) { |
| ret = get_user(ch, ubuf++); |
| if (ret) |
| goto out; |
| read++; |
| cnt--; |
| } |
| |
| /* only spaces were written */ |
| if (isspace(ch)) { |
| *ppos += read; |
| ret = read; |
| goto out; |
| } |
| |
| parser->idx = 0; |
| } |
| |
| /* read the non-space input */ |
| while (cnt && !isspace(ch)) { |
| if (parser->idx < parser->size - 1) |
| parser->buffer[parser->idx++] = ch; |
| else { |
| ret = -EINVAL; |
| goto out; |
| } |
| ret = get_user(ch, ubuf++); |
| if (ret) |
| goto out; |
| read++; |
| cnt--; |
| } |
| |
| /* We either got finished input or we have to wait for another call. */ |
| if (isspace(ch)) { |
| parser->buffer[parser->idx] = 0; |
| parser->cont = false; |
| } else if (parser->idx < parser->size - 1) { |
| parser->cont = true; |
| parser->buffer[parser->idx++] = ch; |
| } else { |
| ret = -EINVAL; |
| goto out; |
| } |
| |
| *ppos += read; |
| ret = read; |
| |
| out: |
| return ret; |
| } |
| |
| /* TODO add a seq_buf_to_buffer() */ |
| static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt) |
| { |
| int len; |
| |
| if (trace_seq_used(s) <= s->seq.readpos) |
| return -EBUSY; |
| |
| len = trace_seq_used(s) - s->seq.readpos; |
| if (cnt > len) |
| cnt = len; |
| memcpy(buf, s->buffer + s->seq.readpos, cnt); |
| |
| s->seq.readpos += cnt; |
| return cnt; |
| } |
| |
| unsigned long __read_mostly tracing_thresh; |
| |
| #ifdef CONFIG_TRACER_MAX_TRACE |
| /* |
| * Copy the new maximum trace into the separate maximum-trace |
| * structure. (this way the maximum trace is permanently saved, |
| * for later retrieval via /sys/kernel/debug/tracing/latency_trace) |
| */ |
| static void |
| __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) |
| { |
| struct trace_buffer *trace_buf = &tr->trace_buffer; |
| struct trace_buffer *max_buf = &tr->max_buffer; |
| struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu); |
| struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu); |
| |
| max_buf->cpu = cpu; |
| max_buf->time_start = data->preempt_timestamp; |
| |
| max_data->saved_latency = tr->max_latency; |
| max_data->critical_start = data->critical_start; |
| max_data->critical_end = data->critical_end; |
| |
| memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN); |
| max_data->pid = tsk->pid; |
| /* |
| * If tsk == current, then use current_uid(), as that does not use |
| * RCU. The irq tracer can be called out of RCU scope. |
| */ |
| if (tsk == current) |
| max_data->uid = current_uid(); |
| else |
| max_data->uid = task_uid(tsk); |
| |
| max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO; |
| max_data->policy = tsk->policy; |
| max_data->rt_priority = tsk->rt_priority; |
| |
| /* record this tasks comm */ |
| tracing_record_cmdline(tsk); |
| } |
| |
| /** |
| * update_max_tr - snapshot all trace buffers from global_trace to max_tr |
| * @tr: tracer |
| * @tsk: the task with the latency |
| * @cpu: The cpu that initiated the trace. |
| * |
| * Flip the buffers between the @tr and the max_tr and record information |
| * about which task was the cause of this latency. |
| */ |
| void |
| update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) |
| { |
| struct ring_buffer *buf; |
| |
| if (tr->stop_count) |
| return; |
| |
| WARN_ON_ONCE(!irqs_disabled()); |
| |
| if (!tr->allocated_snapshot) { |
| /* Only the nop tracer should hit this when disabling */ |
| WARN_ON_ONCE(tr->current_trace != &nop_trace); |
| return; |
| } |
| |
| arch_spin_lock(&tr->max_lock); |
| |
| buf = tr->trace_buffer.buffer; |
| tr->trace_buffer.buffer = tr->max_buffer.buffer; |
| tr->max_buffer.buffer = buf; |
| |
| __update_max_tr(tr, tsk, cpu); |
| arch_spin_unlock(&tr->max_lock); |
| } |
| |
| /** |
| * update_max_tr_single - only copy one trace over, and reset the rest |
| * @tr - tracer |
| * @tsk - task with the latency |
| * @cpu - the cpu of the buffer to copy. |
| * |
| * Flip the trace of a single CPU buffer between the @tr and the max_tr. |
| */ |
| void |
| update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu) |
| { |
| int ret; |
| |
| if (tr->stop_count) |
| return; |
| |
| WARN_ON_ONCE(!irqs_disabled()); |
| if (!tr->allocated_snapshot) { |
| /* Only the nop tracer should hit this when disabling */ |
| WARN_ON_ONCE(tr->current_trace != &nop_trace); |
| return; |
| } |
| |
| arch_spin_lock(&tr->max_lock); |
| |
| ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu); |
| |
| if (ret == -EBUSY) { |
| /* |
| * We failed to swap the buffer due to a commit taking |
| * place on this CPU. We fail to record, but we reset |
| * the max trace buffer (no one writes directly to it) |
| * and flag that it failed. |
| */ |
| trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_, |
| "Failed to swap buffers due to commit in progress\n"); |
| } |
| |
| WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY); |
| |
| __update_max_tr(tr, tsk, cpu); |
| arch_spin_unlock(&tr->max_lock); |
| } |
| #endif /* CONFIG_TRACER_MAX_TRACE */ |
| |
| static int wait_on_pipe(struct trace_iterator *iter, bool full) |
| { |
| /* Iterators are static, they should be filled or empty */ |
| if (trace_buffer_iter(iter, iter->cpu_file)) |
| return 0; |
| |
| return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file, |
| full); |
| } |
| |
| #ifdef CONFIG_FTRACE_STARTUP_TEST |
| static bool selftests_can_run; |
| |
| struct trace_selftests { |
| struct list_head list; |
| struct tracer *type; |
| }; |
| |
| static LIST_HEAD(postponed_selftests); |
| |
| static int save_selftest(struct tracer *type) |
| { |
| struct trace_selftests *selftest; |
| |
| selftest = kmalloc(sizeof(*selftest), GFP_KERNEL); |
| if (!selftest) |
| return -ENOMEM; |
| |
| selftest->type = type; |
| list_add(&selftest->list, &postponed_selftests); |
| return 0; |
| } |
| |
| static int run_tracer_selftest(struct tracer *type) |
| { |
| struct trace_array *tr = &global_trace; |
| struct tracer *saved_tracer = tr->current_trace; |
| int ret; |
| |
| if (!type->selftest || tracing_selftest_disabled) |
| return 0; |
| |
| /* |
| * If a tracer registers early in boot up (before scheduling is |
| * initialized and such), then do not run its selftests yet. |
| * Instead, run it a little later in the boot process. |
| */ |
| if (!selftests_can_run) |
| return save_selftest(type); |
| |
| /* |
| * Run a selftest on this tracer. |
| * Here we reset the trace buffer, and set the current |
| * tracer to be this tracer. The tracer can then run some |
| * internal tracing to verify that everything is in order. |
| * If we fail, we do not register this tracer. |
| */ |
| tracing_reset_online_cpus(&tr->trace_buffer); |
| |
| tr->current_trace = type; |
| |
| #ifdef CONFIG_TRACER_MAX_TRACE |
| if (type->use_max_tr) { |
| /* If we expanded the buffers, make sure the max is expanded too */ |
| if (ring_buffer_expanded) |
| ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size, |
| RING_BUFFER_ALL_CPUS); |
| tr->allocated_snapshot = true; |
| } |
| #endif |
| |
| /* the test is responsible for initializing and enabling */ |
| pr_info("Testing tracer %s: ", type->name); |
| ret = type->selftest(type, tr); |
| /* the test is responsible for resetting too */ |
| tr->current_trace = saved_tracer; |
| if (ret) { |
| printk(KERN_CONT "FAILED!\n"); |
| /* Add the warning after printing 'FAILED' */ |
| WARN_ON(1); |
| return -1; |
| } |
| /* Only reset on passing, to avoid touching corrupted buffers */ |
| tracing_reset_online_cpus(&tr->trace_buffer); |
| |
| #ifdef CONFIG_TRACER_MAX_TRACE |
| if (type->use_max_tr) { |
| tr->allocated_snapshot = false; |
| |
| /* Shrink the max buffer again */ |
| if (ring_buffer_expanded) |
| ring_buffer_resize(tr->max_buffer.buffer, 1, |
| RING_BUFFER_ALL_CPUS); |
| } |
| #endif |
| |
| printk(KERN_CONT "PASSED\n"); |
| return 0; |
| } |
| |
| static __init int init_trace_selftests(void) |
| { |
| struct trace_selftests *p, *n; |
| struct tracer *t, **last; |
| int ret; |
| |
| selftests_can_run = true; |
| |
| mutex_lock(&trace_types_lock); |
| |
| if (list_empty(&postponed_selftests)) |
| goto out; |
| |
| pr_info("Running postponed tracer tests:\n"); |
| |
| list_for_each_entry_safe(p, n, &postponed_selftests, list) { |
| ret = run_tracer_selftest(p->type); |
| /* If the test fails, then warn and remove from available_tracers */ |
| if (ret < 0) { |
| WARN(1, "tracer: %s failed selftest, disabling\n", |
| p->type->name); |
| last = &trace_types; |
| for (t = trace_types; t; t = t->next) { |
| if (t == p->type) { |
| *last = t->next; |
| break; |
| } |
| last = &t->next; |
| } |
| } |
| list_del(&p->list); |
| kfree(p); |
| } |
| |
| out: |
| mutex_unlock(&trace_types_lock); |
| |
| return 0; |
| } |
| core_initcall(init_trace_selftests); |
| #else |
| static inline int run_tracer_selftest(struct tracer *type) |
| { |
| return 0; |
| } |
| #endif /* CONFIG_FTRACE_STARTUP_TEST */ |
| |
| static void add_tracer_options(struct trace_array *tr, struct tracer *t); |
| |
| static void __init apply_trace_boot_options(void); |
| |
| /** |
| * register_tracer - register a tracer with the ftrace system. |
| * @type - the plugin for the tracer |
| * |
| * Register a new plugin tracer. |
| */ |
| int __init register_tracer(struct tracer *type) |
| { |
| struct tracer *t; |
| int ret = 0; |
| |
| if (!type->name) { |
| pr_info("Tracer must have a name\n"); |
| return -1; |
| } |
| |
| if (strlen(type->name) >= MAX_TRACER_SIZE) { |
| pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE); |
| return -1; |
| } |
| |
| mutex_lock(&trace_types_lock); |
| |
| tracing_selftest_running = true; |
| |
| for (t = trace_types; t; t = t->next) { |
| if (strcmp(type->name, t->name) == 0) { |
| /* already found */ |
| pr_info("Tracer %s already registered\n", |
| type->name); |
| ret = -1; |
| goto out; |
| } |
| } |
| |
| if (!type->set_flag) |
| type->set_flag = &dummy_set_flag; |
| if (!type->flags) { |
| /*allocate a dummy tracer_flags*/ |
| type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL); |
| if (!type->flags) { |
| ret = -ENOMEM; |
| goto out; |
| } |
| type->flags->val = 0; |
| type->flags->opts = dummy_tracer_opt; |
| } else |
| if (!type->flags->opts) |
| type->flags->opts = dummy_tracer_opt; |
| |
| /* store the tracer for __set_tracer_option */ |
| type->flags->trace = type; |
| |
| ret = run_tracer_selftest(type); |
| if (ret < 0) |
| goto out; |
| |
| type->next = trace_types; |
| trace_types = type; |
| add_tracer_options(&global_trace, type); |
| |
| out: |
| tracing_selftest_running = false; |
| mutex_unlock(&trace_types_lock); |
| |
| if (ret || !default_bootup_tracer) |
| goto out_unlock; |
| |
| if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE)) |
| goto out_unlock; |
| |
| printk(KERN_INFO "Starting tracer '%s'\n", type->name); |
| /* Do we want this tracer to start on bootup? */ |
| tracing_set_tracer(&global_trace, type->name); |
| default_bootup_tracer = NULL; |
| |
| apply_trace_boot_options(); |
| |
| /* disable other selftests, since this will break it. */ |
| tracing_selftest_disabled = true; |
| #ifdef CONFIG_FTRACE_STARTUP_TEST |
| printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n", |
| type->name); |
| #endif |
| |
| out_unlock: |
| return ret; |
| } |
| |
| void tracing_reset(struct trace_buffer *buf, int cpu) |
| { |
| struct ring_buffer *buffer = buf->buffer; |
| |
| if (!buffer) |
| return; |
| |
| ring_buffer_record_disable(buffer); |
| |
| /* Make sure all commits have finished */ |
| synchronize_sched(); |
| ring_buffer_reset_cpu(buffer, cpu); |
| |
| ring_buffer_record_enable(buffer); |
| } |
| |
| void tracing_reset_online_cpus(struct trace_buffer *buf) |
| { |
| struct ring_buffer *buffer = buf->buffer; |
| int cpu; |
| |
| if (!buffer) |
| return; |
| |
| ring_buffer_record_disable(buffer); |
| |
| /* Make sure all commits have finished */ |
| synchronize_sched(); |
| |
| buf->time_start = buffer_ftrace_now(buf, buf->cpu); |
| |
| for_each_online_cpu(cpu) |
| ring_buffer_reset_cpu(buffer, cpu); |
| |
| ring_buffer_record_enable(buffer); |
| } |
| |
| /* Must have trace_types_lock held */ |
| void tracing_reset_all_online_cpus(void) |
| { |
| struct trace_array *tr; |
| |
| list_for_each_entry(tr, &ftrace_trace_arrays, list) { |
| if (!tr->clear_trace) |
| continue; |
| tr->clear_trace = false; |
| tracing_reset_online_cpus(&tr->trace_buffer); |
| #ifdef CONFIG_TRACER_MAX_TRACE |
| tracing_reset_online_cpus(&tr->max_buffer); |
| #endif |
| } |
| } |
| |
| static int *tgid_map; |
| |
| #define SAVED_CMDLINES_DEFAULT 128 |
| #define NO_CMDLINE_MAP UINT_MAX |
| static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED; |
| struct saved_cmdlines_buffer { |
| unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1]; |
| unsigned *map_cmdline_to_pid; |
| unsigned cmdline_num; |
| int cmdline_idx; |
| char *saved_cmdlines; |
| }; |
| static struct saved_cmdlines_buffer *savedcmd; |
| |
| /* temporary disable recording */ |
| static atomic_t trace_record_taskinfo_disabled __read_mostly; |
| |
| static inline char *get_saved_cmdlines(int idx) |
| { |
| return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN]; |
| } |
| |
| static inline void set_cmdline(int idx, const char *cmdline) |
| { |
| memcpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN); |
| } |
| |
| static int allocate_cmdlines_buffer(unsigned int val, |
| struct saved_cmdlines_buffer *s) |
| { |
| s->map_cmdline_to_pid = kmalloc(val * sizeof(*s->map_cmdline_to_pid), |
| GFP_KERNEL); |
| if (!s->map_cmdline_to_pid) |
| return -ENOMEM; |
| |
| s->saved_cmdlines = kmalloc(val * TASK_COMM_LEN, GFP_KERNEL); |
| if (!s->saved_cmdlines) { |
| kfree(s->map_cmdline_to_pid); |
| return -ENOMEM; |
| } |
| |
| s->cmdline_idx = 0; |
| s->cmdline_num = val; |
| memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP, |
| sizeof(s->map_pid_to_cmdline)); |
| memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP, |
| val * sizeof(*s->map_cmdline_to_pid)); |
| |
| return 0; |
| } |
| |
| static int trace_create_savedcmd(void) |
| { |
| int ret; |
| |
| savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL); |
| if (!savedcmd) |
| return -ENOMEM; |
| |
| ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd); |
| if (ret < 0) { |
| kfree(savedcmd); |
| savedcmd = NULL; |
| return -ENOMEM; |
| } |
| |
| return 0; |
| } |
| |
| int is_tracing_stopped(void) |
| { |
| return global_trace.stop_count; |
| } |
| |
| /** |
| * tracing_start - quick start of the tracer |
| * |
| * If tracing is enabled but was stopped by tracing_stop, |
| * this will start the tracer back up. |
| */ |
| void tracing_start(void) |
| { |
| struct ring_buffer *buffer; |
| unsigned long flags; |
| |
| if (tracing_disabled) |
| return; |
| |
| raw_spin_lock_irqsave(&global_trace.start_lock, flags); |
| if (--global_trace.stop_count) { |
| if (global_trace.stop_count < 0) { |
| /* Someone screwed up their debugging */ |
| WARN_ON_ONCE(1); |
| global_trace.stop_count = 0; |
| } |
| goto out; |
| } |
| |
| /* Prevent the buffers from switching */ |
| arch_spin_lock(&global_trace.max_lock); |
| |
| buffer = global_trace.trace_buffer.buffer; |
| if (buffer) |
| ring_buffer_record_enable(buffer); |
| |
| #ifdef CONFIG_TRACER_MAX_TRACE |
| buffer = global_trace.max_buffer.buffer; |
| if (buffer) |
| ring_buffer_record_enable(buffer); |
| #endif |
| |
| arch_spin_unlock(&global_trace.max_lock); |
| |
| out: |
| raw_spin_unlock_irqrestore(&global_trace.start_lock, flags); |
| } |
| |
| static void tracing_start_tr(struct trace_array *tr) |
| { |
| struct ring_buffer *buffer; |
| unsigned long flags; |
| |
| if (tracing_disabled) |
| return; |
| |
| /* If global, we need to also start the max tracer */ |
| if (tr->flags & TRACE_ARRAY_FL_GLOBAL) |
| return tracing_start(); |
| |
| raw_spin_lock_irqsave(&tr->start_lock, flags); |
| |
| if (--tr->stop_count) { |
| if (tr->stop_count < 0) { |
| /* Someone screwed up their debugging */ |
| WARN_ON_ONCE(1); |
| tr->stop_count = 0; |
| } |
| goto out; |
| } |
| |
| buffer = tr->trace_buffer.buffer; |
| if (buffer) |
| ring_buffer_record_enable(buffer); |
| |
| out: |
| raw_spin_unlock_irqrestore(&tr->start_lock, flags); |
| } |
| |
| /** |
| * tracing_stop - quick stop of the tracer |
| * |
| * Light weight way to stop tracing. Use in conjunction with |
| * tracing_start. |
| */ |
| void tracing_stop(void) |
| { |
| struct ring_buffer *buffer; |
| unsigned long flags; |
| |
| raw_spin_lock_irqsave(&global_trace.start_lock, flags); |
| if (global_trace.stop_count++) |
| goto out; |
| |
| /* Prevent the buffers from switching */ |
| arch_spin_lock(&global_trace.max_lock); |
| |
| buffer = global_trace.trace_buffer.buffer; |
| if (buffer) |
| ring_buffer_record_disable(buffer); |
| |
| #ifdef CONFIG_TRACER_MAX_TRACE |
| buffer = global_trace.max_buffer.buffer; |
| if (buffer) |
| ring_buffer_record_disable(buffer); |
| #endif |
| |
| arch_spin_unlock(&global_trace.max_lock); |
| |
| out: |
| raw_spin_unlock_irqrestore(&global_trace.start_lock, flags); |
| } |
| |
| static void tracing_stop_tr(struct trace_array *tr) |
| { |
| struct ring_buffer *buffer; |
| unsigned long flags; |
| |
| /* If global, we need to also stop the max tracer */ |
| if (tr->flags & TRACE_ARRAY_FL_GLOBAL) |
| return tracing_stop(); |
| |
| raw_spin_lock_irqsave(&tr->start_lock, flags); |
| if (tr->stop_count++) |
| goto out; |
| |
| buffer = tr->trace_buffer.buffer; |
| if (buffer) |
| ring_buffer_record_disable(buffer); |
| |
| out: |
| raw_spin_unlock_irqrestore(&tr->start_lock, flags); |
| } |
| |
| static int trace_save_cmdline(struct task_struct *tsk) |
| { |
| unsigned pid, idx; |
| |
| /* treat recording of idle task as a success */ |
| if (!tsk->pid) |
| return 1; |
| |
| if (unlikely(tsk->pid > PID_MAX_DEFAULT)) |
| return 0; |
| |
| /* |
| * It's not the end of the world if we don't get |
| * the lock, but we also don't want to spin |
| * nor do we want to disable interrupts, |
| * so if we miss here, then better luck next time. |
| */ |
| if (!arch_spin_trylock(&trace_cmdline_lock)) |
| return 0; |
| |
| idx = savedcmd->map_pid_to_cmdline[tsk->pid]; |
| if (idx == NO_CMDLINE_MAP) { |
| idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num; |
| |
| /* |
| * Check whether the cmdline buffer at idx has a pid |
| * mapped. We are going to overwrite that entry so we |
| * need to clear the map_pid_to_cmdline. Otherwise we |
| * would read the new comm for the old pid. |
| */ |
| pid = savedcmd->map_cmdline_to_pid[idx]; |
| if (pid != NO_CMDLINE_MAP) |
| savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP; |
| |
| savedcmd->map_cmdline_to_pid[idx] = tsk->pid; |
| savedcmd->map_pid_to_cmdline[tsk->pid] = idx; |
| |
| savedcmd->cmdline_idx = idx; |
| } |
| |
| set_cmdline(idx, tsk->comm); |
| |
| arch_spin_unlock(&trace_cmdline_lock); |
| |
| return 1; |
| } |
| |
| static void __trace_find_cmdline(int pid, char comm[]) |
| { |
| unsigned map; |
| |
| if (!pid) { |
| strcpy(comm, "<idle>"); |
| return; |
| } |
| |
| if (WARN_ON_ONCE(pid < 0)) { |
| strcpy(comm, "<XXX>"); |
| return; |
| } |
| |
| if (pid > PID_MAX_DEFAULT) { |
| strcpy(comm, "<...>"); |
| return; |
| } |
| |
| map = savedcmd->map_pid_to_cmdline[pid]; |
| if (map != NO_CMDLINE_MAP) |
| strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN); |
| else |
| strcpy(comm, "<...>"); |
| } |
| |
| void trace_find_cmdline(int pid, char comm[]) |
| { |
| preempt_disable(); |
| arch_spin_lock(&trace_cmdline_lock); |
| |
| __trace_find_cmdline(pid, comm); |
| |
| arch_spin_unlock(&trace_cmdline_lock); |
| preempt_enable(); |
| } |
| |
| int trace_find_tgid(int pid) |
| { |
| if (unlikely(!tgid_map || !pid || pid > PID_MAX_DEFAULT)) |
| return 0; |
| |
| return tgid_map[pid]; |
| } |
| |
| static int trace_save_tgid(struct task_struct *tsk) |
| { |
| /* treat recording of idle task as a success */ |
| if (!tsk->pid) |
| return 1; |
| |
| if (unlikely(!tgid_map || tsk->pid > PID_MAX_DEFAULT)) |
| return 0; |
| |
| tgid_map[tsk->pid] = tsk->tgid; |
| return 1; |
| } |
| |
| static bool tracing_record_taskinfo_skip(int flags) |
| { |
| if (unlikely(!(flags & (TRACE_RECORD_CMDLINE | TRACE_RECORD_TGID)))) |
| return true; |
| if (atomic_read(&trace_record_taskinfo_disabled) || !tracing_is_on()) |
| return true; |
| if (!__this_cpu_read(trace_taskinfo_save)) |
| return true; |
| return false; |
| } |
| |
| /** |
| * tracing_record_taskinfo - record the task info of a task |
| * |
| * @task - task to record |
| * @flags - TRACE_RECORD_CMDLINE for recording comm |
| * - TRACE_RECORD_TGID for recording tgid |
| */ |
| void tracing_record_taskinfo(struct task_struct *task, int flags) |
| { |
| bool done; |
| |
| if (tracing_record_taskinfo_skip(flags)) |
| return; |
| |
| /* |
| * Record as much task information as possible. If some fail, continue |
| * to try to record the others. |
| */ |
| done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(task); |
| done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(task); |
| |
| /* If recording any information failed, retry again soon. */ |
| if (!done) |
| return; |
| |
| __this_cpu_write(trace_taskinfo_save, false); |
| } |
| |
| /** |
| * tracing_record_taskinfo_sched_switch - record task info for sched_switch |
| * |
| * @prev - previous task during sched_switch |
| * @next - next task during sched_switch |
| * @flags - TRACE_RECORD_CMDLINE for recording comm |
| * TRACE_RECORD_TGID for recording tgid |
| */ |
| void tracing_record_taskinfo_sched_switch(struct task_struct *prev, |
| struct task_struct *next, int flags) |
| { |
| bool done; |
| |
| if (tracing_record_taskinfo_skip(flags)) |
| return; |
| |
| /* |
| * Record as much task information as possible. If some fail, continue |
| * to try to record the others. |
| */ |
| done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(prev); |
| done &= !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(next); |
| done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(prev); |
| done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(next); |
| |
| /* If recording any information failed, retry again soon. */ |
| if (!done) |
| return; |
| |
| __this_cpu_write(trace_taskinfo_save, false); |
| } |
| |
| /* Helpers to record a specific task information */ |
| void tracing_record_cmdline(struct task_struct *task) |
| { |
| tracing_record_taskinfo(task, TRACE_RECORD_CMDLINE); |
| } |
| |
| void tracing_record_tgid(struct task_struct *task) |
| { |
| tracing_record_taskinfo(task, TRACE_RECORD_TGID); |
| } |
| |
| /* |
| * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq |
| * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function |
| * simplifies those functions and keeps them in sync. |
| */ |
| enum print_line_t trace_handle_return(struct trace_seq *s) |
| { |
| return trace_seq_has_overflowed(s) ? |
| TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED; |
| } |
| EXPORT_SYMBOL_GPL(trace_handle_return); |
| |
| void |
| tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags, |
| int pc) |
| { |
| struct task_struct *tsk = current; |
| |
| entry->preempt_count = pc & 0xff; |
| entry->pid = (tsk) ? tsk->pid : 0; |
| entry->flags = |
| #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT |
| (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) | |
| #else |
| TRACE_FLAG_IRQS_NOSUPPORT | |
| #endif |
| ((pc & NMI_MASK ) ? TRACE_FLAG_NMI : 0) | |
| ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) | |
| ((pc & SOFTIRQ_OFFSET) ? TRACE_FLAG_SOFTIRQ : 0) | |
| (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) | |
| (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0); |
| } |
| EXPORT_SYMBOL_GPL(tracing_generic_entry_update); |
| |
| struct ring_buffer_event * |
| trace_buffer_lock_reserve(struct ring_buffer *buffer, |
| int type, |
| unsigned long len, |
| unsigned long flags, int pc) |
| { |
| return __trace_buffer_lock_reserve(buffer, type, len, flags, pc); |
| } |
| |
| DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event); |
| DEFINE_PER_CPU(int, trace_buffered_event_cnt); |
| static int trace_buffered_event_ref; |
| |
| /** |
| * trace_buffered_event_enable - enable buffering events |
| * |
| * When events are being filtered, it is quicker to use a temporary |
| * buffer to write the event data into if there's a likely chance |
| * that it will not be committed. The discard of the ring buffer |
| * is not as fast as committing, and is much slower than copying |
| * a commit. |
| * |
| * When an event is to be filtered, allocate per cpu buffers to |
| * write the event data into, and if the event is filtered and discarded |
| * it is simply dropped, otherwise, the entire data is to be committed |
| * in one shot. |
| */ |
| void trace_buffered_event_enable(void) |
| { |
| struct ring_buffer_event *event; |
| struct page *page; |
| int cpu; |
| |
| WARN_ON_ONCE(!mutex_is_locked(&event_mutex)); |
| |
| if (trace_buffered_event_ref++) |
| return; |
| |
| for_each_tracing_cpu(cpu) { |
| page = alloc_pages_node(cpu_to_node(cpu), |
| GFP_KERNEL | __GFP_NORETRY, 0); |
| if (!page) |
| goto failed; |
| |
| event = page_address(page); |
| memset(event, 0, sizeof(*event)); |
| |
| per_cpu(trace_buffered_event, cpu) = event; |
| |
| preempt_disable(); |
| if (cpu == smp_processor_id() && |
| this_cpu_read(trace_buffered_event) != |
| per_cpu(trace_buffered_event, cpu)) |
| WARN_ON_ONCE(1); |
| preempt_enable(); |
| } |
| |
| return; |
| failed: |
| trace_buffered_event_disable(); |
| } |
| |
| static void enable_trace_buffered_event(void *data) |
| { |
| /* Probably not needed, but do it anyway */ |
| smp_rmb(); |
| this_cpu_dec(trace_buffered_event_cnt); |
| } |
| |
| static void disable_trace_buffered_event(void *data) |
| { |
| this_cpu_inc(trace_buffered_event_cnt); |
| } |
| |
| /** |
| * trace_buffered_event_disable - disable buffering events |
| * |
| * When a filter is removed, it is faster to not use the buffered |
| * events, and to commit directly into the ring buffer. Free up |
| * the temp buffers when there are no more users. This requires |
| * special synchronization with current events. |
| */ |
| void trace_buffered_event_disable(void) |
| { |
| int cpu; |
| |
| WARN_ON_ONCE(!mutex_is_locked(&event_mutex)); |
| |
| if (WARN_ON_ONCE(!trace_buffered_event_ref)) |
| return; |
| |
| if (--trace_buffered_event_ref) |
| return; |
| |
| preempt_disable(); |
| /* For each CPU, set the buffer as used. */ |
| smp_call_function_many(tracing_buffer_mask, |
| disable_trace_buffered_event, NULL, 1); |
| preempt_enable(); |
| |
| /* Wait for all current users to finish */ |
| synchronize_sched(); |
| |
| for_each_tracing_cpu(cpu) { |
| free_page((unsigned long)per_cpu(trace_buffered_event, cpu)); |
| per_cpu(trace_buffered_event, cpu) = NULL; |
| } |
| /* |
| * Make sure trace_buffered_event is NULL before clearing |
| * trace_buffered_event_cnt. |
| */ |
| smp_wmb(); |
| |
| preempt_disable(); |
| /* Do the work on each cpu */ |
| smp_call_function_many(tracing_buffer_mask, |
| enable_trace_buffered_event, NULL, 1); |
| preempt_enable(); |
| } |
| |
| static struct ring_buffer *temp_buffer; |
| |
| struct ring_buffer_event * |
| trace_event_buffer_lock_reserve(struct ring_buffer **current_rb, |
| struct trace_event_file *trace_file, |
| int type, unsigned long len, |
| unsigned long flags, int pc) |
| { |
| struct ring_buffer_event *entry; |
| int val; |
| |
| *current_rb = trace_file->tr->trace_buffer.buffer; |
| |
| if ((trace_file->flags & |
| (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) && |
| (entry = this_cpu_read(trace_buffered_event))) { |
| /* Try to use the per cpu buffer first */ |
| val = this_cpu_inc_return(trace_buffered_event_cnt); |
| if (val == 1) { |
| trace_event_setup(entry, type, flags, pc); |
| entry->array[0] = len; |
| return entry; |
| } |
| this_cpu_dec(trace_buffered_event_cnt); |
| } |
| |
| entry = __trace_buffer_lock_reserve(*current_rb, |
| type, len, flags, pc); |
| /* |
| * If tracing is off, but we have triggers enabled |
| * we still need to look at the event data. Use the temp_buffer |
| * to store the trace event for the tigger to use. It's recusive |
| * safe and will not be recorded anywhere. |
| */ |
| if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) { |
| *current_rb = temp_buffer; |
| entry = __trace_buffer_lock_reserve(*current_rb, |
| type, len, flags, pc); |
| } |
| return entry; |
| } |
| EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve); |
| |
| static DEFINE_SPINLOCK(tracepoint_iter_lock); |
| static DEFINE_MUTEX(tracepoint_printk_mutex); |
| |
| static void output_printk(struct trace_event_buffer *fbuffer) |
| { |
| struct trace_event_call *event_call; |
| struct trace_event *event; |
| unsigned long flags; |
| struct trace_iterator *iter = tracepoint_print_iter; |
| |
| /* We should never get here if iter is NULL */ |
| if (WARN_ON_ONCE(!iter)) |
| return; |
| |
| event_call = fbuffer->trace_file->event_call; |
| if (!event_call || !event_call->event.funcs || |
| !event_call->event.funcs->trace) |
| return; |
| |
| event = &fbuffer->trace_file->event_call->event; |
| |
| spin_lock_irqsave(&tracepoint_iter_lock, flags); |
| trace_seq_init(&iter->seq); |
| iter->ent = fbuffer->entry; |
| event_call->event.funcs->trace(iter, 0, event); |
| trace_seq_putc(&iter->seq, 0); |
| printk("%s", iter->seq.buffer); |
| |
| spin_unlock_irqrestore(&tracepoint_iter_lock, flags); |
| } |
| |
| int tracepoint_printk_sysctl(struct ctl_table *table, int write, |
| void __user *buffer, size_t *lenp, |
| loff_t *ppos) |
| { |
| int save_tracepoint_printk; |
| int ret; |
| |
| mutex_lock(&tracepoint_printk_mutex); |
| save_tracepoint_printk = tracepoint_printk; |
| |
| ret = proc_dointvec(table, write, buffer, lenp, ppos); |
| |
| /* |
| * This will force exiting early, as tracepoint_printk |
| * is always zero when tracepoint_printk_iter is not allocated |
| */ |
| if (!tracepoint_print_iter) |
| tracepoint_printk = 0; |
| |
| if (save_tracepoint_printk == tracepoint_printk) |
| goto out; |
| |
| if (tracepoint_printk) |
| static_key_enable(&tracepoint_printk_key.key); |
| else |
| static_key_disable(&tracepoint_printk_key.key); |
| |
| out: |
| mutex_unlock(&tracepoint_printk_mutex); |
| |
| return ret; |
| } |
| |
| void trace_event_buffer_commit(struct trace_event_buffer *fbuffer) |
| { |
| if (static_key_false(&tracepoint_printk_key.key)) |
| output_printk(fbuffer); |
| |
| event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer, |
| fbuffer->event, fbuffer->entry, |
| fbuffer->flags, fbuffer->pc); |
| } |
| EXPORT_SYMBOL_GPL(trace_event_buffer_commit); |
| |
| void trace_buffer_unlock_commit_regs(struct trace_array *tr, |
| struct ring_buffer *buffer, |
| struct ring_buffer_event *event, |
| unsigned long flags, int pc, |
| struct pt_regs *regs) |
| { |
| __buffer_unlock_commit(buffer, event); |
| |
| /* |
| * If regs is not set, then skip the following callers: |
| * trace_buffer_unlock_commit_regs |
| * event_trigger_unlock_commit |
| * trace_event_buffer_commit |
| * trace_event_raw_event_sched_switch |
| * Note, we can still get here via blktrace, wakeup tracer |
| * and mmiotrace, but that's ok if they lose a function or |
| * two. They are that meaningful. |
| */ |
| ftrace_trace_stack(tr, buffer, flags, regs ? 0 : 4, pc, regs); |
| ftrace_trace_userstack(buffer, flags, pc); |
| } |
| |
| /* |
| * Similar to trace_buffer_unlock_commit_regs() but do not dump stack. |
| */ |
| void |
| trace_buffer_unlock_commit_nostack(struct ring_buffer *buffer, |
| struct ring_buffer_event *event) |
| { |
| __buffer_unlock_commit(buffer, event); |
| } |
| |
| static void |
| trace_process_export(struct trace_export *export, |
| struct ring_buffer_event *event) |
| { |
| struct trace_entry *entry; |
| unsigned int size = 0; |
| |
| entry = ring_buffer_event_data(event); |
| size = ring_buffer_event_length(event); |
| export->write(entry, size); |
| } |
| |
| static DEFINE_MUTEX(ftrace_export_lock); |
| |
| static struct trace_export __rcu *ftrace_exports_list __read_mostly; |
| |
| static DEFINE_STATIC_KEY_FALSE(ftrace_exports_enabled); |
| |
| static inline void ftrace_exports_enable(void) |
| { |
| static_branch_enable(&ftrace_exports_enabled); |
| } |
| |
| static inline void ftrace_exports_disable(void) |
| { |
| static_branch_disable(&ftrace_exports_enabled); |
| } |
| |
| void ftrace_exports(struct ring_buffer_event *event) |
| { |
| struct trace_export *export; |
| |
| preempt_disable_notrace(); |
| |
| export = rcu_dereference_raw_notrace(ftrace_exports_list); |
| while (export) { |
| trace_process_export(export, event); |
| export = rcu_dereference_raw_notrace(export->next); |
| } |
| |
| preempt_enable_notrace(); |
| } |
| |
| static inline void |
| add_trace_export(struct trace_export **list, struct trace_export *export) |
| { |
| rcu_assign_pointer(export->next, *list); |
| /* |
| * We are entering export into the list but another |
| * CPU might be walking that list. We need to make sure |
| * the export->next pointer is valid before another CPU sees |
| * the export pointer included into the list. |
| */ |
| rcu_assign_pointer(*list, export); |
| } |
| |
| static inline int |
| rm_trace_export(struct trace_export **list, struct trace_export *export) |
| { |
| struct trace_export **p; |
| |
| for (p = list; *p != NULL; p = &(*p)->next) |
| if (*p == export) |
| break; |
| |
| if (*p != export) |
| return -1; |
| |
| rcu_assign_pointer(*p, (*p)->next); |
| |
| return 0; |
| } |
| |
| static inline void |
| add_ftrace_export(struct trace_export **list, struct trace_export *export) |
| { |
| if (*list == NULL) |
| ftrace_exports_enable(); |
| |
| add_trace_export(list, export); |
| } |
| |
| static inline int |
| rm_ftrace_export(struct trace_export **list, struct trace_export *export) |
| { |
| int ret; |
| |
| ret = rm_trace_export(list, export); |
| if (*list == NULL) |
| ftrace_exports_disable(); |
| |
| return ret; |
| } |
| |
| int register_ftrace_export(struct trace_export *export) |
| { |
| if (WARN_ON_ONCE(!export->write)) |
| return -1; |
| |
| mutex_lock(&ftrace_export_lock); |
| |
| add_ftrace_export(&ftrace_exports_list, export); |
| |
| mutex_unlock(&ftrace_export_lock); |
| |
| return 0; |
| } |
| EXPORT_SYMBOL_GPL(register_ftrace_export); |
| |
| int unregister_ftrace_export(struct trace_export *export) |
| { |
| int ret; |
| |
| mutex_lock(&ftrace_export_lock); |
| |
| ret = rm_ftrace_export(&ftrace_exports_list, export); |
| |
| mutex_unlock(&ftrace_export_lock); |
| |
| return ret; |
| } |
| EXPORT_SYMBOL_GPL(unregister_ftrace_export); |
| |
| void |
| trace_function(struct trace_array *tr, |
| unsigned long ip, unsigned long parent_ip, unsigned long flags, |
| int pc) |
| { |
| struct trace_event_call *call = &event_function; |
| struct ring_buffer *buffer = tr->trace_buffer.buffer; |
| struct ring_buffer_event *event; |
| struct ftrace_entry *entry; |
| |
| event = __trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry), |
| flags, pc); |
| if (!event) |
| return; |
| entry = ring_buffer_event_data(event); |
| entry->ip = ip; |
| entry->parent_ip = parent_ip; |
| |
| if (!call_filter_check_discard(call, entry, buffer, event)) { |
| if (static_branch_unlikely(&ftrace_exports_enabled)) |
| ftrace_exports(event); |
| __buffer_unlock_commit(buffer, event); |
| } |
| } |
| |
| #ifdef CONFIG_STACKTRACE |
| |
| #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long)) |
| struct ftrace_stack { |
| unsigned long calls[FTRACE_STACK_MAX_ENTRIES]; |
| }; |
| |
| static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack); |
| static DEFINE_PER_CPU(int, ftrace_stack_reserve); |
| |
| static void __ftrace_trace_stack(struct ring_buffer *buffer, |
| unsigned long flags, |
| int skip, int pc, struct pt_regs *regs) |
| { |
| struct trace_event_call *call = &event_kernel_stack; |
| struct ring_buffer_event *event; |
| struct stack_entry *entry; |
| struct stack_trace trace; |
| int use_stack; |
| int size = FTRACE_STACK_ENTRIES; |
| |
| trace.nr_entries = 0; |
| trace.skip = skip; |
| |
| /* |
| * Add two, for this function and the call to save_stack_trace() |
| * If regs is set, then these functions will not be in the way. |
| */ |
| if (!regs) |
| trace.skip += 2; |
| |
| /* |
| * Since events can happen in NMIs there's no safe way to |
| * use the per cpu ftrace_stacks. We reserve it and if an interrupt |
| * or NMI comes in, it will just have to use the default |
| * FTRACE_STACK_SIZE. |
| */ |
| preempt_disable_notrace(); |
| |
| use_stack = __this_cpu_inc_return(ftrace_stack_reserve); |
| /* |
| * We don't need any atomic variables, just a barrier. |
| * If an interrupt comes in, we don't care, because it would |
| * have exited and put the counter back to what we want. |
| * We just need a barrier to keep gcc from moving things |
| * around. |
| */ |
| barrier(); |
| if (use_stack == 1) { |
| trace.entries = this_cpu_ptr(ftrace_stack.calls); |
| trace.max_entries = FTRACE_STACK_MAX_ENTRIES; |
| |
| if (regs) |
| save_stack_trace_regs(regs, &trace); |
| else |
| save_stack_trace(&trace); |
| |
| if (trace.nr_entries > size) |
| size = trace.nr_entries; |
| } else |
| /* From now on, use_stack is a boolean */ |
| use_stack = 0; |
| |
| size *= sizeof(unsigned long); |
| |
| event = __trace_buffer_lock_reserve(buffer, TRACE_STACK, |
| sizeof(*entry) + size, flags, pc); |
| if (!event) |
| goto out; |
| entry = ring_buffer_event_data(event); |
| |
| memset(&entry->caller, 0, size); |
| |
| if (use_stack) |
| memcpy(&entry->caller, trace.entries, |
| trace.nr_entries * sizeof(unsigned long)); |
| else { |
| trace.max_entries = FTRACE_STACK_ENTRIES; |
| trace.entries = entry->caller; |
| if (regs) |
| save_stack_trace_regs(regs, &trace); |
| else |
| save_stack_trace(&trace); |
| } |
| |
| entry->size = trace.nr_entries; |
| |
| if (!call_filter_check_discard(call, entry, buffer, event)) |
| __buffer_unlock_commit(buffer, event); |
| |
| out: |
| /* Again, don't let gcc optimize things here */ |
| barrier(); |
| __this_cpu_dec(ftrace_stack_reserve); |
| preempt_enable_notrace(); |
| |
| } |
| |
| static inline void ftrace_trace_stack(struct trace_array *tr, |
| struct ring_buffer *buffer, |
| unsigned long flags, |
| int skip, int pc, struct pt_regs *regs) |
| { |
| if (!(tr->trace_flags & TRACE_ITER_STACKTRACE)) |
| return; |
| |
| __ftrace_trace_stack(buffer, flags, skip, pc, regs); |
| } |
| |
| void __trace_stack(struct trace_array *tr, unsigned long flags, int skip, |
| int pc) |
| { |
| struct ring_buffer *buffer = tr->trace_buffer.buffer; |
| |
| if (rcu_is_watching()) { |
| __ftrace_trace_stack(buffer, flags, skip, pc, NULL); |
| return; |
| } |
| |
| /* |
| * When an NMI triggers, RCU is enabled via rcu_nmi_enter(), |
| * but if the above rcu_is_watching() failed, then the NMI |
| * triggered someplace critical, and rcu_irq_enter() should |
| * not be called from NMI. |
| */ |
| if (unlikely(in_nmi())) |
| return; |
| |
| /* |
| * It is possible that a function is being traced in a |
| * location that RCU is not watching. A call to |
| * rcu_irq_enter() will make sure that it is, but there's |
| * a few internal rcu functions that could be traced |
| * where that wont work either. In those cases, we just |
| * do nothing. |
| */ |
| if (unlikely(rcu_irq_enter_disabled())) |
| return; |
| |
| rcu_irq_enter_irqson(); |
| __ftrace_trace_stack(buffer, flags, skip, pc, NULL); |
| rcu_irq_exit_irqson(); |
| } |
| |
| /** |
| * trace_dump_stack - record a stack back trace in the trace buffer |
| * @skip: Number of functions to skip (helper handlers) |
| */ |
| void trace_dump_stack(int skip) |
| { |
| unsigned long flags; |
| |
| if (tracing_disabled || tracing_selftest_running) |
| return; |
| |
| local_save_flags(flags); |
| |
| /* |
| * Skip 3 more, seems to get us at the caller of |
| * this function. |
| */ |
| skip += 3; |
| __ftrace_trace_stack(global_trace.trace_buffer.buffer, |
| flags, skip, preempt_count(), NULL); |
| } |
| |
| static DEFINE_PER_CPU(int, user_stack_count); |
| |
| void |
| ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc) |
| { |
| struct trace_event_call *call = &event_user_stack; |
| struct ring_buffer_event *event; |
| struct userstack_entry *entry; |
| struct stack_trace trace; |
| |
| if (!(global_trace.trace_flags & TRACE_ITER_USERSTACKTRACE)) |
| return; |
| |
| /* |
| * NMIs can not handle page faults, even with fix ups. |
| * The save user stack can (and often does) fault. |
| */ |
| if (unlikely(in_nmi())) |
| return; |
| |
| /* |
| * prevent recursion, since the user stack tracing may |
| * trigger other kernel events. |
| */ |
| preempt_disable(); |
| if (__this_cpu_read(user_stack_count)) |
| goto out; |
| |
| __this_cpu_inc(user_stack_count); |
| |
| event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK, |
| sizeof(*entry), flags, pc); |
| if (!event) |
| goto out_drop_count; |
| entry = ring_buffer_event_data(event); |
| |
| entry->tgid = current->tgid; |
| memset(&entry->caller, 0, sizeof(entry->caller)); |
| |
| trace.nr_entries = 0; |
| trace.max_entries = FTRACE_STACK_ENTRIES; |
| trace.skip = 0; |
| trace.entries = entry->caller; |
| |
| save_stack_trace_user(&trace); |
| if (!call_filter_check_discard(call, entry, buffer, event)) |
| __buffer_unlock_commit(buffer, event); |
| |
| out_drop_count: |
| __this_cpu_dec(user_stack_count); |
| out: |
| preempt_enable(); |
| } |
| |
| #ifdef UNUSED |
| static void __trace_userstack(struct trace_array *tr, unsigned long flags) |
| { |
| ftrace_trace_userstack(tr, flags, preempt_count()); |
| } |
| #endif /* UNUSED */ |
| |
| #endif /* CONFIG_STACKTRACE */ |
| |
| /* created for use with alloc_percpu */ |
| struct trace_buffer_struct { |
| int nesting; |
| char buffer[4][TRACE_BUF_SIZE]; |
| }; |
| |
| static struct trace_buffer_struct *trace_percpu_buffer; |
| |
| /* |
| * Thise allows for lockless recording. If we're nested too deeply, then |
| * this returns NULL. |
| */ |
| static char *get_trace_buf(void) |
| { |
| struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer); |
| |
| if (!buffer || buffer->nesting >= 4) |
| return NULL; |
| |
| buffer->nesting++; |
| |
| /* Interrupts must see nesting incremented before we use the buffer */ |
| barrier(); |
| return &buffer->buffer[buffer->nesting][0]; |
| } |
| |
| static void put_trace_buf(void) |
| { |
| /* Don't let the decrement of nesting leak before this */ |
| barrier(); |
| this_cpu_dec(trace_percpu_buffer->nesting); |
| } |
| |
| static int alloc_percpu_trace_buffer(void) |
| { |
| struct trace_buffer_struct *buffers; |
| |
| buffers = alloc_percpu(struct trace_buffer_struct); |
| if (WARN(!buffers, "Could not allocate percpu trace_printk buffer")) |
| return -ENOMEM; |
| |
| trace_percpu_buffer = buffers; |
| return 0; |
| } |
| |
| static int buffers_allocated; |
| |
| void trace_printk_init_buffers(void) |
| { |
| if (buffers_allocated) |
| return; |
| |
| if (alloc_percpu_trace_buffer()) |
| return; |
| |
| /* trace_printk() is for debug use only. Don't use it in production. */ |
| |
| pr_warn("\n"); |
| pr_warn("**********************************************************\n"); |
| pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); |
| pr_warn("** **\n"); |
| pr_warn("** trace_printk() being used. Allocating extra memory. **\n"); |
| pr_warn("** **\n"); |
| pr_warn("** This means that this is a DEBUG kernel and it is **\n"); |
| pr_warn("** unsafe for production use. **\n"); |
| pr_warn("** **\n"); |
| pr_warn("** If you see this message and you are not debugging **\n"); |
| pr_warn("** the kernel, report this immediately to your vendor! **\n"); |
| pr_warn("** **\n"); |
| pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); |
| pr_warn("**********************************************************\n"); |
| |
| /* Expand the buffers to set size */ |
| tracing_update_buffers(); |
| |
| buffers_allocated = 1; |
| |
| /* |
| * trace_printk_init_buffers() can be called by modules. |
| * If that happens, then we need to start cmdline recording |
| * directly here. If the global_trace.buffer is already |
| * allocated here, then this was called by module code. |
| */ |
| if (global_trace.trace_buffer.buffer) |
| tracing_start_cmdline_record(); |
| } |
| |
| void trace_printk_start_comm(void) |
| { |
| /* Start tracing comms if trace printk is set */ |
| if (!buffers_allocated) |
| return; |
| tracing_start_cmdline_record(); |
| } |
| |
| static void trace_printk_start_stop_comm(int enabled) |
| { |
| if (!buffers_allocated) |
| return; |
| |
| if (enabled) |
| tracing_start_cmdline_record(); |
| else |
| tracing_stop_cmdline_record(); |
| } |
| |
| /** |
| * trace_vbprintk - write binary msg to tracing buffer |
| * |
| */ |
| int trace_vbprintk(unsigned long ip, const char *fmt, va_list args) |
| { |
| struct trace_event_call *call = &event_bprint; |
| struct ring_buffer_event *event; |
| struct ring_buffer *buffer; |
| struct trace_array *tr = &global_trace; |
| struct bprint_entry *entry; |
| unsigned long flags; |
| char *tbuffer; |
| int len = 0, size, pc; |
| |
| if (unlikely(tracing_selftest_running || tracing_disabled)) |
| return 0; |
| |
| /* Don't pollute graph traces with trace_vprintk internals */ |
| pause_graph_tracing(); |
| |
| pc = preempt_count(); |
| preempt_disable_notrace(); |
| |
| tbuffer = get_trace_buf(); |
| if (!tbuffer) { |
| len = 0; |
| goto out_nobuffer; |
| } |
| |
| len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args); |
| |
| if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0) |
| goto out; |
| |
| local_save_flags(flags); |
| size = sizeof(*entry) + sizeof(u32) * len; |
| buffer = tr->trace_buffer.buffer; |
| event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size, |
| flags, pc); |
| if (!event) |
| goto out; |
| entry = ring_buffer_event_data(event); |
| entry->ip = ip; |
| entry->fmt = fmt; |
| |
| memcpy(entry->buf, tbuffer, sizeof(u32) * len); |
| if (!call_filter_check_discard(call, entry, buffer, event)) { |
| __buffer_unlock_commit(buffer, event); |
| ftrace_trace_stack(tr, buffer, flags, 6, pc, NULL); |
| } |
| |
| out: |
| put_trace_buf(); |
| |
| out_nobuffer: |
| preempt_enable_notrace(); |
| unpause_graph_tracing(); |
| |
| return len; |
| } |
| EXPORT_SYMBOL_GPL(trace_vbprintk); |
| |
| static int |
| __trace_array_vprintk(struct ring_buffer *buffer, |
| unsigned long ip, const char *fmt, va_list args) |
| { |
| struct trace_event_call *call = &event_print; |
| struct ring_buffer_event *event; |
| int len = 0, size, pc; |
| struct print_entry *entry; |
| unsigned long flags; |
| char *tbuffer; |
| |
| if (tracing_disabled || tracing_selftest_running) |
| return 0; |
| |
| /* Don't pollute graph traces with trace_vprintk internals */ |
| pause_graph_tracing(); |
| |
| pc = preempt_count(); |
| preempt_disable_notrace(); |
| |
| |
| tbuffer = get_trace_buf(); |
| if (!tbuffer) { |
| len = 0; |
| goto out_nobuffer; |
| } |
| |
| len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args); |
| |
| local_save_flags(flags); |
| size = sizeof(*entry) + len + 1; |
| event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size, |
| flags, pc); |
| if (!event) |
| goto out; |
| entry = ring_buffer_event_data(event); |
| entry->ip = ip; |
| |
| memcpy(&entry->buf, tbuffer, len + 1); |
| if (!call_filter_check_discard(call, entry, buffer, event)) { |
| __buffer_unlock_commit(buffer, event); |
| ftrace_trace_stack(&global_trace, buffer, flags, 6, pc, NULL); |
| } |
| |
| out: |
| put_trace_buf(); |
| |
| out_nobuffer: |
| preempt_enable_notrace(); |
| unpause_graph_tracing(); |
| |
| return len; |
| } |
| |
| int trace_array_vprintk(struct trace_array *tr, |
| unsigned long ip, const char *fmt, va_list args) |
| { |
| return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args); |
| } |
| |
| int trace_array_printk(struct trace_array *tr, |
| unsigned long ip, const char *fmt, ...) |
| { |
| int ret; |
| va_list ap; |
| |
| if (!(global_trace.trace_flags & TRACE_ITER_PRINTK)) |
| return 0; |
| |
| va_start(ap, fmt); |
| ret = trace_array_vprintk(tr, ip, fmt, ap); |
| va_end(ap); |
| return ret; |
| } |
| |
| int trace_array_printk_buf(struct ring_buffer *buffer, |
| unsigned long ip, const char *fmt, ...) |
| { |
| int ret; |
| va_list ap; |
| |
| if (!(global_trace.trace_flags & TRACE_ITER_PRINTK)) |
| return 0; |
| |
| va_start(ap, fmt); |
| ret = __trace_array_vprintk(buffer, ip, fmt, ap); |
| va_end(ap); |
| return ret; |
| } |
| |
| int trace_vprintk(unsigned long ip, const char *fmt, va_list args) |
| { |
| return trace_array_vprintk(&global_trace, ip, fmt, args); |
| } |
| EXPORT_SYMBOL_GPL(trace_vprintk); |
| |
| static void trace_iterator_increment(struct trace_iterator *iter) |
| { |
| struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu); |
| |
| iter->idx++; |
| if (buf_iter) |
| ring_buffer_read(buf_iter, NULL); |
| } |
| |
| static struct trace_entry * |
| peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts, |
| unsigned long *lost_events) |
| { |
| struct ring_buffer_event *event; |
| struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu); |
| |
| if (buf_iter) |
| event = ring_buffer_iter_peek(buf_iter, ts); |
| else |
| event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts, |
| lost_events); |
| |
| if (event) { |
| iter->ent_size = ring_buffer_event_length(event); |
| return ring_buffer_event_data(event); |
| } |
| iter->ent_size = 0; |
| return NULL; |
| } |
| |
| static struct trace_entry * |
| __find_next_entry(struct trace_iterator *iter, int *ent_cpu, |
| unsigned long *missing_events, u64 *ent_ts) |
| { |
| struct ring_buffer *buffer = iter->trace_buffer->buffer; |
| struct trace_entry *ent, *next = NULL; |
| unsigned long lost_events = 0, next_lost = 0; |
| int cpu_file = iter->cpu_file; |
| u64 next_ts = 0, ts; |
| int next_cpu = -1; |
| int next_size = 0; |
| int cpu; |
| |
| /* |
| * If we are in a per_cpu trace file, don't bother by iterating over |
| * all cpu and peek directly. |
| */ |
| if (cpu_file > RING_BUFFER_ALL_CPUS) { |
| if (ring_buffer_empty_cpu(buffer, cpu_file)) |
| return NULL; |
| ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events); |
| if (ent_cpu) |
| *ent_cpu = cpu_file; |
| |
| return ent; |
| } |
| |
| for_each_tracing_cpu(cpu) { |
| |
| if (ring_buffer_empty_cpu(buffer, cpu)) |
| continue; |
| |
| ent = peek_next_entry(iter, cpu, &ts, &lost_events); |
| |
| /* |
| * Pick the entry with the smallest timestamp: |
| */ |
| if (ent && (!next || ts < next_ts)) { |
| next = ent; |
| next_cpu = cpu; |
| next_ts = ts; |
| next_lost = lost_events; |
| next_size = iter->ent_size; |
| } |
| } |
| |
| iter->ent_size = next_size; |
| |
| if (ent_cpu) |
| *ent_cpu = next_cpu; |
| |
| if (ent_ts) |
| *ent_ts = next_ts; |
| |
| if (missing_events) |
| *missing_events = next_lost; |
| |
| return next; |
| } |
| |
| /* Find the next real entry, without updating the iterator itself */ |
| struct trace_entry *trace_find_next_entry(struct trace_iterator *iter, |
| int *ent_cpu, u64 *ent_ts) |
| { |
| return __find_next_entry(iter, ent_cpu, NULL, ent_ts); |
| } |
| |
| /* Find the next real entry, and increment the iterator to the next entry */ |
| void *trace_find_next_entry_inc(struct trace_iterator *iter) |
| { |
| iter->ent = __find_next_entry(iter, &iter->cpu, |
| &iter->lost_events, &iter->ts); |
| |
| if (iter->ent) |
| trace_iterator_increment(iter); |
| |
| return iter->ent ? iter : NULL; |
| } |
| |
| static void trace_consume(struct trace_iterator *iter) |
| { |
| ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts, |
| &iter->lost_events); |
| } |
| |
| static void *s_next(struct seq_file *m, void *v, loff_t *pos) |
| { |
| struct trace_iterator *iter = m->private; |
| int i = (int)*pos; |
| void *ent; |
| |
| WARN_ON_ONCE(iter->leftover); |
| |
| (*pos)++; |
| |
| /* can't go backwards */ |
| if (iter->idx > i) |
| return NULL; |
| |
| if (iter->idx < 0) |
| ent = trace_find_next_entry_inc(iter); |
| else |
| ent = iter; |
| |
| while (ent && iter->idx < i) |
| ent = trace_find_next_entry_inc(iter); |
| |
| iter->pos = *pos; |
| |
| return ent; |
| } |
| |
| void tracing_iter_reset(struct trace_iterator *iter, int cpu) |
| { |
| struct ring_buffer_event *event; |
| struct ring_buffer_iter *buf_iter; |
| unsigned long entries = 0; |
| u64 ts; |
| |
| per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0; |
| |
| buf_iter = trace_buffer_iter(iter, cpu); |
| if (!buf_iter) |
| return; |
| |
| ring_buffer_iter_reset(buf_iter); |
| |
| /* |
| * We could have the case with the max latency tracers |
| * that a reset never took place on a cpu. This is evident |
| * by the timestamp being before the start of the buffer. |
| */ |
| while ((event = ring_buffer_iter_peek(buf_iter, &ts))) { |
| if (ts >= iter->trace_buffer->time_start) |
| break; |
| entries++; |
| ring_buffer_read(buf_iter, NULL); |
| } |
| |
| per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries; |
| } |
| |
| /* |
| * The current tracer is copied to avoid a global locking |
| * all around. |
| */ |
| static void *s_start(struct seq_file *m, loff_t *pos) |
| { |
| struct trace_iterator *iter = m->private; |
| struct trace_array *tr = iter->tr; |
| int cpu_file = iter->cpu_file; |
| void *p = NULL; |
| loff_t l = 0; |
| int cpu; |
| |
| /* |
| * copy the tracer to avoid using a global lock all around. |
| * iter->trace is a copy of current_trace, the pointer to the |
| * name may be used instead of a strcmp(), as iter->trace->name |
| * will point to the same string as current_trace->name. |
| */ |
| mutex_lock(&trace_types_lock); |
| if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name)) |
| *iter->trace = *tr->current_trace; |
| mutex_unlock(&trace_types_lock); |
| |
| #ifdef CONFIG_TRACER_MAX_TRACE |
| if (iter->snapshot && iter->trace->use_max_tr) |
| return ERR_PTR(-EBUSY); |
| #endif |
| |
| if (!iter->snapshot) |
| atomic_inc(&trace_record_taskinfo_disabled); |
| |
| if (*pos != iter->pos) { |
| iter->ent = NULL; |
| iter->cpu = 0; |
| iter->idx = -1; |
| |
| if (cpu_file == RING_BUFFER_ALL_CPUS) { |
| for_each_tracing_cpu(cpu) |
| tracing_iter_reset(iter, cpu); |
| } else |
| tracing_iter_reset(iter, cpu_file); |
| |
| iter->leftover = 0; |
| for (p = iter; p && l < *pos; p = s_next(m, p, &l)) |
| ; |
| |
| } else { |
| /* |
| * If we overflowed the seq_file before, then we want |
| * to just reuse the trace_seq buffer again. |
| */ |
| if (iter->leftover) |
| p = iter; |
| else { |
| l = *pos - 1; |
| p = s_next(m, p, &l); |
| } |
| } |
| |
| trace_event_read_lock(); |
| trace_access_lock(cpu_file); |
| return p; |
| } |
| |
| static void s_stop(struct seq_file *m, void *p) |
| { |
| struct trace_iterator *iter = m->private; |
| |
| #ifdef CONFIG_TRACER_MAX_TRACE |
| if (iter->snapshot && iter->trace->use_max_tr) |
| return; |
| #endif |
| |
| if (!iter->snapshot) |
| atomic_dec(&trace_record_taskinfo_disabled); |
| |
| trace_access_unlock(iter->cpu_file); |
| trace_event_read_unlock(); |
| } |
| |
| static void |
| get_total_entries(struct trace_buffer *buf, |
| unsigned long *total, unsigned long *entries) |
| { |
| unsigned long count; |
| int cpu; |
| |
| *total = 0; |
| *entries = 0; |
| |
| for_each_tracing_cpu(cpu) { |
| count = ring_buffer_entries_cpu(buf->buffer, cpu); |
| /* |
| * If this buffer has skipped entries, then we hold all |
| * entries for the trace and we need to ignore the |
| * ones before the time stamp. |
| */ |
| if (per_cpu_ptr(buf->data, cpu)->skipped_entries) { |
| count -= per_cpu_ptr(buf->data, cpu)->skipped_entries; |
| /* total is the same as the entries */ |
| *total += count; |
| } else |
| *total += count + |
| ring_buffer_overrun_cpu(buf->buffer, cpu); |
| *entries += count; |
| } |
| } |
| |
| static void print_lat_help_header(struct seq_file *m) |
| { |
| seq_puts(m, "# _------=> CPU# \n" |
| "# / _-----=> irqs-off \n" |
| "# | / _----=> need-resched \n" |
| "# || / _---=> hardirq/softirq \n" |
| "# ||| / _--=> preempt-depth \n" |
| "# |||| / delay \n" |
| "# cmd pid ||||| time | caller \n" |
| "# \\ / ||||| \\ | / \n"); |
| } |
| |
| static void print_event_info(struct trace_buffer *buf, struct seq_file *m) |
| { |
| unsigned long total; |
| unsigned long entries; |
| |
| get_total_entries(buf, &total, &entries); |
| seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n", |
| entries, total, num_online_cpus()); |
| seq_puts(m, "#\n"); |
| } |
| |
| static |