blob: 8294be133bcb60e3f85f716019edd8aa2ab81546 [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
30using namespace android;
31
Yi Jinb592e3b2018-02-01 15:17:04 -080032const int64_t REMOTE_CALL_TIMEOUT_MS = 10 * 1000; // 10 seconds
Yi Jinb44f7d42017-07-21 12:12:59 -070033
Joe Onorato1754d742016-11-21 17:51:35 -080034/**
35 * Base class for sections
36 */
Yi Jinb592e3b2018-02-01 15:17:04 -080037class Section {
Joe Onorato1754d742016-11-21 17:51:35 -080038public:
Yi Jinb44f7d42017-07-21 12:12:59 -070039 const int id;
Yi Jinb592e3b2018-02-01 15:17:04 -080040 const int64_t timeoutMs; // each section must have a timeout
Joe Onorato1754d742016-11-21 17:51:35 -080041 String8 name;
42
Yi Jinb44f7d42017-07-21 12:12:59 -070043 Section(int id, const int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS);
Joe Onorato1754d742016-11-21 17:51:35 -080044 virtual ~Section();
45
46 virtual status_t Execute(ReportRequestSet* requests) const = 0;
Joe Onorato1754d742016-11-21 17:51:35 -080047};
48
49/**
Yi Jinedfd5bb2017-09-06 17:09:11 -070050 * Section that generates incident headers.
51 */
Yi Jinb592e3b2018-02-01 15:17:04 -080052class HeaderSection : public Section {
Yi Jinedfd5bb2017-09-06 17:09:11 -070053public:
54 HeaderSection();
55 virtual ~HeaderSection();
56
57 virtual status_t Execute(ReportRequestSet* requests) const;
58};
59
60/**
Yi Jin329130b2018-02-09 16:47:47 -080061 * Section that generates incident metadata.
62 */
Yi Jinb592e3b2018-02-01 15:17:04 -080063class MetadataSection : public Section {
Yi Jin329130b2018-02-09 16:47:47 -080064public:
65 MetadataSection();
66 virtual ~MetadataSection();
67
68 virtual status_t Execute(ReportRequestSet* requests) const;
69};
70
71/**
Joe Onorato1754d742016-11-21 17:51:35 -080072 * Section that reads in a file.
73 */
Yi Jinb592e3b2018-02-01 15:17:04 -080074class FileSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -080075public:
Yi Jinb44f7d42017-07-21 12:12:59 -070076 FileSection(int id, const char* filename, const int64_t timeoutMs = 5000 /* 5 seconds */);
Joe Onorato1754d742016-11-21 17:51:35 -080077 virtual ~FileSection();
78
79 virtual status_t Execute(ReportRequestSet* requests) const;
80
81private:
82 const char* mFilename;
Yi Jinb592e3b2018-02-01 15:17:04 -080083 bool mIsSysfs; // sysfs files are pollable but return POLLERR by default, handle it separately
Joe Onorato1754d742016-11-21 17:51:35 -080084};
85
86/**
Yi Jin1a11fa12018-02-22 16:44:10 -080087 * Section that reads in a file and gzips the content.
88 */
89class GZipSection : public Section {
90public:
91 GZipSection(int id, const char* filename, ...);
92 virtual ~GZipSection();
93
94 virtual status_t Execute(ReportRequestSet* requests) const;
95
96private:
97 // It looks up the content from multiple files and stops when the first one is available.
98 const char** mFilenames;
99};
100
101/**
Joe Onorato1754d742016-11-21 17:51:35 -0800102 * Base class for sections that call a command that might need a timeout.
103 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800104class WorkerThreadSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -0800105public:
106 WorkerThreadSection(int id);
107 virtual ~WorkerThreadSection();
108
109 virtual status_t Execute(ReportRequestSet* requests) const;
110
111 virtual status_t BlockingCall(int pipeWriteFd) const = 0;
112};
113
114/**
115 * Section that forks and execs a command, and puts stdout as the section.
116 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800117class CommandSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -0800118public:
Yi Jinb44f7d42017-07-21 12:12:59 -0700119 CommandSection(int id, const int64_t timeoutMs, const char* command, ...);
120
121 CommandSection(int id, const char* command, ...);
122
Joe Onorato1754d742016-11-21 17:51:35 -0800123 virtual ~CommandSection();
124
125 virtual status_t Execute(ReportRequestSet* requests) const;
126
127private:
128 const char** mCommand;
Joe Onorato1754d742016-11-21 17:51:35 -0800129};
130
131/**
132 * Section that calls dumpsys on a system service.
133 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800134class DumpsysSection : public WorkerThreadSection {
Joe Onorato1754d742016-11-21 17:51:35 -0800135public:
136 DumpsysSection(int id, const char* service, ...);
137 virtual ~DumpsysSection();
138
139 virtual status_t BlockingCall(int pipeWriteFd) const;
140
141private:
142 String16 mService;
143 Vector<String16> mArgs;
144};
145
Yi Jin3c034c92017-12-22 17:36:47 -0800146/**
147 * Section that reads from logd.
148 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800149class LogSection : public WorkerThreadSection {
Yi Jin3c034c92017-12-22 17:36:47 -0800150 // global last log retrieved timestamp for each log_id_t.
151 static map<log_id_t, log_time> gLastLogsRetrieved;
152
153public:
154 LogSection(int id, log_id_t logID);
155 virtual ~LogSection();
156
157 virtual status_t BlockingCall(int pipeWriteFd) const;
158
159private:
160 log_id_t mLogID;
161 bool mBinary;
162};
163
Yi Jinb592e3b2018-02-01 15:17:04 -0800164#endif // SECTIONS_H