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