revert to oldcode
[snitchaser:mainline.git] / snitchaser_args.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <error.h>
4 #include <argp.h>
5 #include <stdint.h>
6 #include "snitchaser_args.h"
7
8
9 const char *argp_program_version = "snitchaser-0.0";
10 const char *argp_program_bug_address = "<wangnan06@ict.ac.cn>";
11 static char doc[] =
12         "snitchaser: continuous log/replay";
13 static char args_doc[] =
14         "TARGET-EXECUTABLE [TARGET OPTIONS]";
15
16 static struct argp_option options[] = {
17         {"injectso",    'j', "filename",        0, "Set the system wrapper so file name, "
18                                                                                         "'./libwrapper.so' by default"},
19         {"bias",                'b', "bias",            0, "Set the inject so load bias, 0x3000 by default"},
20         {"syswrapper",  'w', "wrapsym",         0, "System call wrapper symbol, syscall_wrapper_entry by default"},
21         {"entry",               'E', "entry",           0, "entry, \'__entry\' by default"},
22         {"vsyscall",    'e', "vsyscall",        0, "symbol which hold syscall, \'__vsyscall\' by default"},
23         {"statevect",   'v', "statevect",       0, "state_vect's symbol, \'state_vector\' by default"},
24         {"loggersize",  's', "size",            0, "logger size threshold, 10MB by default, at least 4kB"},
25         {"sigreturn",   -1, "sigreturn sym",
26                 0, "wrapped_sigreturn symbol, \"wrapped_sigreturn\" by default"},
27         {"rt_sigreturn", -2, "rt_sigaction sym", 
28                 0, "wrapped_rt_sigreturn symbol, \"wrapped_rt_sigreturn\" by default"},
29         {"sighandler",   -5, "sighandler proxy sym",
30                 0, "wrapped_sighandler symbol, \"wrapped_sighandler\" by default"},
31         {"tracefork",   'f', NULL,                      0, "trace fork, default: off"},
32         {"injopts",             -3, "opts sym", 0, "injector opts symbol, \"injector_opts\" by default"},
33         {"untraced",    -4, NULL,                       0, "start untraced running. traced by default. "
34                 "This option is designed for debug use only."},
35         {NULL},
36 };
37
38 static struct opts opts = {
39         .inj_so         = "./libwrapper.so",
40         .wrap_sym       = "syscall_wrapper_entry",
41         .inj_bias       = 0x3000,
42         .entry          = "__entry",
43         .old_vsyscall   = "__vsyscall",
44         .state_vect             = "state_vector",
45         .injector_opts  = "injector_opts",
46         .sigreturn              = "wrapped_sigreturn",
47         .rt_sigreturn   = "wrapped_rt_sigreturn",
48         .sighandler             = "wrapped_sighandler",
49         .logger_threshold       = 10 << 20,
50         .trace_fork             = 0,
51         .untraced               = 0,
52 };
53
54 static error_t
55 parse_opt(int key, char *arg, struct argp_state *state)
56 {
57         static int found_target = 0;
58         switch (key) {
59                 case -4:
60                         opts.untraced = 1;
61                         return 0;
62                 case -3:
63                         opts.injector_opts = arg;
64                         return 0;
65                 case 'f':
66                         opts.trace_fork = 1;
67                         return 0;
68                 case -1:
69                         opts.sigreturn= arg;
70                         return 0;
71                 case -2:
72                         opts.rt_sigreturn = arg;
73                         return 0;
74                 case -5:
75                         opts.sighandler = arg;
76                         return 0;
77                 case 's':
78                         {
79                                 char * t, s;
80                                 errno = 0;
81                                 int sz = strtoul(arg, &t, 10);
82
83                                 s = toupper(*t);
84                                 if (s == 'K')
85                                         sz *= 1024;
86                                 if (s == 'M')
87                                         sz *= (1 << 20);
88
89                                 if ((sz < 4096) || (errno != 0)) {
90                                         printf("logger size parse error: at least 4kB\n");
91                                         argp_usage(state);
92                                         return EINVAL;
93                                 }
94
95                                 opts.logger_threshold = sz;
96                         }
97                         return 0;
98                 case 'j':
99                         opts.inj_so = arg;
100                         return 0;
101                 case 'b':
102                         opts.inj_bias = strtoul(arg, NULL, 0);
103                         return 0;
104                 case 'w':
105                         opts.wrap_sym = arg;
106                         return 0;
107                 case 'E':
108                         opts.entry = arg;
109                         return 0;
110                 case 'e':
111                         opts.old_vsyscall = arg;
112                         return 0;
113                 case 'v':
114                         opts.state_vect = arg;
115                         return 0;
116                 case ARGP_KEY_ARG:
117                         /* target args */
118                         found_target = 1;
119                         return ARGP_ERR_UNKNOWN;
120                 case ARGP_KEY_ARGS:
121                         /* consume all args */
122                         return ARGP_ERR_UNKNOWN;
123                 case ARGP_KEY_END:
124                         if (!found_target) {
125                                 argp_usage(state);
126                                 return EINVAL;
127                         }
128                         return 0;
129                 default:
130                         return ARGP_ERR_UNKNOWN;
131         }
132         return 0;
133 }
134
135 static struct argp argp = {options, parse_opt, args_doc, doc};
136
137 struct opts *
138 parse_args(int argc, char * argv[])
139 {
140         int idx;
141         int err;
142         err = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, &idx, NULL);
143         opts.cmd_idx = idx;
144         return &opts;
145 }
146
147 // vim:ts=4:sw=4
148