blob: d79123b6a972e5f9a2912d92e75af314565da57d [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
2 * Copyright (C) 2016 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 */
Yi Jin4e843102018-02-14 15:36:18 -080016#define DEBUG false
Yi Jinb592e3b2018-02-01 15:17:04 -080017#include "Log.h"
Joe Onorato1754d742016-11-21 17:51:35 -080018
19#include "Section.h"
Yi Jin99c248f2017-08-25 18:11:58 -070020
Kweku Adamseadd1232018-02-05 16:45:13 -080021#include <dirent.h>
22#include <errno.h>
Yi Jin3c034c92017-12-22 17:36:47 -080023#include <mutex>
Kweku Adamseadd1232018-02-05 16:45:13 -080024#include <set>
Mike Ma5510f7c2020-02-19 02:56:04 -080025#include <thread>
Joe Onorato1754d742016-11-21 17:51:35 -080026
Yi Jinb592e3b2018-02-01 15:17:04 -080027#include <android-base/file.h>
Joe Onoratofe7bbf42019-03-24 20:57:16 -070028#include <android-base/properties.h>
Kweku Adamseadd1232018-02-05 16:45:13 -080029#include <android-base/stringprintf.h>
Yi Jinc23fad22017-09-15 17:24:59 -070030#include <android/util/protobuf.h>
Joe Onorato99598ee2019-02-11 15:55:13 +000031#include <android/util/ProtoOutputStream.h>
Joe Onorato1754d742016-11-21 17:51:35 -080032#include <binder/IServiceManager.h>
Kweku Adamseadd1232018-02-05 16:45:13 -080033#include <debuggerd/client.h>
34#include <dumputils/dump_utils.h>
Yi Jin3c034c92017-12-22 17:36:47 -080035#include <log/log_event_list.h>
Yi Jin3c034c92017-12-22 17:36:47 -080036#include <log/log_read.h>
Yi Jinb592e3b2018-02-01 15:17:04 -080037#include <log/logprint.h>
Yi Jin3c034c92017-12-22 17:36:47 -080038#include <private/android_logger.h>
39
40#include "FdBuffer.h"
Yi Jin3c034c92017-12-22 17:36:47 -080041#include "Privacy.h"
Kweku Adamseadd1232018-02-05 16:45:13 -080042#include "frameworks/base/core/proto/android/os/backtrace.proto.h"
Yi Jin1a11fa12018-02-22 16:44:10 -080043#include "frameworks/base/core/proto/android/os/data.proto.h"
Yi Jinb592e3b2018-02-01 15:17:04 -080044#include "frameworks/base/core/proto/android/util/log.proto.h"
Mike Ma5510f7c2020-02-19 02:56:04 -080045#include "frameworks/base/core/proto/android/util/textdump.proto.h"
Yi Jinb592e3b2018-02-01 15:17:04 -080046#include "incidentd_util.h"
Joe Onorato1754d742016-11-21 17:51:35 -080047
Yi Jin6cacbcb2018-03-30 14:04:52 -070048namespace android {
49namespace os {
50namespace incidentd {
51
Yi Jinb592e3b2018-02-01 15:17:04 -080052using namespace android::base;
Yi Jinc23fad22017-09-15 17:24:59 -070053using namespace android::util;
Joe Onorato1754d742016-11-21 17:51:35 -080054
Yi Jinc23fad22017-09-15 17:24:59 -070055// special section ids
Yi Jin329130b2018-02-09 16:47:47 -080056const int FIELD_ID_INCIDENT_METADATA = 2;
Yi Jinc23fad22017-09-15 17:24:59 -070057
58// incident section parameters
Yi Jin3c034c92017-12-22 17:36:47 -080059const char INCIDENT_HELPER[] = "/system/bin/incident_helper";
Yi Jinc36e91d2018-03-08 11:25:58 -080060const char* GZIP[] = {"/system/bin/gzip", NULL};
Yi Jinb44f7d42017-07-21 12:12:59 -070061
Yi Jin1a11fa12018-02-22 16:44:10 -080062static pid_t fork_execute_incident_helper(const int id, Fpipe* p2cPipe, Fpipe* c2pPipe) {
Yi Jinb592e3b2018-02-01 15:17:04 -080063 const char* ihArgs[]{INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL};
Yi Jinc36e91d2018-03-08 11:25:58 -080064 return fork_execute_cmd(const_cast<char**>(ihArgs), p2cPipe, c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -070065}
66
Joe Onorato7a406b42019-04-12 18:11:30 -070067bool section_requires_specific_mention(int sectionId) {
68 switch (sectionId) {
69 case 3025: // restricted_images
70 return true;
Ryan Savitskicc7d9972019-06-03 23:57:09 +010071 case 3026: // system_trace
72 return true;
Joe Onorato7a406b42019-04-12 18:11:30 -070073 default:
74 return false;
75 }
76}
77
Yi Jin99c248f2017-08-25 18:11:58 -070078// ================================================================================
Joe Onoratofe7bbf42019-03-24 20:57:16 -070079Section::Section(int i, int64_t timeoutMs)
Kweku Adams3d160912018-05-07 11:26:27 -070080 : id(i),
Joe Onoratofe7bbf42019-03-24 20:57:16 -070081 timeoutMs(timeoutMs) {
82}
Joe Onorato1754d742016-11-21 17:51:35 -080083
Yi Jinb592e3b2018-02-01 15:17:04 -080084Section::~Section() {}
Joe Onorato1754d742016-11-21 17:51:35 -080085
Joe Onorato1754d742016-11-21 17:51:35 -080086// ================================================================================
Yi Jin1a11fa12018-02-22 16:44:10 -080087static inline bool isSysfs(const char* filename) { return strncmp(filename, "/sys/", 5) == 0; }
88
Kweku Adamse04ef772018-06-13 12:24:38 -070089FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs)
Joe Onoratofe7bbf42019-03-24 20:57:16 -070090 : Section(id, timeoutMs), mFilename(filename) {
Yi Jin3be0b432018-04-20 17:08:11 -070091 name = "file ";
92 name += filename;
Yi Jin1a11fa12018-02-22 16:44:10 -080093 mIsSysfs = isSysfs(filename);
Yi Jin0a3406f2017-06-22 19:23:11 -070094}
95
96FileSection::~FileSection() {}
97
Joe Onorato99598ee2019-02-11 15:55:13 +000098status_t FileSection::Execute(ReportWriter* writer) const {
Yi Jinb44f7d42017-07-21 12:12:59 -070099 // read from mFilename first, make sure the file is available
100 // add O_CLOEXEC to make sure it is closed when exec incident helper
Yi Jin6355d2f2018-03-14 15:18:02 -0700101 unique_fd fd(open(mFilename, O_RDONLY | O_CLOEXEC));
102 if (fd.get() == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700103 ALOGW("[%s] failed to open file", this->name.string());
Kweku Adamse04ef772018-06-13 12:24:38 -0700104 // There may be some devices/architectures that won't have the file.
105 // Just return here without an error.
106 return NO_ERROR;
Yi Jin0a3406f2017-06-22 19:23:11 -0700107 }
108
Yi Jinb44f7d42017-07-21 12:12:59 -0700109 FdBuffer buffer;
110 Fpipe p2cPipe;
111 Fpipe c2pPipe;
112 // initiate pipes to pass data to/from incident_helper
113 if (!p2cPipe.init() || !c2pPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700114 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jin0a3406f2017-06-22 19:23:11 -0700115 return -errno;
116 }
117
Yi Jin1a11fa12018-02-22 16:44:10 -0800118 pid_t pid = fork_execute_incident_helper(this->id, &p2cPipe, &c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700119 if (pid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700120 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700121 return -errno;
122 }
123
124 // parent process
Yi Jine3dab2d2018-03-22 16:56:39 -0700125 status_t readStatus = buffer.readProcessedDataInStream(fd.get(), std::move(p2cPipe.writeFd()),
126 std::move(c2pPipe.readFd()),
127 this->timeoutMs, mIsSysfs);
Joe Onorato99598ee2019-02-11 15:55:13 +0000128 writer->setSectionStats(buffer);
Yi Jinb44f7d42017-07-21 12:12:59 -0700129 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700130 ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800131 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800132 kill_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700133 return readStatus;
134 }
135
Yi Jinedfd5bb2017-09-06 17:09:11 -0700136 status_t ihStatus = wait_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700137 if (ihStatus != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700138 ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-ihStatus));
Mike Ma5510f7c2020-02-19 02:56:04 -0800139 return OK; // Not a fatal error.
Yi Jinb44f7d42017-07-21 12:12:59 -0700140 }
141
Joe Onorato99598ee2019-02-11 15:55:13 +0000142 return writer->writeSection(buffer);
Yi Jin0a3406f2017-06-22 19:23:11 -0700143}
Yi Jin1a11fa12018-02-22 16:44:10 -0800144// ================================================================================
145GZipSection::GZipSection(int id, const char* filename, ...) : Section(id) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800146 va_list args;
147 va_start(args, filename);
148 mFilenames = varargs(filename, args);
149 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800150 name = "gzip";
151 for (int i = 0; mFilenames[i] != NULL; i++) {
152 name += " ";
153 name += mFilenames[i];
154 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800155}
Yi Jin0a3406f2017-06-22 19:23:11 -0700156
Yi Jin480de782018-04-06 15:37:36 -0700157GZipSection::~GZipSection() { free(mFilenames); }
Yi Jin1a11fa12018-02-22 16:44:10 -0800158
Joe Onorato99598ee2019-02-11 15:55:13 +0000159status_t GZipSection::Execute(ReportWriter* writer) const {
Yi Jin1a11fa12018-02-22 16:44:10 -0800160 // Reads the files in order, use the first available one.
161 int index = 0;
Yi Jin6355d2f2018-03-14 15:18:02 -0700162 unique_fd fd;
Yi Jin1a11fa12018-02-22 16:44:10 -0800163 while (mFilenames[index] != NULL) {
Yi Jin6355d2f2018-03-14 15:18:02 -0700164 fd.reset(open(mFilenames[index], O_RDONLY | O_CLOEXEC));
165 if (fd.get() != -1) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800166 break;
167 }
168 ALOGW("GZipSection failed to open file %s", mFilenames[index]);
169 index++; // look at the next file.
170 }
Yi Jinc858e272018-03-28 15:32:50 -0700171 if (fd.get() == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700172 ALOGW("[%s] can't open all the files", this->name.string());
Yi Jin6cacbcb2018-03-30 14:04:52 -0700173 return NO_ERROR; // e.g. LAST_KMSG will reach here in user build.
Yi Jinc858e272018-03-28 15:32:50 -0700174 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800175 FdBuffer buffer;
176 Fpipe p2cPipe;
177 Fpipe c2pPipe;
178 // initiate pipes to pass data to/from gzip
179 if (!p2cPipe.init() || !c2pPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700180 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jin1a11fa12018-02-22 16:44:10 -0800181 return -errno;
182 }
183
Yi Jinc36e91d2018-03-08 11:25:58 -0800184 pid_t pid = fork_execute_cmd((char* const*)GZIP, &p2cPipe, &c2pPipe);
Yi Jin1a11fa12018-02-22 16:44:10 -0800185 if (pid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700186 ALOGW("[%s] failed to fork", this->name.string());
Yi Jin1a11fa12018-02-22 16:44:10 -0800187 return -errno;
188 }
189 // parent process
190
191 // construct Fdbuffer to output GZippedfileProto, the reason to do this instead of using
192 // ProtoOutputStream is to avoid allocation of another buffer inside ProtoOutputStream.
Joe Onorato99598ee2019-02-11 15:55:13 +0000193 sp<EncodedBuffer> internalBuffer = buffer.data();
Yi Jin1a11fa12018-02-22 16:44:10 -0800194 internalBuffer->writeHeader((uint32_t)GZippedFileProto::FILENAME, WIRE_TYPE_LENGTH_DELIMITED);
Yi Jina9635e42018-03-23 12:05:32 -0700195 size_t fileLen = strlen(mFilenames[index]);
196 internalBuffer->writeRawVarint32(fileLen);
197 for (size_t i = 0; i < fileLen; i++) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800198 internalBuffer->writeRawByte(mFilenames[index][i]);
199 }
200 internalBuffer->writeHeader((uint32_t)GZippedFileProto::GZIPPED_DATA,
201 WIRE_TYPE_LENGTH_DELIMITED);
202 size_t editPos = internalBuffer->wp()->pos();
203 internalBuffer->wp()->move(8); // reserve 8 bytes for the varint of the data size.
204 size_t dataBeginAt = internalBuffer->wp()->pos();
Yi Jin3be0b432018-04-20 17:08:11 -0700205 VLOG("[%s] editPos=%zu, dataBeginAt=%zu", this->name.string(), editPos, dataBeginAt);
Yi Jin1a11fa12018-02-22 16:44:10 -0800206
Yi Jine3dab2d2018-03-22 16:56:39 -0700207 status_t readStatus = buffer.readProcessedDataInStream(
208 fd.get(), std::move(p2cPipe.writeFd()), std::move(c2pPipe.readFd()), this->timeoutMs,
209 isSysfs(mFilenames[index]));
Joe Onorato99598ee2019-02-11 15:55:13 +0000210 writer->setSectionStats(buffer);
Yi Jin1a11fa12018-02-22 16:44:10 -0800211 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700212 ALOGW("[%s] failed to read data from gzip: %s, timedout: %s", this->name.string(),
213 strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin1a11fa12018-02-22 16:44:10 -0800214 kill_child(pid);
215 return readStatus;
216 }
217
218 status_t gzipStatus = wait_child(pid);
219 if (gzipStatus != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700220 ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-gzipStatus));
Yi Jin1a11fa12018-02-22 16:44:10 -0800221 return gzipStatus;
222 }
223 // Revisit the actual size from gzip result and edit the internal buffer accordingly.
224 size_t dataSize = buffer.size() - dataBeginAt;
225 internalBuffer->wp()->rewind()->move(editPos);
226 internalBuffer->writeRawVarint32(dataSize);
227 internalBuffer->copy(dataBeginAt, dataSize);
Yi Jin1a11fa12018-02-22 16:44:10 -0800228
Joe Onorato99598ee2019-02-11 15:55:13 +0000229 return writer->writeSection(buffer);
Yi Jin1a11fa12018-02-22 16:44:10 -0800230}
Kweku Adamseadd1232018-02-05 16:45:13 -0800231
Yi Jin0a3406f2017-06-22 19:23:11 -0700232// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800233struct WorkerThreadData : public virtual RefBase {
Joe Onorato1754d742016-11-21 17:51:35 -0800234 const WorkerThreadSection* section;
Yi Jin6355d2f2018-03-14 15:18:02 -0700235 Fpipe pipe;
Joe Onorato1754d742016-11-21 17:51:35 -0800236
237 // Lock protects these fields
Mike Ma5510f7c2020-02-19 02:56:04 -0800238 std::mutex lock;
Joe Onorato1754d742016-11-21 17:51:35 -0800239 bool workerDone;
240 status_t workerError;
241
Chih-Hung Hsieh7a88a932018-12-20 13:45:04 -0800242 explicit WorkerThreadData(const WorkerThreadSection* section);
Joe Onorato1754d742016-11-21 17:51:35 -0800243 virtual ~WorkerThreadData();
Joe Onorato1754d742016-11-21 17:51:35 -0800244};
245
246WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec)
Yi Jin6355d2f2018-03-14 15:18:02 -0700247 : section(sec), workerDone(false), workerError(NO_ERROR) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800248
Yi Jinb592e3b2018-02-01 15:17:04 -0800249WorkerThreadData::~WorkerThreadData() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800250
251// ================================================================================
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700252WorkerThreadSection::WorkerThreadSection(int id, const int64_t timeoutMs)
253 : Section(id, timeoutMs) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800254
Yi Jinb592e3b2018-02-01 15:17:04 -0800255WorkerThreadSection::~WorkerThreadSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800256
Kweku Adams5b763c12018-09-13 15:44:58 -0700257void sigpipe_handler(int signum) {
258 if (signum == SIGPIPE) {
259 ALOGE("Wrote to a broken pipe\n");
260 } else {
261 ALOGE("Received unexpected signal: %d\n", signum);
262 }
263}
264
Joe Onorato99598ee2019-02-11 15:55:13 +0000265status_t WorkerThreadSection::Execute(ReportWriter* writer) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800266 status_t err = NO_ERROR;
Mike Ma28381692018-12-04 15:46:29 -0800267 bool workerDone = false;
Joe Onorato1754d742016-11-21 17:51:35 -0800268 FdBuffer buffer;
269
Mike Ma5510f7c2020-02-19 02:56:04 -0800270 // Create shared data and pipe
271 WorkerThreadData data(this);
272 if (!data.pipe.init()) {
Joe Onorato1754d742016-11-21 17:51:35 -0800273 return -errno;
274 }
275
Mike Ma5510f7c2020-02-19 02:56:04 -0800276 std::thread([&]() {
277 // Don't crash the service if writing to a closed pipe (may happen if dumping times out)
278 signal(SIGPIPE, sigpipe_handler);
279 status_t err = data.section->BlockingCall(data.pipe.writeFd());
280 {
281 std::unique_lock<std::mutex> lock(data.lock);
282 data.workerDone = true;
283 data.workerError = err;
284 // unique_fd is not thread safe. If we don't lock it, reset() may pause half way while
285 // the other thread executes to the end, calling ~Fpipe, which is a race condition.
286 data.pipe.writeFd().reset();
287 }
288 }).detach();
Joe Onorato1754d742016-11-21 17:51:35 -0800289
290 // Loop reading until either the timeout or the worker side is done (i.e. eof).
Mike Ma5510f7c2020-02-19 02:56:04 -0800291 err = buffer.read(data.pipe.readFd().get(), this->timeoutMs);
Joe Onorato1754d742016-11-21 17:51:35 -0800292 if (err != NO_ERROR) {
Mike Ma28381692018-12-04 15:46:29 -0800293 ALOGE("[%s] reader failed with error '%s'", this->name.string(), strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800294 }
295
Joe Onorato1754d742016-11-21 17:51:35 -0800296 // If the worker side is finished, then return its error (which may overwrite
Mike Ma28381692018-12-04 15:46:29 -0800297 // our possible error -- but it's more interesting anyway). If not, then we timed out.
Joe Onorato1754d742016-11-21 17:51:35 -0800298 {
Mike Ma5510f7c2020-02-19 02:56:04 -0800299 std::unique_lock<std::mutex> lock(data.lock);
300 data.pipe.close();
301 if (data.workerError != NO_ERROR) {
302 err = data.workerError;
Mike Ma28381692018-12-04 15:46:29 -0800303 ALOGE("[%s] worker failed with error '%s'", this->name.string(), strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800304 }
Mike Ma5510f7c2020-02-19 02:56:04 -0800305 workerDone = data.workerDone;
Joe Onorato1754d742016-11-21 17:51:35 -0800306 }
Kweku Adams5b763c12018-09-13 15:44:58 -0700307
Joe Onorato99598ee2019-02-11 15:55:13 +0000308 writer->setSectionStats(buffer);
Mike Ma28381692018-12-04 15:46:29 -0800309 if (err != NO_ERROR) {
310 char errMsg[128];
311 snprintf(errMsg, 128, "[%s] failed with error '%s'",
312 this->name.string(), strerror(-err));
Joe Onorato99598ee2019-02-11 15:55:13 +0000313 writer->error(this, err, "WorkerThreadSection failed.");
Joe Onorato1754d742016-11-21 17:51:35 -0800314 return NO_ERROR;
315 }
Mike Ma28381692018-12-04 15:46:29 -0800316 if (buffer.truncated()) {
317 ALOGW("[%s] too large, truncating", this->name.string());
Joe Onorato99598ee2019-02-11 15:55:13 +0000318 // Do not write a truncated section. It won't pass through the PrivacyFilter.
Mike Ma28381692018-12-04 15:46:29 -0800319 return NO_ERROR;
320 }
321 if (!workerDone || buffer.timedOut()) {
322 ALOGW("[%s] timed out", this->name.string());
Joe Onorato1754d742016-11-21 17:51:35 -0800323 return NO_ERROR;
324 }
325
326 // Write the data that was collected
Joe Onorato99598ee2019-02-11 15:55:13 +0000327 return writer->writeSection(buffer);
Joe Onorato1754d742016-11-21 17:51:35 -0800328}
329
330// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700331CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...)
Yi Jinb592e3b2018-02-01 15:17:04 -0800332 : Section(id, timeoutMs) {
Joe Onorato1754d742016-11-21 17:51:35 -0800333 va_list args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700334 va_start(args, command);
Yi Jin1a11fa12018-02-22 16:44:10 -0800335 mCommand = varargs(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800336 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800337 name = "cmd";
338 for (int i = 0; mCommand[i] != NULL; i++) {
339 name += " ";
340 name += mCommand[i];
341 }
Yi Jinb44f7d42017-07-21 12:12:59 -0700342}
Joe Onorato1754d742016-11-21 17:51:35 -0800343
Yi Jinb592e3b2018-02-01 15:17:04 -0800344CommandSection::CommandSection(int id, const char* command, ...) : Section(id) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700345 va_list args;
346 va_start(args, command);
Yi Jin1a11fa12018-02-22 16:44:10 -0800347 mCommand = varargs(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800348 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800349 name = "cmd";
350 for (int i = 0; mCommand[i] != NULL; i++) {
351 name += " ";
352 name += mCommand[i];
353 }
Joe Onorato1754d742016-11-21 17:51:35 -0800354}
355
Yi Jinb592e3b2018-02-01 15:17:04 -0800356CommandSection::~CommandSection() { free(mCommand); }
Joe Onorato1754d742016-11-21 17:51:35 -0800357
Joe Onorato99598ee2019-02-11 15:55:13 +0000358status_t CommandSection::Execute(ReportWriter* writer) const {
Yi Jinb44f7d42017-07-21 12:12:59 -0700359 FdBuffer buffer;
360 Fpipe cmdPipe;
361 Fpipe ihPipe;
362
363 if (!cmdPipe.init() || !ihPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700364 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700365 return -errno;
366 }
367
Yi Jinc36e91d2018-03-08 11:25:58 -0800368 pid_t cmdPid = fork_execute_cmd((char* const*)mCommand, NULL, &cmdPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700369 if (cmdPid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700370 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700371 return -errno;
372 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800373 pid_t ihPid = fork_execute_incident_helper(this->id, &cmdPipe, &ihPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700374 if (ihPid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700375 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700376 return -errno;
377 }
378
Yi Jin6355d2f2018-03-14 15:18:02 -0700379 cmdPipe.writeFd().reset();
Yi Jine3dab2d2018-03-22 16:56:39 -0700380 status_t readStatus = buffer.read(ihPipe.readFd().get(), this->timeoutMs);
Joe Onorato99598ee2019-02-11 15:55:13 +0000381 writer->setSectionStats(buffer);
Yi Jinb44f7d42017-07-21 12:12:59 -0700382 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700383 ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800384 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800385 kill_child(cmdPid);
386 kill_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700387 return readStatus;
388 }
389
Kweku Adamseadd1232018-02-05 16:45:13 -0800390 // Waiting for command here has one trade-off: the failed status of command won't be detected
Yi Jin1a11fa12018-02-22 16:44:10 -0800391 // until buffer timeout, but it has advatage on starting the data stream earlier.
Yi Jinedfd5bb2017-09-06 17:09:11 -0700392 status_t cmdStatus = wait_child(cmdPid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800393 status_t ihStatus = wait_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700394 if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000395 ALOGW("[%s] abnormal child processes, return status: command: %s, incident helper: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800396 this->name.string(), strerror(-cmdStatus), strerror(-ihStatus));
Joe Onorato99598ee2019-02-11 15:55:13 +0000397 // Not a fatal error.
398 return NO_ERROR;
Yi Jinb44f7d42017-07-21 12:12:59 -0700399 }
400
Joe Onorato99598ee2019-02-11 15:55:13 +0000401 return writer->writeSection(buffer);
Joe Onorato1754d742016-11-21 17:51:35 -0800402}
403
404// ================================================================================
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700405DumpsysSection::DumpsysSection(int id, const char* service, ...)
406 : WorkerThreadSection(id, REMOTE_CALL_TIMEOUT_MS), mService(service) {
Joe Onorato1754d742016-11-21 17:51:35 -0800407 name = "dumpsys ";
408 name += service;
409
410 va_list args;
411 va_start(args, service);
412 while (true) {
Yi Jin0a3406f2017-06-22 19:23:11 -0700413 const char* arg = va_arg(args, const char*);
Joe Onorato1754d742016-11-21 17:51:35 -0800414 if (arg == NULL) {
415 break;
416 }
417 mArgs.add(String16(arg));
418 name += " ";
419 name += arg;
420 }
421 va_end(args);
422}
423
Yi Jinb592e3b2018-02-01 15:17:04 -0800424DumpsysSection::~DumpsysSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800425
Mike Ma643de922019-12-17 10:56:17 -0800426status_t DumpsysSection::BlockingCall(unique_fd& pipeWriteFd) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800427 // checkService won't wait for the service to show up like getService will.
428 sp<IBinder> service = defaultServiceManager()->checkService(mService);
Yi Jin0a3406f2017-06-22 19:23:11 -0700429
Joe Onorato1754d742016-11-21 17:51:35 -0800430 if (service == NULL) {
Joe Onorato1754d742016-11-21 17:51:35 -0800431 ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string());
Mike Ma28381692018-12-04 15:46:29 -0800432 return NAME_NOT_FOUND;
Joe Onorato1754d742016-11-21 17:51:35 -0800433 }
434
Mike Ma643de922019-12-17 10:56:17 -0800435 service->dump(pipeWriteFd.get(), mArgs);
Joe Onorato1754d742016-11-21 17:51:35 -0800436
437 return NO_ERROR;
438}
Yi Jin3c034c92017-12-22 17:36:47 -0800439
440// ================================================================================
Mike Ma5510f7c2020-02-19 02:56:04 -0800441TextDumpsysSection::TextDumpsysSection(int id, const char* service, ...)
442 : WorkerThreadSection(id, REMOTE_CALL_TIMEOUT_MS), mService(service) {
443 name = "dumpsys ";
444 name += service;
445
446 va_list args;
447 va_start(args, service);
448 while (true) {
449 const char* arg = va_arg(args, const char*);
450 if (arg == NULL) {
451 break;
452 }
453 mArgs.add(String16(arg));
454 name += " ";
455 name += arg;
456 }
457 va_end(args);
458}
459
460TextDumpsysSection::~TextDumpsysSection() {}
461
462status_t TextDumpsysSection::BlockingCall(unique_fd& pipeWriteFd) const {
463 // checkService won't wait for the service to show up like getService will.
464 sp<IBinder> service = defaultServiceManager()->checkService(mService);
465 if (service == NULL) {
466 ALOGW("TextDumpsysSection: Can't lookup service: %s", String8(mService).string());
467 return NAME_NOT_FOUND;
468 }
469
470 // Create pipe
471 Fpipe dumpPipe;
472 if (!dumpPipe.init()) {
473 ALOGW("[%s] failed to setup pipe", this->name.string());
474 return -errno;
475 }
476
477 // Run dumping thread
478 const uint64_t start = Nanotime();
479 std::thread worker([&]() {
480 // Don't crash the service if writing to a closed pipe (may happen if dumping times out)
481 signal(SIGPIPE, sigpipe_handler);
482 status_t err = service->dump(dumpPipe.writeFd().get(), mArgs);
483 if (err != OK) {
484 ALOGW("[%s] dump thread failed. Error: %s", this->name.string(), strerror(-err));
485 }
486 dumpPipe.writeFd().reset();
487 });
488
489 // Collect dump content
490 std::string content;
491 bool success = ReadFdToString(dumpPipe.readFd(), &content);
492 worker.join(); // Wait for worker to finish
493 dumpPipe.readFd().reset();
494 if (!success) {
495 ALOGW("[%s] failed to read data from pipe", this->name.string());
496 return -1;
497 }
498
499 ProtoOutputStream proto;
500 proto.write(util::TextDumpProto::COMMAND, std::string(name.string()));
501 proto.write(util::TextDumpProto::CONTENT, content);
502 proto.write(util::TextDumpProto::DUMP_DURATION_NS, int64_t(Nanotime() - start));
503
504 if (!proto.flush(pipeWriteFd.get()) && errno == EPIPE) {
505 ALOGE("[%s] wrote to a broken pipe\n", this->name.string());
506 return EPIPE;
507 }
508 return OK;
509}
510
511// ================================================================================
Yi Jin3c034c92017-12-22 17:36:47 -0800512// initialization only once in Section.cpp.
513map<log_id_t, log_time> LogSection::gLastLogsRetrieved;
514
zhouwenjiec3bf8042019-10-30 14:31:54 -0700515LogSection::LogSection(int id, const char* logID, ...) : WorkerThreadSection(id), mLogMode(logModeBase) {
516 name = "logcat -b ";
517 name += logID;
518
519 va_list args;
520 va_start(args, logID);
521 mLogID = android_name_to_log_id(logID);
522 while(true) {
523 const char* arg = va_arg(args, const char*);
524 if (arg == NULL) {
525 break;
526 }
527 if (!strcmp(arg, "-L")) {
528 // Read from last logcat buffer
529 mLogMode = mLogMode | ANDROID_LOG_PSTORE;
530 }
531 name += " ";
532 name += arg;
533 }
Greg Kaiserfa89cde2019-11-04 06:04:42 -0800534 va_end(args);
zhouwenjiec3bf8042019-10-30 14:31:54 -0700535
536 switch (mLogID) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800537 case LOG_ID_EVENTS:
538 case LOG_ID_STATS:
539 case LOG_ID_SECURITY:
540 mBinary = true;
541 break;
542 default:
543 mBinary = false;
Yi Jin3c034c92017-12-22 17:36:47 -0800544 }
545}
546
Yi Jinb592e3b2018-02-01 15:17:04 -0800547LogSection::~LogSection() {}
Yi Jin3c034c92017-12-22 17:36:47 -0800548
Yi Jinb592e3b2018-02-01 15:17:04 -0800549static size_t trimTail(char const* buf, size_t len) {
Yi Jin3c034c92017-12-22 17:36:47 -0800550 while (len > 0) {
551 char c = buf[len - 1];
552 if (c == '\0' || c == ' ' || c == '\n' || c == '\r' || c == ':') {
553 len--;
554 } else {
555 break;
556 }
557 }
558 return len;
559}
560
561static inline int32_t get4LE(uint8_t const* src) {
562 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
563}
564
Mike Ma643de922019-12-17 10:56:17 -0800565status_t LogSection::BlockingCall(unique_fd& pipeWriteFd) const {
Yi Jin3c034c92017-12-22 17:36:47 -0800566 // Open log buffer and getting logs since last retrieved time if any.
567 unique_ptr<logger_list, void (*)(logger_list*)> loggers(
Yi Jinb592e3b2018-02-01 15:17:04 -0800568 gLastLogsRetrieved.find(mLogID) == gLastLogsRetrieved.end()
zhouwenjiec3bf8042019-10-30 14:31:54 -0700569 ? android_logger_list_alloc(mLogMode, 0, 0)
570 : android_logger_list_alloc_time(mLogMode, gLastLogsRetrieved[mLogID], 0),
Yi Jinb592e3b2018-02-01 15:17:04 -0800571 android_logger_list_free);
Yi Jin3c034c92017-12-22 17:36:47 -0800572
573 if (android_logger_open(loggers.get(), mLogID) == NULL) {
Yi Jin3be0b432018-04-20 17:08:11 -0700574 ALOGE("[%s] Can't get logger.", this->name.string());
Yi Jin83fb1d52018-03-16 12:03:53 -0700575 return -1;
Yi Jin3c034c92017-12-22 17:36:47 -0800576 }
577
578 log_msg msg;
579 log_time lastTimestamp(0);
580
581 ProtoOutputStream proto;
Yi Jinb592e3b2018-02-01 15:17:04 -0800582 while (true) { // keeps reading until logd buffer is fully read.
Yi Jin83fb1d52018-03-16 12:03:53 -0700583 status_t err = android_logger_list_read(loggers.get(), &msg);
Yi Jin3c034c92017-12-22 17:36:47 -0800584 // err = 0 - no content, unexpected connection drop or EOF.
585 // err = +ive number - size of retrieved data from logger
586 // err = -ive number, OS supplied error _except_ for -EAGAIN
587 // err = -EAGAIN, graceful indication for ANDRODI_LOG_NONBLOCK that this is the end of data.
588 if (err <= 0) {
589 if (err != -EAGAIN) {
Yi Jin3be0b432018-04-20 17:08:11 -0700590 ALOGW("[%s] fails to read a log_msg.\n", this->name.string());
Yi Jin3c034c92017-12-22 17:36:47 -0800591 }
Yi Jin83fb1d52018-03-16 12:03:53 -0700592 // dump previous logs and don't consider this error a failure.
Yi Jin3c034c92017-12-22 17:36:47 -0800593 break;
594 }
595 if (mBinary) {
596 // remove the first uint32 which is tag's index in event log tags
597 android_log_context context = create_android_log_parser(msg.msg() + sizeof(uint32_t),
Yi Jinb592e3b2018-02-01 15:17:04 -0800598 msg.len() - sizeof(uint32_t));
599 ;
Yi Jin3c034c92017-12-22 17:36:47 -0800600 android_log_list_element elem;
601
Tom Cherryd0269892019-10-15 17:10:15 -0700602 lastTimestamp.tv_sec = msg.entry.sec;
603 lastTimestamp.tv_nsec = msg.entry.nsec;
Yi Jin3c034c92017-12-22 17:36:47 -0800604
605 // format a BinaryLogEntry
Yi Jin5ee07872018-03-05 18:18:27 -0800606 uint64_t token = proto.start(LogProto::BINARY_LOGS);
Tom Cherryd0269892019-10-15 17:10:15 -0700607 proto.write(BinaryLogEntry::SEC, (int32_t)msg.entry.sec);
608 proto.write(BinaryLogEntry::NANOSEC, (int32_t)msg.entry.nsec);
609 proto.write(BinaryLogEntry::UID, (int)msg.entry.uid);
610 proto.write(BinaryLogEntry::PID, msg.entry.pid);
611 proto.write(BinaryLogEntry::TID, (int32_t)msg.entry.tid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800612 proto.write(BinaryLogEntry::TAG_INDEX,
613 get4LE(reinterpret_cast<uint8_t const*>(msg.msg())));
Yi Jin3c034c92017-12-22 17:36:47 -0800614 do {
615 elem = android_log_read_next(context);
Yi Jin5ee07872018-03-05 18:18:27 -0800616 uint64_t elemToken = proto.start(BinaryLogEntry::ELEMS);
Yi Jin3c034c92017-12-22 17:36:47 -0800617 switch (elem.type) {
618 case EVENT_TYPE_INT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800619 proto.write(BinaryLogEntry::Elem::TYPE,
620 BinaryLogEntry::Elem::EVENT_TYPE_INT);
621 proto.write(BinaryLogEntry::Elem::VAL_INT32, (int)elem.data.int32);
Yi Jin3c034c92017-12-22 17:36:47 -0800622 break;
623 case EVENT_TYPE_LONG:
Yi Jinb592e3b2018-02-01 15:17:04 -0800624 proto.write(BinaryLogEntry::Elem::TYPE,
625 BinaryLogEntry::Elem::EVENT_TYPE_LONG);
626 proto.write(BinaryLogEntry::Elem::VAL_INT64, (long long)elem.data.int64);
Yi Jin3c034c92017-12-22 17:36:47 -0800627 break;
628 case EVENT_TYPE_STRING:
Yi Jinb592e3b2018-02-01 15:17:04 -0800629 proto.write(BinaryLogEntry::Elem::TYPE,
630 BinaryLogEntry::Elem::EVENT_TYPE_STRING);
Yi Jin3c034c92017-12-22 17:36:47 -0800631 proto.write(BinaryLogEntry::Elem::VAL_STRING, elem.data.string, elem.len);
632 break;
633 case EVENT_TYPE_FLOAT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800634 proto.write(BinaryLogEntry::Elem::TYPE,
635 BinaryLogEntry::Elem::EVENT_TYPE_FLOAT);
Yi Jin3c034c92017-12-22 17:36:47 -0800636 proto.write(BinaryLogEntry::Elem::VAL_FLOAT, elem.data.float32);
637 break;
638 case EVENT_TYPE_LIST:
Yi Jinb592e3b2018-02-01 15:17:04 -0800639 proto.write(BinaryLogEntry::Elem::TYPE,
640 BinaryLogEntry::Elem::EVENT_TYPE_LIST);
Yi Jin3c034c92017-12-22 17:36:47 -0800641 break;
642 case EVENT_TYPE_LIST_STOP:
Yi Jinb592e3b2018-02-01 15:17:04 -0800643 proto.write(BinaryLogEntry::Elem::TYPE,
644 BinaryLogEntry::Elem::EVENT_TYPE_LIST_STOP);
Yi Jin3c034c92017-12-22 17:36:47 -0800645 break;
646 case EVENT_TYPE_UNKNOWN:
Yi Jinb592e3b2018-02-01 15:17:04 -0800647 proto.write(BinaryLogEntry::Elem::TYPE,
648 BinaryLogEntry::Elem::EVENT_TYPE_UNKNOWN);
Yi Jin3c034c92017-12-22 17:36:47 -0800649 break;
650 }
651 proto.end(elemToken);
652 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
653 proto.end(token);
654 if (context) {
655 android_log_destroy(&context);
656 }
657 } else {
658 AndroidLogEntry entry;
Tom Cherryd0269892019-10-15 17:10:15 -0700659 err = android_log_processLogBuffer(&msg.entry, &entry);
Yi Jin3c034c92017-12-22 17:36:47 -0800660 if (err != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700661 ALOGW("[%s] fails to process to an entry.\n", this->name.string());
Yi Jin3c034c92017-12-22 17:36:47 -0800662 break;
663 }
664 lastTimestamp.tv_sec = entry.tv_sec;
665 lastTimestamp.tv_nsec = entry.tv_nsec;
666
667 // format a TextLogEntry
Yi Jin5ee07872018-03-05 18:18:27 -0800668 uint64_t token = proto.start(LogProto::TEXT_LOGS);
Yi Jin3c034c92017-12-22 17:36:47 -0800669 proto.write(TextLogEntry::SEC, (long long)entry.tv_sec);
670 proto.write(TextLogEntry::NANOSEC, (long long)entry.tv_nsec);
671 proto.write(TextLogEntry::PRIORITY, (int)entry.priority);
672 proto.write(TextLogEntry::UID, entry.uid);
673 proto.write(TextLogEntry::PID, entry.pid);
674 proto.write(TextLogEntry::TID, entry.tid);
675 proto.write(TextLogEntry::TAG, entry.tag, trimTail(entry.tag, entry.tagLen));
Yi Jinb592e3b2018-02-01 15:17:04 -0800676 proto.write(TextLogEntry::LOG, entry.message,
677 trimTail(entry.message, entry.messageLen));
Yi Jin3c034c92017-12-22 17:36:47 -0800678 proto.end(token);
679 }
680 }
681 gLastLogsRetrieved[mLogID] = lastTimestamp;
Mike Ma643de922019-12-17 10:56:17 -0800682 if (!proto.flush(pipeWriteFd.get()) && errno == EPIPE) {
Kweku Adams5b763c12018-09-13 15:44:58 -0700683 ALOGE("[%s] wrote to a broken pipe\n", this->name.string());
684 return EPIPE;
685 }
Yi Jin83fb1d52018-03-16 12:03:53 -0700686 return NO_ERROR;
Yi Jin3c034c92017-12-22 17:36:47 -0800687}
Kweku Adamseadd1232018-02-05 16:45:13 -0800688
689// ================================================================================
690
691TombstoneSection::TombstoneSection(int id, const char* type, const int64_t timeoutMs)
692 : WorkerThreadSection(id, timeoutMs), mType(type) {
Yi Jin3be0b432018-04-20 17:08:11 -0700693 name = "tombstone ";
Kweku Adamseadd1232018-02-05 16:45:13 -0800694 name += type;
695}
696
697TombstoneSection::~TombstoneSection() {}
698
Mike Ma643de922019-12-17 10:56:17 -0800699status_t TombstoneSection::BlockingCall(unique_fd& pipeWriteFd) const {
Kweku Adamseadd1232018-02-05 16:45:13 -0800700 std::unique_ptr<DIR, decltype(&closedir)> proc(opendir("/proc"), closedir);
701 if (proc.get() == nullptr) {
702 ALOGE("opendir /proc failed: %s\n", strerror(errno));
703 return -errno;
704 }
705
706 const std::set<int> hal_pids = get_interesting_hal_pids();
707
708 ProtoOutputStream proto;
709 struct dirent* d;
710 status_t err = NO_ERROR;
711 while ((d = readdir(proc.get()))) {
712 int pid = atoi(d->d_name);
713 if (pid <= 0) {
714 continue;
715 }
716
717 const std::string link_name = android::base::StringPrintf("/proc/%d/exe", pid);
718 std::string exe;
719 if (!android::base::Readlink(link_name, &exe)) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000720 ALOGE("Section %s: Can't read '%s': %s\n", name.string(),
721 link_name.c_str(), strerror(errno));
Kweku Adamseadd1232018-02-05 16:45:13 -0800722 continue;
723 }
724
725 bool is_java_process;
726 if (exe == "/system/bin/app_process32" || exe == "/system/bin/app_process64") {
727 if (mType != "java") continue;
728 // Don't bother dumping backtraces for the zygote.
729 if (IsZygote(pid)) {
730 VLOG("Skipping Zygote");
731 continue;
732 }
733
734 is_java_process = true;
735 } else if (should_dump_native_traces(exe.c_str())) {
736 if (mType != "native") continue;
737 is_java_process = false;
738 } else if (hal_pids.find(pid) != hal_pids.end()) {
739 if (mType != "hal") continue;
740 is_java_process = false;
741 } else {
742 // Probably a native process we don't care about, continue.
743 VLOG("Skipping %d", pid);
744 continue;
745 }
746
747 Fpipe dumpPipe;
748 if (!dumpPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700749 ALOGW("[%s] failed to setup dump pipe", this->name.string());
Kweku Adamseadd1232018-02-05 16:45:13 -0800750 err = -errno;
751 break;
752 }
753
754 const uint64_t start = Nanotime();
755 pid_t child = fork();
756 if (child < 0) {
757 ALOGE("Failed to fork child process");
758 break;
759 } else if (child == 0) {
760 // This is the child process.
Yi Jin6355d2f2018-03-14 15:18:02 -0700761 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800762 const int ret = dump_backtrace_to_file_timeout(
763 pid, is_java_process ? kDebuggerdJavaBacktrace : kDebuggerdNativeBacktrace,
Yi Jin6355d2f2018-03-14 15:18:02 -0700764 is_java_process ? 5 : 20, dumpPipe.writeFd().get());
Kweku Adamseadd1232018-02-05 16:45:13 -0800765 if (ret == -1) {
766 if (errno == 0) {
767 ALOGW("Dumping failed for pid '%d', likely due to a timeout\n", pid);
768 } else {
769 ALOGE("Dumping failed for pid '%d': %s\n", pid, strerror(errno));
770 }
771 }
Yi Jin6355d2f2018-03-14 15:18:02 -0700772 dumpPipe.writeFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800773 _exit(EXIT_SUCCESS);
774 }
Yi Jin6355d2f2018-03-14 15:18:02 -0700775 dumpPipe.writeFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800776 // Parent process.
777 // Read from the pipe concurrently to avoid blocking the child.
778 FdBuffer buffer;
Yi Jine3dab2d2018-03-22 16:56:39 -0700779 err = buffer.readFully(dumpPipe.readFd().get());
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700780 // Wait on the child to avoid it becoming a zombie process.
781 status_t cStatus = wait_child(child);
Kweku Adamseadd1232018-02-05 16:45:13 -0800782 if (err != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700783 ALOGW("[%s] failed to read stack dump: %d", this->name.string(), err);
Yi Jin6355d2f2018-03-14 15:18:02 -0700784 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800785 break;
786 }
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700787 if (cStatus != NO_ERROR) {
Kweku Adams5b763c12018-09-13 15:44:58 -0700788 ALOGE("[%s] child had an issue: %s\n", this->name.string(), strerror(-cStatus));
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700789 }
Kweku Adamseadd1232018-02-05 16:45:13 -0800790
791 auto dump = std::make_unique<char[]>(buffer.size());
Joe Onorato99598ee2019-02-11 15:55:13 +0000792 sp<ProtoReader> reader = buffer.data()->read();
Kweku Adamseadd1232018-02-05 16:45:13 -0800793 int i = 0;
Joe Onorato99598ee2019-02-11 15:55:13 +0000794 while (reader->hasNext()) {
795 dump[i] = reader->next();
Kweku Adamseadd1232018-02-05 16:45:13 -0800796 i++;
797 }
Yi Jin6cacbcb2018-03-30 14:04:52 -0700798 uint64_t token = proto.start(android::os::BackTraceProto::TRACES);
Kweku Adamseadd1232018-02-05 16:45:13 -0800799 proto.write(android::os::BackTraceProto::Stack::PID, pid);
800 proto.write(android::os::BackTraceProto::Stack::DUMP, dump.get(), i);
801 proto.write(android::os::BackTraceProto::Stack::DUMP_DURATION_NS,
802 static_cast<long long>(Nanotime() - start));
803 proto.end(token);
Yi Jin6355d2f2018-03-14 15:18:02 -0700804 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800805 }
806
Mike Ma643de922019-12-17 10:56:17 -0800807 if (!proto.flush(pipeWriteFd.get()) && errno == EPIPE) {
Kweku Adams5b763c12018-09-13 15:44:58 -0700808 ALOGE("[%s] wrote to a broken pipe\n", this->name.string());
809 if (err != NO_ERROR) {
810 return EPIPE;
811 }
812 }
813
Kweku Adamseadd1232018-02-05 16:45:13 -0800814 return err;
815}
Yi Jin6cacbcb2018-03-30 14:04:52 -0700816
Mike Ma643de922019-12-17 10:56:17 -0800817// ================================================================================
818BringYourOwnSection::BringYourOwnSection(int id, const char* customName, const uid_t callingUid,
819 const sp<IIncidentDumpCallback>& callback)
820 : WorkerThreadSection(id, REMOTE_CALL_TIMEOUT_MS), uid(callingUid), mCallback(callback) {
821 name = "registered ";
822 name += customName;
823}
824
825BringYourOwnSection::~BringYourOwnSection() {}
826
827status_t BringYourOwnSection::BlockingCall(unique_fd& pipeWriteFd) const {
828 android::os::ParcelFileDescriptor pfd(std::move(pipeWriteFd));
829 mCallback->onDumpSection(pfd);
830 return NO_ERROR;
831}
832
Yi Jin6cacbcb2018-03-30 14:04:52 -0700833} // namespace incidentd
834} // namespace os
Yi Jin98ce8102018-04-12 11:15:39 -0700835} // namespace android