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;
3ecec76a 37static int opt_kernel;
d0254c7c 38static int opt_userspace;
d0254c7c
MD
39
40enum {
41 OPT_HELP = 1,
d0254c7c
MD
42 OPT_TRACEPOINT,
43 OPT_MARKER,
44 OPT_PROBE,
45 OPT_FUNCTION,
46 OPT_FUNCTION_ENTRY,
a54bd42d 47 OPT_SYSCALL,
eeac7d46 48 OPT_USERSPACE,
3ecec76a 49 OPT_KERNEL,
679b4943 50 OPT_LIST_OPTIONS,
d0254c7c
MD
51};
52
cd80958d 53static struct lttng_handle *handle;
7e66b1b0 54static struct mi_writer *writer;
cd80958d 55
d0254c7c
MD
56static struct poptOption long_options[] = {
57 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
58 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
3ecec76a 59 {"kernel", 'k', POPT_ARG_NONE, 0, OPT_KERNEL, 0, 0},
4466912f 60 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
d0254c7c 61 {"function", 0, POPT_ARG_NONE, 0, OPT_FUNCTION, 0, 0},
679b4943 62 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
d0254c7c
MD
63 {0, 0, 0, 0, 0, 0, 0}
64};
65
66/*
67 * usage
68 */
69static void usage(FILE *ofp)
70{
32a6298d 71 fprintf(ofp, "usage: lttng calibrate [-k|-u] [OPTIONS]\n");
d0254c7c 72 fprintf(ofp, "\n");
32a6298d 73 fprintf(ofp, "Options:\n");
d0254c7c 74 fprintf(ofp, " -h, --help Show this help\n");
679b4943 75 fprintf(ofp, " --list-options Simple listing of options\n");
af87c45a 76 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
af87c45a 77 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
d0254c7c
MD
78 fprintf(ofp, "\n");
79 fprintf(ofp, "Calibrate options:\n");
32a6298d 80 fprintf(ofp, " --function Dynamic function entry/return probe (default)\n");
d0254c7c
MD
81 fprintf(ofp, "\n");
82}
83
84/*
af87c45a 85 * Calibrate LTTng.
d0254c7c 86 *
af87c45a 87 * Returns a CMD_* error.
d0254c7c
MD
88 */
89static int calibrate_lttng(void)
90{
91 int ret = CMD_SUCCESS;
92 struct lttng_domain dom;
93 struct lttng_calibrate calibrate;
94
441c16a7
MD
95 memset(&dom, 0, sizeof(dom));
96 memset(&calibrate, 0, sizeof(calibrate));
97
d0254c7c
MD
98 /* Create lttng domain */
99 if (opt_kernel) {
100 dom.type = LTTNG_DOMAIN_KERNEL;
4466912f
DG
101 } else if (opt_userspace) {
102 dom.type = LTTNG_DOMAIN_UST;
103 } else {
3ecec76a
PP
104 /* Checked by the caller. */
105 assert(0);
d0254c7c
MD
106 }
107
cd80958d
DG
108 handle = lttng_create_handle(NULL, &dom);
109 if (handle == NULL) {
af87c45a 110 ret = CMD_ERROR;
4466912f 111 goto error;
cd80958d
DG
112 }
113
4466912f
DG
114 switch (opt_event_type) {
115 case LTTNG_EVENT_TRACEPOINT:
116 DBG("Calibrating kernel tracepoints");
117 break;
118 case LTTNG_EVENT_PROBE:
119 DBG("Calibrating kernel probes");
120 break;
121 case LTTNG_EVENT_FUNCTION:
122 DBG("Calibrating kernel functions");
123 calibrate.type = LTTNG_CALIBRATE_FUNCTION;
124 ret = lttng_calibrate(handle, &calibrate);
125 if (ret < 0) {
8f3f773f 126 ERR("%s", lttng_strerror(ret));
4466912f 127 goto error;
d0254c7c 128 }
4466912f
DG
129 MSG("%s calibration done", opt_kernel ? "Kernel" : "UST");
130 break;
131 case LTTNG_EVENT_FUNCTION_ENTRY:
132 DBG("Calibrating kernel function entry");
133 break;
134 case LTTNG_EVENT_SYSCALL:
135 DBG("Calibrating kernel syscall");
136 break;
137 default:
1ab1ea0b 138 ret = CMD_UNDEFINED;
4466912f 139 goto error;
d0254c7c 140 }
4466912f 141
7e66b1b0
JRJ
142 if (lttng_opt_mi) {
143 assert(writer);
144 ret = mi_lttng_calibrate(writer, &calibrate);
145 if (ret) {
146 ret = CMD_ERROR;
147 goto error;
148 }
149 }
af87c45a 150
4466912f 151error:
cd80958d
DG
152 lttng_destroy_handle(handle);
153
d0254c7c
MD
154 return ret;
155}
156
157/*
af87c45a 158 * Calibrate LTTng tracer.
1c8d13c8
TD
159 *
160 * Returns a CMD_* error.
d0254c7c
MD
161 */
162int cmd_calibrate(int argc, const char **argv)
163{
7e66b1b0 164 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
d0254c7c
MD
165 static poptContext pc;
166
167 pc = poptGetContext(NULL, argc, argv, long_options, 0);
168 poptReadDefaultConfig(pc, 0);
169
170 /* Default event type */
4466912f 171 opt_event_type = LTTNG_EVENT_FUNCTION;
d0254c7c
MD
172
173 while ((opt = poptGetNextOpt(pc)) != -1) {
174 switch (opt) {
175 case OPT_HELP:
af87c45a 176 usage(stdout);
d0254c7c 177 goto end;
d0254c7c 178 case OPT_TRACEPOINT:
1ab1ea0b 179 ret = CMD_UNDEFINED;
4466912f 180 goto end;
d0254c7c 181 case OPT_MARKER:
1ab1ea0b 182 ret = CMD_UNDEFINED;
d0254c7c
MD
183 goto end;
184 case OPT_PROBE:
1ab1ea0b 185 ret = CMD_UNDEFINED;
d0254c7c
MD
186 break;
187 case OPT_FUNCTION:
188 opt_event_type = LTTNG_EVENT_FUNCTION;
189 break;
190 case OPT_FUNCTION_ENTRY:
1ab1ea0b 191 ret = CMD_UNDEFINED;
4466912f 192 goto end;
a54bd42d 193 case OPT_SYSCALL:
1ab1ea0b 194 ret = CMD_UNDEFINED;
4466912f 195 goto end;
eeac7d46
MD
196 case OPT_USERSPACE:
197 opt_userspace = 1;
eeac7d46 198 break;
3ecec76a
PP
199 case OPT_KERNEL:
200 opt_kernel = 1;
201 break;
679b4943
SM
202 case OPT_LIST_OPTIONS:
203 list_cmd_options(stdout, long_options);
679b4943 204 goto end;
d0254c7c
MD
205 default:
206 usage(stderr);
207 ret = CMD_UNDEFINED;
208 goto end;
209 }
210 }
211
3ecec76a
PP
212 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
213 if (ret) {
214 ret = CMD_ERROR;
215 goto end;
216 }
217
7e66b1b0
JRJ
218 /* Mi check */
219 if (lttng_opt_mi) {
220 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
221 if (!writer) {
222 ret = -LTTNG_ERR_NOMEM;
223 goto end;
224 }
225
226 /* Open command element */
227 ret = mi_lttng_writer_command_open(writer,
228 mi_lttng_element_command_calibrate);
229 if (ret) {
230 ret = CMD_ERROR;
231 goto end;
232 }
233
234 /* Open output element */
235 ret = mi_lttng_writer_open_element(writer,
236 mi_lttng_element_command_output);
237 if (ret) {
238 ret = CMD_ERROR;
239 goto end;
240 }
241 }
242
243 command_ret = calibrate_lttng();
244 if (command_ret) {
245 success = 0;
246 }
247
248 /* Mi closing */
249 if (lttng_opt_mi) {
250 /* Close output element */
251 ret = mi_lttng_writer_close_element(writer);
252 if (ret) {
253 ret = CMD_ERROR;
254 goto end;
255 }
256
257 /* Success ? */
258 ret = mi_lttng_writer_write_element_bool(writer,
259 mi_lttng_element_command_success, success);
260 if (ret) {
261 ret = CMD_ERROR;
262 goto end;
263 }
264
265 /* Command element close */
266 ret = mi_lttng_writer_command_close(writer);
267 if (ret) {
268 ret = CMD_ERROR;
269 goto end;
270 }
271 }
d0254c7c
MD
272
273end:
7e66b1b0
JRJ
274 /* Mi clean-up */
275 if (writer && mi_lttng_writer_destroy(writer)) {
276 /* Preserve original error code */
277 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
278 }
279
280 /* Overwrite ret if an error occurred during calibrate_lttng() */
281 ret = command_ret ? command_ret : ret;
282
ca1c3607 283 poptFreeContext(pc);
d0254c7c
MD
284 return ret;
285}
This page took 0.063207 seconds and 5 git commands to generate.