lttng: show man page when using command's --help
[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 _LGPL_SOURCE
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>
29 #include <assert.h>
30
31 #include <common/mi-lttng.h>
32
33 #include "../command.h"
34
35 static int opt_event_type;
36 static int opt_kernel;
37 static int opt_userspace;
38
39 enum {
40 OPT_HELP = 1,
41 OPT_TRACEPOINT,
42 OPT_MARKER,
43 OPT_PROBE,
44 OPT_FUNCTION,
45 OPT_FUNCTION_ENTRY,
46 OPT_SYSCALL,
47 OPT_USERSPACE,
48 OPT_KERNEL,
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_NONE, 0, OPT_KERNEL, 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 /* Checked by the caller. */
104 assert(0);
105 }
106
107 handle = lttng_create_handle(NULL, &dom);
108 if (handle == NULL) {
109 ret = CMD_ERROR;
110 goto error;
111 }
112
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) {
125 ERR("%s", lttng_strerror(ret));
126 goto error;
127 }
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:
137 ret = CMD_UNDEFINED;
138 goto error;
139 }
140
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 }
149
150 error:
151 lttng_destroy_handle(handle);
152
153 return ret;
154 }
155
156 /*
157 * Calibrate LTTng tracer.
158 *
159 * Returns a CMD_* error.
160 */
161 int cmd_calibrate(int argc, const char **argv)
162 {
163 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
164 static poptContext pc;
165
166 pc = poptGetContext(NULL, argc, argv, long_options, 0);
167 poptReadDefaultConfig(pc, 0);
168
169 /* Default event type */
170 opt_event_type = LTTNG_EVENT_FUNCTION;
171
172 while ((opt = poptGetNextOpt(pc)) != -1) {
173 switch (opt) {
174 case OPT_HELP:
175 SHOW_HELP();
176 goto end;
177 case OPT_TRACEPOINT:
178 ret = CMD_UNDEFINED;
179 goto end;
180 case OPT_MARKER:
181 ret = CMD_UNDEFINED;
182 goto end;
183 case OPT_PROBE:
184 ret = CMD_UNDEFINED;
185 break;
186 case OPT_FUNCTION:
187 opt_event_type = LTTNG_EVENT_FUNCTION;
188 break;
189 case OPT_FUNCTION_ENTRY:
190 ret = CMD_UNDEFINED;
191 goto end;
192 case OPT_SYSCALL:
193 ret = CMD_UNDEFINED;
194 goto end;
195 case OPT_USERSPACE:
196 opt_userspace = 1;
197 break;
198 case OPT_KERNEL:
199 opt_kernel = 1;
200 break;
201 case OPT_LIST_OPTIONS:
202 list_cmd_options(stdout, long_options);
203 goto end;
204 default:
205 usage(stderr);
206 ret = CMD_UNDEFINED;
207 goto end;
208 }
209 }
210
211 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
212 if (ret) {
213 ret = CMD_ERROR;
214 goto end;
215 }
216
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 }
271
272 end:
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
282 poptFreeContext(pc);
283 return ret;
284 }
This page took 0.035072 seconds and 4 git commands to generate.