2017-06-22 19:23:11 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "incident_helper"
|
|
|
|
|
|
|
|
#include "IncidentHelper.h"
|
|
|
|
|
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
using namespace android::base;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
static void usage(FILE* out) {
|
2017-07-21 12:12:59 -07:00
|
|
|
fprintf(out, "incident_helper is not designed to run manually,");
|
|
|
|
fprintf(out, "it reads from stdin and writes to stdout, see README.md for details.\n");
|
|
|
|
fprintf(out, "usage: incident_helper -s SECTION\n");
|
2017-06-22 19:23:11 -07:00
|
|
|
fprintf(out, "REQUIRED:\n");
|
|
|
|
fprintf(out, " -s section id, must be positive\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
static TextParserBase* selectParser(int section) {
|
|
|
|
switch (section) {
|
|
|
|
// IDs smaller than or equal to 0 are reserved for testing
|
|
|
|
case -1:
|
|
|
|
return new TimeoutParser();
|
|
|
|
case 0:
|
2017-08-25 18:11:58 -07:00
|
|
|
return new NoopParser();
|
|
|
|
case 1: // 1 is reserved for incident header so it won't be section id
|
2017-06-22 19:23:11 -07:00
|
|
|
return new ReverseParser();
|
|
|
|
/* ========================================================================= */
|
2017-08-25 18:11:58 -07:00
|
|
|
// IDs larger than 1 are section ids reserved in incident.proto
|
2017-07-21 12:12:59 -07:00
|
|
|
case 2000:
|
|
|
|
return new ProcrankParser();
|
2017-06-22 19:23:11 -07:00
|
|
|
case 2002:
|
|
|
|
return new KernelWakesParser();
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
fprintf(stderr, "Start incident_helper...\n");
|
|
|
|
|
|
|
|
// Parse the args
|
|
|
|
int opt;
|
|
|
|
int sectionID = 0;
|
2017-07-21 12:12:59 -07:00
|
|
|
while ((opt = getopt(argc, argv, "hs:")) != -1) {
|
2017-06-22 19:23:11 -07:00
|
|
|
switch (opt) {
|
|
|
|
case 'h':
|
|
|
|
usage(stdout);
|
|
|
|
return 0;
|
|
|
|
case 's':
|
|
|
|
sectionID = atoi(optarg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Pasring section %d...\n", sectionID);
|
|
|
|
TextParserBase* parser = selectParser(sectionID);
|
|
|
|
if (parser != NULL) {
|
|
|
|
fprintf(stderr, "Running parser: %s\n", parser->name.string());
|
2017-07-21 12:12:59 -07:00
|
|
|
status_t err = parser->Parse(STDIN_FILENO, STDOUT_FILENO);
|
2017-06-22 19:23:11 -07:00
|
|
|
if (err != NO_ERROR) {
|
|
|
|
fprintf(stderr, "Parse error in section %d: %s\n", sectionID, strerror(-err));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete parser;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Finish section %d, exiting...\n", sectionID);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|