Home | History | Annotate | Download | only in qtools
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <unistd.h>
      5 #include <ctype.h>
      6 #include "bitvector.h"
      7 #include "parse_options.h"
      8 #include "hash_table.h"
      9 
     10 const char *root = "";
     11 bool lump_kernel;
     12 bool lump_libraries;
     13 Bitvector pid_include_vector(32768);
     14 Bitvector pid_exclude_vector(32768);
     15 bool include_some_pids;
     16 bool exclude_some_pids;
     17 
     18 HashTable<int> excluded_procedures(2000);
     19 HashTable<int> included_procedures(2000);
     20 bool exclude_some_procedures;
     21 bool include_some_procedures;
     22 
     23 bool exclude_kernel_syms;
     24 bool exclude_library_syms;
     25 bool include_kernel_syms;
     26 bool include_library_syms;
     27 bool demangle = true;
     28 
     29 static const char *OptionsUsageStr =
     30     "  -e :kernel exclude all kernel symbols\n"
     31     "  -e :libs   exclude all library symbols\n"
     32     "  -e <func>  exclude function <func>\n"
     33     "  -e <pid>   exclude process <pid>\n"
     34     "  -i :kernel include all kernel symbols\n"
     35     "  -i :libs   include all library symbols\n"
     36     "  -i <func>  include function <func>\n"
     37     "  -i <pid>   include process <pid>\n"
     38     "  -l :kernel lump all the kernel symbols together\n"
     39     "  -l :libs   lump all the library symbols together\n"
     40     "  -m         do not demangle C++ symbols (m for 'mangle')\n"
     41     "  -r <root>  use <root> as the path for finding ELF executables\n"
     42     ;
     43 
     44 void OptionsUsage()
     45 {
     46     fprintf(stderr, "%s", OptionsUsageStr);
     47 }
     48 
     49 void ParseOptions(int argc, char **argv)
     50 {
     51     bool err = false;
     52     while (!err) {
     53         int opt = getopt(argc, argv, "+e:i:l:mr:");
     54         if (opt == -1)
     55             break;
     56         switch (opt) {
     57         case 'e':
     58             if (*optarg == ':') {
     59                 if (strcmp(optarg, ":kernel") == 0)
     60                     exclude_kernel_syms = true;
     61                 else if (strcmp(optarg, ":libs") == 0)
     62                     exclude_library_syms = true;
     63                 else
     64                     err = true;
     65                 excluded_procedures.Update(optarg, 1);
     66                 exclude_some_procedures = true;
     67             } else if (isdigit(*optarg)) {
     68                 int bitnum = atoi(optarg);
     69                 pid_exclude_vector.SetBit(bitnum);
     70                 exclude_some_pids = true;
     71             } else {
     72                 excluded_procedures.Update(optarg, 1);
     73                 exclude_some_procedures = true;
     74             }
     75             break;
     76         case 'i':
     77             if (*optarg == ':') {
     78                 if (strcmp(optarg, ":kernel") == 0)
     79                     include_kernel_syms = true;
     80                 else if (strcmp(optarg, ":libs") == 0)
     81                     include_library_syms = true;
     82                 else
     83                     err = true;
     84                 included_procedures.Update(optarg, 1);
     85                 include_some_procedures = true;
     86             } else if (isdigit(*optarg)) {
     87                 int bitnum = atoi(optarg);
     88                 pid_include_vector.SetBit(bitnum);
     89                 include_some_pids = true;
     90             } else {
     91                 included_procedures.Update(optarg, 1);
     92                 include_some_procedures = true;
     93             }
     94             break;
     95         case 'l':
     96             if (strcmp(optarg, ":kernel") == 0)
     97                 lump_kernel = true;
     98             else if (strcmp(optarg, ":libs") == 0)
     99                 lump_libraries = true;
    100             else
    101                 err = true;
    102             break;
    103         case 'm':
    104             demangle = false;
    105             break;
    106         case 'r':
    107             root = optarg;
    108             break;
    109         default:
    110             err = true;
    111             break;
    112         }
    113     }
    114 
    115     if (err) {
    116         Usage(argv[0]);
    117         exit(1);
    118     }
    119 }
    120