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