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 | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 30 | #include <binder/IServiceManager.h> |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 31 | #include <debuggerd/client.h> |
| 32 | #include <dumputils/dump_utils.h> |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 33 | #include <log/log_event_list.h> |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 34 | #include <log/log_read.h> |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 35 | #include <log/logprint.h> |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 36 | #include <private/android_logger.h> |
| 37 | |
| 38 | #include "FdBuffer.h" |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 39 | #include "Privacy.h" |
| 40 | #include "PrivacyBuffer.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 | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 46 | using namespace android::base; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 47 | using namespace android::util; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 48 | using namespace std; |
| 49 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 50 | // special section ids |
| 51 | const int FIELD_ID_INCIDENT_HEADER = 1; |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 52 | const int FIELD_ID_INCIDENT_METADATA = 2; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 53 | |
| 54 | // incident section parameters |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 55 | const char INCIDENT_HELPER[] = "/system/bin/incident_helper"; |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame^] | 56 | const char* GZIP[] = {"/system/bin/gzip", NULL}; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 57 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 58 | 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] | 59 | const char* ihArgs[]{INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL}; |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame^] | 60 | return fork_execute_cmd(const_cast<char**>(ihArgs), p2cPipe, c2pPipe); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 63 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 64 | static status_t write_section_header(int fd, int sectionId, size_t size) { |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 65 | uint8_t buf[20]; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 66 | uint8_t* p = write_length_delimited_tag_header(buf, sectionId, size); |
| 67 | return WriteFully(fd, buf, p - buf) ? NO_ERROR : -errno; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 70 | // Reads data from FdBuffer and writes it to the requests file descriptor. |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 71 | static status_t write_report_requests(const int id, const FdBuffer& buffer, |
| 72 | ReportRequestSet* requests) { |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 73 | status_t err = -EBADF; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 74 | EncodedBuffer::iterator data = buffer.data(); |
| 75 | PrivacyBuffer privacyBuffer(get_privacy_of_section(id), data); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 76 | int writeable = 0; |
Yi Jin | 86dce41 | 2018-03-07 11:36:57 -0800 | [diff] [blame] | 77 | IncidentMetadata::SectionStats* stats = requests->sectionStats(id); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 78 | |
| 79 | stats->set_dump_size_bytes(data.size()); |
| 80 | stats->set_dump_duration_ms(buffer.durationMs()); |
| 81 | stats->set_timed_out(buffer.timedOut()); |
| 82 | stats->set_is_truncated(buffer.truncated()); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 83 | |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 84 | // The streaming ones, group requests by spec in order to save unnecessary strip operations |
| 85 | map<PrivacySpec, vector<sp<ReportRequest>>> requestsBySpec; |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 86 | for (auto it = requests->begin(); it != requests->end(); it++) { |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 87 | sp<ReportRequest> request = *it; |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 88 | if (!request->ok() || !request->args.containsSection(id)) { |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 89 | continue; // skip invalid request |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 90 | } |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 91 | PrivacySpec spec = PrivacySpec::new_spec(request->args.dest()); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 92 | requestsBySpec[spec].push_back(request); |
| 93 | } |
| 94 | |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 95 | for (auto mit = requestsBySpec.begin(); mit != requestsBySpec.end(); mit++) { |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 96 | PrivacySpec spec = mit->first; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 97 | err = privacyBuffer.strip(spec); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 98 | if (err != NO_ERROR) return err; // it means the privacyBuffer data is corrupted. |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 99 | if (privacyBuffer.size() == 0) continue; |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 100 | |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 101 | for (auto it = mit->second.begin(); it != mit->second.end(); it++) { |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 102 | sp<ReportRequest> request = *it; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 103 | err = write_section_header(request->fd, id, privacyBuffer.size()); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 104 | if (err != NO_ERROR) { |
| 105 | request->err = err; |
| 106 | continue; |
| 107 | } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 108 | err = privacyBuffer.flush(request->fd); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 109 | if (err != NO_ERROR) { |
| 110 | request->err = err; |
| 111 | continue; |
| 112 | } |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 113 | writeable++; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 114 | VLOG("Section %d flushed %zu bytes to fd %d with spec %d", id, privacyBuffer.size(), |
| 115 | request->fd, spec.dest); |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 116 | } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 117 | privacyBuffer.clear(); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // The dropbox file |
| 121 | if (requests->mainFd() >= 0) { |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 122 | PrivacySpec spec = PrivacySpec::new_spec(requests->mainDest()); |
Yi Jin | 3ec5cc7 | 2018-01-26 13:42:43 -0800 | [diff] [blame] | 123 | err = privacyBuffer.strip(spec); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 124 | if (err != NO_ERROR) return err; // the buffer data is corrupted. |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 125 | if (privacyBuffer.size() == 0) goto DONE; |
Yi Jin | 0f04716 | 2017-09-05 13:44:22 -0700 | [diff] [blame] | 126 | |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 127 | err = write_section_header(requests->mainFd(), id, privacyBuffer.size()); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 128 | if (err != NO_ERROR) { |
| 129 | requests->setMainFd(-1); |
| 130 | goto DONE; |
| 131 | } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 132 | err = privacyBuffer.flush(requests->mainFd()); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 133 | if (err != NO_ERROR) { |
| 134 | requests->setMainFd(-1); |
| 135 | goto DONE; |
| 136 | } |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 137 | writeable++; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 138 | VLOG("Section %d flushed %zu bytes to dropbox %d with spec %d", id, privacyBuffer.size(), |
| 139 | requests->mainFd(), spec.dest); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 140 | stats->set_report_size_bytes(privacyBuffer.size()); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 141 | } |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 142 | |
| 143 | DONE: |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 144 | // only returns error if there is no fd to write to. |
| 145 | return writeable > 0 ? NO_ERROR : err; |
| 146 | } |
| 147 | |
| 148 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 149 | Section::Section(int i, const int64_t timeoutMs) : id(i), timeoutMs(timeoutMs) {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 150 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 151 | Section::~Section() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 152 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 153 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 154 | HeaderSection::HeaderSection() : Section(FIELD_ID_INCIDENT_HEADER, 0) {} |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 155 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 156 | HeaderSection::~HeaderSection() {} |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 157 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 158 | status_t HeaderSection::Execute(ReportRequestSet* requests) const { |
| 159 | for (ReportRequestSet::iterator it = requests->begin(); it != requests->end(); it++) { |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 160 | const sp<ReportRequest> request = *it; |
Yi Jin | bdf5894 | 2017-11-14 17:58:19 -0800 | [diff] [blame] | 161 | const vector<vector<uint8_t>>& headers = request->args.headers(); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 162 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 163 | for (vector<vector<uint8_t>>::const_iterator buf = headers.begin(); buf != headers.end(); |
| 164 | buf++) { |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 165 | if (buf->empty()) continue; |
| 166 | |
| 167 | // So the idea is only requests with negative fd are written to dropbox file. |
| 168 | int fd = request->fd >= 0 ? request->fd : requests->mainFd(); |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 169 | write_section_header(fd, id, buf->size()); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 170 | WriteFully(fd, (uint8_t const*)buf->data(), buf->size()); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 171 | // If there was an error now, there will be an error later and we will remove |
| 172 | // it from the list then. |
| 173 | } |
| 174 | } |
| 175 | return NO_ERROR; |
| 176 | } |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 177 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 178 | MetadataSection::MetadataSection() : Section(FIELD_ID_INCIDENT_METADATA, 0) {} |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 179 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 180 | MetadataSection::~MetadataSection() {} |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 181 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 182 | status_t MetadataSection::Execute(ReportRequestSet* requests) const { |
Yi Jin | 86dce41 | 2018-03-07 11:36:57 -0800 | [diff] [blame] | 183 | ProtoOutputStream proto; |
| 184 | IncidentMetadata metadata = requests->metadata(); |
| 185 | proto.write(FIELD_TYPE_ENUM | IncidentMetadata::kDestFieldNumber, metadata.dest()); |
| 186 | proto.write(FIELD_TYPE_INT32 | IncidentMetadata::kRequestSizeFieldNumber, |
| 187 | metadata.request_size()); |
| 188 | proto.write(FIELD_TYPE_BOOL | IncidentMetadata::kUseDropboxFieldNumber, metadata.use_dropbox()); |
| 189 | for (auto iter = requests->allSectionStats().begin(); iter != requests->allSectionStats().end(); |
| 190 | iter++) { |
| 191 | IncidentMetadata::SectionStats stats = iter->second; |
| 192 | uint64_t token = proto.start(FIELD_TYPE_MESSAGE | IncidentMetadata::kSectionsFieldNumber); |
| 193 | proto.write(FIELD_TYPE_INT32 | IncidentMetadata::SectionStats::kIdFieldNumber, stats.id()); |
| 194 | proto.write(FIELD_TYPE_BOOL | IncidentMetadata::SectionStats::kSuccessFieldNumber, |
| 195 | stats.success()); |
| 196 | proto.write(FIELD_TYPE_INT32 | IncidentMetadata::SectionStats::kReportSizeBytesFieldNumber, |
| 197 | stats.report_size_bytes()); |
| 198 | proto.write(FIELD_TYPE_INT64 | IncidentMetadata::SectionStats::kExecDurationMsFieldNumber, |
| 199 | stats.exec_duration_ms()); |
| 200 | proto.write(FIELD_TYPE_INT32 | IncidentMetadata::SectionStats::kDumpSizeBytesFieldNumber, |
| 201 | stats.dump_size_bytes()); |
| 202 | proto.write(FIELD_TYPE_INT64 | IncidentMetadata::SectionStats::kDumpDurationMsFieldNumber, |
| 203 | stats.dump_duration_ms()); |
| 204 | proto.write(FIELD_TYPE_BOOL | IncidentMetadata::SectionStats::kTimedOutFieldNumber, |
| 205 | stats.timed_out()); |
| 206 | proto.write(FIELD_TYPE_BOOL | IncidentMetadata::SectionStats::kIsTruncatedFieldNumber, |
| 207 | stats.is_truncated()); |
| 208 | proto.end(token); |
| 209 | } |
| 210 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 211 | for (ReportRequestSet::iterator it = requests->begin(); it != requests->end(); it++) { |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 212 | const sp<ReportRequest> request = *it; |
Yi Jin | 86dce41 | 2018-03-07 11:36:57 -0800 | [diff] [blame] | 213 | if (request->fd < 0 || request->err != NO_ERROR) { |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 214 | continue; |
| 215 | } |
Yi Jin | 86dce41 | 2018-03-07 11:36:57 -0800 | [diff] [blame] | 216 | write_section_header(request->fd, id, proto.size()); |
| 217 | if (!proto.flush(request->fd)) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 218 | ALOGW("Failed to write metadata to fd %d", request->fd); |
| 219 | // we don't fail if we can't write to a single request's fd. |
| 220 | } |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 221 | } |
Yi Jin | 86dce41 | 2018-03-07 11:36:57 -0800 | [diff] [blame] | 222 | if (requests->mainFd() >= 0) { |
| 223 | write_section_header(requests->mainFd(), id, proto.size()); |
| 224 | if (!proto.flush(requests->mainFd())) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 225 | ALOGW("Failed to write metadata to dropbox fd %d", requests->mainFd()); |
| 226 | return -1; |
| 227 | } |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 228 | } |
| 229 | return NO_ERROR; |
| 230 | } |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 231 | // ================================================================================ |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 232 | static inline bool isSysfs(const char* filename) { return strncmp(filename, "/sys/", 5) == 0; } |
| 233 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 234 | FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs) |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 235 | : Section(id, timeoutMs), mFilename(filename) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 236 | name = filename; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 237 | mIsSysfs = isSysfs(filename); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | FileSection::~FileSection() {} |
| 241 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 242 | status_t FileSection::Execute(ReportRequestSet* requests) const { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 243 | // read from mFilename first, make sure the file is available |
| 244 | // 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] | 245 | unique_fd fd(open(mFilename, O_RDONLY | O_CLOEXEC)); |
| 246 | if (fd.get() == -1) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 247 | ALOGW("FileSection '%s' failed to open file", this->name.string()); |
| 248 | return -errno; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 251 | FdBuffer buffer; |
| 252 | Fpipe p2cPipe; |
| 253 | Fpipe c2pPipe; |
| 254 | // initiate pipes to pass data to/from incident_helper |
| 255 | if (!p2cPipe.init() || !c2pPipe.init()) { |
| 256 | ALOGW("FileSection '%s' failed to setup pipes", this->name.string()); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 257 | return -errno; |
| 258 | } |
| 259 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 260 | pid_t pid = fork_execute_incident_helper(this->id, &p2cPipe, &c2pPipe); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 261 | if (pid == -1) { |
| 262 | ALOGW("FileSection '%s' failed to fork", this->name.string()); |
| 263 | return -errno; |
| 264 | } |
| 265 | |
| 266 | // parent process |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 267 | status_t readStatus = buffer.readProcessedDataInStream( |
| 268 | &fd, &p2cPipe.writeFd(), &c2pPipe.readFd(), this->timeoutMs, mIsSysfs); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 269 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 270 | if (readStatus != NO_ERROR || buffer.timedOut()) { |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 271 | ALOGW("FileSection '%s' failed to read data from incident helper: %s, timedout: %s", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 272 | this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false"); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 273 | kill_child(pid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 274 | return readStatus; |
| 275 | } |
| 276 | |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 277 | status_t ihStatus = wait_child(pid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 278 | if (ihStatus != NO_ERROR) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 279 | ALOGW("FileSection '%s' abnormal child process: %s", this->name.string(), |
| 280 | strerror(-ihStatus)); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 281 | return ihStatus; |
| 282 | } |
| 283 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 284 | VLOG("FileSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(), |
| 285 | (int)buffer.durationMs()); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 286 | status_t err = write_report_requests(this->id, buffer, requests); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 287 | if (err != NO_ERROR) { |
| 288 | ALOGW("FileSection '%s' failed writing: %s", this->name.string(), strerror(-err)); |
| 289 | return err; |
| 290 | } |
| 291 | |
| 292 | return NO_ERROR; |
| 293 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 294 | // ================================================================================ |
| 295 | GZipSection::GZipSection(int id, const char* filename, ...) : Section(id) { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 296 | va_list args; |
| 297 | va_start(args, filename); |
| 298 | mFilenames = varargs(filename, args); |
| 299 | va_end(args); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame^] | 300 | name = "gzip"; |
| 301 | for (int i = 0; mFilenames[i] != NULL; i++) { |
| 302 | name += " "; |
| 303 | name += mFilenames[i]; |
| 304 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 305 | } |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 306 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 307 | GZipSection::~GZipSection() {} |
| 308 | |
| 309 | status_t GZipSection::Execute(ReportRequestSet* requests) const { |
| 310 | // Reads the files in order, use the first available one. |
| 311 | int index = 0; |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 312 | unique_fd fd; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 313 | while (mFilenames[index] != NULL) { |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 314 | fd.reset(open(mFilenames[index], O_RDONLY | O_CLOEXEC)); |
| 315 | if (fd.get() != -1) { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 316 | break; |
| 317 | } |
| 318 | ALOGW("GZipSection failed to open file %s", mFilenames[index]); |
| 319 | index++; // look at the next file. |
| 320 | } |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 321 | VLOG("GZipSection is using file %s, fd=%d", mFilenames[index], fd.get()); |
| 322 | if (fd.get() == -1) return -1; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 323 | |
| 324 | FdBuffer buffer; |
| 325 | Fpipe p2cPipe; |
| 326 | Fpipe c2pPipe; |
| 327 | // initiate pipes to pass data to/from gzip |
| 328 | if (!p2cPipe.init() || !c2pPipe.init()) { |
| 329 | ALOGW("GZipSection '%s' failed to setup pipes", this->name.string()); |
| 330 | return -errno; |
| 331 | } |
| 332 | |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame^] | 333 | pid_t pid = fork_execute_cmd((char* const*)GZIP, &p2cPipe, &c2pPipe); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 334 | if (pid == -1) { |
| 335 | ALOGW("GZipSection '%s' failed to fork", this->name.string()); |
| 336 | return -errno; |
| 337 | } |
| 338 | // parent process |
| 339 | |
| 340 | // construct Fdbuffer to output GZippedfileProto, the reason to do this instead of using |
| 341 | // ProtoOutputStream is to avoid allocation of another buffer inside ProtoOutputStream. |
| 342 | EncodedBuffer* internalBuffer = buffer.getInternalBuffer(); |
| 343 | internalBuffer->writeHeader((uint32_t)GZippedFileProto::FILENAME, WIRE_TYPE_LENGTH_DELIMITED); |
| 344 | String8 usedFile(mFilenames[index]); |
| 345 | internalBuffer->writeRawVarint32(usedFile.size()); |
| 346 | for (size_t i = 0; i < usedFile.size(); i++) { |
| 347 | internalBuffer->writeRawByte(mFilenames[index][i]); |
| 348 | } |
| 349 | internalBuffer->writeHeader((uint32_t)GZippedFileProto::GZIPPED_DATA, |
| 350 | WIRE_TYPE_LENGTH_DELIMITED); |
| 351 | size_t editPos = internalBuffer->wp()->pos(); |
| 352 | internalBuffer->wp()->move(8); // reserve 8 bytes for the varint of the data size. |
| 353 | size_t dataBeginAt = internalBuffer->wp()->pos(); |
| 354 | VLOG("GZipSection '%s' editPos=%zd, dataBeginAt=%zd", this->name.string(), editPos, |
| 355 | dataBeginAt); |
| 356 | |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 357 | status_t readStatus = |
| 358 | buffer.readProcessedDataInStream(&fd, &p2cPipe.writeFd(), &c2pPipe.readFd(), |
| 359 | this->timeoutMs, isSysfs(mFilenames[index])); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 360 | |
| 361 | if (readStatus != NO_ERROR || buffer.timedOut()) { |
| 362 | ALOGW("GZipSection '%s' failed to read data from gzip: %s, timedout: %s", |
| 363 | this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false"); |
| 364 | kill_child(pid); |
| 365 | return readStatus; |
| 366 | } |
| 367 | |
| 368 | status_t gzipStatus = wait_child(pid); |
| 369 | if (gzipStatus != NO_ERROR) { |
| 370 | ALOGW("GZipSection '%s' abnormal child process: %s", this->name.string(), |
| 371 | strerror(-gzipStatus)); |
| 372 | return gzipStatus; |
| 373 | } |
| 374 | // Revisit the actual size from gzip result and edit the internal buffer accordingly. |
| 375 | size_t dataSize = buffer.size() - dataBeginAt; |
| 376 | internalBuffer->wp()->rewind()->move(editPos); |
| 377 | internalBuffer->writeRawVarint32(dataSize); |
| 378 | internalBuffer->copy(dataBeginAt, dataSize); |
| 379 | VLOG("GZipSection '%s' wrote %zd bytes in %d ms, dataSize=%zd", this->name.string(), |
| 380 | buffer.size(), (int)buffer.durationMs(), dataSize); |
| 381 | status_t err = write_report_requests(this->id, buffer, requests); |
| 382 | if (err != NO_ERROR) { |
| 383 | ALOGW("GZipSection '%s' failed writing: %s", this->name.string(), strerror(-err)); |
| 384 | return err; |
| 385 | } |
| 386 | |
| 387 | return NO_ERROR; |
| 388 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 389 | |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 390 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 391 | struct WorkerThreadData : public virtual RefBase { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 392 | const WorkerThreadSection* section; |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 393 | Fpipe pipe; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 394 | |
| 395 | // Lock protects these fields |
| 396 | mutex lock; |
| 397 | bool workerDone; |
| 398 | status_t workerError; |
| 399 | |
| 400 | WorkerThreadData(const WorkerThreadSection* section); |
| 401 | virtual ~WorkerThreadData(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 402 | }; |
| 403 | |
| 404 | WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec) |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 405 | : section(sec), workerDone(false), workerError(NO_ERROR) {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 406 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 407 | WorkerThreadData::~WorkerThreadData() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 408 | |
| 409 | // ================================================================================ |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 410 | WorkerThreadSection::WorkerThreadSection(int id, const int64_t timeoutMs) |
| 411 | : Section(id, timeoutMs) {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 412 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 413 | WorkerThreadSection::~WorkerThreadSection() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 414 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 415 | static void* worker_thread_func(void* cookie) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 416 | WorkerThreadData* data = (WorkerThreadData*)cookie; |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 417 | status_t err = data->section->BlockingCall(data->pipe.writeFd().get()); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 418 | |
| 419 | { |
| 420 | unique_lock<mutex> lock(data->lock); |
| 421 | data->workerDone = true; |
| 422 | data->workerError = err; |
| 423 | } |
| 424 | |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 425 | data->pipe.writeFd().reset(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 426 | data->decStrong(data->section); |
| 427 | // data might be gone now. don't use it after this point in this thread. |
| 428 | return NULL; |
| 429 | } |
| 430 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 431 | status_t WorkerThreadSection::Execute(ReportRequestSet* requests) const { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 432 | status_t err = NO_ERROR; |
| 433 | pthread_t thread; |
| 434 | pthread_attr_t attr; |
| 435 | bool timedOut = false; |
| 436 | FdBuffer buffer; |
| 437 | |
| 438 | // Data shared between this thread and the worker thread. |
| 439 | sp<WorkerThreadData> data = new WorkerThreadData(this); |
| 440 | |
| 441 | // Create the pipe |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 442 | if (!data->pipe.init()) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 443 | return -errno; |
| 444 | } |
| 445 | |
| 446 | // The worker thread needs a reference and we can't let the count go to zero |
| 447 | // if that thread is slow to start. |
| 448 | data->incStrong(this); |
| 449 | |
| 450 | // Create the thread |
| 451 | err = pthread_attr_init(&attr); |
| 452 | if (err != 0) { |
| 453 | return -err; |
| 454 | } |
| 455 | // TODO: Do we need to tweak thread priority? |
| 456 | err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 457 | if (err != 0) { |
| 458 | pthread_attr_destroy(&attr); |
| 459 | return -err; |
| 460 | } |
| 461 | err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get()); |
| 462 | if (err != 0) { |
| 463 | pthread_attr_destroy(&attr); |
| 464 | return -err; |
| 465 | } |
| 466 | pthread_attr_destroy(&attr); |
| 467 | |
| 468 | // Loop reading until either the timeout or the worker side is done (i.e. eof). |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 469 | err = buffer.read(&data->pipe.readFd(), this->timeoutMs); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 470 | if (err != NO_ERROR) { |
| 471 | // TODO: Log this error into the incident report. |
| 472 | ALOGW("WorkerThreadSection '%s' reader failed with error '%s'", this->name.string(), |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 473 | strerror(-err)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | // Done with the read fd. The worker thread closes the write one so |
| 477 | // we never race and get here first. |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 478 | data->pipe.readFd().reset(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 479 | |
| 480 | // If the worker side is finished, then return its error (which may overwrite |
| 481 | // our possible error -- but it's more interesting anyway). If not, then we timed out. |
| 482 | { |
| 483 | unique_lock<mutex> lock(data->lock); |
| 484 | if (!data->workerDone) { |
| 485 | // We timed out |
| 486 | timedOut = true; |
| 487 | } else { |
| 488 | if (data->workerError != NO_ERROR) { |
| 489 | err = data->workerError; |
| 490 | // TODO: Log this error into the incident report. |
| 491 | ALOGW("WorkerThreadSection '%s' worker failed with error '%s'", this->name.string(), |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 492 | strerror(-err)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | if (timedOut || buffer.timedOut()) { |
| 498 | ALOGW("WorkerThreadSection '%s' timed out", this->name.string()); |
| 499 | return NO_ERROR; |
| 500 | } |
| 501 | |
| 502 | if (buffer.truncated()) { |
| 503 | // TODO: Log this into the incident report. |
| 504 | } |
| 505 | |
| 506 | // TODO: There was an error with the command or buffering. Report that. For now |
| 507 | // just exit with a log messasge. |
| 508 | if (err != NO_ERROR) { |
| 509 | ALOGW("WorkerThreadSection '%s' failed with error '%s'", this->name.string(), |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 510 | strerror(-err)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 511 | return NO_ERROR; |
| 512 | } |
| 513 | |
| 514 | // Write the data that was collected |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 515 | VLOG("WorkerThreadSection '%s' wrote %zd bytes in %d ms", name.string(), buffer.size(), |
| 516 | (int)buffer.durationMs()); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 517 | err = write_report_requests(this->id, buffer, requests); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 518 | if (err != NO_ERROR) { |
| 519 | ALOGW("WorkerThreadSection '%s' failed writing: '%s'", this->name.string(), strerror(-err)); |
| 520 | return err; |
| 521 | } |
| 522 | |
| 523 | return NO_ERROR; |
| 524 | } |
| 525 | |
| 526 | // ================================================================================ |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 527 | CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...) |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 528 | : Section(id, timeoutMs) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 529 | va_list args; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 530 | va_start(args, command); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 531 | mCommand = varargs(command, args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 532 | va_end(args); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame^] | 533 | name = "cmd"; |
| 534 | for (int i = 0; mCommand[i] != NULL; i++) { |
| 535 | name += " "; |
| 536 | name += mCommand[i]; |
| 537 | } |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 538 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 539 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 540 | CommandSection::CommandSection(int id, const char* command, ...) : Section(id) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 541 | va_list args; |
| 542 | va_start(args, command); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 543 | mCommand = varargs(command, args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 544 | va_end(args); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame^] | 545 | name = "cmd"; |
| 546 | for (int i = 0; mCommand[i] != NULL; i++) { |
| 547 | name += " "; |
| 548 | name += mCommand[i]; |
| 549 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 550 | } |
| 551 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 552 | CommandSection::~CommandSection() { free(mCommand); } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 553 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 554 | status_t CommandSection::Execute(ReportRequestSet* requests) const { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 555 | FdBuffer buffer; |
| 556 | Fpipe cmdPipe; |
| 557 | Fpipe ihPipe; |
| 558 | |
| 559 | if (!cmdPipe.init() || !ihPipe.init()) { |
| 560 | ALOGW("CommandSection '%s' failed to setup pipes", this->name.string()); |
| 561 | return -errno; |
| 562 | } |
| 563 | |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame^] | 564 | pid_t cmdPid = fork_execute_cmd((char* const*)mCommand, NULL, &cmdPipe); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 565 | if (cmdPid == -1) { |
| 566 | ALOGW("CommandSection '%s' failed to fork", this->name.string()); |
| 567 | return -errno; |
| 568 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 569 | pid_t ihPid = fork_execute_incident_helper(this->id, &cmdPipe, &ihPipe); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 570 | if (ihPid == -1) { |
| 571 | ALOGW("CommandSection '%s' failed to fork", this->name.string()); |
| 572 | return -errno; |
| 573 | } |
| 574 | |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 575 | cmdPipe.writeFd().reset(); |
| 576 | status_t readStatus = buffer.read(&ihPipe.readFd(), this->timeoutMs); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 577 | if (readStatus != NO_ERROR || buffer.timedOut()) { |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 578 | ALOGW("CommandSection '%s' failed to read data from incident helper: %s, timedout: %s", |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 579 | this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false"); |
Yi Jin | 4bab3a1 | 2018-01-10 16:50:59 -0800 | [diff] [blame] | 580 | kill_child(cmdPid); |
| 581 | kill_child(ihPid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 582 | return readStatus; |
| 583 | } |
| 584 | |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 585 | // 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] | 586 | // 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] | 587 | status_t cmdStatus = wait_child(cmdPid); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 588 | status_t ihStatus = wait_child(ihPid); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 589 | if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 590 | ALOGW("CommandSection '%s' abnormal child processes, return status: command: %s, incident " |
| 591 | "helper: %s", |
| 592 | this->name.string(), strerror(-cmdStatus), strerror(-ihStatus)); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 593 | return cmdStatus != NO_ERROR ? cmdStatus : ihStatus; |
| 594 | } |
| 595 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 596 | VLOG("CommandSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(), |
| 597 | (int)buffer.durationMs()); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 598 | status_t err = write_report_requests(this->id, buffer, requests); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 599 | if (err != NO_ERROR) { |
| 600 | ALOGW("CommandSection '%s' failed writing: %s", this->name.string(), strerror(-err)); |
| 601 | return err; |
| 602 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 603 | return NO_ERROR; |
| 604 | } |
| 605 | |
| 606 | // ================================================================================ |
| 607 | DumpsysSection::DumpsysSection(int id, const char* service, ...) |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 608 | : WorkerThreadSection(id), mService(service) { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 609 | name = "dumpsys "; |
| 610 | name += service; |
| 611 | |
| 612 | va_list args; |
| 613 | va_start(args, service); |
| 614 | while (true) { |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 615 | const char* arg = va_arg(args, const char*); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 616 | if (arg == NULL) { |
| 617 | break; |
| 618 | } |
| 619 | mArgs.add(String16(arg)); |
| 620 | name += " "; |
| 621 | name += arg; |
| 622 | } |
| 623 | va_end(args); |
| 624 | } |
| 625 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 626 | DumpsysSection::~DumpsysSection() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 627 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 628 | status_t DumpsysSection::BlockingCall(int pipeWriteFd) const { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 629 | // checkService won't wait for the service to show up like getService will. |
| 630 | sp<IBinder> service = defaultServiceManager()->checkService(mService); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 631 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 632 | if (service == NULL) { |
| 633 | // Returning an error interrupts the entire incident report, so just |
| 634 | // log the failure. |
| 635 | // TODO: have a meta record inside the report that would log this |
| 636 | // failure inside the report, because the fact that we can't find |
| 637 | // the service is good data in and of itself. This is running in |
| 638 | // another thread so lock that carefully... |
| 639 | ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string()); |
| 640 | return NO_ERROR; |
| 641 | } |
| 642 | |
| 643 | service->dump(pipeWriteFd, mArgs); |
| 644 | |
| 645 | return NO_ERROR; |
| 646 | } |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 647 | |
| 648 | // ================================================================================ |
| 649 | // initialization only once in Section.cpp. |
| 650 | map<log_id_t, log_time> LogSection::gLastLogsRetrieved; |
| 651 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 652 | LogSection::LogSection(int id, log_id_t logID) : WorkerThreadSection(id), mLogID(logID) { |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 653 | name += "logcat "; |
| 654 | name += android_log_id_to_name(logID); |
| 655 | switch (logID) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 656 | case LOG_ID_EVENTS: |
| 657 | case LOG_ID_STATS: |
| 658 | case LOG_ID_SECURITY: |
| 659 | mBinary = true; |
| 660 | break; |
| 661 | default: |
| 662 | mBinary = false; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 666 | LogSection::~LogSection() {} |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 667 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 668 | static size_t trimTail(char const* buf, size_t len) { |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 669 | while (len > 0) { |
| 670 | char c = buf[len - 1]; |
| 671 | if (c == '\0' || c == ' ' || c == '\n' || c == '\r' || c == ':') { |
| 672 | len--; |
| 673 | } else { |
| 674 | break; |
| 675 | } |
| 676 | } |
| 677 | return len; |
| 678 | } |
| 679 | |
| 680 | static inline int32_t get4LE(uint8_t const* src) { |
| 681 | return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); |
| 682 | } |
| 683 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 684 | status_t LogSection::BlockingCall(int pipeWriteFd) const { |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 685 | // Open log buffer and getting logs since last retrieved time if any. |
| 686 | unique_ptr<logger_list, void (*)(logger_list*)> loggers( |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 687 | gLastLogsRetrieved.find(mLogID) == gLastLogsRetrieved.end() |
| 688 | ? android_logger_list_alloc(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, 0) |
| 689 | : android_logger_list_alloc_time(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, |
| 690 | gLastLogsRetrieved[mLogID], 0), |
| 691 | android_logger_list_free); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 692 | |
| 693 | if (android_logger_open(loggers.get(), mLogID) == NULL) { |
Yi Jin | 83fb1d5 | 2018-03-16 12:03:53 -0700 | [diff] [blame] | 694 | ALOGE("LogSection %s: Can't get logger.", this->name.string()); |
| 695 | return -1; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | log_msg msg; |
| 699 | log_time lastTimestamp(0); |
| 700 | |
| 701 | ProtoOutputStream proto; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 702 | while (true) { // keeps reading until logd buffer is fully read. |
Yi Jin | 83fb1d5 | 2018-03-16 12:03:53 -0700 | [diff] [blame] | 703 | status_t err = android_logger_list_read(loggers.get(), &msg); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 704 | // err = 0 - no content, unexpected connection drop or EOF. |
| 705 | // err = +ive number - size of retrieved data from logger |
| 706 | // err = -ive number, OS supplied error _except_ for -EAGAIN |
| 707 | // err = -EAGAIN, graceful indication for ANDRODI_LOG_NONBLOCK that this is the end of data. |
| 708 | if (err <= 0) { |
| 709 | if (err != -EAGAIN) { |
Yi Jin | 83fb1d5 | 2018-03-16 12:03:53 -0700 | [diff] [blame] | 710 | ALOGW("LogSection %s: fails to read a log_msg.\n", this->name.string()); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 711 | } |
Yi Jin | 83fb1d5 | 2018-03-16 12:03:53 -0700 | [diff] [blame] | 712 | // dump previous logs and don't consider this error a failure. |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 713 | break; |
| 714 | } |
| 715 | if (mBinary) { |
| 716 | // remove the first uint32 which is tag's index in event log tags |
| 717 | 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] | 718 | msg.len() - sizeof(uint32_t)); |
| 719 | ; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 720 | android_log_list_element elem; |
| 721 | |
| 722 | lastTimestamp.tv_sec = msg.entry_v1.sec; |
| 723 | lastTimestamp.tv_nsec = msg.entry_v1.nsec; |
| 724 | |
| 725 | // format a BinaryLogEntry |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 726 | uint64_t token = proto.start(LogProto::BINARY_LOGS); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 727 | proto.write(BinaryLogEntry::SEC, msg.entry_v1.sec); |
| 728 | proto.write(BinaryLogEntry::NANOSEC, msg.entry_v1.nsec); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 729 | proto.write(BinaryLogEntry::UID, (int)msg.entry_v4.uid); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 730 | proto.write(BinaryLogEntry::PID, msg.entry_v1.pid); |
| 731 | proto.write(BinaryLogEntry::TID, msg.entry_v1.tid); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 732 | proto.write(BinaryLogEntry::TAG_INDEX, |
| 733 | get4LE(reinterpret_cast<uint8_t const*>(msg.msg()))); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 734 | do { |
| 735 | elem = android_log_read_next(context); |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 736 | uint64_t elemToken = proto.start(BinaryLogEntry::ELEMS); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 737 | switch (elem.type) { |
| 738 | case EVENT_TYPE_INT: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 739 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 740 | BinaryLogEntry::Elem::EVENT_TYPE_INT); |
| 741 | proto.write(BinaryLogEntry::Elem::VAL_INT32, (int)elem.data.int32); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 742 | break; |
| 743 | case EVENT_TYPE_LONG: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 744 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 745 | BinaryLogEntry::Elem::EVENT_TYPE_LONG); |
| 746 | proto.write(BinaryLogEntry::Elem::VAL_INT64, (long long)elem.data.int64); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 747 | break; |
| 748 | case EVENT_TYPE_STRING: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 749 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 750 | BinaryLogEntry::Elem::EVENT_TYPE_STRING); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 751 | proto.write(BinaryLogEntry::Elem::VAL_STRING, elem.data.string, elem.len); |
| 752 | break; |
| 753 | case EVENT_TYPE_FLOAT: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 754 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 755 | BinaryLogEntry::Elem::EVENT_TYPE_FLOAT); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 756 | proto.write(BinaryLogEntry::Elem::VAL_FLOAT, elem.data.float32); |
| 757 | break; |
| 758 | case EVENT_TYPE_LIST: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 759 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 760 | BinaryLogEntry::Elem::EVENT_TYPE_LIST); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 761 | break; |
| 762 | case EVENT_TYPE_LIST_STOP: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 763 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 764 | BinaryLogEntry::Elem::EVENT_TYPE_LIST_STOP); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 765 | break; |
| 766 | case EVENT_TYPE_UNKNOWN: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 767 | proto.write(BinaryLogEntry::Elem::TYPE, |
| 768 | BinaryLogEntry::Elem::EVENT_TYPE_UNKNOWN); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 769 | break; |
| 770 | } |
| 771 | proto.end(elemToken); |
| 772 | } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete); |
| 773 | proto.end(token); |
| 774 | if (context) { |
| 775 | android_log_destroy(&context); |
| 776 | } |
| 777 | } else { |
| 778 | AndroidLogEntry entry; |
| 779 | err = android_log_processLogBuffer(&msg.entry_v1, &entry); |
| 780 | if (err != NO_ERROR) { |
Yi Jin | 83fb1d5 | 2018-03-16 12:03:53 -0700 | [diff] [blame] | 781 | ALOGW("LogSection %s: fails to process to an entry.\n", this->name.string()); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 782 | break; |
| 783 | } |
| 784 | lastTimestamp.tv_sec = entry.tv_sec; |
| 785 | lastTimestamp.tv_nsec = entry.tv_nsec; |
| 786 | |
| 787 | // format a TextLogEntry |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 788 | uint64_t token = proto.start(LogProto::TEXT_LOGS); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 789 | proto.write(TextLogEntry::SEC, (long long)entry.tv_sec); |
| 790 | proto.write(TextLogEntry::NANOSEC, (long long)entry.tv_nsec); |
| 791 | proto.write(TextLogEntry::PRIORITY, (int)entry.priority); |
| 792 | proto.write(TextLogEntry::UID, entry.uid); |
| 793 | proto.write(TextLogEntry::PID, entry.pid); |
| 794 | proto.write(TextLogEntry::TID, entry.tid); |
| 795 | proto.write(TextLogEntry::TAG, entry.tag, trimTail(entry.tag, entry.tagLen)); |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 796 | proto.write(TextLogEntry::LOG, entry.message, |
| 797 | trimTail(entry.message, entry.messageLen)); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 798 | proto.end(token); |
| 799 | } |
| 800 | } |
| 801 | gLastLogsRetrieved[mLogID] = lastTimestamp; |
| 802 | proto.flush(pipeWriteFd); |
Yi Jin | 83fb1d5 | 2018-03-16 12:03:53 -0700 | [diff] [blame] | 803 | return NO_ERROR; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 804 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 805 | |
| 806 | // ================================================================================ |
| 807 | |
| 808 | TombstoneSection::TombstoneSection(int id, const char* type, const int64_t timeoutMs) |
| 809 | : WorkerThreadSection(id, timeoutMs), mType(type) { |
| 810 | name += "tombstone "; |
| 811 | name += type; |
| 812 | } |
| 813 | |
| 814 | TombstoneSection::~TombstoneSection() {} |
| 815 | |
| 816 | status_t TombstoneSection::BlockingCall(int pipeWriteFd) const { |
| 817 | std::unique_ptr<DIR, decltype(&closedir)> proc(opendir("/proc"), closedir); |
| 818 | if (proc.get() == nullptr) { |
| 819 | ALOGE("opendir /proc failed: %s\n", strerror(errno)); |
| 820 | return -errno; |
| 821 | } |
| 822 | |
| 823 | const std::set<int> hal_pids = get_interesting_hal_pids(); |
| 824 | |
| 825 | ProtoOutputStream proto; |
| 826 | struct dirent* d; |
| 827 | status_t err = NO_ERROR; |
| 828 | while ((d = readdir(proc.get()))) { |
| 829 | int pid = atoi(d->d_name); |
| 830 | if (pid <= 0) { |
| 831 | continue; |
| 832 | } |
| 833 | |
| 834 | const std::string link_name = android::base::StringPrintf("/proc/%d/exe", pid); |
| 835 | std::string exe; |
| 836 | if (!android::base::Readlink(link_name, &exe)) { |
| 837 | ALOGE("Can't read '%s': %s\n", link_name.c_str(), strerror(errno)); |
| 838 | continue; |
| 839 | } |
| 840 | |
| 841 | bool is_java_process; |
| 842 | if (exe == "/system/bin/app_process32" || exe == "/system/bin/app_process64") { |
| 843 | if (mType != "java") continue; |
| 844 | // Don't bother dumping backtraces for the zygote. |
| 845 | if (IsZygote(pid)) { |
| 846 | VLOG("Skipping Zygote"); |
| 847 | continue; |
| 848 | } |
| 849 | |
| 850 | is_java_process = true; |
| 851 | } else if (should_dump_native_traces(exe.c_str())) { |
| 852 | if (mType != "native") continue; |
| 853 | is_java_process = false; |
| 854 | } else if (hal_pids.find(pid) != hal_pids.end()) { |
| 855 | if (mType != "hal") continue; |
| 856 | is_java_process = false; |
| 857 | } else { |
| 858 | // Probably a native process we don't care about, continue. |
| 859 | VLOG("Skipping %d", pid); |
| 860 | continue; |
| 861 | } |
| 862 | |
| 863 | Fpipe dumpPipe; |
| 864 | if (!dumpPipe.init()) { |
| 865 | ALOGW("TombstoneSection '%s' failed to setup dump pipe", this->name.string()); |
| 866 | err = -errno; |
| 867 | break; |
| 868 | } |
| 869 | |
| 870 | const uint64_t start = Nanotime(); |
| 871 | pid_t child = fork(); |
| 872 | if (child < 0) { |
| 873 | ALOGE("Failed to fork child process"); |
| 874 | break; |
| 875 | } else if (child == 0) { |
| 876 | // This is the child process. |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 877 | dumpPipe.readFd().reset(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 878 | const int ret = dump_backtrace_to_file_timeout( |
| 879 | pid, is_java_process ? kDebuggerdJavaBacktrace : kDebuggerdNativeBacktrace, |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 880 | is_java_process ? 5 : 20, dumpPipe.writeFd().get()); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 881 | if (ret == -1) { |
| 882 | if (errno == 0) { |
| 883 | ALOGW("Dumping failed for pid '%d', likely due to a timeout\n", pid); |
| 884 | } else { |
| 885 | ALOGE("Dumping failed for pid '%d': %s\n", pid, strerror(errno)); |
| 886 | } |
| 887 | } |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 888 | dumpPipe.writeFd().reset(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 889 | _exit(EXIT_SUCCESS); |
| 890 | } |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 891 | dumpPipe.writeFd().reset(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 892 | // Parent process. |
| 893 | // Read from the pipe concurrently to avoid blocking the child. |
| 894 | FdBuffer buffer; |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 895 | err = buffer.readFully(&dumpPipe.readFd()); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 896 | if (err != NO_ERROR) { |
| 897 | ALOGW("TombstoneSection '%s' failed to read stack dump: %d", this->name.string(), err); |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 898 | dumpPipe.readFd().reset(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 899 | break; |
| 900 | } |
| 901 | |
| 902 | auto dump = std::make_unique<char[]>(buffer.size()); |
| 903 | auto iterator = buffer.data(); |
| 904 | int i = 0; |
| 905 | while (iterator.hasNext()) { |
| 906 | dump[i] = iterator.next(); |
| 907 | i++; |
| 908 | } |
| 909 | long long token = proto.start(android::os::BackTraceProto::TRACES); |
| 910 | proto.write(android::os::BackTraceProto::Stack::PID, pid); |
| 911 | proto.write(android::os::BackTraceProto::Stack::DUMP, dump.get(), i); |
| 912 | proto.write(android::os::BackTraceProto::Stack::DUMP_DURATION_NS, |
| 913 | static_cast<long long>(Nanotime() - start)); |
| 914 | proto.end(token); |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 915 | dumpPipe.readFd().reset(); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | proto.flush(pipeWriteFd); |
| 919 | return err; |
| 920 | } |