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