Home | History | Annotate | Download | only in ndk-stack
      1 /* Copyright (C) 2007-2011 The Android Open Source Project
      2 **
      3 ** This software is licensed under the terms of the GNU General Public
      4 ** License version 2, as published by the Free Software Foundation, and
      5 ** may be copied, distributed, and modified under those terms.
      6 **
      7 ** This program is distributed in the hope that it will be useful,
      8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10 ** GNU General Public License for more details.
     11 */
     12 
     13 /*
     14  * Contains implementation of main routine for ndk-stack utility.
     15  */
     16 
     17 #include <stdio.h>
     18 #include <stdlib.h>
     19 #include <string.h>
     20 #include <errno.h>
     21 
     22 #include "ndk-stack-parser.h"
     23 
     24 /* Usage string. */
     25 static const char* _usage_str =
     26 "Usage:\n"
     27 "   ndk-stack -sym <path> [-dump <path>]\n\n"
     28 "      -sym  Contains full path to the root directory for symbols.\n"
     29 "      -dump Contains full path to the file containing the crash dump.\n"
     30 "            This is an optional parameter. If ommited, ndk-stack will\n"
     31 "            read input data from stdin\n"
     32 "\n"
     33 "   See docs/NDK-STACK.html in your NDK installation tree for more details.\n\n";
     34 
     35 int main(int argc, char **argv, char **envp)
     36 {
     37     const char* dump_file = NULL;
     38     const char* sym_path = NULL;
     39     int use_stdin = 0;
     40 
     41     /* Parse command line. */
     42     {
     43         int n;
     44         for (n = 1; n < argc; n++) {
     45             if (!strcmp(argv[n], "-dump")) {
     46                 n++;
     47                 if (n < argc) {
     48                     dump_file = argv[n];
     49                 }
     50             } else if (!strcmp(argv[n], "-sym")) {
     51                 n++;
     52                 if (n < argc) {
     53                     sym_path = argv[n];
     54                 }
     55             } else {
     56                 fprintf(stdout, "%s", _usage_str);
     57                 return -1;
     58             }
     59         }
     60         if (sym_path == NULL) {
     61             fprintf(stdout, "%s", _usage_str);
     62             return -1;
     63         }
     64         if (dump_file == NULL) {
     65             use_stdin = 1;
     66         }
     67     }
     68 
     69     /* Create crash dump parser, open dump file, and parse it line by line. */
     70     NdkCrashParser* parser = CreateNdkCrashParser(stdout, sym_path);
     71     if (parser != NULL) {
     72         FILE* handle = use_stdin ? stdin : fopen(dump_file, "r");
     73         if (handle != NULL) {
     74             char str[2048];
     75             while (fgets(str, sizeof(str), handle)) {
     76                 /* ParseLine requires that there are no \r, or \n symbols in the
     77                  * string. */
     78                 str[strcspn(str, "\r\n")] = '\0';
     79                 ParseLine(parser, str);
     80             }
     81             fclose(handle);
     82         } else {
     83             fprintf(stderr, "Unable to open dump file %s: %s\n",
     84                     dump_file, strerror(errno));
     85         }
     86         DestroyNdkCrashParser(parser);
     87     } else {
     88         fprintf(stderr, "Unable to create NDK stack parser: %s\n",
     89                 strerror(errno));
     90     }
     91     return 0;
     92 }
     93 
     94