GNU Linux-libre 4.19.211-gnu1
[releases.git] / kernel / trace / trace_kdb.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * kdb helper for dumping the ftrace buffer
4  *
5  * Copyright (C) 2010 Jason Wessel <jason.wessel@windriver.com>
6  *
7  * ftrace_dump_buf based on ftrace_dump:
8  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
9  * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
10  *
11  */
12 #include <linux/init.h>
13 #include <linux/kgdb.h>
14 #include <linux/kdb.h>
15 #include <linux/ftrace.h>
16
17 #include "trace.h"
18 #include "trace_output.h"
19
20 static void ftrace_dump_buf(int skip_lines, long cpu_file)
21 {
22         /* use static because iter can be a bit big for the stack */
23         static struct trace_iterator iter;
24         static struct ring_buffer_iter *buffer_iter[CONFIG_NR_CPUS];
25         struct trace_array *tr;
26         unsigned int old_userobj;
27         int cnt = 0, cpu;
28
29         trace_init_global_iter(&iter);
30         iter.buffer_iter = buffer_iter;
31         tr = iter.tr;
32
33         for_each_tracing_cpu(cpu) {
34                 atomic_inc(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
35         }
36
37         old_userobj = tr->trace_flags;
38
39         /* don't look at user memory in panic mode */
40         tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
41
42         kdb_printf("Dumping ftrace buffer:\n");
43
44         trace_iterator_reset(&iter);
45         iter.iter_flags |= TRACE_FILE_LAT_FMT;
46
47         if (cpu_file == RING_BUFFER_ALL_CPUS) {
48                 for_each_tracing_cpu(cpu) {
49                         iter.buffer_iter[cpu] =
50                         ring_buffer_read_prepare(iter.trace_buffer->buffer,
51                                                  cpu, GFP_ATOMIC);
52                         ring_buffer_read_start(iter.buffer_iter[cpu]);
53                         tracing_iter_reset(&iter, cpu);
54                 }
55         } else {
56                 iter.cpu_file = cpu_file;
57                 iter.buffer_iter[cpu_file] =
58                         ring_buffer_read_prepare(iter.trace_buffer->buffer,
59                                                  cpu_file, GFP_ATOMIC);
60                 ring_buffer_read_start(iter.buffer_iter[cpu_file]);
61                 tracing_iter_reset(&iter, cpu_file);
62         }
63
64         while (trace_find_next_entry_inc(&iter)) {
65                 if (!cnt)
66                         kdb_printf("---------------------------------\n");
67                 cnt++;
68
69                 if (!skip_lines) {
70                         print_trace_line(&iter);
71                         trace_printk_seq(&iter.seq);
72                 } else {
73                         skip_lines--;
74                 }
75
76                 if (KDB_FLAG(CMD_INTERRUPT))
77                         goto out;
78         }
79
80         if (!cnt)
81                 kdb_printf("   (ftrace buffer empty)\n");
82         else
83                 kdb_printf("---------------------------------\n");
84
85 out:
86         tr->trace_flags = old_userobj;
87
88         for_each_tracing_cpu(cpu) {
89                 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
90         }
91
92         for_each_tracing_cpu(cpu) {
93                 if (iter.buffer_iter[cpu]) {
94                         ring_buffer_read_finish(iter.buffer_iter[cpu]);
95                         iter.buffer_iter[cpu] = NULL;
96                 }
97         }
98 }
99
100 /*
101  * kdb_ftdump - Dump the ftrace log buffer
102  */
103 static int kdb_ftdump(int argc, const char **argv)
104 {
105         int skip_lines = 0;
106         long cpu_file;
107         char *cp;
108
109         if (argc > 2)
110                 return KDB_ARGCOUNT;
111
112         if (argc) {
113                 skip_lines = simple_strtol(argv[1], &cp, 0);
114                 if (*cp)
115                         skip_lines = 0;
116         }
117
118         if (argc == 2) {
119                 cpu_file = simple_strtol(argv[2], &cp, 0);
120                 if (*cp || cpu_file >= NR_CPUS || cpu_file < 0 ||
121                     !cpu_online(cpu_file))
122                         return KDB_BADINT;
123         } else {
124                 cpu_file = RING_BUFFER_ALL_CPUS;
125         }
126
127         kdb_trap_printk++;
128         ftrace_dump_buf(skip_lines, cpu_file);
129         kdb_trap_printk--;
130
131         return 0;
132 }
133
134 static __init int kdb_ftrace_register(void)
135 {
136         kdb_register_flags("ftdump", kdb_ftdump, "[skip_#lines] [cpu]",
137                             "Dump ftrace log", 0, KDB_ENABLE_ALWAYS_SAFE);
138         return 0;
139 }
140
141 late_initcall(kdb_ftrace_register);