9a7eb0d40702b177edfd92c50bc964cb69420f9c
[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 char *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_LIST_OPTIONS,
50 };
51
52 static struct lttng_handle *handle;
53 static struct mi_writer *writer;
54
55 static 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},
59 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
60 {"function", 0, POPT_ARG_NONE, 0, OPT_FUNCTION, 0, 0},
61 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
62 {0, 0, 0, 0, 0, 0, 0}
63 };
64
65 /*
66 * usage
67 */
68 static void usage(FILE *ofp)
69 {
70 fprintf(ofp, "usage: lttng calibrate [-k|-u] [OPTIONS]\n");
71 fprintf(ofp, "\n");
72 fprintf(ofp, "Options:\n");
73 fprintf(ofp, " -h, --help Show this help\n");
74 fprintf(ofp, " --list-options Simple listing of options\n");
75 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
76 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
77 fprintf(ofp, "\n");
78 fprintf(ofp, "Calibrate options:\n");
79 fprintf(ofp, " --function Dynamic function entry/return probe (default)\n");
80 fprintf(ofp, "\n");
81 }
82
83 /*
84 * Calibrate LTTng.
85 *
86 * Returns a CMD_* error.
87 */
88 static int calibrate_lttng(void)
89 {
90 int ret = CMD_SUCCESS;
91 struct lttng_domain dom;
92 struct lttng_calibrate calibrate;
93
94 memset(&dom, 0, sizeof(dom));
95 memset(&calibrate, 0, sizeof(calibrate));
96
97 /* Create lttng domain */
98 if (opt_kernel) {
99 dom.type = LTTNG_DOMAIN_KERNEL;
100 } else if (opt_userspace) {
101 dom.type = LTTNG_DOMAIN_UST;
102 } else {
103 print_missing_domain();
104 ret = CMD_ERROR;
105 goto error;
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_LIST_OPTIONS:
200 list_cmd_options(stdout, long_options);
201 goto end;
202 default:
203 usage(stderr);
204 ret = CMD_UNDEFINED;
205 goto end;
206 }
207 }
208
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 }
263
264 end:
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
274 poptFreeContext(pc);
275 return ret;
276 }
This page took 0.033866 seconds and 3 git commands to generate.