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