| 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 | * Calibrate LTTng. |
| 67 | * |
| 68 | * Returns a CMD_* error. |
| 69 | */ |
| 70 | static int calibrate_lttng(void) |
| 71 | { |
| 72 | int ret = CMD_SUCCESS; |
| 73 | struct lttng_domain dom; |
| 74 | struct lttng_calibrate calibrate; |
| 75 | |
| 76 | memset(&dom, 0, sizeof(dom)); |
| 77 | memset(&calibrate, 0, sizeof(calibrate)); |
| 78 | |
| 79 | /* Create lttng domain */ |
| 80 | if (opt_kernel) { |
| 81 | dom.type = LTTNG_DOMAIN_KERNEL; |
| 82 | } else if (opt_userspace) { |
| 83 | dom.type = LTTNG_DOMAIN_UST; |
| 84 | } else { |
| 85 | /* Checked by the caller. */ |
| 86 | assert(0); |
| 87 | } |
| 88 | |
| 89 | handle = lttng_create_handle(NULL, &dom); |
| 90 | if (handle == NULL) { |
| 91 | ret = CMD_ERROR; |
| 92 | goto error; |
| 93 | } |
| 94 | |
| 95 | switch (opt_event_type) { |
| 96 | case LTTNG_EVENT_TRACEPOINT: |
| 97 | DBG("Calibrating kernel tracepoints"); |
| 98 | break; |
| 99 | case LTTNG_EVENT_PROBE: |
| 100 | DBG("Calibrating kernel probes"); |
| 101 | break; |
| 102 | case LTTNG_EVENT_FUNCTION: |
| 103 | DBG("Calibrating kernel functions"); |
| 104 | calibrate.type = LTTNG_CALIBRATE_FUNCTION; |
| 105 | ret = lttng_calibrate(handle, &calibrate); |
| 106 | if (ret < 0) { |
| 107 | ERR("%s", lttng_strerror(ret)); |
| 108 | goto error; |
| 109 | } |
| 110 | MSG("%s calibration done", opt_kernel ? "Kernel" : "UST"); |
| 111 | break; |
| 112 | case LTTNG_EVENT_FUNCTION_ENTRY: |
| 113 | DBG("Calibrating kernel function entry"); |
| 114 | break; |
| 115 | case LTTNG_EVENT_SYSCALL: |
| 116 | DBG("Calibrating kernel syscall"); |
| 117 | break; |
| 118 | default: |
| 119 | ret = CMD_UNDEFINED; |
| 120 | goto error; |
| 121 | } |
| 122 | |
| 123 | if (lttng_opt_mi) { |
| 124 | assert(writer); |
| 125 | ret = mi_lttng_calibrate(writer, &calibrate); |
| 126 | if (ret) { |
| 127 | ret = CMD_ERROR; |
| 128 | goto error; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | error: |
| 133 | lttng_destroy_handle(handle); |
| 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Calibrate LTTng tracer. |
| 140 | * |
| 141 | * Returns a CMD_* error. |
| 142 | */ |
| 143 | int cmd_calibrate(int argc, const char **argv) |
| 144 | { |
| 145 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
| 146 | static poptContext pc; |
| 147 | |
| 148 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 149 | poptReadDefaultConfig(pc, 0); |
| 150 | |
| 151 | /* Default event type */ |
| 152 | opt_event_type = LTTNG_EVENT_FUNCTION; |
| 153 | |
| 154 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 155 | switch (opt) { |
| 156 | case OPT_HELP: |
| 157 | SHOW_HELP(); |
| 158 | goto end; |
| 159 | case OPT_TRACEPOINT: |
| 160 | ret = CMD_UNDEFINED; |
| 161 | goto end; |
| 162 | case OPT_MARKER: |
| 163 | ret = CMD_UNDEFINED; |
| 164 | goto end; |
| 165 | case OPT_PROBE: |
| 166 | ret = CMD_UNDEFINED; |
| 167 | break; |
| 168 | case OPT_FUNCTION: |
| 169 | opt_event_type = LTTNG_EVENT_FUNCTION; |
| 170 | break; |
| 171 | case OPT_FUNCTION_ENTRY: |
| 172 | ret = CMD_UNDEFINED; |
| 173 | goto end; |
| 174 | case OPT_SYSCALL: |
| 175 | ret = CMD_UNDEFINED; |
| 176 | goto end; |
| 177 | case OPT_USERSPACE: |
| 178 | opt_userspace = 1; |
| 179 | break; |
| 180 | case OPT_KERNEL: |
| 181 | opt_kernel = 1; |
| 182 | break; |
| 183 | case OPT_LIST_OPTIONS: |
| 184 | list_cmd_options(stdout, long_options); |
| 185 | goto end; |
| 186 | default: |
| 187 | ret = CMD_UNDEFINED; |
| 188 | goto end; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace); |
| 193 | if (ret) { |
| 194 | ret = CMD_ERROR; |
| 195 | goto end; |
| 196 | } |
| 197 | |
| 198 | /* Mi check */ |
| 199 | if (lttng_opt_mi) { |
| 200 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 201 | if (!writer) { |
| 202 | ret = -LTTNG_ERR_NOMEM; |
| 203 | goto end; |
| 204 | } |
| 205 | |
| 206 | /* Open command element */ |
| 207 | ret = mi_lttng_writer_command_open(writer, |
| 208 | mi_lttng_element_command_calibrate); |
| 209 | if (ret) { |
| 210 | ret = CMD_ERROR; |
| 211 | goto end; |
| 212 | } |
| 213 | |
| 214 | /* Open output element */ |
| 215 | ret = mi_lttng_writer_open_element(writer, |
| 216 | mi_lttng_element_command_output); |
| 217 | if (ret) { |
| 218 | ret = CMD_ERROR; |
| 219 | goto end; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | command_ret = calibrate_lttng(); |
| 224 | if (command_ret) { |
| 225 | success = 0; |
| 226 | } |
| 227 | |
| 228 | /* Mi closing */ |
| 229 | if (lttng_opt_mi) { |
| 230 | /* Close output element */ |
| 231 | ret = mi_lttng_writer_close_element(writer); |
| 232 | if (ret) { |
| 233 | ret = CMD_ERROR; |
| 234 | goto end; |
| 235 | } |
| 236 | |
| 237 | /* Success ? */ |
| 238 | ret = mi_lttng_writer_write_element_bool(writer, |
| 239 | mi_lttng_element_command_success, success); |
| 240 | if (ret) { |
| 241 | ret = CMD_ERROR; |
| 242 | goto end; |
| 243 | } |
| 244 | |
| 245 | /* Command element close */ |
| 246 | ret = mi_lttng_writer_command_close(writer); |
| 247 | if (ret) { |
| 248 | ret = CMD_ERROR; |
| 249 | goto end; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | end: |
| 254 | /* Mi clean-up */ |
| 255 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 256 | /* Preserve original error code */ |
| 257 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; |
| 258 | } |
| 259 | |
| 260 | /* Overwrite ret if an error occurred during calibrate_lttng() */ |
| 261 | ret = command_ret ? command_ret : ret; |
| 262 | |
| 263 | poptFreeContext(pc); |
| 264 | return ret; |
| 265 | } |