Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 1 | /* |
| 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 Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 16 | #define DEBUG false |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 17 | #include "Log.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 18 | |
| 19 | #include "Section.h" |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 20 | |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 21 | #include <dirent.h> |
| 22 | #include <errno.h> |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 23 | #include <mutex> |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 24 | #include <set> |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 25 | #include <thread> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 26 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 27 | #include <android-base/file.h> |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 28 | #include <android-base/properties.h> |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 29 | #include <android-base/stringprintf.h> |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 30 | #include <android/util/protobuf.h> |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 31 | #include <android/util/ProtoOutputStream.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 32 | #include <binder/IServiceManager.h> |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 33 | #include <debuggerd/client.h> |
| 34 | #include <dumputils/dump_utils.h> |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 35 | #include <log/log_event_list.h> |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 36 | #include <log/log_read.h> |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 37 | #include <log/logprint.h> |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 38 | #include <private/android_logger.h> |
| 39 | |
| 40 | #include "FdBuffer.h" |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 41 | #include "Privacy.h" |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 42 | #include "frameworks/base/core/proto/android/os/backtrace.proto.h" |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 43 | #include "frameworks/base/core/proto/android/os/data.proto.h" |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 44 | #include "frameworks/base/core/proto/android/util/log.proto.h" |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 45 | #include "frameworks/base/core/proto/android/util/textdump.proto.h" |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 46 | #include "incidentd_util.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 47 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 48 | namespace android { |
| 49 | namespace os { |
| 50 | namespace incidentd { |
| 51 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 52 | using namespace android::base; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 53 | using namespace android::util; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 54 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 55 | // special section ids |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 56 | const int FIELD_ID_INCIDENT_METADATA = 2; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 57 | |
| 58 | // incident section parameters |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 59 | const char INCIDENT_HELPER[] = "/system/bin/incident_helper"; |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 60 | const char* GZIP[] = {"/system/bin/gzip", NULL}; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 61 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 62 | static pid_t fork_execute_incident_helper(const int id, Fpipe* p2cPipe, Fpipe* c2pPipe) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 63 | const char* ihArgs[]{INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL}; |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 64 | return fork_execute_cmd(const_cast<char**>(ihArgs), p2cPipe, c2pPipe); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Joe Onorato | 7a406b4 | 2019-04-12 18:11:30 -0700 | [diff] [blame] | 67 | bool section_requires_specific_mention(int sectionId) { |
| 68 | switch (sectionId) { |
| 69 | case 3025: // restricted_images |
| 70 | return true; |
Ryan Savitski | cc7d997 | 2019-06-03 23:57:09 +0100 | [diff] [blame] | 71 | case 3026: // system_trace |
| 72 | return true; |
Joe Onorato | 7a406b4 | 2019-04-12 18:11:30 -0700 | [diff] [blame] | 73 | default: |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 78 | // ================================================================================ |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 79 | Section::Section(int i, int64_t timeoutMs) |
Kweku Adams | 3d16091 | 2018-05-07 11:26:27 -0700 | [diff] [blame] | 80 | : id(i), |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 81 | timeoutMs(timeoutMs) { |
| 82 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 83 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 84 | Section::~Section() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 85 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 86 | // ================================================================================ |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 87 | static inline bool isSysfs(const char* filename) { return strncmp(filename, "/sys/", 5) == 0; } |
| 88 | |
Kweku Adams | e04ef77 | 2018-06-13 12:24:38 -0700 | [diff] [blame] | 89 | FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs) |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 90 | : Section(id, timeoutMs), mFilename(filename) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 91 | name = "file "; |
| 92 | name += filename; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 93 | mIsSysfs = isSysfs(filename); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | FileSection::~FileSection() {} |
| 97 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 98 | status_t FileSection::Execute(ReportWriter* writer) const { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 99 | // 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 Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 101 | unique_fd fd(open(mFilename, O_RDONLY | O_CLOEXEC)); |
| 102 | if (fd.get() == -1) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 103 | ALOGW("[%s] failed to open file", this->name.string()); |
Kweku Adams | e04ef77 | 2018-06-13 12:24:38 -0700 | [diff] [blame] | 104 | // There may be some devices/architectures that won't have the file. |
| 105 | // Just return here without an error. |
| 106 | return NO_ERROR; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 109 | 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 Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 114 | ALOGW("[%s] failed to setup pipes", this->name.string()); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 115 | return -errno; |
| 116 | } |
| 117 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 118 | pid_t pid = fork_execute_incident_helper(this->id, &p2cPipe, &c2pPipe); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 119 | if (pid == -1) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 120 | ALOGW("[%s] failed to fork", this->name.string()); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 121 | return -errno; |
| 122 | } |
| 123 | |
| 124 | // parent process |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 125 | status_t readStatus = buffer.readProcessedDataInStream(fd.get(), std::move(p2cPipe.writeFd()), |
| 126 | std::move(c2pPipe.readFd()), |
| 127 | this->timeoutMs, mIsSysfs); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 128 | writer->setSectionStats(buffer); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 129 | if (readStatus != NO_ERROR || buffer.timedOut()) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 130 | ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 131 | this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false"); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 132 | kill_child(pid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 133 | return readStatus; |
| 134 | } |
| 135 | |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 136 | status_t ihStatus = wait_child(pid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 137 | if (ihStatus != NO_ERROR) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 138 | ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-ihStatus)); |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 139 | return OK; // Not a fatal error. |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 142 | return writer->writeSection(buffer); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 143 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 144 | // ================================================================================ |
| 145 | GZipSection::GZipSection(int id, const char* filename, ...) : Section(id) { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 146 | va_list args; |
| 147 | va_start(args, filename); |
| 148 | mFilenames = varargs(filename, args); |
| 149 | va_end(args); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 150 | name = "gzip"; |
| 151 | for (int i = 0; mFilenames[i] != NULL; i++) { |
| 152 | name += " "; |
| 153 | name += mFilenames[i]; |
| 154 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 155 | } |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 156 | |
Yi Jin | 480de78 | 2018-04-06 15:37:36 -0700 | [diff] [blame] | 157 | GZipSection::~GZipSection() { free(mFilenames); } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 158 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 159 | status_t GZipSection::Execute(ReportWriter* writer) const { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 160 | // Reads the files in order, use the first available one. |
| 161 | int index = 0; |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 162 | unique_fd fd; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 163 | while (mFilenames[index] != NULL) { |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 164 | fd.reset(open(mFilenames[index], O_RDONLY | O_CLOEXEC)); |
| 165 | if (fd.get() != -1) { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 166 | break; |
| 167 | } |
| 168 | ALOGW("GZipSection failed to open file %s", mFilenames[index]); |
| 169 | index++; // look at the next file. |
| 170 | } |
Yi Jin | c858e27 | 2018-03-28 15:32:50 -0700 | [diff] [blame] | 171 | if (fd.get() == -1) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 172 | ALOGW("[%s] can't open all the files", this->name.string()); |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 173 | return NO_ERROR; // e.g. LAST_KMSG will reach here in user build. |
Yi Jin | c858e27 | 2018-03-28 15:32:50 -0700 | [diff] [blame] | 174 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 175 | FdBuffer buffer; |
| 176 | Fpipe p2cPipe; |
| 177 | Fpipe c2pPipe; |
| 178 | // initiate pipes to pass data to/from gzip |
| 179 | if (!p2cPipe.init() || !c2pPipe.init()) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 180 | ALOGW("[%s] failed to setup pipes", this->name.string()); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 181 | return -errno; |
| 182 | } |
| 183 | |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 184 | pid_t pid = fork_execute_cmd((char* const*)GZIP, &p2cPipe, &c2pPipe); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 185 | if (pid == -1) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 186 | ALOGW("[%s] failed to fork", this->name.string()); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 187 | 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 Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 193 | sp<EncodedBuffer> internalBuffer = buffer.data(); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 194 | internalBuffer->writeHeader((uint32_t)GZippedFileProto::FILENAME, WIRE_TYPE_LENGTH_DELIMITED); |
Yi Jin | a9635e4 | 2018-03-23 12:05:32 -0700 | [diff] [blame] | 195 | size_t fileLen = strlen(mFilenames[index]); |
| 196 | internalBuffer->writeRawVarint32(fileLen); |
| 197 | for (size_t i = 0; i < fileLen; i++) { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 198 | 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 Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 205 | VLOG("[%s] editPos=%zu, dataBeginAt=%zu", this->name.string(), editPos, dataBeginAt); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 206 | |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 207 | status_t readStatus = buffer.readProcessedDataInStream( |
| 208 | fd.get(), std::move(p2cPipe.writeFd()), std::move(c2pPipe.readFd()), this->timeoutMs, |
| 209 | isSysfs(mFilenames[index])); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 210 | writer->setSectionStats(buffer); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 211 | if (readStatus != NO_ERROR || buffer.timedOut()) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 212 | ALOGW("[%s] failed to read data from gzip: %s, timedout: %s", this->name.string(), |
| 213 | strerror(-readStatus), buffer.timedOut() ? "true" : "false"); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 214 | kill_child(pid); |
| 215 | return readStatus; |
| 216 | } |
| 217 | |
| 218 | status_t gzipStatus = wait_child(pid); |
| 219 | if (gzipStatus != NO_ERROR) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 220 | ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-gzipStatus)); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 221 | 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 Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 228 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 229 | return writer->writeSection(buffer); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 230 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 231 | |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 232 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 233 | struct WorkerThreadData : public virtual RefBase { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 234 | const WorkerThreadSection* section; |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 235 | Fpipe pipe; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 236 | |
| 237 | // Lock protects these fields |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 238 | std::mutex lock; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 239 | bool workerDone; |
| 240 | status_t workerError; |
| 241 | |
Chih-Hung Hsieh | 7a88a93 | 2018-12-20 13:45:04 -0800 | [diff] [blame] | 242 | explicit WorkerThreadData(const WorkerThreadSection* section); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 243 | virtual ~WorkerThreadData(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec) |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 247 | : section(sec), workerDone(false), workerError(NO_ERROR) {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 248 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 249 | WorkerThreadData::~WorkerThreadData() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 250 | |
| 251 | // ================================================================================ |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 252 | WorkerThreadSection::WorkerThreadSection(int id, const int64_t timeoutMs) |
| 253 | : Section(id, timeoutMs) {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 254 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 255 | WorkerThreadSection::~WorkerThreadSection() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 256 | |
Kweku Adams | 5b763c1 | 2018-09-13 15:44:58 -0700 | [diff] [blame] | 257 | void 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 Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 265 | status_t WorkerThreadSection::Execute(ReportWriter* writer) const { |
Mike Ma | b328e29 | 2020-03-18 01:52:36 -0700 | [diff] [blame] | 266 | // Create shared data and pipe. Don't put data on the stack since this thread may exit early. |
| 267 | sp<WorkerThreadData> data = new WorkerThreadData(this); |
| 268 | if (!data->pipe.init()) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 269 | return -errno; |
| 270 | } |
Mike Ma | b328e29 | 2020-03-18 01:52:36 -0700 | [diff] [blame] | 271 | data->incStrong(this); |
| 272 | std::thread([data, this]() { |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 273 | // Don't crash the service if writing to a closed pipe (may happen if dumping times out) |
| 274 | signal(SIGPIPE, sigpipe_handler); |
Mike Ma | b328e29 | 2020-03-18 01:52:36 -0700 | [diff] [blame] | 275 | status_t err = data->section->BlockingCall(data->pipe.writeFd()); |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 276 | { |
Mike Ma | b328e29 | 2020-03-18 01:52:36 -0700 | [diff] [blame] | 277 | std::scoped_lock<std::mutex> lock(data->lock); |
| 278 | data->workerDone = true; |
| 279 | data->workerError = err; |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 280 | // unique_fd is not thread safe. If we don't lock it, reset() may pause half way while |
| 281 | // the other thread executes to the end, calling ~Fpipe, which is a race condition. |
Mike Ma | b328e29 | 2020-03-18 01:52:36 -0700 | [diff] [blame] | 282 | data->pipe.writeFd().reset(); |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 283 | } |
Mike Ma | b328e29 | 2020-03-18 01:52:36 -0700 | [diff] [blame] | 284 | data->decStrong(this); |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 285 | }).detach(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 286 | |
| 287 | // Loop reading until either the timeout or the worker side is done (i.e. eof). |
Mike Ma | 87003ad | 2020-03-19 12:31:29 -0700 | [diff] [blame^] | 288 | status_t err = NO_ERROR; |
| 289 | bool workerDone = false; |
| 290 | FdBuffer buffer; |
Mike Ma | b328e29 | 2020-03-18 01:52:36 -0700 | [diff] [blame] | 291 | err = buffer.read(data->pipe.readFd().get(), this->timeoutMs); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 292 | if (err != NO_ERROR) { |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 293 | ALOGE("[%s] reader failed with error '%s'", this->name.string(), strerror(-err)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 296 | // If the worker side is finished, then return its error (which may overwrite |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 297 | // our possible error -- but it's more interesting anyway). If not, then we timed out. |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 298 | { |
Mike Ma | b328e29 | 2020-03-18 01:52:36 -0700 | [diff] [blame] | 299 | std::scoped_lock<std::mutex> lock(data->lock); |
| 300 | data->pipe.close(); |
| 301 | if (data->workerError != NO_ERROR) { |
| 302 | err = data->workerError; |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 303 | ALOGE("[%s] worker failed with error '%s'", this->name.string(), strerror(-err)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 304 | } |
Mike Ma | b328e29 | 2020-03-18 01:52:36 -0700 | [diff] [blame] | 305 | workerDone = data->workerDone; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 306 | } |
Kweku Adams | 5b763c1 | 2018-09-13 15:44:58 -0700 | [diff] [blame] | 307 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 308 | writer->setSectionStats(buffer); |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 309 | if (err != NO_ERROR) { |
| 310 | char errMsg[128]; |
| 311 | snprintf(errMsg, 128, "[%s] failed with error '%s'", |
| 312 | this->name.string(), strerror(-err)); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 313 | writer->error(this, err, "WorkerThreadSection failed."); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 314 | return NO_ERROR; |
| 315 | } |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 316 | if (buffer.truncated()) { |
| 317 | ALOGW("[%s] too large, truncating", this->name.string()); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 318 | // Do not write a truncated section. It won't pass through the PrivacyFilter. |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 319 | return NO_ERROR; |
| 320 | } |
| 321 | if (!workerDone || buffer.timedOut()) { |
| 322 | ALOGW("[%s] timed out", this->name.string()); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 323 | return NO_ERROR; |
| 324 | } |
| 325 | |
| 326 | // Write the data that was collected |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 327 | return writer->writeSection(buffer); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // ================================================================================ |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 331 | CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...) |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 332 | : Section(id, timeoutMs) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 333 | va_list args; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 334 | va_start(args, command); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 335 | mCommand = varargs(command, args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 336 | va_end(args); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 337 | name = "cmd"; |
| 338 | for (int i = 0; mCommand[i] != NULL; i++) { |
| 339 | name += " "; |
| 340 | name += mCommand[i]; |
| 341 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 342 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 343 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 344 | CommandSection::CommandSection(int id, const char* command, ...) : Section(id) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 345 | va_list args; |
| 346 | va_start(args, command); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 347 | mCommand = varargs(command, args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 348 | va_end(args); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 349 | name = "cmd"; |
| 350 | for (int i = 0; mCommand[i] != NULL; i++) { |
| 351 | name += " "; |
| 352 | name += mCommand[i]; |
| 353 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 356 | CommandSection::~CommandSection() { free(mCommand); } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 357 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 358 | status_t CommandSection::Execute(ReportWriter* writer) const { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 359 | FdBuffer buffer; |
| 360 | Fpipe cmdPipe; |
| 361 | Fpipe ihPipe; |
| 362 | |
| 363 | if (!cmdPipe.init() || !ihPipe.init()) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 364 | ALOGW("[%s] failed to setup pipes", this->name.string()); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 365 | return -errno; |
| 366 | } |
| 367 | |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 368 | pid_t cmdPid = fork_execute_cmd((char* const*)mCommand, NULL, &cmdPipe); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 369 | if (cmdPid == -1) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 370 | ALOGW("[%s] failed to fork", this->name.string()); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 371 | return -errno; |
| 372 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 373 | pid_t ihPid = fork_execute_incident_helper(this->id, &cmdPipe, &ihPipe); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 374 | if (ihPid == -1) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 375 | ALOGW("[%s] failed to fork", this->name.string()); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 376 | return -errno; |
| 377 | } |
| 378 | |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 379 | cmdPipe.writeFd().reset(); |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 380 | status_t readStatus = buffer.read(ihPipe.readFd().get(), this->timeoutMs); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 381 | writer->setSectionStats(buffer); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 382 | if (readStatus != NO_ERROR || buffer.timedOut()) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 383 | ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 384 | this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false"); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 385 | kill_child(cmdPid); |
| 386 | kill_child(ihPid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 387 | return readStatus; |
| 388 | } |
| 389 | |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 390 | // Waiting for command here has one trade-off: the failed status of command won't be detected |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 391 | // until buffer timeout, but it has advatage on starting the data stream earlier. |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 392 | status_t cmdStatus = wait_child(cmdPid); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 393 | status_t ihStatus = wait_child(ihPid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 394 | if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 395 | ALOGW("[%s] abnormal child processes, return status: command: %s, incident helper: %s", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 396 | this->name.string(), strerror(-cmdStatus), strerror(-ihStatus)); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 397 | // Not a fatal error. |
| 398 | return NO_ERROR; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 401 | return writer->writeSection(buffer); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | // ================================================================================ |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 405 | DumpsysSection::DumpsysSection(int id, const char* service, ...) |
| 406 | : WorkerThreadSection(id, REMOTE_CALL_TIMEOUT_MS), mService(service) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 407 | name = "dumpsys "; |
| 408 | name += service; |
| 409 | |
| 410 | va_list args; |
| 411 | va_start(args, service); |
| 412 | while (true) { |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 413 | const char* arg = va_arg(args, const char*); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 414 | if (arg == NULL) { |
| 415 | break; |
| 416 | } |
| 417 | mArgs.add(String16(arg)); |
| 418 | name += " "; |
| 419 | name += arg; |
| 420 | } |
| 421 | va_end(args); |
| 422 | } |
| 423 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 424 | DumpsysSection::~DumpsysSection() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 425 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 426 | status_t DumpsysSection::BlockingCall(unique_fd& pipeWriteFd) const { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 427 | // checkService won't wait for the service to show up like getService will. |
| 428 | sp<IBinder> service = defaultServiceManager()->checkService(mService); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 429 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 430 | if (service == NULL) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 431 | ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string()); |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 432 | return NAME_NOT_FOUND; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 435 | service->dump(pipeWriteFd.get(), mArgs); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 436 | |
| 437 | return NO_ERROR; |
| 438 | } |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 439 | |
| 440 | // ================================================================================ |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 441 | TextDumpsysSection::TextDumpsysSection(int id, const char* service, ...) |
Mike Ma | 87003ad | 2020-03-19 12:31:29 -0700 | [diff] [blame^] | 442 | :Section(id), mService(service) { |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 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 | |
| 460 | TextDumpsysSection::~TextDumpsysSection() {} |
| 461 | |
Mike Ma | 87003ad | 2020-03-19 12:31:29 -0700 | [diff] [blame^] | 462 | status_t TextDumpsysSection::Execute(ReportWriter* writer) const { |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 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 |
Mike Ma | 87003ad | 2020-03-19 12:31:29 -0700 | [diff] [blame^] | 490 | FdBuffer buffer; |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 491 | ProtoOutputStream proto; |
Mike Ma | 87003ad | 2020-03-19 12:31:29 -0700 | [diff] [blame^] | 492 | proto.write(TextDumpProto::COMMAND, std::string(name.string())); |
| 493 | proto.write(TextDumpProto::DUMP_DURATION_NS, int64_t(Nanotime() - start)); |
| 494 | buffer.write(proto.data()); |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 495 | |
Mike Ma | 87003ad | 2020-03-19 12:31:29 -0700 | [diff] [blame^] | 496 | sp<EncodedBuffer> internalBuffer = buffer.data(); |
| 497 | internalBuffer->writeHeader((uint32_t)TextDumpProto::CONTENT, WIRE_TYPE_LENGTH_DELIMITED); |
| 498 | size_t editPos = internalBuffer->wp()->pos(); |
| 499 | internalBuffer->wp()->move(8); // reserve 8 bytes for the varint of the data size |
| 500 | size_t dataBeginPos = internalBuffer->wp()->pos(); |
| 501 | |
| 502 | status_t readStatus = buffer.read(dumpPipe.readFd(), this->timeoutMs); |
| 503 | dumpPipe.readFd().reset(); |
| 504 | writer->setSectionStats(buffer); |
| 505 | if (readStatus != OK || buffer.timedOut()) { |
| 506 | ALOGW("[%s] failed to read from dumpsys: %s, timedout: %s", this->name.string(), |
| 507 | strerror(-readStatus), buffer.timedOut() ? "true" : "false"); |
| 508 | worker.detach(); |
| 509 | return readStatus; |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 510 | } |
Mike Ma | 87003ad | 2020-03-19 12:31:29 -0700 | [diff] [blame^] | 511 | worker.join(); // wait for worker to finish |
| 512 | |
| 513 | // Revisit the actual size from dumpsys and edit the internal buffer accordingly. |
| 514 | size_t dumpSize = buffer.size() - dataBeginPos; |
| 515 | internalBuffer->wp()->rewind()->move(editPos); |
| 516 | internalBuffer->writeRawVarint32(dumpSize); |
| 517 | internalBuffer->copy(dataBeginPos, dumpSize); |
| 518 | |
| 519 | return writer->writeSection(buffer); |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | // ================================================================================ |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 523 | // initialization only once in Section.cpp. |
| 524 | map<log_id_t, log_time> LogSection::gLastLogsRetrieved; |
| 525 | |
zhouwenjie | c3bf804 | 2019-10-30 14:31:54 -0700 | [diff] [blame] | 526 | LogSection::LogSection(int id, const char* logID, ...) : WorkerThreadSection(id), mLogMode(logModeBase) { |
| 527 | name = "logcat -b "; |
| 528 | name += logID; |
| 529 | |
| 530 | va_list args; |
| 531 | va_start(args, logID); |
| 532 | mLogID = android_name_to_log_id(logID); |
| 533 | while(true) { |
| 534 | const char* arg = va_arg(args, const char*); |
| 535 | if (arg == NULL) { |
| 536 | break; |
| 537 | } |
| 538 | if (!strcmp(arg, "-L")) { |
| 539 | // Read from last logcat buffer |
| 540 | mLogMode = mLogMode | ANDROID_LOG_PSTORE; |
| 541 | } |
| 542 | name += " "; |
| 543 | name += arg; |
| 544 | } |
Greg Kaiser | fa89cde | 2019-11-04 06:04:42 -0800 | [diff] [blame] | 545 | va_end(args); |
zhouwenjie | c3bf804 | 2019-10-30 14:31:54 -0700 | [diff] [blame] | 546 | |
| 547 | switch (mLogID) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 548 | case LOG_ID_EVENTS: |
| 549 | case LOG_ID_STATS: |
| 550 | case LOG_ID_SECURITY: |
| 551 | mBinary = true; |
| 552 | break; |
| 553 | default: |
| 554 | mBinary = false; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 558 | LogSection::~LogSection() {} |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 559 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 560 | static size_t trimTail(char const* buf, size_t len) { |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 561 | while (len > 0) { |
| 562 | char c = buf[len - 1]; |
| 563 | if (c == '\0' || c == ' ' || c == '\n' || c == '\r' || c == ':') { |
| 564 | len--; |
| 565 | } else { |
| 566 | break; |
| 567 | } |
| 568 | } |
| 569 | return len; |
| 570 | } |
| 571 | |
| 572 | static inline int32_t get4LE(uint8_t const* src) { |
| 573 | return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); |
| 574 | } |
| 575 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 576 | status_t LogSection::BlockingCall(unique_fd& pipeWriteFd) const { |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 577 | // Open log buffer and getting logs since last retrieved time if any. |
| 578 | unique_ptr<logger_list, void (*)(logger_list*)> loggers( |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 579 | gLastLogsRetrieved.find(mLogID) == gLastLogsRetrieved.end() |
zhouwenjie | c3bf804 | 2019-10-30 14:31:54 -0700 | [diff] [blame] | 580 | ? android_logger_list_alloc(mLogMode, 0, 0) |
| 581 | : android_logger_list_alloc_time(mLogMode, gLastLogsRetrieved[mLogID], 0), |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 582 | android_logger_list_free); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 583 | |
| 584 | if (android_logger_open(loggers.get(), mLogID) == NULL) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 585 | ALOGE("[%s] Can't get logger.", this->name.string()); |
Yi Jin | 83fb1d5 | 2018-03-16 12:03:53 -0700 | [diff] [blame] | 586 | return -1; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | log_msg msg; |
| 590 | log_time lastTimestamp(0); |
| 591 | |
| 592 | ProtoOutputStream proto; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 593 | while (true) { // keeps reading until logd buffer is fully read. |
Yi Jin | 83fb1d5 | 2018-03-16 12:03:53 -0700 | [diff] [blame] | 594 | status_t err = android_logger_list_read(loggers.get(), &msg); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 595 | // err = 0 - no content, unexpected connection drop or EOF. |
| 596 | // err = +ive number - size of retrieved data from logger |
| 597 | // err = -ive number, OS supplied error _except_ for -EAGAIN |
| 598 | // err = -EAGAIN, graceful indication for ANDRODI_LOG_NONBLOCK that this is the end of data. |
| 599 | if (err <= 0) { |
| 600 | if (err != -EAGAIN) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 601 | ALOGW("[%s] fails to read a log_msg.\n", this->name.string()); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 602 | } |
Yi Jin | 83fb1d5 | 2018-03-16 12:03:53 -0700 | [diff] [blame] | 603 | // dump previous logs and don't consider this error a failure. |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 604 | break; |
| 605 | } |
| 606 | if (mBinary) { |
| 607 | // remove the first uint32 which is tag's index in event log tags |
| 608 | android_log_context context = create_android_log_parser(msg.msg() + sizeof(uint32_t), |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 609 | msg.len() - sizeof(uint32_t)); |
| 610 | ; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 611 | android_log_list_element elem; |
| 612 | |
Tom Cherry | d026989 | 2019-10-15 17:10:15 -0700 | [diff] [blame] | 613 | lastTimestamp.tv_sec = msg.entry.sec; |
| 614 | lastTimestamp.tv_nsec = msg.entry.nsec; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 615 | |
| 616 | // format a BinaryLogEntry |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 617 | uint64_t token = proto.start(LogProto::BINARY_LOGS); |
Tom Cherry | d026989 | 2019-10-15 17:10:15 -0700 | [diff] [blame] | 618 | proto.write(BinaryLogEntry::SEC, (int32_t)msg.entry.sec); |
| 619 | proto.write(BinaryLogEntry::NANOSEC, (int32_t)msg.entry.nsec); |
| 620 | proto.write(BinaryLogEntry::UID, (int)msg.entry.uid); |
| 621 | proto.write(BinaryLogEntry::PID, msg.entry.pid); |
| 622 | proto.write(BinaryLogEntry::TID, (int32_t)msg.entry.tid); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 623 | proto.write(BinaryLogEntry::TAG_INDEX, |
| 624 | get4LE(reinterpret_cast<uint8_t const*>(msg.msg()))); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 625 | do { |
| 626 | elem = android_log_read_next(context); |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 627 | uint64_t elemToken = proto.start(BinaryLogEntry::ELEMS); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 628 | switch (elem.type) { |
| 629 | case EVENT_TYPE_INT: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 630 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 631 | BinaryLogEntry::Elem::EVENT_TYPE_INT); |
| 632 | proto.write(BinaryLogEntry::Elem::VAL_INT32, (int)elem.data.int32); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 633 | break; |
| 634 | case EVENT_TYPE_LONG: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 635 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 636 | BinaryLogEntry::Elem::EVENT_TYPE_LONG); |
| 637 | proto.write(BinaryLogEntry::Elem::VAL_INT64, (long long)elem.data.int64); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 638 | break; |
| 639 | case EVENT_TYPE_STRING: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 640 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 641 | BinaryLogEntry::Elem::EVENT_TYPE_STRING); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 642 | proto.write(BinaryLogEntry::Elem::VAL_STRING, elem.data.string, elem.len); |
| 643 | break; |
| 644 | case EVENT_TYPE_FLOAT: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 645 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 646 | BinaryLogEntry::Elem::EVENT_TYPE_FLOAT); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 647 | proto.write(BinaryLogEntry::Elem::VAL_FLOAT, elem.data.float32); |
| 648 | break; |
| 649 | case EVENT_TYPE_LIST: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 650 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 651 | BinaryLogEntry::Elem::EVENT_TYPE_LIST); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 652 | break; |
| 653 | case EVENT_TYPE_LIST_STOP: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 654 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 655 | BinaryLogEntry::Elem::EVENT_TYPE_LIST_STOP); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 656 | break; |
| 657 | case EVENT_TYPE_UNKNOWN: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 658 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 659 | BinaryLogEntry::Elem::EVENT_TYPE_UNKNOWN); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 660 | break; |
| 661 | } |
| 662 | proto.end(elemToken); |
| 663 | } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete); |
| 664 | proto.end(token); |
| 665 | if (context) { |
| 666 | android_log_destroy(&context); |
| 667 | } |
| 668 | } else { |
| 669 | AndroidLogEntry entry; |
Tom Cherry | d026989 | 2019-10-15 17:10:15 -0700 | [diff] [blame] | 670 | err = android_log_processLogBuffer(&msg.entry, &entry); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 671 | if (err != NO_ERROR) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 672 | ALOGW("[%s] fails to process to an entry.\n", this->name.string()); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 673 | break; |
| 674 | } |
| 675 | lastTimestamp.tv_sec = entry.tv_sec; |
| 676 | lastTimestamp.tv_nsec = entry.tv_nsec; |
| 677 | |
| 678 | // format a TextLogEntry |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 679 | uint64_t token = proto.start(LogProto::TEXT_LOGS); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 680 | proto.write(TextLogEntry::SEC, (long long)entry.tv_sec); |
| 681 | proto.write(TextLogEntry::NANOSEC, (long long)entry.tv_nsec); |
| 682 | proto.write(TextLogEntry::PRIORITY, (int)entry.priority); |
| 683 | proto.write(TextLogEntry::UID, entry.uid); |
| 684 | proto.write(TextLogEntry::PID, entry.pid); |
| 685 | proto.write(TextLogEntry::TID, entry.tid); |
| 686 | proto.write(TextLogEntry::TAG, entry.tag, trimTail(entry.tag, entry.tagLen)); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 687 | proto.write(TextLogEntry::LOG, entry.message, |
| 688 | trimTail(entry.message, entry.messageLen)); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 689 | proto.end(token); |
| 690 | } |
| 691 | } |
| 692 | gLastLogsRetrieved[mLogID] = lastTimestamp; |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 693 | if (!proto.flush(pipeWriteFd.get()) && errno == EPIPE) { |
Kweku Adams | 5b763c1 | 2018-09-13 15:44:58 -0700 | [diff] [blame] | 694 | ALOGE("[%s] wrote to a broken pipe\n", this->name.string()); |
| 695 | return EPIPE; |
| 696 | } |
Yi Jin | 83fb1d5 | 2018-03-16 12:03:53 -0700 | [diff] [blame] | 697 | return NO_ERROR; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 698 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 699 | |
| 700 | // ================================================================================ |
| 701 | |
| 702 | TombstoneSection::TombstoneSection(int id, const char* type, const int64_t timeoutMs) |
| 703 | : WorkerThreadSection(id, timeoutMs), mType(type) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 704 | name = "tombstone "; |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 705 | name += type; |
| 706 | } |
| 707 | |
| 708 | TombstoneSection::~TombstoneSection() {} |
| 709 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 710 | status_t TombstoneSection::BlockingCall(unique_fd& pipeWriteFd) const { |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 711 | std::unique_ptr<DIR, decltype(&closedir)> proc(opendir("/proc"), closedir); |
| 712 | if (proc.get() == nullptr) { |
| 713 | ALOGE("opendir /proc failed: %s\n", strerror(errno)); |
| 714 | return -errno; |
| 715 | } |
| 716 | |
| 717 | const std::set<int> hal_pids = get_interesting_hal_pids(); |
| 718 | |
| 719 | ProtoOutputStream proto; |
| 720 | struct dirent* d; |
| 721 | status_t err = NO_ERROR; |
| 722 | while ((d = readdir(proc.get()))) { |
| 723 | int pid = atoi(d->d_name); |
| 724 | if (pid <= 0) { |
| 725 | continue; |
| 726 | } |
| 727 | |
| 728 | const std::string link_name = android::base::StringPrintf("/proc/%d/exe", pid); |
| 729 | std::string exe; |
| 730 | if (!android::base::Readlink(link_name, &exe)) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 731 | ALOGE("Section %s: Can't read '%s': %s\n", name.string(), |
| 732 | link_name.c_str(), strerror(errno)); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 733 | continue; |
| 734 | } |
| 735 | |
| 736 | bool is_java_process; |
| 737 | if (exe == "/system/bin/app_process32" || exe == "/system/bin/app_process64") { |
| 738 | if (mType != "java") continue; |
| 739 | // Don't bother dumping backtraces for the zygote. |
| 740 | if (IsZygote(pid)) { |
| 741 | VLOG("Skipping Zygote"); |
| 742 | continue; |
| 743 | } |
| 744 | |
| 745 | is_java_process = true; |
| 746 | } else if (should_dump_native_traces(exe.c_str())) { |
| 747 | if (mType != "native") continue; |
| 748 | is_java_process = false; |
| 749 | } else if (hal_pids.find(pid) != hal_pids.end()) { |
| 750 | if (mType != "hal") continue; |
| 751 | is_java_process = false; |
| 752 | } else { |
| 753 | // Probably a native process we don't care about, continue. |
| 754 | VLOG("Skipping %d", pid); |
| 755 | continue; |
| 756 | } |
| 757 | |
| 758 | Fpipe dumpPipe; |
| 759 | if (!dumpPipe.init()) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 760 | ALOGW("[%s] failed to setup dump pipe", this->name.string()); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 761 | err = -errno; |
| 762 | break; |
| 763 | } |
| 764 | |
| 765 | const uint64_t start = Nanotime(); |
| 766 | pid_t child = fork(); |
| 767 | if (child < 0) { |
| 768 | ALOGE("Failed to fork child process"); |
| 769 | break; |
| 770 | } else if (child == 0) { |
| 771 | // This is the child process. |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 772 | dumpPipe.readFd().reset(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 773 | const int ret = dump_backtrace_to_file_timeout( |
| 774 | pid, is_java_process ? kDebuggerdJavaBacktrace : kDebuggerdNativeBacktrace, |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 775 | is_java_process ? 5 : 20, dumpPipe.writeFd().get()); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 776 | if (ret == -1) { |
| 777 | if (errno == 0) { |
| 778 | ALOGW("Dumping failed for pid '%d', likely due to a timeout\n", pid); |
| 779 | } else { |
| 780 | ALOGE("Dumping failed for pid '%d': %s\n", pid, strerror(errno)); |
| 781 | } |
| 782 | } |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 783 | dumpPipe.writeFd().reset(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 784 | _exit(EXIT_SUCCESS); |
| 785 | } |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 786 | dumpPipe.writeFd().reset(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 787 | // Parent process. |
| 788 | // Read from the pipe concurrently to avoid blocking the child. |
| 789 | FdBuffer buffer; |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 790 | err = buffer.readFully(dumpPipe.readFd().get()); |
Kweku Adams | a8a56c8 | 2018-04-23 06:15:31 -0700 | [diff] [blame] | 791 | // Wait on the child to avoid it becoming a zombie process. |
| 792 | status_t cStatus = wait_child(child); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 793 | if (err != NO_ERROR) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 794 | ALOGW("[%s] failed to read stack dump: %d", this->name.string(), err); |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 795 | dumpPipe.readFd().reset(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 796 | break; |
| 797 | } |
Kweku Adams | a8a56c8 | 2018-04-23 06:15:31 -0700 | [diff] [blame] | 798 | if (cStatus != NO_ERROR) { |
Kweku Adams | 5b763c1 | 2018-09-13 15:44:58 -0700 | [diff] [blame] | 799 | ALOGE("[%s] child had an issue: %s\n", this->name.string(), strerror(-cStatus)); |
Kweku Adams | a8a56c8 | 2018-04-23 06:15:31 -0700 | [diff] [blame] | 800 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 801 | |
| 802 | auto dump = std::make_unique<char[]>(buffer.size()); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 803 | sp<ProtoReader> reader = buffer.data()->read(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 804 | int i = 0; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 805 | while (reader->hasNext()) { |
| 806 | dump[i] = reader->next(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 807 | i++; |
| 808 | } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 809 | uint64_t token = proto.start(android::os::BackTraceProto::TRACES); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 810 | proto.write(android::os::BackTraceProto::Stack::PID, pid); |
| 811 | proto.write(android::os::BackTraceProto::Stack::DUMP, dump.get(), i); |
| 812 | proto.write(android::os::BackTraceProto::Stack::DUMP_DURATION_NS, |
| 813 | static_cast<long long>(Nanotime() - start)); |
| 814 | proto.end(token); |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 815 | dumpPipe.readFd().reset(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 816 | } |
| 817 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 818 | if (!proto.flush(pipeWriteFd.get()) && errno == EPIPE) { |
Kweku Adams | 5b763c1 | 2018-09-13 15:44:58 -0700 | [diff] [blame] | 819 | ALOGE("[%s] wrote to a broken pipe\n", this->name.string()); |
| 820 | if (err != NO_ERROR) { |
| 821 | return EPIPE; |
| 822 | } |
| 823 | } |
| 824 | |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 825 | return err; |
| 826 | } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 827 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 828 | // ================================================================================ |
| 829 | BringYourOwnSection::BringYourOwnSection(int id, const char* customName, const uid_t callingUid, |
| 830 | const sp<IIncidentDumpCallback>& callback) |
| 831 | : WorkerThreadSection(id, REMOTE_CALL_TIMEOUT_MS), uid(callingUid), mCallback(callback) { |
| 832 | name = "registered "; |
| 833 | name += customName; |
| 834 | } |
| 835 | |
| 836 | BringYourOwnSection::~BringYourOwnSection() {} |
| 837 | |
| 838 | status_t BringYourOwnSection::BlockingCall(unique_fd& pipeWriteFd) const { |
| 839 | android::os::ParcelFileDescriptor pfd(std::move(pipeWriteFd)); |
| 840 | mCallback->onDumpSection(pfd); |
| 841 | return NO_ERROR; |
| 842 | } |
| 843 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 844 | } // namespace incidentd |
| 845 | } // namespace os |
Yi Jin | 98ce810 | 2018-04-12 11:15:39 -0700 | [diff] [blame] | 846 | } // namespace android |