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