blob: 0bb9da9aac5bf8db6163b9985e4da69efba16009 [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/**
115 * Section that calls dumpsys on a system service.
116 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800117class DumpsysSection : public WorkerThreadSection {
Joe Onorato1754d742016-11-21 17:51:35 -0800118public:
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700119 DumpsysSection(int id, const char* service, ...);
Joe Onorato1754d742016-11-21 17:51:35 -0800120 virtual ~DumpsysSection();
121
Mike Ma643de922019-12-17 10:56:17 -0800122 virtual status_t BlockingCall(unique_fd& pipeWriteFd) const;
Joe Onorato1754d742016-11-21 17:51:35 -0800123
124private:
125 String16 mService;
126 Vector<String16> mArgs;
127};
128
Yi Jin3c034c92017-12-22 17:36:47 -0800129/**
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700130 * Section that calls dumpsys on a system service.
131 */
132class SystemPropertyDumpsysSection : public WorkerThreadSection {
133public:
134 SystemPropertyDumpsysSection(int id, const char* service, ...);
135 virtual ~SystemPropertyDumpsysSection();
136
Mike Ma643de922019-12-17 10:56:17 -0800137 virtual status_t BlockingCall(unique_fd& pipeWriteFd) const;
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700138
139private:
140 String16 mService;
141 Vector<String16> mArgs;
142};
143
144/**
Yi Jin3c034c92017-12-22 17:36:47 -0800145 * Section that reads from logd.
146 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800147class LogSection : public WorkerThreadSection {
Yi Jin3c034c92017-12-22 17:36:47 -0800148 // global last log retrieved timestamp for each log_id_t.
149 static map<log_id_t, log_time> gLastLogsRetrieved;
150
zhouwenjiec3bf8042019-10-30 14:31:54 -0700151 // log mode: read only & non blocking.
152 const static int logModeBase = ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
153
Yi Jin3c034c92017-12-22 17:36:47 -0800154public:
zhouwenjiec3bf8042019-10-30 14:31:54 -0700155 LogSection(int id, const char* logID, ...);
Yi Jin3c034c92017-12-22 17:36:47 -0800156 virtual ~LogSection();
157
Mike Ma643de922019-12-17 10:56:17 -0800158 virtual status_t BlockingCall(unique_fd& pipeWriteFd) const;
Yi Jin3c034c92017-12-22 17:36:47 -0800159
160private:
161 log_id_t mLogID;
162 bool mBinary;
zhouwenjiec3bf8042019-10-30 14:31:54 -0700163 int mLogMode;
Yi Jin3c034c92017-12-22 17:36:47 -0800164};
165
Kweku Adamseadd1232018-02-05 16:45:13 -0800166/**
167 * Section that gets data from tombstoned.
168 */
169class TombstoneSection : public WorkerThreadSection {
170public:
Kweku Adams0f716792018-09-13 15:48:27 -0700171 TombstoneSection(int id, const char* type, int64_t timeoutMs = 120000 /* 2 minutes */);
Kweku Adamseadd1232018-02-05 16:45:13 -0800172 virtual ~TombstoneSection();
173
Mike Ma643de922019-12-17 10:56:17 -0800174 virtual status_t BlockingCall(unique_fd& pipeWriteFd) const;
Kweku Adamseadd1232018-02-05 16:45:13 -0800175
176private:
177 std::string mType;
178};
179
Mike Ma643de922019-12-17 10:56:17 -0800180/**
181 * Section that gets data from a registered dump callback.
182 */
183class BringYourOwnSection : public WorkerThreadSection {
184public:
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
193private:
194 const sp<IIncidentDumpCallback>& mCallback;
195};
196
Joe Onorato7a406b42019-04-12 18:11:30 -0700197
198/**
199 * These sections will not be generated when doing an 'all' report, either
200 * for size, speed of collection, or privacy.
201 */
202bool section_requires_specific_mention(int sectionId);
203
Yi Jin6cacbcb2018-03-30 14:04:52 -0700204} // namespace incidentd
205} // namespace os
206} // namespace android
207
Yi Jinb592e3b2018-02-01 15:17:04 -0800208#endif // SECTIONS_H