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 | |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 24 | #include <mutex> |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 25 | #include <set> |
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" |
| 45 | #include "incidentd_util.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 46 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 47 | namespace android { |
| 48 | namespace os { |
| 49 | namespace incidentd { |
| 50 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 51 | using namespace android::base; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 52 | using namespace android::util; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 53 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 54 | // special section ids |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 55 | const int FIELD_ID_INCIDENT_METADATA = 2; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 56 | |
| 57 | // incident section parameters |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 58 | const char INCIDENT_HELPER[] = "/system/bin/incident_helper"; |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 59 | const char* GZIP[] = {"/system/bin/gzip", NULL}; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 60 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 61 | 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] | 62 | const char* ihArgs[]{INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL}; |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 63 | return fork_execute_cmd(const_cast<char**>(ihArgs), p2cPipe, c2pPipe); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Joe Onorato | 7a406b4 | 2019-04-12 18:11:30 -0700 | [diff] [blame^] | 66 | bool section_requires_specific_mention(int sectionId) { |
| 67 | switch (sectionId) { |
| 68 | case 3025: // restricted_images |
| 69 | return true; |
| 70 | default: |
| 71 | return false; |
| 72 | } |
| 73 | } |
| 74 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 75 | // ================================================================================ |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 76 | Section::Section(int i, int64_t timeoutMs) |
Kweku Adams | 3d16091 | 2018-05-07 11:26:27 -0700 | [diff] [blame] | 77 | : id(i), |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 78 | timeoutMs(timeoutMs) { |
| 79 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 80 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 81 | Section::~Section() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 82 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 83 | // ================================================================================ |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 84 | static inline bool isSysfs(const char* filename) { return strncmp(filename, "/sys/", 5) == 0; } |
| 85 | |
Kweku Adams | e04ef77 | 2018-06-13 12:24:38 -0700 | [diff] [blame] | 86 | FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs) |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 87 | : Section(id, timeoutMs), mFilename(filename) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 88 | name = "file "; |
| 89 | name += filename; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 90 | mIsSysfs = isSysfs(filename); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | FileSection::~FileSection() {} |
| 94 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 95 | status_t FileSection::Execute(ReportWriter* writer) const { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 96 | // read from mFilename first, make sure the file is available |
| 97 | // 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] | 98 | unique_fd fd(open(mFilename, O_RDONLY | O_CLOEXEC)); |
| 99 | if (fd.get() == -1) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 100 | ALOGW("[%s] failed to open file", this->name.string()); |
Kweku Adams | e04ef77 | 2018-06-13 12:24:38 -0700 | [diff] [blame] | 101 | // There may be some devices/architectures that won't have the file. |
| 102 | // Just return here without an error. |
| 103 | return NO_ERROR; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 106 | FdBuffer buffer; |
| 107 | Fpipe p2cPipe; |
| 108 | Fpipe c2pPipe; |
| 109 | // initiate pipes to pass data to/from incident_helper |
| 110 | if (!p2cPipe.init() || !c2pPipe.init()) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 111 | ALOGW("[%s] failed to setup pipes", this->name.string()); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 112 | return -errno; |
| 113 | } |
| 114 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 115 | pid_t pid = fork_execute_incident_helper(this->id, &p2cPipe, &c2pPipe); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 116 | if (pid == -1) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 117 | ALOGW("[%s] failed to fork", this->name.string()); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 118 | return -errno; |
| 119 | } |
| 120 | |
| 121 | // parent process |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 122 | status_t readStatus = buffer.readProcessedDataInStream(fd.get(), std::move(p2cPipe.writeFd()), |
| 123 | std::move(c2pPipe.readFd()), |
| 124 | this->timeoutMs, mIsSysfs); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 125 | writer->setSectionStats(buffer); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 126 | if (readStatus != NO_ERROR || buffer.timedOut()) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 127 | ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 128 | this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false"); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 129 | kill_child(pid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 130 | return readStatus; |
| 131 | } |
| 132 | |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 133 | status_t ihStatus = wait_child(pid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 134 | if (ihStatus != NO_ERROR) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 135 | ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-ihStatus)); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 136 | return ihStatus; |
| 137 | } |
| 138 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 139 | return writer->writeSection(buffer); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 140 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 141 | // ================================================================================ |
| 142 | GZipSection::GZipSection(int id, const char* filename, ...) : Section(id) { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 143 | va_list args; |
| 144 | va_start(args, filename); |
| 145 | mFilenames = varargs(filename, args); |
| 146 | va_end(args); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 147 | name = "gzip"; |
| 148 | for (int i = 0; mFilenames[i] != NULL; i++) { |
| 149 | name += " "; |
| 150 | name += mFilenames[i]; |
| 151 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 152 | } |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 153 | |
Yi Jin | 480de78 | 2018-04-06 15:37:36 -0700 | [diff] [blame] | 154 | GZipSection::~GZipSection() { free(mFilenames); } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 155 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 156 | status_t GZipSection::Execute(ReportWriter* writer) const { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 157 | // Reads the files in order, use the first available one. |
| 158 | int index = 0; |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 159 | unique_fd fd; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 160 | while (mFilenames[index] != NULL) { |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 161 | fd.reset(open(mFilenames[index], O_RDONLY | O_CLOEXEC)); |
| 162 | if (fd.get() != -1) { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 163 | break; |
| 164 | } |
| 165 | ALOGW("GZipSection failed to open file %s", mFilenames[index]); |
| 166 | index++; // look at the next file. |
| 167 | } |
Yi Jin | c858e27 | 2018-03-28 15:32:50 -0700 | [diff] [blame] | 168 | if (fd.get() == -1) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 169 | ALOGW("[%s] can't open all the files", this->name.string()); |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 170 | 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] | 171 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 172 | FdBuffer buffer; |
| 173 | Fpipe p2cPipe; |
| 174 | Fpipe c2pPipe; |
| 175 | // initiate pipes to pass data to/from gzip |
| 176 | if (!p2cPipe.init() || !c2pPipe.init()) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 177 | ALOGW("[%s] failed to setup pipes", this->name.string()); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 178 | return -errno; |
| 179 | } |
| 180 | |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 181 | pid_t pid = fork_execute_cmd((char* const*)GZIP, &p2cPipe, &c2pPipe); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 182 | if (pid == -1) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 183 | ALOGW("[%s] failed to fork", this->name.string()); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 184 | return -errno; |
| 185 | } |
| 186 | // parent process |
| 187 | |
| 188 | // construct Fdbuffer to output GZippedfileProto, the reason to do this instead of using |
| 189 | // ProtoOutputStream is to avoid allocation of another buffer inside ProtoOutputStream. |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 190 | sp<EncodedBuffer> internalBuffer = buffer.data(); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 191 | internalBuffer->writeHeader((uint32_t)GZippedFileProto::FILENAME, WIRE_TYPE_LENGTH_DELIMITED); |
Yi Jin | a9635e4 | 2018-03-23 12:05:32 -0700 | [diff] [blame] | 192 | size_t fileLen = strlen(mFilenames[index]); |
| 193 | internalBuffer->writeRawVarint32(fileLen); |
| 194 | for (size_t i = 0; i < fileLen; i++) { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 195 | internalBuffer->writeRawByte(mFilenames[index][i]); |
| 196 | } |
| 197 | internalBuffer->writeHeader((uint32_t)GZippedFileProto::GZIPPED_DATA, |
| 198 | WIRE_TYPE_LENGTH_DELIMITED); |
| 199 | size_t editPos = internalBuffer->wp()->pos(); |
| 200 | internalBuffer->wp()->move(8); // reserve 8 bytes for the varint of the data size. |
| 201 | size_t dataBeginAt = internalBuffer->wp()->pos(); |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 202 | VLOG("[%s] editPos=%zu, dataBeginAt=%zu", this->name.string(), editPos, dataBeginAt); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 203 | |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 204 | status_t readStatus = buffer.readProcessedDataInStream( |
| 205 | fd.get(), std::move(p2cPipe.writeFd()), std::move(c2pPipe.readFd()), this->timeoutMs, |
| 206 | isSysfs(mFilenames[index])); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 207 | writer->setSectionStats(buffer); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 208 | if (readStatus != NO_ERROR || buffer.timedOut()) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 209 | ALOGW("[%s] failed to read data from gzip: %s, timedout: %s", this->name.string(), |
| 210 | strerror(-readStatus), buffer.timedOut() ? "true" : "false"); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 211 | kill_child(pid); |
| 212 | return readStatus; |
| 213 | } |
| 214 | |
| 215 | status_t gzipStatus = wait_child(pid); |
| 216 | if (gzipStatus != NO_ERROR) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 217 | ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-gzipStatus)); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 218 | return gzipStatus; |
| 219 | } |
| 220 | // Revisit the actual size from gzip result and edit the internal buffer accordingly. |
| 221 | size_t dataSize = buffer.size() - dataBeginAt; |
| 222 | internalBuffer->wp()->rewind()->move(editPos); |
| 223 | internalBuffer->writeRawVarint32(dataSize); |
| 224 | internalBuffer->copy(dataBeginAt, dataSize); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 225 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 226 | return writer->writeSection(buffer); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 227 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 228 | |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 229 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 230 | struct WorkerThreadData : public virtual RefBase { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 231 | const WorkerThreadSection* section; |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 232 | Fpipe pipe; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 233 | |
| 234 | // Lock protects these fields |
| 235 | mutex lock; |
| 236 | bool workerDone; |
| 237 | status_t workerError; |
| 238 | |
Chih-Hung Hsieh | 7a88a93 | 2018-12-20 13:45:04 -0800 | [diff] [blame] | 239 | explicit WorkerThreadData(const WorkerThreadSection* section); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 240 | virtual ~WorkerThreadData(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec) |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 244 | : section(sec), workerDone(false), workerError(NO_ERROR) {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 245 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 246 | WorkerThreadData::~WorkerThreadData() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 247 | |
| 248 | // ================================================================================ |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 249 | WorkerThreadSection::WorkerThreadSection(int id, const int64_t timeoutMs) |
| 250 | : Section(id, timeoutMs) {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 251 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 252 | WorkerThreadSection::~WorkerThreadSection() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 253 | |
Kweku Adams | 5b763c1 | 2018-09-13 15:44:58 -0700 | [diff] [blame] | 254 | void sigpipe_handler(int signum) { |
| 255 | if (signum == SIGPIPE) { |
| 256 | ALOGE("Wrote to a broken pipe\n"); |
| 257 | } else { |
| 258 | ALOGE("Received unexpected signal: %d\n", signum); |
| 259 | } |
| 260 | } |
| 261 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 262 | static void* worker_thread_func(void* cookie) { |
Kweku Adams | 5b763c1 | 2018-09-13 15:44:58 -0700 | [diff] [blame] | 263 | // Don't crash the service if we write to a closed pipe (which can happen if |
| 264 | // dumping times out). |
| 265 | signal(SIGPIPE, sigpipe_handler); |
| 266 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 267 | WorkerThreadData* data = (WorkerThreadData*)cookie; |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 268 | status_t err = data->section->BlockingCall(data->pipe.writeFd().get()); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 269 | |
| 270 | { |
| 271 | unique_lock<mutex> lock(data->lock); |
| 272 | data->workerDone = true; |
| 273 | data->workerError = err; |
| 274 | } |
| 275 | |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 276 | data->pipe.writeFd().reset(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 277 | data->decStrong(data->section); |
| 278 | // data might be gone now. don't use it after this point in this thread. |
| 279 | return NULL; |
| 280 | } |
| 281 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 282 | status_t WorkerThreadSection::Execute(ReportWriter* writer) const { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 283 | status_t err = NO_ERROR; |
| 284 | pthread_t thread; |
| 285 | pthread_attr_t attr; |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 286 | bool workerDone = false; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 287 | FdBuffer buffer; |
| 288 | |
| 289 | // Data shared between this thread and the worker thread. |
| 290 | sp<WorkerThreadData> data = new WorkerThreadData(this); |
| 291 | |
| 292 | // Create the pipe |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 293 | if (!data->pipe.init()) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 294 | return -errno; |
| 295 | } |
| 296 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 297 | // Create the thread |
| 298 | err = pthread_attr_init(&attr); |
| 299 | if (err != 0) { |
| 300 | return -err; |
| 301 | } |
| 302 | // TODO: Do we need to tweak thread priority? |
| 303 | err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 304 | if (err != 0) { |
| 305 | pthread_attr_destroy(&attr); |
| 306 | return -err; |
| 307 | } |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 308 | |
| 309 | // The worker thread needs a reference and we can't let the count go to zero |
| 310 | // if that thread is slow to start. |
| 311 | data->incStrong(this); |
| 312 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 313 | err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get()); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 314 | pthread_attr_destroy(&attr); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 315 | if (err != 0) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 316 | data->decStrong(this); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 317 | return -err; |
| 318 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 319 | |
| 320 | // Loop reading until either the timeout or the worker side is done (i.e. eof). |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 321 | err = buffer.read(data->pipe.readFd().get(), this->timeoutMs); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 322 | if (err != NO_ERROR) { |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 323 | ALOGE("[%s] reader failed with error '%s'", this->name.string(), strerror(-err)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | // Done with the read fd. The worker thread closes the write one so |
| 327 | // we never race and get here first. |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 328 | data->pipe.readFd().reset(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 329 | |
| 330 | // 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] | 331 | // 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] | 332 | { |
| 333 | unique_lock<mutex> lock(data->lock); |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 334 | if (data->workerError != NO_ERROR) { |
| 335 | err = data->workerError; |
| 336 | ALOGE("[%s] worker failed with error '%s'", this->name.string(), strerror(-err)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 337 | } |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 338 | workerDone = data->workerDone; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 339 | } |
Kweku Adams | 5b763c1 | 2018-09-13 15:44:58 -0700 | [diff] [blame] | 340 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 341 | writer->setSectionStats(buffer); |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 342 | if (err != NO_ERROR) { |
| 343 | char errMsg[128]; |
| 344 | snprintf(errMsg, 128, "[%s] failed with error '%s'", |
| 345 | this->name.string(), strerror(-err)); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 346 | writer->error(this, err, "WorkerThreadSection failed."); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 347 | return NO_ERROR; |
| 348 | } |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 349 | if (buffer.truncated()) { |
| 350 | ALOGW("[%s] too large, truncating", this->name.string()); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 351 | // 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] | 352 | return NO_ERROR; |
| 353 | } |
| 354 | if (!workerDone || buffer.timedOut()) { |
| 355 | ALOGW("[%s] timed out", this->name.string()); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 356 | return NO_ERROR; |
| 357 | } |
| 358 | |
| 359 | // Write the data that was collected |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 360 | return writer->writeSection(buffer); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // ================================================================================ |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 364 | CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...) |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 365 | : Section(id, timeoutMs) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 366 | va_list args; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 367 | va_start(args, command); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 368 | mCommand = varargs(command, args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 369 | va_end(args); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 370 | name = "cmd"; |
| 371 | for (int i = 0; mCommand[i] != NULL; i++) { |
| 372 | name += " "; |
| 373 | name += mCommand[i]; |
| 374 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 375 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 376 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 377 | CommandSection::CommandSection(int id, const char* command, ...) : Section(id) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 378 | va_list args; |
| 379 | va_start(args, command); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 380 | mCommand = varargs(command, args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 381 | va_end(args); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 382 | name = "cmd"; |
| 383 | for (int i = 0; mCommand[i] != NULL; i++) { |
| 384 | name += " "; |
| 385 | name += mCommand[i]; |
| 386 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 389 | CommandSection::~CommandSection() { free(mCommand); } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 390 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 391 | status_t CommandSection::Execute(ReportWriter* writer) const { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 392 | FdBuffer buffer; |
| 393 | Fpipe cmdPipe; |
| 394 | Fpipe ihPipe; |
| 395 | |
| 396 | if (!cmdPipe.init() || !ihPipe.init()) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 397 | ALOGW("[%s] failed to setup pipes", this->name.string()); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 398 | return -errno; |
| 399 | } |
| 400 | |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 401 | pid_t cmdPid = fork_execute_cmd((char* const*)mCommand, NULL, &cmdPipe); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 402 | if (cmdPid == -1) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 403 | ALOGW("[%s] failed to fork", this->name.string()); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 404 | return -errno; |
| 405 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 406 | pid_t ihPid = fork_execute_incident_helper(this->id, &cmdPipe, &ihPipe); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 407 | if (ihPid == -1) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 408 | ALOGW("[%s] failed to fork", this->name.string()); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 409 | return -errno; |
| 410 | } |
| 411 | |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 412 | cmdPipe.writeFd().reset(); |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 413 | status_t readStatus = buffer.read(ihPipe.readFd().get(), this->timeoutMs); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 414 | writer->setSectionStats(buffer); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 415 | if (readStatus != NO_ERROR || buffer.timedOut()) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 416 | ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 417 | this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false"); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 418 | kill_child(cmdPid); |
| 419 | kill_child(ihPid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 420 | return readStatus; |
| 421 | } |
| 422 | |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 423 | // 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] | 424 | // 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] | 425 | status_t cmdStatus = wait_child(cmdPid); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 426 | status_t ihStatus = wait_child(ihPid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 427 | if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 428 | ALOGW("[%s] abnormal child processes, return status: command: %s, incident helper: %s", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 429 | this->name.string(), strerror(-cmdStatus), strerror(-ihStatus)); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 430 | // Not a fatal error. |
| 431 | return NO_ERROR; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 434 | return writer->writeSection(buffer); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | // ================================================================================ |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 438 | DumpsysSection::DumpsysSection(int id, const char* service, ...) |
| 439 | : WorkerThreadSection(id, REMOTE_CALL_TIMEOUT_MS), mService(service) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 440 | name = "dumpsys "; |
| 441 | name += service; |
| 442 | |
| 443 | va_list args; |
| 444 | va_start(args, service); |
| 445 | while (true) { |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 446 | const char* arg = va_arg(args, const char*); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 447 | if (arg == NULL) { |
| 448 | break; |
| 449 | } |
| 450 | mArgs.add(String16(arg)); |
| 451 | name += " "; |
| 452 | name += arg; |
| 453 | } |
| 454 | va_end(args); |
| 455 | } |
| 456 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 457 | DumpsysSection::~DumpsysSection() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 458 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 459 | status_t DumpsysSection::BlockingCall(int pipeWriteFd) const { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 460 | // checkService won't wait for the service to show up like getService will. |
| 461 | sp<IBinder> service = defaultServiceManager()->checkService(mService); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 462 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 463 | if (service == NULL) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 464 | ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string()); |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 465 | return NAME_NOT_FOUND; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | service->dump(pipeWriteFd, mArgs); |
| 469 | |
| 470 | return NO_ERROR; |
| 471 | } |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 472 | |
| 473 | // ================================================================================ |
| 474 | // initialization only once in Section.cpp. |
| 475 | map<log_id_t, log_time> LogSection::gLastLogsRetrieved; |
| 476 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 477 | LogSection::LogSection(int id, log_id_t logID) : WorkerThreadSection(id), mLogID(logID) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 478 | name = "logcat "; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 479 | name += android_log_id_to_name(logID); |
| 480 | switch (logID) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 481 | case LOG_ID_EVENTS: |
| 482 | case LOG_ID_STATS: |
| 483 | case LOG_ID_SECURITY: |
| 484 | mBinary = true; |
| 485 | break; |
| 486 | default: |
| 487 | mBinary = false; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 491 | LogSection::~LogSection() {} |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 492 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 493 | static size_t trimTail(char const* buf, size_t len) { |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 494 | while (len > 0) { |
| 495 | char c = buf[len - 1]; |
| 496 | if (c == '\0' || c == ' ' || c == '\n' || c == '\r' || c == ':') { |
| 497 | len--; |
| 498 | } else { |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | return len; |
| 503 | } |
| 504 | |
| 505 | static inline int32_t get4LE(uint8_t const* src) { |
| 506 | return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); |
| 507 | } |
| 508 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 509 | status_t LogSection::BlockingCall(int pipeWriteFd) const { |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 510 | // Open log buffer and getting logs since last retrieved time if any. |
| 511 | unique_ptr<logger_list, void (*)(logger_list*)> loggers( |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 512 | gLastLogsRetrieved.find(mLogID) == gLastLogsRetrieved.end() |
| 513 | ? android_logger_list_alloc(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, 0) |
| 514 | : android_logger_list_alloc_time(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, |
| 515 | gLastLogsRetrieved[mLogID], 0), |
| 516 | android_logger_list_free); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 517 | |
| 518 | if (android_logger_open(loggers.get(), mLogID) == NULL) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 519 | ALOGE("[%s] Can't get logger.", this->name.string()); |
Yi Jin | 83fb1d5 | 2018-03-16 12:03:53 -0700 | [diff] [blame] | 520 | return -1; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | log_msg msg; |
| 524 | log_time lastTimestamp(0); |
| 525 | |
| 526 | ProtoOutputStream proto; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 527 | while (true) { // keeps reading until logd buffer is fully read. |
Yi Jin | 83fb1d5 | 2018-03-16 12:03:53 -0700 | [diff] [blame] | 528 | status_t err = android_logger_list_read(loggers.get(), &msg); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 529 | // err = 0 - no content, unexpected connection drop or EOF. |
| 530 | // err = +ive number - size of retrieved data from logger |
| 531 | // err = -ive number, OS supplied error _except_ for -EAGAIN |
| 532 | // err = -EAGAIN, graceful indication for ANDRODI_LOG_NONBLOCK that this is the end of data. |
| 533 | if (err <= 0) { |
| 534 | if (err != -EAGAIN) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 535 | ALOGW("[%s] fails to read a log_msg.\n", this->name.string()); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 536 | } |
Yi Jin | 83fb1d5 | 2018-03-16 12:03:53 -0700 | [diff] [blame] | 537 | // dump previous logs and don't consider this error a failure. |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 538 | break; |
| 539 | } |
| 540 | if (mBinary) { |
| 541 | // remove the first uint32 which is tag's index in event log tags |
| 542 | 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] | 543 | msg.len() - sizeof(uint32_t)); |
| 544 | ; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 545 | android_log_list_element elem; |
| 546 | |
| 547 | lastTimestamp.tv_sec = msg.entry_v1.sec; |
| 548 | lastTimestamp.tv_nsec = msg.entry_v1.nsec; |
| 549 | |
| 550 | // format a BinaryLogEntry |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 551 | uint64_t token = proto.start(LogProto::BINARY_LOGS); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 552 | proto.write(BinaryLogEntry::SEC, msg.entry_v1.sec); |
| 553 | proto.write(BinaryLogEntry::NANOSEC, msg.entry_v1.nsec); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 554 | proto.write(BinaryLogEntry::UID, (int)msg.entry_v4.uid); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 555 | proto.write(BinaryLogEntry::PID, msg.entry_v1.pid); |
| 556 | proto.write(BinaryLogEntry::TID, msg.entry_v1.tid); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 557 | proto.write(BinaryLogEntry::TAG_INDEX, |
| 558 | get4LE(reinterpret_cast<uint8_t const*>(msg.msg()))); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 559 | do { |
| 560 | elem = android_log_read_next(context); |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 561 | uint64_t elemToken = proto.start(BinaryLogEntry::ELEMS); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 562 | switch (elem.type) { |
| 563 | case EVENT_TYPE_INT: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 564 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 565 | BinaryLogEntry::Elem::EVENT_TYPE_INT); |
| 566 | proto.write(BinaryLogEntry::Elem::VAL_INT32, (int)elem.data.int32); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 567 | break; |
| 568 | case EVENT_TYPE_LONG: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 569 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 570 | BinaryLogEntry::Elem::EVENT_TYPE_LONG); |
| 571 | proto.write(BinaryLogEntry::Elem::VAL_INT64, (long long)elem.data.int64); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 572 | break; |
| 573 | case EVENT_TYPE_STRING: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 574 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 575 | BinaryLogEntry::Elem::EVENT_TYPE_STRING); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 576 | proto.write(BinaryLogEntry::Elem::VAL_STRING, elem.data.string, elem.len); |
| 577 | break; |
| 578 | case EVENT_TYPE_FLOAT: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 579 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 580 | BinaryLogEntry::Elem::EVENT_TYPE_FLOAT); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 581 | proto.write(BinaryLogEntry::Elem::VAL_FLOAT, elem.data.float32); |
| 582 | break; |
| 583 | case EVENT_TYPE_LIST: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 584 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 585 | BinaryLogEntry::Elem::EVENT_TYPE_LIST); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 586 | break; |
| 587 | case EVENT_TYPE_LIST_STOP: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 588 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 589 | BinaryLogEntry::Elem::EVENT_TYPE_LIST_STOP); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 590 | break; |
| 591 | case EVENT_TYPE_UNKNOWN: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 592 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 593 | BinaryLogEntry::Elem::EVENT_TYPE_UNKNOWN); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 594 | break; |
| 595 | } |
| 596 | proto.end(elemToken); |
| 597 | } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete); |
| 598 | proto.end(token); |
| 599 | if (context) { |
| 600 | android_log_destroy(&context); |
| 601 | } |
| 602 | } else { |
| 603 | AndroidLogEntry entry; |
| 604 | err = android_log_processLogBuffer(&msg.entry_v1, &entry); |
| 605 | if (err != NO_ERROR) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 606 | ALOGW("[%s] fails to process to an entry.\n", this->name.string()); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 607 | break; |
| 608 | } |
| 609 | lastTimestamp.tv_sec = entry.tv_sec; |
| 610 | lastTimestamp.tv_nsec = entry.tv_nsec; |
| 611 | |
| 612 | // format a TextLogEntry |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 613 | uint64_t token = proto.start(LogProto::TEXT_LOGS); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 614 | proto.write(TextLogEntry::SEC, (long long)entry.tv_sec); |
| 615 | proto.write(TextLogEntry::NANOSEC, (long long)entry.tv_nsec); |
| 616 | proto.write(TextLogEntry::PRIORITY, (int)entry.priority); |
| 617 | proto.write(TextLogEntry::UID, entry.uid); |
| 618 | proto.write(TextLogEntry::PID, entry.pid); |
| 619 | proto.write(TextLogEntry::TID, entry.tid); |
| 620 | proto.write(TextLogEntry::TAG, entry.tag, trimTail(entry.tag, entry.tagLen)); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 621 | proto.write(TextLogEntry::LOG, entry.message, |
| 622 | trimTail(entry.message, entry.messageLen)); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 623 | proto.end(token); |
| 624 | } |
| 625 | } |
| 626 | gLastLogsRetrieved[mLogID] = lastTimestamp; |
Kweku Adams | 5b763c1 | 2018-09-13 15:44:58 -0700 | [diff] [blame] | 627 | if (!proto.flush(pipeWriteFd) && errno == EPIPE) { |
| 628 | ALOGE("[%s] wrote to a broken pipe\n", this->name.string()); |
| 629 | return EPIPE; |
| 630 | } |
Yi Jin | 83fb1d5 | 2018-03-16 12:03:53 -0700 | [diff] [blame] | 631 | return NO_ERROR; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 632 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 633 | |
| 634 | // ================================================================================ |
| 635 | |
| 636 | TombstoneSection::TombstoneSection(int id, const char* type, const int64_t timeoutMs) |
| 637 | : WorkerThreadSection(id, timeoutMs), mType(type) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 638 | name = "tombstone "; |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 639 | name += type; |
| 640 | } |
| 641 | |
| 642 | TombstoneSection::~TombstoneSection() {} |
| 643 | |
| 644 | status_t TombstoneSection::BlockingCall(int pipeWriteFd) const { |
| 645 | std::unique_ptr<DIR, decltype(&closedir)> proc(opendir("/proc"), closedir); |
| 646 | if (proc.get() == nullptr) { |
| 647 | ALOGE("opendir /proc failed: %s\n", strerror(errno)); |
| 648 | return -errno; |
| 649 | } |
| 650 | |
| 651 | const std::set<int> hal_pids = get_interesting_hal_pids(); |
| 652 | |
| 653 | ProtoOutputStream proto; |
| 654 | struct dirent* d; |
| 655 | status_t err = NO_ERROR; |
| 656 | while ((d = readdir(proc.get()))) { |
| 657 | int pid = atoi(d->d_name); |
| 658 | if (pid <= 0) { |
| 659 | continue; |
| 660 | } |
| 661 | |
| 662 | const std::string link_name = android::base::StringPrintf("/proc/%d/exe", pid); |
| 663 | std::string exe; |
| 664 | if (!android::base::Readlink(link_name, &exe)) { |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 665 | ALOGE("Section %s: Can't read '%s': %s\n", name.string(), |
| 666 | link_name.c_str(), strerror(errno)); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 667 | continue; |
| 668 | } |
| 669 | |
| 670 | bool is_java_process; |
| 671 | if (exe == "/system/bin/app_process32" || exe == "/system/bin/app_process64") { |
| 672 | if (mType != "java") continue; |
| 673 | // Don't bother dumping backtraces for the zygote. |
| 674 | if (IsZygote(pid)) { |
| 675 | VLOG("Skipping Zygote"); |
| 676 | continue; |
| 677 | } |
| 678 | |
| 679 | is_java_process = true; |
| 680 | } else if (should_dump_native_traces(exe.c_str())) { |
| 681 | if (mType != "native") continue; |
| 682 | is_java_process = false; |
| 683 | } else if (hal_pids.find(pid) != hal_pids.end()) { |
| 684 | if (mType != "hal") continue; |
| 685 | is_java_process = false; |
| 686 | } else { |
| 687 | // Probably a native process we don't care about, continue. |
| 688 | VLOG("Skipping %d", pid); |
| 689 | continue; |
| 690 | } |
| 691 | |
| 692 | Fpipe dumpPipe; |
| 693 | if (!dumpPipe.init()) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 694 | ALOGW("[%s] failed to setup dump pipe", this->name.string()); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 695 | err = -errno; |
| 696 | break; |
| 697 | } |
| 698 | |
| 699 | const uint64_t start = Nanotime(); |
| 700 | pid_t child = fork(); |
| 701 | if (child < 0) { |
| 702 | ALOGE("Failed to fork child process"); |
| 703 | break; |
| 704 | } else if (child == 0) { |
| 705 | // This is the child process. |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 706 | dumpPipe.readFd().reset(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 707 | const int ret = dump_backtrace_to_file_timeout( |
| 708 | pid, is_java_process ? kDebuggerdJavaBacktrace : kDebuggerdNativeBacktrace, |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 709 | is_java_process ? 5 : 20, dumpPipe.writeFd().get()); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 710 | if (ret == -1) { |
| 711 | if (errno == 0) { |
| 712 | ALOGW("Dumping failed for pid '%d', likely due to a timeout\n", pid); |
| 713 | } else { |
| 714 | ALOGE("Dumping failed for pid '%d': %s\n", pid, strerror(errno)); |
| 715 | } |
| 716 | } |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 717 | dumpPipe.writeFd().reset(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 718 | _exit(EXIT_SUCCESS); |
| 719 | } |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 720 | dumpPipe.writeFd().reset(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 721 | // Parent process. |
| 722 | // Read from the pipe concurrently to avoid blocking the child. |
| 723 | FdBuffer buffer; |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 724 | err = buffer.readFully(dumpPipe.readFd().get()); |
Kweku Adams | a8a56c8 | 2018-04-23 06:15:31 -0700 | [diff] [blame] | 725 | // Wait on the child to avoid it becoming a zombie process. |
| 726 | status_t cStatus = wait_child(child); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 727 | if (err != NO_ERROR) { |
Yi Jin | 3be0b43 | 2018-04-20 17:08:11 -0700 | [diff] [blame] | 728 | ALOGW("[%s] failed to read stack dump: %d", this->name.string(), err); |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 729 | dumpPipe.readFd().reset(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 730 | break; |
| 731 | } |
Kweku Adams | a8a56c8 | 2018-04-23 06:15:31 -0700 | [diff] [blame] | 732 | if (cStatus != NO_ERROR) { |
Kweku Adams | 5b763c1 | 2018-09-13 15:44:58 -0700 | [diff] [blame] | 733 | 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] | 734 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 735 | |
| 736 | auto dump = std::make_unique<char[]>(buffer.size()); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 737 | sp<ProtoReader> reader = buffer.data()->read(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 738 | int i = 0; |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 739 | while (reader->hasNext()) { |
| 740 | dump[i] = reader->next(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 741 | i++; |
| 742 | } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 743 | uint64_t token = proto.start(android::os::BackTraceProto::TRACES); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 744 | proto.write(android::os::BackTraceProto::Stack::PID, pid); |
| 745 | proto.write(android::os::BackTraceProto::Stack::DUMP, dump.get(), i); |
| 746 | proto.write(android::os::BackTraceProto::Stack::DUMP_DURATION_NS, |
| 747 | static_cast<long long>(Nanotime() - start)); |
| 748 | proto.end(token); |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 749 | dumpPipe.readFd().reset(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 750 | } |
| 751 | |
Kweku Adams | 5b763c1 | 2018-09-13 15:44:58 -0700 | [diff] [blame] | 752 | if (!proto.flush(pipeWriteFd) && errno == EPIPE) { |
| 753 | ALOGE("[%s] wrote to a broken pipe\n", this->name.string()); |
| 754 | if (err != NO_ERROR) { |
| 755 | return EPIPE; |
| 756 | } |
| 757 | } |
| 758 | |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 759 | return err; |
| 760 | } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 761 | |
| 762 | } // namespace incidentd |
| 763 | } // namespace os |
Yi Jin | 98ce810 | 2018-04-12 11:15:39 -0700 | [diff] [blame] | 764 | } // namespace android |