10017 lines · 246.6 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* * 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 <linux/utsname.h>#include <linux/stacktrace.h>#include <linux/writeback.h>#include <linux/kallsyms.h>#include <linux/security.h>#include <linux/seq_file.h>#include <linux/irqflags.h>#include <linux/syscalls.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/cleanup.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/panic_notifier.h>#include <linux/poll.h>#include <linux/nmi.h>#include <linux/fs.h>#include <linux/trace.h>#include <linux/sched/clock.h>#include <linux/sched/rt.h>#include <linux/irq_work.h>#include <linux/workqueue.h>#include <linux/sort.h>#include <linux/io.h> /* vmap_page_range() */#include <linux/fs_context.h> #include <asm/setup.h> /* COMMAND_LINE_SIZE */ #include "trace.h"#include "trace_output.h" #ifdef CONFIG_FTRACE_STARTUP_TEST/* * 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. */bool __read_mostly tracing_selftest_running; /* * If boot-time tracing including tracers/events via kernel cmdline * is running, we do not want to run SELFTEST. */bool __read_mostly tracing_selftest_disabled; void __init disable_tracing_selftest(const char *reason){	if (!tracing_selftest_disabled) {		tracing_selftest_disabled = true;		pr_info("Ftrace startup test is disabled due to %s\n", reason);	}}#else#define tracing_selftest_disabled	0#endif /* Pipe tracepoints to printk */static struct trace_iterator *tracepoint_print_iter;static int tracepoint_printk;static bool tracepoint_printk_stop_on_boot __initdata;static bool traceoff_after_boot __initdata;static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key); /* Store tracers and their flags per instance */struct tracers {	struct list_head	list;	struct tracer		*tracer;	struct tracer_flags	*flags;}; /* * To prevent the comm cache from being overwritten when no * tracing is active, only save the comm when a trace event * occurred. */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. */int tracing_disabled = 1; cpumask_var_t __read_mostly	tracing_buffer_mask; #define MAX_TRACER_SIZE		100/* * 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 outputting 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 * Set instance name if you want to dump the specific trace instance * Multiple instance dump is also supported, and instances are separated * by commas. *//* Set to string format zero to disable by default */static char ftrace_dump_on_oops[MAX_TRACER_SIZE] = "0"; /* When set, tracing will stop when a WARN*() is hit */static int __disable_trace_on_warning; int tracepoint_printk_sysctl(const struct ctl_table *table, int write,			     void *buffer, size_t *lenp, loff_t *ppos);static const struct ctl_table trace_sysctl_table[] = {	{		.procname	= "ftrace_dump_on_oops",		.data		= &ftrace_dump_on_oops,		.maxlen		= MAX_TRACER_SIZE,		.mode		= 0644,		.proc_handler	= proc_dostring,	},	{		.procname	= "traceoff_on_warning",		.data		= &__disable_trace_on_warning,		.maxlen		= sizeof(__disable_trace_on_warning),		.mode		= 0644,		.proc_handler	= proc_dointvec,	},	{		.procname	= "tracepoint_printk",		.data		= &tracepoint_printk,		.maxlen		= sizeof(tracepoint_printk),		.mode		= 0644,		.proc_handler	= tracepoint_printk_sysctl,	},}; static int __init init_trace_sysctls(void){	register_sysctl_init("kernel", trace_sysctl_table);	return 0;}subsys_initcall(init_trace_sysctls); #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 */ int tracing_set_tracer(struct trace_array *tr, const char *buf);static void ftrace_trace_userstack(struct trace_array *tr,				   struct trace_buffer *buffer,				   unsigned int trace_ctx); static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;static char *default_bootup_tracer; static char boot_instance_info[COMMAND_LINE_SIZE] __initdata;static int boot_instance_index; /* * Repeated boot parameters, including Bootconfig array expansions, need * to stay in the delimiter form that the existing parser consumes. */void __init trace_append_boot_param(char *buf, const char *str, char sep,				    int size){	int len, needed, str_len; 	if (!*str)		return; 	len = strlen(buf);	str_len = strlen(str);	needed = len + str_len + 1; 	/* For continuation, account for the separator. */	if (len)		needed++;	if (needed > size)		return; 	if (len)		buf[len++] = sep; 	strscpy(buf + len, str, size - len);} static int __init set_cmdline_ftrace(char *str){	strscpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);	default_bootup_tracer = bootup_tracer_buf;	/* We are using ftrace early, expand it */	trace_set_ring_buffer_expanded(NULL);	return 1;}__setup("ftrace=", set_cmdline_ftrace); int ftrace_dump_on_oops_enabled(void){	if (!strcmp("0", ftrace_dump_on_oops))		return 0;	else		return 1;} static int __init set_ftrace_dump_on_oops(char *str){	if (!*str) {		strscpy(ftrace_dump_on_oops, "1", MAX_TRACER_SIZE);		return 1;	} 	if (*str == ',') {		strscpy(ftrace_dump_on_oops, "1", MAX_TRACER_SIZE);		strscpy(ftrace_dump_on_oops + 1, str, MAX_TRACER_SIZE - 1);		return 1;	} 	if (*str++ == '=') {		strscpy(ftrace_dump_on_oops, str, MAX_TRACER_SIZE);		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_instance(char *str){	char *slot = boot_instance_info + boot_instance_index;	int left = sizeof(boot_instance_info) - boot_instance_index;	int ret; 	if (strlen(str) >= left)		return -1; 	ret = snprintf(slot, left, "%s\t", str);	boot_instance_index += ret; 	return 1;}__setup("trace_instance=", boot_instance);  static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata; static int __init set_trace_boot_options(char *str){	trace_append_boot_param(trace_boot_options_buf, str, ',',				MAX_TRACER_SIZE);	return 1;}__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){	strscpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);	trace_boot_clock = trace_boot_clock_buf;	return 1;}__setup("trace_clock=", set_trace_boot_clock); static int __init set_tracepoint_printk(char *str){	/* Ignore the "tp_printk_stop_on_boot" param */	if (*str == '_')		return 0; 	if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))		tracepoint_printk = 1;	return 1;}__setup("tp_printk", set_tracepoint_printk); static int __init set_tracepoint_printk_stop(char *str){	tracepoint_printk_stop_on_boot = true;	return 1;}__setup("tp_printk_stop_on_boot", set_tracepoint_printk_stop); static int __init set_traceoff_after_boot(char *str){	traceoff_after_boot = true;	return 1;}__setup("traceoff_after_boot", set_traceoff_after_boot); unsigned long long ns2usecs(u64 nsec){	nsec += 500;	do_div(nsec, 1000);	return nsec;} static voidtrace_process_export(struct trace_export *export,	       struct ring_buffer_event *event, int flag){	struct trace_entry *entry;	unsigned int size = 0; 	if (export->flags & flag) {		entry = ring_buffer_event_data(event);		size = ring_buffer_event_length(event);		export->write(export, entry, size);	}} static DEFINE_MUTEX(ftrace_export_lock); static struct trace_export __rcu *ftrace_exports_list __read_mostly; static DEFINE_STATIC_KEY_FALSE(trace_function_exports_enabled);static DEFINE_STATIC_KEY_FALSE(trace_event_exports_enabled);static DEFINE_STATIC_KEY_FALSE(trace_marker_exports_enabled); static inline void ftrace_exports_enable(struct trace_export *export){	if (export->flags & TRACE_EXPORT_FUNCTION)		static_branch_inc(&trace_function_exports_enabled); 	if (export->flags & TRACE_EXPORT_EVENT)		static_branch_inc(&trace_event_exports_enabled); 	if (export->flags & TRACE_EXPORT_MARKER)		static_branch_inc(&trace_marker_exports_enabled);} static inline void ftrace_exports_disable(struct trace_export *export){	if (export->flags & TRACE_EXPORT_FUNCTION)		static_branch_dec(&trace_function_exports_enabled); 	if (export->flags & TRACE_EXPORT_EVENT)		static_branch_dec(&trace_event_exports_enabled); 	if (export->flags & TRACE_EXPORT_MARKER)		static_branch_dec(&trace_marker_exports_enabled);} static void ftrace_exports(struct ring_buffer_event *event, int flag){	struct trace_export *export; 	guard(preempt_notrace)(); 	export = rcu_dereference_raw_check(ftrace_exports_list);	while (export) {		trace_process_export(export, event, flag);		export = rcu_dereference_raw_check(export->next);	}} static inline voidadd_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 intrm_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 voidadd_ftrace_export(struct trace_export **list, struct trace_export *export){	ftrace_exports_enable(export); 	add_trace_export(list, export);} static inline intrm_ftrace_export(struct trace_export **list, struct trace_export *export){	int ret; 	ret = rm_trace_export(list, export);	ftrace_exports_disable(export); 	return ret;} int register_ftrace_export(struct trace_export *export){	if (WARN_ON_ONCE(!export->write))		return -1; 	guard(mutex)(&ftrace_export_lock); 	add_ftrace_export(&ftrace_exports_list, export); 	return 0;}EXPORT_SYMBOL_GPL(register_ftrace_export); int unregister_ftrace_export(struct trace_export *export){	guard(mutex)(&ftrace_export_lock);	return rm_ftrace_export(&ftrace_exports_list, export);}EXPORT_SYMBOL_GPL(unregister_ftrace_export); /* trace_flags holds trace_options default values */#define TRACE_DEFAULT_FLAGS						\

Lines 1–500 of 10017.