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