1 /*
2  * Copyright (C) 2019 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 <getopt.h>
18 
19 #include <string>
20 #include <vector>
21 
22 #include "perfetto/base/logging.h"
23 #include "perfetto/ext/base/unix_task_runner.h"
24 #include "perfetto/ext/traced/traced.h"
25 #include "src/perfetto_cmd/trigger_producer.h"
26 
27 namespace perfetto {
28 namespace {
29 
PrintUsage(const char * argv0)30 int PrintUsage(const char* argv0) {
31   PERFETTO_ELOG(R"(
32 Usage: %s TRIGGER...
33   -h|--help  Show this message
34 )",
35                 argv0);
36   return 1;
37 }
38 
39 }  // namespace
40 
41 int __attribute__((visibility("default")))
TriggerPerfettoMain(int argc,char ** argv)42 TriggerPerfettoMain(int argc, char** argv) {
43   static const struct option long_options[] = {
44       {"help", no_argument, nullptr, 'h'}, {nullptr, 0, nullptr, 0}};
45 
46   int option_index = 0;
47 
48   std::vector<std::string> triggers_to_activate;
49 
50   for (;;) {
51     int option = getopt_long(argc, argv, "h", long_options, &option_index);
52 
53     if (option == 'h')
54       return PrintUsage(argv[0]);
55 
56     if (option == -1)
57       break;  // EOF.
58   }
59 
60   for (int i = optind; i < argc; i++)
61     triggers_to_activate.push_back(std::string(argv[i]));
62 
63   if (triggers_to_activate.empty()) {
64     PERFETTO_ELOG("At least one trigger must the specified.");
65     return PrintUsage(argv[0]);
66   }
67 
68   bool finished_with_success = false;
69   base::UnixTaskRunner task_runner;
70   TriggerProducer producer(
71       &task_runner,
72       [&task_runner, &finished_with_success](bool success) {
73         finished_with_success = success;
74         task_runner.Quit();
75       },
76       &triggers_to_activate);
77   task_runner.Run();
78 
79   return finished_with_success ? 0 : 1;
80 }
81 
82 }  // namespace perfetto
83