Disable lttng ust domain not implemented
[lttng-tools.git] / src / bin / lttng / commands / add_context.c
CommitLineData
d65106b1
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
d65106b1
DG
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
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#define _GNU_SOURCE
90b9a268 20#include <ctype.h>
d65106b1
DG
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
3301e740
DG
29#include <urcu/list.h>
30
c399183f 31#include "../command.h"
d65106b1 32
b13d56d7
MD
33#define PRINT_LINE_LEN 80
34
d65106b1
DG
35static char *opt_event_name;
36static char *opt_channel_name;
5440dc42 37static char *opt_session_name;
55cc08a6 38static int opt_kernel;
d65106b1 39static int opt_userspace;
d78d6610
DG
40static char *opt_type;
41#if 0
42/* Not implemented yet */
eeac7d46 43static char *opt_cmd_name;
d65106b1 44static pid_t opt_pid;
d78d6610 45#endif
d65106b1
DG
46
47enum {
48 OPT_HELP = 1,
49 OPT_TYPE,
eeac7d46 50 OPT_USERSPACE,
d65106b1
DG
51};
52
cd80958d
DG
53static struct lttng_handle *handle;
54
90b9a268
DG
55/*
56 * Taken from the LTTng ABI
57 */
58enum context_type {
59 CONTEXT_PID = 0,
60 CONTEXT_PERF_COUNTER = 1,
95da1297 61 CONTEXT_PROCNAME = 2,
90b9a268
DG
62 CONTEXT_PRIO = 3,
63 CONTEXT_NICE = 4,
64 CONTEXT_VPID = 5,
65 CONTEXT_TID = 6,
66 CONTEXT_VTID = 7,
67 CONTEXT_PPID = 8,
68 CONTEXT_VPPID = 9,
3301e740
DG
69};
70
90b9a268
DG
71/*
72 * Taken from the Perf ABI (all enum perf_*)
73 */
74enum perf_type {
75 PERF_TYPE_HARDWARE = 0,
76 PERF_TYPE_SOFTWARE = 1,
b13d56d7 77 PERF_TYPE_HW_CACHE = 3,
3301e740
DG
78};
79
90b9a268 80enum perf_count_hard {
b13d56d7
MD
81 PERF_COUNT_HW_CPU_CYCLES = 0,
82 PERF_COUNT_HW_INSTRUCTIONS = 1,
83 PERF_COUNT_HW_CACHE_REFERENCES = 2,
84 PERF_COUNT_HW_CACHE_MISSES = 3,
85 PERF_COUNT_HW_BRANCH_INSTRUCTIONS = 4,
86 PERF_COUNT_HW_BRANCH_MISSES = 5,
87 PERF_COUNT_HW_BUS_CYCLES = 6,
88 PERF_COUNT_HW_STALLED_CYCLES_FRONTEND = 7,
89 PERF_COUNT_HW_STALLED_CYCLES_BACKEND = 8,
90b9a268
DG
90};
91
92enum perf_count_soft {
93 PERF_COUNT_SW_CPU_CLOCK = 0,
94 PERF_COUNT_SW_TASK_CLOCK = 1,
95 PERF_COUNT_SW_PAGE_FAULTS = 2,
96 PERF_COUNT_SW_CONTEXT_SWITCHES = 3,
97 PERF_COUNT_SW_CPU_MIGRATIONS = 4,
98 PERF_COUNT_SW_PAGE_FAULTS_MIN = 5,
99 PERF_COUNT_SW_PAGE_FAULTS_MAJ = 6,
100 PERF_COUNT_SW_ALIGNMENT_FAULTS = 7,
101 PERF_COUNT_SW_EMULATION_FAULTS = 8,
3301e740
DG
102};
103
b13d56d7
MD
104/*
105 * Generalized hardware cache events:
106 *
107 * { L1-D, L1-I, LLC, ITLB, DTLB, BPU } x
108 * { read, write, prefetch } x
109 * { accesses, misses }
110 */
111enum perf_hw_cache_id {
112 PERF_COUNT_HW_CACHE_L1D = 0,
113 PERF_COUNT_HW_CACHE_L1I = 1,
114 PERF_COUNT_HW_CACHE_LL = 2,
115 PERF_COUNT_HW_CACHE_DTLB = 3,
116 PERF_COUNT_HW_CACHE_ITLB = 4,
117 PERF_COUNT_HW_CACHE_BPU = 5,
118
119 PERF_COUNT_HW_CACHE_MAX, /* non-ABI */
120};
121
122enum perf_hw_cache_op_id {
123 PERF_COUNT_HW_CACHE_OP_READ = 0,
124 PERF_COUNT_HW_CACHE_OP_WRITE = 1,
125 PERF_COUNT_HW_CACHE_OP_PREFETCH = 2,
126
127 PERF_COUNT_HW_CACHE_OP_MAX, /* non-ABI */
128};
129
130enum perf_hw_cache_op_result_id {
131 PERF_COUNT_HW_CACHE_RESULT_ACCESS = 0,
132 PERF_COUNT_HW_CACHE_RESULT_MISS = 1,
133
134 PERF_COUNT_HW_CACHE_RESULT_MAX, /* non-ABI */
135};
136
d65106b1
DG
137static struct poptOption long_options[] = {
138 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
139 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 140 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
d65106b1
DG
141 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
142 {"event", 'e', POPT_ARG_STRING, &opt_event_name, 0, 0, 0},
143 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610
DG
144#if 0
145 /* Not implemented yet */
d65106b1 146 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
d78d6610
DG
147 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
148#else
149 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
150#endif
6caa2bcc 151 {"type", 't', POPT_ARG_STRING, &opt_type, OPT_TYPE, 0, 0},
d65106b1
DG
152 {0, 0, 0, 0, 0, 0, 0}
153};
154
90b9a268 155/*
b13d56d7 156 * Context options
90b9a268 157 */
b13d56d7
MD
158#define PERF_HW(opt, name) \
159 { \
160 "perf:" #opt, CONTEXT_PERF_COUNTER, \
161 .u.perf = { PERF_TYPE_HARDWARE, PERF_COUNT_HW_##name, },\
162 }
90b9a268 163
b13d56d7
MD
164#define PERF_SW(opt, name) \
165 { \
166 "perf:" #opt, CONTEXT_PERF_COUNTER, \
167 .u.perf = { PERF_TYPE_SOFTWARE, PERF_COUNT_SW_##name, },\
168 }
90b9a268 169
b13d56d7
MD
170#define _PERF_HW_CACHE(optstr, name, op, result) \
171 { \
172 "perf:" optstr, CONTEXT_PERF_COUNTER, \
173 .u.perf = { \
174 PERF_TYPE_HW_CACHE, \
175 (uint64_t) PERF_COUNT_HW_CACHE_##name \
18829107
MD
176 | ((uint64_t) PERF_COUNT_HW_CACHE_OP_##op << 8) \
177 | ((uint64_t) PERF_COUNT_HW_CACHE_RESULT_##result << 16), \
b13d56d7
MD
178 }, \
179 }
180
181#define PERF_HW_CACHE(opt, name) \
182 _PERF_HW_CACHE(#opt "-loads", name, READ, ACCESS), \
183 _PERF_HW_CACHE(#opt "-load-misses", name, READ, MISS), \
184 _PERF_HW_CACHE(#opt "-stores", name, WRITE, ACCESS), \
185 _PERF_HW_CACHE(#opt "-store-misses", name, WRITE, MISS), \
186 _PERF_HW_CACHE(#opt "-prefetches", name, PREFETCH, ACCESS), \
187 _PERF_HW_CACHE(#opt "-prefetch-misses", name, PREFETCH, MISS) \
188
189static
190const struct ctx_opts {
90b9a268 191 char *symbol;
b13d56d7
MD
192 enum context_type ctx_type;
193 union {
194 struct {
195 uint32_t type;
196 uint64_t config;
197 } perf;
198 } u;
199} ctx_opts[] = {
200 { "pid", CONTEXT_PID },
95da1297 201 { "procname", CONTEXT_PROCNAME },
b13d56d7
MD
202 { "prio", CONTEXT_PRIO },
203 { "nice", CONTEXT_NICE },
204 { "vpid", CONTEXT_VPID },
205 { "tid", CONTEXT_TID },
206 { "vtid", CONTEXT_VTID },
207 { "ppid", CONTEXT_PPID },
208 { "vppid", CONTEXT_VPPID },
209 /* Perf options */
210 PERF_HW(cpu-cycles, CPU_CYCLES),
211 PERF_HW(cycles, CPU_CYCLES),
212 PERF_HW(stalled-cycles-frontend, STALLED_CYCLES_FRONTEND),
213 PERF_HW(idle-cycles-frontend, STALLED_CYCLES_FRONTEND),
214 PERF_HW(stalled-cycles-backend, STALLED_CYCLES_BACKEND),
215 PERF_HW(idle-cycles-backend, STALLED_CYCLES_BACKEND),
216 PERF_HW(instructions, INSTRUCTIONS),
217 PERF_HW(cache-references, CACHE_REFERENCES),
218 PERF_HW(cache-misses, CACHE_MISSES),
219 PERF_HW(branch-instructions, BRANCH_INSTRUCTIONS),
220 PERF_HW(branches, BRANCH_INSTRUCTIONS),
221 PERF_HW(branch-misses, BRANCH_MISSES),
222 PERF_HW(bus-cycles, BUS_CYCLES),
223
224 PERF_HW_CACHE(L1-dcache, L1D),
225 PERF_HW_CACHE(L1-icache, L1I),
226 PERF_HW_CACHE(LLC, LL),
227 PERF_HW_CACHE(dTLB, DTLB),
228 _PERF_HW_CACHE("iTLB-loads", ITLB, READ, ACCESS),
229 _PERF_HW_CACHE("iTLB-load-misses", ITLB, READ, MISS),
230 _PERF_HW_CACHE("branch-loads", BPU, READ, ACCESS),
231 _PERF_HW_CACHE("branch-load-misses", BPU, READ, MISS),
232
233
234 PERF_SW(cpu-clock, CPU_CLOCK),
235 PERF_SW(task-clock, TASK_CLOCK),
236 PERF_SW(page-fault, PAGE_FAULTS),
237 PERF_SW(faults, PAGE_FAULTS),
238 PERF_SW(major-faults, PAGE_FAULTS_MAJ),
239 PERF_SW(minor-faults, PAGE_FAULTS_MIN),
240 PERF_SW(context-switches, CONTEXT_SWITCHES),
241 PERF_SW(cs, CONTEXT_SWITCHES),
242 PERF_SW(cpu-migrations, CPU_MIGRATIONS),
243 PERF_SW(migrations, CPU_MIGRATIONS),
244 PERF_SW(alignment-faults, ALIGNMENT_FAULTS),
245 PERF_SW(emulation-faults, EMULATION_FAULTS),
246 { NULL, -1 }, /* Closure */
90b9a268
DG
247};
248
b13d56d7
MD
249#undef PERF_SW
250#undef PERF_HW
251
90b9a268 252/*
b13d56d7 253 * Context type for command line option parsing.
90b9a268 254 */
b13d56d7
MD
255struct ctx_type {
256 const struct ctx_opts *opt;
257 struct cds_list_head list;
90b9a268
DG
258};
259
260/*
261 * List of context type. Use to enable multiple context on a single command
262 * line entry.
263 */
264struct ctx_type_list {
265 struct cds_list_head head;
266} ctx_type_list = {
267 .head = CDS_LIST_HEAD_INIT(ctx_type_list.head),
268};
269
90b9a268
DG
270/*
271 * Pretty print context type.
272 */
273static void print_ctx_type(FILE *ofp)
274{
b13d56d7
MD
275 const char *indent = " ";
276 int indent_len = strlen(indent);
277 int len, i = 0;
90b9a268 278
76e3c5dd 279 fprintf(ofp, "%s", indent);
b13d56d7 280 len = indent_len;
90b9a268 281 while (ctx_opts[i].symbol != NULL) {
b13d56d7
MD
282 if (len > indent_len) {
283 if (len + strlen(ctx_opts[i].symbol) + 2
284 >= PRINT_LINE_LEN) {
285 fprintf(ofp, ",\n");
76e3c5dd 286 fprintf(ofp, "%s", indent);
b13d56d7
MD
287 len = indent_len;
288 } else {
289 len += fprintf(ofp, ", ");
90b9a268
DG
290 }
291 }
b13d56d7 292 len += fprintf(ofp, "%s", ctx_opts[i].symbol);
90b9a268
DG
293 i++;
294 }
90b9a268
DG
295}
296
d65106b1
DG
297/*
298 * usage
299 */
300static void usage(FILE *ofp)
301{
b13d56d7 302 fprintf(ofp, "usage: lttng add-context -t TYPE\n");
d65106b1 303 fprintf(ofp, "\n");
55cc08a6 304 fprintf(ofp, "If no event name is given (-e), the context will be added to the channel\n");
b13d56d7 305 fprintf(ofp, "If no channel and no event is given (-c/-e), the context\n");
55cc08a6 306 fprintf(ofp, "will be added to all events and all channels.\n");
3301e740 307 fprintf(ofp, "\n");
d65106b1
DG
308 fprintf(ofp, "Options:\n");
309 fprintf(ofp, " -h, --help Show this help\n");
5440dc42 310 fprintf(ofp, " -s, --session Apply on session name\n");
d65106b1
DG
311 fprintf(ofp, " -c, --channel NAME Apply on channel\n");
312 fprintf(ofp, " -e, --event NAME Apply on event\n");
313 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
d78d6610 314#if 0
55cc08a6 315 fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n");
e14f64a8
DG
316 fprintf(ofp, " If no CMD, the domain used is UST global\n");
317 fprintf(ofp, " or else the domain is UST EXEC_NAME\n");
318 fprintf(ofp, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n");
d78d6610
DG
319#else
320 fprintf(ofp, " -u, --userspace Apply for the user-space tracer\n");
321#endif
b13d56d7
MD
322 fprintf(ofp, " -t, --type TYPE Context type. You can repeat that option on\n");
323 fprintf(ofp, " the command line.\n");
324 fprintf(ofp, " TYPE can be one of the strings below:\n");
90b9a268 325 print_ctx_type(ofp);
d65106b1 326 fprintf(ofp, "\n");
90b9a268 327 fprintf(ofp, "Example:\n");
b13d56d7 328 fprintf(ofp, "This command will add the context information 'prio' and two perf\n"
e0899ffa 329 "counters: hardware branch misses and cache misses, to all events\n"
b13d56d7
MD
330 "in the trace data output:\n");
331 fprintf(ofp, "# lttng add-context -k -t prio -t perf:branch-misses -t perf:cache-misses\n");
d65106b1
DG
332 fprintf(ofp, "\n");
333}
334
90b9a268
DG
335/*
336 * Find context numerical value from string.
337 */
338static int find_ctx_type_idx(const char *opt)
339{
340 int ret = -1, i = 0;
341
342 while (ctx_opts[i].symbol != NULL) {
343 if (strcmp(opt, ctx_opts[i].symbol) == 0) {
344 ret = i;
345 goto end;
346 }
347 i++;
348 }
349
350end:
351 return ret;
352}
353
90b9a268
DG
354/*
355 * Add context to channel or event.
d65106b1 356 */
cd80958d 357static int add_context(char *session_name)
d65106b1 358{
b13d56d7 359 int ret = CMD_SUCCESS;
7d29a247
DG
360 struct lttng_event_context context;
361 struct lttng_domain dom;
3301e740 362 struct ctx_type *type;
b13d56d7 363 char *ptr;
d65106b1 364
cd80958d
DG
365 if (opt_kernel) {
366 dom.type = LTTNG_DOMAIN_KERNEL;
d78d6610 367 } else if (opt_userspace) {
55cc08a6 368 dom.type = LTTNG_DOMAIN_UST;
55cc08a6 369 } else {
e14f64a8 370 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
55cc08a6
DG
371 ret = CMD_NOT_IMPLEMENTED;
372 goto error;
cd80958d
DG
373 }
374
375 handle = lttng_create_handle(session_name, &dom);
376 if (handle == NULL) {
377 ret = -1;
378 goto error;
379 }
380
3301e740
DG
381 /* Iterate over all context type given */
382 cds_list_for_each_entry(type, &ctx_type_list.head, list) {
b13d56d7
MD
383 context.ctx = type->opt->ctx_type;
384 if (context.ctx == LTTNG_EVENT_CONTEXT_PERF_COUNTER) {
385 context.u.perf_counter.type = type->opt->u.perf.type;
386 context.u.perf_counter.config = type->opt->u.perf.config;
d7b91b3c 387 strcpy(context.u.perf_counter.name, type->opt->symbol);
b13d56d7
MD
388 /* Replace : and - by _ */
389 while ((ptr = strchr(context.u.perf_counter.name, '-')) != NULL) {
390 *ptr = '_';
3301e740 391 }
b13d56d7
MD
392 while ((ptr = strchr(context.u.perf_counter.name, ':')) != NULL) {
393 *ptr = '_';
3301e740 394 }
3301e740 395 }
55cc08a6
DG
396 DBG("Adding context...");
397
398 ret = lttng_add_context(handle, &context, opt_event_name,
399 opt_channel_name);
400 if (ret < 0) {
401 fprintf(stderr, "%s: ", type->opt->symbol);
402 continue;
d65106b1 403 } else {
55cc08a6
DG
404 MSG("%s context %s added to %s event in %s",
405 opt_kernel ? "kernel" : "UST", type->opt->symbol,
406 opt_event_name ? opt_event_name : "all",
407 opt_channel_name ? opt_channel_name : "all channels");
d65106b1 408 }
d65106b1
DG
409 }
410
411error:
cd80958d
DG
412 lttng_destroy_handle(handle);
413
d65106b1
DG
414 return ret;
415}
416
417/*
90b9a268 418 * Add context on channel or event.
d65106b1
DG
419 */
420int cmd_add_context(int argc, const char **argv)
421{
90b9a268 422 int index, opt, ret = CMD_SUCCESS;
d65106b1 423 static poptContext pc;
b13d56d7 424 struct ctx_type *type, *tmptype;
cd80958d 425 char *session_name = NULL;
d65106b1 426
636167b6
DG
427 if (argc < 2) {
428 usage(stderr);
429 goto end;
430 }
431
d65106b1
DG
432 pc = poptGetContext(NULL, argc, argv, long_options, 0);
433 poptReadDefaultConfig(pc, 0);
434
435 while ((opt = poptGetNextOpt(pc)) != -1) {
436 switch (opt) {
437 case OPT_HELP:
438 usage(stderr);
439 ret = CMD_SUCCESS;
440 goto end;
441 case OPT_TYPE:
3301e740
DG
442 type = malloc(sizeof(struct ctx_type));
443 if (type == NULL) {
444 perror("malloc ctx_type");
445 ret = -1;
636167b6
DG
446 goto end;
447 }
6caa2bcc 448 index = find_ctx_type_idx(opt_type);
b13d56d7 449 if (index < 0) {
6caa2bcc 450 ERR("Unknown context type %s", opt_type);
b13d56d7 451 goto end;
90b9a268 452 }
b13d56d7
MD
453 type->opt = &ctx_opts[index];
454 if (type->opt->ctx_type == -1) {
6caa2bcc 455 ERR("Unknown context type %s", opt_type);
90b9a268
DG
456 } else {
457 cds_list_add(&type->list, &ctx_type_list.head);
458 }
d65106b1 459 break;
eeac7d46
MD
460 case OPT_USERSPACE:
461 opt_userspace = 1;
d78d6610 462#if 0
eeac7d46 463 opt_cmd_name = poptGetOptArg(pc);
d78d6610 464#endif
eeac7d46 465 break;
d65106b1
DG
466 default:
467 usage(stderr);
468 ret = CMD_UNDEFINED;
469 goto end;
470 }
471 }
472
cd80958d
DG
473 if (!opt_session_name) {
474 session_name = get_session_name();
475 if (session_name == NULL) {
476 ret = -1;
477 goto end;
478 }
479 } else {
480 session_name = opt_session_name;
481 }
482
483 ret = add_context(session_name);
3301e740
DG
484
485 /* Cleanup allocated memory */
b13d56d7 486 cds_list_for_each_entry_safe(type, tmptype, &ctx_type_list.head, list) {
3301e740
DG
487 free(type);
488 }
489
d65106b1
DG
490end:
491 return ret;
492}
This page took 0.048023 seconds and 4 git commands to generate.