Commit | Line | Data |
---|---|---|
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 | ||
19 | #define _GNU_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> | |
7e66b1b0 JRJ |
29 | #include <assert.h> |
30 | ||
31 | #include <common/mi-lttng.h> | |
d0254c7c | 32 | |
c399183f | 33 | #include "../command.h" |
d0254c7c MD |
34 | |
35 | static int opt_event_type; | |
36 | static char *opt_kernel; | |
d0254c7c | 37 | static int opt_userspace; |
d78d6610 DG |
38 | #if 0 |
39 | /* Not implemented yet */ | |
eeac7d46 | 40 | static char *opt_cmd_name; |
d0254c7c | 41 | static pid_t opt_pid; |
d78d6610 | 42 | #endif |
d0254c7c MD |
43 | |
44 | enum { | |
45 | OPT_HELP = 1, | |
d0254c7c MD |
46 | OPT_TRACEPOINT, |
47 | OPT_MARKER, | |
48 | OPT_PROBE, | |
49 | OPT_FUNCTION, | |
50 | OPT_FUNCTION_ENTRY, | |
a54bd42d | 51 | OPT_SYSCALL, |
eeac7d46 | 52 | OPT_USERSPACE, |
679b4943 | 53 | OPT_LIST_OPTIONS, |
d0254c7c MD |
54 | }; |
55 | ||
cd80958d | 56 | static struct lttng_handle *handle; |
7e66b1b0 | 57 | static struct mi_writer *writer; |
cd80958d | 58 | |
d0254c7c MD |
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}, | |
d78d6610 DG |
63 | #if 0 |
64 | /* Not implemented yet */ | |
6caa2bcc | 65 | {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0}, |
d0254c7c MD |
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}, | |
4466912f DG |
70 | #else |
71 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, | |
d0254c7c | 72 | {"function", 0, POPT_ARG_NONE, 0, OPT_FUNCTION, 0, 0}, |
4466912f | 73 | #endif |
b213d387 MD |
74 | #if 0 |
75 | /* | |
76 | * Removed from options to discourage its use. Not in kernel | |
77 | * tracer anymore. | |
78 | */ | |
d0254c7c | 79 | {"function:entry", 0, POPT_ARG_NONE, 0, OPT_FUNCTION_ENTRY, 0, 0}, |
a54bd42d | 80 | {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0}, |
5c6886fa | 81 | #endif |
679b4943 | 82 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
d0254c7c MD |
83 | {0, 0, 0, 0, 0, 0, 0} |
84 | }; | |
85 | ||
86 | /* | |
87 | * usage | |
88 | */ | |
89 | static void usage(FILE *ofp) | |
90 | { | |
32a6298d | 91 | fprintf(ofp, "usage: lttng calibrate [-k|-u] [OPTIONS]\n"); |
d0254c7c | 92 | fprintf(ofp, "\n"); |
32a6298d | 93 | fprintf(ofp, "Options:\n"); |
d0254c7c | 94 | fprintf(ofp, " -h, --help Show this help\n"); |
679b4943 | 95 | fprintf(ofp, " --list-options Simple listing of options\n"); |
af87c45a | 96 | fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n"); |
af87c45a | 97 | fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n"); |
d0254c7c MD |
98 | fprintf(ofp, "\n"); |
99 | fprintf(ofp, "Calibrate options:\n"); | |
32a6298d | 100 | fprintf(ofp, " --function Dynamic function entry/return probe (default)\n"); |
4466912f | 101 | #if 0 |
d0254c7c MD |
102 | fprintf(ofp, " --tracepoint Tracepoint event (default)\n"); |
103 | fprintf(ofp, " --probe\n"); | |
104 | fprintf(ofp, " Dynamic probe.\n"); | |
b213d387 | 105 | #if 0 |
d0254c7c MD |
106 | fprintf(ofp, " --function:entry symbol\n"); |
107 | fprintf(ofp, " Function tracer event\n"); | |
b213d387 | 108 | #endif |
a54bd42d | 109 | fprintf(ofp, " --syscall System call eventl\n"); |
d0254c7c | 110 | fprintf(ofp, " --marker User-space marker (deprecated)\n"); |
4466912f | 111 | #endif |
d0254c7c MD |
112 | fprintf(ofp, "\n"); |
113 | } | |
114 | ||
115 | /* | |
af87c45a | 116 | * Calibrate LTTng. |
d0254c7c | 117 | * |
af87c45a | 118 | * Returns a CMD_* error. |
d0254c7c MD |
119 | */ |
120 | static int calibrate_lttng(void) | |
121 | { | |
122 | int ret = CMD_SUCCESS; | |
123 | struct lttng_domain dom; | |
124 | struct lttng_calibrate calibrate; | |
125 | ||
441c16a7 MD |
126 | memset(&dom, 0, sizeof(dom)); |
127 | memset(&calibrate, 0, sizeof(calibrate)); | |
128 | ||
d0254c7c MD |
129 | /* Create lttng domain */ |
130 | if (opt_kernel) { | |
131 | dom.type = LTTNG_DOMAIN_KERNEL; | |
4466912f DG |
132 | } else if (opt_userspace) { |
133 | dom.type = LTTNG_DOMAIN_UST; | |
134 | } else { | |
b9dfb167 | 135 | print_missing_domain(); |
d16c1a4c | 136 | ret = CMD_ERROR; |
4466912f | 137 | goto error; |
d0254c7c MD |
138 | } |
139 | ||
cd80958d DG |
140 | handle = lttng_create_handle(NULL, &dom); |
141 | if (handle == NULL) { | |
af87c45a | 142 | ret = CMD_ERROR; |
4466912f | 143 | goto error; |
cd80958d DG |
144 | } |
145 | ||
4466912f DG |
146 | switch (opt_event_type) { |
147 | case LTTNG_EVENT_TRACEPOINT: | |
148 | DBG("Calibrating kernel tracepoints"); | |
149 | break; | |
150 | case LTTNG_EVENT_PROBE: | |
151 | DBG("Calibrating kernel probes"); | |
152 | break; | |
153 | case LTTNG_EVENT_FUNCTION: | |
154 | DBG("Calibrating kernel functions"); | |
155 | calibrate.type = LTTNG_CALIBRATE_FUNCTION; | |
156 | ret = lttng_calibrate(handle, &calibrate); | |
157 | if (ret < 0) { | |
8f3f773f | 158 | ERR("%s", lttng_strerror(ret)); |
4466912f | 159 | goto error; |
d0254c7c | 160 | } |
4466912f DG |
161 | MSG("%s calibration done", opt_kernel ? "Kernel" : "UST"); |
162 | break; | |
163 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
164 | DBG("Calibrating kernel function entry"); | |
165 | break; | |
166 | case LTTNG_EVENT_SYSCALL: | |
167 | DBG("Calibrating kernel syscall"); | |
168 | break; | |
169 | default: | |
1ab1ea0b | 170 | ret = CMD_UNDEFINED; |
4466912f | 171 | goto error; |
d0254c7c | 172 | } |
4466912f | 173 | |
7e66b1b0 JRJ |
174 | if (lttng_opt_mi) { |
175 | assert(writer); | |
176 | ret = mi_lttng_calibrate(writer, &calibrate); | |
177 | if (ret) { | |
178 | ret = CMD_ERROR; | |
179 | goto error; | |
180 | } | |
181 | } | |
af87c45a | 182 | |
4466912f | 183 | error: |
cd80958d DG |
184 | lttng_destroy_handle(handle); |
185 | ||
d0254c7c MD |
186 | return ret; |
187 | } | |
188 | ||
189 | /* | |
af87c45a | 190 | * Calibrate LTTng tracer. |
1c8d13c8 TD |
191 | * |
192 | * Returns a CMD_* error. | |
d0254c7c MD |
193 | */ |
194 | int cmd_calibrate(int argc, const char **argv) | |
195 | { | |
7e66b1b0 | 196 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
d0254c7c MD |
197 | static poptContext pc; |
198 | ||
199 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
200 | poptReadDefaultConfig(pc, 0); | |
201 | ||
202 | /* Default event type */ | |
4466912f | 203 | opt_event_type = LTTNG_EVENT_FUNCTION; |
d0254c7c MD |
204 | |
205 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
206 | switch (opt) { | |
207 | case OPT_HELP: | |
af87c45a | 208 | usage(stdout); |
d0254c7c | 209 | goto end; |
d0254c7c | 210 | case OPT_TRACEPOINT: |
1ab1ea0b | 211 | ret = CMD_UNDEFINED; |
4466912f | 212 | goto end; |
d0254c7c | 213 | case OPT_MARKER: |
1ab1ea0b | 214 | ret = CMD_UNDEFINED; |
d0254c7c MD |
215 | goto end; |
216 | case OPT_PROBE: | |
1ab1ea0b | 217 | ret = CMD_UNDEFINED; |
d0254c7c MD |
218 | break; |
219 | case OPT_FUNCTION: | |
220 | opt_event_type = LTTNG_EVENT_FUNCTION; | |
221 | break; | |
222 | case OPT_FUNCTION_ENTRY: | |
1ab1ea0b | 223 | ret = CMD_UNDEFINED; |
4466912f | 224 | goto end; |
a54bd42d | 225 | case OPT_SYSCALL: |
1ab1ea0b | 226 | ret = CMD_UNDEFINED; |
4466912f | 227 | goto end; |
eeac7d46 MD |
228 | case OPT_USERSPACE: |
229 | opt_userspace = 1; | |
eeac7d46 | 230 | break; |
679b4943 SM |
231 | case OPT_LIST_OPTIONS: |
232 | list_cmd_options(stdout, long_options); | |
679b4943 | 233 | goto end; |
d0254c7c MD |
234 | default: |
235 | usage(stderr); | |
236 | ret = CMD_UNDEFINED; | |
237 | goto end; | |
238 | } | |
239 | } | |
240 | ||
7e66b1b0 JRJ |
241 | /* Mi check */ |
242 | if (lttng_opt_mi) { | |
243 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
244 | if (!writer) { | |
245 | ret = -LTTNG_ERR_NOMEM; | |
246 | goto end; | |
247 | } | |
248 | ||
249 | /* Open command element */ | |
250 | ret = mi_lttng_writer_command_open(writer, | |
251 | mi_lttng_element_command_calibrate); | |
252 | if (ret) { | |
253 | ret = CMD_ERROR; | |
254 | goto end; | |
255 | } | |
256 | ||
257 | /* Open output element */ | |
258 | ret = mi_lttng_writer_open_element(writer, | |
259 | mi_lttng_element_command_output); | |
260 | if (ret) { | |
261 | ret = CMD_ERROR; | |
262 | goto end; | |
263 | } | |
264 | } | |
265 | ||
266 | command_ret = calibrate_lttng(); | |
267 | if (command_ret) { | |
268 | success = 0; | |
269 | } | |
270 | ||
271 | /* Mi closing */ | |
272 | if (lttng_opt_mi) { | |
273 | /* Close output element */ | |
274 | ret = mi_lttng_writer_close_element(writer); | |
275 | if (ret) { | |
276 | ret = CMD_ERROR; | |
277 | goto end; | |
278 | } | |
279 | ||
280 | /* Success ? */ | |
281 | ret = mi_lttng_writer_write_element_bool(writer, | |
282 | mi_lttng_element_command_success, success); | |
283 | if (ret) { | |
284 | ret = CMD_ERROR; | |
285 | goto end; | |
286 | } | |
287 | ||
288 | /* Command element close */ | |
289 | ret = mi_lttng_writer_command_close(writer); | |
290 | if (ret) { | |
291 | ret = CMD_ERROR; | |
292 | goto end; | |
293 | } | |
294 | } | |
d0254c7c MD |
295 | |
296 | end: | |
7e66b1b0 JRJ |
297 | /* Mi clean-up */ |
298 | if (writer && mi_lttng_writer_destroy(writer)) { | |
299 | /* Preserve original error code */ | |
300 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
301 | } | |
302 | ||
303 | /* Overwrite ret if an error occurred during calibrate_lttng() */ | |
304 | ret = command_ret ? command_ret : ret; | |
305 | ||
ca1c3607 | 306 | poptFreeContext(pc); |
d0254c7c MD |
307 | return ret; |
308 | } |