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