xref: /aosp_15_r20/external/perfetto/src/trace_config_utils/main.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 #include <string.h>
19 
20 #include "perfetto/ext/base/file_utils.h"
21 #include "src/trace_config_utils/pb_to_txt.h"
22 #include "src/trace_config_utils/txt_to_pb.h"
23 
24 namespace {
PrintUsage(const char * argv0)25 void PrintUsage(const char* argv0) {
26   printf(R"(
27 Converts a TraceConfig from pbtxt to proto-encoded bytes and viceversa
28 
29 Usage: %s  txt_to_pb | pb_to_txt < in > out
30 )",
31          argv0);
32 }
33 
34 }  // namespace
35 
main(int argc,char ** argv)36 int main(int argc, char** argv) {
37   using namespace ::perfetto;
38 
39   if (argc < 2) {
40     PrintUsage(argv[0]);
41     return 1;
42   }
43 
44   const char* cmd = argv[1];
45   std::string in_data;
46   if (argc == 2) {
47     base::ReadFileStream(stdin, &in_data);
48   } else {
49     bool ok = base::ReadFile(argv[2], &in_data);
50     if (!ok) {
51       printf("Failed to open input file %s\n", argv[2]);
52       return 1;
53     }
54   }
55 
56   if (strcmp(cmd, "txt_to_pb") == 0) {
57     base::StatusOr<std::vector<uint8_t>> res = TraceConfigTxtToPb(in_data);
58     if (!res.ok()) {
59       printf("%s\n", res.status().c_message());
60       return 1;
61     }
62     fwrite(res->data(), res->size(), 1, stdout);
63     return 0;
64   }
65 
66   if (strcmp(cmd, "pb_to_txt") == 0) {
67     std::string txt = TraceConfigPbToTxt(in_data.data(), in_data.size());
68     printf("%s\n", txt.c_str());
69     return 0;
70   }
71 
72   PrintUsage(argv[0]);
73   return 1;
74 }
75