• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 <fcntl.h>
18 #include <getopt.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 
23 #include "perfetto/base/logging.h"
24 #include "perfetto/ext/base/unix_task_runner.h"
25 #include "perfetto/ext/traced/traced.h"
26 #include "perfetto/ext/tracing/ipc/default_socket.h"
27 
28 #include "src/traced/probes/ftrace/ftrace_procfs.h"
29 #include "src/traced/probes/probes_producer.h"
30 
31 #if PERFETTO_BUILDFLAG(PERFETTO_VERSION_GEN)
32 #include "perfetto_version.gen.h"
33 #else
34 #define PERFETTO_GET_GIT_REVISION() "unknown"
35 #endif
36 
37 namespace perfetto {
38 
ProbesMain(int argc,char ** argv)39 int __attribute__((visibility("default"))) ProbesMain(int argc, char** argv) {
40   enum LongOption {
41     OPT_CLEANUP_AFTER_CRASH = 1000,
42     OPT_VERSION,
43   };
44 
45   static const struct option long_options[] = {
46       {"cleanup-after-crash", no_argument, nullptr, OPT_CLEANUP_AFTER_CRASH},
47       {"version", no_argument, nullptr, OPT_VERSION},
48       {nullptr, 0, nullptr, 0}};
49 
50   int option_index;
51   for (;;) {
52     int option = getopt_long(argc, argv, "", long_options, &option_index);
53     if (option == -1)
54       break;
55     switch (option) {
56       case OPT_CLEANUP_AFTER_CRASH:
57         HardResetFtraceState();
58         return 0;
59       case OPT_VERSION:
60         printf("%s\n", PERFETTO_GET_GIT_REVISION());
61         return 0;
62       default:
63         PERFETTO_ELOG("Usage: %s [--cleanup-after-crash|--version]", argv[0]);
64         return 1;
65     }
66   }
67 
68   base::Watchdog* watchdog = base::Watchdog::GetInstance();
69   // The memory watchdog will be updated soon after connect, once the shmem
70   // buffer size is known, in ProbesProducer::OnTracingSetup().
71   watchdog->SetMemoryLimit(base::kWatchdogDefaultMemorySlack,
72                            base::kWatchdogDefaultMemoryWindow);
73   watchdog->SetCpuLimit(base::kWatchdogDefaultCpuLimit,
74                         base::kWatchdogDefaultCpuWindow);
75   watchdog->Start();
76 
77   PERFETTO_LOG("Starting %s service", argv[0]);
78 
79   // This environment variable is set by Android's init to a fd to /dev/kmsg
80   // opened for writing (see perfetto.rc). We cannot open the file directly
81   // due to permissions.
82   const char* env = getenv("ANDROID_FILE__dev_kmsg");
83   if (env) {
84     FtraceProcfs::g_kmesg_fd = atoi(env);
85     // The file descriptor passed by init doesn't have the FD_CLOEXEC bit set.
86     // Set it so we don't leak this fd while invoking atrace.
87     int res = fcntl(FtraceProcfs::g_kmesg_fd, F_SETFD, FD_CLOEXEC);
88     PERFETTO_DCHECK(res == 0);
89   }
90 
91   base::UnixTaskRunner task_runner;
92   ProbesProducer producer;
93   producer.ConnectWithRetries(GetProducerSocket(), &task_runner);
94   task_runner.Run();
95   return 0;
96 }
97 
98 }  // namespace perfetto
99