trace.c (5661706efa200252d0e9fea02421b0a5857808c3) trace.c (2455f0e124d317dd08d337a7550a78a224d4ba41)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * ring buffer based function tracer
4 *
5 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 *
8 * Originally taken from the RT patch by:

--- 35 unchanged lines hidden (view full) ---

44#include <linux/fs.h>
45#include <linux/trace.h>
46#include <linux/sched/clock.h>
47#include <linux/sched/rt.h>
48#include <linux/fsnotify.h>
49#include <linux/irq_work.h>
50#include <linux/workqueue.h>
51
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * ring buffer based function tracer
4 *
5 * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 *
8 * Originally taken from the RT patch by:

--- 35 unchanged lines hidden (view full) ---

44#include <linux/fs.h>
45#include <linux/trace.h>
46#include <linux/sched/clock.h>
47#include <linux/sched/rt.h>
48#include <linux/fsnotify.h>
49#include <linux/irq_work.h>
50#include <linux/workqueue.h>
51
52#include <asm/setup.h> /* COMMAND_LINE_SIZE */
53
52#include "trace.h"
53#include "trace_output.h"
54
55/*
56 * On boot up, the ring buffer is set to the minimum size, so that
57 * we do not waste memory on systems that are not using tracing.
58 */
59bool ring_buffer_expanded;

--- 121 unchanged lines hidden (view full) ---

181
182#define MAX_TRACER_SIZE 100
183static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
184static char *default_bootup_tracer;
185
186static bool allocate_snapshot;
187static bool snapshot_at_boot;
188
54#include "trace.h"
55#include "trace_output.h"
56
57/*
58 * On boot up, the ring buffer is set to the minimum size, so that
59 * we do not waste memory on systems that are not using tracing.
60 */
61bool ring_buffer_expanded;

--- 121 unchanged lines hidden (view full) ---

183
184#define MAX_TRACER_SIZE 100
185static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
186static char *default_bootup_tracer;
187
188static bool allocate_snapshot;
189static bool snapshot_at_boot;
190
191static char boot_instance_info[COMMAND_LINE_SIZE] __initdata;
192static int boot_instance_index;
193
194static char boot_snapshot_info[COMMAND_LINE_SIZE] __initdata;
195static int boot_snapshot_index;
196
189static int __init set_cmdline_ftrace(char *str)
190{
191 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
192 default_bootup_tracer = bootup_tracer_buf;
193 /* We are using ftrace early, expand it */
194 ring_buffer_expanded = true;
195 return 1;
196}

--- 20 unchanged lines hidden (view full) ---

217 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
218 __disable_trace_on_warning = 1;
219 return 1;
220}
221__setup("traceoff_on_warning", stop_trace_on_warning);
222
223static int __init boot_alloc_snapshot(char *str)
224{
197static int __init set_cmdline_ftrace(char *str)
198{
199 strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
200 default_bootup_tracer = bootup_tracer_buf;
201 /* We are using ftrace early, expand it */
202 ring_buffer_expanded = true;
203 return 1;
204}

--- 20 unchanged lines hidden (view full) ---

225 if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
226 __disable_trace_on_warning = 1;
227 return 1;
228}
229__setup("traceoff_on_warning", stop_trace_on_warning);
230
231static int __init boot_alloc_snapshot(char *str)
232{
225 allocate_snapshot = true;
226 /* We also need the main ring buffer expanded */
227 ring_buffer_expanded = true;
233 char *slot = boot_snapshot_info + boot_snapshot_index;
234 int left = sizeof(boot_snapshot_info) - boot_snapshot_index;
235 int ret;
236
237 if (str[0] == '=') {
238 str++;
239 if (strlen(str) >= left)
240 return -1;
241
242 ret = snprintf(slot, left, "%s\t", str);
243 boot_snapshot_index += ret;
244 } else {
245 allocate_snapshot = true;
246 /* We also need the main ring buffer expanded */
247 ring_buffer_expanded = true;
248 }
228 return 1;
229}
230__setup("alloc_snapshot", boot_alloc_snapshot);
231
232
233static int __init boot_snapshot(char *str)
234{
235 snapshot_at_boot = true;
236 boot_alloc_snapshot(str);
237 return 1;
238}
239__setup("ftrace_boot_snapshot", boot_snapshot);
240
241
249 return 1;
250}
251__setup("alloc_snapshot", boot_alloc_snapshot);
252
253
254static int __init boot_snapshot(char *str)
255{
256 snapshot_at_boot = true;
257 boot_alloc_snapshot(str);
258 return 1;
259}
260__setup("ftrace_boot_snapshot", boot_snapshot);
261
262
263static int __init boot_instance(char *str)
264{
265 char *slot = boot_instance_info + boot_instance_index;
266 int left = sizeof(boot_instance_info) - boot_instance_index;
267 int ret;
268
269 if (strlen(str) >= left)
270 return -1;
271
272 ret = snprintf(slot, left, "%s\t", str);
273 boot_instance_index += ret;
274
275 return 1;
276}
277__setup("trace_instance=", boot_instance);
278
279
242static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
243
244static int __init set_trace_boot_options(char *str)
245{
246 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
247 return 1;
248}
249__setup("trace_options=", set_trace_boot_options);

