2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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.
25 #include <sys/types.h>
31 #include <common/mi-lttng.h>
33 #include "../command.h"
35 static int opt_event_type
;
36 static char *opt_kernel
;
37 static int opt_userspace
;
39 /* Not implemented yet */
40 static char *opt_cmd_name
;
56 static struct lttng_handle
*handle
;
57 static struct mi_writer
*writer
;
59 static struct poptOption long_options
[] = {
60 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
61 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
62 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
64 /* Not implemented yet */
65 {"userspace", 'u', POPT_ARG_STRING
| POPT_ARGFLAG_OPTIONAL
, &opt_cmd_name
, OPT_USERSPACE
, 0, 0},
66 {"pid", 'p', POPT_ARG_INT
, &opt_pid
, 0, 0, 0},
67 {"tracepoint", 0, POPT_ARG_NONE
, 0, OPT_TRACEPOINT
, 0, 0},
68 {"marker", 0, POPT_ARG_NONE
, 0, OPT_MARKER
, 0, 0},
69 {"probe", 0, POPT_ARG_NONE
, 0, OPT_PROBE
, 0, 0},
71 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
72 {"function", 0, POPT_ARG_NONE
, 0, OPT_FUNCTION
, 0, 0},
76 * Removed from options to discourage its use. Not in kernel
79 {"function:entry", 0, POPT_ARG_NONE
, 0, OPT_FUNCTION_ENTRY
, 0, 0},
80 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_SYSCALL
, 0, 0},
82 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
89 static void usage(FILE *ofp
)
91 fprintf(ofp
, "usage: lttng calibrate [-k|-u] [OPTIONS]\n");
93 fprintf(ofp
, "Options:\n");
94 fprintf(ofp
, " -h, --help Show this help\n");
95 fprintf(ofp
, " --list-options Simple listing of options\n");
96 fprintf(ofp
, " -k, --kernel Apply to the kernel tracer\n");
97 fprintf(ofp
, " -u, --userspace Apply to the user-space tracer\n");
99 fprintf(ofp
, "Calibrate options:\n");
100 fprintf(ofp
, " --function Dynamic function entry/return probe (default)\n");
102 fprintf(ofp
, " --tracepoint Tracepoint event (default)\n");
103 fprintf(ofp
, " --probe\n");
104 fprintf(ofp
, " Dynamic probe.\n");
106 fprintf(ofp
, " --function:entry symbol\n");
107 fprintf(ofp
, " Function tracer event\n");
109 fprintf(ofp
, " --syscall System call eventl\n");
110 fprintf(ofp
, " --marker User-space marker (deprecated)\n");
118 * Returns a CMD_* error.
120 static int calibrate_lttng(void)
122 int ret
= CMD_SUCCESS
;
123 struct lttng_domain dom
;
124 struct lttng_calibrate calibrate
;
126 memset(&dom
, 0, sizeof(dom
));
127 memset(&calibrate
, 0, sizeof(calibrate
));
129 /* Create lttng domain */
131 dom
.type
= LTTNG_DOMAIN_KERNEL
;
132 } else if (opt_userspace
) {
133 dom
.type
= LTTNG_DOMAIN_UST
;
135 print_missing_domain();
140 handle
= lttng_create_handle(NULL
, &dom
);
141 if (handle
== NULL
) {
146 switch (opt_event_type
) {
147 case LTTNG_EVENT_TRACEPOINT
:
148 DBG("Calibrating kernel tracepoints");
150 case LTTNG_EVENT_PROBE
:
151 DBG("Calibrating kernel probes");
153 case LTTNG_EVENT_FUNCTION
:
154 DBG("Calibrating kernel functions");
155 calibrate
.type
= LTTNG_CALIBRATE_FUNCTION
;
156 ret
= lttng_calibrate(handle
, &calibrate
);
158 ERR("%s", lttng_strerror(ret
));
161 MSG("%s calibration done", opt_kernel
? "Kernel" : "UST");
163 case LTTNG_EVENT_FUNCTION_ENTRY
:
164 DBG("Calibrating kernel function entry");
166 case LTTNG_EVENT_SYSCALL
:
167 DBG("Calibrating kernel syscall");
176 ret
= mi_lttng_calibrate(writer
, &calibrate
);
184 lttng_destroy_handle(handle
);
190 * Calibrate LTTng tracer.
192 * Returns a CMD_* error.
194 int cmd_calibrate(int argc
, const char **argv
)
196 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
197 static poptContext pc
;
199 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
200 poptReadDefaultConfig(pc
, 0);
202 /* Default event type */
203 opt_event_type
= LTTNG_EVENT_FUNCTION
;
205 while ((opt
= poptGetNextOpt(pc
)) != -1) {
220 opt_event_type
= LTTNG_EVENT_FUNCTION
;
222 case OPT_FUNCTION_ENTRY
:
231 case OPT_LIST_OPTIONS
:
232 list_cmd_options(stdout
, long_options
);
243 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
245 ret
= -LTTNG_ERR_NOMEM
;
249 /* Open command element */
250 ret
= mi_lttng_writer_command_open(writer
,
251 mi_lttng_element_command_calibrate
);
257 /* Open output element */
258 ret
= mi_lttng_writer_open_element(writer
,
259 mi_lttng_element_command_output
);
266 command_ret
= calibrate_lttng();
273 /* Close output element */
274 ret
= mi_lttng_writer_close_element(writer
);
281 ret
= mi_lttng_writer_write_element_bool(writer
,
282 mi_lttng_element_command_success
, success
);
288 /* Command element close */
289 ret
= mi_lttng_writer_command_close(writer
);
298 if (writer
&& mi_lttng_writer_destroy(writer
)) {
299 /* Preserve original error code */
300 ret
= ret
? ret
: -LTTNG_ERR_MI_IO_FAIL
;
303 /* Overwrite ret if an error occurred during calibrate_lttng() */
304 ret
= command_ret
? command_ret
: ret
;