xref: /aosp_15_r20/external/trace-cmd/tracecmd/trace-show.c (revision 58e6ee5f017f6a8912852c892d18457e4bafb554)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <[email protected]>
4  *
5  */
6 #include <stdlib.h>
7 #include <getopt.h>
8 #include <errno.h>
9 
10 #include "tracefs.h"
11 #include "trace-local.h"
12 
13 enum {
14 	OPT_tracing_on			= 255,
15 	OPT_current_tracer		= 254,
16 	OPT_buffer_size_kb		= 253,
17 	OPT_buffer_total_size_kb	= 252,
18 	OPT_ftrace_filter		= 251,
19 	OPT_ftrace_notrace		= 250,
20 	OPT_ftrace_pid			= 249,
21 	OPT_graph_function		= 248,
22 	OPT_graph_notrace		= 247,
23 	OPT_cpumask			= 246,
24 };
25 
trace_show(int argc,char ** argv)26 void trace_show(int argc, char **argv)
27 {
28 	const char *buffer = NULL;
29 	const char *file = "trace";
30 	const char *cpu = NULL;
31 	struct buffer_instance *instance = &top_instance;
32 	char cpu_path[128];
33 	char *path;
34 	int snap = 0;
35 	int pipe = 0;
36 	int show_name = 0;
37 	int option_index = 0;
38 	int stop = 0;
39 	int c;
40 	static struct option long_options[] = {
41 		{"tracing_on", no_argument, NULL, OPT_tracing_on},
42 		{"current_tracer", no_argument, NULL, OPT_current_tracer},
43 		{"buffer_size", no_argument, NULL, OPT_buffer_size_kb},
44 		{"buffer_total_size", no_argument, NULL, OPT_buffer_total_size_kb},
45 		{"ftrace_filter", no_argument, NULL, OPT_ftrace_filter},
46 		{"ftrace_notrace", no_argument, NULL, OPT_ftrace_notrace},
47 		{"ftrace_pid", no_argument, NULL, OPT_ftrace_pid},
48 		{"graph_function", no_argument, NULL, OPT_graph_function},
49 		{"graph_notrace", no_argument, NULL, OPT_graph_notrace},
50 		{"cpumask", no_argument, NULL, OPT_cpumask},
51 		{"help", no_argument, NULL, '?'},
52 		{NULL, 0, NULL, 0}
53 	};
54 
55 	init_top_instance();
56 
57 	while ((c = getopt_long(argc-1, argv+1, "B:c:fsp",
58 				long_options, &option_index)) >= 0) {
59 		switch (c) {
60 		case 'h':
61 			usage(argv);
62 			break;
63 		case 'B':
64 			if (buffer)
65 				die("Can only show one buffer at a time");
66 			buffer = optarg;
67 			instance = allocate_instance(optarg);
68 			if (!instance)
69 				die("Failed to create instance");
70 			break;
71 		case 'c':
72 			if (cpu)
73 				die("Can only show one CPU at a time");
74 			cpu = optarg;
75 			break;
76 		case 'f':
77 			show_name = 1;
78 			break;
79 		case 's':
80 			snap = 1;
81 			if (pipe)
82 				die("Can not have -s and -p together");
83 			break;
84 		case 'p':
85 			pipe = 1;
86 			if (snap)
87 				die("Can not have -s and -p together");
88 			break;
89 		case OPT_tracing_on:
90 			show_instance_file(instance, "tracing_on");
91 			stop = 1;
92 			break;
93 		case OPT_current_tracer:
94 			show_instance_file(instance, "current_tracer");
95 			stop = 1;
96 			break;
97 		case OPT_buffer_size_kb:
98 			show_instance_file(instance, "buffer_size_kb");
99 			stop = 1;
100 			break;
101 		case OPT_buffer_total_size_kb:
102 			show_instance_file(instance, "buffer_total_size_kb");
103 			stop = 1;
104 			break;
105 		case OPT_ftrace_filter:
106 			show_instance_file(instance, "set_ftrace_filter");
107 			stop = 1;
108 			break;
109 		case OPT_ftrace_notrace:
110 			show_instance_file(instance, "set_ftrace_notrace");
111 			stop = 1;
112 			break;
113 		case OPT_ftrace_pid:
114 			show_instance_file(instance, "set_ftrace_pid");
115 			stop = 1;
116 			break;
117 		case OPT_graph_function:
118 			show_instance_file(instance, "set_graph_function");
119 			stop = 1;
120 			break;
121 		case OPT_graph_notrace:
122 			show_instance_file(instance, "set_graph_notrace");
123 			stop = 1;
124 			break;
125 		case OPT_cpumask:
126 			show_instance_file(instance, "tracing_cpumask");
127 			stop = 1;
128 			break;
129 		default:
130 			usage(argv);
131 		}
132 	}
133 	if (stop)
134 		exit(0);
135 	if (pipe)
136 		file = "trace_pipe";
137 	else if (snap)
138 		file = "snapshot";
139 
140 	if (cpu) {
141 		char *endptr;
142 		long val;
143 
144 		errno = 0;
145 		val = strtol(cpu, &endptr, 0);
146 		if (errno || cpu == endptr)
147 			die("Invalid CPU index '%s'", cpu);
148 		snprintf(cpu_path, 128, "per_cpu/cpu%ld/%s", val, file);
149 		file = cpu_path;
150 	}
151 
152 	if (buffer) {
153 		int ret;
154 
155 		ret = asprintf(&path, "instances/%s/%s", buffer, file);
156 		if (ret < 0)
157 			die("Failed to allocate instance path %s", file);
158 		file = path;
159 	}
160 
161 	if (show_name) {
162 		char *name;
163 		name = tracefs_get_tracing_file(file);
164 		printf("%s\n", name);
165 		tracefs_put_tracing_file(name);
166 	}
167 	show_file(file);
168 	if (buffer)
169 		free(path);
170 
171 	return;
172 }
173