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