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