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