blob: d953c163a0794f5cd4578ae6957f0645ff1eaa0d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jason Wessel955b61e2010-08-05 09:22:23 -05002/*
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
Jason Wessel955b61e2010-08-05 09:22:23 -050017#include "trace.h"
18#include "trace_output.h"
19
Jason Wessel19063c72010-08-05 09:22:23 -050020static void ftrace_dump_buf(int skip_lines, long cpu_file)
Jason Wessel955b61e2010-08-05 09:22:23 -050021{
22 /* use static because iter can be a bit big for the stack */
23 static struct trace_iterator iter;
Daniel Thompsonc270cc72014-11-06 12:41:55 +000024 static struct ring_buffer_iter *buffer_iter[CONFIG_NR_CPUS];
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -040025 struct trace_array *tr;
Jason Wessel955b61e2010-08-05 09:22:23 -050026 unsigned int old_userobj;
27 int cnt = 0, cpu;
28
29 trace_init_global_iter(&iter);
Daniel Thompsonc270cc72014-11-06 12:41:55 +000030 iter.buffer_iter = buffer_iter;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -040031 tr = iter.tr;
Jason Wessel955b61e2010-08-05 09:22:23 -050032
33 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050034 atomic_inc(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
Jason Wessel955b61e2010-08-05 09:22:23 -050035 }
36
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -040037 old_userobj = tr->trace_flags;
Jason Wessel955b61e2010-08-05 09:22:23 -050038
39 /* don't look at user memory in panic mode */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -040040 tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
Jason Wessel955b61e2010-08-05 09:22:23 -050041
42 kdb_printf("Dumping ftrace buffer:\n");
43
44 /* reset all but tr, trace, and overruns */
45 memset(&iter.seq, 0,
46 sizeof(struct trace_iterator) -
47 offsetof(struct trace_iterator, seq));
48 iter.iter_flags |= TRACE_FILE_LAT_FMT;
49 iter.pos = -1;
50
Steven Rostedtae3b5092013-01-23 15:22:59 -050051 if (cpu_file == RING_BUFFER_ALL_CPUS) {
Jason Wessel19063c72010-08-05 09:22:23 -050052 for_each_tracing_cpu(cpu) {
53 iter.buffer_iter[cpu] =
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050054 ring_buffer_read_prepare(iter.trace_buffer->buffer, cpu);
Jason Wessel19063c72010-08-05 09:22:23 -050055 ring_buffer_read_start(iter.buffer_iter[cpu]);
56 tracing_iter_reset(&iter, cpu);
57 }
58 } else {
59 iter.cpu_file = cpu_file;
60 iter.buffer_iter[cpu_file] =
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050061 ring_buffer_read_prepare(iter.trace_buffer->buffer, cpu_file);
Jason Wessel19063c72010-08-05 09:22:23 -050062 ring_buffer_read_start(iter.buffer_iter[cpu_file]);
63 tracing_iter_reset(&iter, cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -050064 }
Daniel Thompson8520ded2014-11-06 12:41:56 +000065
66 while (trace_find_next_entry_inc(&iter)) {
Jason Wessel955b61e2010-08-05 09:22:23 -050067 if (!cnt)
68 kdb_printf("---------------------------------\n");
69 cnt++;
70
Daniel Thompson8520ded2014-11-06 12:41:56 +000071 if (!skip_lines) {
Jason Wessel955b61e2010-08-05 09:22:23 -050072 print_trace_line(&iter);
Jason Wessel955b61e2010-08-05 09:22:23 -050073 trace_printk_seq(&iter.seq);
Daniel Thompson8520ded2014-11-06 12:41:56 +000074 } else {
Jason Wessel955b61e2010-08-05 09:22:23 -050075 skip_lines--;
Daniel Thompson8520ded2014-11-06 12:41:56 +000076 }
77
Jason Wessel955b61e2010-08-05 09:22:23 -050078 if (KDB_FLAG(CMD_INTERRUPT))
79 goto out;
80 }
81
82 if (!cnt)
83 kdb_printf(" (ftrace buffer empty)\n");
84 else
85 kdb_printf("---------------------------------\n");
86
87out:
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -040088 tr->trace_flags = old_userobj;
Jason Wessel955b61e2010-08-05 09:22:23 -050089
90 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050091 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
Jason Wessel955b61e2010-08-05 09:22:23 -050092 }
93
Daniel Thompsonc270cc72014-11-06 12:41:55 +000094 for_each_tracing_cpu(cpu) {
95 if (iter.buffer_iter[cpu]) {
Jason Wessel955b61e2010-08-05 09:22:23 -050096 ring_buffer_read_finish(iter.buffer_iter[cpu]);
Daniel Thompsonc270cc72014-11-06 12:41:55 +000097 iter.buffer_iter[cpu] = NULL;
98 }
99 }
Jason Wessel955b61e2010-08-05 09:22:23 -0500100}
101
102/*
103 * kdb_ftdump - Dump the ftrace log buffer
104 */
105static int kdb_ftdump(int argc, const char **argv)
106{
107 int skip_lines = 0;
Jason Wessel19063c72010-08-05 09:22:23 -0500108 long cpu_file;
Jason Wessel955b61e2010-08-05 09:22:23 -0500109 char *cp;
110
Jason Wessel19063c72010-08-05 09:22:23 -0500111 if (argc > 2)
Jason Wessel955b61e2010-08-05 09:22:23 -0500112 return KDB_ARGCOUNT;
113
114 if (argc) {
115 skip_lines = simple_strtol(argv[1], &cp, 0);
116 if (*cp)
117 skip_lines = 0;
118 }
119
Jason Wessel19063c72010-08-05 09:22:23 -0500120 if (argc == 2) {
121 cpu_file = simple_strtol(argv[2], &cp, 0);
122 if (*cp || cpu_file >= NR_CPUS || cpu_file < 0 ||
123 !cpu_online(cpu_file))
124 return KDB_BADINT;
125 } else {
Steven Rostedtae3b5092013-01-23 15:22:59 -0500126 cpu_file = RING_BUFFER_ALL_CPUS;
Jason Wessel19063c72010-08-05 09:22:23 -0500127 }
128
Jason Wessel955b61e2010-08-05 09:22:23 -0500129 kdb_trap_printk++;
Jason Wessel19063c72010-08-05 09:22:23 -0500130 ftrace_dump_buf(skip_lines, cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -0500131 kdb_trap_printk--;
132
133 return 0;
134}
135
136static __init int kdb_ftrace_register(void)
137{
Anton Vorontsov42c884c2014-11-06 14:36:42 +0000138 kdb_register_flags("ftdump", kdb_ftdump, "[skip_#lines] [cpu]",
Daniel Thompson9452e972014-11-06 14:36:45 +0000139 "Dump ftrace log", 0, KDB_ENABLE_ALWAYS_SAFE);
Jason Wessel955b61e2010-08-05 09:22:23 -0500140 return 0;
141}
142
143late_initcall(kdb_ftrace_register);