Change lttng command line options for UST domain
[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;
55cc08a6 40static int opt_kernel;
d65106b1 41static int opt_userspace;
eeac7d46 42static char *opt_cmd_name;
d65106b1 43static pid_t opt_pid;
6caa2bcc 44static char *opt_type;
d65106b1
DG
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,
95da1297 60 CONTEXT_PROCNAME = 2,
90b9a268
DG
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},
55cc08a6 143 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
d65106b1 144 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
6caa2bcc 145 {"type", 't', POPT_ARG_STRING, &opt_type, OPT_TYPE, 0, 0},
d65106b1
DG
146 {0, 0, 0, 0, 0, 0, 0}
147};
148
90b9a268 149/*
b13d56d7 150 * Context options
90b9a268 151 */
b13d56d7
MD
152#define PERF_HW(opt, name) \
153 { \
154 "perf:" #opt, CONTEXT_PERF_COUNTER, \
155 .u.perf = { PERF_TYPE_HARDWARE, PERF_COUNT_HW_##name, },\
156 }
90b9a268 157
b13d56d7
MD
158#define PERF_SW(opt, name) \
159 { \
160 "perf:" #opt, CONTEXT_PERF_COUNTER, \
161 .u.perf = { PERF_TYPE_SOFTWARE, PERF_COUNT_SW_##name, },\
162 }
90b9a268 163
b13d56d7
MD
164#define _PERF_HW_CACHE(optstr, name, op, result) \
165 { \
166 "perf:" optstr, CONTEXT_PERF_COUNTER, \
167 .u.perf = { \
168 PERF_TYPE_HW_CACHE, \
169 (uint64_t) PERF_COUNT_HW_CACHE_##name \
18829107
MD
170 | ((uint64_t) PERF_COUNT_HW_CACHE_OP_##op << 8) \
171 | ((uint64_t) PERF_COUNT_HW_CACHE_RESULT_##result << 16), \
b13d56d7
MD
172 }, \
173 }
174
175#define PERF_HW_CACHE(opt, name) \
176 _PERF_HW_CACHE(#opt "-loads", name, READ, ACCESS), \
177 _PERF_HW_CACHE(#opt "-load-misses", name, READ, MISS), \
178 _PERF_HW_CACHE(#opt "-stores", name, WRITE, ACCESS), \
179 _PERF_HW_CACHE(#opt "-store-misses", name, WRITE, MISS), \
180 _PERF_HW_CACHE(#opt "-prefetches", name, PREFETCH, ACCESS), \
181 _PERF_HW_CACHE(#opt "-prefetch-misses", name, PREFETCH, MISS) \
182
183static
184const struct ctx_opts {
90b9a268 185 char *symbol;
b13d56d7
MD
186 enum context_type ctx_type;
187 union {
188 struct {
189 uint32_t type;
190 uint64_t config;
191 } perf;
192 } u;
193} ctx_opts[] = {
194 { "pid", CONTEXT_PID },
95da1297 195 { "procname", CONTEXT_PROCNAME },
b13d56d7
MD
196 { "prio", CONTEXT_PRIO },
197 { "nice", CONTEXT_NICE },
198 { "vpid", CONTEXT_VPID },
199 { "tid", CONTEXT_TID },
200 { "vtid", CONTEXT_VTID },
201 { "ppid", CONTEXT_PPID },
202 { "vppid", CONTEXT_VPPID },
203 /* Perf options */
204 PERF_HW(cpu-cycles, CPU_CYCLES),
205 PERF_HW(cycles, CPU_CYCLES),
206 PERF_HW(stalled-cycles-frontend, STALLED_CYCLES_FRONTEND),
207 PERF_HW(idle-cycles-frontend, STALLED_CYCLES_FRONTEND),
208 PERF_HW(stalled-cycles-backend, STALLED_CYCLES_BACKEND),
209 PERF_HW(idle-cycles-backend, STALLED_CYCLES_BACKEND),
210 PERF_HW(instructions, INSTRUCTIONS),
211 PERF_HW(cache-references, CACHE_REFERENCES),
212 PERF_HW(cache-misses, CACHE_MISSES),
213 PERF_HW(branch-instructions, BRANCH_INSTRUCTIONS),
214 PERF_HW(branches, BRANCH_INSTRUCTIONS),
215 PERF_HW(branch-misses, BRANCH_MISSES),
216 PERF_HW(bus-cycles, BUS_CYCLES),
217
218 PERF_HW_CACHE(L1-dcache, L1D),
219 PERF_HW_CACHE(L1-icache, L1I),
220 PERF_HW_CACHE(LLC, LL),
221 PERF_HW_CACHE(dTLB, DTLB),
222 _PERF_HW_CACHE("iTLB-loads", ITLB, READ, ACCESS),
223 _PERF_HW_CACHE("iTLB-load-misses", ITLB, READ, MISS),
224 _PERF_HW_CACHE("branch-loads", BPU, READ, ACCESS),
225 _PERF_HW_CACHE("branch-load-misses", BPU, READ, MISS),
226
227
228 PERF_SW(cpu-clock, CPU_CLOCK),
229 PERF_SW(task-clock, TASK_CLOCK),
230 PERF_SW(page-fault, PAGE_FAULTS),
231 PERF_SW(faults, PAGE_FAULTS),
232 PERF_SW(major-faults, PAGE_FAULTS_MAJ),
233 PERF_SW(minor-faults, PAGE_FAULTS_MIN),
234 PERF_SW(context-switches, CONTEXT_SWITCHES),
235 PERF_SW(cs, CONTEXT_SWITCHES),
236 PERF_SW(cpu-migrations, CPU_MIGRATIONS),
237 PERF_SW(migrations, CPU_MIGRATIONS),
238 PERF_SW(alignment-faults, ALIGNMENT_FAULTS),
239 PERF_SW(emulation-faults, EMULATION_FAULTS),
240 { NULL, -1 }, /* Closure */
90b9a268
DG
241};
242
b13d56d7
MD
243#undef PERF_SW
244#undef PERF_HW
245
90b9a268 246/*
b13d56d7 247 * Context type for command line option parsing.
90b9a268 248 */
b13d56d7
MD
249struct ctx_type {
250 const struct ctx_opts *opt;
251 struct cds_list_head list;
90b9a268
DG
252};
253
254/*
255 * List of context type. Use to enable multiple context on a single command
256 * line entry.
257 */
258struct ctx_type_list {
259 struct cds_list_head head;
260} ctx_type_list = {
261 .head = CDS_LIST_HEAD_INIT(ctx_type_list.head),
262};
263
90b9a268
DG
264/*
265 * Pretty print context type.
266 */
267static void print_ctx_type(FILE *ofp)
268{
b13d56d7
MD
269 const char *indent = " ";
270 int indent_len = strlen(indent);
271 int len, i = 0;
90b9a268 272
76e3c5dd 273 fprintf(ofp, "%s", indent);
b13d56d7 274 len = indent_len;
90b9a268 275 while (ctx_opts[i].symbol != NULL) {
b13d56d7
MD
276 if (len > indent_len) {
277 if (len + strlen(ctx_opts[i].symbol) + 2
278 >= PRINT_LINE_LEN) {
279 fprintf(ofp, ",\n");
76e3c5dd 280 fprintf(ofp, "%s", indent);
b13d56d7
MD
281 len = indent_len;
282 } else {
283 len += fprintf(ofp, ", ");
90b9a268
DG
284 }
285 }
b13d56d7 286 len += fprintf(ofp, "%s", ctx_opts[i].symbol);
90b9a268
DG
287 i++;
288 }
90b9a268
DG
289}
290
d65106b1
DG
291/*
292 * usage
293 */
294static void usage(FILE *ofp)
295{
b13d56d7 296 fprintf(ofp, "usage: lttng add-context -t TYPE\n");
d65106b1 297 fprintf(ofp, "\n");
55cc08a6 298 fprintf(ofp, "If no event name is given (-e), the context will be added to the channel\n");
b13d56d7 299 fprintf(ofp, "If no channel and no event is given (-c/-e), the context\n");
55cc08a6 300 fprintf(ofp, "will be added to all events and all channels.\n");
3301e740 301 fprintf(ofp, "\n");
d65106b1
DG
302 fprintf(ofp, "Options:\n");
303 fprintf(ofp, " -h, --help Show this help\n");
5440dc42 304 fprintf(ofp, " -s, --session Apply on session name\n");
d65106b1
DG
305 fprintf(ofp, " -c, --channel NAME Apply on channel\n");
306 fprintf(ofp, " -e, --event NAME Apply on event\n");
307 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
55cc08a6 308 fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n");
e14f64a8
DG
309 fprintf(ofp, " If no CMD, the domain used is UST global\n");
310 fprintf(ofp, " or else the domain is UST EXEC_NAME\n");
311 fprintf(ofp, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n");
b13d56d7
MD
312 fprintf(ofp, " -t, --type TYPE Context type. You can repeat that option on\n");
313 fprintf(ofp, " the command line.\n");
314 fprintf(ofp, " TYPE can be one of the strings below:\n");
90b9a268 315 print_ctx_type(ofp);
d65106b1 316 fprintf(ofp, "\n");
90b9a268 317 fprintf(ofp, "Example:\n");
b13d56d7 318 fprintf(ofp, "This command will add the context information 'prio' and two perf\n"
e0899ffa 319 "counters: hardware branch misses and cache misses, to all events\n"
b13d56d7
MD
320 "in the trace data output:\n");
321 fprintf(ofp, "# lttng add-context -k -t prio -t perf:branch-misses -t perf:cache-misses\n");
d65106b1
DG
322 fprintf(ofp, "\n");
323}
324
90b9a268
DG
325/*
326 * Find context numerical value from string.
327 */
328static int find_ctx_type_idx(const char *opt)
329{
330 int ret = -1, i = 0;
331
332 while (ctx_opts[i].symbol != NULL) {
333 if (strcmp(opt, ctx_opts[i].symbol) == 0) {
334 ret = i;
335 goto end;
336 }
337 i++;
338 }
339
340end:
341 return ret;
342}
343
90b9a268
DG
344/*
345 * Add context to channel or event.
d65106b1 346 */
cd80958d 347static int add_context(char *session_name)
d65106b1 348{
b13d56d7 349 int ret = CMD_SUCCESS;
7d29a247
DG
350 struct lttng_event_context context;
351 struct lttng_domain dom;
3301e740 352 struct ctx_type *type;
b13d56d7 353 char *ptr;
d65106b1 354
cd80958d
DG
355 if (opt_kernel) {
356 dom.type = LTTNG_DOMAIN_KERNEL;
55cc08a6
DG
357 } else if (opt_pid != 0) {
358 dom.type = LTTNG_DOMAIN_UST_PID;
359 dom.attr.pid = opt_pid;
360 DBG("PID %d set to lttng handle", opt_pid);
361 } else if (opt_userspace && opt_cmd_name == NULL) {
362 dom.type = LTTNG_DOMAIN_UST;
363 } else if (opt_userspace && opt_cmd_name != NULL) {
364 dom.type = LTTNG_DOMAIN_UST_EXEC_NAME;
365 strncpy(dom.attr.exec_name, opt_cmd_name, NAME_MAX);
366 } else {
e14f64a8 367 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
55cc08a6
DG
368 ret = CMD_NOT_IMPLEMENTED;
369 goto error;
cd80958d
DG
370 }
371
372 handle = lttng_create_handle(session_name, &dom);
373 if (handle == NULL) {
374 ret = -1;
375 goto error;
376 }
377
3301e740
DG
378 /* Iterate over all context type given */
379 cds_list_for_each_entry(type, &ctx_type_list.head, list) {
b13d56d7
MD
380 context.ctx = type->opt->ctx_type;
381 if (context.ctx == LTTNG_EVENT_CONTEXT_PERF_COUNTER) {
382 context.u.perf_counter.type = type->opt->u.perf.type;
383 context.u.perf_counter.config = type->opt->u.perf.config;
d7b91b3c 384 strcpy(context.u.perf_counter.name, type->opt->symbol);
b13d56d7
MD
385 /* Replace : and - by _ */
386 while ((ptr = strchr(context.u.perf_counter.name, '-')) != NULL) {
387 *ptr = '_';
3301e740 388 }
b13d56d7
MD
389 while ((ptr = strchr(context.u.perf_counter.name, ':')) != NULL) {
390 *ptr = '_';
3301e740 391 }
3301e740 392 }
55cc08a6
DG
393 DBG("Adding context...");
394
395 ret = lttng_add_context(handle, &context, opt_event_name,
396 opt_channel_name);
397 if (ret < 0) {
398 fprintf(stderr, "%s: ", type->opt->symbol);
399 continue;
d65106b1 400 } else {
55cc08a6
DG
401 MSG("%s context %s added to %s event in %s",
402 opt_kernel ? "kernel" : "UST", type->opt->symbol,
403 opt_event_name ? opt_event_name : "all",
404 opt_channel_name ? opt_channel_name : "all channels");
d65106b1 405 }
d65106b1
DG
406 }
407
408error:
cd80958d
DG
409 lttng_destroy_handle(handle);
410
d65106b1
DG
411 return ret;
412}
413
414/*
90b9a268 415 * Add context on channel or event.
d65106b1
DG
416 */
417int cmd_add_context(int argc, const char **argv)
418{
90b9a268 419 int index, opt, ret = CMD_SUCCESS;
d65106b1 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:
3301e740
DG
439 type = malloc(sizeof(struct ctx_type));
440 if (type == NULL) {
441 perror("malloc ctx_type");
442 ret = -1;
636167b6
DG
443 goto end;
444 }
6caa2bcc 445 index = find_ctx_type_idx(opt_type);
b13d56d7 446 if (index < 0) {
6caa2bcc 447 ERR("Unknown context type %s", opt_type);
b13d56d7 448 goto end;
90b9a268 449 }
b13d56d7
MD
450 type->opt = &ctx_opts[index];
451 if (type->opt->ctx_type == -1) {
6caa2bcc 452 ERR("Unknown context type %s", opt_type);
90b9a268
DG
453 } else {
454 cds_list_add(&type->list, &ctx_type_list.head);
455 }
d65106b1 456 break;
eeac7d46
MD
457 case OPT_USERSPACE:
458 opt_userspace = 1;
459 opt_cmd_name = poptGetOptArg(pc);
460 break;
d65106b1
DG
461 default:
462 usage(stderr);
463 ret = CMD_UNDEFINED;
464 goto end;
465 }
466 }
467
cd80958d
DG
468 if (!opt_session_name) {
469 session_name = get_session_name();
470 if (session_name == NULL) {
471 ret = -1;
472 goto end;
473 }
474 } else {
475 session_name = opt_session_name;
476 }
477
478 ret = add_context(session_name);
3301e740
DG
479
480 /* Cleanup allocated memory */
b13d56d7 481 cds_list_for_each_entry_safe(type, tmptype, &ctx_type_list.head, list) {
3301e740
DG
482 free(type);
483 }
484
d65106b1
DG
485end:
486 return ret;
487}
This page took 0.046642 seconds and 4 git commands to generate.