--- 746 unchanged lines hidden (view full) ---

996 /* Release the temp buffer */
997 this_cpu_dec(trace_buffered_event_cnt);
998 /* ring_buffer_unlock_commit() enables preemption */
999 preempt_enable_notrace();
1000 } else
1001 ring_buffer_unlock_commit(buffer);
1002}
1003
280static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
281
282static int __init set_trace_boot_options(char *str)
283{
284 strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
285 return 1;
286}
287__setup("trace_options=", set_trace_boot_options);

--- 746 unchanged lines hidden (view full) ---

1034 /* Release the temp buffer */
1035 this_cpu_dec(trace_buffered_event_cnt);
1036 /* ring_buffer_unlock_commit() enables preemption */
1037 preempt_enable_notrace();
1038 } else
1039 ring_buffer_unlock_commit(buffer);
1040}
1041
1004/**
1005 * __trace_puts - write a constant string into the trace buffer.
1006 * @ip: The address of the caller
1007 * @str: The constant string to write
1008 * @size: The size of the string.
1009 */
1010int __trace_puts(unsigned long ip, const char *str, int size)
1042int __trace_array_puts(struct trace_array *tr, unsigned long ip,
1043 const char *str, int size)
1011{
1012 struct ring_buffer_event *event;
1013 struct trace_buffer *buffer;
1014 struct print_entry *entry;
1015 unsigned int trace_ctx;
1016 int alloc;
1017
1044{
1045 struct ring_buffer_event *event;
1046 struct trace_buffer *buffer;
1047 struct print_entry *entry;
1048 unsigned int trace_ctx;
1049 int alloc;
1050
1018 if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
1051 if (!(tr->trace_flags & TRACE_ITER_PRINTK))
1019 return 0;
1020
1021 if (unlikely(tracing_selftest_running || tracing_disabled))
1022 return 0;
1023
1024 alloc = sizeof(*entry) + size + 2; /* possible \n added */
1025
1026 trace_ctx = tracing_gen_ctx();
1052 return 0;
1053
1054 if (unlikely(tracing_selftest_running || tracing_disabled))
1055 return 0;
1056
1057 alloc = sizeof(*entry) + size + 2; /* possible \n added */
1058
1059 trace_ctx = tracing_gen_ctx();
1027 buffer = global_trace.array_buffer.buffer;
1060 buffer = tr->array_buffer.buffer;
1028 ring_buffer_nest_start(buffer);
1029 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
1030 trace_ctx);
1031 if (!event) {
1032 size = 0;
1033 goto out;
1034 }
1035

--- 5 unchanged lines hidden (view full) ---

1041 /* Add a newline if necessary */
1042 if (entry->buf[size - 1] != '\n') {
1043 entry->buf[size] = '\n';
1044 entry->buf[size + 1] = '\0';
1045 } else
1046 entry->buf[size] = '\0';
1047
1048 __buffer_unlock_commit(buffer, event);
1061 ring_buffer_nest_start(buffer);
1062 event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
1063 trace_ctx);
1064 if (!event) {
1065 size = 0;
1066 goto out;
1067 }
1068

--- 5 unchanged lines hidden (view full) ---

