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