Commit | Line | Data |
---|---|---|
fbd8191b PMF |
1 | #include <unistd.h> |
2 | #include <getopt.h> | |
3 | #include <stdlib.h> | |
4 | #include <stdio.h> | |
5 | #include <fcntl.h> | |
fbd8191b | 6 | |
f9e5ce61 | 7 | #include "ustcomm.h" |
fbd8191b | 8 | |
52c51a47 PMF |
9 | struct ust_opts { |
10 | char *cmd; | |
11 | pid_t *pids; | |
12 | int take_reply; | |
13 | }; | |
14 | ||
15 | int parse_opts_long(int argc, char **argv, struct ust_opts *opts) | |
fbd8191b | 16 | { |
52c51a47 PMF |
17 | int c; |
18 | int digit_optind = 0; | |
19 | ||
20 | opts->cmd = NULL; | |
21 | opts->pids = NULL; | |
22 | opts->take_reply = 0; | |
23 | ||
24 | while (1) { | |
25 | int this_option_optind = optind ? optind : 1; | |
26 | int option_index = 0; | |
27 | static struct option long_options[] = { | |
28 | {"start-trace", 0, 0, 1000}, | |
29 | {"stop-trace", 0, 0, 1001}, | |
30 | {"destroy-trace", 0, 0, 1002}, | |
31 | {"list-markers", 0, 0, 1004}, | |
32 | {"print-markers", 0, 0, 1005}, | |
33 | {"pid", 1, 0, 1006}, | |
34 | {"enable-marker", 1, 0, 1007}, | |
35 | {"disable-marker", 1, 0, 1008}, | |
36 | {"start", 0, 0, 1009}, | |
37 | {0, 0, 0, 0} | |
38 | }; | |
39 | ||
40 | c = getopt_long(argc, argv, "", long_options, &option_index); | |
41 | if (c == -1) | |
42 | break; | |
43 | ||
44 | switch (c) { | |
45 | case 0: | |
46 | printf("option %s", long_options[option_index].name); | |
47 | if (optarg) | |
48 | printf(" with arg %s", optarg); | |
49 | printf("\n"); | |
50 | break; | |
51 | ||
52 | case 1000: | |
53 | opts->cmd = strdup("trace_start"); | |
54 | break; | |
55 | case 1001: | |
56 | opts->cmd = strdup("trace_stop"); | |
57 | break; | |
58 | case 1009: | |
59 | opts->cmd = strdup("start"); | |
60 | break; | |
61 | case 1002: | |
62 | opts->cmd = strdup("trace_destroy"); | |
63 | break; | |
64 | case 1004: | |
65 | opts->cmd = strdup("list_markers"); | |
66 | opts->take_reply = 1; | |
67 | break; | |
68 | case 1007: | |
69 | asprintf(&opts->cmd, "enable_marker %s", optarg); | |
70 | break; | |
71 | case 1008: | |
72 | asprintf(&opts->cmd, "disable_marker %s", optarg); | |
73 | break; | |
74 | ||
75 | default: | |
76 | /* unknown option or other error; error is printed by getopt, just return */ | |
77 | return 1; | |
fbd8191b PMF |
78 | } |
79 | } | |
80 | ||
52c51a47 PMF |
81 | if(argc - optind > 0) { |
82 | int i; | |
83 | int pididx=0; | |
84 | opts->pids = malloc((argc-optind+1) * sizeof(pid_t)); | |
fbd8191b | 85 | |
52c51a47 PMF |
86 | for(i=optind; i<argc; i++) { |
87 | opts->pids[pididx++] = atoi(argv[i]); | |
88 | } | |
89 | opts->pids[pididx] = -1; | |
fbd8191b PMF |
90 | } |
91 | ||
52c51a47 PMF |
92 | return 0; |
93 | } | |
fbd8191b | 94 | |
52c51a47 | 95 | char *progname = NULL; |
fbd8191b | 96 | |
52c51a47 PMF |
97 | void usage(void) |
98 | { | |
99 | fprintf(stderr, "usage: %s [OPTIONS] OPERATION PID...\n", progname); | |
100 | fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\ | |
101 | \n\ | |
102 | Operations:\n\ | |
103 | \t--start-trace\tStart tracing\n\ | |
104 | \t--stop-trace\tStop tracing\n\ | |
105 | \t--destroy-trace\tDestroy the trace\n\ | |
106 | \t--enable-marker CHANNEL_NAME/MARKER_NAME\tEnable a marker\n\ | |
107 | \t--disable-marker CHANNEL_NAME/MARKER_NAME\tDisable a marker\n\ | |
108 | \t--list-markers\tList the markers of the process and their state\n\ | |
109 | \n\ | |
110 | "); | |
fbd8191b PMF |
111 | } |
112 | ||
fbd8191b PMF |
113 | int main(int argc, char *argv[]) |
114 | { | |
52c51a47 PMF |
115 | pid_t *pidit; |
116 | //char *msg = argv[2]; | |
117 | struct ustcomm_connection conn; | |
118 | int result; | |
119 | struct ust_opts opts; | |
fbd8191b | 120 | |
52c51a47 | 121 | progname = argv[0]; |
fbd8191b | 122 | |
52c51a47 PMF |
123 | if(argc <= 1) { |
124 | fprintf(stderr, "No operation specified.\n"); | |
125 | usage(); | |
126 | exit(EXIT_FAILURE); | |
127 | } | |
128 | ||
129 | result = parse_opts_long(argc, argv, &opts); | |
130 | if(result) { | |
131 | usage(); | |
132 | exit(EXIT_FAILURE); | |
133 | } | |
134 | ||
135 | if(opts.pids == NULL) { | |
136 | fprintf(stderr, "No pid specified.\n"); | |
137 | usage(); | |
138 | exit(EXIT_FAILURE); | |
139 | } | |
140 | if(opts.cmd == NULL) { | |
141 | fprintf(stderr, "No command specified.\n"); | |
142 | usage(); | |
143 | exit(EXIT_FAILURE); | |
144 | } | |
145 | ||
146 | pidit = opts.pids; | |
147 | ||
148 | while(*pidit != -1) { | |
149 | char *reply; | |
150 | char **preply; | |
151 | ||
152 | if(opts.take_reply) | |
153 | preply = &reply; | |
154 | else | |
155 | preply = NULL; | |
156 | ||
157 | result = ustcomm_connect_app(*pidit, &conn); | |
158 | if(result) { | |
159 | fprintf(stderr, "error connecting to process\n"); | |
160 | exit(EXIT_FAILURE); | |
161 | } | |
162 | ustcomm_send_request(&conn, opts.cmd, preply); | |
163 | ||
164 | if(opts.take_reply) | |
165 | printf("%s", reply); | |
166 | pidit++; | |
167 | } | |
82b1a169 | 168 | |
52c51a47 PMF |
169 | free(opts.pids); |
170 | free(opts.cmd); | |
fbd8191b PMF |
171 | |
172 | return 0; | |
173 | } |