blob: f89824c07b8734da0755d2444d78086455ef8e6c [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
Joe Onorato1754d742016-11-21 17:51:35 -080026#include <utils/String16.h>
Yi Jinb592e3b2018-02-01 15:17:04 -080027#include <utils/String8.h>
Joe Onorato1754d742016-11-21 17:51:35 -080028#include <utils/Vector.h>
29
Yi Jin6cacbcb2018-03-30 14:04:52 -070030namespace android {
31namespace os {
32namespace incidentd {
Joe Onorato1754d742016-11-21 17:51:35 -080033
Yi Jinad3e6e52018-04-03 15:10:34 -070034const int64_t REMOTE_CALL_TIMEOUT_MS = 30 * 1000; // 30 seconds
Yi Jinb44f7d42017-07-21 12:12:59 -070035
Joe Onorato1754d742016-11-21 17:51:35 -080036/**
37 * Base class for sections
38 */
Yi Jinb592e3b2018-02-01 15:17:04 -080039class Section {
Joe Onorato1754d742016-11-21 17:51:35 -080040public:
Yi Jinb44f7d42017-07-21 12:12:59 -070041 const int id;
Yi Jinb592e3b2018-02-01 15:17:04 -080042 const int64_t timeoutMs; // each section must have a timeout
Kweku Adams3d160912018-05-07 11:26:27 -070043 const bool userdebugAndEngOnly;
Joe Onorato1754d742016-11-21 17:51:35 -080044 String8 name;
45
Kweku Adamse04ef772018-06-13 12:24:38 -070046 Section(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS, bool userdebugAndEngOnly = false);
Joe Onorato1754d742016-11-21 17:51:35 -080047 virtual ~Section();
48
Joe Onorato99598ee2019-02-11 15:55:13 +000049 virtual status_t Execute(ReportWriter* writer) const = 0;
Yi Jin329130b2018-02-09 16:47:47 -080050};
51
52/**
Joe Onorato1754d742016-11-21 17:51:35 -080053 * Section that reads in a file.
54 */
Yi Jinb592e3b2018-02-01 15:17:04 -080055class FileSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -080056public:
Kweku Adamse04ef772018-06-13 12:24:38 -070057 FileSection(int id, const char* filename,
Yi Jin3f360352018-04-16 16:13:04 -070058 int64_t timeoutMs = 5000 /* 5 seconds */);
Joe Onorato1754d742016-11-21 17:51:35 -080059 virtual ~FileSection();
60
Joe Onorato99598ee2019-02-11 15:55:13 +000061 virtual status_t Execute(ReportWriter* writer) const;
Joe Onorato1754d742016-11-21 17:51:35 -080062
63private:
64 const char* mFilename;
Yi Jinb592e3b2018-02-01 15:17:04 -080065 bool mIsSysfs; // sysfs files are pollable but return POLLERR by default, handle it separately
Joe Onorato1754d742016-11-21 17:51:35 -080066};
67
68/**
Yi Jin1a11fa12018-02-22 16:44:10 -080069 * Section that reads in a file and gzips the content.
70 */
71class GZipSection : public Section {
72public:
73 GZipSection(int id, const char* filename, ...);
74 virtual ~GZipSection();
75
Joe Onorato99598ee2019-02-11 15:55:13 +000076 virtual status_t Execute(ReportWriter* writer) const;
Yi Jin1a11fa12018-02-22 16:44:10 -080077
78private:
79 // It looks up the content from multiple files and stops when the first one is available.
80 const char** mFilenames;
81};
82
83/**
Joe Onorato1754d742016-11-21 17:51:35 -080084 * Base class for sections that call a command that might need a timeout.
85 */
Yi Jinb592e3b2018-02-01 15:17:04 -080086class WorkerThreadSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -080087public:
Kweku Adams3d160912018-05-07 11:26:27 -070088 WorkerThreadSection(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS,
89 bool userdebugAndEngOnly = false);
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
94 virtual status_t BlockingCall(int pipeWriteFd) const = 0;
95};
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:
Kweku Adams3d160912018-05-07 11:26:27 -0700119 DumpsysSection(int id, bool userdebugAndEngOnly, const char* service, ...);
Joe Onorato1754d742016-11-21 17:51:35 -0800120 virtual ~DumpsysSection();
121
122 virtual status_t BlockingCall(int pipeWriteFd) const;
123
124private:
125 String16 mService;
126 Vector<String16> mArgs;
127};
128
Yi Jin3c034c92017-12-22 17:36:47 -0800129/**
130 * Section that reads from logd.
131 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800132class LogSection : public WorkerThreadSection {
Yi Jin3c034c92017-12-22 17:36:47 -0800133 // global last log retrieved timestamp for each log_id_t.
134 static map<log_id_t, log_time> gLastLogsRetrieved;
135
136public:
137 LogSection(int id, log_id_t logID);
138 virtual ~LogSection();
139
140 virtual status_t BlockingCall(int pipeWriteFd) const;
141
142private:
143 log_id_t mLogID;
144 bool mBinary;
145};
146
Kweku Adamseadd1232018-02-05 16:45:13 -0800147/**
148 * Section that gets data from tombstoned.
149 */
150class TombstoneSection : public WorkerThreadSection {
151public:
Kweku Adams0f716792018-09-13 15:48:27 -0700152 TombstoneSection(int id, const char* type, int64_t timeoutMs = 120000 /* 2 minutes */);
Kweku Adamseadd1232018-02-05 16:45:13 -0800153 virtual ~TombstoneSection();
154
155 virtual status_t BlockingCall(int pipeWriteFd) const;
156
157private:
158 std::string mType;
159};
160
Yi Jin6cacbcb2018-03-30 14:04:52 -0700161} // namespace incidentd
162} // namespace os
163} // namespace android
164
Yi Jinb592e3b2018-02-01 15:17:04 -0800165#endif // SECTIONS_H