blob: 3ccf5c2c1320131e5b1bc0bfe6a26b02edc402a8 [file] [log] [blame]
Jason Wessel955b61e2010-08-05 09:22:23 -05001/*
2 * kdb helper for dumping the ftrace buffer
3 *
4 * Copyright (C) 2010 Jason Wessel <jason.wessel@windriver.com>
5 *
6 * ftrace_dump_buf based on ftrace_dump:
7 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
8 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
9 *
10 */
11#include <linux/init.h>
12#include <linux/kgdb.h>
13#include <linux/kdb.h>
14#include <linux/ftrace.h>
15
Jason Wessel955b61e2010-08-05 09:22:23 -050016#include "trace.h"
17#include "trace_output.h"
18
Jason Wessel19063c72010-08-05 09:22:23 -050019static void ftrace_dump_buf(int skip_lines, long cpu_file)
Jason Wessel955b61e2010-08-05 09:22:23 -050020{
21 /* use static because iter can be a bit big for the stack */
22 static struct trace_iterator iter;
Daniel Thompsonc270cc72014-11-06 12:41:55 +000023 static struct ring_buffer_iter *buffer_iter[CONFIG_NR_CPUS];
Jason Wessel955b61e2010-08-05 09:22:23 -050024 unsigned int old_userobj;
25 int cnt = 0, cpu;
26
27 trace_init_global_iter(&iter);
Daniel Thompsonc270cc72014-11-06 12:41:55 +000028 iter.buffer_iter = buffer_iter;
Jason Wessel955b61e2010-08-05 09:22:23 -050029
30 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050031 atomic_inc(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
Jason Wessel955b61e2010-08-05 09:22:23 -050032 }
33
34 old_userobj = trace_flags;
35
36 /* don't look at user memory in panic mode */
37 trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
38
39 kdb_printf("Dumping ftrace buffer:\n");
40
41 /* reset all but tr, trace, and overruns */
42 memset(&iter.seq, 0,
43 sizeof(struct trace_iterator) -
44 offsetof(struct trace_iterator, seq));
45 iter.iter_flags |= TRACE_FILE_LAT_FMT;
46 iter.pos = -1;
47
Steven Rostedtae3b5092013-01-23 15:22:59 -050048 if (cpu_file == RING_BUFFER_ALL_CPUS) {
Jason Wessel19063c72010-08-05 09:22:23 -050049 for_each_tracing_cpu(cpu) {
50 iter.buffer_iter[cpu] =
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050051 ring_buffer_read_prepare(iter.trace_buffer->buffer, cpu);
Jason Wessel19063c72010-08-05 09:22:23 -050052 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] =
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050058 ring_buffer_read_prepare(iter.trace_buffer->buffer, cpu_file);
Jason Wessel19063c72010-08-05 09:22:23 -050059 ring_buffer_read_start(iter.buffer_iter[cpu_file]);
60 tracing_iter_reset(&iter, cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -050061 }
Daniel Thompson8520ded2014-11-06 12:41:56 +000062
63 while (trace_find_next_entry_inc(&iter)) {
Jason Wessel955b61e2010-08-05 09:22:23 -050064 if (!cnt)
65 kdb_printf("---------------------------------\n");
66 cnt++;
67
Daniel Thompson8520ded2014-11-06 12:41:56 +000068 if (!skip_lines) {
Jason Wessel955b61e2010-08-05 09:22:23 -050069 print_trace_line(&iter);
Jason Wessel955b61e2010-08-05 09:22:23 -050070 trace_printk_seq(&iter.seq);
Daniel Thompson8520ded2014-11-06 12:41:56 +000071 } else {
Jason Wessel955b61e2010-08-05 09:22:23 -050072 skip_lines--;
Daniel Thompson8520ded2014-11-06 12:41:56 +000073 }
74
Jason Wessel955b61e2010-08-05 09:22:23 -050075 if (KDB_FLAG(CMD_INTERRUPT))
76 goto out;
77 }
78
79 if (!cnt)
80 kdb_printf(" (ftrace buffer empty)\n");
81 else
82 kdb_printf("---------------------------------\n");
83
84out:
85 trace_flags = old_userobj;
86
87 for_each_tracing_cpu(cpu) {
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050088 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
Jason Wessel955b61e2010-08-05 09:22:23 -050089 }
90
Daniel Thompsonc270cc72014-11-06 12:41:55 +000091 for_each_tracing_cpu(cpu) {
92 if (iter.buffer_iter[cpu]) {
Jason Wessel955b61e2010-08-05 09:22:23 -050093 ring_buffer_read_finish(iter.buffer_iter[cpu]);
Daniel Thompsonc270cc72014-11-06 12:41:55 +000094 iter.buffer_iter[cpu] = NULL;
95 }
96 }
Jason Wessel955b61e2010-08-05 09:22:23 -050097}
98
99/*
100 * kdb_ftdump - Dump the ftrace log buffer
101 */
102static int kdb_ftdump(int argc, const char **argv)
103{
104 int skip_lines = 0;
Jason Wessel19063c72010-08-05 09:22:23 -0500105 long cpu_file;
Jason Wessel955b61e2010-08-05 09:22:23 -0500106 char *cp;
107
Jason Wessel19063c72010-08-05 09:22:23 -0500108 if (argc > 2)
Jason Wessel955b61e2010-08-05 09:22:23 -0500109 return KDB_ARGCOUNT;
110
111 if (argc) {
112 skip_lines = simple_strtol(argv[1], &cp, 0);
113 if (*cp)
114 skip_lines = 0;
115 }
116
Jason Wessel19063c72010-08-05 09:22:23 -0500117 if (argc == 2) {
118 cpu_file = simple_strtol(argv[2], &cp, 0);
119 if (*cp || cpu_file >= NR_CPUS || cpu_file < 0 ||
120 !cpu_online(cpu_file))
121 return KDB_BADINT;
122 } else {
Steven Rostedtae3b5092013-01-23 15:22:59 -0500123 cpu_file = RING_BUFFER_ALL_CPUS;
Jason Wessel19063c72010-08-05 09:22:23 -0500124 }
125
Jason Wessel955b61e2010-08-05 09:22:23 -0500126 kdb_trap_printk++;
Jason Wessel19063c72010-08-05 09:22:23 -0500127 ftrace_dump_buf(skip_lines, cpu_file);
Jason Wessel955b61e2010-08-05 09:22:23 -0500128 kdb_trap_printk--;
129
130 return 0;
131}
132
133static __init int kdb_ftrace_register(void)
134{
Anton Vorontsov42c884c2014-11-06 14:36:42 +0000135 kdb_register_flags("ftdump", kdb_ftdump, "[skip_#lines] [cpu]",
Daniel Thompson9452e972014-11-06 14:36:45 +0000136 "Dump ftrace log", 0, KDB_ENABLE_ALWAYS_SAFE);
Jason Wessel955b61e2010-08-05 09:22:23 -0500137 return 0;
138}
139
140late_initcall(kdb_ftrace_register);