Remove dead code from the calibrate command
[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;
37static char *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,
679b4943 49 OPT_LIST_OPTIONS,
d0254c7c
MD
50};
51
cd80958d 52static struct lttng_handle *handle;
7e66b1b0 53static struct mi_writer *writer;
cd80958d 54
d0254c7c
MD
55static struct poptOption long_options[] = {
56 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
57 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
58 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
4466912f 59 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
d0254c7c 60 {"function", 0, POPT_ARG_NONE, 0, OPT_FUNCTION, 0, 0},
679b4943 61 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
d0254c7c
MD
62 {0, 0, 0, 0, 0, 0, 0}
63};
64
65/*
66 * usage
67 */
68static void usage(FILE *ofp)
69{
32a6298d 70 fprintf(ofp, "usage: lttng calibrate [-k|-u] [OPTIONS]\n");
d0254c7c 71 fprintf(ofp, "\n");
32a6298d 72 fprintf(ofp, "Options:\n");
d0254c7c 73 fprintf(ofp, " -h, --help Show this help\n");
679b4943 74 fprintf(ofp, " --list-options Simple listing of options\n");
af87c45a 75 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
af87c45a 76 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
d0254c7c
MD
77 fprintf(ofp, "\n");
78 fprintf(ofp, "Calibrate options:\n");
32a6298d 79 fprintf(ofp, " --function Dynamic function entry/return probe (default)\n");
d0254c7c
MD
80 fprintf(ofp, "\n");
81}
82
83/*
af87c45a 84 * Calibrate LTTng.
d0254c7c 85 *
af87c45a 86 * Returns a CMD_* error.
d0254c7c
MD
87 */
88static int calibrate_lttng(void)
89{
90 int ret = CMD_SUCCESS;
91 struct lttng_domain dom;
92 struct lttng_calibrate calibrate;
93
441c16a7
MD
94 memset(&dom, 0, sizeof(dom));
95 memset(&calibrate, 0, sizeof(calibrate));
96
d0254c7c
MD
97 /* Create lttng domain */
98 if (opt_kernel) {
99 dom.type = LTTNG_DOMAIN_KERNEL;
4466912f
DG
100 } else if (opt_userspace) {
101 dom.type = LTTNG_DOMAIN_UST;
102 } else {
b9dfb167 103 print_missing_domain();
d16c1a4c 104 ret = CMD_ERROR;
4466912f 105 goto error;
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;
679b4943
SM
199 case OPT_LIST_OPTIONS:
200 list_cmd_options(stdout, long_options);
679b4943 201 goto end;
d0254c7c
MD
202 default:
203 usage(stderr);
204 ret = CMD_UNDEFINED;
205 goto end;
206 }
207 }
208
7e66b1b0
JRJ
209 /* Mi check */
210 if (lttng_opt_mi) {
211 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
212 if (!writer) {
213 ret = -LTTNG_ERR_NOMEM;
214 goto end;
215 }
216
217 /* Open command element */
218 ret = mi_lttng_writer_command_open(writer,
219 mi_lttng_element_command_calibrate);
220 if (ret) {
221 ret = CMD_ERROR;
222 goto end;
223 }
224
225 /* Open output element */
226 ret = mi_lttng_writer_open_element(writer,
227 mi_lttng_element_command_output);
228 if (ret) {
229 ret = CMD_ERROR;
230 goto end;
231 }
232 }
233
234 command_ret = calibrate_lttng();
235 if (command_ret) {
236 success = 0;
237 }
238
239 /* Mi closing */
240 if (lttng_opt_mi) {
241 /* Close output element */
242 ret = mi_lttng_writer_close_element(writer);
243 if (ret) {
244 ret = CMD_ERROR;
245 goto end;
246 }
247
248 /* Success ? */
249 ret = mi_lttng_writer_write_element_bool(writer,
250 mi_lttng_element_command_success, success);
251 if (ret) {
252 ret = CMD_ERROR;
253 goto end;
254 }
255
256 /* Command element close */
257 ret = mi_lttng_writer_command_close(writer);
258 if (ret) {
259 ret = CMD_ERROR;
260 goto end;
261 }
262 }
d0254c7c
MD
263
264end:
7e66b1b0
JRJ
265 /* Mi clean-up */
266 if (writer && mi_lttng_writer_destroy(writer)) {
267 /* Preserve original error code */
268 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
269 }
270
271 /* Overwrite ret if an error occurred during calibrate_lttng() */
272 ret = command_ret ? command_ret : ret;
273
ca1c3607 274 poptFreeContext(pc);
d0254c7c
MD
275 return ret;
276}
This page took 0.049601 seconds and 4 git commands to generate.