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