blob: 1d8a96850c4be8721705355c4468ff5f6f52e6e3 [file] [log] [blame]
Primiano Tuccie4d44912018-01-10 12:14:50 +00001/*
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 "Log.h"
18
19#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" // Alert
20
21#include <android-base/unique_fd.h>
22#include <errno.h>
Primiano Tuccia7455602018-01-24 19:53:52 +000023#include <fcntl.h>
Primiano Tuccie4d44912018-01-10 12:14:50 +000024#include <sys/types.h>
25#include <sys/wait.h>
26#include <unistd.h>
27
28#include <string>
29
30namespace {
31const char kDropboxTag[] = "perfetto";
32}
33
34namespace android {
35namespace os {
36namespace statsd {
37
38bool CollectPerfettoTraceAndUploadToDropbox(const PerfettoDetails& config) {
39 ALOGD("Starting trace collection through perfetto");
40
41 if (!config.has_trace_config()) {
42 ALOGE("The perfetto trace config is empty, aborting");
43 return false;
44 }
45
46 android::base::unique_fd readPipe;
47 android::base::unique_fd writePipe;
48 if (!android::base::Pipe(&readPipe, &writePipe)) {
49 ALOGE("pipe() failed while calling the Perfetto client: %s", strerror(errno));
50 return false;
51 }
52
53 pid_t pid = fork();
54 if (pid < 0) {
55 ALOGE("fork() failed while calling the Perfetto client: %s", strerror(errno));
56 return false;
57 }
58
59 if (pid == 0) {
60 // Child process.
61
62 // No malloc calls or library calls after this point. Remember that even
63 // ALOGx (aka android_printLog()) can use dynamic memory for vsprintf().
64
65 writePipe.reset(); // Close the write end (owned by the main process).
66
67 // Replace stdin with |readPipe| so the main process can write into it.
68 if (dup2(readPipe.get(), STDIN_FILENO) < 0) _exit(1);
Primiano Tuccia7455602018-01-24 19:53:52 +000069 readPipe.reset();
70
71 // Replace stdout/stderr with /dev/null and close any other file
72 // descriptor. This is to avoid SELinux complaining about perfetto
73 // trying to access files accidentally left open by statsd (i.e. files
74 // that have been opened without the O_CLOEXEC flag).
75 int devNullFd = open("/dev/null", O_RDWR | O_CLOEXEC);
76 if (dup2(devNullFd, STDOUT_FILENO) < 0) _exit(2);
77 if (dup2(devNullFd, STDERR_FILENO) < 0) _exit(3);
78 close(devNullFd);
79 for (int i = 0; i < 1024; i++) {
80 if (i != STDIN_FILENO && i != STDOUT_FILENO && i != STDERR_FILENO) close(i);
81 }
82
Primiano Tuccie4d44912018-01-10 12:14:50 +000083 execl("/system/bin/perfetto", "perfetto", "--background", "--config", "-", "--dropbox",
84 kDropboxTag, nullptr);
85
Primiano Tuccia7455602018-01-24 19:53:52 +000086 // execl() doesn't return in case of success, if we get here something
87 // failed.
88 _exit(4);
Primiano Tuccie4d44912018-01-10 12:14:50 +000089 }
90
91 // Main process.
92
93 readPipe.reset(); // Close the read end (owned by the child process).
94
95 // Using fopen() because fwrite() has the right logic to chunking write()
96 // over a pipe (see __sfvwrite()).
97 FILE* writePipeStream = fdopen(writePipe.get(), "wb");
98 if (!writePipeStream) {
99 ALOGE("fdopen() failed while calling the Perfetto client: %s", strerror(errno));
100 return false;
101 }
102
103 std::string cfgProto = config.trace_config().SerializeAsString();
104 size_t bytesWritten = fwrite(cfgProto.data(), 1, cfgProto.size(), writePipeStream);
105 fclose(writePipeStream);
106 if (bytesWritten != cfgProto.size() || cfgProto.size() == 0) {
107 ALOGE("fwrite() failed (ret: %zd) while calling the Perfetto client: %s", bytesWritten,
108 strerror(errno));
109 return false;
110 }
111
Primiano Tuccia7455602018-01-24 19:53:52 +0000112 // This does NOT wait for the full duration of the trace. It just waits until
113 // the process has read the config from stdin and detached.
Primiano Tuccie4d44912018-01-10 12:14:50 +0000114 int childStatus = 0;
115 waitpid(pid, &childStatus, 0);
116 if (!WIFEXITED(childStatus) || WEXITSTATUS(childStatus) != 0) {
117 ALOGE("Child process failed (0x%x) while calling the Perfetto client", childStatus);
118 return false;
119 }
120
121 ALOGD("CollectPerfettoTraceAndUploadToDropbox() succeeded");
122 return true;
123}
124
125} // namespace statsd
126} // namespace os
127} // namespace android