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