1074 /* Add a newline if necessary */
1075 if (entry->buf[size - 1] != '\n') {
1076 entry->buf[size] = '\n';
1077 entry->buf[size + 1] = '\0';
1078 } else
1079 entry->buf[size] = '\0';
1080
1081 __buffer_unlock_commit(buffer, event);
1049 ftrace_trace_stack(&global_trace, buffer, trace_ctx, 4, NULL);
1082 ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL);
1050 out:
1051 ring_buffer_nest_end(buffer);
1052 return size;
1053}
1083 out:
1084 ring_buffer_nest_end(buffer);
1085 return size;
1086}
1087EXPORT_SYMBOL_GPL(__trace_array_puts);
1088
1089/**
1090 * __trace_puts - write a constant string into the trace buffer.
1091 * @ip: The address of the caller
1092 * @str: The constant string to write
1093 * @size: The size of the string.
1094 */
1095int __trace_puts(unsigned long ip, const char *str, int size)
1096{
1097 return __trace_array_puts(&global_trace, ip, str, size);
1098}
1054EXPORT_SYMBOL_GPL(__trace_puts);
1055
1056/**
1057 * __trace_bputs - write the pointer to a constant string into trace buffer
1058 * @ip: The address of the caller
1059 * @str: The constant string to write to the buffer to
1060 */
1061int __trace_bputs(unsigned long ip, const char *str)

--- 75 unchanged lines hidden (view full) ---

1137 * tracing_snapshot - take a snapshot of the current buffer.
1138 *
1139 * This causes a swap between the snapshot buffer and the current live
1140 * tracing buffer. You can use this to take snapshots of the live
1141 * trace when some condition is triggered, but continue to trace.
1142 *
1143 * Note, make sure to allocate the snapshot with either
1144 * a tracing_snapshot_alloc(), or by doing it manually
1099EXPORT_SYMBOL_GPL(__trace_puts);
1100
1101/**
1102 * __trace_bputs - write the pointer to a constant string into trace buffer
1103 * @ip: The address of the caller
1104 * @str: The constant string to write to the buffer to
1105 */
1106int __trace_bputs(unsigned long ip, const char *str)

--- 75 unchanged lines hidden (view full) ---

