blob: 52ff77720d708d50694f3449cd627487540ca712 [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
19#include "IncidentHelper.h"
20
21#include <android-base/file.h>
22#include <getopt.h>
23#include <stdlib.h>
24#include <unistd.h>
25
26using namespace android::base;
27using namespace std;
28
29static void usage(FILE* out) {
Yi Jinb44f7d42017-07-21 12:12:59 -070030 fprintf(out, "incident_helper is not designed to run manually,");
31 fprintf(out, "it reads from stdin and writes to stdout, see README.md for details.\n");
32 fprintf(out, "usage: incident_helper -s SECTION\n");
Yi Jin0a3406f2017-06-22 19:23:11 -070033 fprintf(out, "REQUIRED:\n");
34 fprintf(out, " -s section id, must be positive\n");
Yi Jin0a3406f2017-06-22 19:23:11 -070035}
36
37//=============================================================================
38static TextParserBase* selectParser(int section) {
39 switch (section) {
40 // IDs smaller than or equal to 0 are reserved for testing
41 case -1:
42 return new TimeoutParser();
43 case 0:
Yi Jin99c248f2017-08-25 18:11:58 -070044 return new NoopParser();
45 case 1: // 1 is reserved for incident header so it won't be section id
Yi Jin0a3406f2017-06-22 19:23:11 -070046 return new ReverseParser();
47/* ========================================================================= */
Yi Jin99c248f2017-08-25 18:11:58 -070048 // IDs larger than 1 are section ids reserved in incident.proto
Yi Jinb44f7d42017-07-21 12:12:59 -070049 case 2000:
50 return new ProcrankParser();
Yi Jin810b14f2017-09-11 19:01:08 -070051 case 2001:
52 return new PageTypeInfoParser();
Yi Jin0a3406f2017-06-22 19:23:11 -070053 case 2002:
54 return new KernelWakesParser();
55 default:
56 return NULL;
57 }
58}
59
60//=============================================================================
61int main(int argc, char** argv) {
62 fprintf(stderr, "Start incident_helper...\n");
63
64 // Parse the args
65 int opt;
66 int sectionID = 0;
Yi Jinb44f7d42017-07-21 12:12:59 -070067 while ((opt = getopt(argc, argv, "hs:")) != -1) {
Yi Jin0a3406f2017-06-22 19:23:11 -070068 switch (opt) {
69 case 'h':
70 usage(stdout);
71 return 0;
72 case 's':
73 sectionID = atoi(optarg);
74 break;
Yi Jin0a3406f2017-06-22 19:23:11 -070075 }
76 }
77
Yi Jin0a3406f2017-06-22 19:23:11 -070078 fprintf(stderr, "Pasring section %d...\n", sectionID);
79 TextParserBase* parser = selectParser(sectionID);
80 if (parser != NULL) {
81 fprintf(stderr, "Running parser: %s\n", parser->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -070082 status_t err = parser->Parse(STDIN_FILENO, STDOUT_FILENO);
Yi Jin0a3406f2017-06-22 19:23:11 -070083 if (err != NO_ERROR) {
84 fprintf(stderr, "Parse error in section %d: %s\n", sectionID, strerror(-err));
85 return -1;
86 }
87
88 delete parser;
89 }
90 fprintf(stderr, "Finish section %d, exiting...\n", sectionID);
91
92 return 0;
93}