d957cb3ac9068ec69db594936a02a1107adcb932
[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
38 opts->cmd = NULL;
39 opts->pids = NULL;
40 opts->take_reply = 0;
41
42 while (1) {
43 int option_index = 0;
44 static struct option long_options[] = {
45 {"start-trace", 0, 0, 1000},
46 {"stop-trace", 0, 0, 1001},
47 {"destroy-trace", 0, 0, 1002},
48 {"list-markers", 0, 0, 1004},
49 {"print-markers", 0, 0, 1005},
50 {"pid", 1, 0, 1006},
51 {"enable-marker", 1, 0, 1007},
52 {"disable-marker", 1, 0, 1008},
53 {"start", 0, 0, 1009},
54 {"help", 0, 0, 'h'},
55 {"version", 0, 0, 1010},
56 {0, 0, 0, 0}
57 };
58
59 c = getopt_long(argc, argv, "h", long_options, &option_index);
60 if (c == -1)
61 break;
62
63 switch (c) {
64 case 0:
65 printf("option %s", long_options[option_index].name);
66 if (optarg)
67 printf(" with arg %s", optarg);
68 printf("\n");
69 break;
70
71 case 1000:
72 opts->cmd = strdup("trace_start");
73 break;
74 case 1001:
75 opts->cmd = strdup("trace_stop");
76 break;
77 case 1009:
78 opts->cmd = strdup("start");
79 break;
80 case 1002:
81 opts->cmd = strdup("trace_destroy");
82 break;
83 case 1004:
84 opts->cmd = strdup("list_markers");
85 opts->take_reply = 1;
86 break;
87 case 1007:
88 asprintf(&opts->cmd, "enable_marker %s", optarg);
89 break;
90 case 1008:
91 asprintf(&opts->cmd, "disable_marker %s", optarg);
92 break;
93 case 'h':
94 usage();
95 exit(0);
96 case 1010:
97 printf("Version 0\n");
98
99 default:
100 /* unknown option or other error; error is printed by getopt, just return */
101 return 1;
102 }
103 }
104
105 if(argc - optind > 0) {
106 int i;
107 int pididx=0;
108 opts->pids = malloc((argc-optind+1) * sizeof(pid_t));
109
110 for(i=optind; i<argc; i++) {
111 opts->pids[pididx++] = atoi(argv[i]);
112 }
113 opts->pids[pididx] = -1;
114 }
115
116 return 0;
117 }
118
119 int main(int argc, char *argv[])
120 {
121 pid_t *pidit;
122 //char *msg = argv[2];
123 struct ustcomm_connection conn;
124 int result;
125 struct ust_opts opts;
126
127 progname = argv[0];
128
129 if(argc <= 1) {
130 fprintf(stderr, "No operation specified.\n");
131 usage();
132 exit(EXIT_FAILURE);
133 }
134
135 result = parse_opts_long(argc, argv, &opts);
136 if(result) {
137 usage();
138 exit(EXIT_FAILURE);
139 }
140
141 if(opts.pids == NULL) {
142 fprintf(stderr, "No pid specified.\n");
143 usage();
144 exit(EXIT_FAILURE);
145 }
146 if(opts.cmd == NULL) {
147 fprintf(stderr, "No command specified.\n");
148 usage();
149 exit(EXIT_FAILURE);
150 }
151
152 pidit = opts.pids;
153
154 while(*pidit != -1) {
155 char *reply;
156 char **preply;
157
158 if(opts.take_reply)
159 preply = &reply;
160 else
161 preply = NULL;
162
163 result = ustcomm_connect_app(*pidit, &conn);
164 if(result) {
165 fprintf(stderr, "error connecting to process\n");
166 exit(EXIT_FAILURE);
167 }
168 ustcomm_send_request(&conn, opts.cmd, preply);
169
170 if(opts.take_reply)
171 printf("%s", reply);
172 pidit++;
173 }
174
175 free(opts.pids);
176 free(opts.cmd);
177
178 return 0;
179 }
This page took 0.031874 seconds and 3 git commands to generate.