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