blob: bf915c4165d5613d1246265478019c8d7e50a292 [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
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 Jinb592e3b2018-02-01 15:17:04 -080016#pragma once
Joe Onorato1754d742016-11-21 17:51:35 -080017
18#ifndef SECTIONS_H
19#define SECTIONS_H
20
Yi Jin99c248f2017-08-25 18:11:58 -070021#include "Reporter.h"
Joe Onorato1754d742016-11-21 17:51:35 -080022
Yi Jinb44f7d42017-07-21 12:12:59 -070023#include <stdarg.h>
Yi Jinb592e3b2018-02-01 15:17:04 -080024#include <map>
Yi Jin3c034c92017-12-22 17:36:47 -080025
Mike Ma643de922019-12-17 10:56:17 -080026#include <android/os/IIncidentDumpCallback.h>
27
Joe Onorato1754d742016-11-21 17:51:35 -080028#include <utils/String16.h>
Yi Jinb592e3b2018-02-01 15:17:04 -080029#include <utils/String8.h>
Joe Onorato1754d742016-11-21 17:51:35 -080030#include <utils/Vector.h>
31
Yi Jin6cacbcb2018-03-30 14:04:52 -070032namespace android {
33namespace os {
34namespace incidentd {
Joe Onorato1754d742016-11-21 17:51:35 -080035
Yi Jinad3e6e52018-04-03 15:10:34 -070036const int64_t REMOTE_CALL_TIMEOUT_MS = 30 * 1000; // 30 seconds
Yi Jinb44f7d42017-07-21 12:12:59 -070037
Joe Onorato1754d742016-11-21 17:51:35 -080038/**
39 * Base class for sections
40 */
Yi Jinb592e3b2018-02-01 15:17:04 -080041class Section {
Joe Onorato1754d742016-11-21 17:51:35 -080042public:
Yi Jinb44f7d42017-07-21 12:12:59 -070043 const int id;
Yi Jinb592e3b2018-02-01 15:17:04 -080044 const int64_t timeoutMs; // each section must have a timeout
Joe Onorato1754d742016-11-21 17:51:35 -080045 String8 name;
46
Joe Onoratofe7bbf42019-03-24 20:57:16 -070047 Section(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS);
Joe Onorato1754d742016-11-21 17:51:35 -080048 virtual ~Section();
49
Joe Onorato99598ee2019-02-11 15:55:13 +000050 virtual status_t Execute(ReportWriter* writer) const = 0;
Yi Jin329130b2018-02-09 16:47:47 -080051};
52
53/**
Joe Onorato1754d742016-11-21 17:51:35 -080054 * Section that reads in a file.
55 */
Yi Jinb592e3b2018-02-01 15:17:04 -080056class FileSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -080057public:
Kweku Adamse04ef772018-06-13 12:24:38 -070058 FileSection(int id, const char* filename,
Yi Jin3f360352018-04-16 16:13:04 -070059 int64_t timeoutMs = 5000 /* 5 seconds */);
Joe Onorato1754d742016-11-21 17:51:35 -080060 virtual ~FileSection();
61
Joe Onorato99598ee2019-02-11 15:55:13 +000062 virtual status_t Execute(ReportWriter* writer) const;
Joe Onorato1754d742016-11-21 17:51:35 -080063
64private:
65 const char* mFilename;
Yi Jinb592e3b2018-02-01 15:17:04 -080066 bool mIsSysfs; // sysfs files are pollable but return POLLERR by default, handle it separately
Joe Onorato1754d742016-11-21 17:51:35 -080067};
68
69/**
Yi Jin1a11fa12018-02-22 16:44:10 -080070 * Section that reads in a file and gzips the content.
71 */
72class GZipSection : public Section {
73public:
74 GZipSection(int id, const char* filename, ...);
75 virtual ~GZipSection();
76
Joe Onorato99598ee2019-02-11 15:55:13 +000077 virtual status_t Execute(ReportWriter* writer) const;
Yi Jin1a11fa12018-02-22 16:44:10 -080078
79private:
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 Onorato1754d742016-11-21 17:51:35 -080085 * Base class for sections that call a command that might need a timeout.
86 */
Yi Jinb592e3b2018-02-01 15:17:04 -080087class WorkerThreadSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -080088public:
Joe Onoratofe7bbf42019-03-24 20:57:16 -070089 WorkerThreadSection(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS);
Joe Onorato1754d742016-11-21 17:51:35 -080090 virtual ~WorkerThreadSection();
91
Joe Onorato99598ee2019-02-11 15:55:13 +000092 virtual status_t Execute(ReportWriter* writer) const;
Joe Onorato1754d742016-11-21 17:51:35 -080093
Mike Ma643de922019-12-17 10:56:17 -080094 virtual status_t BlockingCall(unique_fd& pipeWriteFd) const = 0;
Joe Onorato1754d742016-11-21 17:51:35 -080095};
96
97/**
98 * Section that forks and execs a command, and puts stdout as the section.
99 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800100class CommandSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -0800101public:
Yi Jin3f360352018-04-16 16:13:04 -0700102 CommandSection(int id, int64_t timeoutMs, const char* command, ...);
Yi Jinb44f7d42017-07-21 12:12:59 -0700103
104 CommandSection(int id, const char* command, ...);
105
Joe Onorato1754d742016-11-21 17:51:35 -0800106 virtual ~CommandSection();
107
Joe Onorato99598ee2019-02-11 15:55:13 +0000108 virtual status_t Execute(ReportWriter* writer) const;
Joe Onorato1754d742016-11-21 17:51:35 -0800109
110private:
111 const char** mCommand;
Joe Onorato1754d742016-11-21 17:51:35 -0800112};
113
114/**
Mike Ma5510f7c2020-02-19 02:56:04 -0800115 * Section that calls protobuf dumpsys on a system service, usually
116 * "dumpsys [service_name] --proto".
Joe Onorato1754d742016-11-21 17:51:35 -0800117 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800118class DumpsysSection : public WorkerThreadSection {
Joe Onorato1754d742016-11-21 17:51:35 -0800119public:
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700120 DumpsysSection(int id, const char* service, ...);
Joe Onorato1754d742016-11-21 17:51:35 -0800121 virtual ~DumpsysSection();
122
Mike Ma643de922019-12-17 10:56:17 -0800123 virtual status_t BlockingCall(unique_fd& pipeWriteFd) const;
Joe Onorato1754d742016-11-21 17:51:35 -0800124
125private:
126 String16 mService;
127 Vector<String16> mArgs;
128};
129
Yi Jin3c034c92017-12-22 17:36:47 -0800130/**
Mike Ma5510f7c2020-02-19 02:56:04 -0800131 * Section that calls text dumpsys on a system service, usually "dumpsys [service_name]".
132 */
Mike Ma87003ad2020-03-19 12:31:29 -0700133class TextDumpsysSection : public Section {
Mike Ma5510f7c2020-02-19 02:56:04 -0800134public:
135 TextDumpsysSection(int id, const char* service, ...);
136 virtual ~TextDumpsysSection();
137
Mike Ma87003ad2020-03-19 12:31:29 -0700138 virtual status_t Execute(ReportWriter* writer) const;
Mike Ma5510f7c2020-02-19 02:56:04 -0800139
140private:
141 String16 mService;
142 Vector<String16> mArgs;
143};
144
145/**
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700146 * Section that calls dumpsys on a system service.
147 */
148class SystemPropertyDumpsysSection : public WorkerThreadSection {
149public:
150 SystemPropertyDumpsysSection(int id, const char* service, ...);
151 virtual ~SystemPropertyDumpsysSection();
152
Mike Ma643de922019-12-17 10:56:17 -0800153 virtual status_t BlockingCall(unique_fd& pipeWriteFd) const;
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700154
155private:
156 String16 mService;
157 Vector<String16> mArgs;
158};
159
160/**
Yi Jin3c034c92017-12-22 17:36:47 -0800161 * Section that reads from logd.
162 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800163class LogSection : public WorkerThreadSection {
Yi Jin3c034c92017-12-22 17:36:47 -0800164 // global last log retrieved timestamp for each log_id_t.
165 static map<log_id_t, log_time> gLastLogsRetrieved;
166
Tom Cherry01cd1712020-03-23 13:41:46 -0700167 // log mode: non blocking.
168 const static int logModeBase = ANDROID_LOG_NONBLOCK;
zhouwenjiec3bf8042019-10-30 14:31:54 -0700169
Yi Jin3c034c92017-12-22 17:36:47 -0800170public:
zhouwenjiec3bf8042019-10-30 14:31:54 -0700171 LogSection(int id, const char* logID, ...);
Yi Jin3c034c92017-12-22 17:36:47 -0800172 virtual ~LogSection();
173
Mike Ma643de922019-12-17 10:56:17 -0800174 virtual status_t BlockingCall(unique_fd& pipeWriteFd) const;
Yi Jin3c034c92017-12-22 17:36:47 -0800175
176private:
177 log_id_t mLogID;
178 bool mBinary;
zhouwenjiec3bf8042019-10-30 14:31:54 -0700179 int mLogMode;
Yi Jin3c034c92017-12-22 17:36:47 -0800180};
181
Kweku Adamseadd1232018-02-05 16:45:13 -0800182/**
183 * Section that gets data from tombstoned.
184 */
185class TombstoneSection : public WorkerThreadSection {
186public:
Kweku Adams0f716792018-09-13 15:48:27 -0700187 TombstoneSection(int id, const char* type, int64_t timeoutMs = 120000 /* 2 minutes */);
Kweku Adamseadd1232018-02-05 16:45:13 -0800188 virtual ~TombstoneSection();
189
Mike Ma643de922019-12-17 10:56:17 -0800190 virtual status_t BlockingCall(unique_fd& pipeWriteFd) const;
Kweku Adamseadd1232018-02-05 16:45:13 -0800191
192private:
193 std::string mType;
194};
195
Mike Ma643de922019-12-17 10:56:17 -0800196/**
197 * Section that gets data from a registered dump callback.
198 */
199class BringYourOwnSection : public WorkerThreadSection {
200public:
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
209private:
210 const sp<IIncidentDumpCallback>& mCallback;
211};
212
Joe Onorato7a406b42019-04-12 18:11:30 -0700213
214/**
215 * These sections will not be generated when doing an 'all' report, either
216 * for size, speed of collection, or privacy.
217 */
218bool section_requires_specific_mention(int sectionId);
219
Yi Jin6cacbcb2018-03-30 14:04:52 -0700220} // namespace incidentd
221} // namespace os
222} // namespace android
223
Yi Jinb592e3b2018-02-01 15:17:04 -0800224#endif // SECTIONS_H