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