blob: 85b660efc956c55e96e1c073a9088f2dd64ff8e9 [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 Kyostila209a59152018-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>
Sami Kyostila209a59152018-03-22 19:09:02 +000024#include <inttypes.h>
Primiano Tuccie4d44912018-01-10 12:14:50 +000025#include <sys/wait.h>
Primiano Tuccie4d44912018-01-10 12:14:50 +000026
27#include <string>
28
29namespace {
30const char kDropboxTag[] = "perfetto";
31}
32
33namespace android {
34namespace os {
35namespace statsd {
36
Sami Kyostila209a59152018-03-22 19:09:02 +000037bool CollectPerfettoTraceAndUploadToDropbox(const PerfettoDetails& config,
Lalit Magantie5755002019-01-14 17:55:16 +000038 int64_t subscription_id,
Sami Kyostila209a59152018-03-22 19:09:02 +000039 int64_t alert_id,
40 const ConfigKey& configKey) {
Tej Singh484524a2018-02-01 15:10:05 -080041 VLOG("Starting trace collection through perfetto");
Primiano Tuccie4d44912018-01-10 12:14:50 +000042
43 if (!config.has_trace_config()) {
44 ALOGE("The perfetto trace config is empty, aborting");
45 return false;
46 }
47
Lalit Magantie5755002019-01-14 17:55:16 +000048 char subscriptionId[25];
49 char alertId[25];
50 char configId[25];
51 char configUid[25];
52 snprintf(subscriptionId, sizeof(subscriptionId), "%" PRId64, subscription_id);
Sami Kyostila209a59152018-03-22 19:09:02 +000053 snprintf(alertId, sizeof(alertId), "%" PRId64, alert_id);
54 snprintf(configId, sizeof(configId), "%" PRId64, configKey.GetId());
55 snprintf(configUid, sizeof(configUid), "%d", configKey.GetUid());
56
Primiano Tuccie4d44912018-01-10 12:14:50 +000057 android::base::unique_fd readPipe;
58 android::base::unique_fd writePipe;
59 if (!android::base::Pipe(&readPipe, &writePipe)) {
60 ALOGE("pipe() failed while calling the Perfetto client: %s", strerror(errno));
61 return false;
62 }
63
64 pid_t pid = fork();
65 if (pid < 0) {
66 ALOGE("fork() failed while calling the Perfetto client: %s", strerror(errno));
67 return false;
68 }
69
70 if (pid == 0) {
71 // Child process.
72
73 // No malloc calls or library calls after this point. Remember that even
74 // ALOGx (aka android_printLog()) can use dynamic memory for vsprintf().
75
76 writePipe.reset(); // Close the write end (owned by the main process).
77
78 // Replace stdin with |readPipe| so the main process can write into it.
79 if (dup2(readPipe.get(), STDIN_FILENO) < 0) _exit(1);
Primiano Tuccia7455602018-01-24 19:53:52 +000080 readPipe.reset();
81
82 // Replace stdout/stderr with /dev/null and close any other file
83 // descriptor. This is to avoid SELinux complaining about perfetto
84 // trying to access files accidentally left open by statsd (i.e. files
85 // that have been opened without the O_CLOEXEC flag).
86 int devNullFd = open("/dev/null", O_RDWR | O_CLOEXEC);
87 if (dup2(devNullFd, STDOUT_FILENO) < 0) _exit(2);
88 if (dup2(devNullFd, STDERR_FILENO) < 0) _exit(3);
89 close(devNullFd);
90 for (int i = 0; i < 1024; i++) {
91 if (i != STDIN_FILENO && i != STDOUT_FILENO && i != STDERR_FILENO) close(i);
92 }
93
Primiano Tuccie4d44912018-01-10 12:14:50 +000094 execl("/system/bin/perfetto", "perfetto", "--background", "--config", "-", "--dropbox",
Sami Kyostila209a59152018-03-22 19:09:02 +000095 kDropboxTag, "--alert-id", alertId, "--config-id", configId, "--config-uid",
Lalit Magantie5755002019-01-14 17:55:16 +000096 configUid, "--subscription-id", subscriptionId, nullptr);
Primiano Tuccie4d44912018-01-10 12:14:50 +000097
Primiano Tuccia7455602018-01-24 19:53:52 +000098 // execl() doesn't return in case of success, if we get here something
99 // failed.
100 _exit(4);
Primiano Tuccie4d44912018-01-10 12:14:50 +0000101 }
102
103 // Main process.
104
105 readPipe.reset(); // Close the read end (owned by the child process).
106
Josh Gao3330d122018-09-04 11:12:04 -0700107 // Using fdopen() because fwrite() has the right logic to chunking write()
Primiano Tuccie4d44912018-01-10 12:14:50 +0000108 // over a pipe (see __sfvwrite()).
Josh Gao3330d122018-09-04 11:12:04 -0700109 FILE* writePipeStream = android::base::Fdopen(std::move(writePipe), "wb");
Primiano Tuccie4d44912018-01-10 12:14:50 +0000110 if (!writePipeStream) {
111 ALOGE("fdopen() failed while calling the Perfetto client: %s", strerror(errno));
112 return false;
113 }
114
Primiano Tucci65c72fc2018-07-17 16:53:14 +0100115 const std::string& cfgProto = config.trace_config();
Primiano Tuccie4d44912018-01-10 12:14:50 +0000116 size_t bytesWritten = fwrite(cfgProto.data(), 1, cfgProto.size(), writePipeStream);
117 fclose(writePipeStream);
118 if (bytesWritten != cfgProto.size() || cfgProto.size() == 0) {
119 ALOGE("fwrite() failed (ret: %zd) while calling the Perfetto client: %s", bytesWritten,
120 strerror(errno));
121 return false;
122 }
123
Primiano Tuccia7455602018-01-24 19:53:52 +0000124 // This does NOT wait for the full duration of the trace. It just waits until
125 // the process has read the config from stdin and detached.
Primiano Tuccie4d44912018-01-10 12:14:50 +0000126 int childStatus = 0;
127 waitpid(pid, &childStatus, 0);
128 if (!WIFEXITED(childStatus) || WEXITSTATUS(childStatus) != 0) {
129 ALOGE("Child process failed (0x%x) while calling the Perfetto client", childStatus);
130 return false;
131 }
132
Tej Singh484524a2018-02-01 15:10:05 -0800133 VLOG("CollectPerfettoTraceAndUploadToDropbox() succeeded");
Primiano Tuccie4d44912018-01-10 12:14:50 +0000134 return true;
135}
136
137} // namespace statsd
138} // namespace os
139} // namespace android