main.cpp revision 0eb223496c3dee6bd0a33ea4fa664fb92ef557cc
1/*
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 "parsers/CpuFreqParser.h"
20#include "parsers/CpuInfoParser.h"
21#include "parsers/KernelWakesParser.h"
22#include "parsers/PageTypeInfoParser.h"
23#include "parsers/ProcrankParser.h"
24
25#include <android-base/file.h>
26#include <getopt.h>
27#include <stdlib.h>
28#include <unistd.h>
29
30using namespace android::base;
31using namespace std;
32
33static void usage(FILE* out) {
34    fprintf(out, "incident_helper is not designed to run manually,");
35    fprintf(out, "it reads from stdin and writes to stdout, see README.md for details.\n");
36    fprintf(out, "usage: incident_helper -s SECTION\n");
37    fprintf(out, "REQUIRED:\n");
38    fprintf(out, "  -s           section id, must be positive\n");
39}
40
41//=============================================================================
42static TextParserBase* selectParser(int section) {
43    switch (section) {
44        // IDs smaller than or equal to 0 are reserved for testing
45        case -1:
46            return new TimeoutParser();
47        case 0:
48            return new NoopParser();
49        case 1: // 1 is reserved for incident header so it won't be section id
50            return new ReverseParser();
51/* ========================================================================= */
52        // IDs larger than 1 are section ids reserved in incident.proto
53        case 2000:
54            return new ProcrankParser();
55        case 2001:
56            return new PageTypeInfoParser();
57        case 2002:
58            return new KernelWakesParser();
59        case 2003:
60            return new CpuInfoParser();
61        case 2004:
62            return new CpuFreqParser();
63        default:
64            return NULL;
65    }
66}
67
68//=============================================================================
69int main(int argc, char** argv) {
70    fprintf(stderr, "Start incident_helper...\n");
71
72    // Parse the args
73    int opt;
74    int sectionID = 0;
75    while ((opt = getopt(argc, argv, "hs:")) != -1) {
76        switch (opt) {
77            case 'h':
78                usage(stdout);
79                return 0;
80            case 's':
81                sectionID = atoi(optarg);
82                break;
83        }
84    }
85
86    fprintf(stderr, "Pasring section %d...\n", sectionID);
87    TextParserBase* parser = selectParser(sectionID);
88    if (parser != NULL) {
89        fprintf(stderr, "Running parser: %s\n", parser->name.string());
90        status_t err = parser->Parse(STDIN_FILENO, STDOUT_FILENO);
91        if (err != NO_ERROR) {
92            fprintf(stderr, "Parse error in section %d: %s\n", sectionID, strerror(-err));
93            return -1;
94        }
95
96        delete parser;
97    }
98    fprintf(stderr, "Finish section %d, exiting...\n", sectionID);
99
100    return 0;
101}
102