blob: 296d3001b7bb30b538a0ac231af2fcc1b4d29a1f [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 Jin0a3406f2017-06-22 19:23:11 -070051 case 2002:
52 return new KernelWakesParser();
53 default:
54 return NULL;
55 }
56}
57
58//=============================================================================
59int main(int argc, char** argv) {
60 fprintf(stderr, "Start incident_helper...\n");
61
62 // Parse the args
63 int opt;
64 int sectionID = 0;
Yi Jinb44f7d42017-07-21 12:12:59 -070065 while ((opt = getopt(argc, argv, "hs:")) != -1) {
Yi Jin0a3406f2017-06-22 19:23:11 -070066 switch (opt) {
67 case 'h':
68 usage(stdout);
69 return 0;
70 case 's':
71 sectionID = atoi(optarg);
72 break;
Yi Jin0a3406f2017-06-22 19:23:11 -070073 }
74 }
75
Yi Jin0a3406f2017-06-22 19:23:11 -070076 fprintf(stderr, "Pasring section %d...\n", sectionID);
77 TextParserBase* parser = selectParser(sectionID);
78 if (parser != NULL) {
79 fprintf(stderr, "Running parser: %s\n", parser->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -070080 status_t err = parser->Parse(STDIN_FILENO, STDOUT_FILENO);
Yi Jin0a3406f2017-06-22 19:23:11 -070081 if (err != NO_ERROR) {
82 fprintf(stderr, "Parse error in section %d: %s\n", sectionID, strerror(-err));
83 return -1;
84 }
85
86 delete parser;
87 }
88 fprintf(stderr, "Finish section %d, exiting...\n", sectionID);
89
90 return 0;
91}