118a398578bacc8dc181d43456324b0909d0fc55
[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 #define _LGPL_SOURCE
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>
30 #include <assert.h>
31
32 #include <common/mi-lttng.h>
33
34 #include "../command.h"
35
36 static int opt_event_type;
37 static int opt_kernel;
38 static int opt_userspace;
39
40 enum {
41 OPT_HELP = 1,
42 OPT_TRACEPOINT,
43 OPT_MARKER,
44 OPT_PROBE,
45 OPT_FUNCTION,
46 OPT_FUNCTION_ENTRY,
47 OPT_SYSCALL,
48 OPT_USERSPACE,
49 OPT_KERNEL,
50 OPT_LIST_OPTIONS,
51 };
52
53 static struct lttng_handle *handle;
54 static struct mi_writer *writer;
55
56 static struct poptOption long_options[] = {
57 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
58 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
59 {"kernel", 'k', POPT_ARG_NONE, 0, OPT_KERNEL, 0, 0},
60 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
61 {"function", 0, POPT_ARG_NONE, 0, OPT_FUNCTION, 0, 0},
62 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
63 {0, 0, 0, 0, 0, 0, 0}
64 };
65
66 /*
67 * usage
68 */
69 static void usage(FILE *ofp)
70 {
71 fprintf(ofp, "usage: lttng calibrate [-k|-u] [OPTIONS]\n");
72 fprintf(ofp, "\n");
73 fprintf(ofp, "Options:\n");
74 fprintf(ofp, " -h, --help Show this help\n");
75 fprintf(ofp, " --list-options Simple listing of options\n");
76 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
77 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
78 fprintf(ofp, "\n");
79 fprintf(ofp, "Calibrate options:\n");
80 fprintf(ofp, " --function Dynamic function entry/return probe (default)\n");
81 fprintf(ofp, "\n");
82 }
83
84 /*
85 * Calibrate LTTng.
86 *
87 * Returns a CMD_* error.
88 */
89 static int calibrate_lttng(void)
90 {
91 int ret = CMD_SUCCESS;
92 struct lttng_domain dom;
93 struct lttng_calibrate calibrate;
94
95 memset(&dom, 0, sizeof(dom));
96 memset(&calibrate, 0, sizeof(calibrate));
97
98 /* Create lttng domain */
99 if (opt_kernel) {
100 dom.type = LTTNG_DOMAIN_KERNEL;
101 } else if (opt_userspace) {
102 dom.type = LTTNG_DOMAIN_UST;
103 } else {
104 /* Checked by the caller. */
105 assert(0);
106 }
107
108 handle = lttng_create_handle(NULL, &dom);
109 if (handle == NULL) {
110 ret = CMD_ERROR;
111 goto error;
112 }
113
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) {
126 ERR("%s", lttng_strerror(ret));
127 goto error;
128 }
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:
138 ret = CMD_UNDEFINED;
139 goto error;
140 }
141
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 }
150
151 error:
152 lttng_destroy_handle(handle);
153
154 return ret;
155 }
156
157 /*
158 * Calibrate LTTng tracer.
159 *
160 * Returns a CMD_* error.
161 */
162 int cmd_calibrate(int argc, const char **argv)
163 {
164 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
165 static poptContext pc;
166
167 pc = poptGetContext(NULL, argc, argv, long_options, 0);
168 poptReadDefaultConfig(pc, 0);
169
170 /* Default event type */
171 opt_event_type = LTTNG_EVENT_FUNCTION;
172
173 while ((opt = poptGetNextOpt(pc)) != -1) {
174 switch (opt) {
175 case OPT_HELP:
176 usage(stdout);
177 goto end;
178 case OPT_TRACEPOINT:
179 ret = CMD_UNDEFINED;
180 goto end;
181 case OPT_MARKER:
182 ret = CMD_UNDEFINED;
183 goto end;
184 case OPT_PROBE:
185 ret = CMD_UNDEFINED;
186 break;
187 case OPT_FUNCTION:
188 opt_event_type = LTTNG_EVENT_FUNCTION;
189 break;
190 case OPT_FUNCTION_ENTRY:
191 ret = CMD_UNDEFINED;
192 goto end;
193 case OPT_SYSCALL:
194 ret = CMD_UNDEFINED;
195 goto end;
196 case OPT_USERSPACE:
197 opt_userspace = 1;
198 break;
199 case OPT_KERNEL:
200 opt_kernel = 1;
201 break;
202 case OPT_LIST_OPTIONS:
203 list_cmd_options(stdout, long_options);
204 goto end;
205 default:
206 usage(stderr);
207 ret = CMD_UNDEFINED;
208 goto end;
209 }
210 }
211
212 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
213 if (ret) {
214 ret = CMD_ERROR;
215 goto end;
216 }
217
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 }
272
273 end:
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
283 poptFreeContext(pc);
284 return ret;
285 }
This page took 0.035309 seconds and 4 git commands to generate.