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