blob: 333344b8ce865f0c3ac5589a1bcac3840fcea3de [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:
44 return new ReverseParser();
45/* ========================================================================= */
46 // IDs larger than 0 are reserved in incident.proto
Yi Jinb44f7d42017-07-21 12:12:59 -070047 case 2000:
48 return new ProcrankParser();
Yi Jin0a3406f2017-06-22 19:23:11 -070049 case 2002:
50 return new KernelWakesParser();
51 default:
52 return NULL;
53 }
54}
55
56//=============================================================================
57int main(int argc, char** argv) {
58 fprintf(stderr, "Start incident_helper...\n");
59
60 // Parse the args
61 int opt;
62 int sectionID = 0;
Yi Jinb44f7d42017-07-21 12:12:59 -070063 while ((opt = getopt(argc, argv, "hs:")) != -1) {
Yi Jin0a3406f2017-06-22 19:23:11 -070064 switch (opt) {
65 case 'h':
66 usage(stdout);
67 return 0;
68 case 's':
69 sectionID = atoi(optarg);
70 break;
Yi Jin0a3406f2017-06-22 19:23:11 -070071 }
72 }
73
Yi Jin0a3406f2017-06-22 19:23:11 -070074 fprintf(stderr, "Pasring section %d...\n", sectionID);
75 TextParserBase* parser = selectParser(sectionID);
76 if (parser != NULL) {
77 fprintf(stderr, "Running parser: %s\n", parser->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -070078 status_t err = parser->Parse(STDIN_FILENO, STDOUT_FILENO);
Yi Jin0a3406f2017-06-22 19:23:11 -070079 if (err != NO_ERROR) {
80 fprintf(stderr, "Parse error in section %d: %s\n", sectionID, strerror(-err));
81 return -1;
82 }
83
84 delete parser;
85 }
86 fprintf(stderr, "Finish section %d, exiting...\n", sectionID);
87
88 return 0;
89}