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