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 <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21
22 #include "perfetto/base/logging.h"
23 #include "perfetto/ext/base/file_utils.h"
24 #include "perfetto/ext/base/getopt.h"
25 #include "perfetto/ext/base/unix_task_runner.h"
26 #include "perfetto/ext/base/utils.h"
27 #include "perfetto/ext/base/version.h"
28 #include "perfetto/ext/traced/traced.h"
29 #include "perfetto/tracing/default_socket.h"
30
31 #include "src/traced/probes/ftrace/ftrace_procfs.h"
32 #include "src/traced/probes/kmem_activity_trigger.h"
33 #include "src/traced/probes/probes_producer.h"
34
35 namespace perfetto {
36
ProbesMain(int argc,char ** argv)37 int PERFETTO_EXPORT_ENTRYPOINT ProbesMain(int argc, char** argv) {
38 enum LongOption {
39 OPT_CLEANUP_AFTER_CRASH = 1000,
40 OPT_VERSION,
41 OPT_BACKGROUND,
42 OPT_RESET_FTRACE,
43 };
44
45 bool background = false;
46 bool reset_ftrace = false;
47
48 static const option long_options[] = {
49 {"background", no_argument, nullptr, OPT_BACKGROUND},
50 {"cleanup-after-crash", no_argument, nullptr, OPT_CLEANUP_AFTER_CRASH},
51 {"reset-ftrace", no_argument, nullptr, OPT_RESET_FTRACE},
52 {"version", no_argument, nullptr, OPT_VERSION},
53 {nullptr, 0, nullptr, 0}};
54
55 for (;;) {
56 int option = getopt_long(argc, argv, "", long_options, nullptr);
57 if (option == -1)
58 break;
59 switch (option) {
60 case OPT_BACKGROUND:
61 background = true;
62 break;
63 case OPT_CLEANUP_AFTER_CRASH:
64 // Used by perfetto.rc in Android.
65 PERFETTO_LOG("Hard resetting ftrace state.");
66 HardResetFtraceState();
67 return 0;
68 case OPT_RESET_FTRACE:
69 // This is like --cleanup-after-crash but doesn't quit.
70 reset_ftrace = true;
71 break;
72 case OPT_VERSION:
73 printf("%s\n", base::GetVersionString());
74 return 0;
75 default:
76 fprintf(
77 stderr,
78 "Usage: %s [--background] [--reset-ftrace] [--cleanup-after-crash] "
79 "[--version]\n",
80 argv[0]);
81 return 1;
82 }
83 }
84
85 if (reset_ftrace && !HardResetFtraceState()) {
86 PERFETTO_ELOG(
87 "Failed to reset ftrace. Either run this as root or run "
88 "`sudo chown -R $USER /sys/kernel/tracing`");
89 }
90
91 if (background) {
92 base::Daemonize([] { return 0; });
93 }
94
95 base::Watchdog* watchdog = base::Watchdog::GetInstance();
96 // The memory watchdog will be updated soon after connect, once the shmem
97 // buffer size is known, in ProbesProducer::OnTracingSetup().
98 watchdog->SetMemoryLimit(base::kWatchdogDefaultMemorySlack,
99 base::kWatchdogDefaultMemoryWindow);
100 watchdog->SetCpuLimit(base::kWatchdogDefaultCpuLimit,
101 base::kWatchdogDefaultCpuWindow);
102 watchdog->Start();
103
104 PERFETTO_LOG("Starting %s service", argv[0]);
105
106 // This environment variable is set by Android's init to a fd to /dev/kmsg
107 // opened for writing (see perfetto.rc). We cannot open the file directly
108 // due to permissions.
109 const char* env = getenv("ANDROID_FILE__dev_kmsg");
110 if (env) {
111 FtraceProcfs::g_kmesg_fd = atoi(env);
112 // The file descriptor passed by init doesn't have the FD_CLOEXEC bit set.
113 // Set it so we don't leak this fd while invoking atrace.
114 int res = fcntl(FtraceProcfs::g_kmesg_fd, F_SETFD, FD_CLOEXEC);
115 PERFETTO_DCHECK(res == 0);
116 }
117
118 base::UnixTaskRunner task_runner;
119 ProbesProducer producer;
120 // If the TRACED_PROBES_NOTIFY_FD env var is set, write 1 and close the FD,
121 // when all data sources have been registered. This is used for //src/tracebox
122 // --background-wait, to make sure that the data sources are registered before
123 // waiting for them to be started.
124 const char* env_notif = getenv("TRACED_PROBES_NOTIFY_FD");
125 if (env_notif) {
126 int notif_fd = atoi(env_notif);
127 producer.SetAllDataSourcesRegisteredCb([notif_fd] {
128 PERFETTO_CHECK(base::WriteAll(notif_fd, "1", 1) == 1);
129 PERFETTO_CHECK(base::CloseFile(notif_fd) == 0);
130 });
131 }
132 producer.ConnectWithRetries(GetProducerSocket(), &task_runner);
133
134 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
135 // Start the thread that polls mm_event instance and triggers
136 KmemActivityTrigger kmem_activity_trigger;
137 #endif
138
139 task_runner.Run();
140 return 0;
141 }
142
143 } // namespace perfetto
144