blob: ab92473b8ba3e49b1575d5961b4e56dd83f6e69e [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 Jin04625ad2017-10-17 18:29:33 -070022#include "parsers/KernelWakesParser.h"
23#include "parsers/PageTypeInfoParser.h"
24#include "parsers/ProcrankParser.h"
Yi Jin0dfa7522017-11-06 17:43:47 -080025#include "parsers/SystemPropertiesParser.h"
Yi Jin0a3406f2017-06-22 19:23:11 -070026
27#include <android-base/file.h>
28#include <getopt.h>
29#include <stdlib.h>
30#include <unistd.h>
31
32using namespace android::base;
33using namespace std;
34
35static void usage(FILE* out) {
Yi Jinb44f7d42017-07-21 12:12:59 -070036 fprintf(out, "incident_helper is not designed to run manually,");
37 fprintf(out, "it reads from stdin and writes to stdout, see README.md for details.\n");
38 fprintf(out, "usage: incident_helper -s SECTION\n");
Yi Jin0a3406f2017-06-22 19:23:11 -070039 fprintf(out, "REQUIRED:\n");
40 fprintf(out, " -s section id, must be positive\n");
Yi Jin0a3406f2017-06-22 19:23:11 -070041}
42
43//=============================================================================
44static TextParserBase* selectParser(int section) {
45 switch (section) {
46 // IDs smaller than or equal to 0 are reserved for testing
47 case -1:
48 return new TimeoutParser();
49 case 0:
Yi Jin99c248f2017-08-25 18:11:58 -070050 return new NoopParser();
51 case 1: // 1 is reserved for incident header so it won't be section id
Yi Jin0a3406f2017-06-22 19:23:11 -070052 return new ReverseParser();
53/* ========================================================================= */
Yi Jin99c248f2017-08-25 18:11:58 -070054 // IDs larger than 1 are section ids reserved in incident.proto
Yi Jin0dfa7522017-11-06 17:43:47 -080055 case 1000:
56 return new SystemPropertiesParser();
Yi Jinb44f7d42017-07-21 12:12:59 -070057 case 2000:
58 return new ProcrankParser();
Yi Jin810b14f2017-09-11 19:01:08 -070059 case 2001:
60 return new PageTypeInfoParser();
Yi Jin0a3406f2017-06-22 19:23:11 -070061 case 2002:
62 return new KernelWakesParser();
Yi Jine2f7f792017-11-01 17:08:27 -070063 case 2003:
64 return new CpuInfoParser();
Yi Jin0eb22342017-11-06 17:17:27 -080065 case 2004:
66 return new CpuFreqParser();
Kweku Adams59e6fd72017-12-27 15:48:14 -080067 case 2006:
68 return new BatteryTypeParser();
Yi Jin0a3406f2017-06-22 19:23:11 -070069 default:
70 return NULL;
71 }
72}
73
74//=============================================================================
75int main(int argc, char** argv) {
76 fprintf(stderr, "Start incident_helper...\n");
77
78 // Parse the args
79 int opt;
80 int sectionID = 0;
Yi Jinb44f7d42017-07-21 12:12:59 -070081 while ((opt = getopt(argc, argv, "hs:")) != -1) {
Yi Jin0a3406f2017-06-22 19:23:11 -070082 switch (opt) {
83 case 'h':
84 usage(stdout);
85 return 0;
86 case 's':
87 sectionID = atoi(optarg);
88 break;
Yi Jin0a3406f2017-06-22 19:23:11 -070089 }
90 }
91
Yi Jin0a3406f2017-06-22 19:23:11 -070092 fprintf(stderr, "Pasring section %d...\n", sectionID);
93 TextParserBase* parser = selectParser(sectionID);
94 if (parser != NULL) {
95 fprintf(stderr, "Running parser: %s\n", parser->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -070096 status_t err = parser->Parse(STDIN_FILENO, STDOUT_FILENO);
Yi Jin0a3406f2017-06-22 19:23:11 -070097 if (err != NO_ERROR) {
98 fprintf(stderr, "Parse error in section %d: %s\n", sectionID, strerror(-err));
99 return -1;
100 }
101
102 delete parser;
103 }
104 fprintf(stderr, "Finish section %d, exiting...\n", sectionID);
105
106 return 0;
107}