blob: 05544837b752ac48f40319cb953b81395b17011c [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
Sami Kyostilad0dd6c72018-03-22 19:09:02 +000018#include "config/ConfigKey.h"
Primiano Tuccie4d44912018-01-10 12:14:50 +000019#include "Log.h"
20
21#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" // Alert
22
23#include <android-base/unique_fd.h>
24#include <errno.h>
Primiano Tuccia7455602018-01-24 19:53:52 +000025#include <fcntl.h>
Sami Kyostilad0dd6c72018-03-22 19:09:02 +000026#include <inttypes.h>
Primiano Tuccie4d44912018-01-10 12:14:50 +000027#include <sys/types.h>
28#include <sys/wait.h>
29#include <unistd.h>
30
31#include <string>
32
33namespace {
34const char kDropboxTag[] = "perfetto";
35}
36
37namespace android {
38namespace os {
39namespace statsd {
40
Sami Kyostilad0dd6c72018-03-22 19:09:02 +000041bool CollectPerfettoTraceAndUploadToDropbox(const PerfettoDetails& config,
42 int64_t alert_id,
43 const ConfigKey& configKey) {
Tej Singh484524a2018-02-01 15:10:05 -080044 VLOG("Starting trace collection through perfetto");
Primiano Tuccie4d44912018-01-10 12:14:50 +000045
46 if (!config.has_trace_config()) {
47 ALOGE("The perfetto trace config is empty, aborting");
48 return false;
49 }
50
Sami Kyostilad0dd6c72018-03-22 19:09:02 +000051 char alertId[20];
52 char configId[20];
53 char configUid[20];
54 snprintf(alertId, sizeof(alertId), "%" PRId64, alert_id);
55 snprintf(configId, sizeof(configId), "%" PRId64, configKey.GetId());
56 snprintf(configUid, sizeof(configUid), "%d", configKey.GetUid());
57
Primiano Tuccie4d44912018-01-10 12:14:50 +000058 android::base::unique_fd readPipe;
59 android::base::unique_fd writePipe;
60 if (!android::base::Pipe(&readPipe, &writePipe)) {
61 ALOGE("pipe() failed while calling the Perfetto client: %s", strerror(errno));
62 return false;
63 }
64
65 pid_t pid = fork();
66 if (pid < 0) {
67 ALOGE("fork() failed while calling the Perfetto client: %s", strerror(errno));
68 return false;
69 }
70
71 if (pid == 0) {
72 // Child process.
73
74 // No malloc calls or library calls after this point. Remember that even
75 // ALOGx (aka android_printLog()) can use dynamic memory for vsprintf().
76
77 writePipe.reset(); // Close the write end (owned by the main process).
78
79 // Replace stdin with |readPipe| so the main process can write into it.
80 if (dup2(readPipe.get(), STDIN_FILENO) < 0) _exit(1);
Primiano Tuccia7455602018-01-24 19:53:52 +000081 readPipe.reset();
82
83 // Replace stdout/stderr with /dev/null and close any other file
84 // descriptor. This is to avoid SELinux complaining about perfetto
85 // trying to access files accidentally left open by statsd (i.e. files
86 // that have been opened without the O_CLOEXEC flag).
87 int devNullFd = open("/dev/null", O_RDWR | O_CLOEXEC);
88 if (dup2(devNullFd, STDOUT_FILENO) < 0) _exit(2);
89 if (dup2(devNullFd, STDERR_FILENO) < 0) _exit(3);
90 close(devNullFd);
91 for (int i = 0; i < 1024; i++) {
92 if (i != STDIN_FILENO && i != STDOUT_FILENO && i != STDERR_FILENO) close(i);
93 }
94
Primiano Tuccie4d44912018-01-10 12:14:50 +000095 execl("/system/bin/perfetto", "perfetto", "--background", "--config", "-", "--dropbox",
Sami Kyostilad0dd6c72018-03-22 19:09:02 +000096 kDropboxTag, "--alert-id", alertId, "--config-id", configId, "--config-uid",
97 configUid, nullptr);
Primiano Tuccie4d44912018-01-10 12:14:50 +000098
Primiano Tuccia7455602018-01-24 19:53:52 +000099 // execl() doesn't return in case of success, if we get here something
100 // failed.
101 _exit(4);
Primiano Tuccie4d44912018-01-10 12:14:50 +0000102 }
103
104 // Main process.
105
106 readPipe.reset(); // Close the read end (owned by the child process).
107
108 // Using fopen() because fwrite() has the right logic to chunking write()
109 // over a pipe (see __sfvwrite()).
110 FILE* writePipeStream = fdopen(writePipe.get(), "wb");
111 if (!writePipeStream) {
112 ALOGE("fdopen() failed while calling the Perfetto client: %s", strerror(errno));
113 return false;
114 }
115
116 std::string cfgProto = config.trace_config().SerializeAsString();
117 size_t bytesWritten = fwrite(cfgProto.data(), 1, cfgProto.size(), writePipeStream);
118 fclose(writePipeStream);
119 if (bytesWritten != cfgProto.size() || cfgProto.size() == 0) {
120 ALOGE("fwrite() failed (ret: %zd) while calling the Perfetto client: %s", bytesWritten,
121 strerror(errno));
122 return false;
123 }
124
Primiano Tuccia7455602018-01-24 19:53:52 +0000125 // This does NOT wait for the full duration of the trace. It just waits until
126 // the process has read the config from stdin and detached.
Primiano Tuccie4d44912018-01-10 12:14:50 +0000127 int childStatus = 0;
128 waitpid(pid, &childStatus, 0);
129 if (!WIFEXITED(childStatus) || WEXITSTATUS(childStatus) != 0) {
130 ALOGE("Child process failed (0x%x) while calling the Perfetto client", childStatus);
131 return false;
132 }
133
Tej Singh484524a2018-02-01 15:10:05 -0800134 VLOG("CollectPerfettoTraceAndUploadToDropbox() succeeded");
Primiano Tuccie4d44912018-01-10 12:14:50 +0000135 return true;
136}
137
138} // namespace statsd
139} // namespace os
140} // namespace android