blob: cc03d4a65f1789ade1ad5b5de656e4af002d559f [file] [log] [blame]
Yi Jin0a3406f2017-06-22 19:23:11 -07001/*
2 * Copyright (C) 2017 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 */
16
17#define LOG_TAG "incident_helper"
18
Kweku Adams59e6fd72017-12-27 15:48:14 -080019#include "parsers/BatteryTypeParser.h"
Yi Jin0eb22342017-11-06 17:17:27 -080020#include "parsers/CpuFreqParser.h"
Yi Jine2f7f792017-11-01 17:08:27 -070021#include "parsers/CpuInfoParser.h"
Yi Jin3c034c92017-12-22 17:36:47 -080022#include "parsers/EventLogTagsParser.h"
Yi Jin04625ad2017-10-17 18:29:33 -070023#include "parsers/KernelWakesParser.h"
24#include "parsers/PageTypeInfoParser.h"
25#include "parsers/ProcrankParser.h"
Kweku Adamsf5cc5752017-12-20 17:59:17 -080026#include "parsers/PsParser.h"
Yi Jin0dfa7522017-11-06 17:43:47 -080027#include "parsers/SystemPropertiesParser.h"
Yi Jin0a3406f2017-06-22 19:23:11 -070028
29#include <android-base/file.h>
30#include <getopt.h>
31#include <stdlib.h>
32#include <unistd.h>
33
34using namespace android::base;
35using namespace std;
36
37static void usage(FILE* out) {
Yi Jinb44f7d42017-07-21 12:12:59 -070038 fprintf(out, "incident_helper is not designed to run manually,");
39 fprintf(out, "it reads from stdin and writes to stdout, see README.md for details.\n");
40 fprintf(out, "usage: incident_helper -s SECTION\n");
Yi Jin0a3406f2017-06-22 19:23:11 -070041 fprintf(out, "REQUIRED:\n");
42 fprintf(out, " -s section id, must be positive\n");
Yi Jin0a3406f2017-06-22 19:23:11 -070043}
44
45//=============================================================================
46static TextParserBase* selectParser(int section) {
47 switch (section) {
48 // IDs smaller than or equal to 0 are reserved for testing
49 case -1:
50 return new TimeoutParser();
51 case 0:
Yi Jin99c248f2017-08-25 18:11:58 -070052 return new NoopParser();
53 case 1: // 1 is reserved for incident header so it won't be section id
Yi Jin0a3406f2017-06-22 19:23:11 -070054 return new ReverseParser();
55/* ========================================================================= */
Yi Jin99c248f2017-08-25 18:11:58 -070056 // IDs larger than 1 are section ids reserved in incident.proto
Yi Jin0dfa7522017-11-06 17:43:47 -080057 case 1000:
58 return new SystemPropertiesParser();
Yi Jin3c034c92017-12-22 17:36:47 -080059 case 1100:
60 return new EventLogTagsParser();
Yi Jinb44f7d42017-07-21 12:12:59 -070061 case 2000:
62 return new ProcrankParser();
Yi Jin810b14f2017-09-11 19:01:08 -070063 case 2001:
64 return new PageTypeInfoParser();
Yi Jin0a3406f2017-06-22 19:23:11 -070065 case 2002:
66 return new KernelWakesParser();
Yi Jine2f7f792017-11-01 17:08:27 -070067 case 2003:
68 return new CpuInfoParser();
Yi Jin0eb22342017-11-06 17:17:27 -080069 case 2004:
70 return new CpuFreqParser();
Kweku Adamsf5cc5752017-12-20 17:59:17 -080071 case 2005:
72 return new PsParser();
Kweku Adams59e6fd72017-12-27 15:48:14 -080073 case 2006:
74 return new BatteryTypeParser();
Ryan Savitskicc7d9972019-06-03 23:57:09 +010075 case 3026: // system_trace is already a serialized protobuf
76 return new NoopParser();
Yi Jin0a3406f2017-06-22 19:23:11 -070077 default:
Yi Jin908c02f2018-06-22 16:51:40 -070078 // Return no op parser when no specific ones are implemented.
79 return new NoopParser();
Yi Jin0a3406f2017-06-22 19:23:11 -070080 }
81}
82
83//=============================================================================
84int main(int argc, char** argv) {
85 fprintf(stderr, "Start incident_helper...\n");
86
87 // Parse the args
88 int opt;
89 int sectionID = 0;
Yi Jinb44f7d42017-07-21 12:12:59 -070090 while ((opt = getopt(argc, argv, "hs:")) != -1) {
Yi Jin0a3406f2017-06-22 19:23:11 -070091 switch (opt) {
92 case 'h':
93 usage(stdout);
94 return 0;
95 case 's':
96 sectionID = atoi(optarg);
97 break;
Yi Jin0a3406f2017-06-22 19:23:11 -070098 }
99 }
100
Yi Jin0a3406f2017-06-22 19:23:11 -0700101 fprintf(stderr, "Pasring section %d...\n", sectionID);
102 TextParserBase* parser = selectParser(sectionID);
Yi Kong08a8d722018-08-06 14:48:58 -0700103 if (parser != nullptr) {
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +0000104 fprintf(stderr, "Running parser: %s\n", parser->name.c_str());
Yi Jinb44f7d42017-07-21 12:12:59 -0700105 status_t err = parser->Parse(STDIN_FILENO, STDOUT_FILENO);
Yi Jin0a3406f2017-06-22 19:23:11 -0700106 if (err != NO_ERROR) {
107 fprintf(stderr, "Parse error in section %d: %s\n", sectionID, strerror(-err));
108 return -1;
109 }
110
111 delete parser;
112 }
113 fprintf(stderr, "Finish section %d, exiting...\n", sectionID);
114
115 return 0;
116}