Port: Remove _GNU_SOURCE, defined in config.h
[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
6c1c0768 19#define _LGPL_SOURCE
d0254c7c
MD
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>
7e66b1b0
JRJ
29#include <assert.h>
30
31#include <common/mi-lttng.h>
d0254c7c 32
c399183f 33#include "../command.h"
d0254c7c
MD
34
35static int opt_event_type;
3ecec76a 36static int opt_kernel;
d0254c7c 37static int opt_userspace;
d0254c7c
MD
38
39enum {
40 OPT_HELP = 1,
d0254c7c
MD
41 OPT_TRACEPOINT,
42 OPT_MARKER,
43 OPT_PROBE,
44 OPT_FUNCTION,
45 OPT_FUNCTION_ENTRY,
a54bd42d 46 OPT_SYSCALL,
eeac7d46 47 OPT_USERSPACE,
3ecec76a 48 OPT_KERNEL,
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},
3ecec76a 58 {"kernel", 'k', POPT_ARG_NONE, 0, OPT_KERNEL, 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 {
3ecec76a
PP
103 /* Checked by the caller. */
104 assert(0);
d0254c7c
MD
105 }
106
cd80958d
DG
107 handle = lttng_create_handle(NULL, &dom);
108 if (handle == NULL) {
af87c45a 109 ret = CMD_ERROR;
4466912f 110 goto error;
cd80958d
DG
111 }
112
4466912f
DG
113 switch (opt_event_type) {
114 case LTTNG_EVENT_TRACEPOINT:
115 DBG("Calibrating kernel tracepoints");
116 break;
117 case LTTNG_EVENT_PROBE:
118 DBG("Calibrating kernel probes");
119 break;
120 case LTTNG_EVENT_FUNCTION:
121 DBG("Calibrating kernel functions");
122 calibrate.type = LTTNG_CALIBRATE_FUNCTION;
123 ret = lttng_calibrate(handle, &calibrate);
124 if (ret < 0) {
8f3f773f 125 ERR("%s", lttng_strerror(ret));
4466912f 126 goto error;
d0254c7c 127 }
4466912f
DG
128 MSG("%s calibration done", opt_kernel ? "Kernel" : "UST");
129 break;
130 case LTTNG_EVENT_FUNCTION_ENTRY:
131 DBG("Calibrating kernel function entry");
132 break;
133 case LTTNG_EVENT_SYSCALL:
134 DBG("Calibrating kernel syscall");
135 break;
136 default:
1ab1ea0b 137 ret = CMD_UNDEFINED;
4466912f 138 goto error;
d0254c7c 139 }
4466912f 140
7e66b1b0
JRJ
141 if (lttng_opt_mi) {
142 assert(writer);
143 ret = mi_lttng_calibrate(writer, &calibrate);
144 if (ret) {
145 ret = CMD_ERROR;
146 goto error;
147 }
148 }
af87c45a 149
4466912f 150error:
cd80958d
DG
151 lttng_destroy_handle(handle);
152
d0254c7c
MD
153 return ret;
154}
155
156/*
af87c45a 157 * Calibrate LTTng tracer.
1c8d13c8
TD
158 *
159 * Returns a CMD_* error.
d0254c7c
MD
160 */
161int cmd_calibrate(int argc, const char **argv)
162{
7e66b1b0 163 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
d0254c7c
MD
164 static poptContext pc;
165
166 pc = poptGetContext(NULL, argc, argv, long_options, 0);
167 poptReadDefaultConfig(pc, 0);
168
169 /* Default event type */
4466912f 170 opt_event_type = LTTNG_EVENT_FUNCTION;
d0254c7c
MD
171
172 while ((opt = poptGetNextOpt(pc)) != -1) {
173 switch (opt) {
174 case OPT_HELP:
af87c45a 175 usage(stdout);
d0254c7c 176 goto end;
d0254c7c 177 case OPT_TRACEPOINT:
1ab1ea0b 178 ret = CMD_UNDEFINED;
4466912f 179 goto end;
d0254c7c 180 case OPT_MARKER:
1ab1ea0b 181 ret = CMD_UNDEFINED;
d0254c7c
MD
182 goto end;
183 case OPT_PROBE:
1ab1ea0b 184 ret = CMD_UNDEFINED;
d0254c7c
MD
185 break;
186 case OPT_FUNCTION:
187 opt_event_type = LTTNG_EVENT_FUNCTION;
188 break;
189 case OPT_FUNCTION_ENTRY:
1ab1ea0b 190 ret = CMD_UNDEFINED;
4466912f 191 goto end;
a54bd42d 192 case OPT_SYSCALL:
1ab1ea0b 193 ret = CMD_UNDEFINED;
4466912f 194 goto end;
eeac7d46
MD
195 case OPT_USERSPACE:
196 opt_userspace = 1;
eeac7d46 197 break;
3ecec76a
PP
198 case OPT_KERNEL:
199 opt_kernel = 1;
200 break;
679b4943
SM
201 case OPT_LIST_OPTIONS:
202 list_cmd_options(stdout, long_options);
679b4943 203 goto end;
d0254c7c
MD
204 default:
205 usage(stderr);
206 ret = CMD_UNDEFINED;
207 goto end;
208 }
209 }
210
3ecec76a
PP
211 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
212 if (ret) {
213 ret = CMD_ERROR;
214 goto end;
215 }
216
7e66b1b0
JRJ
217 /* Mi check */
218 if (lttng_opt_mi) {
219 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
220 if (!writer) {
221 ret = -LTTNG_ERR_NOMEM;
222 goto end;
223 }
224
225 /* Open command element */
226 ret = mi_lttng_writer_command_open(writer,
227 mi_lttng_element_command_calibrate);
228 if (ret) {
229 ret = CMD_ERROR;
230 goto end;
231 }
232
233 /* Open output element */
234 ret = mi_lttng_writer_open_element(writer,
235 mi_lttng_element_command_output);
236 if (ret) {
237 ret = CMD_ERROR;
238 goto end;
239 }
240 }
241
242 command_ret = calibrate_lttng();
243 if (command_ret) {
244 success = 0;
245 }
246
247 /* Mi closing */
248 if (lttng_opt_mi) {
249 /* Close output element */
250 ret = mi_lttng_writer_close_element(writer);
251 if (ret) {
252 ret = CMD_ERROR;
253 goto end;
254 }
255
256 /* Success ? */
257 ret = mi_lttng_writer_write_element_bool(writer,
258 mi_lttng_element_command_success, success);
259 if (ret) {
260 ret = CMD_ERROR;
261 goto end;
262 }
263
264 /* Command element close */
265 ret = mi_lttng_writer_command_close(writer);
266 if (ret) {
267 ret = CMD_ERROR;
268 goto end;
269 }
270 }
d0254c7c
MD
271
272end:
7e66b1b0
JRJ
273 /* Mi clean-up */
274 if (writer && mi_lttng_writer_destroy(writer)) {
275 /* Preserve original error code */
276 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
277 }
278
279 /* Overwrite ret if an error occurred during calibrate_lttng() */
280 ret = command_ret ? command_ret : ret;
281
ca1c3607 282 poptFreeContext(pc);
d0254c7c
MD
283 return ret;
284}
This page took 0.06379 seconds and 5 git commands to generate.