blob: 0176f80479745c869e92c1831c68e80fa2678be2 [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
Yi Jin3c034c92017-12-22 17:36:47 -080024#include <mutex>
Kweku Adamseadd1232018-02-05 16:45:13 -080025#include <set>
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"
45#include "incidentd_util.h"
Joe Onorato1754d742016-11-21 17:51:35 -080046
Yi Jin6cacbcb2018-03-30 14:04:52 -070047namespace android {
48namespace os {
49namespace incidentd {
50
Yi Jinb592e3b2018-02-01 15:17:04 -080051using namespace android::base;
Yi Jinc23fad22017-09-15 17:24:59 -070052using namespace android::util;
Joe Onorato1754d742016-11-21 17:51:35 -080053
Yi Jinc23fad22017-09-15 17:24:59 -070054// special section ids
Yi Jin329130b2018-02-09 16:47:47 -080055const int FIELD_ID_INCIDENT_METADATA = 2;
Yi Jinc23fad22017-09-15 17:24:59 -070056
57// incident section parameters
Yi Jin3c034c92017-12-22 17:36:47 -080058const char INCIDENT_HELPER[] = "/system/bin/incident_helper";
Yi Jinc36e91d2018-03-08 11:25:58 -080059const char* GZIP[] = {"/system/bin/gzip", NULL};
Yi Jinb44f7d42017-07-21 12:12:59 -070060
Yi Jin1a11fa12018-02-22 16:44:10 -080061static pid_t fork_execute_incident_helper(const int id, Fpipe* p2cPipe, Fpipe* c2pPipe) {
Yi Jinb592e3b2018-02-01 15:17:04 -080062 const char* ihArgs[]{INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL};
Yi Jinc36e91d2018-03-08 11:25:58 -080063 return fork_execute_cmd(const_cast<char**>(ihArgs), p2cPipe, c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -070064}
65
Joe Onorato7a406b42019-04-12 18:11:30 -070066bool section_requires_specific_mention(int sectionId) {
67 switch (sectionId) {
68 case 3025: // restricted_images
69 return true;
Ryan Savitskicc7d9972019-06-03 23:57:09 +010070 case 3026: // system_trace
71 return true;
Joe Onorato7a406b42019-04-12 18:11:30 -070072 default:
73 return false;
74 }
75}
76
Yi Jin99c248f2017-08-25 18:11:58 -070077// ================================================================================
Joe Onoratofe7bbf42019-03-24 20:57:16 -070078Section::Section(int i, int64_t timeoutMs)
Kweku Adams3d160912018-05-07 11:26:27 -070079 : id(i),
Joe Onoratofe7bbf42019-03-24 20:57:16 -070080 timeoutMs(timeoutMs) {
81}
Joe Onorato1754d742016-11-21 17:51:35 -080082
Yi Jinb592e3b2018-02-01 15:17:04 -080083Section::~Section() {}
Joe Onorato1754d742016-11-21 17:51:35 -080084
Joe Onorato1754d742016-11-21 17:51:35 -080085// ================================================================================
Yi Jin1a11fa12018-02-22 16:44:10 -080086static inline bool isSysfs(const char* filename) { return strncmp(filename, "/sys/", 5) == 0; }
87
Kweku Adamse04ef772018-06-13 12:24:38 -070088FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs)
Joe Onoratofe7bbf42019-03-24 20:57:16 -070089 : Section(id, timeoutMs), mFilename(filename) {
Yi Jin3be0b432018-04-20 17:08:11 -070090 name = "file ";
91 name += filename;
Yi Jin1a11fa12018-02-22 16:44:10 -080092 mIsSysfs = isSysfs(filename);
Yi Jin0a3406f2017-06-22 19:23:11 -070093}
94
95FileSection::~FileSection() {}
96
Joe Onorato99598ee2019-02-11 15:55:13 +000097status_t FileSection::Execute(ReportWriter* writer) const {
Yi Jinb44f7d42017-07-21 12:12:59 -070098 // read from mFilename first, make sure the file is available
99 // add O_CLOEXEC to make sure it is closed when exec incident helper
Yi Jin6355d2f2018-03-14 15:18:02 -0700100 unique_fd fd(open(mFilename, O_RDONLY | O_CLOEXEC));
101 if (fd.get() == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700102 ALOGW("[%s] failed to open file", this->name.string());
Kweku Adamse04ef772018-06-13 12:24:38 -0700103 // There may be some devices/architectures that won't have the file.
104 // Just return here without an error.
105 return NO_ERROR;
Yi Jin0a3406f2017-06-22 19:23:11 -0700106 }
107
Yi Jinb44f7d42017-07-21 12:12:59 -0700108 FdBuffer buffer;
109 Fpipe p2cPipe;
110 Fpipe c2pPipe;
111 // initiate pipes to pass data to/from incident_helper
112 if (!p2cPipe.init() || !c2pPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700113 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jin0a3406f2017-06-22 19:23:11 -0700114 return -errno;
115 }
116
Yi Jin1a11fa12018-02-22 16:44:10 -0800117 pid_t pid = fork_execute_incident_helper(this->id, &p2cPipe, &c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700118 if (pid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700119 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700120 return -errno;
121 }
122
123 // parent process
Yi Jine3dab2d2018-03-22 16:56:39 -0700124 status_t readStatus = buffer.readProcessedDataInStream(fd.get(), std::move(p2cPipe.writeFd()),
125 std::move(c2pPipe.readFd()),
126 this->timeoutMs, mIsSysfs);
Joe Onorato99598ee2019-02-11 15:55:13 +0000127 writer->setSectionStats(buffer);
Yi Jinb44f7d42017-07-21 12:12:59 -0700128 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700129 ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800130 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800131 kill_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700132 return readStatus;
133 }
134
Yi Jinedfd5bb2017-09-06 17:09:11 -0700135 status_t ihStatus = wait_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700136 if (ihStatus != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700137 ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-ihStatus));
Yi Jinb44f7d42017-07-21 12:12:59 -0700138 return ihStatus;
139 }
140
Joe Onorato99598ee2019-02-11 15:55:13 +0000141 return writer->writeSection(buffer);
Yi Jin0a3406f2017-06-22 19:23:11 -0700142}
Yi Jin1a11fa12018-02-22 16:44:10 -0800143// ================================================================================
144GZipSection::GZipSection(int id, const char* filename, ...) : Section(id) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800145 va_list args;
146 va_start(args, filename);
147 mFilenames = varargs(filename, args);
148 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800149 name = "gzip";
150 for (int i = 0; mFilenames[i] != NULL; i++) {
151 name += " ";
152 name += mFilenames[i];
153 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800154}
Yi Jin0a3406f2017-06-22 19:23:11 -0700155
Yi Jin480de782018-04-06 15:37:36 -0700156GZipSection::~GZipSection() { free(mFilenames); }
Yi Jin1a11fa12018-02-22 16:44:10 -0800157
Joe Onorato99598ee2019-02-11 15:55:13 +0000158status_t GZipSection::Execute(ReportWriter* writer) const {
Yi Jin1a11fa12018-02-22 16:44:10 -0800159 // Reads the files in order, use the first available one.
160 int index = 0;
Yi Jin6355d2f2018-03-14 15:18:02 -0700161 unique_fd fd;
Yi Jin1a11fa12018-02-22 16:44:10 -0800162 while (mFilenames[index] != NULL) {
Yi Jin6355d2f2018-03-14 15:18:02 -0700163 fd.reset(open(mFilenames[index], O_RDONLY | O_CLOEXEC));
164 if (fd.get() != -1) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800165 break;
166 }
167 ALOGW("GZipSection failed to open file %s", mFilenames[index]);
168 index++; // look at the next file.
169 }
Yi Jinc858e272018-03-28 15:32:50 -0700170 if (fd.get() == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700171 ALOGW("[%s] can't open all the files", this->name.string());
Yi Jin6cacbcb2018-03-30 14:04:52 -0700172 return NO_ERROR; // e.g. LAST_KMSG will reach here in user build.
Yi Jinc858e272018-03-28 15:32:50 -0700173 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800174 FdBuffer buffer;
175 Fpipe p2cPipe;
176 Fpipe c2pPipe;
177 // initiate pipes to pass data to/from gzip
178 if (!p2cPipe.init() || !c2pPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700179 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jin1a11fa12018-02-22 16:44:10 -0800180 return -errno;
181 }
182
Yi Jinc36e91d2018-03-08 11:25:58 -0800183 pid_t pid = fork_execute_cmd((char* const*)GZIP, &p2cPipe, &c2pPipe);
Yi Jin1a11fa12018-02-22 16:44:10 -0800184 if (pid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700185 ALOGW("[%s] failed to fork", this->name.string());
Yi Jin1a11fa12018-02-22 16:44:10 -0800186 return -errno;
187 }
188 // parent process
189
190 // construct Fdbuffer to output GZippedfileProto, the reason to do this instead of using
191 // ProtoOutputStream is to avoid allocation of another buffer inside ProtoOutputStream.
Joe Onorato99598ee2019-02-11 15:55:13 +0000192 sp<EncodedBuffer> internalBuffer = buffer.data();
Yi Jin1a11fa12018-02-22 16:44:10 -0800193 internalBuffer->writeHeader((uint32_t)GZippedFileProto::FILENAME, WIRE_TYPE_LENGTH_DELIMITED);
Yi Jina9635e42018-03-23 12:05:32 -0700194 size_t fileLen = strlen(mFilenames[index]);
195 internalBuffer->writeRawVarint32(fileLen);
196 for (size_t i = 0; i < fileLen; i++) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800197 internalBuffer->writeRawByte(mFilenames[index][i]);
198 }
199 internalBuffer->writeHeader((uint32_t)GZippedFileProto::GZIPPED_DATA,
200 WIRE_TYPE_LENGTH_DELIMITED);
201 size_t editPos = internalBuffer->wp()->pos();
202 internalBuffer->wp()->move(8); // reserve 8 bytes for the varint of the data size.
203 size_t dataBeginAt = internalBuffer->wp()->pos();
Yi Jin3be0b432018-04-20 17:08:11 -0700204 VLOG("[%s] editPos=%zu, dataBeginAt=%zu", this->name.string(), editPos, dataBeginAt);
Yi Jin1a11fa12018-02-22 16:44:10 -0800205
Yi Jine3dab2d2018-03-22 16:56:39 -0700206 status_t readStatus = buffer.readProcessedDataInStream(
207 fd.get(), std::move(p2cPipe.writeFd()), std::move(c2pPipe.readFd()), this->timeoutMs,
208 isSysfs(mFilenames[index]));
Joe Onorato99598ee2019-02-11 15:55:13 +0000209 writer->setSectionStats(buffer);
Yi Jin1a11fa12018-02-22 16:44:10 -0800210 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700211 ALOGW("[%s] failed to read data from gzip: %s, timedout: %s", this->name.string(),
212 strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin1a11fa12018-02-22 16:44:10 -0800213 kill_child(pid);
214 return readStatus;
215 }
216
217 status_t gzipStatus = wait_child(pid);
218 if (gzipStatus != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700219 ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-gzipStatus));
Yi Jin1a11fa12018-02-22 16:44:10 -0800220 return gzipStatus;
221 }
222 // Revisit the actual size from gzip result and edit the internal buffer accordingly.
223 size_t dataSize = buffer.size() - dataBeginAt;
224 internalBuffer->wp()->rewind()->move(editPos);
225 internalBuffer->writeRawVarint32(dataSize);
226 internalBuffer->copy(dataBeginAt, dataSize);
Yi Jin1a11fa12018-02-22 16:44:10 -0800227
Joe Onorato99598ee2019-02-11 15:55:13 +0000228 return writer->writeSection(buffer);
Yi Jin1a11fa12018-02-22 16:44:10 -0800229}
Kweku Adamseadd1232018-02-05 16:45:13 -0800230
Yi Jin0a3406f2017-06-22 19:23:11 -0700231// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800232struct WorkerThreadData : public virtual RefBase {
Joe Onorato1754d742016-11-21 17:51:35 -0800233 const WorkerThreadSection* section;
Yi Jin6355d2f2018-03-14 15:18:02 -0700234 Fpipe pipe;
Joe Onorato1754d742016-11-21 17:51:35 -0800235
236 // Lock protects these fields
237 mutex lock;
238 bool workerDone;
239 status_t workerError;
240
Chih-Hung Hsieh7a88a932018-12-20 13:45:04 -0800241 explicit WorkerThreadData(const WorkerThreadSection* section);
Joe Onorato1754d742016-11-21 17:51:35 -0800242 virtual ~WorkerThreadData();
Joe Onorato1754d742016-11-21 17:51:35 -0800243};
244
245WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec)
Yi Jin6355d2f2018-03-14 15:18:02 -0700246 : section(sec), workerDone(false), workerError(NO_ERROR) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800247
Yi Jinb592e3b2018-02-01 15:17:04 -0800248WorkerThreadData::~WorkerThreadData() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800249
250// ================================================================================
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700251WorkerThreadSection::WorkerThreadSection(int id, const int64_t timeoutMs)
252 : Section(id, timeoutMs) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800253
Yi Jinb592e3b2018-02-01 15:17:04 -0800254WorkerThreadSection::~WorkerThreadSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800255
Kweku Adams5b763c12018-09-13 15:44:58 -0700256void sigpipe_handler(int signum) {
257 if (signum == SIGPIPE) {
258 ALOGE("Wrote to a broken pipe\n");
259 } else {
260 ALOGE("Received unexpected signal: %d\n", signum);
261 }
262}
263
Yi Jinb592e3b2018-02-01 15:17:04 -0800264static void* worker_thread_func(void* cookie) {
Kweku Adams5b763c12018-09-13 15:44:58 -0700265 // Don't crash the service if we write to a closed pipe (which can happen if
266 // dumping times out).
267 signal(SIGPIPE, sigpipe_handler);
268
Joe Onorato1754d742016-11-21 17:51:35 -0800269 WorkerThreadData* data = (WorkerThreadData*)cookie;
Yi Jin6355d2f2018-03-14 15:18:02 -0700270 status_t err = data->section->BlockingCall(data->pipe.writeFd().get());
Joe Onorato1754d742016-11-21 17:51:35 -0800271
272 {
273 unique_lock<mutex> lock(data->lock);
274 data->workerDone = true;
275 data->workerError = err;
276 }
277
Yi Jin6355d2f2018-03-14 15:18:02 -0700278 data->pipe.writeFd().reset();
Joe Onorato1754d742016-11-21 17:51:35 -0800279 data->decStrong(data->section);
280 // data might be gone now. don't use it after this point in this thread.
281 return NULL;
282}
283
Joe Onorato99598ee2019-02-11 15:55:13 +0000284status_t WorkerThreadSection::Execute(ReportWriter* writer) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800285 status_t err = NO_ERROR;
286 pthread_t thread;
287 pthread_attr_t attr;
Mike Ma28381692018-12-04 15:46:29 -0800288 bool workerDone = false;
Joe Onorato1754d742016-11-21 17:51:35 -0800289 FdBuffer buffer;
290
291 // Data shared between this thread and the worker thread.
292 sp<WorkerThreadData> data = new WorkerThreadData(this);
293
294 // Create the pipe
Yi Jin6355d2f2018-03-14 15:18:02 -0700295 if (!data->pipe.init()) {
Joe Onorato1754d742016-11-21 17:51:35 -0800296 return -errno;
297 }
298
Joe Onorato1754d742016-11-21 17:51:35 -0800299 // Create the thread
300 err = pthread_attr_init(&attr);
301 if (err != 0) {
302 return -err;
303 }
304 // TODO: Do we need to tweak thread priority?
305 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
306 if (err != 0) {
307 pthread_attr_destroy(&attr);
308 return -err;
309 }
Joe Onorato99598ee2019-02-11 15:55:13 +0000310
311 // The worker thread needs a reference and we can't let the count go to zero
312 // if that thread is slow to start.
313 data->incStrong(this);
314
Joe Onorato1754d742016-11-21 17:51:35 -0800315 err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get());
Joe Onorato99598ee2019-02-11 15:55:13 +0000316 pthread_attr_destroy(&attr);
Joe Onorato1754d742016-11-21 17:51:35 -0800317 if (err != 0) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000318 data->decStrong(this);
Joe Onorato1754d742016-11-21 17:51:35 -0800319 return -err;
320 }
Joe Onorato1754d742016-11-21 17:51:35 -0800321
322 // Loop reading until either the timeout or the worker side is done (i.e. eof).
Yi Jine3dab2d2018-03-22 16:56:39 -0700323 err = buffer.read(data->pipe.readFd().get(), this->timeoutMs);
Joe Onorato1754d742016-11-21 17:51:35 -0800324 if (err != NO_ERROR) {
Mike Ma28381692018-12-04 15:46:29 -0800325 ALOGE("[%s] reader failed with error '%s'", this->name.string(), strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800326 }
327
328 // Done with the read fd. The worker thread closes the write one so
329 // we never race and get here first.
Yi Jin6355d2f2018-03-14 15:18:02 -0700330 data->pipe.readFd().reset();
Joe Onorato1754d742016-11-21 17:51:35 -0800331
332 // If the worker side is finished, then return its error (which may overwrite
Mike Ma28381692018-12-04 15:46:29 -0800333 // our possible error -- but it's more interesting anyway). If not, then we timed out.
Joe Onorato1754d742016-11-21 17:51:35 -0800334 {
335 unique_lock<mutex> lock(data->lock);
Mike Ma28381692018-12-04 15:46:29 -0800336 if (data->workerError != NO_ERROR) {
337 err = data->workerError;
338 ALOGE("[%s] worker failed with error '%s'", this->name.string(), strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800339 }
Mike Ma28381692018-12-04 15:46:29 -0800340 workerDone = data->workerDone;
Joe Onorato1754d742016-11-21 17:51:35 -0800341 }
Kweku Adams5b763c12018-09-13 15:44:58 -0700342
Joe Onorato99598ee2019-02-11 15:55:13 +0000343 writer->setSectionStats(buffer);
Mike Ma28381692018-12-04 15:46:29 -0800344 if (err != NO_ERROR) {
345 char errMsg[128];
346 snprintf(errMsg, 128, "[%s] failed with error '%s'",
347 this->name.string(), strerror(-err));
Joe Onorato99598ee2019-02-11 15:55:13 +0000348 writer->error(this, err, "WorkerThreadSection failed.");
Joe Onorato1754d742016-11-21 17:51:35 -0800349 return NO_ERROR;
350 }
Mike Ma28381692018-12-04 15:46:29 -0800351 if (buffer.truncated()) {
352 ALOGW("[%s] too large, truncating", this->name.string());
Joe Onorato99598ee2019-02-11 15:55:13 +0000353 // Do not write a truncated section. It won't pass through the PrivacyFilter.
Mike Ma28381692018-12-04 15:46:29 -0800354 return NO_ERROR;
355 }
356 if (!workerDone || buffer.timedOut()) {
357 ALOGW("[%s] timed out", this->name.string());
Joe Onorato1754d742016-11-21 17:51:35 -0800358 return NO_ERROR;
359 }
360
361 // Write the data that was collected
Joe Onorato99598ee2019-02-11 15:55:13 +0000362 return writer->writeSection(buffer);
Joe Onorato1754d742016-11-21 17:51:35 -0800363}
364
365// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700366CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...)
Yi Jinb592e3b2018-02-01 15:17:04 -0800367 : Section(id, timeoutMs) {
Joe Onorato1754d742016-11-21 17:51:35 -0800368 va_list args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700369 va_start(args, command);
Yi Jin1a11fa12018-02-22 16:44:10 -0800370 mCommand = varargs(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800371 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800372 name = "cmd";
373 for (int i = 0; mCommand[i] != NULL; i++) {
374 name += " ";
375 name += mCommand[i];
376 }
Yi Jinb44f7d42017-07-21 12:12:59 -0700377}
Joe Onorato1754d742016-11-21 17:51:35 -0800378
Yi Jinb592e3b2018-02-01 15:17:04 -0800379CommandSection::CommandSection(int id, const char* command, ...) : Section(id) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700380 va_list args;
381 va_start(args, command);
Yi Jin1a11fa12018-02-22 16:44:10 -0800382 mCommand = varargs(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800383 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800384 name = "cmd";
385 for (int i = 0; mCommand[i] != NULL; i++) {
386 name += " ";
387 name += mCommand[i];
388 }
Joe Onorato1754d742016-11-21 17:51:35 -0800389}
390
Yi Jinb592e3b2018-02-01 15:17:04 -0800391CommandSection::~CommandSection() { free(mCommand); }
Joe Onorato1754d742016-11-21 17:51:35 -0800392
Joe Onorato99598ee2019-02-11 15:55:13 +0000393status_t CommandSection::Execute(ReportWriter* writer) const {
Yi Jinb44f7d42017-07-21 12:12:59 -0700394 FdBuffer buffer;
395 Fpipe cmdPipe;
396 Fpipe ihPipe;
397
398 if (!cmdPipe.init() || !ihPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700399 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700400 return -errno;
401 }
402
Yi Jinc36e91d2018-03-08 11:25:58 -0800403 pid_t cmdPid = fork_execute_cmd((char* const*)mCommand, NULL, &cmdPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700404 if (cmdPid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700405 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700406 return -errno;
407 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800408 pid_t ihPid = fork_execute_incident_helper(this->id, &cmdPipe, &ihPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700409 if (ihPid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700410 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700411 return -errno;
412 }
413
Yi Jin6355d2f2018-03-14 15:18:02 -0700414 cmdPipe.writeFd().reset();
Yi Jine3dab2d2018-03-22 16:56:39 -0700415 status_t readStatus = buffer.read(ihPipe.readFd().get(), this->timeoutMs);
Joe Onorato99598ee2019-02-11 15:55:13 +0000416 writer->setSectionStats(buffer);
Yi Jinb44f7d42017-07-21 12:12:59 -0700417 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700418 ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800419 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800420 kill_child(cmdPid);
421 kill_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700422 return readStatus;
423 }
424
Kweku Adamseadd1232018-02-05 16:45:13 -0800425 // Waiting for command here has one trade-off: the failed status of command won't be detected
Yi Jin1a11fa12018-02-22 16:44:10 -0800426 // until buffer timeout, but it has advatage on starting the data stream earlier.
Yi Jinedfd5bb2017-09-06 17:09:11 -0700427 status_t cmdStatus = wait_child(cmdPid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800428 status_t ihStatus = wait_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700429 if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000430 ALOGW("[%s] abnormal child processes, return status: command: %s, incident helper: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800431 this->name.string(), strerror(-cmdStatus), strerror(-ihStatus));
Joe Onorato99598ee2019-02-11 15:55:13 +0000432 // Not a fatal error.
433 return NO_ERROR;
Yi Jinb44f7d42017-07-21 12:12:59 -0700434 }
435
Joe Onorato99598ee2019-02-11 15:55:13 +0000436 return writer->writeSection(buffer);
Joe Onorato1754d742016-11-21 17:51:35 -0800437}
438
439// ================================================================================
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700440DumpsysSection::DumpsysSection(int id, const char* service, ...)
441 : WorkerThreadSection(id, REMOTE_CALL_TIMEOUT_MS), mService(service) {
Joe Onorato1754d742016-11-21 17:51:35 -0800442 name = "dumpsys ";
443 name += service;
444
445 va_list args;
446 va_start(args, service);
447 while (true) {
Yi Jin0a3406f2017-06-22 19:23:11 -0700448 const char* arg = va_arg(args, const char*);
Joe Onorato1754d742016-11-21 17:51:35 -0800449 if (arg == NULL) {
450 break;
451 }
452 mArgs.add(String16(arg));
453 name += " ";
454 name += arg;
455 }
456 va_end(args);
457}
458
Yi Jinb592e3b2018-02-01 15:17:04 -0800459DumpsysSection::~DumpsysSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800460
Yi Jinb592e3b2018-02-01 15:17:04 -0800461status_t DumpsysSection::BlockingCall(int pipeWriteFd) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800462 // checkService won't wait for the service to show up like getService will.
463 sp<IBinder> service = defaultServiceManager()->checkService(mService);
Yi Jin0a3406f2017-06-22 19:23:11 -0700464
Joe Onorato1754d742016-11-21 17:51:35 -0800465 if (service == NULL) {
Joe Onorato1754d742016-11-21 17:51:35 -0800466 ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string());
Mike Ma28381692018-12-04 15:46:29 -0800467 return NAME_NOT_FOUND;
Joe Onorato1754d742016-11-21 17:51:35 -0800468 }
469
470 service->dump(pipeWriteFd, mArgs);
471
472 return NO_ERROR;
473}
Yi Jin3c034c92017-12-22 17:36:47 -0800474
475// ================================================================================
476// initialization only once in Section.cpp.
477map<log_id_t, log_time> LogSection::gLastLogsRetrieved;
478
zhouwenjiec3bf8042019-10-30 14:31:54 -0700479LogSection::LogSection(int id, const char* logID, ...) : WorkerThreadSection(id), mLogMode(logModeBase) {
480 name = "logcat -b ";
481 name += logID;
482
483 va_list args;
484 va_start(args, logID);
485 mLogID = android_name_to_log_id(logID);
486 while(true) {
487 const char* arg = va_arg(args, const char*);
488 if (arg == NULL) {
489 break;
490 }
491 if (!strcmp(arg, "-L")) {
492 // Read from last logcat buffer
493 mLogMode = mLogMode | ANDROID_LOG_PSTORE;
494 }
495 name += " ";
496 name += arg;
497 }
498
499 switch (mLogID) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800500 case LOG_ID_EVENTS:
501 case LOG_ID_STATS:
502 case LOG_ID_SECURITY:
503 mBinary = true;
504 break;
505 default:
506 mBinary = false;
Yi Jin3c034c92017-12-22 17:36:47 -0800507 }
508}
509
Yi Jinb592e3b2018-02-01 15:17:04 -0800510LogSection::~LogSection() {}
Yi Jin3c034c92017-12-22 17:36:47 -0800511
Yi Jinb592e3b2018-02-01 15:17:04 -0800512static size_t trimTail(char const* buf, size_t len) {
Yi Jin3c034c92017-12-22 17:36:47 -0800513 while (len > 0) {
514 char c = buf[len - 1];
515 if (c == '\0' || c == ' ' || c == '\n' || c == '\r' || c == ':') {
516 len--;
517 } else {
518 break;
519 }
520 }
521 return len;
522}
523
524static inline int32_t get4LE(uint8_t const* src) {
525 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
526}
527
Yi Jinb592e3b2018-02-01 15:17:04 -0800528status_t LogSection::BlockingCall(int pipeWriteFd) const {
Yi Jin3c034c92017-12-22 17:36:47 -0800529 // Open log buffer and getting logs since last retrieved time if any.
530 unique_ptr<logger_list, void (*)(logger_list*)> loggers(
Yi Jinb592e3b2018-02-01 15:17:04 -0800531 gLastLogsRetrieved.find(mLogID) == gLastLogsRetrieved.end()
zhouwenjiec3bf8042019-10-30 14:31:54 -0700532 ? android_logger_list_alloc(mLogMode, 0, 0)
533 : android_logger_list_alloc_time(mLogMode, gLastLogsRetrieved[mLogID], 0),
Yi Jinb592e3b2018-02-01 15:17:04 -0800534 android_logger_list_free);
Yi Jin3c034c92017-12-22 17:36:47 -0800535
536 if (android_logger_open(loggers.get(), mLogID) == NULL) {
Yi Jin3be0b432018-04-20 17:08:11 -0700537 ALOGE("[%s] Can't get logger.", this->name.string());
Yi Jin83fb1d52018-03-16 12:03:53 -0700538 return -1;
Yi Jin3c034c92017-12-22 17:36:47 -0800539 }
540
541 log_msg msg;
542 log_time lastTimestamp(0);
543
544 ProtoOutputStream proto;
Yi Jinb592e3b2018-02-01 15:17:04 -0800545 while (true) { // keeps reading until logd buffer is fully read.
Yi Jin83fb1d52018-03-16 12:03:53 -0700546 status_t err = android_logger_list_read(loggers.get(), &msg);
Yi Jin3c034c92017-12-22 17:36:47 -0800547 // err = 0 - no content, unexpected connection drop or EOF.
548 // err = +ive number - size of retrieved data from logger
549 // err = -ive number, OS supplied error _except_ for -EAGAIN
550 // err = -EAGAIN, graceful indication for ANDRODI_LOG_NONBLOCK that this is the end of data.
551 if (err <= 0) {
552 if (err != -EAGAIN) {
Yi Jin3be0b432018-04-20 17:08:11 -0700553 ALOGW("[%s] fails to read a log_msg.\n", this->name.string());
Yi Jin3c034c92017-12-22 17:36:47 -0800554 }
Yi Jin83fb1d52018-03-16 12:03:53 -0700555 // dump previous logs and don't consider this error a failure.
Yi Jin3c034c92017-12-22 17:36:47 -0800556 break;
557 }
558 if (mBinary) {
559 // remove the first uint32 which is tag's index in event log tags
560 android_log_context context = create_android_log_parser(msg.msg() + sizeof(uint32_t),
Yi Jinb592e3b2018-02-01 15:17:04 -0800561 msg.len() - sizeof(uint32_t));
562 ;
Yi Jin3c034c92017-12-22 17:36:47 -0800563 android_log_list_element elem;
564
Tom Cherryd0269892019-10-15 17:10:15 -0700565 lastTimestamp.tv_sec = msg.entry.sec;
566 lastTimestamp.tv_nsec = msg.entry.nsec;
Yi Jin3c034c92017-12-22 17:36:47 -0800567
568 // format a BinaryLogEntry
Yi Jin5ee07872018-03-05 18:18:27 -0800569 uint64_t token = proto.start(LogProto::BINARY_LOGS);
Tom Cherryd0269892019-10-15 17:10:15 -0700570 proto.write(BinaryLogEntry::SEC, (int32_t)msg.entry.sec);
571 proto.write(BinaryLogEntry::NANOSEC, (int32_t)msg.entry.nsec);
572 proto.write(BinaryLogEntry::UID, (int)msg.entry.uid);
573 proto.write(BinaryLogEntry::PID, msg.entry.pid);
574 proto.write(BinaryLogEntry::TID, (int32_t)msg.entry.tid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800575 proto.write(BinaryLogEntry::TAG_INDEX,
576 get4LE(reinterpret_cast<uint8_t const*>(msg.msg())));
Yi Jin3c034c92017-12-22 17:36:47 -0800577 do {
578 elem = android_log_read_next(context);
Yi Jin5ee07872018-03-05 18:18:27 -0800579 uint64_t elemToken = proto.start(BinaryLogEntry::ELEMS);
Yi Jin3c034c92017-12-22 17:36:47 -0800580 switch (elem.type) {
581 case EVENT_TYPE_INT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800582 proto.write(BinaryLogEntry::Elem::TYPE,
583 BinaryLogEntry::Elem::EVENT_TYPE_INT);
584 proto.write(BinaryLogEntry::Elem::VAL_INT32, (int)elem.data.int32);
Yi Jin3c034c92017-12-22 17:36:47 -0800585 break;
586 case EVENT_TYPE_LONG:
Yi Jinb592e3b2018-02-01 15:17:04 -0800587 proto.write(BinaryLogEntry::Elem::TYPE,
588 BinaryLogEntry::Elem::EVENT_TYPE_LONG);
589 proto.write(BinaryLogEntry::Elem::VAL_INT64, (long long)elem.data.int64);
Yi Jin3c034c92017-12-22 17:36:47 -0800590 break;
591 case EVENT_TYPE_STRING:
Yi Jinb592e3b2018-02-01 15:17:04 -0800592 proto.write(BinaryLogEntry::Elem::TYPE,
593 BinaryLogEntry::Elem::EVENT_TYPE_STRING);
Yi Jin3c034c92017-12-22 17:36:47 -0800594 proto.write(BinaryLogEntry::Elem::VAL_STRING, elem.data.string, elem.len);
595 break;
596 case EVENT_TYPE_FLOAT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800597 proto.write(BinaryLogEntry::Elem::TYPE,
598 BinaryLogEntry::Elem::EVENT_TYPE_FLOAT);
Yi Jin3c034c92017-12-22 17:36:47 -0800599 proto.write(BinaryLogEntry::Elem::VAL_FLOAT, elem.data.float32);
600 break;
601 case EVENT_TYPE_LIST:
Yi Jinb592e3b2018-02-01 15:17:04 -0800602 proto.write(BinaryLogEntry::Elem::TYPE,
603 BinaryLogEntry::Elem::EVENT_TYPE_LIST);
Yi Jin3c034c92017-12-22 17:36:47 -0800604 break;
605 case EVENT_TYPE_LIST_STOP:
Yi Jinb592e3b2018-02-01 15:17:04 -0800606 proto.write(BinaryLogEntry::Elem::TYPE,
607 BinaryLogEntry::Elem::EVENT_TYPE_LIST_STOP);
Yi Jin3c034c92017-12-22 17:36:47 -0800608 break;
609 case EVENT_TYPE_UNKNOWN:
Yi Jinb592e3b2018-02-01 15:17:04 -0800610 proto.write(BinaryLogEntry::Elem::TYPE,
611 BinaryLogEntry::Elem::EVENT_TYPE_UNKNOWN);
Yi Jin3c034c92017-12-22 17:36:47 -0800612 break;
613 }
614 proto.end(elemToken);
615 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
616 proto.end(token);
617 if (context) {
618 android_log_destroy(&context);
619 }
620 } else {
621 AndroidLogEntry entry;
Tom Cherryd0269892019-10-15 17:10:15 -0700622 err = android_log_processLogBuffer(&msg.entry, &entry);
Yi Jin3c034c92017-12-22 17:36:47 -0800623 if (err != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700624 ALOGW("[%s] fails to process to an entry.\n", this->name.string());
Yi Jin3c034c92017-12-22 17:36:47 -0800625 break;
626 }
627 lastTimestamp.tv_sec = entry.tv_sec;
628 lastTimestamp.tv_nsec = entry.tv_nsec;
629
630 // format a TextLogEntry
Yi Jin5ee07872018-03-05 18:18:27 -0800631 uint64_t token = proto.start(LogProto::TEXT_LOGS);
Yi Jin3c034c92017-12-22 17:36:47 -0800632 proto.write(TextLogEntry::SEC, (long long)entry.tv_sec);
633 proto.write(TextLogEntry::NANOSEC, (long long)entry.tv_nsec);
634 proto.write(TextLogEntry::PRIORITY, (int)entry.priority);
635 proto.write(TextLogEntry::UID, entry.uid);
636 proto.write(TextLogEntry::PID, entry.pid);
637 proto.write(TextLogEntry::TID, entry.tid);
638 proto.write(TextLogEntry::TAG, entry.tag, trimTail(entry.tag, entry.tagLen));
Yi Jinb592e3b2018-02-01 15:17:04 -0800639 proto.write(TextLogEntry::LOG, entry.message,
640 trimTail(entry.message, entry.messageLen));
Yi Jin3c034c92017-12-22 17:36:47 -0800641 proto.end(token);
642 }
643 }
644 gLastLogsRetrieved[mLogID] = lastTimestamp;
Kweku Adams5b763c12018-09-13 15:44:58 -0700645 if (!proto.flush(pipeWriteFd) && errno == EPIPE) {
646 ALOGE("[%s] wrote to a broken pipe\n", this->name.string());
647 return EPIPE;
648 }
Yi Jin83fb1d52018-03-16 12:03:53 -0700649 return NO_ERROR;
Yi Jin3c034c92017-12-22 17:36:47 -0800650}
Kweku Adamseadd1232018-02-05 16:45:13 -0800651
652// ================================================================================
653
654TombstoneSection::TombstoneSection(int id, const char* type, const int64_t timeoutMs)
655 : WorkerThreadSection(id, timeoutMs), mType(type) {
Yi Jin3be0b432018-04-20 17:08:11 -0700656 name = "tombstone ";
Kweku Adamseadd1232018-02-05 16:45:13 -0800657 name += type;
658}
659
660TombstoneSection::~TombstoneSection() {}
661
662status_t TombstoneSection::BlockingCall(int pipeWriteFd) const {
663 std::unique_ptr<DIR, decltype(&closedir)> proc(opendir("/proc"), closedir);
664 if (proc.get() == nullptr) {
665 ALOGE("opendir /proc failed: %s\n", strerror(errno));
666 return -errno;
667 }
668
669 const std::set<int> hal_pids = get_interesting_hal_pids();
670
671 ProtoOutputStream proto;
672 struct dirent* d;
673 status_t err = NO_ERROR;
674 while ((d = readdir(proc.get()))) {
675 int pid = atoi(d->d_name);
676 if (pid <= 0) {
677 continue;
678 }
679
680 const std::string link_name = android::base::StringPrintf("/proc/%d/exe", pid);
681 std::string exe;
682 if (!android::base::Readlink(link_name, &exe)) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000683 ALOGE("Section %s: Can't read '%s': %s\n", name.string(),
684 link_name.c_str(), strerror(errno));
Kweku Adamseadd1232018-02-05 16:45:13 -0800685 continue;
686 }
687
688 bool is_java_process;
689 if (exe == "/system/bin/app_process32" || exe == "/system/bin/app_process64") {
690 if (mType != "java") continue;
691 // Don't bother dumping backtraces for the zygote.
692 if (IsZygote(pid)) {
693 VLOG("Skipping Zygote");
694 continue;
695 }
696
697 is_java_process = true;
698 } else if (should_dump_native_traces(exe.c_str())) {
699 if (mType != "native") continue;
700 is_java_process = false;
701 } else if (hal_pids.find(pid) != hal_pids.end()) {
702 if (mType != "hal") continue;
703 is_java_process = false;
704 } else {
705 // Probably a native process we don't care about, continue.
706 VLOG("Skipping %d", pid);
707 continue;
708 }
709
710 Fpipe dumpPipe;
711 if (!dumpPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700712 ALOGW("[%s] failed to setup dump pipe", this->name.string());
Kweku Adamseadd1232018-02-05 16:45:13 -0800713 err = -errno;
714 break;
715 }
716
717 const uint64_t start = Nanotime();
718 pid_t child = fork();
719 if (child < 0) {
720 ALOGE("Failed to fork child process");
721 break;
722 } else if (child == 0) {
723 // This is the child process.
Yi Jin6355d2f2018-03-14 15:18:02 -0700724 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800725 const int ret = dump_backtrace_to_file_timeout(
726 pid, is_java_process ? kDebuggerdJavaBacktrace : kDebuggerdNativeBacktrace,
Yi Jin6355d2f2018-03-14 15:18:02 -0700727 is_java_process ? 5 : 20, dumpPipe.writeFd().get());
Kweku Adamseadd1232018-02-05 16:45:13 -0800728 if (ret == -1) {
729 if (errno == 0) {
730 ALOGW("Dumping failed for pid '%d', likely due to a timeout\n", pid);
731 } else {
732 ALOGE("Dumping failed for pid '%d': %s\n", pid, strerror(errno));
733 }
734 }
Yi Jin6355d2f2018-03-14 15:18:02 -0700735 dumpPipe.writeFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800736 _exit(EXIT_SUCCESS);
737 }
Yi Jin6355d2f2018-03-14 15:18:02 -0700738 dumpPipe.writeFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800739 // Parent process.
740 // Read from the pipe concurrently to avoid blocking the child.
741 FdBuffer buffer;
Yi Jine3dab2d2018-03-22 16:56:39 -0700742 err = buffer.readFully(dumpPipe.readFd().get());
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700743 // Wait on the child to avoid it becoming a zombie process.
744 status_t cStatus = wait_child(child);
Kweku Adamseadd1232018-02-05 16:45:13 -0800745 if (err != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700746 ALOGW("[%s] failed to read stack dump: %d", this->name.string(), err);
Yi Jin6355d2f2018-03-14 15:18:02 -0700747 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800748 break;
749 }
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700750 if (cStatus != NO_ERROR) {
Kweku Adams5b763c12018-09-13 15:44:58 -0700751 ALOGE("[%s] child had an issue: %s\n", this->name.string(), strerror(-cStatus));
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700752 }
Kweku Adamseadd1232018-02-05 16:45:13 -0800753
754 auto dump = std::make_unique<char[]>(buffer.size());
Joe Onorato99598ee2019-02-11 15:55:13 +0000755 sp<ProtoReader> reader = buffer.data()->read();
Kweku Adamseadd1232018-02-05 16:45:13 -0800756 int i = 0;
Joe Onorato99598ee2019-02-11 15:55:13 +0000757 while (reader->hasNext()) {
758 dump[i] = reader->next();
Kweku Adamseadd1232018-02-05 16:45:13 -0800759 i++;
760 }
Yi Jin6cacbcb2018-03-30 14:04:52 -0700761 uint64_t token = proto.start(android::os::BackTraceProto::TRACES);
Kweku Adamseadd1232018-02-05 16:45:13 -0800762 proto.write(android::os::BackTraceProto::Stack::PID, pid);
763 proto.write(android::os::BackTraceProto::Stack::DUMP, dump.get(), i);
764 proto.write(android::os::BackTraceProto::Stack::DUMP_DURATION_NS,
765 static_cast<long long>(Nanotime() - start));
766 proto.end(token);
Yi Jin6355d2f2018-03-14 15:18:02 -0700767 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800768 }
769
Kweku Adams5b763c12018-09-13 15:44:58 -0700770 if (!proto.flush(pipeWriteFd) && errno == EPIPE) {
771 ALOGE("[%s] wrote to a broken pipe\n", this->name.string());
772 if (err != NO_ERROR) {
773 return EPIPE;
774 }
775 }
776
Kweku Adamseadd1232018-02-05 16:45:13 -0800777 return err;
778}
Yi Jin6cacbcb2018-03-30 14:04:52 -0700779
780} // namespace incidentd
781} // namespace os
Yi Jin98ce8102018-04-12 11:15:39 -0700782} // namespace android