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