xref: /aosp_15_r20/external/trace-cmd/tracecmd/trace-convert.c (revision 58e6ee5f017f6a8912852c892d18457e4bafb554)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2021, VMware, Tzvetomir Stoyanov <[email protected]>
4  */
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <getopt.h>
8 #include <fcntl.h>
9 #include <errno.h>
10 
11 #include "trace-local.h"
12 #include "trace-cmd.h"
13 #include "trace-cmd-private.h"
14 
convert_file(const char * in,const char * out,int file_version,char * compr)15 static void convert_file(const char *in, const char *out, int file_version, char *compr)
16 {
17 	struct tracecmd_input *ihandle;
18 	struct tracecmd_output *ohandle;
19 
20 	ihandle = tracecmd_open_head(in, 0);
21 	if (!ihandle)
22 		die("error reading %s", in);
23 
24 	ohandle = tracecmd_copy(ihandle, out, TRACECMD_FILE_CPU_FLYRECORD, file_version, compr);
25 	if (!ohandle)
26 		die("error writing %s", out);
27 
28 	tracecmd_output_close(ohandle);
29 	tracecmd_close(ihandle);
30 }
31 
32 enum {
33 	OPT_file_version	= 254,
34 	OPT_compression		= 255,
35 };
36 
trace_convert(int argc,char ** argv)37 void trace_convert(int argc, char **argv)
38 {
39 	char *input_file = NULL;
40 	char *output_file = NULL;
41 	char *compression = NULL;
42 	int file_version = tracecmd_default_file_version();
43 	int c;
44 
45 	if (argc < 2)
46 		usage(argv);
47 
48 	if (strcmp(argv[1], "convert") != 0)
49 		usage(argv);
50 
51 	for (;;) {
52 		int option_index = 0;
53 		static struct option long_options[] = {
54 			{"compression", required_argument, NULL, OPT_compression},
55 			{"file-version", required_argument, NULL, OPT_file_version},
56 			{"help", no_argument, NULL, '?'},
57 			{NULL, 0, NULL, 0}
58 		};
59 
60 		c = getopt_long (argc-1, argv+1, "+hi:o:", long_options, &option_index);
61 		if (c == -1)
62 			break;
63 		switch (c) {
64 		case 'i':
65 			if (input_file)
66 				die("Only one input file is supported, %s already set",
67 				    input_file);
68 			input_file = optarg;
69 			break;
70 		case 'o':
71 			if (output_file)
72 				die("Only one output file is supported, %s already set",
73 				    output_file);
74 			output_file = optarg;
75 			break;
76 		case OPT_compression:
77 			if (strcmp(optarg, "any") && strcmp(optarg, "none") &&
78 			    !tracecmd_compress_is_supported(optarg, NULL))
79 				die("Compression algorithm  %s is not supported", optarg);
80 			compression = optarg;
81 			break;
82 		case OPT_file_version:
83 			file_version = atoi(optarg);
84 			if (file_version < FILE_VERSION_MIN || file_version > FILE_VERSION_MAX)
85 				die("Unsupported file version %d, "
86 				    "supported versions are from %d to %d",
87 				    file_version, FILE_VERSION_MIN, FILE_VERSION_MAX);
88 
89 			break;
90 		case 'h':
91 		case '?':
92 		default:
93 			usage(argv);
94 		}
95 	}
96 
97 	if ((argc - optind) >= 2) {
98 		if (output_file)
99 			usage(argv);
100 		output_file = argv[optind + 1];
101 	}
102 
103 	if (!input_file)
104 		input_file = DEFAULT_INPUT_FILE;
105 	if (!output_file)
106 		usage(argv);
107 
108 	convert_file(input_file, output_file, file_version, compression);
109 }
110