| 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 | #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 | |
| 30 | #include "../command.h" |
| 31 | |
| 32 | static int opt_event_type; |
| 33 | static char *opt_kernel; |
| 34 | static int opt_userspace; |
| 35 | #if 0 |
| 36 | /* Not implemented yet */ |
| 37 | static char *opt_cmd_name; |
| 38 | static pid_t opt_pid; |
| 39 | #endif |
| 40 | |
| 41 | enum { |
| 42 | OPT_HELP = 1, |
| 43 | OPT_TRACEPOINT, |
| 44 | OPT_MARKER, |
| 45 | OPT_PROBE, |
| 46 | OPT_FUNCTION, |
| 47 | OPT_FUNCTION_ENTRY, |
| 48 | OPT_SYSCALL, |
| 49 | OPT_USERSPACE, |
| 50 | OPT_LIST_OPTIONS, |
| 51 | }; |
| 52 | |
| 53 | static struct lttng_handle *handle; |
| 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_VAL, &opt_kernel, 1, 0, 0}, |
| 59 | #if 0 |
| 60 | /* Not implemented yet */ |
| 61 | {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0}, |
| 62 | {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0}, |
| 63 | {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0}, |
| 64 | {"marker", 0, POPT_ARG_NONE, 0, OPT_MARKER, 0, 0}, |
| 65 | {"probe", 0, POPT_ARG_NONE, 0, OPT_PROBE, 0, 0}, |
| 66 | #else |
| 67 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, |
| 68 | {"function", 0, POPT_ARG_NONE, 0, OPT_FUNCTION, 0, 0}, |
| 69 | #endif |
| 70 | #if 0 |
| 71 | /* |
| 72 | * Removed from options to discourage its use. Not in kernel |
| 73 | * tracer anymore. |
| 74 | */ |
| 75 | {"function:entry", 0, POPT_ARG_NONE, 0, OPT_FUNCTION_ENTRY, 0, 0}, |
| 76 | {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0}, |
| 77 | #endif |
| 78 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 79 | {0, 0, 0, 0, 0, 0, 0} |
| 80 | }; |
| 81 | |
| 82 | /* |
| 83 | * usage |
| 84 | */ |
| 85 | static void usage(FILE *ofp) |
| 86 | { |
| 87 | fprintf(ofp, "usage: lttng calibrate [-k|-u] [OPTIONS]\n"); |
| 88 | fprintf(ofp, "\n"); |
| 89 | fprintf(ofp, "Options:\n"); |
| 90 | fprintf(ofp, " -h, --help Show this help\n"); |
| 91 | fprintf(ofp, " --list-options Simple listing of options\n"); |
| 92 | fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n"); |
| 93 | fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n"); |
| 94 | fprintf(ofp, "\n"); |
| 95 | fprintf(ofp, "Calibrate options:\n"); |
| 96 | fprintf(ofp, " --function Dynamic function entry/return probe (default)\n"); |
| 97 | #if 0 |
| 98 | fprintf(ofp, " --tracepoint Tracepoint event (default)\n"); |
| 99 | fprintf(ofp, " --probe\n"); |
| 100 | fprintf(ofp, " Dynamic probe.\n"); |
| 101 | #if 0 |
| 102 | fprintf(ofp, " --function:entry symbol\n"); |
| 103 | fprintf(ofp, " Function tracer event\n"); |
| 104 | #endif |
| 105 | fprintf(ofp, " --syscall System call eventl\n"); |
| 106 | fprintf(ofp, " --marker User-space marker (deprecated)\n"); |
| 107 | #endif |
| 108 | fprintf(ofp, "\n"); |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Calibrate LTTng. |
| 113 | * |
| 114 | * Returns a CMD_* error. |
| 115 | */ |
| 116 | static int calibrate_lttng(void) |
| 117 | { |
| 118 | int ret = CMD_SUCCESS; |
| 119 | struct lttng_domain dom; |
| 120 | struct lttng_calibrate calibrate; |
| 121 | |
| 122 | memset(&dom, 0, sizeof(dom)); |
| 123 | memset(&calibrate, 0, sizeof(calibrate)); |
| 124 | |
| 125 | /* Create lttng domain */ |
| 126 | if (opt_kernel) { |
| 127 | dom.type = LTTNG_DOMAIN_KERNEL; |
| 128 | } else if (opt_userspace) { |
| 129 | dom.type = LTTNG_DOMAIN_UST; |
| 130 | } else { |
| 131 | ERR("Please specify a tracer (-k/--kernel or -u/--userspace)"); |
| 132 | ret = CMD_ERROR; |
| 133 | goto error; |
| 134 | } |
| 135 | |
| 136 | handle = lttng_create_handle(NULL, &dom); |
| 137 | if (handle == NULL) { |
| 138 | ret = CMD_ERROR; |
| 139 | goto error; |
| 140 | } |
| 141 | |
| 142 | switch (opt_event_type) { |
| 143 | case LTTNG_EVENT_TRACEPOINT: |
| 144 | DBG("Calibrating kernel tracepoints"); |
| 145 | break; |
| 146 | case LTTNG_EVENT_PROBE: |
| 147 | DBG("Calibrating kernel probes"); |
| 148 | break; |
| 149 | case LTTNG_EVENT_FUNCTION: |
| 150 | DBG("Calibrating kernel functions"); |
| 151 | calibrate.type = LTTNG_CALIBRATE_FUNCTION; |
| 152 | ret = lttng_calibrate(handle, &calibrate); |
| 153 | if (ret < 0) { |
| 154 | ERR("%s", lttng_strerror(ret)); |
| 155 | goto error; |
| 156 | } |
| 157 | MSG("%s calibration done", opt_kernel ? "Kernel" : "UST"); |
| 158 | break; |
| 159 | case LTTNG_EVENT_FUNCTION_ENTRY: |
| 160 | DBG("Calibrating kernel function entry"); |
| 161 | break; |
| 162 | case LTTNG_EVENT_SYSCALL: |
| 163 | DBG("Calibrating kernel syscall"); |
| 164 | break; |
| 165 | default: |
| 166 | ret = CMD_UNDEFINED; |
| 167 | goto error; |
| 168 | } |
| 169 | |
| 170 | ret = CMD_SUCCESS; |
| 171 | |
| 172 | error: |
| 173 | lttng_destroy_handle(handle); |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Calibrate LTTng tracer. |
| 180 | * |
| 181 | * Returns a CMD_* error. |
| 182 | */ |
| 183 | int cmd_calibrate(int argc, const char **argv) |
| 184 | { |
| 185 | int opt, ret = CMD_SUCCESS; |
| 186 | static poptContext pc; |
| 187 | |
| 188 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 189 | poptReadDefaultConfig(pc, 0); |
| 190 | |
| 191 | /* Default event type */ |
| 192 | opt_event_type = LTTNG_EVENT_FUNCTION; |
| 193 | |
| 194 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 195 | switch (opt) { |
| 196 | case OPT_HELP: |
| 197 | usage(stdout); |
| 198 | goto end; |
| 199 | case OPT_TRACEPOINT: |
| 200 | ret = CMD_UNDEFINED; |
| 201 | goto end; |
| 202 | case OPT_MARKER: |
| 203 | ret = CMD_UNDEFINED; |
| 204 | goto end; |
| 205 | case OPT_PROBE: |
| 206 | ret = CMD_UNDEFINED; |
| 207 | break; |
| 208 | case OPT_FUNCTION: |
| 209 | opt_event_type = LTTNG_EVENT_FUNCTION; |
| 210 | break; |
| 211 | case OPT_FUNCTION_ENTRY: |
| 212 | ret = CMD_UNDEFINED; |
| 213 | goto end; |
| 214 | case OPT_SYSCALL: |
| 215 | ret = CMD_UNDEFINED; |
| 216 | goto end; |
| 217 | case OPT_USERSPACE: |
| 218 | opt_userspace = 1; |
| 219 | break; |
| 220 | case OPT_LIST_OPTIONS: |
| 221 | list_cmd_options(stdout, long_options); |
| 222 | goto end; |
| 223 | default: |
| 224 | usage(stderr); |
| 225 | ret = CMD_UNDEFINED; |
| 226 | goto end; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | ret = calibrate_lttng(); |
| 231 | |
| 232 | end: |
| 233 | poptFreeContext(pc); |
| 234 | return ret; |
| 235 | } |