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