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