1182 * tracing_snapshot - take a snapshot of the current buffer.
1183 *
1184 * This causes a swap between the snapshot buffer and the current live
1185 * tracing buffer. You can use this to take snapshots of the live
1186 * trace when some condition is triggered, but continue to trace.
1187 *
1188 * Note, make sure to allocate the snapshot with either
1189 * a tracing_snapshot_alloc(), or by doing it manually
1145 * with: echo 1 > /sys/kernel/debug/tracing/snapshot
1190 * with: echo 1 > /sys/kernel/tracing/snapshot
1146 *
1147 * If the snapshot buffer is not allocated, it will stop tracing.
1148 * Basically making a permanent snapshot.
1149 */
1150void tracing_snapshot(void)
1151{
1152 struct trace_array *tr = &global_trace;
1153

--- 4598 unchanged lines hidden (view full) ---

5752 "\t trace(<synthetic_event>,param list) - generate synthetic event\n"
5753 "\t save(field,...) - save current event fields\n"
5754#ifdef CONFIG_TRACER_SNAPSHOT
5755 "\t snapshot() - snapshot the trace buffer\n\n"
5756#endif
5757#ifdef CONFIG_SYNTH_EVENTS
5758 " events/synthetic_events\t- Create/append/remove/show synthetic events\n"
5759 "\t Write into this file to define/undefine new synthetic events.\n"
1191 *
1192 * If the snapshot buffer is not allocated, it will stop tracing.
1193 * Basically making a permanent snapshot.
1194 */
1195void tracing_snapshot(void)
1196{
1197 struct trace_array *tr = &global_trace;
1198

--- 4598 unchanged lines hidden (view full) ---

5797 "\t trace(<synthetic_event>,param list) - generate synthetic event\n"
5798 "\t save(field,...) - save current event fields\n"
5799#ifdef CONFIG_TRACER_SNAPSHOT
5800 "\t snapshot() - snapshot the trace buffer\n\n"
5801#endif
5802#ifdef CONFIG_SYNTH_EVENTS
5803 " events/synthetic_events\t- Create/append/remove/show synthetic events\n"
5804 "\t Write into this file to define/undefine new synthetic events.\n"
5760 "\t example: echo 'myevent u64 lat; char name[]' >> synthetic_events\n"
5805 "\t example: echo 'myevent u64 lat; char name[]; long[] stack' >> synthetic_events\n"
5761#endif
5762#endif
5763;
5764
5765static ssize_t
5766tracing_readme_read(struct file *filp, char __user *ubuf,
5767 size_t cnt, loff_t *ppos)
5768{

--- 3374 unchanged lines hidden (view full) ---

9143
9144 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
9145 if (ret)
9146 return ret;
9147
9148 if (val > 100)
9149 return -EINVAL;
9150
5806#endif
5807#endif
5808;
5809
5810static ssize_t
5811tracing_readme_read(struct file *filp, char __user *ubuf,
5812 size_t cnt, loff_t *ppos)
5813{

--- 3374 unchanged lines hidden (view full) ---

9188
9189 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
9190 if (ret)
9191 return ret;
9192
9193 if (val > 100)
9194 return -EINVAL;
9195
9196 if (!val)
9197 val = 1;
9198
9151 tr->buffer_percent = val;
9152
9153 (*ppos)++;
9154
9155 return cnt;
9156}
9157
9158static const struct file_operations buffer_percent_fops = {

--- 58 unchanged lines hidden (view full) ---

9217 ret = allocate_trace_buffer(tr, &tr->max_buffer,
9218 allocate_snapshot ? size : 1);
9219 if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) {
9220 free_trace_buffer(&tr->array_buffer);
9221 return -ENOMEM;
9222 }
9223 tr->allocated_snapshot = allocate_snapshot;
9224
9199 tr->buffer_percent = val;
9200
9201 (*ppos)++;
9202
9203 return cnt;
9204}
9205
9206static const struct file_operations buffer_percent_fops = {

--- 58 unchanged lines hidden (view full) ---

9265 ret = allocate_trace_buffer(tr, &tr->max_buffer,
9266 allocate_snapshot ? size : 1);
9267 if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) {
9268 free_trace_buffer(&tr->array_buffer);
9269 return -ENOMEM;
9270 }
9271 tr->allocated_snapshot = allocate_snapshot;
9272
9225 /*
9226 * Only the top level trace array gets its snapshot allocated
9227 * from the kernel command line.
9228 */
9229 allocate_snapshot = false;
9230#endif
9231
9232 return 0;
9233}
9234
9235static void free_trace_buffers(struct trace_array *tr)
9236{

--- 899 unchanged lines hidden (view full) ---

10136 ret = done;
10137
10138out:
10139 kfree(kbuf);
10140
10141 return ret;
10142}
10143
9273 allocate_snapshot = false;
9274#endif
9275
9276 return 0;
9277}
9278
9279static void free_trace_buffers(struct trace_array *tr)
9280{

--- 899 unchanged lines hidden (view full) ---

10180 ret = done;
10181
10182out:
10183 kfree(kbuf);
10184
10185 return ret;
10186}
10187
10188#ifdef CONFIG_TRACER_MAX_TRACE
10189__init static bool tr_needs_alloc_snapshot(const char *name)
10190{
10191 char *test;
10192 int len = strlen(name);
10193 bool ret;
10194
10195 if (!boot_snapshot_index)
10196 return false;
10197
10198 if (strncmp(name, boot_snapshot_info, len) == 0 &&
10199 boot_snapshot_info[len] == '\t')
10200 return true;
10201
10202 test = kmalloc(strlen(name) + 3, GFP_KERNEL);
10203 if (!test)
10204 return false;
10205
10206 sprintf(test, "\t%s\t", name);
10207 ret = strstr(boot_snapshot_info, test) == NULL;
10208 kfree(test);
10209 return ret;
10210}
10211
10212__init static void do_allocate_snapshot(const char *name)
10213{
10214 if (!tr_needs_alloc_snapshot(name))
10215 return;
10216
10217 /*
10218 * When allocate_snapshot is set, the next call to
10219 * allocate_trace_buffers() (called by trace_array_get_by_name())
10220 * will allocate the snapshot buffer. That will alse clear
10221 * this flag.
10222 */
10223 allocate_snapshot = true;
10224}
10225#else
10226static inline void do_allocate_snapshot(const char *name) { }
10227#endif
10228
10229__init static void enable_instances(void)
10230{
10231 struct trace_array *tr;
10232 char *curr_str;
10233 char *str;
10234 char *tok;
10235
10236 /* A tab is always appended */
10237 boot_instance_info[boot_instance_index - 1] = '\0';
10238 str = boot_instance_info;
10239
10240 while ((curr_str = strsep(&str, "\t"))) {
10241
10242 tok = strsep(&curr_str, ",");
10243
10244 if (IS_ENABLED(CONFIG_TRACER_MAX_TRACE))
10245 do_allocate_snapshot(tok);
10246
10247 tr = trace_array_get_by_name(tok);
10248 if (!tr) {
10249 pr_warn("Failed to create instance buffer %s\n", curr_str);
10250 continue;
10251 }
10252 /* Allow user space to delete it */
10253 trace_array_put(tr);
10254
10255 while ((tok = strsep(&curr_str, ","))) {
10256 early_enable_events(tr, tok, true);
10257 }
10258 }
10259}
10260
10144__init static int tracer_alloc_buffers(void)
10145{
10146 int ring_buf_size;
10147 int ret = -ENOMEM;
10148
10149
10150 if (security_locked_down(LOCKDOWN_TRACEFS)) {
10151 pr_warn("Tracing disabled due to lockdown\n");

--- 117 unchanged lines hidden (view full) ---

10269out_free_buffer_mask:
10270 free_cpumask_var(tracing_buffer_mask);
10271out:
10272 return ret;
10273}
10274
10275void __init ftrace_boot_snapshot(void)
10276{
10261__init static int tracer_alloc_buffers(void)
10262{
10263 int ring_buf_size;
10264 int ret = -ENOMEM;
10265
10266
10267 if (security_locked_down(LOCKDOWN_TRACEFS)) {
10268 pr_warn("Tracing disabled due to lockdown\n");

--- 117 unchanged lines hidden (view full) ---

10386out_free_buffer_mask:
10387 free_cpumask_var(tracing_buffer_mask);
10388out:
10389 return ret;
10390}
10391
10392void __init ftrace_boot_snapshot(void)
10393{
10394 struct trace_array *tr;
10395
10277 if (snapshot_at_boot) {
10278 tracing_snapshot();
10279 internal_trace_puts("** Boot snapshot taken **\n");
10280 }
10396 if (snapshot_at_boot) {
10397 tracing_snapshot();
10398 internal_trace_puts("** Boot snapshot taken **\n");
10399 }
10400
10401 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
10402 if (tr == &global_trace)
10403 continue;
10404 trace_array_puts(tr, "** Boot snapshot taken **\n");
10405 tracing_snapshot_instance(tr);
10406 }
10281}
10282
10283void __init early_trace_init(void)
10284{
10285 if (tracepoint_printk) {
10286 tracepoint_print_iter =
10287 kzalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
10288 if (MEM_FAIL(!tracepoint_print_iter,

--- 5 unchanged lines hidden (view full) ---

10294 tracer_alloc_buffers();
10295
10296 init_events();
10297}
10298
10299void __init trace_init(void)
10300{
10301 trace_event_init();
10407}
10408
10409void __init early_trace_init(void)
10410{
10411 if (tracepoint_printk) {
10412 tracepoint_print_iter =
10413 kzalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
10414 if (MEM_FAIL(!tracepoint_print_iter,

--- 5 unchanged lines hidden (view full) ---

10420 tracer_alloc_buffers();
10421
10422 init_events();
10423}
10424
10425void __init trace_init(void)
10426{
10427 trace_event_init();
10428
10429 if (boot_instance_index)
10430 enable_instances();
10302}
10303
10304__init static void clear_boot_tracer(void)
10305{
10306 /*
10307 * The default tracer at boot buffer is an init section.
10308 * This function is called in lateinit. If we did not
10309 * find the boot tracer, then clear it out, to prevent

--- 46 unchanged lines hidden ---
10431}
10432
10433__init static void clear_boot_tracer(void)
10434{
10435 /*
10436 * The default tracer at boot buffer is an init section.
10437 * This function is called in lateinit. If we did not
10438 * find the boot tracer, then clear it out, to prevent

--- 46 unchanged lines hidden ---