7aef4d50f7dd575e34c704f5b2a82896af8f5db1
[lttng-tools.git] / src / bin / lttng / commands / add_context.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
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 along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _LGPL_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 #include <assert.h>
29
30 #include <urcu/list.h>
31
32 #include <common/mi-lttng.h>
33
34 #include "../command.h"
35
36 static char *opt_channel_name;
37 static char *opt_session_name;
38 static int opt_kernel;
39 static int opt_userspace;
40 static int opt_jul;
41 static int opt_log4j;
42 static char *opt_type;
43
44 #ifdef LTTNG_EMBED_HELP
45 static const char help_msg[] =
46 #include <lttng-add-context.1.h>
47 ;
48 #endif
49
50 enum {
51 OPT_HELP = 1,
52 OPT_TYPE,
53 OPT_USERSPACE,
54 OPT_JUL,
55 OPT_LOG4J,
56 OPT_LIST_OPTIONS,
57 OPT_LIST,
58 };
59
60 static struct lttng_handle *handle;
61 static struct mi_writer *writer;
62
63 /*
64 * Taken from the LTTng ABI
65 */
66 enum context_type {
67 CONTEXT_PID = 0,
68 CONTEXT_PERF_COUNTER = 1, /* Backward compat. */
69 CONTEXT_PROCNAME = 2,
70 CONTEXT_PRIO = 3,
71 CONTEXT_NICE = 4,
72 CONTEXT_VPID = 5,
73 CONTEXT_TID = 6,
74 CONTEXT_VTID = 7,
75 CONTEXT_PPID = 8,
76 CONTEXT_VPPID = 9,
77 CONTEXT_PTHREAD_ID = 10,
78 CONTEXT_HOSTNAME = 11,
79 CONTEXT_IP = 12,
80 CONTEXT_PERF_CPU_COUNTER = 13,
81 CONTEXT_PERF_THREAD_COUNTER = 14,
82 CONTEXT_APP_CONTEXT = 15,
83 CONTEXT_INTERRUPTIBLE = 16,
84 CONTEXT_PREEMPTIBLE = 17,
85 CONTEXT_NEED_RESCHEDULE = 18,
86 CONTEXT_MIGRATABLE = 19,
87 CONTEXT_CALLSTACK_KERNEL = 20,
88 CONTEXT_CALLSTACK_USER = 21,
89 };
90
91 /*
92 * Taken from the Perf ABI (all enum perf_*)
93 */
94 enum perf_type {
95 PERF_TYPE_HARDWARE = 0,
96 PERF_TYPE_SOFTWARE = 1,
97 PERF_TYPE_HW_CACHE = 3,
98 PERF_TYPE_RAW = 4,
99 };
100
101 enum perf_count_hard {
102 PERF_COUNT_HW_CPU_CYCLES = 0,
103 PERF_COUNT_HW_INSTRUCTIONS = 1,
104 PERF_COUNT_HW_CACHE_REFERENCES = 2,
105 PERF_COUNT_HW_CACHE_MISSES = 3,
106 PERF_COUNT_HW_BRANCH_INSTRUCTIONS = 4,
107 PERF_COUNT_HW_BRANCH_MISSES = 5,
108 PERF_COUNT_HW_BUS_CYCLES = 6,
109 PERF_COUNT_HW_STALLED_CYCLES_FRONTEND = 7,
110 PERF_COUNT_HW_STALLED_CYCLES_BACKEND = 8,
111 };
112
113 enum perf_count_soft {
114 PERF_COUNT_SW_CPU_CLOCK = 0,
115 PERF_COUNT_SW_TASK_CLOCK = 1,
116 PERF_COUNT_SW_PAGE_FAULTS = 2,
117 PERF_COUNT_SW_CONTEXT_SWITCHES = 3,
118 PERF_COUNT_SW_CPU_MIGRATIONS = 4,
119 PERF_COUNT_SW_PAGE_FAULTS_MIN = 5,
120 PERF_COUNT_SW_PAGE_FAULTS_MAJ = 6,
121 PERF_COUNT_SW_ALIGNMENT_FAULTS = 7,
122 PERF_COUNT_SW_EMULATION_FAULTS = 8,
123 };
124
125 /*
126 * Generalized hardware cache events:
127 *
128 * { L1-D, L1-I, LLC, ITLB, DTLB, BPU } x
129 * { read, write, prefetch } x
130 * { accesses, misses }
131 */
132 enum perf_hw_cache_id {
133 PERF_COUNT_HW_CACHE_L1D = 0,
134 PERF_COUNT_HW_CACHE_L1I = 1,
135 PERF_COUNT_HW_CACHE_LL = 2,
136 PERF_COUNT_HW_CACHE_DTLB = 3,
137 PERF_COUNT_HW_CACHE_ITLB = 4,
138 PERF_COUNT_HW_CACHE_BPU = 5,
139
140 PERF_COUNT_HW_CACHE_MAX, /* non-ABI */
141 };
142
143 enum perf_hw_cache_op_id {
144 PERF_COUNT_HW_CACHE_OP_READ = 0,
145 PERF_COUNT_HW_CACHE_OP_WRITE = 1,
146 PERF_COUNT_HW_CACHE_OP_PREFETCH = 2,
147
148 PERF_COUNT_HW_CACHE_OP_MAX, /* non-ABI */
149 };
150
151 enum perf_hw_cache_op_result_id {
152 PERF_COUNT_HW_CACHE_RESULT_ACCESS = 0,
153 PERF_COUNT_HW_CACHE_RESULT_MISS = 1,
154
155 PERF_COUNT_HW_CACHE_RESULT_MAX, /* non-ABI */
156 };
157
158 static struct poptOption long_options[] = {
159 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
160 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
161 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
162 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
163 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
164 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
165 {"jul", 'j', POPT_ARG_NONE, 0, OPT_JUL, 0, 0},
166 {"log4j", 'l', POPT_ARG_NONE, 0, OPT_LOG4J, 0, 0},
167 {"type", 't', POPT_ARG_STRING, &opt_type, OPT_TYPE, 0, 0},
168 {"list", 0, POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL},
169 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
170 {0, 0, 0, 0, 0, 0, 0}
171 };
172
173 /*
174 * Context options
175 */
176 #define PERF_HW(optstr, name, type, hide) \
177 { \
178 optstr, type, hide, \
179 .u.perf = { PERF_TYPE_HARDWARE, PERF_COUNT_HW_##name, },\
180 }
181
182 #define PERF_SW(optstr, name, type, hide) \
183 { \
184 optstr, type, hide, \
185 .u.perf = { PERF_TYPE_SOFTWARE, PERF_COUNT_SW_##name, },\
186 }
187
188 #define _PERF_HW_CACHE(optstr, name, type, op, result, hide) \
189 { \
190 optstr, type, hide, \
191 .u.perf = { \
192 PERF_TYPE_HW_CACHE, \
193 (uint64_t) PERF_COUNT_HW_CACHE_##name \
194 | ((uint64_t) PERF_COUNT_HW_CACHE_OP_##op << 8) \
195 | ((uint64_t) PERF_COUNT_HW_CACHE_RESULT_##result << 16), \
196 }, \
197 }
198
199 #define PERF_HW_CACHE(optstr, name, type, hide) \
200 _PERF_HW_CACHE(optstr "-loads", name, type, \
201 READ, ACCESS, hide), \
202 _PERF_HW_CACHE(optstr "-load-misses", name, type, \
203 READ, MISS, hide), \
204 _PERF_HW_CACHE(optstr "-stores", name, type, \
205 WRITE, ACCESS, hide), \
206 _PERF_HW_CACHE(optstr "-store-misses", name, type, \
207 WRITE, MISS, hide), \
208 _PERF_HW_CACHE(optstr "-prefetches", name, type, \
209 PREFETCH, ACCESS, hide), \
210 _PERF_HW_CACHE(optstr "-prefetch-misses", name, type, \
211 PREFETCH, MISS, hide)
212
213 static
214 const struct ctx_opts {
215 char *symbol;
216 enum context_type ctx_type;
217 int hide_help; /* Hide from --help */
218 union {
219 struct {
220 uint32_t type;
221 uint64_t config;
222 } perf;
223 struct {
224 char *provider_name;
225 char *ctx_name;
226 } app_ctx;
227 } u;
228 } ctx_opts[] = {
229 { "pid", CONTEXT_PID },
230 { "procname", CONTEXT_PROCNAME },
231 { "prio", CONTEXT_PRIO },
232 { "nice", CONTEXT_NICE },
233 { "vpid", CONTEXT_VPID },
234 { "tid", CONTEXT_TID },
235 { "pthread_id", CONTEXT_PTHREAD_ID },
236 { "vtid", CONTEXT_VTID },
237 { "ppid", CONTEXT_PPID },
238 { "vppid", CONTEXT_VPPID },
239 { "hostname", CONTEXT_HOSTNAME },
240 { "ip", CONTEXT_IP },
241 { "interruptible", CONTEXT_INTERRUPTIBLE },
242 { "preemptible", CONTEXT_PREEMPTIBLE },
243 { "need_reschedule", CONTEXT_NEED_RESCHEDULE },
244 { "migratable", CONTEXT_MIGRATABLE },
245 { "callstack-kernel", CONTEXT_CALLSTACK_KERNEL },
246 #if HAVE_MODULES_USERSPACE_CALLSTACK_CONTEXT
247 { "callstack-user", CONTEXT_CALLSTACK_USER },
248 #endif
249
250 /* Perf options */
251
252 /* Perf per-CPU counters */
253 PERF_HW("perf:cpu:cpu-cycles", CPU_CYCLES,
254 CONTEXT_PERF_CPU_COUNTER, 0),
255 PERF_HW("perf:cpu:cycles", CPU_CYCLES,
256 CONTEXT_PERF_CPU_COUNTER, 0),
257 PERF_HW("perf:cpu:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND,
258 CONTEXT_PERF_CPU_COUNTER, 0),
259 PERF_HW("perf:cpu:idle-cycles-frontend", STALLED_CYCLES_FRONTEND,
260 CONTEXT_PERF_CPU_COUNTER, 0),
261 PERF_HW("perf:cpu:stalled-cycles-backend", STALLED_CYCLES_BACKEND,
262 CONTEXT_PERF_CPU_COUNTER, 0),
263 PERF_HW("perf:cpu:idle-cycles-backend", STALLED_CYCLES_BACKEND,
264 CONTEXT_PERF_CPU_COUNTER, 0),
265 PERF_HW("perf:cpu:instructions", INSTRUCTIONS,
266 CONTEXT_PERF_CPU_COUNTER, 0),
267 PERF_HW("perf:cpu:cache-references", CACHE_REFERENCES,
268 CONTEXT_PERF_CPU_COUNTER, 0),
269 PERF_HW("perf:cpu:cache-misses", CACHE_MISSES,
270 CONTEXT_PERF_CPU_COUNTER, 0),
271 PERF_HW("perf:cpu:branch-instructions", BRANCH_INSTRUCTIONS,
272 CONTEXT_PERF_CPU_COUNTER, 0),
273 PERF_HW("perf:cpu:branches", BRANCH_INSTRUCTIONS,
274 CONTEXT_PERF_CPU_COUNTER, 0),
275 PERF_HW("perf:cpu:branch-misses", BRANCH_MISSES,
276 CONTEXT_PERF_CPU_COUNTER, 0),
277 PERF_HW("perf:cpu:bus-cycles", BUS_CYCLES,
278 CONTEXT_PERF_CPU_COUNTER, 0),
279
280 PERF_HW_CACHE("perf:cpu:L1-dcache", L1D,
281 CONTEXT_PERF_CPU_COUNTER, 0),
282 PERF_HW_CACHE("perf:cpu:L1-icache", L1I,
283 CONTEXT_PERF_CPU_COUNTER, 0),
284 PERF_HW_CACHE("perf:cpu:LLC", LL,
285 CONTEXT_PERF_CPU_COUNTER, 0),
286 PERF_HW_CACHE("perf:cpu:dTLB", DTLB,
287 CONTEXT_PERF_CPU_COUNTER, 0),
288 _PERF_HW_CACHE("perf:cpu:iTLB-loads", ITLB,
289 CONTEXT_PERF_CPU_COUNTER, READ, ACCESS, 0),
290 _PERF_HW_CACHE("perf:cpu:iTLB-load-misses", ITLB,
291 CONTEXT_PERF_CPU_COUNTER, READ, MISS, 0),
292 _PERF_HW_CACHE("perf:cpu:branch-loads", BPU,
293 CONTEXT_PERF_CPU_COUNTER, READ, ACCESS, 0),
294 _PERF_HW_CACHE("perf:cpu:branch-load-misses", BPU,
295 CONTEXT_PERF_CPU_COUNTER, READ, MISS, 0),
296
297 PERF_SW("perf:cpu:cpu-clock", CPU_CLOCK,
298 CONTEXT_PERF_CPU_COUNTER, 0),
299 PERF_SW("perf:cpu:task-clock", TASK_CLOCK,
300 CONTEXT_PERF_CPU_COUNTER, 0),
301 PERF_SW("perf:cpu:page-fault", PAGE_FAULTS,
302 CONTEXT_PERF_CPU_COUNTER, 0),
303 PERF_SW("perf:cpu:faults", PAGE_FAULTS,
304 CONTEXT_PERF_CPU_COUNTER, 0),
305 PERF_SW("perf:cpu:major-faults", PAGE_FAULTS_MAJ,
306 CONTEXT_PERF_CPU_COUNTER, 0),
307 PERF_SW("perf:cpu:minor-faults", PAGE_FAULTS_MIN,
308 CONTEXT_PERF_CPU_COUNTER, 0),
309 PERF_SW("perf:cpu:context-switches", CONTEXT_SWITCHES,
310 CONTEXT_PERF_CPU_COUNTER, 0),
311 PERF_SW("perf:cpu:cs", CONTEXT_SWITCHES,
312 CONTEXT_PERF_CPU_COUNTER, 0),
313 PERF_SW("perf:cpu:cpu-migrations", CPU_MIGRATIONS,
314 CONTEXT_PERF_CPU_COUNTER, 0),
315 PERF_SW("perf:cpu:migrations", CPU_MIGRATIONS,
316 CONTEXT_PERF_CPU_COUNTER, 0),
317 PERF_SW("perf:cpu:alignment-faults", ALIGNMENT_FAULTS,
318 CONTEXT_PERF_CPU_COUNTER, 0),
319 PERF_SW("perf:cpu:emulation-faults", EMULATION_FAULTS,
320 CONTEXT_PERF_CPU_COUNTER, 0),
321
322 /* Perf per-thread counters */
323 PERF_HW("perf:thread:cpu-cycles", CPU_CYCLES,
324 CONTEXT_PERF_THREAD_COUNTER, 0),
325 PERF_HW("perf:thread:cycles", CPU_CYCLES,
326 CONTEXT_PERF_THREAD_COUNTER, 0),
327 PERF_HW("perf:thread:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND,
328 CONTEXT_PERF_THREAD_COUNTER, 0),
329 PERF_HW("perf:thread:idle-cycles-frontend", STALLED_CYCLES_FRONTEND,
330 CONTEXT_PERF_THREAD_COUNTER, 0),
331 PERF_HW("perf:thread:stalled-cycles-backend", STALLED_CYCLES_BACKEND,
332 CONTEXT_PERF_THREAD_COUNTER, 0),
333 PERF_HW("perf:thread:idle-cycles-backend", STALLED_CYCLES_BACKEND,
334 CONTEXT_PERF_THREAD_COUNTER, 0),
335 PERF_HW("perf:thread:instructions", INSTRUCTIONS,
336 CONTEXT_PERF_THREAD_COUNTER, 0),
337 PERF_HW("perf:thread:cache-references", CACHE_REFERENCES,
338 CONTEXT_PERF_THREAD_COUNTER, 0),
339 PERF_HW("perf:thread:cache-misses", CACHE_MISSES,
340 CONTEXT_PERF_THREAD_COUNTER, 0),
341 PERF_HW("perf:thread:branch-instructions", BRANCH_INSTRUCTIONS,
342 CONTEXT_PERF_THREAD_COUNTER, 0),
343 PERF_HW("perf:thread:branches", BRANCH_INSTRUCTIONS,
344 CONTEXT_PERF_THREAD_COUNTER, 0),
345 PERF_HW("perf:thread:branch-misses", BRANCH_MISSES,
346 CONTEXT_PERF_THREAD_COUNTER, 0),
347 PERF_HW("perf:thread:bus-cycles", BUS_CYCLES,
348 CONTEXT_PERF_THREAD_COUNTER, 0),
349
350 PERF_HW_CACHE("perf:thread:L1-dcache", L1D,
351 CONTEXT_PERF_THREAD_COUNTER, 0),
352 PERF_HW_CACHE("perf:thread:L1-icache", L1I,
353 CONTEXT_PERF_THREAD_COUNTER, 0),
354 PERF_HW_CACHE("perf:thread:LLC", LL,
355 CONTEXT_PERF_THREAD_COUNTER, 0),
356 PERF_HW_CACHE("perf:thread:dTLB", DTLB,
357 CONTEXT_PERF_THREAD_COUNTER, 0),
358 _PERF_HW_CACHE("perf:thread:iTLB-loads", ITLB,
359 CONTEXT_PERF_THREAD_COUNTER, READ, ACCESS, 0),
360 _PERF_HW_CACHE("perf:thread:iTLB-load-misses", ITLB,
361 CONTEXT_PERF_THREAD_COUNTER, READ, MISS, 0),
362 _PERF_HW_CACHE("perf:thread:branch-loads", BPU,
363 CONTEXT_PERF_THREAD_COUNTER, READ, ACCESS, 0),
364 _PERF_HW_CACHE("perf:thread:branch-load-misses", BPU,
365 CONTEXT_PERF_THREAD_COUNTER, READ, MISS, 0),
366
367 PERF_SW("perf:thread:cpu-clock", CPU_CLOCK,
368 CONTEXT_PERF_THREAD_COUNTER, 0),
369 PERF_SW("perf:thread:task-clock", TASK_CLOCK,
370 CONTEXT_PERF_THREAD_COUNTER, 0),
371 PERF_SW("perf:thread:page-fault", PAGE_FAULTS,
372 CONTEXT_PERF_THREAD_COUNTER, 0),
373 PERF_SW("perf:thread:faults", PAGE_FAULTS,
374 CONTEXT_PERF_THREAD_COUNTER, 0),
375 PERF_SW("perf:thread:major-faults", PAGE_FAULTS_MAJ,
376 CONTEXT_PERF_THREAD_COUNTER, 0),
377 PERF_SW("perf:thread:minor-faults", PAGE_FAULTS_MIN,
378 CONTEXT_PERF_THREAD_COUNTER, 0),
379 PERF_SW("perf:thread:context-switches", CONTEXT_SWITCHES,
380 CONTEXT_PERF_THREAD_COUNTER, 0),
381 PERF_SW("perf:thread:cs", CONTEXT_SWITCHES,
382 CONTEXT_PERF_THREAD_COUNTER, 0),
383 PERF_SW("perf:thread:cpu-migrations", CPU_MIGRATIONS,
384 CONTEXT_PERF_THREAD_COUNTER, 0),
385 PERF_SW("perf:thread:migrations", CPU_MIGRATIONS,
386 CONTEXT_PERF_THREAD_COUNTER, 0),
387 PERF_SW("perf:thread:alignment-faults", ALIGNMENT_FAULTS,
388 CONTEXT_PERF_THREAD_COUNTER, 0),
389 PERF_SW("perf:thread:emulation-faults", EMULATION_FAULTS,
390 CONTEXT_PERF_THREAD_COUNTER, 0),
391
392 /*
393 * Perf per-CPU counters, backward compatibilty for names.
394 * Hidden from help listing.
395 */
396 PERF_HW("perf:cpu-cycles", CPU_CYCLES,
397 CONTEXT_PERF_COUNTER, 1),
398 PERF_HW("perf:cycles", CPU_CYCLES,
399 CONTEXT_PERF_COUNTER, 1),
400 PERF_HW("perf:stalled-cycles-frontend", STALLED_CYCLES_FRONTEND,
401 CONTEXT_PERF_COUNTER, 1),
402 PERF_HW("perf:idle-cycles-frontend", STALLED_CYCLES_FRONTEND,
403 CONTEXT_PERF_COUNTER, 1),
404 PERF_HW("perf:stalled-cycles-backend", STALLED_CYCLES_BACKEND,
405 CONTEXT_PERF_COUNTER, 1),
406 PERF_HW("perf:idle-cycles-backend", STALLED_CYCLES_BACKEND,
407 CONTEXT_PERF_COUNTER, 1),
408 PERF_HW("perf:instructions", INSTRUCTIONS,
409 CONTEXT_PERF_COUNTER, 1),
410 PERF_HW("perf:cache-references", CACHE_REFERENCES,
411 CONTEXT_PERF_COUNTER, 1),
412 PERF_HW("perf:cache-misses", CACHE_MISSES,
413 CONTEXT_PERF_COUNTER, 1),
414 PERF_HW("perf:branch-instructions", BRANCH_INSTRUCTIONS,
415 CONTEXT_PERF_COUNTER, 1),
416 PERF_HW("perf:branches", BRANCH_INSTRUCTIONS,
417 CONTEXT_PERF_COUNTER, 1),
418 PERF_HW("perf:branch-misses", BRANCH_MISSES,
419 CONTEXT_PERF_COUNTER, 1),
420 PERF_HW("perf:bus-cycles", BUS_CYCLES,
421 CONTEXT_PERF_COUNTER, 1),
422
423 PERF_HW_CACHE("perf:L1-dcache", L1D,
424 CONTEXT_PERF_COUNTER, 1),
425 PERF_HW_CACHE("perf:L1-icache", L1I,
426 CONTEXT_PERF_COUNTER, 1),
427 PERF_HW_CACHE("perf:LLC", LL,
428 CONTEXT_PERF_COUNTER, 1),
429 PERF_HW_CACHE("perf:dTLB", DTLB,
430 CONTEXT_PERF_COUNTER, 1),
431 _PERF_HW_CACHE("perf:iTLB-loads", ITLB,
432 CONTEXT_PERF_COUNTER, READ, ACCESS, 1),
433 _PERF_HW_CACHE("perf:iTLB-load-misses", ITLB,
434 CONTEXT_PERF_COUNTER, READ, MISS, 1),
435 _PERF_HW_CACHE("perf:branch-loads", BPU,
436 CONTEXT_PERF_COUNTER, READ, ACCESS, 1),
437 _PERF_HW_CACHE("perf:branch-load-misses", BPU,
438 CONTEXT_PERF_COUNTER, READ, MISS, 1),
439
440 PERF_SW("perf:cpu-clock", CPU_CLOCK,
441 CONTEXT_PERF_COUNTER, 1),
442 PERF_SW("perf:task-clock", TASK_CLOCK,
443 CONTEXT_PERF_COUNTER, 1),
444 PERF_SW("perf:page-fault", PAGE_FAULTS,
445 CONTEXT_PERF_COUNTER, 1),
446 PERF_SW("perf:faults", PAGE_FAULTS,
447 CONTEXT_PERF_COUNTER, 1),
448 PERF_SW("perf:major-faults", PAGE_FAULTS_MAJ,
449 CONTEXT_PERF_COUNTER, 1),
450 PERF_SW("perf:minor-faults", PAGE_FAULTS_MIN,
451 CONTEXT_PERF_COUNTER, 1),
452 PERF_SW("perf:context-switches", CONTEXT_SWITCHES,
453 CONTEXT_PERF_COUNTER, 1),
454 PERF_SW("perf:cs", CONTEXT_SWITCHES,
455 CONTEXT_PERF_COUNTER, 1),
456 PERF_SW("perf:cpu-migrations", CPU_MIGRATIONS,
457 CONTEXT_PERF_COUNTER, 1),
458 PERF_SW("perf:migrations", CPU_MIGRATIONS,
459 CONTEXT_PERF_COUNTER, 1),
460 PERF_SW("perf:alignment-faults", ALIGNMENT_FAULTS,
461 CONTEXT_PERF_COUNTER, 1),
462 PERF_SW("perf:emulation-faults", EMULATION_FAULTS,
463 CONTEXT_PERF_COUNTER, 1),
464
465 { NULL, -1 }, /* Closure */
466 };
467
468 #undef PERF_HW_CACHE
469 #undef _PERF_HW_CACHE
470 #undef PERF_SW
471 #undef PERF_HW
472
473 /*
474 * Context type for command line option parsing.
475 */
476 struct ctx_type {
477 struct ctx_opts *opt;
478 struct cds_list_head list;
479 };
480
481 /*
482 * List of context type. Use to enable multiple context on a single command
483 * line entry.
484 */
485 struct ctx_type_list {
486 struct cds_list_head head;
487 } ctx_type_list = {
488 .head = CDS_LIST_HEAD_INIT(ctx_type_list.head),
489 };
490
491
492
493 /*
494 * Find context numerical value from string.
495 *
496 * Return -1 if not found.
497 */
498 static int find_ctx_type_idx(const char *opt)
499 {
500 int ret, i = 0;
501
502 while (ctx_opts[i].symbol != NULL) {
503 if (strcmp(opt, ctx_opts[i].symbol) == 0) {
504 ret = i;
505 goto end;
506 }
507 i++;
508 }
509
510 ret = -1;
511 end:
512 return ret;
513 }
514
515 static
516 enum lttng_domain_type get_domain(void)
517 {
518 if (opt_kernel) {
519 return LTTNG_DOMAIN_KERNEL;
520 } else if (opt_userspace) {
521 return LTTNG_DOMAIN_UST;
522 } else if (opt_jul) {
523 return LTTNG_DOMAIN_JUL;
524 } else if (opt_log4j) {
525 return LTTNG_DOMAIN_LOG4J;
526 } else {
527 assert(0);
528 }
529 }
530
531 static
532 int mi_open(void)
533 {
534 int ret;
535
536 /* MI check */
537 if (!lttng_opt_mi) {
538 ret = 0;
539 goto end;
540 }
541
542 ret = fileno(stdout);
543 if (ret < 0) {
544 PERROR("Unable to retrieve fileno of stdout");
545 ret = CMD_ERROR;
546 goto end;
547 }
548
549 writer = mi_lttng_writer_create(ret, lttng_opt_mi);
550 if (!writer) {
551 ret = CMD_ERROR;
552 goto end;
553 }
554
555 /* Open command element */
556 ret = mi_lttng_writer_command_open(writer,
557 mi_lttng_element_command_add_context);
558 if (ret) {
559 ret = CMD_ERROR;
560 goto end;
561 }
562
563 /* Open output element */
564 ret = mi_lttng_writer_open_element(writer,
565 mi_lttng_element_command_output);
566 if (ret) {
567 ret = CMD_ERROR;
568 goto end;
569 }
570 end:
571 return ret;
572 }
573
574 static
575 int mi_close(enum cmd_error_code success)
576 {
577 int ret;
578
579 /* MI closing */
580 if (!lttng_opt_mi) {
581 ret = 0;
582 goto end;
583 }
584 /* Close output element */
585 ret = mi_lttng_writer_close_element(writer);
586 if (ret) {
587 ret = CMD_ERROR;
588 goto end;
589 }
590
591 /* Success ? */
592 ret = mi_lttng_writer_write_element_bool(writer,
593 mi_lttng_element_command_success, !success);
594 if (ret) {
595 ret = CMD_ERROR;
596 goto end;
597 }
598
599 /* Command element close */
600 ret = mi_lttng_writer_command_close(writer);
601 if (ret) {
602 ret = CMD_ERROR;
603 goto end;
604 }
605 end:
606 return ret;
607 }
608
609 static
610 void populate_context(struct lttng_event_context *context,
611 const struct ctx_opts *opt)
612 {
613 char *ptr;
614
615 context->ctx = (enum lttng_event_context_type) opt->ctx_type;
616 switch (context->ctx) {
617 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
618 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
619 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
620 context->u.perf_counter.type = opt->u.perf.type;
621 context->u.perf_counter.config = opt->u.perf.config;
622 strncpy(context->u.perf_counter.name, opt->symbol,
623 LTTNG_SYMBOL_NAME_LEN);
624 context->u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
625 /* Replace : and - by _ */
626 while ((ptr = strchr(context->u.perf_counter.name, '-')) != NULL) {
627 *ptr = '_';
628 }
629 while ((ptr = strchr(context->u.perf_counter.name, ':')) != NULL) {
630 *ptr = '_';
631 }
632 break;
633 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
634 context->u.app_ctx.provider_name =
635 opt->u.app_ctx.provider_name;
636 context->u.app_ctx.ctx_name =
637 opt->u.app_ctx.ctx_name;
638 break;
639 default:
640 break;
641 }
642 }
643
644 /*
645 * Pretty print context type.
646 */
647 static
648 int print_ctx_type(void)
649 {
650
651 FILE *ofp = stdout;
652 int i = 0;
653 int ret;
654 struct lttng_event_context context;
655
656 memset(&context, 0, sizeof(context));
657
658 ret = mi_open();
659 if (ret) {
660 ret = CMD_ERROR;
661 goto end;
662 }
663
664 if (lttng_opt_mi) {
665 /* Open a contexts element */
666 ret = mi_lttng_writer_open_element(writer, config_element_contexts);
667 if (ret) {
668 ret = CMD_ERROR;
669 goto end;
670 }
671 }
672
673 while (ctx_opts[i].symbol != NULL) {
674 if (!ctx_opts[i].hide_help) {
675 if (lttng_opt_mi) {
676 populate_context(&context, &ctx_opts[i]);
677 ret = mi_lttng_context(writer, &context, 1);
678 if (ret) {
679 ret = CMD_ERROR;
680 goto end;
681 }
682
683 ret = mi_lttng_writer_write_element_string(
684 writer,
685 mi_lttng_element_context_symbol,
686 ctx_opts[i].symbol);
687 if (ret) {
688 ret = CMD_ERROR;
689 goto end;
690 }
691
692 ret = mi_lttng_writer_close_element(writer);
693 if (ret) {
694 ret = CMD_ERROR;
695 goto end;
696 }
697 } else {
698 fprintf(ofp, "%s\n", ctx_opts[i].symbol);
699 }
700 }
701 i++;
702 }
703
704 if (lttng_opt_mi) {
705 /* Close contexts element */
706 ret = mi_lttng_writer_close_element(writer);
707 if (ret) {
708 goto end;
709 }
710 }
711
712 end:
713 ret = mi_close(ret);
714 if (ret) {
715 ret = CMD_ERROR;
716 }
717 return ret;
718 }
719
720 /*
721 * Add context to channel or event.
722 */
723 static int add_context(char *session_name)
724 {
725 int ret = CMD_SUCCESS, warn = 0, success = 0;
726 struct lttng_event_context context;
727 struct lttng_domain dom;
728 struct ctx_type *type;
729
730 memset(&context, 0, sizeof(context));
731 memset(&dom, 0, sizeof(dom));
732
733 dom.type = get_domain();
734 handle = lttng_create_handle(session_name, &dom);
735 if (handle == NULL) {
736 ret = CMD_ERROR;
737 goto error;
738 }
739
740 if (lttng_opt_mi) {
741 /* Open a contexts element */
742 ret = mi_lttng_writer_open_element(writer, config_element_contexts);
743 if (ret) {
744 goto error;
745 }
746 }
747
748 /* Iterate over all the context types given */
749 cds_list_for_each_entry(type, &ctx_type_list.head, list) {
750 DBG("Adding context...");
751
752 populate_context(&context, type->opt);
753
754 if (lttng_opt_mi) {
755 /* We leave context open the update the success of the command */
756 ret = mi_lttng_context(writer, &context, 1);
757 if (ret) {
758 ret = CMD_ERROR;
759 goto error;
760 }
761
762 ret = mi_lttng_writer_write_element_string(writer,
763 mi_lttng_element_context_symbol,
764 type->opt->symbol);
765 if (ret) {
766 ret = CMD_ERROR;
767 goto error;
768 }
769 }
770
771 ret = lttng_add_context(handle, &context, NULL, opt_channel_name);
772 if (ret < 0) {
773 ERR("%s: %s", type->opt->symbol, lttng_strerror(ret));
774 warn = 1;
775 success = 0;
776 } else {
777 if (opt_channel_name) {
778 MSG("%s context %s added to channel %s",
779 get_domain_str(dom.type), type->opt->symbol,
780 opt_channel_name);
781 } else {
782 MSG("%s context %s added to all channels",
783 get_domain_str(dom.type), type->opt->symbol);
784 }
785 success = 1;
786 }
787
788 if (lttng_opt_mi) {
789 /* Is the single operation a success ? */
790 ret = mi_lttng_writer_write_element_bool(writer,
791 mi_lttng_element_success, success);
792 if (ret) {
793 ret = CMD_ERROR;
794 goto error;
795 }
796
797 /* Close the context element */
798 ret = mi_lttng_writer_close_element(writer);
799 if (ret) {
800 ret = CMD_ERROR;
801 goto error;
802 }
803 }
804 }
805
806 if (lttng_opt_mi) {
807 /* Close contexts element */
808 ret = mi_lttng_writer_close_element(writer);
809 if (ret) {
810 goto error;
811 }
812 }
813
814 ret = CMD_SUCCESS;
815
816 error:
817 lttng_destroy_handle(handle);
818
819 /*
820 * This means that at least one add_context failed and tells the user to
821 * look on stderr for error(s).
822 */
823 if (!ret && warn) {
824 ret = CMD_WARNING;
825 }
826 return ret;
827 }
828
829 static
830 void destroy_ctx_type(struct ctx_type *type)
831 {
832 if (!type) {
833 return;
834 }
835 if (type->opt) {
836 free(type->opt->symbol);
837 }
838 free(type->opt);
839 free(type);
840 }
841
842 static
843 struct ctx_type *create_ctx_type(void)
844 {
845 struct ctx_type *type = zmalloc(sizeof(*type));
846
847 if (!type) {
848 PERROR("malloc ctx_type");
849 goto end;
850 }
851
852 type->opt = zmalloc(sizeof(*type->opt));
853 if (!type->opt) {
854 PERROR("malloc ctx_type options");
855 destroy_ctx_type(type);
856 type = NULL;
857 goto end;
858 }
859 end:
860 return type;
861 }
862
863 static
864 int find_ctx_type_perf_raw(const char *ctx, struct ctx_type *type)
865 {
866 int ret;
867 int field_pos = 0;
868 char *tmp_list, *cur_list;
869
870 cur_list = tmp_list = strdup(ctx);
871 if (!tmp_list) {
872 PERROR("strdup temp list");
873 ret = -ENOMEM;
874 goto end;
875 }
876
877 /* Looking for "perf:[cpu|thread]:raw:<mask>:<name>". */
878 for (;;) {
879 char *next;
880
881 next = strtok(cur_list, ":");
882 if (!next) {
883 break;
884 }
885 cur_list = NULL;
886 switch (field_pos) {
887 case 0:
888 if (strncmp(next, "perf", 4) != 0) {
889 ret = -1;
890 goto end;
891 }
892 break;
893 case 1:
894 if (strncmp(next, "cpu", 3) == 0) {
895 type->opt->ctx_type = CONTEXT_PERF_CPU_COUNTER;
896 } else if (strncmp(next, "thread", 4) == 0) {
897 type->opt->ctx_type = CONTEXT_PERF_THREAD_COUNTER;
898 } else {
899 ret = -1;
900 goto end;
901 }
902 break;
903 case 2:
904 if (strncmp(next, "raw", 3) != 0) {
905 ret = -1;
906 goto end;
907 }
908 break;
909 case 3:
910 {
911 char *endptr;
912
913 if (strlen(next) < 2 || next[0] != 'r') {
914 ERR("Wrong perf raw mask format: expected rNNN");
915 ret = -1;
916 goto end;
917 }
918 errno = 0;
919 type->opt->u.perf.config = strtoll(next + 1, &endptr, 16);
920 if (errno != 0 || !endptr || *endptr) {
921 ERR("Wrong perf raw mask format: expected rNNN");
922 ret = -1;
923 goto end;
924 }
925 break;
926 }
927 case 4:
928 /* name */
929 break;
930 case 5:
931 ERR("Too many ':' in perf raw format");
932 ret = -1;
933 goto end;
934 };
935 field_pos++;
936 }
937
938 if (field_pos < 5) {
939 ERR("Invalid perf counter specifier, expected a specifier of "
940 "the form perf:cpu:raw:rNNN:<name> or "
941 "perf:thread:raw:rNNN:<name>");
942 ret = -1;
943 goto end;
944 }
945
946 ret = 0;
947 goto end;
948
949 end:
950 free(tmp_list);
951 return ret;
952 }
953
954 static
955 struct ctx_type *get_context_type(const char *ctx)
956 {
957 int opt_index, ret;
958 struct ctx_type *type = NULL;
959 const char app_ctx_prefix[] = "$app.";
960 char *provider_name = NULL, *ctx_name = NULL;
961 size_t i, len, colon_pos = 0, provider_name_len, ctx_name_len;
962
963 if (!ctx) {
964 goto not_found;
965 }
966
967 type = create_ctx_type();
968 if (!type) {
969 goto not_found;
970 }
971
972 /* Check if ctx matches a known static context. */
973 opt_index = find_ctx_type_idx(ctx);
974 if (opt_index >= 0) {
975 *type->opt = ctx_opts[opt_index];
976 type->opt->symbol = strdup(ctx_opts[opt_index].symbol);
977 goto found;
978 }
979
980 /* Check if ctx is a raw perf context. */
981 ret = find_ctx_type_perf_raw(ctx, type);
982 if (ret == 0) {
983 type->opt->u.perf.type = PERF_TYPE_RAW;
984 type->opt->symbol = strdup(ctx);
985 if (!type->opt->symbol) {
986 PERROR("Copy perf field name");
987 goto not_found;
988 }
989 goto found;
990 }
991
992 /*
993 * No match found against static contexts; check if it is an app
994 * context.
995 */
996 len = strlen(ctx);
997 if (len <= sizeof(app_ctx_prefix) - 1) {
998 goto not_found;
999 }
1000
1001 /* String starts with $app. */
1002 if (strncmp(ctx, app_ctx_prefix, sizeof(app_ctx_prefix) - 1)) {
1003 goto not_found;
1004 }
1005
1006 /* Validate that the ':' separator is present. */
1007 for (i = sizeof(app_ctx_prefix); i < len; i++) {
1008 const char c = ctx[i];
1009
1010 if (c == ':') {
1011 colon_pos = i;
1012 break;
1013 }
1014 }
1015
1016 /*
1017 * No colon found or no ctx name ("$app.provider:") or no provider name
1018 * given ("$app.:..."), which is invalid.
1019 */
1020 if (!colon_pos || colon_pos == len ||
1021 colon_pos == sizeof(app_ctx_prefix)) {
1022 ERR("Invalid application context provided: no provider or context name provided.");
1023 goto not_found;
1024 }
1025
1026 provider_name_len = colon_pos - sizeof(app_ctx_prefix) + 2;
1027 provider_name = zmalloc(provider_name_len);
1028 if (!provider_name) {
1029 PERROR("malloc provider_name");
1030 goto not_found;
1031 }
1032 strncpy(provider_name, ctx + sizeof(app_ctx_prefix) - 1,
1033 provider_name_len - 1);
1034 type->opt->u.app_ctx.provider_name = provider_name;
1035
1036 ctx_name_len = len - colon_pos;
1037 ctx_name = zmalloc(ctx_name_len);
1038 if (!ctx_name) {
1039 PERROR("malloc ctx_name");
1040 goto not_found;
1041 }
1042 strncpy(ctx_name, ctx + colon_pos + 1, ctx_name_len - 1);
1043 type->opt->u.app_ctx.ctx_name = ctx_name;
1044 type->opt->ctx_type = CONTEXT_APP_CONTEXT;
1045 type->opt->symbol = strdup(ctx);
1046 found:
1047 return type;
1048 not_found:
1049 free(provider_name);
1050 free(ctx_name);
1051 destroy_ctx_type(type);
1052 return NULL;
1053 }
1054
1055 /*
1056 * Add context to channel or event.
1057 */
1058 int cmd_add_context(int argc, const char **argv)
1059 {
1060 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
1061 static poptContext pc;
1062 struct ctx_type *type, *tmptype;
1063 char *session_name = NULL;
1064 const char *leftover = NULL;
1065
1066 if (argc < 2) {
1067 ret = CMD_ERROR;
1068 goto end;
1069 }
1070
1071 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1072 poptReadDefaultConfig(pc, 0);
1073
1074 while ((opt = poptGetNextOpt(pc)) != -1) {
1075 switch (opt) {
1076 case OPT_HELP:
1077 SHOW_HELP();
1078 goto end;
1079 case OPT_LIST:
1080 ret = print_ctx_type();
1081 goto end;
1082 case OPT_TYPE:
1083 {
1084 type = get_context_type(opt_type);
1085 if (!type) {
1086 ERR("Unknown context type %s", opt_type);
1087 ret = CMD_FATAL;
1088 goto end;
1089 }
1090 cds_list_add_tail(&type->list, &ctx_type_list.head);
1091 break;
1092 }
1093 case OPT_USERSPACE:
1094 opt_userspace = 1;
1095 break;
1096 case OPT_JUL:
1097 opt_jul = 1;
1098 break;
1099 case OPT_LOG4J:
1100 opt_log4j = 1;
1101 break;
1102 case OPT_LIST_OPTIONS:
1103 list_cmd_options(stdout, long_options);
1104 goto end;
1105 default:
1106 ret = CMD_UNDEFINED;
1107 goto end;
1108 }
1109 }
1110
1111 leftover = poptGetArg(pc);
1112 if (leftover) {
1113 ERR("Unknown argument: %s", leftover);
1114 ret = CMD_ERROR;
1115 goto end;
1116 }
1117
1118 ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace +
1119 opt_jul + opt_log4j);
1120 if (ret) {
1121 ret = CMD_ERROR;
1122 goto end;
1123 }
1124
1125 if (!opt_type) {
1126 ERR("Missing mandatory -t TYPE");
1127 ret = CMD_ERROR;
1128 goto end;
1129 }
1130
1131 if (!opt_session_name) {
1132 session_name = get_session_name();
1133 if (session_name == NULL) {
1134 ret = CMD_ERROR;
1135 goto end;
1136 }
1137 } else {
1138 session_name = opt_session_name;
1139 }
1140
1141 ret = mi_open();
1142 if (ret) {
1143 goto end;
1144 }
1145
1146 command_ret = add_context(session_name);
1147 ret = mi_close(command_ret);
1148 if (ret) {
1149 goto end;
1150 }
1151
1152 end:
1153 if (!opt_session_name) {
1154 free(session_name);
1155 }
1156
1157 /* Mi clean-up */
1158 if (writer && mi_lttng_writer_destroy(writer)) {
1159 /* Preserve original error code */
1160 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
1161 }
1162
1163 /* Cleanup allocated memory */
1164 cds_list_for_each_entry_safe(type, tmptype, &ctx_type_list.head, list) {
1165 destroy_ctx_type(type);
1166 }
1167
1168 /* Overwrite ret if an error occurred during add_context() */
1169 ret = command_ret ? command_ret : ret;
1170
1171 poptFreeContext(pc);
1172 return ret;
1173 }
This page took 0.063513 seconds and 4 git commands to generate.