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