Fix: error on no/multiple domain options
[lttng-tools.git] / src / bin / lttng / commands / calibrate.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <popt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <inttypes.h>
28 #include <ctype.h>
29 #include <assert.h>
30
31 #include <common/mi-lttng.h>
32
33 #include "../command.h"
34
35 static int opt_event_type;
36 static int opt_kernel;
37 static int opt_userspace;
38 #if 0
39 /* Not implemented yet */
40 static char *opt_cmd_name;
41 static pid_t opt_pid;
42 #endif
43
44 enum {
45 OPT_HELP = 1,
46 OPT_TRACEPOINT,
47 OPT_MARKER,
48 OPT_PROBE,
49 OPT_FUNCTION,
50 OPT_FUNCTION_ENTRY,
51 OPT_SYSCALL,
52 OPT_USERSPACE,
53 OPT_KERNEL,
54 OPT_LIST_OPTIONS,
55 };
56
57 static struct lttng_handle *handle;
58 static struct mi_writer *writer;
59
60 static struct poptOption long_options[] = {
61 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
62 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
63 #if 0
64 /* Not implemented yet */
65 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
66 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
67 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
68 {"marker", 0, POPT_ARG_NONE, 0, OPT_MARKER, 0, 0},
69 {"probe", 0, POPT_ARG_NONE, 0, OPT_PROBE, 0, 0},
70 #else
71 {"kernel", 'k', POPT_ARG_NONE, 0, OPT_KERNEL, 0, 0},
72 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
73 {"function", 0, POPT_ARG_NONE, 0, OPT_FUNCTION, 0, 0},
74 #endif
75 #if 0
76 /*
77 * Removed from options to discourage its use. Not in kernel
78 * tracer anymore.
79 */
80 {"function:entry", 0, POPT_ARG_NONE, 0, OPT_FUNCTION_ENTRY, 0, 0},
81 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
82 #endif
83 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
84 {0, 0, 0, 0, 0, 0, 0}
85 };
86
87 /*
88 * usage
89 */
90 static void usage(FILE *ofp)
91 {
92 fprintf(ofp, "usage: lttng calibrate [-k|-u] [OPTIONS]\n");
93 fprintf(ofp, "\n");
94 fprintf(ofp, "Options:\n");
95 fprintf(ofp, " -h, --help Show this help\n");
96 fprintf(ofp, " --list-options Simple listing of options\n");
97 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
98 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
99 fprintf(ofp, "\n");
100 fprintf(ofp, "Calibrate options:\n");
101 fprintf(ofp, " --function Dynamic function entry/return probe (default)\n");
102 #if 0
103 fprintf(ofp, " --tracepoint Tracepoint event (default)\n");
104 fprintf(ofp, " --probe\n");
105 fprintf(ofp, " Dynamic probe.\n");
106 #if 0
107 fprintf(ofp, " --function:entry symbol\n");
108 fprintf(ofp, " Function tracer event\n");
109 #endif
110 fprintf(ofp, " --syscall System call eventl\n");
111 fprintf(ofp, " --marker User-space marker (deprecated)\n");
112 #endif
113 fprintf(ofp, "\n");
114 }
115
116 /*
117 * Calibrate LTTng.
118 *
119 * Returns a CMD_* error.
120 */
121 static int calibrate_lttng(void)
122 {
123 int ret = CMD_SUCCESS;
124 struct lttng_domain dom;
125 struct lttng_calibrate calibrate;
126
127 memset(&dom, 0, sizeof(dom));
128 memset(&calibrate, 0, sizeof(calibrate));
129
130 /* Create lttng domain */
131 if (opt_kernel) {
132 dom.type = LTTNG_DOMAIN_KERNEL;
133 } else if (opt_userspace) {
134 dom.type = LTTNG_DOMAIN_UST;
135 } else {
136 /* Checked by the caller. */
137 assert(0);
138 }
139
140 handle = lttng_create_handle(NULL, &dom);
141 if (handle == NULL) {
142 ret = CMD_ERROR;
143 goto error;
144 }
145
146 switch (opt_event_type) {
147 case LTTNG_EVENT_TRACEPOINT:
148 DBG("Calibrating kernel tracepoints");
149 break;
150 case LTTNG_EVENT_PROBE:
151 DBG("Calibrating kernel probes");
152 break;
153 case LTTNG_EVENT_FUNCTION:
154 DBG("Calibrating kernel functions");
155 calibrate.type = LTTNG_CALIBRATE_FUNCTION;
156 ret = lttng_calibrate(handle, &calibrate);
157 if (ret < 0) {
158 ERR("%s", lttng_strerror(ret));
159 goto error;
160 }
161 MSG("%s calibration done", opt_kernel ? "Kernel" : "UST");
162 break;
163 case LTTNG_EVENT_FUNCTION_ENTRY:
164 DBG("Calibrating kernel function entry");
165 break;
166 case LTTNG_EVENT_SYSCALL:
167 DBG("Calibrating kernel syscall");
168 break;
169 default:
170 ret = CMD_UNDEFINED;
171 goto error;
172 }
173
174 if (lttng_opt_mi) {
175 assert(writer);
176 ret = mi_lttng_calibrate(writer, &calibrate);
177 if (ret) {
178 ret = CMD_ERROR;
179 goto error;
180 }
181 }
182
183 error:
184 lttng_destroy_handle(handle);
185
186 return ret;
187 }
188
189 /*
190 * Calibrate LTTng tracer.
191 *
192 * Returns a CMD_* error.
193 */
194 int cmd_calibrate(int argc, const char **argv)
195 {
196 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
197 static poptContext pc;
198
199 pc = poptGetContext(NULL, argc, argv, long_options, 0);
200 poptReadDefaultConfig(pc, 0);
201
202 /* Default event type */
203 opt_event_type = LTTNG_EVENT_FUNCTION;
204
205 while ((opt = poptGetNextOpt(pc)) != -1) {
206 switch (opt) {
207 case OPT_HELP:
208 usage(stdout);
209 goto end;
210 case OPT_TRACEPOINT:
211 ret = CMD_UNDEFINED;
212 goto end;
213 case OPT_MARKER:
214 ret = CMD_UNDEFINED;
215 goto end;
216 case OPT_PROBE:
217 ret = CMD_UNDEFINED;
218 break;
219 case OPT_FUNCTION:
220 opt_event_type = LTTNG_EVENT_FUNCTION;
221 break;
222 case OPT_FUNCTION_ENTRY:
223 ret = CMD_UNDEFINED;
224 goto end;
225 case OPT_SYSCALL:
226 ret = CMD_UNDEFINED;
227 goto end;
228 case OPT_USERSPACE:
229 opt_userspace = 1;
230 break;
231 case OPT_KERNEL:
232 opt_kernel = 1;
233 break;
234 case OPT_LIST_OPTIONS:
235 list_cmd_options(stdout, long_options);
236 goto end;
237 default:
238 usage(stderr);
239 ret = CMD_UNDEFINED;
240 goto end;
241 }
242 }
243
244 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
245 if (ret) {
246 ret = CMD_ERROR;
247 goto end;
248 }
249
250 /* Mi check */
251 if (lttng_opt_mi) {
252 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
253 if (!writer) {
254 ret = -LTTNG_ERR_NOMEM;
255 goto end;
256 }
257
258 /* Open command element */
259 ret = mi_lttng_writer_command_open(writer,
260 mi_lttng_element_command_calibrate);
261 if (ret) {
262 ret = CMD_ERROR;
263 goto end;
264 }
265
266 /* Open output element */
267 ret = mi_lttng_writer_open_element(writer,
268 mi_lttng_element_command_output);
269 if (ret) {
270 ret = CMD_ERROR;
271 goto end;
272 }
273 }
274
275 command_ret = calibrate_lttng();
276 if (command_ret) {
277 success = 0;
278 }
279
280 /* Mi closing */
281 if (lttng_opt_mi) {
282 /* Close output element */
283 ret = mi_lttng_writer_close_element(writer);
284 if (ret) {
285 ret = CMD_ERROR;
286 goto end;
287 }
288
289 /* Success ? */
290 ret = mi_lttng_writer_write_element_bool(writer,
291 mi_lttng_element_command_success, success);
292 if (ret) {
293 ret = CMD_ERROR;
294 goto end;
295 }
296
297 /* Command element close */
298 ret = mi_lttng_writer_command_close(writer);
299 if (ret) {
300 ret = CMD_ERROR;
301 goto end;
302 }
303 }
304
305 end:
306 /* Mi clean-up */
307 if (writer && mi_lttng_writer_destroy(writer)) {
308 /* Preserve original error code */
309 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
310 }
311
312 /* Overwrite ret if an error occurred during calibrate_lttng() */
313 ret = command_ret ? command_ret : ret;
314
315 poptFreeContext(pc);
316 return ret;
317 }
This page took 0.035234 seconds and 4 git commands to generate.