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 | /** |
| 115 | * Section that calls dumpsys on a system service. |
| 116 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 117 | class DumpsysSection : public WorkerThreadSection { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 118 | public: |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 119 | DumpsysSection(int id, const char* service, ...); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 120 | virtual ~DumpsysSection(); |
| 121 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame^] | 122 | virtual status_t BlockingCall(unique_fd& pipeWriteFd) const; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 123 | |
| 124 | private: |
| 125 | String16 mService; |
| 126 | Vector<String16> mArgs; |
| 127 | }; |
| 128 | |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 129 | /** |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 130 | * Section that calls dumpsys on a system service. |
| 131 | */ |
| 132 | class SystemPropertyDumpsysSection : public WorkerThreadSection { |
| 133 | public: |
| 134 | SystemPropertyDumpsysSection(int id, const char* service, ...); |
| 135 | virtual ~SystemPropertyDumpsysSection(); |
| 136 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame^] | 137 | virtual status_t BlockingCall(unique_fd& pipeWriteFd) const; |
Joe Onorato | fe7bbf4 | 2019-03-24 20:57:16 -0700 | [diff] [blame] | 138 | |
| 139 | private: |
| 140 | String16 mService; |
| 141 | Vector<String16> mArgs; |
| 142 | }; |
| 143 | |
| 144 | /** |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 145 | * Section that reads from logd. |
| 146 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 147 | class LogSection : public WorkerThreadSection { |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 148 | // global last log retrieved timestamp for each log_id_t. |
| 149 | static map<log_id_t, log_time> gLastLogsRetrieved; |
| 150 | |
zhouwenjie | c3bf804 | 2019-10-30 14:31:54 -0700 | [diff] [blame] | 151 | // log mode: read only & non blocking. |
| 152 | const static int logModeBase = ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK; |
| 153 | |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 154 | public: |
zhouwenjie | c3bf804 | 2019-10-30 14:31:54 -0700 | [diff] [blame] | 155 | LogSection(int id, const char* logID, ...); |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 156 | virtual ~LogSection(); |
| 157 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame^] | 158 | virtual status_t BlockingCall(unique_fd& pipeWriteFd) const; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 159 | |
| 160 | private: |
| 161 | log_id_t mLogID; |
| 162 | bool mBinary; |
zhouwenjie | c3bf804 | 2019-10-30 14:31:54 -0700 | [diff] [blame] | 163 | int mLogMode; |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 164 | }; |
| 165 | |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 166 | /** |
| 167 | * Section that gets data from tombstoned. |
| 168 | */ |
| 169 | class TombstoneSection : public WorkerThreadSection { |
| 170 | public: |
Kweku Adams | 0f71679 | 2018-09-13 15:48:27 -0700 | [diff] [blame] | 171 | TombstoneSection(int id, const char* type, int64_t timeoutMs = 120000 /* 2 minutes */); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 172 | virtual ~TombstoneSection(); |
| 173 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame^] | 174 | virtual status_t BlockingCall(unique_fd& pipeWriteFd) const; |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 175 | |
| 176 | private: |
| 177 | std::string mType; |
| 178 | }; |
| 179 | |
Mike Ma | 643de92 | 2019-12-17 10:56:17 -0800 | [diff] [blame^] | 180 | /** |
| 181 | * Section that gets data from a registered dump callback. |
| 182 | */ |
| 183 | class BringYourOwnSection : public WorkerThreadSection { |
| 184 | public: |
| 185 | const uid_t uid; |
| 186 | |
| 187 | BringYourOwnSection(int id, const char* customName, const uid_t callingUid, |
| 188 | const sp<IIncidentDumpCallback>& callback); |
| 189 | virtual ~BringYourOwnSection(); |
| 190 | |
| 191 | virtual status_t BlockingCall(unique_fd& pipeWriteFd) const; |
| 192 | |
| 193 | private: |
| 194 | const sp<IIncidentDumpCallback>& mCallback; |
| 195 | }; |
| 196 | |
Joe Onorato | 7a406b4 | 2019-04-12 18:11:30 -0700 | [diff] [blame] | 197 | |
| 198 | /** |
| 199 | * These sections will not be generated when doing an 'all' report, either |
| 200 | * for size, speed of collection, or privacy. |
| 201 | */ |
| 202 | bool section_requires_specific_mention(int sectionId); |
| 203 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 204 | } // namespace incidentd |
| 205 | } // namespace os |
| 206 | } // namespace android |
| 207 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 208 | #endif // SECTIONS_H |