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 | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 16 | #pragma once |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 17 | |
| 18 | #ifndef SECTIONS_H |
| 19 | #define SECTIONS_H |
| 20 | |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 21 | #include "Reporter.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 22 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 23 | #include <stdarg.h> |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 24 | #include <map> |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 25 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 26 | #include <android/os/IIncidentDumpCallback.h> |
| 27 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 28 | #include <utils/String16.h> |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 29 | #include <utils/String8.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 30 | #include <utils/Vector.h> |
| 31 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 32 | namespace android { |
| 33 | namespace os { |
| 34 | namespace incidentd { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 35 | |
Yi Jin | ad3e6e5 | 2018-04-03 15:10:34 -0700 | [diff] [blame] | 36 | const int64_t REMOTE_CALL_TIMEOUT_MS = 30 * 1000; // 30 seconds |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 37 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 38 | /** |
| 39 | * Base class for sections |
| 40 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 41 | class Section { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 42 | public: |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 43 | const int id; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 44 | const int64_t timeoutMs; // each section must have a timeout |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 45 | String8 name; |
| 46 | |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 47 | Section(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 48 | virtual ~Section(); |
| 49 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 50 | virtual status_t Execute(ReportWriter* writer) const = 0; |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | /** |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 54 | * Section that reads in a file. |
| 55 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 56 | class FileSection : public Section { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 57 | public: |
Kweku Adams | e04ef77 | 2018-06-13 12:24:38 -0700 | [diff] [blame] | 58 | FileSection(int id, const char* filename, |
Yi Jin | 3f36035 | 2018-04-16 16:13:04 -0700 | [diff] [blame] | 59 | int64_t timeoutMs = 5000 /* 5 seconds */); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 60 | virtual ~FileSection(); |
| 61 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 62 | virtual status_t Execute(ReportWriter* writer) const; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 63 | |
| 64 | private: |
| 65 | const char* mFilename; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 66 | bool mIsSysfs; // sysfs files are pollable but return POLLERR by default, handle it separately |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | /** |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 70 | * Section that reads in a file and gzips the content. |
| 71 | */ |
| 72 | class GZipSection : public Section { |
| 73 | public: |
| 74 | GZipSection(int id, const char* filename, ...); |
| 75 | virtual ~GZipSection(); |
| 76 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 77 | virtual status_t Execute(ReportWriter* writer) const; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | // It looks up the content from multiple files and stops when the first one is available. |
| 81 | const char** mFilenames; |
| 82 | }; |
| 83 | |
| 84 | /** |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 85 | * Base class for sections that call a command that might need a timeout. |
| 86 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 87 | class WorkerThreadSection : public Section { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 88 | public: |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 89 | WorkerThreadSection(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 90 | virtual ~WorkerThreadSection(); |
| 91 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 92 | virtual status_t Execute(ReportWriter* writer) const; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 93 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 94 | virtual status_t BlockingCall(unique_fd& pipeWriteFd) const = 0; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | /** |
| 98 | * Section that forks and execs a command, and puts stdout as the section. |
| 99 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 100 | class CommandSection : public Section { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 101 | public: |
Yi Jin | 3f36035 | 2018-04-16 16:13:04 -0700 | [diff] [blame] | 102 | CommandSection(int id, int64_t timeoutMs, const char* command, ...); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 103 | |
| 104 | CommandSection(int id, const char* command, ...); |
| 105 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 106 | virtual ~CommandSection(); |
| 107 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 108 | virtual status_t Execute(ReportWriter* writer) const; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 109 | |
| 110 | private: |
| 111 | const char** mCommand; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | /** |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 115 | * Section that calls protobuf dumpsys on a system service, usually |
| 116 | * "dumpsys [service_name] --proto". |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 117 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 118 | class DumpsysSection : public WorkerThreadSection { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 119 | public: |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 120 | DumpsysSection(int id, const char* service, ...); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 121 | virtual ~DumpsysSection(); |
| 122 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 123 | virtual status_t BlockingCall(unique_fd& pipeWriteFd) const; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 124 | |
| 125 | private: |
| 126 | String16 mService; |
| 127 | Vector<String16> mArgs; |
| 128 | }; |
| 129 | |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 130 | /** |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 131 | * Section that calls text dumpsys on a system service, usually "dumpsys [service_name]". |
| 132 | */ |
Mike Ma | 87003ad | 2020-03-19 12:31:29 -0700 | [diff] [blame] | 133 | class TextDumpsysSection : public Section { |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 134 | public: |
| 135 | TextDumpsysSection(int id, const char* service, ...); |
| 136 | virtual ~TextDumpsysSection(); |
| 137 | |
Mike Ma | 87003ad | 2020-03-19 12:31:29 -0700 | [diff] [blame] | 138 | virtual status_t Execute(ReportWriter* writer) const; |
Mike Ma | 5510f7c | 2020-02-19 02:56:04 -0800 | [diff] [blame] | 139 | |
| 140 | private: |
| 141 | String16 mService; |
| 142 | Vector<String16> mArgs; |
| 143 | }; |
| 144 | |
| 145 | /** |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 146 | * Section that calls dumpsys on a system service. |
| 147 | */ |
| 148 | class SystemPropertyDumpsysSection : public WorkerThreadSection { |
| 149 | public: |
| 150 | SystemPropertyDumpsysSection(int id, const char* service, ...); |
| 151 | virtual ~SystemPropertyDumpsysSection(); |
| 152 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 153 | virtual status_t BlockingCall(unique_fd& pipeWriteFd) const; |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 154 | |
| 155 | private: |
| 156 | String16 mService; |
| 157 | Vector<String16> mArgs; |
| 158 | }; |
| 159 | |
| 160 | /** |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 161 | * Section that reads from logd. |
| 162 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 163 | class LogSection : public WorkerThreadSection { |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 164 | // global last log retrieved timestamp for each log_id_t. |
| 165 | static map<log_id_t, log_time> gLastLogsRetrieved; |
| 166 | |
Tom Cherry | 01cd171 | 2020-03-23 13:41:46 -0700 | [diff] [blame^] | 167 | // log mode: non blocking. |
| 168 | const static int logModeBase = ANDROID_LOG_NONBLOCK; |
zhouwenjie | c3bf804 | 2019-10-30 14:31:54 -0700 | [diff] [blame] | 169 | |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 170 | public: |
zhouwenjie | c3bf804 | 2019-10-30 14:31:54 -0700 | [diff] [blame] | 171 | LogSection(int id, const char* logID, ...); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 172 | virtual ~LogSection(); |
| 173 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 174 | virtual status_t BlockingCall(unique_fd& pipeWriteFd) const; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 175 | |
| 176 | private: |
| 177 | log_id_t mLogID; |
| 178 | bool mBinary; |
zhouwenjie | c3bf804 | 2019-10-30 14:31:54 -0700 | [diff] [blame] | 179 | int mLogMode; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 180 | }; |
| 181 | |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 182 | /** |
| 183 | * Section that gets data from tombstoned. |
| 184 | */ |
| 185 | class TombstoneSection : public WorkerThreadSection { |
| 186 | public: |
Kweku Adams | 0f71679 | 2018-09-13 15:48:27 -0700 | [diff] [blame] | 187 | TombstoneSection(int id, const char* type, int64_t timeoutMs = 120000 /* 2 minutes */); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 188 | virtual ~TombstoneSection(); |
| 189 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 190 | virtual status_t BlockingCall(unique_fd& pipeWriteFd) const; |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 191 | |
| 192 | private: |
| 193 | std::string mType; |
| 194 | }; |
| 195 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame] | 196 | /** |
| 197 | * Section that gets data from a registered dump callback. |
| 198 | */ |
| 199 | class BringYourOwnSection : public WorkerThreadSection { |
| 200 | public: |
| 201 | const uid_t uid; |
| 202 | |
| 203 | BringYourOwnSection(int id, const char* customName, const uid_t callingUid, |
| 204 | const sp<IIncidentDumpCallback>& callback); |
| 205 | virtual ~BringYourOwnSection(); |
| 206 | |
| 207 | virtual status_t BlockingCall(unique_fd& pipeWriteFd) const; |
| 208 | |
| 209 | private: |
| 210 | const sp<IIncidentDumpCallback>& mCallback; |
| 211 | }; |
| 212 | |
Joe Onorato | 7a406b4 | 2019-04-12 18:11:30 -0700 | [diff] [blame] | 213 | |
| 214 | /** |
| 215 | * These sections will not be generated when doing an 'all' report, either |
| 216 | * for size, speed of collection, or privacy. |
| 217 | */ |
| 218 | bool section_requires_specific_mention(int sectionId); |
| 219 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 220 | } // namespace incidentd |
| 221 | } // namespace os |
| 222 | } // namespace android |
| 223 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 224 | #endif // SECTIONS_H |