1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <libgen.h>
6
7 #include "trace-local.h"
8 #include "version.h"
9
10 struct usage_help {
11 char *name;
12 char *short_help;
13 char *long_help;
14 };
15
16 static struct usage_help usage_help[] = {
17 {
18 "record",
19 "record a trace into a trace.dat file",
20 " %s record [-v][-e event [-f filter]][-p plugin][-F][-d][-D][-o file] \\\n"
21 " [-q][-s usecs][-O option ][-l func][-g func][-n func] \\\n"
22 " [-P pid][-N host:port][-t][-r prio][-b size][-B buf][command ...]\n"
23 " [-m max][-C clock]\n"
24 " -e run command with event enabled\n"
25 " -f filter for previous -e event\n"
26 " -R trigger for previous -e event\n"
27 " -p run command with plugin enabled\n"
28 " -F filter only on the given process\n"
29 " -P trace the given pid like -F for the command\n"
30 " -c also trace the children of -F (or -P if kernel supports it)\n"
31 " -C set the trace clock\n"
32 " -T do a stacktrace on all events\n"
33 " -l filter function name\n"
34 " -g set graph function\n"
35 " -n do not trace function\n"
36 " -m max size per CPU in kilobytes\n"
37 " -M set CPU mask to trace\n"
38 " -v will negate all -e (disable those events) and -B (delete those instances) after it\n"
39 " -d disable function tracer when running\n"
40 " -D Full disable of function tracing (for all users)\n"
41 " -o data output file [default trace.dat]\n"
42 " -O option to enable (or disable)\n"
43 " -r real time priority to run the capture threads\n"
44 " -s sleep interval between recording (in usecs) [default: 1000]\n"
45 " -S used with --profile, to enable only events in command line\n"
46 " -N host:port to connect to (see listen)\n"
47 " -V cid:port to connect to via vsocket (see listen)\n"
48 " -t used with -N, forces use of tcp in live trace\n"
49 " -b change kernel buffersize (in kilobytes per CPU)\n"
50 " -B create sub buffer and following events will be enabled here\n"
51 " -k do not reset the buffers after tracing.\n"
52 " -i do not fail if an event is not found\n"
53 " -q print no output to the screen\n"
54 " -G when profiling, set soft and hard irqs as global\n"
55 " --quiet print no output to the screen\n"
56 " --module filter module name\n"
57 " --by-comm used with --profile, merge events for related comms\n"
58 " --profile enable tracing options needed for report --profile\n"
59 " --func-stack perform a stack trace for function tracer\n"
60 " (use with caution)\n"
61 " --max-graph-depth limit function_graph depth\n"
62 " --cmdlines-size change kernel saved_cmdlines_size\n"
63 " --no-filter include trace-cmd threads in the trace\n"
64 " --proc-map save the traced processes address map into the trace.dat file\n"
65 " --user execute the specified [command ...] as given user\n"
66 " --tsc2nsec Convert the current clock to nanoseconds, using tsc multiplier and shift from the Linux"
67 " kernel's perf interface\n"
68 " --tsync-interval set the loop interval, in ms, for timestamps synchronization with guests:"
69 " If a negative number is specified, timestamps synchronization is disabled"
70 " If 0 is specified, no loop is performed - timestamps offset is calculated only twice,"
71 " at the beginnig and at the end of the trace\n"
72 " --poll don't block while reading from the trace buffer\n"
73 " --name used with -A to give the agent a specific name\n"
74 " --file-version set the desired trace file version\n"
75 " --compression compress the trace output file, one of these strings can be passed:\n"
76 " any - auto select the best available compression algorithm\n"
77 " none - do not compress the trace file\n"
78 " name - the name of the desired compression algorithms\n"
79 " available algorithms can be listed with trace-cmd list -c\n"
80 },
81 {
82 "set",
83 "set a ftrace configuration parameter",
84 " %s set [-v][-e event [-f filter]][-p plugin][-F][-d][-D] \\\n"
85 " [-q][-s usecs][-O option ][-l func][-g func][-n func] \\\n"
86 " [-P pid][-b size][-B buf][-m max][-C clock][command ...]\n"
87 " -e enable event\n"
88 " -f filter for previous -e event\n"
89 " -R trigger for previous -e event\n"
90 " -p set ftrace plugin\n"
91 " -P set PIDs to be traced\n"
92 " -c also trace the children of -F (or -P if kernel supports it)\n"
93 " -C set the trace clock\n"
94 " -T do a stacktrace on all events\n"
95 " -l filter function name\n"
96 " -g set graph function\n"
97 " -n do not trace function\n"
98 " -m max size per CPU in kilobytes\n"
99 " -M set CPU mask to trace\n"
100 " -v will negate all -e (disable those events) and -B (delete those instances) after it\n"
101 " -d disable function tracer when running\n"
102 " -D Full disable of function tracing (for all users)\n"
103 " -O option to enable (or disable)\n"
104 " -b change kernel buffersize (in kilobytes per CPU)\n"
105 " -B create sub buffer and following events will be enabled here\n"
106 " -i do not fail if an event is not found\n"
107 " -q print no output to the screen\n"
108 " --quiet print no output to the screen\n"
109 " --module filter module name\n"
110 " --func-stack perform a stack trace for function tracer\n"
111 " (use with caution)\n"
112 " --max-graph-depth limit function_graph depth\n"
113 " --cmdlines-size change kernel saved_cmdlines_size\n"
114 " --user execute the specified [command ...] as given user\n"
115 " --fork return immediately if a command is specified\n"
116 " --verbose 'level' Set the desired log level\n"
117 },
118 {
119 "start",
120 "start tracing without recording into a file",
121 " %s start [-e event][-p plugin][-d][-O option ][-P pid]\n"
122 " Uses same options as record.\n"
123 " It only enables the tracing and exits\n"
124 "\n"
125 " --fork: If a command is specified, then return right after it forks\n"
126 " --verbose 'level' Set the desired log level\n"
127 },
128 {
129 "extract",
130 "extract a trace from the kernel",
131 " %s extract [-p plugin][-O option][-o file][-B buf][-s][-a][-t]\n"
132 " Uses similar options as record, but only reads an existing trace.\n"
133 " -s : extract the snapshot instead of the main buffer\n"
134 " -B : extract a given buffer (more than one may be specified)\n"
135 " -a : extract all buffers (except top one)\n"
136 " -t : extract the top level buffer (useful with -B and -a)\n"
137 " --verbose 'level' Set the desired log level\n"
138 },
139 {
140 "stop",
141 "stop the kernel from recording trace data",
142 " %s stop [-B buf [-B buf]..] [-a] [-t]\n"
143 " Stops the tracer from recording more data.\n"
144 " Used in conjunction with start\n"
145 " -B stop a given buffer (more than one may be specified)\n"
146 " -a stop all buffers (except top one)\n"
147 " -t stop the top level buffer (useful with -B or -a)\n"
148 },
149 {
150 "restart",
151 "restart the kernel trace data recording",
152 " %s restart [-B buf [-B buf]..] [-a] [-t]\n"
153 " Restarts recording after a trace-cmd stop.\n"
154 " Used in conjunction with stop\n"
155 " -B restart a given buffer (more than one may be specified)\n"
156 " -a restart all buffers (except top one)\n"
157 " -t restart the top level buffer (useful with -B or -a)\n"
158 },
159 {
160 "show",
161 "show the contents of the kernel tracing buffer",
162 " %s show [-p|-s][-c cpu][-B buf][options]\n"
163 " Basically, this is a cat of the trace file.\n"
164 " -p read the trace_pipe file instead\n"
165 " -s read the snapshot file instance\n"
166 " (Can't have both -p and -s)\n"
167 " -c just show the file associated with a given CPU\n"
168 " -B read from a tracing buffer instance.\n"
169 " -f display the file path that is being dumped\n"
170 " The following options shows the corresponding file name\n"
171 " and then exits.\n"
172 " --tracing_on\n"
173 " --current_tracer\n"
174 " --buffer_size (for buffer_size_kb)\n"
175 " --buffer_total_size (for buffer_total_size_kb)\n"
176 " --ftrace_filter (for set_ftrace_filter)\n"
177 " --ftrace_notrace (for set_ftrace_notrace)\n"
178 " --ftrace_pid (for set_ftrace_pid)\n"
179 " --graph_function (for set_graph_function)\n"
180 " --graph_notrace (for set_graph_notrace)\n"
181 " --cpumask (for tracing_cpumask)\n"
182 },
183 {
184 "reset",
185 "disable all kernel tracing and clear the trace buffers",
186 " %s reset [-b size][-B buf][-a][-d][-t]\n"
187 " Disables the tracer (may reset trace file)\n"
188 " Used in conjunction with start\n"
189 " -b change the kernel buffer size (in kilobytes per CPU)\n"
190 " -d delete the previous specified instance\n"
191 " -B reset the given buffer instance (may specify multiple -B)\n"
192 " -a reset all instances (except top one)\n"
193 " -t reset the top level instance (useful with -B or -a)\n"
194 },
195 {
196 "clear",
197 "clear the trace buffers",
198 " %s clear [-B buf][-a]\n"
199 " -B clear the given buffer (may specify multiple -B)\n"
200 " -a clear all existing buffers, including the top level one\n"
201 },
202 {
203 "report",
204 "read out the trace stored in a trace.dat file",
205 " %s report [-i file] [--cpu cpu] [-e][-f][-l][-P][-L][-N][-R][-E]\\\n"
206 " [-r events][-n events][-F filter][-v][-V[1-6]][-T][-O option]\n"
207 " [-H [start_system:]start_event,start_match[,pid]/[end_system:]end_event,end_match[,flags]\n"
208 " [-G]\n"
209 " -i input file [default trace.dat]\n"
210 " -e show file endianess\n"
211 " -f show function mapping list\n"
212 " -P show printk list\n"
213 " -E show event files stored\n"
214 " -F filter to filter output on\n"
215 " -I filter out events with the HARDIRQ flag set\n"
216 " -S filter out events with the SOFTIRQ flag set\n"
217 " -t print out full timestamp. Do not truncate to 6 places.\n"
218 " -R raw format: ignore print format and only show field data\n"
219 " -r raw format the events that match the option\n"
220 " -v will negate all -F after it (Not show matches)\n"
221 " -T print out the filter strings created and exit\n"
222 " -V[level] verbose (shows plugins being loaded)\n"
223 " With optional level (see --verbose numbers)\n"
224 " -L load only local (~/.trace-cmd/plugins) plugins\n"
225 " -N do not load any plugins\n"
226 " -n ignore plugin handlers for events that match the option\n"
227 " -w show wakeup latencies\n"
228 " -l show latency format (default with latency tracers)\n"
229 " -O plugin option -O [plugin:]var[=val]\n"
230 " --cpu <cpu1,cpu2,...> - filter events according to the given cpu list.\n"
231 " A range of CPUs can be specified using 'cpuX-cpuY' notation.\n"
232 " --cpus - List the CPUs that have content in it then exit.\n"
233 " --check-events return whether all event formats can be parsed\n"
234 " --stat - show the buffer stats that were reported at the end of the record.\n"
235 " --uname - show uname of the record, if it was saved\n"
236 " --version - show version used to build the trace-cmd exec that created the file\n"
237 " --profile report stats on where tasks are blocked and such\n"
238 " -G when profiling, set soft and hard irqs as global\n"
239 " -H Allows users to hook two events together for timings\n"
240 " (used with --profile)\n"
241 " --by-comm used with --profile, merge events for related comms\n"
242 " --ts-offset will add amount to timestamp of all events of the\n"
243 " previous data file.\n"
244 " --ts2secs HZ, pass in the timestamp frequency (per second)\n"
245 " to convert the displayed timestamps to seconds\n"
246 " Affects the previous data file, unless there was no\n"
247 " previous data file, in which case it becomes default\n"
248 " --ts-diff Show the delta timestamp between events.\n"
249 " --ts-check Check to make sure no time stamp on any CPU goes backwards.\n"
250 " --nodate Ignore the --date processing of trace-cmd record.\n"
251 " --raw-ts Display raw timestamps, without any corrections.\n"
252 " --align-ts Display timestamps aligned to the first event.\n"
253 " --verbose[=level] Set the desired log level\n"
254 " 0 or none - no error messages\n"
255 " 1 or crit - only critical messages\n"
256 " 2 or err - 'crit' and error messages\n"
257 " 3 or warn - 'err' and warning messages\n"
258 " 4 or info - 'warn' and informational messages\n"
259 " 5 or debug - 'info' and debugging messages\n"
260 " 6 or all - same as debug\n"
261 },
262 {
263 "stream",
264 "Start tracing and read the output directly",
265 " %s stream [-e event][-p plugin][-d][-O option ][-P pid]\n"
266 " Uses same options as record but does not write to files or the network.\n"
267 " --verbose 'level' Set the desired log level\n"
268 },
269 {
270 "profile",
271 "Start profiling and read the output directly",
272 " %s profile [-e event][-p plugin][-d][-O option ][-P pid][-G][-S][-o output]\n"
273 " [-H [start_system:]start_event,start_match[,pid]/[end_system:]end_event,end_match[,flags]\n\n"
274 " Uses same options as record --profile.\n"
275 " -H Allows users to hook two events together for timings\n"
276 " --verbose 'level' Set the desired log level\n"
277 },
278 {
279 "hist",
280 "show a histogram of the trace.dat information",
281 " %s hist [-i file][-P] [file]"
282 " -P ignore pids (compact all functions)\n"
283 },
284 {
285 "stat",
286 "show the status of the running tracing (ftrace) system",
287 " %s stat [-B buf][-t][-o]"
288 " -B show the status of a instance buffer\n"
289 " -t show the top level status along with buffer specified by -B\n"
290 " -o list tracing options\n"
291 },
292 {
293 "split",
294 "parse a trace.dat file into smaller file(s)",
295 " %s split [options] -o file [start [end]]\n"
296 " -o output file to write to (file.1, file.2, etc)\n"
297 " -s n split file up by n seconds\n"
298 " -m n split file up by n milliseconds\n"
299 " -u n split file up by n microseconds\n"
300 " -e n split file up by n events\n"
301 " -p n split file up by n pages\n"
302 " -r repeat from start to end\n"
303 " -c per cpu, that is -p 2 will be 2 pages for each CPU\n"
304 " if option is specified, it will split the file\n"
305 " up starting at start, and ending at end\n"
306 " start - decimal start time in seconds (ex: 75678.923853)\n"
307 " if left out, will start at beginning of file\n"
308 " end - decimal end time in seconds\n"
309 },
310 {
311 "options",
312 "list the plugin options available for trace-cmd report",
313 " %s options\n"
314 },
315 {
316 "listen",
317 "listen on a network socket for trace clients",
318 " %s listen -p port[-D][-o file][-d dir][-l logfile]\n"
319 " Creates a socket to listen for clients.\n"
320 " -p port number to listen on.\n"
321 " -D run in daemon mode.\n"
322 " -V listen on a vsocket instead.\n"
323 " -o file name to use for clients.\n"
324 " -d directory to store client files.\n"
325 " -l logfile to write messages to.\n"
326 " --verbose 'level' Set the desired log level\n"
327 },
328 {
329 "agent",
330 "listen on a vsocket for trace clients",
331 " %s agent -p port[-D]\n"
332 " Creates a vsocket to listen for clients.\n"
333 " -N Connect to IP via TCP instead of vsockets\n"
334 " *** Insecure setting, only use on a trusted network ***\n"
335 " *** Only use if the client is totally trusted. ***\n"
336 " -p port number to listen on.\n"
337 " -D run in daemon mode.\n"
338 " --verbose 'level' Set the desired log level\n"
339 },
340 {
341 "setup-guest",
342 "create FIFOs for tracing guest VMs",
343 " %s setup-guest [-c cpus][-p perm][-g group][-a] guest\n"
344 " -c number of guest virtual CPUs\n"
345 " -p FIFOs permissions (default: 0660)\n"
346 " -g FIFOs group owner\n"
347 " -a Attach FIFOs to guest VM config\n"
348 },
349 {
350 "list",
351 "list the available events, plugins or options",
352 " %s list [-e [regex]][-t][-o][-f [regex]]\n"
353 " -e list available events\n"
354 " -F show event format\n"
355 " --full show the print fmt with -F\n"
356 " -R show event triggers\n"
357 " -l show event filters\n"
358 " -t list available tracers\n"
359 " -o list available options\n"
360 " -f [regex] list available functions to filter on\n"
361 " -P list loaded plugin files (by path)\n"
362 " -O list plugin options\n"
363 " -B list defined buffer instances\n"
364 " -C list the defined clocks (and active one)\n"
365 " -c list the supported trace file compression algorithms\n"
366 },
367 {
368 "restore",
369 "restore a crashed record",
370 " %s restore [-c][-o file][-i file] cpu-file [cpu-file ...]\n"
371 " -c create a partial trace.dat file only\n"
372 " -o output file\n"
373 " -i partial trace.dat file for input\n"
374 },
375 {
376 "snapshot",
377 "take snapshot of running trace",
378 " %s snapshot [-s][-r][-f][-B buf][-c cpu]\n"
379 " -s take a snapshot of the trace buffer\n"
380 " -r reset current snapshot\n"
381 " -f free the snapshot buffer\n"
382 " without the above three options, display snapshot\n"
383 " -c operate on the snapshot buffer for the given CPU\n"
384 " -B operate on the snapshot buffer for a tracing buffer instance.\n"
385 },
386 {
387 "stack",
388 "output, enable or disable kernel stack tracing",
389 " %s stack [--start][--stop][--reset]\n"
390 " --start enable the stack tracer\n"
391 " --stop disable the stack tracer\n"
392 " --reset reset the maximum stack found\n"
393 " --verbose 'level' Set the desired log level\n"
394 },
395 {
396 "check-events",
397 "parse trace event formats",
398 " %s check-events [-N]\n"
399 " -N do not load any plugins\n"
400 " --verbose 'level' Set the desired log level\n"
401 },
402 {
403 "dump",
404 "read out the meta data from a trace file",
405 " %s dump [options]\n"
406 " -i input file, default is trace.dat\n"
407 " -v validate a trace file\n"
408 " --all print all meta data from a trace file\n"
409 " --summary print a meta data summary\n"
410 " --head-page print header page information\n"
411 " --head-event print header event information\n"
412 " --ftrace-events print ftrace events format\n"
413 " --systems print recorded event systems\n"
414 " --events print format of recorded events\n"
415 " --kallsyms print information of the mapping of function addresses to the function names\n"
416 " --printk print trace_printk() format strings\n"
417 " --cmd-lines print information mapping a PID to a process name\n"
418 " --options print options\n"
419 " --flyrecord information of offset and count of recorded events per CPU\n"
420 " --clock trace clock, saved in the file\n"
421 " -h, --help show usage information\n"
422 " --verbose 'level' Set the desired log level\n"
423 },
424 {
425 "convert",
426 "convert trace file to different version",
427 " %s convert [options]\n"
428 " -i input file, default is trace.dat\n"
429 " -o output file, mandatory parameter.\n"
430 " The output file can be specified also as last argument of the command\n"
431 " --file-version set the desired trace file version\n"
432 " --compression compress the trace output file, one of these strings can be passed:\n"
433 " any - auto select the best available compression algorithm\n"
434 " none - do not compress the trace file\n"
435 " name - the name of the desired compression algorithms\n"
436 " available algorithms can be listed with trace-cmd list -c\n" },
437 {
438 NULL, NULL, NULL
439 }
440 };
441
find_help(char * cmd)442 static struct usage_help *find_help(char *cmd)
443 {
444 struct usage_help *help;
445
446 help = usage_help;
447 while (help->name) {
448 if (strcmp(cmd, help->name) == 0)
449 return help;
450 help++;
451 }
452 return NULL;
453 }
454
usage(char ** argv)455 void usage(char **argv)
456 {
457 struct usage_help *help = NULL;
458 char *arg = argv[0];
459 char *p;
460
461 p = basename(arg);
462
463 printf("\n"
464 "%s version %s (%s)\n\n"
465 "usage:\n", p, VERSION_STRING, VERSION_GIT);
466
467 if (argv[1])
468 help = find_help(argv[1]);
469
470 if (help) {
471 printf(help->long_help, p);
472 goto out;
473 }
474
475 printf(" %s [COMMAND] ...\n\n"
476 " commands:\n", p);
477
478 help = usage_help;
479 while (help->name) {
480 printf(" %s - %s\n", help->name, help->short_help);
481 help++;
482 }
483 out:
484 printf("\n");
485 exit(-1);
486 }
487
488
trace_usage(int argc,char ** argv)489 void trace_usage(int argc, char **argv)
490 {
491 usage(argv);
492 }
493