Add new API call lttng_channel_set_default_attr
[lttng-tools.git] / lttng / commands / add_context.c
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
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
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
20 #include <ctype.h>
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
29 #include <urcu/list.h>
30
31 #include "../cmd.h"
32 #include "../conf.h"
33 #include "../utils.h"
34
35 #define PRINT_LINE_LEN 80
36
37 static char *opt_event_name;
38 static char *opt_channel_name;
39 static char *opt_session_name;
40 static int *opt_kernel;
41 static int opt_pid_all;
42 static int opt_userspace;
43 static char *opt_cmd_name;
44 static pid_t opt_pid;
45 static char *opt_type;
46
47 enum {
48 OPT_HELP = 1,
49 OPT_TYPE,
50 OPT_USERSPACE,
51 };
52
53 static struct lttng_handle *handle;
54
55 /*
56 * Taken from the LTTng ABI
57 */
58 enum context_type {
59 CONTEXT_PID = 0,
60 CONTEXT_PERF_COUNTER = 1,
61 CONTEXT_PROCNAME = 2,
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,
69 };
70
71 /*
72 * Taken from the Perf ABI (all enum perf_*)
73 */
74 enum perf_type {
75 PERF_TYPE_HARDWARE = 0,
76 PERF_TYPE_SOFTWARE = 1,
77 PERF_TYPE_HW_CACHE = 3,
78 };
79
80 enum perf_count_hard {
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,
90 };
91
92 enum 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,
102 };
103
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 */
111 enum 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
122 enum 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
130 enum 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
137 static struct poptOption long_options[] = {
138 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
139 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
140 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
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},
144 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, 0, OPT_USERSPACE, 0, 0},
145 {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0},
146 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
147 {"type", 't', POPT_ARG_STRING, &opt_type, OPT_TYPE, 0, 0},
148 {0, 0, 0, 0, 0, 0, 0}
149 };
150
151 /*
152 * Context options
153 */
154 #define PERF_HW(opt, name) \
155 { \
156 "perf:" #opt, CONTEXT_PERF_COUNTER, \
157 .u.perf = { PERF_TYPE_HARDWARE, PERF_COUNT_HW_##name, },\
158 }
159
160 #define PERF_SW(opt, name) \
161 { \
162 "perf:" #opt, CONTEXT_PERF_COUNTER, \
163 .u.perf = { PERF_TYPE_SOFTWARE, PERF_COUNT_SW_##name, },\
164 }
165
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 \
172 | ((uint64_t) PERF_COUNT_HW_CACHE_OP_##op << 8) \
173 | ((uint64_t) PERF_COUNT_HW_CACHE_RESULT_##result << 16), \
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
185 static
186 const struct ctx_opts {
187 char *symbol;
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 },
197 { "procname", CONTEXT_PROCNAME },
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 */
243 };
244
245 #undef PERF_SW
246 #undef PERF_HW
247
248 /*
249 * Context type for command line option parsing.
250 */
251 struct ctx_type {
252 const struct ctx_opts *opt;
253 struct cds_list_head list;
254 };
255
256 /*
257 * List of context type. Use to enable multiple context on a single command
258 * line entry.
259 */
260 struct 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
266 /*
267 * Pretty print context type.
268 */
269 static void print_ctx_type(FILE *ofp)
270 {
271 const char *indent = " ";
272 int indent_len = strlen(indent);
273 int len, i = 0;
274
275 fprintf(ofp, "%s", indent);
276 len = indent_len;
277 while (ctx_opts[i].symbol != NULL) {
278 if (len > indent_len) {
279 if (len + strlen(ctx_opts[i].symbol) + 2
280 >= PRINT_LINE_LEN) {
281 fprintf(ofp, ",\n");
282 fprintf(ofp, "%s", indent);
283 len = indent_len;
284 } else {
285 len += fprintf(ofp, ", ");
286 }
287 }
288 len += fprintf(ofp, "%s", ctx_opts[i].symbol);
289 i++;
290 }
291 }
292
293 /*
294 * usage
295 */
296 static void usage(FILE *ofp)
297 {
298 fprintf(ofp, "usage: lttng add-context -t TYPE\n");
299 fprintf(ofp, "\n");
300 fprintf(ofp, "If no event name is given (-e), the context will be added to\n");
301 fprintf(ofp, "all events in the channel.\n");
302 fprintf(ofp, "If no channel and no event is given (-c/-e), the context\n");
303 fprintf(ofp, "will be added to all events in all channels.\n");
304 fprintf(ofp, "\n");
305 fprintf(ofp, "Options:\n");
306 fprintf(ofp, " -h, --help Show this help\n");
307 fprintf(ofp, " -s, --session Apply on session name\n");
308 fprintf(ofp, " -c, --channel NAME Apply on channel\n");
309 fprintf(ofp, " -e, --event NAME Apply on event\n");
310 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
311 fprintf(ofp, " -u, --userspace Apply for the user-space tracer\n");
312 fprintf(ofp, " --all If -u, apply on all traceable apps\n");
313 fprintf(ofp, " -p, --pid PID If -u, apply on a specific PID\n");
314 fprintf(ofp, " -t, --type TYPE Context type. You can repeat that option on\n");
315 fprintf(ofp, " the command line.\n");
316 fprintf(ofp, " TYPE can be one of the strings below:\n");
317 print_ctx_type(ofp);
318 fprintf(ofp, "\n");
319 fprintf(ofp, "Example:\n");
320 fprintf(ofp, "This command will add the context information 'prio' and two perf\n"
321 "counters: hardware branch misses and cache misses, to all events\n"
322 "in the trace data output:\n");
323 fprintf(ofp, "# lttng add-context -k -t prio -t perf:branch-misses -t perf:cache-misses\n");
324 fprintf(ofp, "\n");
325 }
326
327 /*
328 * Find context numerical value from string.
329 */
330 static int find_ctx_type_idx(const char *opt)
331 {
332 int ret = -1, i = 0;
333
334 while (ctx_opts[i].symbol != NULL) {
335 if (strcmp(opt, ctx_opts[i].symbol) == 0) {
336 ret = i;
337 goto end;
338 }
339 i++;
340 }
341
342 end:
343 return ret;
344 }
345
346 /*
347 * Add context to channel or event.
348 */
349 static int add_context(char *session_name)
350 {
351 int ret = CMD_SUCCESS;
352 struct lttng_event_context context;
353 struct lttng_domain dom;
354 struct ctx_type *type;
355 char *ptr;
356
357 if (opt_kernel) {
358 dom.type = LTTNG_DOMAIN_KERNEL;
359 }
360
361 handle = lttng_create_handle(session_name, &dom);
362 if (handle == NULL) {
363 ret = -1;
364 goto error;
365 }
366
367 /* Iterate over all context type given */
368 cds_list_for_each_entry(type, &ctx_type_list.head, list) {
369
370 context.ctx = type->opt->ctx_type;
371 if (context.ctx == LTTNG_EVENT_CONTEXT_PERF_COUNTER) {
372 context.u.perf_counter.type = type->opt->u.perf.type;
373 context.u.perf_counter.config = type->opt->u.perf.config;
374 strcpy(context.u.perf_counter.name, type->opt->symbol);
375 /* Replace : and - by _ */
376 while ((ptr = strchr(context.u.perf_counter.name, '-')) != NULL) {
377 *ptr = '_';
378 }
379 while ((ptr = strchr(context.u.perf_counter.name, ':')) != NULL) {
380 *ptr = '_';
381 }
382 }
383 if (opt_kernel) {
384 DBG("Adding kernel context");
385 ret = lttng_add_context(handle, &context, opt_event_name,
386 opt_channel_name);
387 if (ret < 0) {
388 fprintf(stderr, "%s: ", type->opt->symbol);
389 continue;
390 } else {
391 MSG("Kernel context %s added", type->opt->symbol);
392 }
393 } else if (opt_userspace) { /* User-space tracer action */
394 /*
395 * TODO: Waiting on lttng UST 2.0
396 */
397 if (opt_pid_all) {
398 } else if (opt_pid != 0) {
399 }
400 ret = CMD_NOT_IMPLEMENTED;
401 goto error;
402 } else {
403 ERR("Please specify a tracer (--kernel or --userspace)");
404 goto error;
405 }
406 }
407
408 error:
409 lttng_destroy_handle(handle);
410
411 return ret;
412 }
413
414 /*
415 * Add context on channel or event.
416 */
417 int cmd_add_context(int argc, const char **argv)
418 {
419 int index, opt, ret = CMD_SUCCESS;
420 static poptContext pc;
421 struct ctx_type *type, *tmptype;
422 char *session_name = NULL;
423
424 if (argc < 2) {
425 usage(stderr);
426 goto end;
427 }
428
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 type = malloc(sizeof(struct ctx_type));
440 if (type == NULL) {
441 perror("malloc ctx_type");
442 ret = -1;
443 goto end;
444 }
445 index = find_ctx_type_idx(opt_type);
446 if (index < 0) {
447 ERR("Unknown context type %s", opt_type);
448 goto end;
449 }
450 type->opt = &ctx_opts[index];
451 if (type->opt->ctx_type == -1) {
452 ERR("Unknown context type %s", opt_type);
453 } else {
454 cds_list_add(&type->list, &ctx_type_list.head);
455 }
456 break;
457 case OPT_USERSPACE:
458 opt_userspace = 1;
459 opt_cmd_name = poptGetOptArg(pc);
460 break;
461 default:
462 usage(stderr);
463 ret = CMD_UNDEFINED;
464 goto end;
465 }
466 }
467
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);
479
480 /* Cleanup allocated memory */
481 cds_list_for_each_entry_safe(type, tmptype, &ctx_type_list.head, list) {
482 free(type);
483 }
484
485 end:
486 return ret;
487 }
This page took 0.038364 seconds and 4 git commands to generate.