| 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 |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; only version 2 |
| 8 | * of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #define _GNU_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 | |
| 31 | #include "../command.h" |
| 32 | |
| 33 | static int opt_event_type; |
| 34 | static char *opt_kernel; |
| 35 | static int opt_userspace; |
| 36 | #if 0 |
| 37 | /* Not implemented yet */ |
| 38 | static char *opt_cmd_name; |
| 39 | static pid_t opt_pid; |
| 40 | #endif |
| 41 | |
| 42 | enum { |
| 43 | OPT_HELP = 1, |
| 44 | OPT_TRACEPOINT, |
| 45 | OPT_MARKER, |
| 46 | OPT_PROBE, |
| 47 | OPT_FUNCTION, |
| 48 | OPT_FUNCTION_ENTRY, |
| 49 | OPT_SYSCALL, |
| 50 | OPT_USERSPACE, |
| 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 | #else |
| 64 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, |
| 65 | #endif |
| 66 | {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0}, |
| 67 | {"marker", 0, POPT_ARG_NONE, 0, OPT_MARKER, 0, 0}, |
| 68 | {"probe", 0, POPT_ARG_NONE, 0, OPT_PROBE, 0, 0}, |
| 69 | {"function", 0, POPT_ARG_NONE, 0, OPT_FUNCTION, 0, 0}, |
| 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 | #endif |
| 77 | {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0}, |
| 78 | {0, 0, 0, 0, 0, 0, 0} |
| 79 | }; |
| 80 | |
| 81 | /* |
| 82 | * usage |
| 83 | */ |
| 84 | static void usage(FILE *ofp) |
| 85 | { |
| 86 | fprintf(ofp, "usage: lttng calibrate [options] [calibrate_options]\n"); |
| 87 | fprintf(ofp, "\n"); |
| 88 | fprintf(ofp, " -h, --help Show this help\n"); |
| 89 | fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n"); |
| 90 | #if 0 |
| 91 | fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n"); |
| 92 | fprintf(ofp, " If no CMD, the domain used is UST global\n"); |
| 93 | fprintf(ofp, " or else the domain is UST EXEC_NAME\n"); |
| 94 | fprintf(ofp, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n"); |
| 95 | #else |
| 96 | fprintf(ofp, " -u, --userspace Apply for the user-space tracer\n"); |
| 97 | #endif |
| 98 | fprintf(ofp, "\n"); |
| 99 | fprintf(ofp, "Calibrate options:\n"); |
| 100 | fprintf(ofp, " --tracepoint Tracepoint event (default)\n"); |
| 101 | fprintf(ofp, " --probe\n"); |
| 102 | fprintf(ofp, " Dynamic probe.\n"); |
| 103 | fprintf(ofp, " --function\n"); |
| 104 | fprintf(ofp, " Dynamic function entry/return probe.\n"); |
| 105 | #if 0 |
| 106 | fprintf(ofp, " --function:entry symbol\n"); |
| 107 | fprintf(ofp, " Function tracer event\n"); |
| 108 | #endif |
| 109 | fprintf(ofp, " --syscall System call eventl\n"); |
| 110 | fprintf(ofp, " --marker User-space marker (deprecated)\n"); |
| 111 | fprintf(ofp, "\n"); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * calibrate_lttng |
| 116 | * |
| 117 | * Calibrate LTTng. |
| 118 | */ |
| 119 | static int calibrate_lttng(void) |
| 120 | { |
| 121 | int ret = CMD_SUCCESS; |
| 122 | struct lttng_domain dom; |
| 123 | struct lttng_calibrate calibrate; |
| 124 | |
| 125 | /* Create lttng domain */ |
| 126 | if (opt_kernel) { |
| 127 | dom.type = LTTNG_DOMAIN_KERNEL; |
| 128 | } |
| 129 | |
| 130 | handle = lttng_create_handle(NULL, &dom); |
| 131 | if (handle == NULL) { |
| 132 | ret = -1; |
| 133 | goto end; |
| 134 | } |
| 135 | |
| 136 | /* Kernel tracer action */ |
| 137 | if (opt_kernel) { |
| 138 | switch (opt_event_type) { |
| 139 | case LTTNG_EVENT_TRACEPOINT: |
| 140 | DBG("Calibrating kernel tracepoints"); |
| 141 | break; |
| 142 | case LTTNG_EVENT_PROBE: |
| 143 | DBG("Calibrating kernel probes"); |
| 144 | break; |
| 145 | case LTTNG_EVENT_FUNCTION: |
| 146 | DBG("Calibrating kernel functions"); |
| 147 | calibrate.type = LTTNG_CALIBRATE_FUNCTION; |
| 148 | ret = lttng_calibrate(handle, &calibrate); |
| 149 | break; |
| 150 | case LTTNG_EVENT_FUNCTION_ENTRY: |
| 151 | DBG("Calibrating kernel function entry"); |
| 152 | break; |
| 153 | case LTTNG_EVENT_SYSCALL: |
| 154 | DBG("Calibrating kernel syscall"); |
| 155 | break; |
| 156 | default: |
| 157 | ret = CMD_NOT_IMPLEMENTED; |
| 158 | goto end; |
| 159 | } |
| 160 | } else if (opt_userspace) { /* User-space tracer action */ |
| 161 | ret = CMD_NOT_IMPLEMENTED; |
| 162 | goto end; |
| 163 | } else { |
| 164 | ERR("Please specify a tracer (--kernel or --userspace)"); |
| 165 | goto end; |
| 166 | } |
| 167 | end: |
| 168 | lttng_destroy_handle(handle); |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * cmd_calibrate |
| 175 | * |
| 176 | * Calibrate LTTng tracer. |
| 177 | */ |
| 178 | int cmd_calibrate(int argc, const char **argv) |
| 179 | { |
| 180 | int opt, ret; |
| 181 | static poptContext pc; |
| 182 | |
| 183 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 184 | poptReadDefaultConfig(pc, 0); |
| 185 | |
| 186 | /* Default event type */ |
| 187 | opt_event_type = LTTNG_EVENT_TRACEPOINT; |
| 188 | |
| 189 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 190 | switch (opt) { |
| 191 | case OPT_HELP: |
| 192 | usage(stderr); |
| 193 | ret = CMD_SUCCESS; |
| 194 | goto end; |
| 195 | case OPT_TRACEPOINT: |
| 196 | ret = CMD_NOT_IMPLEMENTED; |
| 197 | break; |
| 198 | case OPT_MARKER: |
| 199 | ret = CMD_NOT_IMPLEMENTED; |
| 200 | goto end; |
| 201 | case OPT_PROBE: |
| 202 | ret = CMD_NOT_IMPLEMENTED; |
| 203 | break; |
| 204 | case OPT_FUNCTION: |
| 205 | opt_event_type = LTTNG_EVENT_FUNCTION; |
| 206 | break; |
| 207 | case OPT_FUNCTION_ENTRY: |
| 208 | ret = CMD_NOT_IMPLEMENTED; |
| 209 | break; |
| 210 | case OPT_SYSCALL: |
| 211 | ret = CMD_NOT_IMPLEMENTED; |
| 212 | break; |
| 213 | case OPT_USERSPACE: |
| 214 | opt_userspace = 1; |
| 215 | break; |
| 216 | default: |
| 217 | usage(stderr); |
| 218 | ret = CMD_UNDEFINED; |
| 219 | goto end; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | ret = calibrate_lttng(); |
| 224 | |
| 225 | end: |
| 226 | return ret; |
| 227 | } |