1 /*
2 * Copyright (C) 2018 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 <stdlib.h>
18 #include <unistd.h>
19 #include <array>
20 #include <memory>
21 #include <vector>
22
23 #include <getopt.h>
24 #include <signal.h>
25
26 #include "perfetto/base/event.h"
27 #include "perfetto/base/scoped_file.h"
28 #include "perfetto/base/unix_socket.h"
29 #include "perfetto/base/watchdog.h"
30 #include "src/profiling/memory/heapprofd_producer.h"
31 #include "src/profiling/memory/wire_protocol.h"
32 #include "src/tracing/ipc/default_socket.h"
33
34 #include "perfetto/base/unix_task_runner.h"
35
36 // TODO(rsavitski): the task runner watchdog spawns a thread (normally for
37 // tracking cpu/mem usage) that we don't strictly need.
38
39 namespace perfetto {
40 namespace profiling {
41 namespace {
42
43 int StartChildHeapprofd(pid_t target_pid,
44 std::string target_cmdline,
45 base::ScopedFile inherited_sock_fd);
46 int StartCentralHeapprofd();
47
48 base::Event* g_dump_evt = nullptr;
49
HeapprofdMain(int argc,char ** argv)50 int HeapprofdMain(int argc, char** argv) {
51 bool cleanup_crash = false;
52 pid_t target_pid = base::kInvalidPid;
53 std::string target_cmdline;
54 base::ScopedFile inherited_sock_fd;
55
56 enum { kCleanupCrash = 256, kTargetPid, kTargetCmd, kInheritFd };
57 static struct option long_options[] = {
58 {"cleanup-after-crash", no_argument, nullptr, kCleanupCrash},
59 {"exclusive-for-pid", required_argument, nullptr, kTargetPid},
60 {"exclusive-for-cmdline", required_argument, nullptr, kTargetCmd},
61 {"inherit-socket-fd", required_argument, nullptr, kInheritFd},
62 {nullptr, 0, nullptr, 0}};
63 int option_index;
64 int c;
65 while ((c = getopt_long(argc, argv, "", long_options, &option_index)) != -1) {
66 switch (c) {
67 case kCleanupCrash:
68 cleanup_crash = true;
69 break;
70 case kTargetPid:
71 if (target_pid != base::kInvalidPid)
72 PERFETTO_FATAL("Duplicate exclusive-for-pid");
73 target_pid = static_cast<pid_t>(atoi(optarg));
74 break;
75 case kTargetCmd: // assumed to be already normalized
76 if (!target_cmdline.empty())
77 PERFETTO_FATAL("Duplicate exclusive-for-cmdline");
78 target_cmdline = std::string(optarg);
79 break;
80 case kInheritFd: // repetition not supported
81 if (inherited_sock_fd)
82 PERFETTO_FATAL("Duplicate inherit-socket-fd");
83 inherited_sock_fd = base::ScopedFile(atoi(optarg));
84 break;
85 default:
86 PERFETTO_ELOG("Usage: %s [--cleanup-after-crash]", argv[0]);
87 return 1;
88 }
89 }
90
91 if (cleanup_crash) {
92 PERFETTO_LOG(
93 "Recovering from crash: unsetting heapprofd system properties. "
94 "Expect SELinux denials for unrelated properties.");
95 SystemProperties::ResetHeapprofdProperties();
96 PERFETTO_LOG(
97 "Finished unsetting heapprofd system properties. "
98 "SELinux denials about properties are unexpected after "
99 "this point.");
100 return 0;
101 }
102
103 // If |target_pid| is given, we're supposed to be operating as a private
104 // heapprofd for that process. Note that we might not be a direct child due to
105 // reparenting.
106 bool tpid_set = target_pid != base::kInvalidPid;
107 bool tcmd_set = !target_cmdline.empty();
108 bool fds_set = !!inherited_sock_fd;
109 if (tpid_set || tcmd_set || fds_set) {
110 if (!tpid_set || !tcmd_set || !fds_set) {
111 PERFETTO_ELOG(
112 "If starting in child mode, requires all of: {--exclusive-for-pid, "
113 "--exclusive-for-cmdline, --inherit_socket_fd}");
114 return 1;
115 }
116
117 return StartChildHeapprofd(target_pid, target_cmdline,
118 std::move(inherited_sock_fd));
119 }
120
121 // Otherwise start as a central daemon.
122 return StartCentralHeapprofd();
123 }
124
StartChildHeapprofd(pid_t target_pid,std::string target_cmdline,base::ScopedFile inherited_sock_fd)125 int StartChildHeapprofd(pid_t target_pid,
126 std::string target_cmdline,
127 base::ScopedFile inherited_sock_fd) {
128 base::UnixTaskRunner task_runner;
129 base::Watchdog::GetInstance()->Start(); // crash on exceedingly long tasks
130 HeapprofdProducer producer(HeapprofdMode::kChild, &task_runner);
131 producer.SetTargetProcess(target_pid, target_cmdline,
132 std::move(inherited_sock_fd));
133 producer.ConnectWithRetries(GetProducerSocket());
134 producer.ScheduleActiveDataSourceWatchdog();
135 task_runner.Run();
136 return 0;
137 }
138
StartCentralHeapprofd()139 int StartCentralHeapprofd() {
140 // We set this up before launching any threads, so we do not have to use a
141 // std::atomic for g_dump_evt.
142 g_dump_evt = new base::Event();
143
144 base::UnixTaskRunner task_runner;
145 base::Watchdog::GetInstance()->Start(); // crash on exceedingly long tasks
146 HeapprofdProducer producer(HeapprofdMode::kCentral, &task_runner);
147
148 struct sigaction action = {};
149 action.sa_handler = [](int) { g_dump_evt->Notify(); };
150 // Allow to trigger a full dump by sending SIGUSR1 to heapprofd.
151 // This will allow manually deciding when to dump on userdebug.
152 PERFETTO_CHECK(sigaction(SIGUSR1, &action, nullptr) == 0);
153 task_runner.AddFileDescriptorWatch(g_dump_evt->fd(), [&producer] {
154 g_dump_evt->Clear();
155 producer.DumpAll();
156 });
157 producer.ConnectWithRetries(GetProducerSocket());
158 task_runner.Run();
159 return 0;
160 }
161
162 } // namespace
163 } // namespace profiling
164 } // namespace perfetto
165
main(int argc,char ** argv)166 int main(int argc, char** argv) {
167 return perfetto::profiling::HeapprofdMain(argc, argv);
168 }
169