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