Commit | Line | Data |
---|---|---|
2f77fc4b DG |
1 | /* |
2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License, version 2 only, as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
2f77fc4b | 19 | #include <assert.h> |
6dc3064a | 20 | #include <inttypes.h> |
2f77fc4b DG |
21 | #include <urcu/list.h> |
22 | #include <urcu/uatomic.h> | |
23 | ||
24 | #include <common/defaults.h> | |
25 | #include <common/common.h> | |
26 | #include <common/sessiond-comm/sessiond-comm.h> | |
27 | #include <common/relayd/relayd.h> | |
10a50311 | 28 | #include <common/utils.h> |
f5436bfc | 29 | #include <common/compat/string.h> |
2f77fc4b DG |
30 | |
31 | #include "channel.h" | |
32 | #include "consumer.h" | |
33 | #include "event.h" | |
8782cc74 | 34 | #include "health-sessiond.h" |
2f77fc4b DG |
35 | #include "kernel.h" |
36 | #include "kernel-consumer.h" | |
37 | #include "lttng-sessiond.h" | |
38 | #include "utils.h" | |
834978fd | 39 | #include "syscall.h" |
7c1d2758 | 40 | #include "agent.h" |
2f77fc4b DG |
41 | |
42 | #include "cmd.h" | |
43 | ||
44 | /* | |
45 | * Used to keep a unique index for each relayd socket created where this value | |
46 | * is associated with streams on the consumer so it can match the right relayd | |
d88aee68 DG |
47 | * to send to. It must be accessed with the relayd_net_seq_idx_lock |
48 | * held. | |
2f77fc4b | 49 | */ |
d88aee68 DG |
50 | static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER; |
51 | static uint64_t relayd_net_seq_idx; | |
2f77fc4b | 52 | |
7076b56e JG |
53 | static int validate_event_name(const char *); |
54 | static int validate_ust_event_name(const char *); | |
55 | static int cmd_enable_event_internal(struct ltt_session *session, | |
56 | struct lttng_domain *domain, | |
57 | char *channel_name, struct lttng_event *event, | |
58 | char *filter_expression, | |
59 | struct lttng_filter_bytecode *filter, | |
60 | struct lttng_event_exclusion *exclusion, | |
61 | int wpipe); | |
62 | ||
2f77fc4b DG |
63 | /* |
64 | * Create a session path used by list_lttng_sessions for the case that the | |
65 | * session consumer is on the network. | |
66 | */ | |
67 | static int build_network_session_path(char *dst, size_t size, | |
68 | struct ltt_session *session) | |
69 | { | |
70 | int ret, kdata_port, udata_port; | |
71 | struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL; | |
72 | char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX]; | |
73 | ||
74 | assert(session); | |
75 | assert(dst); | |
76 | ||
77 | memset(tmp_urls, 0, sizeof(tmp_urls)); | |
78 | memset(tmp_uurl, 0, sizeof(tmp_uurl)); | |
79 | ||
80 | kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT; | |
81 | ||
82 | if (session->kernel_session && session->kernel_session->consumer) { | |
83 | kuri = &session->kernel_session->consumer->dst.net.control; | |
84 | kdata_port = session->kernel_session->consumer->dst.net.data.port; | |
85 | } | |
86 | ||
87 | if (session->ust_session && session->ust_session->consumer) { | |
88 | uuri = &session->ust_session->consumer->dst.net.control; | |
89 | udata_port = session->ust_session->consumer->dst.net.data.port; | |
90 | } | |
91 | ||
92 | if (uuri == NULL && kuri == NULL) { | |
93 | uri = &session->consumer->dst.net.control; | |
94 | kdata_port = session->consumer->dst.net.data.port; | |
95 | } else if (kuri && uuri) { | |
96 | ret = uri_compare(kuri, uuri); | |
97 | if (ret) { | |
98 | /* Not Equal */ | |
99 | uri = kuri; | |
100 | /* Build uuri URL string */ | |
101 | ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl)); | |
102 | if (ret < 0) { | |
103 | goto error; | |
104 | } | |
105 | } else { | |
106 | uri = kuri; | |
107 | } | |
108 | } else if (kuri && uuri == NULL) { | |
109 | uri = kuri; | |
110 | } else if (uuri && kuri == NULL) { | |
111 | uri = uuri; | |
112 | } | |
113 | ||
114 | ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls)); | |
115 | if (ret < 0) { | |
116 | goto error; | |
117 | } | |
118 | ||
9aa9f900 DG |
119 | /* |
120 | * Do we have a UST url set. If yes, this means we have both kernel and UST | |
121 | * to print. | |
122 | */ | |
9d035200 | 123 | if (*tmp_uurl != '\0') { |
2f77fc4b DG |
124 | ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]", |
125 | tmp_urls, kdata_port, tmp_uurl, udata_port); | |
126 | } else { | |
9aa9f900 | 127 | int dport; |
bef08707 | 128 | if (kuri || (!kuri && !uuri)) { |
9aa9f900 DG |
129 | dport = kdata_port; |
130 | } else { | |
131 | /* No kernel URI, use the UST port. */ | |
132 | dport = udata_port; | |
133 | } | |
134 | ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport); | |
2f77fc4b DG |
135 | } |
136 | ||
137 | error: | |
138 | return ret; | |
139 | } | |
140 | ||
141 | /* | |
142 | * Fill lttng_channel array of all channels. | |
143 | */ | |
56a37563 JG |
144 | static void list_lttng_channels(enum lttng_domain_type domain, |
145 | struct ltt_session *session, struct lttng_channel *channels) | |
2f77fc4b DG |
146 | { |
147 | int i = 0; | |
148 | struct ltt_kernel_channel *kchan; | |
149 | ||
150 | DBG("Listing channels for session %s", session->name); | |
151 | ||
152 | switch (domain) { | |
153 | case LTTNG_DOMAIN_KERNEL: | |
154 | /* Kernel channels */ | |
155 | if (session->kernel_session != NULL) { | |
156 | cds_list_for_each_entry(kchan, | |
157 | &session->kernel_session->channel_list.head, list) { | |
158 | /* Copy lttng_channel struct to array */ | |
159 | memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel)); | |
160 | channels[i].enabled = kchan->enabled; | |
161 | i++; | |
162 | } | |
163 | } | |
164 | break; | |
165 | case LTTNG_DOMAIN_UST: | |
166 | { | |
167 | struct lttng_ht_iter iter; | |
168 | struct ltt_ust_channel *uchan; | |
169 | ||
e7fe706f | 170 | rcu_read_lock(); |
2f77fc4b DG |
171 | cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht, |
172 | &iter.iter, uchan, node.node) { | |
173 | strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN); | |
174 | channels[i].attr.overwrite = uchan->attr.overwrite; | |
175 | channels[i].attr.subbuf_size = uchan->attr.subbuf_size; | |
176 | channels[i].attr.num_subbuf = uchan->attr.num_subbuf; | |
177 | channels[i].attr.switch_timer_interval = | |
178 | uchan->attr.switch_timer_interval; | |
179 | channels[i].attr.read_timer_interval = | |
180 | uchan->attr.read_timer_interval; | |
181 | channels[i].enabled = uchan->enabled; | |
2f785fe7 DG |
182 | channels[i].attr.tracefile_size = uchan->tracefile_size; |
183 | channels[i].attr.tracefile_count = uchan->tracefile_count; | |
2f77fc4b DG |
184 | switch (uchan->attr.output) { |
185 | case LTTNG_UST_MMAP: | |
186 | default: | |
187 | channels[i].attr.output = LTTNG_EVENT_MMAP; | |
188 | break; | |
189 | } | |
190 | i++; | |
191 | } | |
e7fe706f | 192 | rcu_read_unlock(); |
2f77fc4b DG |
193 | break; |
194 | } | |
195 | default: | |
196 | break; | |
197 | } | |
198 | } | |
199 | ||
3c6a091f | 200 | /* |
022d91ba | 201 | * Create a list of agent domain events. |
3c6a091f DG |
202 | * |
203 | * Return number of events in list on success or else a negative value. | |
204 | */ | |
022d91ba | 205 | static int list_lttng_agent_events(struct agent *agt, |
3c6a091f DG |
206 | struct lttng_event **events) |
207 | { | |
208 | int i = 0, ret = 0; | |
209 | unsigned int nb_event = 0; | |
022d91ba | 210 | struct agent_event *event; |
3c6a091f DG |
211 | struct lttng_event *tmp_events; |
212 | struct lttng_ht_iter iter; | |
213 | ||
022d91ba | 214 | assert(agt); |
3c6a091f DG |
215 | assert(events); |
216 | ||
022d91ba | 217 | DBG3("Listing agent events"); |
3c6a091f | 218 | |
e5bbf678 | 219 | rcu_read_lock(); |
022d91ba | 220 | nb_event = lttng_ht_get_count(agt->events); |
e5bbf678 | 221 | rcu_read_unlock(); |
3c6a091f DG |
222 | if (nb_event == 0) { |
223 | ret = nb_event; | |
224 | goto error; | |
225 | } | |
226 | ||
227 | tmp_events = zmalloc(nb_event * sizeof(*tmp_events)); | |
228 | if (!tmp_events) { | |
022d91ba | 229 | PERROR("zmalloc agent events session"); |
3c6a091f DG |
230 | ret = -LTTNG_ERR_FATAL; |
231 | goto error; | |
232 | } | |
233 | ||
234 | rcu_read_lock(); | |
022d91ba | 235 | cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) { |
3c6a091f DG |
236 | strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name)); |
237 | tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0'; | |
238 | tmp_events[i].enabled = event->enabled; | |
2106efa0 | 239 | tmp_events[i].loglevel = event->loglevel_value; |
ff94328f | 240 | tmp_events[i].loglevel_type = event->loglevel_type; |
3c6a091f DG |
241 | i++; |
242 | } | |
243 | rcu_read_unlock(); | |
244 | ||
245 | *events = tmp_events; | |
246 | ret = nb_event; | |
247 | ||
248 | error: | |
249 | assert(nb_event == i); | |
250 | return ret; | |
251 | } | |
252 | ||
2f77fc4b DG |
253 | /* |
254 | * Create a list of ust global domain events. | |
255 | */ | |
256 | static int list_lttng_ust_global_events(char *channel_name, | |
257 | struct ltt_ust_domain_global *ust_global, struct lttng_event **events) | |
258 | { | |
259 | int i = 0, ret = 0; | |
260 | unsigned int nb_event = 0; | |
261 | struct lttng_ht_iter iter; | |
262 | struct lttng_ht_node_str *node; | |
263 | struct ltt_ust_channel *uchan; | |
264 | struct ltt_ust_event *uevent; | |
265 | struct lttng_event *tmp; | |
266 | ||
267 | DBG("Listing UST global events for channel %s", channel_name); | |
268 | ||
269 | rcu_read_lock(); | |
270 | ||
271 | lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter); | |
272 | node = lttng_ht_iter_get_node_str(&iter); | |
273 | if (node == NULL) { | |
f73fabfd | 274 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2f77fc4b DG |
275 | goto error; |
276 | } | |
277 | ||
278 | uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node); | |
279 | ||
3c442be3 | 280 | nb_event = lttng_ht_get_count(uchan->events); |
2f77fc4b DG |
281 | if (nb_event == 0) { |
282 | ret = nb_event; | |
283 | goto error; | |
284 | } | |
285 | ||
286 | DBG3("Listing UST global %d events", nb_event); | |
287 | ||
288 | tmp = zmalloc(nb_event * sizeof(struct lttng_event)); | |
289 | if (tmp == NULL) { | |
f73fabfd | 290 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
291 | goto error; |
292 | } | |
293 | ||
294 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { | |
3c442be3 JG |
295 | if (uevent->internal) { |
296 | /* This event should remain hidden from clients */ | |
297 | nb_event--; | |
298 | continue; | |
299 | } | |
2f77fc4b DG |
300 | strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN); |
301 | tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
302 | tmp[i].enabled = uevent->enabled; | |
303 | ||
304 | switch (uevent->attr.instrumentation) { | |
305 | case LTTNG_UST_TRACEPOINT: | |
306 | tmp[i].type = LTTNG_EVENT_TRACEPOINT; | |
307 | break; | |
308 | case LTTNG_UST_PROBE: | |
309 | tmp[i].type = LTTNG_EVENT_PROBE; | |
310 | break; | |
311 | case LTTNG_UST_FUNCTION: | |
312 | tmp[i].type = LTTNG_EVENT_FUNCTION; | |
313 | break; | |
314 | } | |
315 | ||
316 | tmp[i].loglevel = uevent->attr.loglevel; | |
317 | switch (uevent->attr.loglevel_type) { | |
318 | case LTTNG_UST_LOGLEVEL_ALL: | |
319 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
320 | break; | |
321 | case LTTNG_UST_LOGLEVEL_RANGE: | |
322 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; | |
323 | break; | |
324 | case LTTNG_UST_LOGLEVEL_SINGLE: | |
325 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; | |
326 | break; | |
327 | } | |
328 | if (uevent->filter) { | |
329 | tmp[i].filter = 1; | |
330 | } | |
4634f12e JI |
331 | if (uevent->exclusion) { |
332 | tmp[i].exclusion = 1; | |
333 | } | |
2f77fc4b DG |
334 | i++; |
335 | } | |
336 | ||
337 | ret = nb_event; | |
338 | *events = tmp; | |
339 | ||
340 | error: | |
341 | rcu_read_unlock(); | |
342 | return ret; | |
343 | } | |
344 | ||
345 | /* | |
346 | * Fill lttng_event array of all kernel events in the channel. | |
347 | */ | |
348 | static int list_lttng_kernel_events(char *channel_name, | |
349 | struct ltt_kernel_session *kernel_session, struct lttng_event **events) | |
350 | { | |
351 | int i = 0, ret; | |
352 | unsigned int nb_event; | |
353 | struct ltt_kernel_event *event; | |
354 | struct ltt_kernel_channel *kchan; | |
355 | ||
356 | kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session); | |
357 | if (kchan == NULL) { | |
f73fabfd | 358 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
2f77fc4b DG |
359 | goto error; |
360 | } | |
361 | ||
362 | nb_event = kchan->event_count; | |
363 | ||
364 | DBG("Listing events for channel %s", kchan->channel->name); | |
365 | ||
366 | if (nb_event == 0) { | |
834978fd DG |
367 | *events = NULL; |
368 | goto syscall; | |
2f77fc4b DG |
369 | } |
370 | ||
371 | *events = zmalloc(nb_event * sizeof(struct lttng_event)); | |
372 | if (*events == NULL) { | |
f73fabfd | 373 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
374 | goto error; |
375 | } | |
376 | ||
377 | /* Kernel channels */ | |
378 | cds_list_for_each_entry(event, &kchan->events_list.head , list) { | |
379 | strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN); | |
380 | (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
381 | (*events)[i].enabled = event->enabled; | |
00f992f2 JG |
382 | (*events)[i].filter = |
383 | (unsigned char) !!event->filter_expression; | |
2f77fc4b DG |
384 | |
385 | switch (event->event->instrumentation) { | |
386 | case LTTNG_KERNEL_TRACEPOINT: | |
387 | (*events)[i].type = LTTNG_EVENT_TRACEPOINT; | |
388 | break; | |
2f77fc4b | 389 | case LTTNG_KERNEL_KRETPROBE: |
1896972b DG |
390 | (*events)[i].type = LTTNG_EVENT_FUNCTION; |
391 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, | |
392 | sizeof(struct lttng_kernel_kprobe)); | |
393 | break; | |
394 | case LTTNG_KERNEL_KPROBE: | |
2f77fc4b DG |
395 | (*events)[i].type = LTTNG_EVENT_PROBE; |
396 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, | |
397 | sizeof(struct lttng_kernel_kprobe)); | |
398 | break; | |
399 | case LTTNG_KERNEL_FUNCTION: | |
400 | (*events)[i].type = LTTNG_EVENT_FUNCTION; | |
401 | memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace, | |
402 | sizeof(struct lttng_kernel_function)); | |
403 | break; | |
404 | case LTTNG_KERNEL_NOOP: | |
405 | (*events)[i].type = LTTNG_EVENT_NOOP; | |
406 | break; | |
407 | case LTTNG_KERNEL_SYSCALL: | |
408 | (*events)[i].type = LTTNG_EVENT_SYSCALL; | |
409 | break; | |
410 | case LTTNG_KERNEL_ALL: | |
411 | assert(0); | |
412 | break; | |
413 | } | |
414 | i++; | |
415 | } | |
416 | ||
834978fd DG |
417 | syscall: |
418 | if (syscall_table) { | |
419 | ssize_t new_size; | |
420 | ||
421 | new_size = syscall_list_channel(kchan, events, nb_event); | |
422 | if (new_size < 0) { | |
423 | free(events); | |
424 | ret = -new_size; | |
425 | goto error; | |
426 | } | |
427 | nb_event = new_size; | |
428 | } | |
429 | ||
2f77fc4b DG |
430 | return nb_event; |
431 | ||
432 | error: | |
f73fabfd DG |
433 | /* Negate the error code to differentiate the size from an error */ |
434 | return -ret; | |
2f77fc4b DG |
435 | } |
436 | ||
437 | /* | |
438 | * Add URI so the consumer output object. Set the correct path depending on the | |
439 | * domain adding the default trace directory. | |
440 | */ | |
441 | static int add_uri_to_consumer(struct consumer_output *consumer, | |
56a37563 JG |
442 | struct lttng_uri *uri, enum lttng_domain_type domain, |
443 | const char *session_name) | |
2f77fc4b | 444 | { |
f73fabfd | 445 | int ret = LTTNG_OK; |
2f77fc4b DG |
446 | const char *default_trace_dir; |
447 | ||
448 | assert(uri); | |
449 | ||
450 | if (consumer == NULL) { | |
451 | DBG("No consumer detected. Don't add URI. Stopping."); | |
f73fabfd | 452 | ret = LTTNG_ERR_NO_CONSUMER; |
2f77fc4b DG |
453 | goto error; |
454 | } | |
455 | ||
456 | switch (domain) { | |
457 | case LTTNG_DOMAIN_KERNEL: | |
458 | default_trace_dir = DEFAULT_KERNEL_TRACE_DIR; | |
459 | break; | |
460 | case LTTNG_DOMAIN_UST: | |
461 | default_trace_dir = DEFAULT_UST_TRACE_DIR; | |
462 | break; | |
463 | default: | |
464 | /* | |
465 | * This case is possible is we try to add the URI to the global tracing | |
466 | * session consumer object which in this case there is no subdir. | |
467 | */ | |
468 | default_trace_dir = ""; | |
469 | } | |
470 | ||
471 | switch (uri->dtype) { | |
472 | case LTTNG_DST_IPV4: | |
473 | case LTTNG_DST_IPV6: | |
474 | DBG2("Setting network URI to consumer"); | |
475 | ||
df75acac DG |
476 | if (consumer->type == CONSUMER_DST_NET) { |
477 | if ((uri->stype == LTTNG_STREAM_CONTROL && | |
785d2d0d DG |
478 | consumer->dst.net.control_isset) || |
479 | (uri->stype == LTTNG_STREAM_DATA && | |
480 | consumer->dst.net.data_isset)) { | |
df75acac DG |
481 | ret = LTTNG_ERR_URL_EXIST; |
482 | goto error; | |
483 | } | |
484 | } else { | |
485 | memset(&consumer->dst.net, 0, sizeof(consumer->dst.net)); | |
785d2d0d DG |
486 | } |
487 | ||
df75acac DG |
488 | consumer->type = CONSUMER_DST_NET; |
489 | ||
2f77fc4b DG |
490 | /* Set URI into consumer output object */ |
491 | ret = consumer_set_network_uri(consumer, uri); | |
492 | if (ret < 0) { | |
a74934ba | 493 | ret = -ret; |
2f77fc4b DG |
494 | goto error; |
495 | } else if (ret == 1) { | |
496 | /* | |
497 | * URI was the same in the consumer so we do not append the subdir | |
498 | * again so to not duplicate output dir. | |
499 | */ | |
f86c9f4e | 500 | ret = LTTNG_OK; |
2f77fc4b DG |
501 | goto error; |
502 | } | |
503 | ||
504 | if (uri->stype == LTTNG_STREAM_CONTROL && strlen(uri->subdir) == 0) { | |
505 | ret = consumer_set_subdir(consumer, session_name); | |
506 | if (ret < 0) { | |
f73fabfd | 507 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
508 | goto error; |
509 | } | |
510 | } | |
511 | ||
512 | if (uri->stype == LTTNG_STREAM_CONTROL) { | |
513 | /* On a new subdir, reappend the default trace dir. */ | |
c30ce0b3 CB |
514 | strncat(consumer->subdir, default_trace_dir, |
515 | sizeof(consumer->subdir) - strlen(consumer->subdir) - 1); | |
2f77fc4b DG |
516 | DBG3("Append domain trace name to subdir %s", consumer->subdir); |
517 | } | |
518 | ||
519 | break; | |
520 | case LTTNG_DST_PATH: | |
521 | DBG2("Setting trace directory path from URI to %s", uri->dst.path); | |
522 | memset(consumer->dst.trace_path, 0, | |
523 | sizeof(consumer->dst.trace_path)); | |
524 | strncpy(consumer->dst.trace_path, uri->dst.path, | |
525 | sizeof(consumer->dst.trace_path)); | |
526 | /* Append default trace dir */ | |
527 | strncat(consumer->dst.trace_path, default_trace_dir, | |
c30ce0b3 CB |
528 | sizeof(consumer->dst.trace_path) - |
529 | strlen(consumer->dst.trace_path) - 1); | |
2f77fc4b DG |
530 | /* Flag consumer as local. */ |
531 | consumer->type = CONSUMER_DST_LOCAL; | |
532 | break; | |
533 | } | |
534 | ||
a74934ba DG |
535 | ret = LTTNG_OK; |
536 | ||
2f77fc4b DG |
537 | error: |
538 | return ret; | |
539 | } | |
540 | ||
541 | /* | |
542 | * Init tracing by creating trace directory and sending fds kernel consumer. | |
543 | */ | |
544 | static int init_kernel_tracing(struct ltt_kernel_session *session) | |
545 | { | |
546 | int ret = 0; | |
547 | struct lttng_ht_iter iter; | |
548 | struct consumer_socket *socket; | |
549 | ||
550 | assert(session); | |
551 | ||
e7fe706f DG |
552 | rcu_read_lock(); |
553 | ||
2f77fc4b DG |
554 | if (session->consumer_fds_sent == 0 && session->consumer != NULL) { |
555 | cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter, | |
556 | socket, node.node) { | |
2f77fc4b | 557 | pthread_mutex_lock(socket->lock); |
f50f23d9 | 558 | ret = kernel_consumer_send_session(socket, session); |
2f77fc4b DG |
559 | pthread_mutex_unlock(socket->lock); |
560 | if (ret < 0) { | |
f73fabfd | 561 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
2f77fc4b DG |
562 | goto error; |
563 | } | |
564 | } | |
565 | } | |
566 | ||
567 | error: | |
e7fe706f | 568 | rcu_read_unlock(); |
2f77fc4b DG |
569 | return ret; |
570 | } | |
571 | ||
572 | /* | |
573 | * Create a socket to the relayd using the URI. | |
574 | * | |
575 | * On success, the relayd_sock pointer is set to the created socket. | |
576 | * Else, it's stays untouched and a lttcomm error code is returned. | |
577 | */ | |
6151a90f JD |
578 | static int create_connect_relayd(struct lttng_uri *uri, |
579 | struct lttcomm_relayd_sock **relayd_sock) | |
2f77fc4b DG |
580 | { |
581 | int ret; | |
6151a90f | 582 | struct lttcomm_relayd_sock *rsock; |
2f77fc4b | 583 | |
6151a90f JD |
584 | rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR, |
585 | RELAYD_VERSION_COMM_MINOR); | |
586 | if (!rsock) { | |
f73fabfd | 587 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
588 | goto error; |
589 | } | |
590 | ||
ffe60014 DG |
591 | /* |
592 | * Connect to relayd so we can proceed with a session creation. This call | |
593 | * can possibly block for an arbitrary amount of time to set the health | |
594 | * state to be in poll execution. | |
595 | */ | |
596 | health_poll_entry(); | |
6151a90f | 597 | ret = relayd_connect(rsock); |
ffe60014 | 598 | health_poll_exit(); |
2f77fc4b DG |
599 | if (ret < 0) { |
600 | ERR("Unable to reach lttng-relayd"); | |
f73fabfd | 601 | ret = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
2f77fc4b DG |
602 | goto free_sock; |
603 | } | |
604 | ||
605 | /* Create socket for control stream. */ | |
606 | if (uri->stype == LTTNG_STREAM_CONTROL) { | |
607 | DBG3("Creating relayd stream socket from URI"); | |
608 | ||
609 | /* Check relayd version */ | |
6151a90f | 610 | ret = relayd_version_check(rsock); |
2f77fc4b | 611 | if (ret < 0) { |
f73fabfd | 612 | ret = LTTNG_ERR_RELAYD_VERSION_FAIL; |
2f77fc4b DG |
613 | goto close_sock; |
614 | } | |
615 | } else if (uri->stype == LTTNG_STREAM_DATA) { | |
616 | DBG3("Creating relayd data socket from URI"); | |
617 | } else { | |
618 | /* Command is not valid */ | |
619 | ERR("Relayd invalid stream type: %d", uri->stype); | |
f73fabfd | 620 | ret = LTTNG_ERR_INVALID; |
2f77fc4b DG |
621 | goto close_sock; |
622 | } | |
623 | ||
6151a90f | 624 | *relayd_sock = rsock; |
2f77fc4b | 625 | |
f73fabfd | 626 | return LTTNG_OK; |
2f77fc4b DG |
627 | |
628 | close_sock: | |
6151a90f JD |
629 | /* The returned value is not useful since we are on an error path. */ |
630 | (void) relayd_close(rsock); | |
2f77fc4b | 631 | free_sock: |
6151a90f | 632 | free(rsock); |
2f77fc4b DG |
633 | error: |
634 | return ret; | |
635 | } | |
636 | ||
637 | /* | |
638 | * Connect to the relayd using URI and send the socket to the right consumer. | |
639 | */ | |
56a37563 JG |
640 | static int send_consumer_relayd_socket(enum lttng_domain_type domain, |
641 | unsigned int session_id, struct lttng_uri *relayd_uri, | |
642 | struct consumer_output *consumer, | |
d3e2ba59 JD |
643 | struct consumer_socket *consumer_sock, |
644 | char *session_name, char *hostname, int session_live_timer) | |
2f77fc4b DG |
645 | { |
646 | int ret; | |
6151a90f | 647 | struct lttcomm_relayd_sock *rsock = NULL; |
2f77fc4b | 648 | |
ffe60014 | 649 | /* Connect to relayd and make version check if uri is the control. */ |
6151a90f | 650 | ret = create_connect_relayd(relayd_uri, &rsock); |
ffe60014 | 651 | if (ret != LTTNG_OK) { |
6151a90f | 652 | goto error; |
ffe60014 | 653 | } |
6151a90f | 654 | assert(rsock); |
ffe60014 | 655 | |
2f77fc4b | 656 | /* Set the network sequence index if not set. */ |
d88aee68 DG |
657 | if (consumer->net_seq_index == (uint64_t) -1ULL) { |
658 | pthread_mutex_lock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
659 | /* |
660 | * Increment net_seq_idx because we are about to transfer the | |
661 | * new relayd socket to the consumer. | |
d88aee68 | 662 | * Assign unique key so the consumer can match streams. |
2f77fc4b | 663 | */ |
d88aee68 DG |
664 | consumer->net_seq_index = ++relayd_net_seq_idx; |
665 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
666 | } |
667 | ||
2f77fc4b | 668 | /* Send relayd socket to consumer. */ |
6151a90f | 669 | ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer, |
d3e2ba59 JD |
670 | relayd_uri->stype, session_id, |
671 | session_name, hostname, session_live_timer); | |
2f77fc4b | 672 | if (ret < 0) { |
f73fabfd | 673 | ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL; |
2f77fc4b DG |
674 | goto close_sock; |
675 | } | |
676 | ||
c890b720 DG |
677 | /* Flag that the corresponding socket was sent. */ |
678 | if (relayd_uri->stype == LTTNG_STREAM_CONTROL) { | |
ffe60014 | 679 | consumer_sock->control_sock_sent = 1; |
c890b720 | 680 | } else if (relayd_uri->stype == LTTNG_STREAM_DATA) { |
ffe60014 | 681 | consumer_sock->data_sock_sent = 1; |
c890b720 DG |
682 | } |
683 | ||
f73fabfd | 684 | ret = LTTNG_OK; |
2f77fc4b DG |
685 | |
686 | /* | |
687 | * Close socket which was dup on the consumer side. The session daemon does | |
688 | * NOT keep track of the relayd socket(s) once transfer to the consumer. | |
689 | */ | |
690 | ||
691 | close_sock: | |
6151a90f JD |
692 | (void) relayd_close(rsock); |
693 | free(rsock); | |
2f77fc4b | 694 | |
6151a90f | 695 | error: |
ffe60014 DG |
696 | if (ret != LTTNG_OK) { |
697 | /* | |
d9078d0c DG |
698 | * The consumer output for this session should not be used anymore |
699 | * since the relayd connection failed thus making any tracing or/and | |
700 | * streaming not usable. | |
ffe60014 | 701 | */ |
d9078d0c | 702 | consumer->enabled = 0; |
ffe60014 | 703 | } |
2f77fc4b DG |
704 | return ret; |
705 | } | |
706 | ||
707 | /* | |
708 | * Send both relayd sockets to a specific consumer and domain. This is a | |
709 | * helper function to facilitate sending the information to the consumer for a | |
710 | * session. | |
711 | */ | |
56a37563 JG |
712 | static int send_consumer_relayd_sockets(enum lttng_domain_type domain, |
713 | unsigned int session_id, struct consumer_output *consumer, | |
714 | struct consumer_socket *sock, char *session_name, | |
715 | char *hostname, int session_live_timer) | |
2f77fc4b | 716 | { |
dda67f6c | 717 | int ret = LTTNG_OK; |
2f77fc4b | 718 | |
2f77fc4b | 719 | assert(consumer); |
6dc3064a | 720 | assert(sock); |
2f77fc4b | 721 | |
2f77fc4b | 722 | /* Sending control relayd socket. */ |
ffe60014 | 723 | if (!sock->control_sock_sent) { |
6dc3064a | 724 | ret = send_consumer_relayd_socket(domain, session_id, |
d3e2ba59 JD |
725 | &consumer->dst.net.control, consumer, sock, |
726 | session_name, hostname, session_live_timer); | |
c890b720 DG |
727 | if (ret != LTTNG_OK) { |
728 | goto error; | |
729 | } | |
2f77fc4b DG |
730 | } |
731 | ||
732 | /* Sending data relayd socket. */ | |
ffe60014 | 733 | if (!sock->data_sock_sent) { |
6dc3064a | 734 | ret = send_consumer_relayd_socket(domain, session_id, |
d3e2ba59 JD |
735 | &consumer->dst.net.data, consumer, sock, |
736 | session_name, hostname, session_live_timer); | |
c890b720 DG |
737 | if (ret != LTTNG_OK) { |
738 | goto error; | |
739 | } | |
2f77fc4b DG |
740 | } |
741 | ||
2f77fc4b DG |
742 | error: |
743 | return ret; | |
744 | } | |
745 | ||
746 | /* | |
747 | * Setup relayd connections for a tracing session. First creates the socket to | |
748 | * the relayd and send them to the right domain consumer. Consumer type MUST be | |
749 | * network. | |
750 | */ | |
ffe60014 | 751 | int cmd_setup_relayd(struct ltt_session *session) |
2f77fc4b | 752 | { |
f73fabfd | 753 | int ret = LTTNG_OK; |
2f77fc4b DG |
754 | struct ltt_ust_session *usess; |
755 | struct ltt_kernel_session *ksess; | |
756 | struct consumer_socket *socket; | |
757 | struct lttng_ht_iter iter; | |
758 | ||
759 | assert(session); | |
760 | ||
761 | usess = session->ust_session; | |
762 | ksess = session->kernel_session; | |
763 | ||
785d2d0d | 764 | DBG("Setting relayd for session %s", session->name); |
2f77fc4b | 765 | |
e7fe706f DG |
766 | rcu_read_lock(); |
767 | ||
2f77fc4b DG |
768 | if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET |
769 | && usess->consumer->enabled) { | |
770 | /* For each consumer socket, send relayd sockets */ | |
771 | cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter, | |
772 | socket, node.node) { | |
2f77fc4b | 773 | pthread_mutex_lock(socket->lock); |
6dc3064a | 774 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id, |
d3e2ba59 JD |
775 | usess->consumer, socket, |
776 | session->name, session->hostname, | |
777 | session->live_timer); | |
2f77fc4b | 778 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 779 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
780 | goto error; |
781 | } | |
6dc3064a DG |
782 | /* Session is now ready for network streaming. */ |
783 | session->net_handle = 1; | |
2f77fc4b DG |
784 | } |
785 | } | |
786 | ||
787 | if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET | |
788 | && ksess->consumer->enabled) { | |
789 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, | |
790 | socket, node.node) { | |
2f77fc4b | 791 | pthread_mutex_lock(socket->lock); |
6dc3064a | 792 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id, |
d3e2ba59 JD |
793 | ksess->consumer, socket, |
794 | session->name, session->hostname, | |
795 | session->live_timer); | |
2f77fc4b | 796 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 797 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
798 | goto error; |
799 | } | |
6dc3064a DG |
800 | /* Session is now ready for network streaming. */ |
801 | session->net_handle = 1; | |
2f77fc4b DG |
802 | } |
803 | } | |
804 | ||
805 | error: | |
e7fe706f | 806 | rcu_read_unlock(); |
2f77fc4b DG |
807 | return ret; |
808 | } | |
809 | ||
9b6c7ec5 DG |
810 | /* |
811 | * Start a kernel session by opening all necessary streams. | |
812 | */ | |
813 | static int start_kernel_session(struct ltt_kernel_session *ksess, int wpipe) | |
814 | { | |
815 | int ret; | |
816 | struct ltt_kernel_channel *kchan; | |
817 | ||
818 | /* Open kernel metadata */ | |
07b86b52 | 819 | if (ksess->metadata == NULL && ksess->output_traces) { |
9b6c7ec5 DG |
820 | ret = kernel_open_metadata(ksess); |
821 | if (ret < 0) { | |
822 | ret = LTTNG_ERR_KERN_META_FAIL; | |
823 | goto error; | |
824 | } | |
825 | } | |
826 | ||
827 | /* Open kernel metadata stream */ | |
07b86b52 | 828 | if (ksess->metadata && ksess->metadata_stream_fd < 0) { |
9b6c7ec5 DG |
829 | ret = kernel_open_metadata_stream(ksess); |
830 | if (ret < 0) { | |
831 | ERR("Kernel create metadata stream failed"); | |
832 | ret = LTTNG_ERR_KERN_STREAM_FAIL; | |
833 | goto error; | |
834 | } | |
835 | } | |
836 | ||
837 | /* For each channel */ | |
838 | cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) { | |
839 | if (kchan->stream_count == 0) { | |
840 | ret = kernel_open_channel_stream(kchan); | |
841 | if (ret < 0) { | |
842 | ret = LTTNG_ERR_KERN_STREAM_FAIL; | |
843 | goto error; | |
844 | } | |
845 | /* Update the stream global counter */ | |
846 | ksess->stream_count_global += ret; | |
847 | } | |
848 | } | |
849 | ||
850 | /* Setup kernel consumer socket and send fds to it */ | |
851 | ret = init_kernel_tracing(ksess); | |
e43c41c5 | 852 | if (ret != 0) { |
9b6c7ec5 DG |
853 | ret = LTTNG_ERR_KERN_START_FAIL; |
854 | goto error; | |
855 | } | |
856 | ||
857 | /* This start the kernel tracing */ | |
858 | ret = kernel_start_session(ksess); | |
859 | if (ret < 0) { | |
860 | ret = LTTNG_ERR_KERN_START_FAIL; | |
861 | goto error; | |
862 | } | |
863 | ||
864 | /* Quiescent wait after starting trace */ | |
784303b0 | 865 | kernel_wait_quiescent(kernel_tracer_fd); |
9b6c7ec5 | 866 | |
14fb1ebe | 867 | ksess->active = 1; |
9b6c7ec5 DG |
868 | |
869 | ret = LTTNG_OK; | |
870 | ||
871 | error: | |
872 | return ret; | |
873 | } | |
874 | ||
2f77fc4b DG |
875 | /* |
876 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. | |
877 | */ | |
56a37563 JG |
878 | int cmd_disable_channel(struct ltt_session *session, |
879 | enum lttng_domain_type domain, char *channel_name) | |
2f77fc4b DG |
880 | { |
881 | int ret; | |
882 | struct ltt_ust_session *usess; | |
883 | ||
884 | usess = session->ust_session; | |
885 | ||
2223c96f DG |
886 | rcu_read_lock(); |
887 | ||
2f77fc4b DG |
888 | switch (domain) { |
889 | case LTTNG_DOMAIN_KERNEL: | |
890 | { | |
891 | ret = channel_kernel_disable(session->kernel_session, | |
892 | channel_name); | |
f73fabfd | 893 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
894 | goto error; |
895 | } | |
896 | ||
897 | kernel_wait_quiescent(kernel_tracer_fd); | |
898 | break; | |
899 | } | |
900 | case LTTNG_DOMAIN_UST: | |
901 | { | |
902 | struct ltt_ust_channel *uchan; | |
903 | struct lttng_ht *chan_ht; | |
904 | ||
905 | chan_ht = usess->domain_global.channels; | |
906 | ||
907 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); | |
908 | if (uchan == NULL) { | |
f73fabfd | 909 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2f77fc4b DG |
910 | goto error; |
911 | } | |
912 | ||
7972aab2 | 913 | ret = channel_ust_disable(usess, uchan); |
f73fabfd | 914 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
915 | goto error; |
916 | } | |
917 | break; | |
918 | } | |
2f77fc4b | 919 | default: |
f73fabfd | 920 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
921 | goto error; |
922 | } | |
923 | ||
f73fabfd | 924 | ret = LTTNG_OK; |
2f77fc4b DG |
925 | |
926 | error: | |
2223c96f | 927 | rcu_read_unlock(); |
2f77fc4b DG |
928 | return ret; |
929 | } | |
930 | ||
ccf10263 MD |
931 | /* |
932 | * Command LTTNG_TRACK_PID processed by the client thread. | |
a9ad0c8f MD |
933 | * |
934 | * Called with session lock held. | |
ccf10263 | 935 | */ |
56a37563 JG |
936 | int cmd_track_pid(struct ltt_session *session, enum lttng_domain_type domain, |
937 | int pid) | |
ccf10263 MD |
938 | { |
939 | int ret; | |
940 | ||
941 | rcu_read_lock(); | |
942 | ||
943 | switch (domain) { | |
944 | case LTTNG_DOMAIN_KERNEL: | |
945 | { | |
946 | struct ltt_kernel_session *ksess; | |
947 | ||
948 | ksess = session->kernel_session; | |
949 | ||
950 | ret = kernel_track_pid(ksess, pid); | |
951 | if (ret != LTTNG_OK) { | |
952 | goto error; | |
953 | } | |
954 | ||
955 | kernel_wait_quiescent(kernel_tracer_fd); | |
956 | break; | |
957 | } | |
ccf10263 | 958 | case LTTNG_DOMAIN_UST: |
a9ad0c8f MD |
959 | { |
960 | struct ltt_ust_session *usess; | |
961 | ||
962 | usess = session->ust_session; | |
963 | ||
964 | ret = trace_ust_track_pid(usess, pid); | |
965 | if (ret != LTTNG_OK) { | |
966 | goto error; | |
967 | } | |
968 | break; | |
969 | } | |
ccf10263 MD |
970 | default: |
971 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
972 | goto error; | |
973 | } | |
974 | ||
975 | ret = LTTNG_OK; | |
976 | ||
977 | error: | |
978 | rcu_read_unlock(); | |
979 | return ret; | |
980 | } | |
981 | ||
982 | /* | |
983 | * Command LTTNG_UNTRACK_PID processed by the client thread. | |
a9ad0c8f MD |
984 | * |
985 | * Called with session lock held. | |
ccf10263 | 986 | */ |
56a37563 JG |
987 | int cmd_untrack_pid(struct ltt_session *session, enum lttng_domain_type domain, |
988 | int pid) | |
ccf10263 MD |
989 | { |
990 | int ret; | |
991 | ||
992 | rcu_read_lock(); | |
993 | ||
994 | switch (domain) { | |
995 | case LTTNG_DOMAIN_KERNEL: | |
996 | { | |
997 | struct ltt_kernel_session *ksess; | |
998 | ||
999 | ksess = session->kernel_session; | |
1000 | ||
1001 | ret = kernel_untrack_pid(ksess, pid); | |
1002 | if (ret != LTTNG_OK) { | |
1003 | goto error; | |
1004 | } | |
1005 | ||
1006 | kernel_wait_quiescent(kernel_tracer_fd); | |
1007 | break; | |
1008 | } | |
ccf10263 | 1009 | case LTTNG_DOMAIN_UST: |
a9ad0c8f MD |
1010 | { |
1011 | struct ltt_ust_session *usess; | |
1012 | ||
1013 | usess = session->ust_session; | |
1014 | ||
1015 | ret = trace_ust_untrack_pid(usess, pid); | |
1016 | if (ret != LTTNG_OK) { | |
1017 | goto error; | |
1018 | } | |
1019 | break; | |
1020 | } | |
ccf10263 MD |
1021 | default: |
1022 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
1023 | goto error; | |
1024 | } | |
1025 | ||
1026 | ret = LTTNG_OK; | |
1027 | ||
1028 | error: | |
1029 | rcu_read_unlock(); | |
1030 | return ret; | |
1031 | } | |
1032 | ||
2f77fc4b DG |
1033 | /* |
1034 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. | |
1035 | * | |
1036 | * The wpipe arguments is used as a notifier for the kernel thread. | |
1037 | */ | |
1038 | int cmd_enable_channel(struct ltt_session *session, | |
7972aab2 | 1039 | struct lttng_domain *domain, struct lttng_channel *attr, int wpipe) |
2f77fc4b DG |
1040 | { |
1041 | int ret; | |
1042 | struct ltt_ust_session *usess = session->ust_session; | |
1043 | struct lttng_ht *chan_ht; | |
1f345e94 | 1044 | size_t len; |
2f77fc4b DG |
1045 | |
1046 | assert(session); | |
1047 | assert(attr); | |
7972aab2 | 1048 | assert(domain); |
2f77fc4b | 1049 | |
f5436bfc | 1050 | len = lttng_strnlen(attr->name, sizeof(attr->name)); |
1f345e94 PP |
1051 | |
1052 | /* Validate channel name */ | |
1053 | if (attr->name[0] == '.' || | |
1054 | memchr(attr->name, '/', len) != NULL) { | |
1055 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1056 | goto end; | |
1057 | } | |
1058 | ||
2f77fc4b DG |
1059 | DBG("Enabling channel %s for session %s", attr->name, session->name); |
1060 | ||
03b4fdcf DG |
1061 | rcu_read_lock(); |
1062 | ||
ecc48a90 JD |
1063 | /* |
1064 | * Don't try to enable a channel if the session has been started at | |
1065 | * some point in time before. The tracer does not allow it. | |
1066 | */ | |
8382cf6f | 1067 | if (session->has_been_started) { |
ecc48a90 JD |
1068 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
1069 | goto error; | |
1070 | } | |
1071 | ||
1072 | /* | |
1073 | * If the session is a live session, remove the switch timer, the | |
1074 | * live timer does the same thing but sends also synchronisation | |
1075 | * beacons for inactive streams. | |
1076 | */ | |
1077 | if (session->live_timer > 0) { | |
1078 | attr->attr.live_timer_interval = session->live_timer; | |
1079 | attr->attr.switch_timer_interval = 0; | |
1080 | } | |
1081 | ||
33fbd469 DG |
1082 | /* |
1083 | * The ringbuffer (both in user space and kernel) behave badly in overwrite | |
1084 | * mode and with less than 2 subbuf so block it right away and send back an | |
1085 | * invalid attribute error. | |
1086 | */ | |
1087 | if (attr->attr.overwrite && attr->attr.num_subbuf < 2) { | |
1088 | ret = LTTNG_ERR_INVALID; | |
1089 | goto error; | |
1090 | } | |
1091 | ||
7972aab2 | 1092 | switch (domain->type) { |
2f77fc4b DG |
1093 | case LTTNG_DOMAIN_KERNEL: |
1094 | { | |
1095 | struct ltt_kernel_channel *kchan; | |
1096 | ||
2f77fc4b DG |
1097 | kchan = trace_kernel_get_channel_by_name(attr->name, |
1098 | session->kernel_session); | |
1099 | if (kchan == NULL) { | |
1100 | ret = channel_kernel_create(session->kernel_session, attr, wpipe); | |
85076754 MD |
1101 | if (attr->name[0] != '\0') { |
1102 | session->kernel_session->has_non_default_channel = 1; | |
1103 | } | |
2f77fc4b DG |
1104 | } else { |
1105 | ret = channel_kernel_enable(session->kernel_session, kchan); | |
1106 | } | |
1107 | ||
f73fabfd | 1108 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1109 | goto error; |
1110 | } | |
1111 | ||
784303b0 | 1112 | kernel_wait_quiescent(kernel_tracer_fd); |
2f77fc4b DG |
1113 | break; |
1114 | } | |
1115 | case LTTNG_DOMAIN_UST: | |
9232818f JG |
1116 | case LTTNG_DOMAIN_JUL: |
1117 | case LTTNG_DOMAIN_LOG4J: | |
1118 | case LTTNG_DOMAIN_PYTHON: | |
2f77fc4b DG |
1119 | { |
1120 | struct ltt_ust_channel *uchan; | |
1121 | ||
9232818f JG |
1122 | /* |
1123 | * FIXME | |
1124 | * | |
1125 | * Current agent implementation limitations force us to allow | |
1126 | * only one channel at once in "agent" subdomains. Each | |
1127 | * subdomain has a default channel name which must be strictly | |
1128 | * adhered to. | |
1129 | */ | |
1130 | if (domain->type == LTTNG_DOMAIN_JUL) { | |
1131 | if (strncmp(attr->name, DEFAULT_JUL_CHANNEL_NAME, | |
1132 | LTTNG_SYMBOL_NAME_LEN)) { | |
1133 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1134 | goto error; | |
1135 | } | |
1136 | } else if (domain->type == LTTNG_DOMAIN_LOG4J) { | |
1137 | if (strncmp(attr->name, DEFAULT_LOG4J_CHANNEL_NAME, | |
1138 | LTTNG_SYMBOL_NAME_LEN)) { | |
1139 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1140 | goto error; | |
1141 | } | |
1142 | } else if (domain->type == LTTNG_DOMAIN_PYTHON) { | |
1143 | if (strncmp(attr->name, DEFAULT_PYTHON_CHANNEL_NAME, | |
1144 | LTTNG_SYMBOL_NAME_LEN)) { | |
1145 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1146 | goto error; | |
1147 | } | |
1148 | } | |
1149 | ||
2f77fc4b DG |
1150 | chan_ht = usess->domain_global.channels; |
1151 | ||
1152 | uchan = trace_ust_find_channel_by_name(chan_ht, attr->name); | |
1153 | if (uchan == NULL) { | |
7972aab2 | 1154 | ret = channel_ust_create(usess, attr, domain->buf_type); |
85076754 MD |
1155 | if (attr->name[0] != '\0') { |
1156 | usess->has_non_default_channel = 1; | |
1157 | } | |
2f77fc4b | 1158 | } else { |
7972aab2 | 1159 | ret = channel_ust_enable(usess, uchan); |
2f77fc4b DG |
1160 | } |
1161 | break; | |
1162 | } | |
2f77fc4b | 1163 | default: |
f73fabfd | 1164 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
1165 | goto error; |
1166 | } | |
1167 | ||
1168 | error: | |
2223c96f | 1169 | rcu_read_unlock(); |
1f345e94 | 1170 | end: |
2f77fc4b DG |
1171 | return ret; |
1172 | } | |
1173 | ||
2f77fc4b DG |
1174 | /* |
1175 | * Command LTTNG_DISABLE_EVENT processed by the client thread. | |
1176 | */ | |
56a37563 JG |
1177 | int cmd_disable_event(struct ltt_session *session, |
1178 | enum lttng_domain_type domain, char *channel_name, | |
6e911cad | 1179 | struct lttng_event *event) |
2f77fc4b DG |
1180 | { |
1181 | int ret; | |
6e911cad MD |
1182 | char *event_name; |
1183 | ||
18a720cd MD |
1184 | DBG("Disable event command for event \'%s\'", event->name); |
1185 | ||
6e911cad | 1186 | event_name = event->name; |
7076b56e JG |
1187 | if (validate_event_name(event_name)) { |
1188 | ret = LTTNG_ERR_INVALID_EVENT_NAME; | |
1189 | goto error; | |
1190 | } | |
6e911cad | 1191 | |
9b7431cf JG |
1192 | /* Error out on unhandled search criteria */ |
1193 | if (event->loglevel_type || event->loglevel != -1 || event->enabled | |
6e911cad | 1194 | || event->pid || event->filter || event->exclusion) { |
7076b56e JG |
1195 | ret = LTTNG_ERR_UNK; |
1196 | goto error; | |
6e911cad | 1197 | } |
2f77fc4b | 1198 | |
2223c96f DG |
1199 | rcu_read_lock(); |
1200 | ||
2f77fc4b DG |
1201 | switch (domain) { |
1202 | case LTTNG_DOMAIN_KERNEL: | |
1203 | { | |
1204 | struct ltt_kernel_channel *kchan; | |
1205 | struct ltt_kernel_session *ksess; | |
1206 | ||
1207 | ksess = session->kernel_session; | |
1208 | ||
85076754 MD |
1209 | /* |
1210 | * If a non-default channel has been created in the | |
1211 | * session, explicitely require that -c chan_name needs | |
1212 | * to be provided. | |
1213 | */ | |
1214 | if (ksess->has_non_default_channel && channel_name[0] == '\0') { | |
1215 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
7076b56e | 1216 | goto error_unlock; |
85076754 MD |
1217 | } |
1218 | ||
2f77fc4b DG |
1219 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); |
1220 | if (kchan == NULL) { | |
f73fabfd | 1221 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
7076b56e | 1222 | goto error_unlock; |
2f77fc4b DG |
1223 | } |
1224 | ||
6e911cad MD |
1225 | switch (event->type) { |
1226 | case LTTNG_EVENT_ALL: | |
9550ee81 | 1227 | case LTTNG_EVENT_TRACEPOINT: |
d0ae4ea8 | 1228 | case LTTNG_EVENT_SYSCALL: |
9550ee81 JR |
1229 | case LTTNG_EVENT_PROBE: |
1230 | case LTTNG_EVENT_FUNCTION: | |
1231 | case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */ | |
1232 | if (event_name[0] == '\0') { | |
1233 | ret = event_kernel_disable_event(kchan, | |
1234 | NULL, event->type); | |
29c62722 | 1235 | } else { |
d0ae4ea8 | 1236 | ret = event_kernel_disable_event(kchan, |
9550ee81 | 1237 | event_name, event->type); |
29c62722 | 1238 | } |
6e911cad | 1239 | if (ret != LTTNG_OK) { |
7076b56e | 1240 | goto error_unlock; |
6e911cad MD |
1241 | } |
1242 | break; | |
6e911cad MD |
1243 | default: |
1244 | ret = LTTNG_ERR_UNK; | |
7076b56e | 1245 | goto error_unlock; |
2f77fc4b DG |
1246 | } |
1247 | ||
1248 | kernel_wait_quiescent(kernel_tracer_fd); | |
1249 | break; | |
1250 | } | |
1251 | case LTTNG_DOMAIN_UST: | |
1252 | { | |
1253 | struct ltt_ust_channel *uchan; | |
1254 | struct ltt_ust_session *usess; | |
1255 | ||
1256 | usess = session->ust_session; | |
1257 | ||
7076b56e JG |
1258 | if (validate_ust_event_name(event_name)) { |
1259 | ret = LTTNG_ERR_INVALID_EVENT_NAME; | |
1260 | goto error_unlock; | |
1261 | } | |
1262 | ||
85076754 MD |
1263 | /* |
1264 | * If a non-default channel has been created in the | |
9550ee81 | 1265 | * session, explicitly require that -c chan_name needs |
85076754 MD |
1266 | * to be provided. |
1267 | */ | |
1268 | if (usess->has_non_default_channel && channel_name[0] == '\0') { | |
1269 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
7076b56e | 1270 | goto error_unlock; |
85076754 MD |
1271 | } |
1272 | ||
2f77fc4b DG |
1273 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
1274 | channel_name); | |
1275 | if (uchan == NULL) { | |
f73fabfd | 1276 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
7076b56e | 1277 | goto error_unlock; |
2f77fc4b DG |
1278 | } |
1279 | ||
6e911cad MD |
1280 | switch (event->type) { |
1281 | case LTTNG_EVENT_ALL: | |
b3639870 JR |
1282 | /* |
1283 | * An empty event name means that everything | |
1284 | * should be disabled. | |
1285 | */ | |
1286 | if (event->name[0] == '\0') { | |
1287 | ret = event_ust_disable_all_tracepoints(usess, uchan); | |
77d536b2 JR |
1288 | } else { |
1289 | ret = event_ust_disable_tracepoint(usess, uchan, | |
1290 | event_name); | |
1291 | } | |
6e911cad | 1292 | if (ret != LTTNG_OK) { |
7076b56e | 1293 | goto error_unlock; |
6e911cad MD |
1294 | } |
1295 | break; | |
1296 | default: | |
1297 | ret = LTTNG_ERR_UNK; | |
7076b56e | 1298 | goto error_unlock; |
2f77fc4b DG |
1299 | } |
1300 | ||
1301 | DBG3("Disable UST event %s in channel %s completed", event_name, | |
1302 | channel_name); | |
1303 | break; | |
1304 | } | |
5cdb6027 | 1305 | case LTTNG_DOMAIN_LOG4J: |
f20baf8e | 1306 | case LTTNG_DOMAIN_JUL: |
0e115563 | 1307 | case LTTNG_DOMAIN_PYTHON: |
f20baf8e | 1308 | { |
fefd409b | 1309 | struct agent *agt; |
f20baf8e DG |
1310 | struct ltt_ust_session *usess = session->ust_session; |
1311 | ||
1312 | assert(usess); | |
1313 | ||
6e911cad MD |
1314 | switch (event->type) { |
1315 | case LTTNG_EVENT_ALL: | |
1316 | break; | |
1317 | default: | |
1318 | ret = LTTNG_ERR_UNK; | |
7076b56e | 1319 | goto error_unlock; |
6e911cad MD |
1320 | } |
1321 | ||
5cdb6027 | 1322 | agt = trace_ust_find_agent(usess, domain); |
fefd409b DG |
1323 | if (!agt) { |
1324 | ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND; | |
7076b56e | 1325 | goto error_unlock; |
fefd409b | 1326 | } |
b3639870 JR |
1327 | /* |
1328 | * An empty event name means that everything | |
1329 | * should be disabled. | |
1330 | */ | |
1331 | if (event->name[0] == '\0') { | |
18a720cd MD |
1332 | ret = event_agent_disable_all(usess, agt); |
1333 | } else { | |
1334 | ret = event_agent_disable(usess, agt, event_name); | |
1335 | } | |
f20baf8e | 1336 | if (ret != LTTNG_OK) { |
7076b56e | 1337 | goto error_unlock; |
f20baf8e DG |
1338 | } |
1339 | ||
1340 | break; | |
1341 | } | |
2f77fc4b | 1342 | default: |
f73fabfd | 1343 | ret = LTTNG_ERR_UND; |
7076b56e | 1344 | goto error_unlock; |
2f77fc4b DG |
1345 | } |
1346 | ||
f73fabfd | 1347 | ret = LTTNG_OK; |
2f77fc4b | 1348 | |
7076b56e | 1349 | error_unlock: |
2223c96f | 1350 | rcu_read_unlock(); |
7076b56e | 1351 | error: |
2f77fc4b DG |
1352 | return ret; |
1353 | } | |
1354 | ||
2f77fc4b DG |
1355 | /* |
1356 | * Command LTTNG_ADD_CONTEXT processed by the client thread. | |
1357 | */ | |
56a37563 | 1358 | int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain, |
601d5acf | 1359 | char *channel_name, struct lttng_event_context *ctx, int kwpipe) |
2f77fc4b | 1360 | { |
d5979e4a | 1361 | int ret, chan_kern_created = 0, chan_ust_created = 0; |
2f77fc4b DG |
1362 | |
1363 | switch (domain) { | |
1364 | case LTTNG_DOMAIN_KERNEL: | |
979e618e DG |
1365 | assert(session->kernel_session); |
1366 | ||
1367 | if (session->kernel_session->channel_count == 0) { | |
1368 | /* Create default channel */ | |
1369 | ret = channel_kernel_create(session->kernel_session, NULL, kwpipe); | |
1370 | if (ret != LTTNG_OK) { | |
1371 | goto error; | |
1372 | } | |
d5979e4a | 1373 | chan_kern_created = 1; |
979e618e | 1374 | } |
2f77fc4b | 1375 | /* Add kernel context to kernel tracer */ |
601d5acf | 1376 | ret = context_kernel_add(session->kernel_session, ctx, channel_name); |
f73fabfd | 1377 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1378 | goto error; |
1379 | } | |
1380 | break; | |
1381 | case LTTNG_DOMAIN_UST: | |
1382 | { | |
1383 | struct ltt_ust_session *usess = session->ust_session; | |
85076754 MD |
1384 | unsigned int chan_count; |
1385 | ||
2f77fc4b DG |
1386 | assert(usess); |
1387 | ||
85076754 | 1388 | chan_count = lttng_ht_get_count(usess->domain_global.channels); |
979e618e DG |
1389 | if (chan_count == 0) { |
1390 | struct lttng_channel *attr; | |
1391 | /* Create default channel */ | |
0a9c6494 | 1392 | attr = channel_new_default_attr(domain, usess->buffer_type); |
979e618e DG |
1393 | if (attr == NULL) { |
1394 | ret = LTTNG_ERR_FATAL; | |
1395 | goto error; | |
1396 | } | |
1397 | ||
7972aab2 | 1398 | ret = channel_ust_create(usess, attr, usess->buffer_type); |
979e618e DG |
1399 | if (ret != LTTNG_OK) { |
1400 | free(attr); | |
1401 | goto error; | |
1402 | } | |
1403 | free(attr); | |
d5979e4a | 1404 | chan_ust_created = 1; |
979e618e DG |
1405 | } |
1406 | ||
601d5acf | 1407 | ret = context_ust_add(usess, domain, ctx, channel_name); |
f73fabfd | 1408 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1409 | goto error; |
1410 | } | |
1411 | break; | |
1412 | } | |
2f77fc4b | 1413 | default: |
f73fabfd | 1414 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1415 | goto error; |
1416 | } | |
1417 | ||
d5979e4a | 1418 | return LTTNG_OK; |
2f77fc4b DG |
1419 | |
1420 | error: | |
d5979e4a DG |
1421 | if (chan_kern_created) { |
1422 | struct ltt_kernel_channel *kchan = | |
1423 | trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME, | |
1424 | session->kernel_session); | |
1425 | /* Created previously, this should NOT fail. */ | |
1426 | assert(kchan); | |
1427 | kernel_destroy_channel(kchan); | |
1428 | } | |
1429 | ||
1430 | if (chan_ust_created) { | |
1431 | struct ltt_ust_channel *uchan = | |
1432 | trace_ust_find_channel_by_name( | |
1433 | session->ust_session->domain_global.channels, | |
1434 | DEFAULT_CHANNEL_NAME); | |
1435 | /* Created previously, this should NOT fail. */ | |
1436 | assert(uchan); | |
1437 | /* Remove from the channel list of the session. */ | |
1438 | trace_ust_delete_channel(session->ust_session->domain_global.channels, | |
1439 | uchan); | |
1440 | trace_ust_destroy_channel(uchan); | |
1441 | } | |
2f77fc4b DG |
1442 | return ret; |
1443 | } | |
1444 | ||
930a2e99 JG |
1445 | static int validate_event_name(const char *name) |
1446 | { | |
1447 | int ret = 0; | |
1448 | const char *c = name; | |
1449 | const char *event_name_end = c + LTTNG_SYMBOL_NAME_LEN; | |
bf9807db | 1450 | bool null_terminated = false; |
930a2e99 JG |
1451 | |
1452 | /* | |
1453 | * Make sure that unescaped wildcards are only used as the last | |
1454 | * character of the event name. | |
1455 | */ | |
1456 | while (c < event_name_end) { | |
1457 | switch (*c) { | |
1458 | case '\0': | |
bf9807db | 1459 | null_terminated = true; |
930a2e99 JG |
1460 | goto end; |
1461 | case '\\': | |
1462 | c++; | |
1463 | break; | |
1464 | case '*': | |
1465 | if ((c + 1) < event_name_end && *(c + 1)) { | |
1466 | /* Wildcard is not the last character */ | |
1467 | ret = LTTNG_ERR_INVALID_EVENT_NAME; | |
1468 | goto end; | |
1469 | } | |
1470 | default: | |
1471 | break; | |
1472 | } | |
1473 | c++; | |
1474 | } | |
1475 | end: | |
bf9807db JG |
1476 | if (!ret && !null_terminated) { |
1477 | ret = LTTNG_ERR_INVALID_EVENT_NAME; | |
1478 | } | |
930a2e99 JG |
1479 | return ret; |
1480 | } | |
1481 | ||
dac8e046 JG |
1482 | static inline bool name_starts_with(const char *name, const char *prefix) |
1483 | { | |
1484 | const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN); | |
1485 | ||
1486 | return !strncmp(name, prefix, max_cmp_len); | |
1487 | } | |
1488 | ||
1489 | /* Perform userspace-specific event name validation */ | |
1490 | static int validate_ust_event_name(const char *name) | |
1491 | { | |
1492 | int ret = 0; | |
1493 | ||
1494 | if (!name) { | |
1495 | ret = -1; | |
1496 | goto end; | |
1497 | } | |
1498 | ||
1499 | /* | |
1500 | * Check name against all internal UST event component namespaces used | |
1501 | * by the agents. | |
1502 | */ | |
1503 | if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) || | |
1504 | name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) || | |
1505 | name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) { | |
1506 | ret = -1; | |
1507 | } | |
1508 | ||
1509 | end: | |
1510 | return ret; | |
1511 | } | |
88f06f15 | 1512 | |
2f77fc4b | 1513 | /* |
88f06f15 JG |
1514 | * Internal version of cmd_enable_event() with a supplemental |
1515 | * "internal_event" flag which is used to enable internal events which should | |
1516 | * be hidden from clients. Such events are used in the agent implementation to | |
1517 | * enable the events through which all "agent" events are funeled. | |
2f77fc4b | 1518 | */ |
88f06f15 JG |
1519 | static int _cmd_enable_event(struct ltt_session *session, |
1520 | struct lttng_domain *domain, | |
025faf73 | 1521 | char *channel_name, struct lttng_event *event, |
6b453b5e | 1522 | char *filter_expression, |
db8f1377 JI |
1523 | struct lttng_filter_bytecode *filter, |
1524 | struct lttng_event_exclusion *exclusion, | |
88f06f15 | 1525 | int wpipe, bool internal_event) |
2f77fc4b | 1526 | { |
e5f5db7f | 1527 | int ret, channel_created = 0; |
2f77fc4b DG |
1528 | struct lttng_channel *attr; |
1529 | ||
1530 | assert(session); | |
1531 | assert(event); | |
1532 | assert(channel_name); | |
1533 | ||
2a385866 JG |
1534 | /* If we have a filter, we must have its filter expression */ |
1535 | assert(!(!!filter_expression ^ !!filter)); | |
1536 | ||
18a720cd MD |
1537 | DBG("Enable event command for event \'%s\'", event->name); |
1538 | ||
f5ac4bd7 MD |
1539 | rcu_read_lock(); |
1540 | ||
930a2e99 JG |
1541 | ret = validate_event_name(event->name); |
1542 | if (ret) { | |
1543 | goto error; | |
1544 | } | |
1545 | ||
7972aab2 | 1546 | switch (domain->type) { |
2f77fc4b DG |
1547 | case LTTNG_DOMAIN_KERNEL: |
1548 | { | |
1549 | struct ltt_kernel_channel *kchan; | |
1550 | ||
85076754 MD |
1551 | /* |
1552 | * If a non-default channel has been created in the | |
1553 | * session, explicitely require that -c chan_name needs | |
1554 | * to be provided. | |
1555 | */ | |
1556 | if (session->kernel_session->has_non_default_channel | |
1557 | && channel_name[0] == '\0') { | |
1558 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
1559 | goto error; | |
1560 | } | |
1561 | ||
2f77fc4b DG |
1562 | kchan = trace_kernel_get_channel_by_name(channel_name, |
1563 | session->kernel_session); | |
1564 | if (kchan == NULL) { | |
0a9c6494 DG |
1565 | attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, |
1566 | LTTNG_BUFFER_GLOBAL); | |
2f77fc4b | 1567 | if (attr == NULL) { |
f73fabfd | 1568 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1569 | goto error; |
1570 | } | |
1571 | strncpy(attr->name, channel_name, sizeof(attr->name)); | |
1572 | ||
1573 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 1574 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1575 | free(attr); |
1576 | goto error; | |
1577 | } | |
1578 | free(attr); | |
e5f5db7f DG |
1579 | |
1580 | channel_created = 1; | |
2f77fc4b DG |
1581 | } |
1582 | ||
1583 | /* Get the newly created kernel channel pointer */ | |
1584 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
1585 | session->kernel_session); | |
1586 | if (kchan == NULL) { | |
1587 | /* This sould not happen... */ | |
f73fabfd | 1588 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1589 | goto error; |
1590 | } | |
1591 | ||
6e911cad MD |
1592 | switch (event->type) { |
1593 | case LTTNG_EVENT_ALL: | |
29c62722 | 1594 | { |
00a62084 MD |
1595 | char *filter_expression_a = NULL; |
1596 | struct lttng_filter_bytecode *filter_a = NULL; | |
1597 | ||
1598 | /* | |
1599 | * We need to duplicate filter_expression and filter, | |
1600 | * because ownership is passed to first enable | |
1601 | * event. | |
1602 | */ | |
1603 | if (filter_expression) { | |
1604 | filter_expression_a = strdup(filter_expression); | |
1605 | if (!filter_expression_a) { | |
1606 | ret = LTTNG_ERR_FATAL; | |
1607 | goto error; | |
1608 | } | |
1609 | } | |
1610 | if (filter) { | |
1611 | filter_a = zmalloc(sizeof(*filter_a) + filter->len); | |
1612 | if (!filter_a) { | |
1613 | free(filter_expression_a); | |
1614 | ret = LTTNG_ERR_FATAL; | |
1615 | goto error; | |
1616 | } | |
1617 | memcpy(filter_a, filter, sizeof(*filter_a) + filter->len); | |
1618 | } | |
29c62722 | 1619 | event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */ |
00a62084 MD |
1620 | ret = event_kernel_enable_event(kchan, event, |
1621 | filter_expression, filter); | |
a969e101 MD |
1622 | /* We have passed ownership */ |
1623 | filter_expression = NULL; | |
1624 | filter = NULL; | |
29c62722 MD |
1625 | if (ret != LTTNG_OK) { |
1626 | if (channel_created) { | |
1627 | /* Let's not leak a useless channel. */ | |
1628 | kernel_destroy_channel(kchan); | |
1629 | } | |
00a62084 MD |
1630 | free(filter_expression_a); |
1631 | free(filter_a); | |
29c62722 MD |
1632 | goto error; |
1633 | } | |
1634 | event->type = LTTNG_EVENT_SYSCALL; /* Hack */ | |
00a62084 MD |
1635 | ret = event_kernel_enable_event(kchan, event, |
1636 | filter_expression_a, filter_a); | |
60d21fa2 AB |
1637 | /* We have passed ownership */ |
1638 | filter_expression_a = NULL; | |
1639 | filter_a = NULL; | |
29c62722 MD |
1640 | if (ret != LTTNG_OK) { |
1641 | goto error; | |
1642 | } | |
1643 | break; | |
1644 | } | |
6e6ef3d7 DG |
1645 | case LTTNG_EVENT_PROBE: |
1646 | case LTTNG_EVENT_FUNCTION: | |
1647 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
6e911cad | 1648 | case LTTNG_EVENT_TRACEPOINT: |
00a62084 MD |
1649 | ret = event_kernel_enable_event(kchan, event, |
1650 | filter_expression, filter); | |
a969e101 MD |
1651 | /* We have passed ownership */ |
1652 | filter_expression = NULL; | |
1653 | filter = NULL; | |
6e911cad MD |
1654 | if (ret != LTTNG_OK) { |
1655 | if (channel_created) { | |
1656 | /* Let's not leak a useless channel. */ | |
1657 | kernel_destroy_channel(kchan); | |
1658 | } | |
1659 | goto error; | |
e5f5db7f | 1660 | } |
6e911cad MD |
1661 | break; |
1662 | case LTTNG_EVENT_SYSCALL: | |
00a62084 MD |
1663 | ret = event_kernel_enable_event(kchan, event, |
1664 | filter_expression, filter); | |
a969e101 MD |
1665 | /* We have passed ownership */ |
1666 | filter_expression = NULL; | |
1667 | filter = NULL; | |
e2b957af MD |
1668 | if (ret != LTTNG_OK) { |
1669 | goto error; | |
1670 | } | |
6e911cad MD |
1671 | break; |
1672 | default: | |
1673 | ret = LTTNG_ERR_UNK; | |
2f77fc4b DG |
1674 | goto error; |
1675 | } | |
1676 | ||
1677 | kernel_wait_quiescent(kernel_tracer_fd); | |
1678 | break; | |
1679 | } | |
1680 | case LTTNG_DOMAIN_UST: | |
1681 | { | |
1682 | struct ltt_ust_channel *uchan; | |
1683 | struct ltt_ust_session *usess = session->ust_session; | |
1684 | ||
1685 | assert(usess); | |
1686 | ||
85076754 MD |
1687 | /* |
1688 | * If a non-default channel has been created in the | |
1689 | * session, explicitely require that -c chan_name needs | |
1690 | * to be provided. | |
1691 | */ | |
1692 | if (usess->has_non_default_channel && channel_name[0] == '\0') { | |
1693 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
1694 | goto error; | |
1695 | } | |
1696 | ||
2f77fc4b DG |
1697 | /* Get channel from global UST domain */ |
1698 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
1699 | channel_name); | |
1700 | if (uchan == NULL) { | |
1701 | /* Create default channel */ | |
0a9c6494 DG |
1702 | attr = channel_new_default_attr(LTTNG_DOMAIN_UST, |
1703 | usess->buffer_type); | |
2f77fc4b | 1704 | if (attr == NULL) { |
f73fabfd | 1705 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1706 | goto error; |
1707 | } | |
1708 | strncpy(attr->name, channel_name, sizeof(attr->name)); | |
1709 | ||
1710 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 1711 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1712 | free(attr); |
1713 | goto error; | |
1714 | } | |
1715 | free(attr); | |
1716 | ||
1717 | /* Get the newly created channel reference back */ | |
1718 | uchan = trace_ust_find_channel_by_name( | |
1719 | usess->domain_global.channels, channel_name); | |
1720 | assert(uchan); | |
1721 | } | |
1722 | ||
141feb8c JG |
1723 | if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) { |
1724 | /* | |
1725 | * Don't allow users to add UST events to channels which | |
1726 | * are assigned to a userspace subdomain (JUL, Log4J, | |
1727 | * Python, etc.). | |
1728 | */ | |
1729 | ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN; | |
1730 | goto error; | |
1731 | } | |
1732 | ||
dac8e046 JG |
1733 | if (!internal_event) { |
1734 | /* | |
1735 | * Ensure the event name is not reserved for internal | |
1736 | * use. | |
1737 | */ | |
1738 | ret = validate_ust_event_name(event->name); | |
1739 | if (ret) { | |
1740 | WARN("Userspace event name %s failed validation.", | |
1741 | event->name ? | |
1742 | event->name : "NULL"); | |
1743 | ret = LTTNG_ERR_INVALID_EVENT_NAME; | |
1744 | goto error; | |
1745 | } | |
1746 | } | |
1747 | ||
2f77fc4b | 1748 | /* At this point, the session and channel exist on the tracer */ |
6b453b5e | 1749 | ret = event_ust_enable_tracepoint(usess, uchan, event, |
88f06f15 JG |
1750 | filter_expression, filter, exclusion, |
1751 | internal_event); | |
49d21f93 MD |
1752 | /* We have passed ownership */ |
1753 | filter_expression = NULL; | |
1754 | filter = NULL; | |
1755 | exclusion = NULL; | |
f73fabfd | 1756 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1757 | goto error; |
1758 | } | |
1759 | break; | |
1760 | } | |
5cdb6027 | 1761 | case LTTNG_DOMAIN_LOG4J: |
f20baf8e | 1762 | case LTTNG_DOMAIN_JUL: |
0e115563 | 1763 | case LTTNG_DOMAIN_PYTHON: |
f20baf8e | 1764 | { |
da6c3a50 | 1765 | const char *default_event_name, *default_chan_name; |
fefd409b | 1766 | struct agent *agt; |
f20baf8e DG |
1767 | struct lttng_event uevent; |
1768 | struct lttng_domain tmp_dom; | |
1769 | struct ltt_ust_session *usess = session->ust_session; | |
1770 | ||
1771 | assert(usess); | |
1772 | ||
5cdb6027 | 1773 | agt = trace_ust_find_agent(usess, domain->type); |
fefd409b | 1774 | if (!agt) { |
5cdb6027 | 1775 | agt = agent_create(domain->type); |
fefd409b | 1776 | if (!agt) { |
e5b3c48c | 1777 | ret = LTTNG_ERR_NOMEM; |
fefd409b DG |
1778 | goto error; |
1779 | } | |
1780 | agent_add(agt, usess->agents); | |
1781 | } | |
1782 | ||
022d91ba | 1783 | /* Create the default tracepoint. */ |
996de3c7 | 1784 | memset(&uevent, 0, sizeof(uevent)); |
f20baf8e DG |
1785 | uevent.type = LTTNG_EVENT_TRACEPOINT; |
1786 | uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
51755dc8 JG |
1787 | default_event_name = event_get_default_agent_ust_name( |
1788 | domain->type); | |
da6c3a50 | 1789 | if (!default_event_name) { |
e5b3c48c | 1790 | ret = LTTNG_ERR_FATAL; |
da6c3a50 | 1791 | goto error; |
f43f95a9 | 1792 | } |
da6c3a50 | 1793 | strncpy(uevent.name, default_event_name, sizeof(uevent.name)); |
f20baf8e DG |
1794 | uevent.name[sizeof(uevent.name) - 1] = '\0'; |
1795 | ||
1796 | /* | |
1797 | * The domain type is changed because we are about to enable the | |
1798 | * default channel and event for the JUL domain that are hardcoded. | |
1799 | * This happens in the UST domain. | |
1800 | */ | |
1801 | memcpy(&tmp_dom, domain, sizeof(tmp_dom)); | |
1802 | tmp_dom.type = LTTNG_DOMAIN_UST; | |
1803 | ||
0e115563 DG |
1804 | switch (domain->type) { |
1805 | case LTTNG_DOMAIN_LOG4J: | |
da6c3a50 | 1806 | default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME; |
0e115563 DG |
1807 | break; |
1808 | case LTTNG_DOMAIN_JUL: | |
da6c3a50 | 1809 | default_chan_name = DEFAULT_JUL_CHANNEL_NAME; |
0e115563 DG |
1810 | break; |
1811 | case LTTNG_DOMAIN_PYTHON: | |
1812 | default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME; | |
1813 | break; | |
1814 | default: | |
e98a44b0 | 1815 | /* The switch/case we are in makes this impossible */ |
0e115563 | 1816 | assert(0); |
da6c3a50 DG |
1817 | } |
1818 | ||
971da06a | 1819 | { |
8404118c | 1820 | char *filter_expression_copy = NULL; |
51755dc8 | 1821 | struct lttng_filter_bytecode *filter_copy = NULL; |
971da06a JG |
1822 | |
1823 | if (filter) { | |
51755dc8 JG |
1824 | const size_t filter_size = sizeof( |
1825 | struct lttng_filter_bytecode) | |
1826 | + filter->len; | |
1827 | ||
1828 | filter_copy = zmalloc(filter_size); | |
971da06a | 1829 | if (!filter_copy) { |
018096a4 | 1830 | ret = LTTNG_ERR_NOMEM; |
b742e3e2 | 1831 | goto error; |
971da06a | 1832 | } |
51755dc8 | 1833 | memcpy(filter_copy, filter, filter_size); |
971da06a | 1834 | |
8404118c JG |
1835 | filter_expression_copy = |
1836 | strdup(filter_expression); | |
1837 | if (!filter_expression) { | |
1838 | ret = LTTNG_ERR_NOMEM; | |
51755dc8 JG |
1839 | } |
1840 | ||
1841 | if (!filter_expression_copy || !filter_copy) { | |
1842 | free(filter_expression_copy); | |
1843 | free(filter_copy); | |
1844 | goto error; | |
8404118c | 1845 | } |
971da06a JG |
1846 | } |
1847 | ||
88f06f15 | 1848 | ret = cmd_enable_event_internal(session, &tmp_dom, |
971da06a | 1849 | (char *) default_chan_name, |
8404118c JG |
1850 | &uevent, filter_expression_copy, |
1851 | filter_copy, NULL, wpipe); | |
971da06a JG |
1852 | } |
1853 | ||
f20baf8e DG |
1854 | if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_ENABLED) { |
1855 | goto error; | |
1856 | } | |
1857 | ||
1858 | /* The wild card * means that everything should be enabled. */ | |
1859 | if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) { | |
8404118c JG |
1860 | ret = event_agent_enable_all(usess, agt, event, filter, |
1861 | filter_expression); | |
f20baf8e | 1862 | } else { |
8404118c JG |
1863 | ret = event_agent_enable(usess, agt, event, filter, |
1864 | filter_expression); | |
f20baf8e | 1865 | } |
51755dc8 JG |
1866 | filter = NULL; |
1867 | filter_expression = NULL; | |
f20baf8e DG |
1868 | if (ret != LTTNG_OK) { |
1869 | goto error; | |
1870 | } | |
1871 | ||
1872 | break; | |
1873 | } | |
2f77fc4b | 1874 | default: |
f73fabfd | 1875 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1876 | goto error; |
1877 | } | |
1878 | ||
f73fabfd | 1879 | ret = LTTNG_OK; |
2f77fc4b DG |
1880 | |
1881 | error: | |
49d21f93 MD |
1882 | free(filter_expression); |
1883 | free(filter); | |
1884 | free(exclusion); | |
2223c96f | 1885 | rcu_read_unlock(); |
2f77fc4b DG |
1886 | return ret; |
1887 | } | |
1888 | ||
88f06f15 JG |
1889 | /* |
1890 | * Command LTTNG_ENABLE_EVENT processed by the client thread. | |
1891 | * We own filter, exclusion, and filter_expression. | |
1892 | */ | |
1893 | int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain, | |
1894 | char *channel_name, struct lttng_event *event, | |
1895 | char *filter_expression, | |
1896 | struct lttng_filter_bytecode *filter, | |
1897 | struct lttng_event_exclusion *exclusion, | |
1898 | int wpipe) | |
1899 | { | |
1900 | return _cmd_enable_event(session, domain, channel_name, event, | |
1901 | filter_expression, filter, exclusion, wpipe, false); | |
1902 | } | |
1903 | ||
1904 | /* | |
1905 | * Enable an event which is internal to LTTng. An internal should | |
1906 | * never be made visible to clients and are immune to checks such as | |
1907 | * reserved names. | |
1908 | */ | |
1909 | static int cmd_enable_event_internal(struct ltt_session *session, | |
1910 | struct lttng_domain *domain, | |
1911 | char *channel_name, struct lttng_event *event, | |
1912 | char *filter_expression, | |
1913 | struct lttng_filter_bytecode *filter, | |
1914 | struct lttng_event_exclusion *exclusion, | |
1915 | int wpipe) | |
1916 | { | |
1917 | return _cmd_enable_event(session, domain, channel_name, event, | |
1918 | filter_expression, filter, exclusion, wpipe, true); | |
1919 | } | |
1920 | ||
2f77fc4b DG |
1921 | /* |
1922 | * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. | |
1923 | */ | |
56a37563 JG |
1924 | ssize_t cmd_list_tracepoints(enum lttng_domain_type domain, |
1925 | struct lttng_event **events) | |
2f77fc4b DG |
1926 | { |
1927 | int ret; | |
1928 | ssize_t nb_events = 0; | |
1929 | ||
1930 | switch (domain) { | |
1931 | case LTTNG_DOMAIN_KERNEL: | |
1932 | nb_events = kernel_list_events(kernel_tracer_fd, events); | |
1933 | if (nb_events < 0) { | |
f73fabfd | 1934 | ret = LTTNG_ERR_KERN_LIST_FAIL; |
2f77fc4b DG |
1935 | goto error; |
1936 | } | |
1937 | break; | |
1938 | case LTTNG_DOMAIN_UST: | |
1939 | nb_events = ust_app_list_events(events); | |
1940 | if (nb_events < 0) { | |
f73fabfd | 1941 | ret = LTTNG_ERR_UST_LIST_FAIL; |
2f77fc4b DG |
1942 | goto error; |
1943 | } | |
1944 | break; | |
5cdb6027 | 1945 | case LTTNG_DOMAIN_LOG4J: |
3c6a091f | 1946 | case LTTNG_DOMAIN_JUL: |
0e115563 | 1947 | case LTTNG_DOMAIN_PYTHON: |
f60140a1 | 1948 | nb_events = agent_list_events(events, domain); |
3c6a091f DG |
1949 | if (nb_events < 0) { |
1950 | ret = LTTNG_ERR_UST_LIST_FAIL; | |
1951 | goto error; | |
1952 | } | |
1953 | break; | |
2f77fc4b | 1954 | default: |
f73fabfd | 1955 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1956 | goto error; |
1957 | } | |
1958 | ||
1959 | return nb_events; | |
1960 | ||
1961 | error: | |
1962 | /* Return negative value to differentiate return code */ | |
1963 | return -ret; | |
1964 | } | |
1965 | ||
1966 | /* | |
1967 | * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread. | |
1968 | */ | |
56a37563 | 1969 | ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain, |
2f77fc4b DG |
1970 | struct lttng_event_field **fields) |
1971 | { | |
1972 | int ret; | |
1973 | ssize_t nb_fields = 0; | |
1974 | ||
1975 | switch (domain) { | |
1976 | case LTTNG_DOMAIN_UST: | |
1977 | nb_fields = ust_app_list_event_fields(fields); | |
1978 | if (nb_fields < 0) { | |
f73fabfd | 1979 | ret = LTTNG_ERR_UST_LIST_FAIL; |
2f77fc4b DG |
1980 | goto error; |
1981 | } | |
1982 | break; | |
1983 | case LTTNG_DOMAIN_KERNEL: | |
1984 | default: /* fall-through */ | |
f73fabfd | 1985 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1986 | goto error; |
1987 | } | |
1988 | ||
1989 | return nb_fields; | |
1990 | ||
1991 | error: | |
1992 | /* Return negative value to differentiate return code */ | |
1993 | return -ret; | |
1994 | } | |
1995 | ||
834978fd DG |
1996 | ssize_t cmd_list_syscalls(struct lttng_event **events) |
1997 | { | |
1998 | return syscall_table_list(events); | |
1999 | } | |
2000 | ||
a5dfbb9d MD |
2001 | /* |
2002 | * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread. | |
2003 | * | |
2004 | * Called with session lock held. | |
2005 | */ | |
2006 | ssize_t cmd_list_tracker_pids(struct ltt_session *session, | |
56a37563 | 2007 | enum lttng_domain_type domain, int32_t **pids) |
a5dfbb9d MD |
2008 | { |
2009 | int ret; | |
2010 | ssize_t nr_pids = 0; | |
2011 | ||
2012 | switch (domain) { | |
2013 | case LTTNG_DOMAIN_KERNEL: | |
2014 | { | |
2015 | struct ltt_kernel_session *ksess; | |
2016 | ||
2017 | ksess = session->kernel_session; | |
2018 | nr_pids = kernel_list_tracker_pids(ksess, pids); | |
2019 | if (nr_pids < 0) { | |
2020 | ret = LTTNG_ERR_KERN_LIST_FAIL; | |
2021 | goto error; | |
2022 | } | |
2023 | break; | |
2024 | } | |
2025 | case LTTNG_DOMAIN_UST: | |
2026 | { | |
2027 | struct ltt_ust_session *usess; | |
2028 | ||
2029 | usess = session->ust_session; | |
2030 | nr_pids = trace_ust_list_tracker_pids(usess, pids); | |
2031 | if (nr_pids < 0) { | |
2032 | ret = LTTNG_ERR_UST_LIST_FAIL; | |
2033 | goto error; | |
2034 | } | |
2035 | break; | |
2036 | } | |
2037 | case LTTNG_DOMAIN_LOG4J: | |
2038 | case LTTNG_DOMAIN_JUL: | |
2039 | case LTTNG_DOMAIN_PYTHON: | |
2040 | default: | |
2041 | ret = LTTNG_ERR_UND; | |
2042 | goto error; | |
2043 | } | |
2044 | ||
2045 | return nr_pids; | |
2046 | ||
2047 | error: | |
2048 | /* Return negative value to differentiate return code */ | |
2049 | return -ret; | |
2050 | } | |
2051 | ||
2f77fc4b DG |
2052 | /* |
2053 | * Command LTTNG_START_TRACE processed by the client thread. | |
a9ad0c8f MD |
2054 | * |
2055 | * Called with session mutex held. | |
2f77fc4b DG |
2056 | */ |
2057 | int cmd_start_trace(struct ltt_session *session) | |
2058 | { | |
2059 | int ret; | |
cde3e505 | 2060 | unsigned long nb_chan = 0; |
2f77fc4b DG |
2061 | struct ltt_kernel_session *ksession; |
2062 | struct ltt_ust_session *usess; | |
2f77fc4b DG |
2063 | |
2064 | assert(session); | |
2065 | ||
2066 | /* Ease our life a bit ;) */ | |
2067 | ksession = session->kernel_session; | |
2068 | usess = session->ust_session; | |
2069 | ||
8382cf6f DG |
2070 | /* Is the session already started? */ |
2071 | if (session->active) { | |
f73fabfd | 2072 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
2f77fc4b DG |
2073 | goto error; |
2074 | } | |
2075 | ||
cde3e505 DG |
2076 | /* |
2077 | * Starting a session without channel is useless since after that it's not | |
2078 | * possible to enable channel thus inform the client. | |
2079 | */ | |
2080 | if (usess && usess->domain_global.channels) { | |
2081 | nb_chan += lttng_ht_get_count(usess->domain_global.channels); | |
2082 | } | |
2083 | if (ksession) { | |
2084 | nb_chan += ksession->channel_count; | |
2085 | } | |
2086 | if (!nb_chan) { | |
2087 | ret = LTTNG_ERR_NO_CHANNEL; | |
2088 | goto error; | |
2089 | } | |
2090 | ||
2f77fc4b DG |
2091 | /* Kernel tracing */ |
2092 | if (ksession != NULL) { | |
9b6c7ec5 DG |
2093 | ret = start_kernel_session(ksession, kernel_tracer_fd); |
2094 | if (ret != LTTNG_OK) { | |
2f77fc4b DG |
2095 | goto error; |
2096 | } | |
2f77fc4b DG |
2097 | } |
2098 | ||
2099 | /* Flag session that trace should start automatically */ | |
2100 | if (usess) { | |
14fb1ebe DG |
2101 | /* |
2102 | * Even though the start trace might fail, flag this session active so | |
2103 | * other application coming in are started by default. | |
2104 | */ | |
2105 | usess->active = 1; | |
2f77fc4b DG |
2106 | |
2107 | ret = ust_app_start_trace_all(usess); | |
2108 | if (ret < 0) { | |
f73fabfd | 2109 | ret = LTTNG_ERR_UST_START_FAIL; |
2f77fc4b DG |
2110 | goto error; |
2111 | } | |
2112 | } | |
2113 | ||
8382cf6f DG |
2114 | /* Flag this after a successful start. */ |
2115 | session->has_been_started = 1; | |
2116 | session->active = 1; | |
9b6c7ec5 | 2117 | |
f73fabfd | 2118 | ret = LTTNG_OK; |
2f77fc4b DG |
2119 | |
2120 | error: | |
2121 | return ret; | |
2122 | } | |
2123 | ||
2124 | /* | |
2125 | * Command LTTNG_STOP_TRACE processed by the client thread. | |
2126 | */ | |
2127 | int cmd_stop_trace(struct ltt_session *session) | |
2128 | { | |
2129 | int ret; | |
2130 | struct ltt_kernel_channel *kchan; | |
2131 | struct ltt_kernel_session *ksession; | |
2132 | struct ltt_ust_session *usess; | |
2133 | ||
2134 | assert(session); | |
2135 | ||
2136 | /* Short cut */ | |
2137 | ksession = session->kernel_session; | |
2138 | usess = session->ust_session; | |
2139 | ||
8382cf6f DG |
2140 | /* Session is not active. Skip everythong and inform the client. */ |
2141 | if (!session->active) { | |
f73fabfd | 2142 | ret = LTTNG_ERR_TRACE_ALREADY_STOPPED; |
2f77fc4b DG |
2143 | goto error; |
2144 | } | |
2145 | ||
2f77fc4b | 2146 | /* Kernel tracer */ |
14fb1ebe | 2147 | if (ksession && ksession->active) { |
2f77fc4b DG |
2148 | DBG("Stop kernel tracing"); |
2149 | ||
2150 | /* Flush metadata if exist */ | |
2151 | if (ksession->metadata_stream_fd >= 0) { | |
2152 | ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd); | |
2153 | if (ret < 0) { | |
2154 | ERR("Kernel metadata flush failed"); | |
2155 | } | |
2156 | } | |
2157 | ||
2158 | /* Flush all buffers before stopping */ | |
2159 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { | |
2160 | ret = kernel_flush_buffer(kchan); | |
2161 | if (ret < 0) { | |
2162 | ERR("Kernel flush buffer error"); | |
2163 | } | |
2164 | } | |
2165 | ||
2166 | ret = kernel_stop_session(ksession); | |
2167 | if (ret < 0) { | |
f73fabfd | 2168 | ret = LTTNG_ERR_KERN_STOP_FAIL; |
2f77fc4b DG |
2169 | goto error; |
2170 | } | |
2171 | ||
2172 | kernel_wait_quiescent(kernel_tracer_fd); | |
9b6c7ec5 | 2173 | |
14fb1ebe | 2174 | ksession->active = 0; |
2f77fc4b DG |
2175 | } |
2176 | ||
14fb1ebe DG |
2177 | if (usess && usess->active) { |
2178 | /* | |
2179 | * Even though the stop trace might fail, flag this session inactive so | |
2180 | * other application coming in are not started by default. | |
2181 | */ | |
2182 | usess->active = 0; | |
2f77fc4b DG |
2183 | |
2184 | ret = ust_app_stop_trace_all(usess); | |
2185 | if (ret < 0) { | |
f73fabfd | 2186 | ret = LTTNG_ERR_UST_STOP_FAIL; |
2f77fc4b DG |
2187 | goto error; |
2188 | } | |
2189 | } | |
2190 | ||
8382cf6f DG |
2191 | /* Flag inactive after a successful stop. */ |
2192 | session->active = 0; | |
f73fabfd | 2193 | ret = LTTNG_OK; |
2f77fc4b DG |
2194 | |
2195 | error: | |
2196 | return ret; | |
2197 | } | |
2198 | ||
2199 | /* | |
2200 | * Command LTTNG_SET_CONSUMER_URI processed by the client thread. | |
2201 | */ | |
bda32d56 JG |
2202 | int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri, |
2203 | struct lttng_uri *uris) | |
2f77fc4b DG |
2204 | { |
2205 | int ret, i; | |
2206 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2207 | struct ltt_ust_session *usess = session->ust_session; | |
2f77fc4b DG |
2208 | |
2209 | assert(session); | |
2210 | assert(uris); | |
2211 | assert(nb_uri > 0); | |
2212 | ||
8382cf6f DG |
2213 | /* Can't set consumer URI if the session is active. */ |
2214 | if (session->active) { | |
f73fabfd | 2215 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
2f77fc4b DG |
2216 | goto error; |
2217 | } | |
2218 | ||
bda32d56 | 2219 | /* Set the "global" consumer URIs */ |
2f77fc4b | 2220 | for (i = 0; i < nb_uri; i++) { |
bda32d56 JG |
2221 | ret = add_uri_to_consumer(session->consumer, |
2222 | &uris[i], 0, session->name); | |
a74934ba | 2223 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2224 | goto error; |
2225 | } | |
2f77fc4b DG |
2226 | } |
2227 | ||
bda32d56 JG |
2228 | /* Set UST session URIs */ |
2229 | if (session->ust_session) { | |
2230 | for (i = 0; i < nb_uri; i++) { | |
2231 | ret = add_uri_to_consumer( | |
2232 | session->ust_session->consumer, | |
2233 | &uris[i], LTTNG_DOMAIN_UST, | |
2234 | session->name); | |
2235 | if (ret != LTTNG_OK) { | |
2236 | goto error; | |
2237 | } | |
2238 | } | |
2239 | } | |
2240 | ||
2241 | /* Set kernel session URIs */ | |
2242 | if (session->kernel_session) { | |
2243 | for (i = 0; i < nb_uri; i++) { | |
2244 | ret = add_uri_to_consumer( | |
2245 | session->kernel_session->consumer, | |
2246 | &uris[i], LTTNG_DOMAIN_KERNEL, | |
2247 | session->name); | |
2248 | if (ret != LTTNG_OK) { | |
2249 | goto error; | |
2250 | } | |
2251 | } | |
2252 | } | |
2253 | ||
7ab70fe0 DG |
2254 | /* |
2255 | * Make sure to set the session in output mode after we set URI since a | |
2256 | * session can be created without URL (thus flagged in no output mode). | |
2257 | */ | |
2258 | session->output_traces = 1; | |
2259 | if (ksess) { | |
2260 | ksess->output_traces = 1; | |
bda32d56 JG |
2261 | } |
2262 | ||
2263 | if (usess) { | |
7ab70fe0 DG |
2264 | usess->output_traces = 1; |
2265 | } | |
2266 | ||
2f77fc4b | 2267 | /* All good! */ |
f73fabfd | 2268 | ret = LTTNG_OK; |
2f77fc4b DG |
2269 | |
2270 | error: | |
2271 | return ret; | |
2272 | } | |
2273 | ||
2274 | /* | |
2275 | * Command LTTNG_CREATE_SESSION processed by the client thread. | |
2276 | */ | |
2277 | int cmd_create_session_uri(char *name, struct lttng_uri *uris, | |
ecc48a90 | 2278 | size_t nb_uri, lttng_sock_cred *creds, unsigned int live_timer) |
2f77fc4b DG |
2279 | { |
2280 | int ret; | |
2f77fc4b DG |
2281 | struct ltt_session *session; |
2282 | ||
2283 | assert(name); | |
2bba9e53 | 2284 | assert(creds); |
785d2d0d | 2285 | |
2f77fc4b DG |
2286 | /* |
2287 | * Verify if the session already exist | |
2288 | * | |
2289 | * XXX: There is no need for the session lock list here since the caller | |
2290 | * (process_client_msg) is holding it. We might want to change that so a | |
2291 | * single command does not lock the entire session list. | |
2292 | */ | |
2293 | session = session_find_by_name(name); | |
2294 | if (session != NULL) { | |
f73fabfd | 2295 | ret = LTTNG_ERR_EXIST_SESS; |
2f77fc4b DG |
2296 | goto find_error; |
2297 | } | |
2298 | ||
2299 | /* Create tracing session in the registry */ | |
dec56f6c | 2300 | ret = session_create(name, LTTNG_SOCK_GET_UID_CRED(creds), |
2f77fc4b | 2301 | LTTNG_SOCK_GET_GID_CRED(creds)); |
f73fabfd | 2302 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2303 | goto session_error; |
2304 | } | |
2305 | ||
2306 | /* | |
2307 | * Get the newly created session pointer back | |
2308 | * | |
2309 | * XXX: There is no need for the session lock list here since the caller | |
2310 | * (process_client_msg) is holding it. We might want to change that so a | |
2311 | * single command does not lock the entire session list. | |
2312 | */ | |
2313 | session = session_find_by_name(name); | |
2314 | assert(session); | |
2315 | ||
ecc48a90 | 2316 | session->live_timer = live_timer; |
2f77fc4b DG |
2317 | /* Create default consumer output for the session not yet created. */ |
2318 | session->consumer = consumer_create_output(CONSUMER_DST_LOCAL); | |
2319 | if (session->consumer == NULL) { | |
f73fabfd | 2320 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2321 | goto consumer_error; |
2322 | } | |
2323 | ||
2bba9e53 | 2324 | if (uris) { |
bda32d56 | 2325 | ret = cmd_set_consumer_uri(session, nb_uri, uris); |
2bba9e53 DG |
2326 | if (ret != LTTNG_OK) { |
2327 | goto consumer_error; | |
2328 | } | |
2329 | session->output_traces = 1; | |
2330 | } else { | |
2331 | session->output_traces = 0; | |
2332 | DBG2("Session %s created with no output", session->name); | |
2f77fc4b DG |
2333 | } |
2334 | ||
2335 | session->consumer->enabled = 1; | |
2336 | ||
f73fabfd | 2337 | return LTTNG_OK; |
2f77fc4b DG |
2338 | |
2339 | consumer_error: | |
2340 | session_destroy(session); | |
2341 | session_error: | |
2342 | find_error: | |
2343 | return ret; | |
2344 | } | |
2345 | ||
27babd3a DG |
2346 | /* |
2347 | * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread. | |
2348 | */ | |
2349 | int cmd_create_session_snapshot(char *name, struct lttng_uri *uris, | |
2350 | size_t nb_uri, lttng_sock_cred *creds) | |
2351 | { | |
2352 | int ret; | |
2353 | struct ltt_session *session; | |
2354 | struct snapshot_output *new_output = NULL; | |
2355 | ||
2356 | assert(name); | |
2357 | assert(creds); | |
2358 | ||
2359 | /* | |
2360 | * Create session in no output mode with URIs set to NULL. The uris we've | |
2361 | * received are for a default snapshot output if one. | |
2362 | */ | |
ae58b817 | 2363 | ret = cmd_create_session_uri(name, NULL, 0, creds, 0); |
27babd3a DG |
2364 | if (ret != LTTNG_OK) { |
2365 | goto error; | |
2366 | } | |
2367 | ||
2368 | /* Get the newly created session pointer back. This should NEVER fail. */ | |
2369 | session = session_find_by_name(name); | |
2370 | assert(session); | |
2371 | ||
2372 | /* Flag session for snapshot mode. */ | |
2373 | session->snapshot_mode = 1; | |
2374 | ||
2375 | /* Skip snapshot output creation if no URI is given. */ | |
2376 | if (nb_uri == 0) { | |
2377 | goto end; | |
2378 | } | |
2379 | ||
2380 | new_output = snapshot_output_alloc(); | |
2381 | if (!new_output) { | |
2382 | ret = LTTNG_ERR_NOMEM; | |
2383 | goto error_snapshot_alloc; | |
2384 | } | |
2385 | ||
2386 | ret = snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE, NULL, | |
2387 | uris, nb_uri, session->consumer, new_output, &session->snapshot); | |
2388 | if (ret < 0) { | |
2389 | if (ret == -ENOMEM) { | |
2390 | ret = LTTNG_ERR_NOMEM; | |
2391 | } else { | |
2392 | ret = LTTNG_ERR_INVALID; | |
2393 | } | |
2394 | goto error_snapshot; | |
2395 | } | |
2396 | ||
2397 | rcu_read_lock(); | |
2398 | snapshot_add_output(&session->snapshot, new_output); | |
2399 | rcu_read_unlock(); | |
2400 | ||
2401 | end: | |
2402 | return LTTNG_OK; | |
2403 | ||
2404 | error_snapshot: | |
2405 | snapshot_output_destroy(new_output); | |
2406 | error_snapshot_alloc: | |
2407 | session_destroy(session); | |
2408 | error: | |
2409 | return ret; | |
2410 | } | |
2411 | ||
2f77fc4b DG |
2412 | /* |
2413 | * Command LTTNG_DESTROY_SESSION processed by the client thread. | |
a9ad0c8f MD |
2414 | * |
2415 | * Called with session lock held. | |
2f77fc4b DG |
2416 | */ |
2417 | int cmd_destroy_session(struct ltt_session *session, int wpipe) | |
2418 | { | |
2419 | int ret; | |
2420 | struct ltt_ust_session *usess; | |
2421 | struct ltt_kernel_session *ksess; | |
2422 | ||
2423 | /* Safety net */ | |
2424 | assert(session); | |
2425 | ||
2426 | usess = session->ust_session; | |
2427 | ksess = session->kernel_session; | |
2428 | ||
2429 | /* Clean kernel session teardown */ | |
2430 | kernel_destroy_session(ksess); | |
2431 | ||
2432 | /* UST session teardown */ | |
2433 | if (usess) { | |
2434 | /* Close any relayd session */ | |
2435 | consumer_output_send_destroy_relayd(usess->consumer); | |
2436 | ||
2437 | /* Destroy every UST application related to this session. */ | |
2438 | ret = ust_app_destroy_trace_all(usess); | |
2439 | if (ret) { | |
2440 | ERR("Error in ust_app_destroy_trace_all"); | |
2441 | } | |
2442 | ||
2443 | /* Clean up the rest. */ | |
2444 | trace_ust_destroy_session(usess); | |
2445 | } | |
2446 | ||
2447 | /* | |
2448 | * Must notify the kernel thread here to update it's poll set in order to | |
2449 | * remove the channel(s)' fd just destroyed. | |
2450 | */ | |
2451 | ret = notify_thread_pipe(wpipe); | |
2452 | if (ret < 0) { | |
2453 | PERROR("write kernel poll pipe"); | |
2454 | } | |
2455 | ||
2456 | ret = session_destroy(session); | |
2457 | ||
2458 | return ret; | |
2459 | } | |
2460 | ||
2461 | /* | |
2462 | * Command LTTNG_CALIBRATE processed by the client thread. | |
2463 | */ | |
56a37563 JG |
2464 | int cmd_calibrate(enum lttng_domain_type domain, |
2465 | struct lttng_calibrate *calibrate) | |
2f77fc4b DG |
2466 | { |
2467 | int ret; | |
2468 | ||
2469 | switch (domain) { | |
2470 | case LTTNG_DOMAIN_KERNEL: | |
2471 | { | |
2472 | struct lttng_kernel_calibrate kcalibrate; | |
2473 | ||
2e84128e DG |
2474 | switch (calibrate->type) { |
2475 | case LTTNG_CALIBRATE_FUNCTION: | |
2476 | default: | |
2477 | /* Default and only possible calibrate option. */ | |
2478 | kcalibrate.type = LTTNG_KERNEL_CALIBRATE_KRETPROBE; | |
2479 | break; | |
2480 | } | |
2481 | ||
2f77fc4b DG |
2482 | ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate); |
2483 | if (ret < 0) { | |
f73fabfd | 2484 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
2f77fc4b DG |
2485 | goto error; |
2486 | } | |
2487 | break; | |
2488 | } | |
2489 | case LTTNG_DOMAIN_UST: | |
2490 | { | |
2491 | struct lttng_ust_calibrate ucalibrate; | |
2492 | ||
2e84128e DG |
2493 | switch (calibrate->type) { |
2494 | case LTTNG_CALIBRATE_FUNCTION: | |
2495 | default: | |
2496 | /* Default and only possible calibrate option. */ | |
2497 | ucalibrate.type = LTTNG_UST_CALIBRATE_TRACEPOINT; | |
2498 | break; | |
2499 | } | |
2500 | ||
2f77fc4b DG |
2501 | ret = ust_app_calibrate_glb(&ucalibrate); |
2502 | if (ret < 0) { | |
f73fabfd | 2503 | ret = LTTNG_ERR_UST_CALIBRATE_FAIL; |
2f77fc4b DG |
2504 | goto error; |
2505 | } | |
2506 | break; | |
2507 | } | |
2508 | default: | |
f73fabfd | 2509 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2510 | goto error; |
2511 | } | |
2512 | ||
f73fabfd | 2513 | ret = LTTNG_OK; |
2f77fc4b DG |
2514 | |
2515 | error: | |
2516 | return ret; | |
2517 | } | |
2518 | ||
2519 | /* | |
2520 | * Command LTTNG_REGISTER_CONSUMER processed by the client thread. | |
2521 | */ | |
56a37563 JG |
2522 | int cmd_register_consumer(struct ltt_session *session, |
2523 | enum lttng_domain_type domain, const char *sock_path, | |
2524 | struct consumer_data *cdata) | |
2f77fc4b DG |
2525 | { |
2526 | int ret, sock; | |
dd81b457 | 2527 | struct consumer_socket *socket = NULL; |
2f77fc4b DG |
2528 | |
2529 | assert(session); | |
2530 | assert(cdata); | |
2531 | assert(sock_path); | |
2532 | ||
2533 | switch (domain) { | |
2534 | case LTTNG_DOMAIN_KERNEL: | |
2535 | { | |
2536 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2537 | ||
2538 | assert(ksess); | |
2539 | ||
2540 | /* Can't register a consumer if there is already one */ | |
2541 | if (ksess->consumer_fds_sent != 0) { | |
f73fabfd | 2542 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
2f77fc4b DG |
2543 | goto error; |
2544 | } | |
2545 | ||
2546 | sock = lttcomm_connect_unix_sock(sock_path); | |
2547 | if (sock < 0) { | |
f73fabfd | 2548 | ret = LTTNG_ERR_CONNECT_FAIL; |
2f77fc4b DG |
2549 | goto error; |
2550 | } | |
4ce514c4 | 2551 | cdata->cmd_sock = sock; |
2f77fc4b | 2552 | |
4ce514c4 | 2553 | socket = consumer_allocate_socket(&cdata->cmd_sock); |
2f77fc4b | 2554 | if (socket == NULL) { |
f66c074c DG |
2555 | ret = close(sock); |
2556 | if (ret < 0) { | |
2557 | PERROR("close register consumer"); | |
2558 | } | |
4ce514c4 | 2559 | cdata->cmd_sock = -1; |
f73fabfd | 2560 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2561 | goto error; |
2562 | } | |
2563 | ||
2564 | socket->lock = zmalloc(sizeof(pthread_mutex_t)); | |
2565 | if (socket->lock == NULL) { | |
2566 | PERROR("zmalloc pthread mutex"); | |
f73fabfd | 2567 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2568 | goto error; |
2569 | } | |
2570 | pthread_mutex_init(socket->lock, NULL); | |
2571 | socket->registered = 1; | |
2572 | ||
2573 | rcu_read_lock(); | |
2574 | consumer_add_socket(socket, ksess->consumer); | |
2575 | rcu_read_unlock(); | |
2576 | ||
2577 | pthread_mutex_lock(&cdata->pid_mutex); | |
2578 | cdata->pid = -1; | |
2579 | pthread_mutex_unlock(&cdata->pid_mutex); | |
2580 | ||
2581 | break; | |
2582 | } | |
2583 | default: | |
2584 | /* TODO: Userspace tracing */ | |
f73fabfd | 2585 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2586 | goto error; |
2587 | } | |
2588 | ||
dd81b457 | 2589 | return LTTNG_OK; |
2f77fc4b DG |
2590 | |
2591 | error: | |
dd81b457 DG |
2592 | if (socket) { |
2593 | consumer_destroy_socket(socket); | |
2594 | } | |
2f77fc4b DG |
2595 | return ret; |
2596 | } | |
2597 | ||
2598 | /* | |
2599 | * Command LTTNG_LIST_DOMAINS processed by the client thread. | |
2600 | */ | |
2601 | ssize_t cmd_list_domains(struct ltt_session *session, | |
2602 | struct lttng_domain **domains) | |
2603 | { | |
2604 | int ret, index = 0; | |
2605 | ssize_t nb_dom = 0; | |
fefd409b DG |
2606 | struct agent *agt; |
2607 | struct lttng_ht_iter iter; | |
2f77fc4b DG |
2608 | |
2609 | if (session->kernel_session != NULL) { | |
2610 | DBG3("Listing domains found kernel domain"); | |
2611 | nb_dom++; | |
2612 | } | |
2613 | ||
2614 | if (session->ust_session != NULL) { | |
2615 | DBG3("Listing domains found UST global domain"); | |
2616 | nb_dom++; | |
3c6a091f | 2617 | |
e0a74f01 | 2618 | rcu_read_lock(); |
fefd409b DG |
2619 | cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, |
2620 | agt, node.node) { | |
2621 | if (agt->being_used) { | |
2622 | nb_dom++; | |
2623 | } | |
3c6a091f | 2624 | } |
e0a74f01 | 2625 | rcu_read_unlock(); |
2f77fc4b DG |
2626 | } |
2627 | ||
fa64dfb4 JG |
2628 | if (!nb_dom) { |
2629 | goto end; | |
2630 | } | |
2631 | ||
2f77fc4b DG |
2632 | *domains = zmalloc(nb_dom * sizeof(struct lttng_domain)); |
2633 | if (*domains == NULL) { | |
f73fabfd | 2634 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2635 | goto error; |
2636 | } | |
2637 | ||
2638 | if (session->kernel_session != NULL) { | |
2639 | (*domains)[index].type = LTTNG_DOMAIN_KERNEL; | |
b5edb9e8 PP |
2640 | |
2641 | /* Kernel session buffer type is always GLOBAL */ | |
2642 | (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL; | |
2643 | ||
2f77fc4b DG |
2644 | index++; |
2645 | } | |
2646 | ||
2647 | if (session->ust_session != NULL) { | |
2648 | (*domains)[index].type = LTTNG_DOMAIN_UST; | |
88c5f0d8 | 2649 | (*domains)[index].buf_type = session->ust_session->buffer_type; |
2f77fc4b | 2650 | index++; |
3c6a091f | 2651 | |
e0a74f01 | 2652 | rcu_read_lock(); |
fefd409b DG |
2653 | cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, |
2654 | agt, node.node) { | |
2655 | if (agt->being_used) { | |
2656 | (*domains)[index].type = agt->domain; | |
2657 | (*domains)[index].buf_type = session->ust_session->buffer_type; | |
2658 | index++; | |
2659 | } | |
3c6a091f | 2660 | } |
e0a74f01 | 2661 | rcu_read_unlock(); |
2f77fc4b | 2662 | } |
fa64dfb4 | 2663 | end: |
2f77fc4b DG |
2664 | return nb_dom; |
2665 | ||
2666 | error: | |
f73fabfd DG |
2667 | /* Return negative value to differentiate return code */ |
2668 | return -ret; | |
2f77fc4b DG |
2669 | } |
2670 | ||
2671 | ||
2672 | /* | |
2673 | * Command LTTNG_LIST_CHANNELS processed by the client thread. | |
2674 | */ | |
56a37563 JG |
2675 | ssize_t cmd_list_channels(enum lttng_domain_type domain, |
2676 | struct ltt_session *session, struct lttng_channel **channels) | |
2f77fc4b DG |
2677 | { |
2678 | int ret; | |
2679 | ssize_t nb_chan = 0; | |
2680 | ||
2681 | switch (domain) { | |
2682 | case LTTNG_DOMAIN_KERNEL: | |
2683 | if (session->kernel_session != NULL) { | |
2684 | nb_chan = session->kernel_session->channel_count; | |
2685 | } | |
2686 | DBG3("Number of kernel channels %zd", nb_chan); | |
c7d620a2 DG |
2687 | if (nb_chan <= 0) { |
2688 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; | |
2689 | } | |
2f77fc4b DG |
2690 | break; |
2691 | case LTTNG_DOMAIN_UST: | |
2692 | if (session->ust_session != NULL) { | |
7ffc31a2 | 2693 | rcu_read_lock(); |
2f77fc4b | 2694 | nb_chan = lttng_ht_get_count( |
7ffc31a2 JG |
2695 | session->ust_session->domain_global.channels); |
2696 | rcu_read_unlock(); | |
2f77fc4b DG |
2697 | } |
2698 | DBG3("Number of UST global channels %zd", nb_chan); | |
ade7ce52 | 2699 | if (nb_chan < 0) { |
c7d620a2 | 2700 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
ade7ce52 | 2701 | goto error; |
c7d620a2 | 2702 | } |
2f77fc4b DG |
2703 | break; |
2704 | default: | |
f73fabfd | 2705 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2706 | goto error; |
2707 | } | |
2708 | ||
2709 | if (nb_chan > 0) { | |
2710 | *channels = zmalloc(nb_chan * sizeof(struct lttng_channel)); | |
2711 | if (*channels == NULL) { | |
f73fabfd | 2712 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2713 | goto error; |
2714 | } | |
2715 | ||
2716 | list_lttng_channels(domain, session, *channels); | |
2f77fc4b DG |
2717 | } |
2718 | ||
2719 | return nb_chan; | |
2720 | ||
2721 | error: | |
f73fabfd DG |
2722 | /* Return negative value to differentiate return code */ |
2723 | return -ret; | |
2f77fc4b DG |
2724 | } |
2725 | ||
2726 | /* | |
2727 | * Command LTTNG_LIST_EVENTS processed by the client thread. | |
2728 | */ | |
56a37563 JG |
2729 | ssize_t cmd_list_events(enum lttng_domain_type domain, |
2730 | struct ltt_session *session, char *channel_name, | |
2731 | struct lttng_event **events) | |
2f77fc4b DG |
2732 | { |
2733 | int ret = 0; | |
2734 | ssize_t nb_event = 0; | |
2735 | ||
2736 | switch (domain) { | |
2737 | case LTTNG_DOMAIN_KERNEL: | |
2738 | if (session->kernel_session != NULL) { | |
2739 | nb_event = list_lttng_kernel_events(channel_name, | |
2740 | session->kernel_session, events); | |
2741 | } | |
2742 | break; | |
2743 | case LTTNG_DOMAIN_UST: | |
2744 | { | |
2745 | if (session->ust_session != NULL) { | |
2746 | nb_event = list_lttng_ust_global_events(channel_name, | |
2747 | &session->ust_session->domain_global, events); | |
2748 | } | |
2749 | break; | |
2750 | } | |
5cdb6027 | 2751 | case LTTNG_DOMAIN_LOG4J: |
3c6a091f | 2752 | case LTTNG_DOMAIN_JUL: |
0e115563 | 2753 | case LTTNG_DOMAIN_PYTHON: |
3c6a091f | 2754 | if (session->ust_session) { |
fefd409b DG |
2755 | struct lttng_ht_iter iter; |
2756 | struct agent *agt; | |
2757 | ||
b11feea5 | 2758 | rcu_read_lock(); |
fefd409b DG |
2759 | cds_lfht_for_each_entry(session->ust_session->agents->ht, |
2760 | &iter.iter, agt, node.node) { | |
1dfd9906 JG |
2761 | if (agt->domain == domain) { |
2762 | nb_event = list_lttng_agent_events( | |
2763 | agt, events); | |
2764 | break; | |
2765 | } | |
fefd409b | 2766 | } |
b11feea5 | 2767 | rcu_read_unlock(); |
3c6a091f DG |
2768 | } |
2769 | break; | |
2f77fc4b | 2770 | default: |
f73fabfd | 2771 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2772 | goto error; |
2773 | } | |
2774 | ||
f73fabfd | 2775 | return nb_event; |
2f77fc4b DG |
2776 | |
2777 | error: | |
f73fabfd DG |
2778 | /* Return negative value to differentiate return code */ |
2779 | return -ret; | |
2f77fc4b DG |
2780 | } |
2781 | ||
2782 | /* | |
2783 | * Using the session list, filled a lttng_session array to send back to the | |
2784 | * client for session listing. | |
2785 | * | |
2786 | * The session list lock MUST be acquired before calling this function. Use | |
2787 | * session_lock_list() and session_unlock_list(). | |
2788 | */ | |
2789 | void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid, | |
2790 | gid_t gid) | |
2791 | { | |
2792 | int ret; | |
2793 | unsigned int i = 0; | |
2794 | struct ltt_session *session; | |
2795 | struct ltt_session_list *list = session_get_list(); | |
2796 | ||
2797 | DBG("Getting all available session for UID %d GID %d", | |
2798 | uid, gid); | |
2799 | /* | |
2800 | * Iterate over session list and append data after the control struct in | |
2801 | * the buffer. | |
2802 | */ | |
2803 | cds_list_for_each_entry(session, &list->head, list) { | |
2804 | /* | |
2805 | * Only list the sessions the user can control. | |
2806 | */ | |
2807 | if (!session_access_ok(session, uid, gid)) { | |
2808 | continue; | |
2809 | } | |
2810 | ||
2811 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2812 | struct ltt_ust_session *usess = session->ust_session; | |
2813 | ||
2814 | if (session->consumer->type == CONSUMER_DST_NET || | |
2815 | (ksess && ksess->consumer->type == CONSUMER_DST_NET) || | |
2816 | (usess && usess->consumer->type == CONSUMER_DST_NET)) { | |
2817 | ret = build_network_session_path(sessions[i].path, | |
dec56f6c | 2818 | sizeof(sessions[i].path), session); |
2f77fc4b | 2819 | } else { |
dec56f6c | 2820 | ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s", |
2f77fc4b DG |
2821 | session->consumer->dst.trace_path); |
2822 | } | |
2823 | if (ret < 0) { | |
2824 | PERROR("snprintf session path"); | |
2825 | continue; | |
2826 | } | |
2827 | ||
2828 | strncpy(sessions[i].name, session->name, NAME_MAX); | |
2829 | sessions[i].name[NAME_MAX - 1] = '\0'; | |
8382cf6f | 2830 | sessions[i].enabled = session->active; |
2cbf8fed | 2831 | sessions[i].snapshot_mode = session->snapshot_mode; |
8960e9cd | 2832 | sessions[i].live_timer_interval = session->live_timer; |
2f77fc4b DG |
2833 | i++; |
2834 | } | |
2835 | } | |
2836 | ||
806e2684 | 2837 | /* |
6d805429 | 2838 | * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning |
d3f14b8a | 2839 | * ready for trace analysis (or any kind of reader) or else 1 for pending data. |
806e2684 | 2840 | */ |
6d805429 | 2841 | int cmd_data_pending(struct ltt_session *session) |
806e2684 DG |
2842 | { |
2843 | int ret; | |
2844 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2845 | struct ltt_ust_session *usess = session->ust_session; | |
2846 | ||
2847 | assert(session); | |
2848 | ||
2849 | /* Session MUST be stopped to ask for data availability. */ | |
8382cf6f | 2850 | if (session->active) { |
806e2684 DG |
2851 | ret = LTTNG_ERR_SESSION_STARTED; |
2852 | goto error; | |
3a89d11a DG |
2853 | } else { |
2854 | /* | |
2855 | * If stopped, just make sure we've started before else the above call | |
2856 | * will always send that there is data pending. | |
2857 | * | |
2858 | * The consumer assumes that when the data pending command is received, | |
2859 | * the trace has been started before or else no output data is written | |
2860 | * by the streams which is a condition for data pending. So, this is | |
2861 | * *VERY* important that we don't ask the consumer before a start | |
2862 | * trace. | |
2863 | */ | |
8382cf6f | 2864 | if (!session->has_been_started) { |
3a89d11a DG |
2865 | ret = 0; |
2866 | goto error; | |
2867 | } | |
806e2684 DG |
2868 | } |
2869 | ||
2870 | if (ksess && ksess->consumer) { | |
6d805429 DG |
2871 | ret = consumer_is_data_pending(ksess->id, ksess->consumer); |
2872 | if (ret == 1) { | |
806e2684 DG |
2873 | /* Data is still being extracted for the kernel. */ |
2874 | goto error; | |
2875 | } | |
2876 | } | |
2877 | ||
2878 | if (usess && usess->consumer) { | |
6d805429 DG |
2879 | ret = consumer_is_data_pending(usess->id, usess->consumer); |
2880 | if (ret == 1) { | |
806e2684 DG |
2881 | /* Data is still being extracted for the kernel. */ |
2882 | goto error; | |
2883 | } | |
2884 | } | |
2885 | ||
2886 | /* Data is ready to be read by a viewer */ | |
6d805429 | 2887 | ret = 0; |
806e2684 DG |
2888 | |
2889 | error: | |
2890 | return ret; | |
2891 | } | |
2892 | ||
6dc3064a DG |
2893 | /* |
2894 | * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library. | |
2895 | * | |
2896 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
2897 | */ | |
2898 | int cmd_snapshot_add_output(struct ltt_session *session, | |
2899 | struct lttng_snapshot_output *output, uint32_t *id) | |
2900 | { | |
2901 | int ret; | |
2902 | struct snapshot_output *new_output; | |
2903 | ||
2904 | assert(session); | |
2905 | assert(output); | |
2906 | ||
2907 | DBG("Cmd snapshot add output for session %s", session->name); | |
2908 | ||
2909 | /* | |
d3f14b8a MD |
2910 | * Permission denied to create an output if the session is not |
2911 | * set in no output mode. | |
6dc3064a DG |
2912 | */ |
2913 | if (session->output_traces) { | |
2914 | ret = LTTNG_ERR_EPERM; | |
2915 | goto error; | |
2916 | } | |
2917 | ||
2918 | /* Only one output is allowed until we have the "tee" feature. */ | |
2919 | if (session->snapshot.nb_output == 1) { | |
2920 | ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST; | |
2921 | goto error; | |
2922 | } | |
2923 | ||
2924 | new_output = snapshot_output_alloc(); | |
2925 | if (!new_output) { | |
2926 | ret = LTTNG_ERR_NOMEM; | |
2927 | goto error; | |
2928 | } | |
2929 | ||
2930 | ret = snapshot_output_init(output->max_size, output->name, | |
2931 | output->ctrl_url, output->data_url, session->consumer, new_output, | |
2932 | &session->snapshot); | |
2933 | if (ret < 0) { | |
2934 | if (ret == -ENOMEM) { | |
2935 | ret = LTTNG_ERR_NOMEM; | |
2936 | } else { | |
2937 | ret = LTTNG_ERR_INVALID; | |
2938 | } | |
2939 | goto free_error; | |
2940 | } | |
2941 | ||
6dc3064a DG |
2942 | rcu_read_lock(); |
2943 | snapshot_add_output(&session->snapshot, new_output); | |
2944 | if (id) { | |
2945 | *id = new_output->id; | |
2946 | } | |
2947 | rcu_read_unlock(); | |
2948 | ||
2949 | return LTTNG_OK; | |
2950 | ||
2951 | free_error: | |
2952 | snapshot_output_destroy(new_output); | |
2953 | error: | |
2954 | return ret; | |
2955 | } | |
2956 | ||
2957 | /* | |
2958 | * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl. | |
2959 | * | |
2960 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
2961 | */ | |
2962 | int cmd_snapshot_del_output(struct ltt_session *session, | |
2963 | struct lttng_snapshot_output *output) | |
2964 | { | |
2965 | int ret; | |
eb240553 | 2966 | struct snapshot_output *sout = NULL; |
6dc3064a DG |
2967 | |
2968 | assert(session); | |
2969 | assert(output); | |
2970 | ||
6dc3064a DG |
2971 | rcu_read_lock(); |
2972 | ||
2973 | /* | |
d3f14b8a MD |
2974 | * Permission denied to create an output if the session is not |
2975 | * set in no output mode. | |
6dc3064a DG |
2976 | */ |
2977 | if (session->output_traces) { | |
2978 | ret = LTTNG_ERR_EPERM; | |
2979 | goto error; | |
2980 | } | |
2981 | ||
eb240553 DG |
2982 | if (output->id) { |
2983 | DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id, | |
2984 | session->name); | |
2985 | sout = snapshot_find_output_by_id(output->id, &session->snapshot); | |
2986 | } else if (*output->name != '\0') { | |
2987 | DBG("Cmd snapshot del output name %s for session %s", output->name, | |
2988 | session->name); | |
2989 | sout = snapshot_find_output_by_name(output->name, &session->snapshot); | |
2990 | } | |
6dc3064a DG |
2991 | if (!sout) { |
2992 | ret = LTTNG_ERR_INVALID; | |
2993 | goto error; | |
2994 | } | |
2995 | ||
2996 | snapshot_delete_output(&session->snapshot, sout); | |
2997 | snapshot_output_destroy(sout); | |
2998 | ret = LTTNG_OK; | |
2999 | ||
3000 | error: | |
3001 | rcu_read_unlock(); | |
3002 | return ret; | |
3003 | } | |
3004 | ||
3005 | /* | |
3006 | * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl. | |
3007 | * | |
3008 | * If no output is available, outputs is untouched and 0 is returned. | |
3009 | * | |
3010 | * Return the size of the newly allocated outputs or a negative LTTNG_ERR code. | |
3011 | */ | |
3012 | ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, | |
3013 | struct lttng_snapshot_output **outputs) | |
3014 | { | |
3015 | int ret, idx = 0; | |
b223ca94 | 3016 | struct lttng_snapshot_output *list = NULL; |
6dc3064a DG |
3017 | struct lttng_ht_iter iter; |
3018 | struct snapshot_output *output; | |
3019 | ||
3020 | assert(session); | |
3021 | assert(outputs); | |
3022 | ||
3023 | DBG("Cmd snapshot list outputs for session %s", session->name); | |
3024 | ||
3025 | /* | |
d3f14b8a MD |
3026 | * Permission denied to create an output if the session is not |
3027 | * set in no output mode. | |
6dc3064a DG |
3028 | */ |
3029 | if (session->output_traces) { | |
b223ca94 | 3030 | ret = -LTTNG_ERR_EPERM; |
6dc3064a DG |
3031 | goto error; |
3032 | } | |
3033 | ||
3034 | if (session->snapshot.nb_output == 0) { | |
3035 | ret = 0; | |
3036 | goto error; | |
3037 | } | |
3038 | ||
3039 | list = zmalloc(session->snapshot.nb_output * sizeof(*list)); | |
3040 | if (!list) { | |
b223ca94 | 3041 | ret = -LTTNG_ERR_NOMEM; |
6dc3064a DG |
3042 | goto error; |
3043 | } | |
3044 | ||
3045 | /* Copy list from session to the new list object. */ | |
b223ca94 | 3046 | rcu_read_lock(); |
6dc3064a DG |
3047 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter, |
3048 | output, node.node) { | |
3049 | assert(output->consumer); | |
3050 | list[idx].id = output->id; | |
3051 | list[idx].max_size = output->max_size; | |
3052 | strncpy(list[idx].name, output->name, sizeof(list[idx].name)); | |
3053 | if (output->consumer->type == CONSUMER_DST_LOCAL) { | |
3054 | strncpy(list[idx].ctrl_url, output->consumer->dst.trace_path, | |
3055 | sizeof(list[idx].ctrl_url)); | |
3056 | } else { | |
3057 | /* Control URI. */ | |
3058 | ret = uri_to_str_url(&output->consumer->dst.net.control, | |
3059 | list[idx].ctrl_url, sizeof(list[idx].ctrl_url)); | |
3060 | if (ret < 0) { | |
b223ca94 JG |
3061 | ret = -LTTNG_ERR_NOMEM; |
3062 | goto error; | |
6dc3064a DG |
3063 | } |
3064 | ||
3065 | /* Data URI. */ | |
3066 | ret = uri_to_str_url(&output->consumer->dst.net.data, | |
3067 | list[idx].data_url, sizeof(list[idx].data_url)); | |
3068 | if (ret < 0) { | |
b223ca94 JG |
3069 | ret = -LTTNG_ERR_NOMEM; |
3070 | goto error; | |
6dc3064a DG |
3071 | } |
3072 | } | |
3073 | idx++; | |
3074 | } | |
3075 | ||
3076 | *outputs = list; | |
b223ca94 JG |
3077 | list = NULL; |
3078 | ret = session->snapshot.nb_output; | |
6dc3064a | 3079 | error: |
b223ca94 JG |
3080 | free(list); |
3081 | rcu_read_unlock(); | |
3082 | return ret; | |
6dc3064a DG |
3083 | } |
3084 | ||
3085 | /* | |
3086 | * Send relayd sockets from snapshot output to consumer. Ignore request if the | |
3087 | * snapshot output is *not* set with a remote destination. | |
3088 | * | |
a934f4af | 3089 | * Return 0 on success or a LTTNG_ERR code. |
6dc3064a DG |
3090 | */ |
3091 | static int set_relayd_for_snapshot(struct consumer_output *consumer, | |
3092 | struct snapshot_output *snap_output, struct ltt_session *session) | |
3093 | { | |
a934f4af | 3094 | int ret = LTTNG_OK; |
6dc3064a DG |
3095 | struct lttng_ht_iter iter; |
3096 | struct consumer_socket *socket; | |
3097 | ||
3098 | assert(consumer); | |
3099 | assert(snap_output); | |
3100 | assert(session); | |
3101 | ||
3102 | DBG2("Set relayd object from snapshot output"); | |
3103 | ||
3104 | /* Ignore if snapshot consumer output is not network. */ | |
3105 | if (snap_output->consumer->type != CONSUMER_DST_NET) { | |
3106 | goto error; | |
3107 | } | |
3108 | ||
3109 | /* | |
3110 | * For each consumer socket, create and send the relayd object of the | |
3111 | * snapshot output. | |
3112 | */ | |
3113 | rcu_read_lock(); | |
5eecee74 DG |
3114 | cds_lfht_for_each_entry(snap_output->consumer->socks->ht, &iter.iter, |
3115 | socket, node.node) { | |
6dc3064a | 3116 | ret = send_consumer_relayd_sockets(0, session->id, |
d3e2ba59 JD |
3117 | snap_output->consumer, socket, |
3118 | session->name, session->hostname, | |
3119 | session->live_timer); | |
a934f4af | 3120 | if (ret != LTTNG_OK) { |
6dc3064a DG |
3121 | rcu_read_unlock(); |
3122 | goto error; | |
3123 | } | |
3124 | } | |
3125 | rcu_read_unlock(); | |
3126 | ||
3127 | error: | |
3128 | return ret; | |
3129 | } | |
3130 | ||
3131 | /* | |
3132 | * Record a kernel snapshot. | |
3133 | * | |
fac41e72 | 3134 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
6dc3064a DG |
3135 | */ |
3136 | static int record_kernel_snapshot(struct ltt_kernel_session *ksess, | |
5c786ded | 3137 | struct snapshot_output *output, struct ltt_session *session, |
d07ceecd | 3138 | int wait, uint64_t nb_packets_per_stream) |
6dc3064a DG |
3139 | { |
3140 | int ret; | |
3141 | ||
3142 | assert(ksess); | |
3143 | assert(output); | |
3144 | assert(session); | |
3145 | ||
10a50311 JD |
3146 | /* Get the datetime for the snapshot output directory. */ |
3147 | ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime, | |
3148 | sizeof(output->datetime)); | |
3149 | if (!ret) { | |
a934f4af | 3150 | ret = LTTNG_ERR_INVALID; |
10a50311 JD |
3151 | goto error; |
3152 | } | |
3153 | ||
af706bb7 DG |
3154 | /* |
3155 | * Copy kernel session sockets so we can communicate with the right | |
3156 | * consumer for the snapshot record command. | |
3157 | */ | |
3158 | ret = consumer_copy_sockets(output->consumer, ksess->consumer); | |
3159 | if (ret < 0) { | |
a934f4af | 3160 | ret = LTTNG_ERR_NOMEM; |
af706bb7 | 3161 | goto error; |
6dc3064a DG |
3162 | } |
3163 | ||
3164 | ret = set_relayd_for_snapshot(ksess->consumer, output, session); | |
a934f4af | 3165 | if (ret != LTTNG_OK) { |
af706bb7 | 3166 | goto error_snapshot; |
6dc3064a DG |
3167 | } |
3168 | ||
d07ceecd | 3169 | ret = kernel_snapshot_record(ksess, output, wait, nb_packets_per_stream); |
2a06df8d | 3170 | if (ret != LTTNG_OK) { |
af706bb7 | 3171 | goto error_snapshot; |
6dc3064a DG |
3172 | } |
3173 | ||
a934f4af | 3174 | ret = LTTNG_OK; |
1371fc12 | 3175 | goto end; |
a934f4af | 3176 | |
af706bb7 DG |
3177 | error_snapshot: |
3178 | /* Clean up copied sockets so this output can use some other later on. */ | |
3179 | consumer_destroy_output_sockets(output->consumer); | |
6dc3064a | 3180 | error: |
1371fc12 | 3181 | end: |
6dc3064a DG |
3182 | return ret; |
3183 | } | |
3184 | ||
3185 | /* | |
3186 | * Record a UST snapshot. | |
3187 | * | |
a934f4af | 3188 | * Return 0 on success or a LTTNG_ERR error code. |
6dc3064a DG |
3189 | */ |
3190 | static int record_ust_snapshot(struct ltt_ust_session *usess, | |
5c786ded | 3191 | struct snapshot_output *output, struct ltt_session *session, |
d07ceecd | 3192 | int wait, uint64_t nb_packets_per_stream) |
6dc3064a DG |
3193 | { |
3194 | int ret; | |
3195 | ||
3196 | assert(usess); | |
3197 | assert(output); | |
3198 | assert(session); | |
3199 | ||
10a50311 JD |
3200 | /* Get the datetime for the snapshot output directory. */ |
3201 | ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime, | |
3202 | sizeof(output->datetime)); | |
3203 | if (!ret) { | |
a934f4af | 3204 | ret = LTTNG_ERR_INVALID; |
10a50311 JD |
3205 | goto error; |
3206 | } | |
3207 | ||
af706bb7 | 3208 | /* |
d3f14b8a | 3209 | * Copy UST session sockets so we can communicate with the right |
af706bb7 DG |
3210 | * consumer for the snapshot record command. |
3211 | */ | |
3212 | ret = consumer_copy_sockets(output->consumer, usess->consumer); | |
3213 | if (ret < 0) { | |
a934f4af | 3214 | ret = LTTNG_ERR_NOMEM; |
af706bb7 | 3215 | goto error; |
6dc3064a DG |
3216 | } |
3217 | ||
3218 | ret = set_relayd_for_snapshot(usess->consumer, output, session); | |
a934f4af | 3219 | if (ret != LTTNG_OK) { |
af706bb7 | 3220 | goto error_snapshot; |
6dc3064a DG |
3221 | } |
3222 | ||
d07ceecd | 3223 | ret = ust_app_snapshot_record(usess, output, wait, nb_packets_per_stream); |
6dc3064a | 3224 | if (ret < 0) { |
7badf927 DG |
3225 | switch (-ret) { |
3226 | case EINVAL: | |
a934f4af | 3227 | ret = LTTNG_ERR_INVALID; |
7badf927 DG |
3228 | break; |
3229 | case ENODATA: | |
3230 | ret = LTTNG_ERR_SNAPSHOT_NODATA; | |
3231 | break; | |
3232 | default: | |
3233 | ret = LTTNG_ERR_SNAPSHOT_FAIL; | |
3234 | break; | |
5c786ded | 3235 | } |
af706bb7 | 3236 | goto error_snapshot; |
6dc3064a DG |
3237 | } |
3238 | ||
a934f4af DG |
3239 | ret = LTTNG_OK; |
3240 | ||
af706bb7 DG |
3241 | error_snapshot: |
3242 | /* Clean up copied sockets so this output can use some other later on. */ | |
3243 | consumer_destroy_output_sockets(output->consumer); | |
6dc3064a DG |
3244 | error: |
3245 | return ret; | |
3246 | } | |
3247 | ||
d07ceecd MD |
3248 | static |
3249 | uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session *session, | |
3250 | uint64_t cur_nr_packets) | |
68808f4e | 3251 | { |
d07ceecd | 3252 | uint64_t tot_size = 0; |
68808f4e DG |
3253 | |
3254 | if (session->kernel_session) { | |
3255 | struct ltt_kernel_channel *chan; | |
3256 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3257 | ||
68808f4e | 3258 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { |
d07ceecd MD |
3259 | if (cur_nr_packets >= chan->channel->attr.num_subbuf) { |
3260 | /* | |
3261 | * Don't take channel into account if we | |
3262 | * already grab all its packets. | |
3263 | */ | |
3264 | continue; | |
68808f4e | 3265 | } |
d07ceecd MD |
3266 | tot_size += chan->channel->attr.subbuf_size |
3267 | * chan->stream_count; | |
68808f4e DG |
3268 | } |
3269 | } | |
3270 | ||
3271 | if (session->ust_session) { | |
68808f4e DG |
3272 | struct ltt_ust_session *usess = session->ust_session; |
3273 | ||
d07ceecd MD |
3274 | tot_size += ust_app_get_size_one_more_packet_per_stream(usess, |
3275 | cur_nr_packets); | |
68808f4e DG |
3276 | } |
3277 | ||
d07ceecd | 3278 | return tot_size; |
68808f4e DG |
3279 | } |
3280 | ||
5c786ded | 3281 | /* |
d07ceecd MD |
3282 | * Calculate the number of packets we can grab from each stream that |
3283 | * fits within the overall snapshot max size. | |
3284 | * | |
3285 | * Returns -1 on error, 0 means infinite number of packets, else > 0 is | |
3286 | * the number of packets per stream. | |
3287 | * | |
3288 | * TODO: this approach is not perfect: we consider the worse case | |
3289 | * (packet filling the sub-buffers) as an upper bound, but we could do | |
3290 | * better if we do this calculation while we actually grab the packet | |
3291 | * content: we would know how much padding we don't actually store into | |
3292 | * the file. | |
3293 | * | |
3294 | * This algorithm is currently bounded by the number of packets per | |
3295 | * stream. | |
3296 | * | |
3297 | * Since we call this algorithm before actually grabbing the data, it's | |
3298 | * an approximation: for instance, applications could appear/disappear | |
3299 | * in between this call and actually grabbing data. | |
5c786ded | 3300 | */ |
d07ceecd MD |
3301 | static |
3302 | int64_t get_session_nb_packets_per_stream(struct ltt_session *session, uint64_t max_size) | |
5c786ded | 3303 | { |
d07ceecd MD |
3304 | int64_t size_left; |
3305 | uint64_t cur_nb_packets = 0; | |
5c786ded | 3306 | |
d07ceecd MD |
3307 | if (!max_size) { |
3308 | return 0; /* Infinite */ | |
5c786ded JD |
3309 | } |
3310 | ||
d07ceecd MD |
3311 | size_left = max_size; |
3312 | for (;;) { | |
3313 | uint64_t one_more_packet_tot_size; | |
5c786ded | 3314 | |
d07ceecd MD |
3315 | one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(session, |
3316 | cur_nb_packets); | |
3317 | if (!one_more_packet_tot_size) { | |
3318 | /* We are already grabbing all packets. */ | |
3319 | break; | |
3320 | } | |
3321 | size_left -= one_more_packet_tot_size; | |
3322 | if (size_left < 0) { | |
3323 | break; | |
3324 | } | |
3325 | cur_nb_packets++; | |
5c786ded | 3326 | } |
d07ceecd MD |
3327 | if (!cur_nb_packets) { |
3328 | /* Not enough room to grab one packet of each stream, error. */ | |
3329 | return -1; | |
3330 | } | |
3331 | return cur_nb_packets; | |
5c786ded JD |
3332 | } |
3333 | ||
6dc3064a DG |
3334 | /* |
3335 | * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl. | |
3336 | * | |
3337 | * The wait parameter is ignored so this call always wait for the snapshot to | |
3338 | * complete before returning. | |
3339 | * | |
3340 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
3341 | */ | |
3342 | int cmd_snapshot_record(struct ltt_session *session, | |
3343 | struct lttng_snapshot_output *output, int wait) | |
3344 | { | |
3345 | int ret = LTTNG_OK; | |
e1986656 DG |
3346 | unsigned int use_tmp_output = 0; |
3347 | struct snapshot_output tmp_output; | |
00e1dfc4 | 3348 | unsigned int snapshot_success = 0; |
6dc3064a DG |
3349 | |
3350 | assert(session); | |
ba45d9f0 | 3351 | assert(output); |
6dc3064a DG |
3352 | |
3353 | DBG("Cmd snapshot record for session %s", session->name); | |
3354 | ||
3355 | /* | |
d3f14b8a MD |
3356 | * Permission denied to create an output if the session is not |
3357 | * set in no output mode. | |
6dc3064a DG |
3358 | */ |
3359 | if (session->output_traces) { | |
3360 | ret = LTTNG_ERR_EPERM; | |
3361 | goto error; | |
3362 | } | |
3363 | ||
3364 | /* The session needs to be started at least once. */ | |
8382cf6f | 3365 | if (!session->has_been_started) { |
6dc3064a DG |
3366 | ret = LTTNG_ERR_START_SESSION_ONCE; |
3367 | goto error; | |
3368 | } | |
3369 | ||
3370 | /* Use temporary output for the session. */ | |
ba45d9f0 | 3371 | if (*output->ctrl_url != '\0') { |
6dc3064a DG |
3372 | ret = snapshot_output_init(output->max_size, output->name, |
3373 | output->ctrl_url, output->data_url, session->consumer, | |
e1986656 | 3374 | &tmp_output, NULL); |
6dc3064a DG |
3375 | if (ret < 0) { |
3376 | if (ret == -ENOMEM) { | |
3377 | ret = LTTNG_ERR_NOMEM; | |
3378 | } else { | |
3379 | ret = LTTNG_ERR_INVALID; | |
3380 | } | |
3381 | goto error; | |
3382 | } | |
1bfe7328 DG |
3383 | /* Use the global session count for the temporary snapshot. */ |
3384 | tmp_output.nb_snapshot = session->snapshot.nb_snapshot; | |
e1986656 | 3385 | use_tmp_output = 1; |
6dc3064a DG |
3386 | } |
3387 | ||
3388 | if (session->kernel_session) { | |
3389 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3390 | ||
e1986656 | 3391 | if (use_tmp_output) { |
d07ceecd MD |
3392 | int64_t nb_packets_per_stream; |
3393 | ||
3394 | nb_packets_per_stream = get_session_nb_packets_per_stream(session, | |
3395 | tmp_output.max_size); | |
3396 | if (nb_packets_per_stream < 0) { | |
3397 | ret = LTTNG_ERR_MAX_SIZE_INVALID; | |
3398 | goto error; | |
3399 | } | |
e1986656 | 3400 | ret = record_kernel_snapshot(ksess, &tmp_output, session, |
d07ceecd | 3401 | wait, nb_packets_per_stream); |
a934f4af | 3402 | if (ret != LTTNG_OK) { |
6dc3064a DG |
3403 | goto error; |
3404 | } | |
1bfe7328 | 3405 | snapshot_success = 1; |
6dc3064a DG |
3406 | } else { |
3407 | struct snapshot_output *sout; | |
3408 | struct lttng_ht_iter iter; | |
3409 | ||
3410 | rcu_read_lock(); | |
3411 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, | |
3412 | &iter.iter, sout, node.node) { | |
d07ceecd MD |
3413 | int64_t nb_packets_per_stream; |
3414 | ||
e1986656 DG |
3415 | /* |
3416 | * Make a local copy of the output and assign the possible | |
3417 | * temporary value given by the caller. | |
3418 | */ | |
3419 | memset(&tmp_output, 0, sizeof(tmp_output)); | |
3420 | memcpy(&tmp_output, sout, sizeof(tmp_output)); | |
3421 | ||
e1986656 DG |
3422 | if (output->max_size != (uint64_t) -1ULL) { |
3423 | tmp_output.max_size = output->max_size; | |
3424 | } | |
3425 | ||
d07ceecd MD |
3426 | nb_packets_per_stream = get_session_nb_packets_per_stream(session, |
3427 | tmp_output.max_size); | |
3428 | if (nb_packets_per_stream < 0) { | |
68808f4e DG |
3429 | ret = LTTNG_ERR_MAX_SIZE_INVALID; |
3430 | goto error; | |
3431 | } | |
3432 | ||
e1986656 DG |
3433 | /* Use temporary name. */ |
3434 | if (*output->name != '\0') { | |
3435 | strncpy(tmp_output.name, output->name, | |
3436 | sizeof(tmp_output.name)); | |
3437 | } | |
3438 | ||
1bfe7328 DG |
3439 | tmp_output.nb_snapshot = session->snapshot.nb_snapshot; |
3440 | ||
e1986656 | 3441 | ret = record_kernel_snapshot(ksess, &tmp_output, |
d07ceecd | 3442 | session, wait, nb_packets_per_stream); |
a934f4af | 3443 | if (ret != LTTNG_OK) { |
6dc3064a DG |
3444 | rcu_read_unlock(); |
3445 | goto error; | |
3446 | } | |
1bfe7328 | 3447 | snapshot_success = 1; |
6dc3064a DG |
3448 | } |
3449 | rcu_read_unlock(); | |
3450 | } | |
3451 | } | |
3452 | ||
3453 | if (session->ust_session) { | |
3454 | struct ltt_ust_session *usess = session->ust_session; | |
3455 | ||
e1986656 | 3456 | if (use_tmp_output) { |
d07ceecd MD |
3457 | int64_t nb_packets_per_stream; |
3458 | ||
3459 | nb_packets_per_stream = get_session_nb_packets_per_stream(session, | |
3460 | tmp_output.max_size); | |
3461 | if (nb_packets_per_stream < 0) { | |
3462 | ret = LTTNG_ERR_MAX_SIZE_INVALID; | |
3463 | goto error; | |
3464 | } | |
e1986656 | 3465 | ret = record_ust_snapshot(usess, &tmp_output, session, |
d07ceecd | 3466 | wait, nb_packets_per_stream); |
a934f4af | 3467 | if (ret != LTTNG_OK) { |
6dc3064a DG |
3468 | goto error; |
3469 | } | |
1bfe7328 | 3470 | snapshot_success = 1; |
6dc3064a DG |
3471 | } else { |
3472 | struct snapshot_output *sout; | |
3473 | struct lttng_ht_iter iter; | |
3474 | ||
3475 | rcu_read_lock(); | |
3476 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, | |
3477 | &iter.iter, sout, node.node) { | |
d07ceecd MD |
3478 | int64_t nb_packets_per_stream; |
3479 | ||
e1986656 DG |
3480 | /* |
3481 | * Make a local copy of the output and assign the possible | |
3482 | * temporary value given by the caller. | |
3483 | */ | |
3484 | memset(&tmp_output, 0, sizeof(tmp_output)); | |
3485 | memcpy(&tmp_output, sout, sizeof(tmp_output)); | |
3486 | ||
e1986656 DG |
3487 | if (output->max_size != (uint64_t) -1ULL) { |
3488 | tmp_output.max_size = output->max_size; | |
3489 | } | |
3490 | ||
d07ceecd MD |
3491 | nb_packets_per_stream = get_session_nb_packets_per_stream(session, |
3492 | tmp_output.max_size); | |
3493 | if (nb_packets_per_stream < 0) { | |
68808f4e | 3494 | ret = LTTNG_ERR_MAX_SIZE_INVALID; |
d07ceecd | 3495 | rcu_read_unlock(); |
68808f4e DG |
3496 | goto error; |
3497 | } | |
3498 | ||
e1986656 DG |
3499 | /* Use temporary name. */ |
3500 | if (*output->name != '\0') { | |
3501 | strncpy(tmp_output.name, output->name, | |
3502 | sizeof(tmp_output.name)); | |
3503 | } | |
3504 | ||
1bfe7328 DG |
3505 | tmp_output.nb_snapshot = session->snapshot.nb_snapshot; |
3506 | ||
e1986656 | 3507 | ret = record_ust_snapshot(usess, &tmp_output, session, |
d07ceecd | 3508 | wait, nb_packets_per_stream); |
a934f4af | 3509 | if (ret != LTTNG_OK) { |
6dc3064a DG |
3510 | rcu_read_unlock(); |
3511 | goto error; | |
3512 | } | |
1bfe7328 | 3513 | snapshot_success = 1; |
6dc3064a DG |
3514 | } |
3515 | rcu_read_unlock(); | |
3516 | } | |
3517 | } | |
3518 | ||
1bfe7328 DG |
3519 | if (snapshot_success) { |
3520 | session->snapshot.nb_snapshot++; | |
b67578cb MD |
3521 | } else { |
3522 | ret = LTTNG_ERR_SNAPSHOT_FAIL; | |
1bfe7328 DG |
3523 | } |
3524 | ||
6dc3064a | 3525 | error: |
6dc3064a DG |
3526 | return ret; |
3527 | } | |
3528 | ||
d7ba1388 MD |
3529 | /* |
3530 | * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread. | |
3531 | */ | |
3532 | int cmd_set_session_shm_path(struct ltt_session *session, | |
3533 | const char *shm_path) | |
3534 | { | |
3535 | /* Safety net */ | |
3536 | assert(session); | |
3537 | ||
3538 | /* | |
3539 | * Can only set shm path before session is started. | |
3540 | */ | |
3541 | if (session->has_been_started) { | |
3542 | return LTTNG_ERR_SESSION_STARTED; | |
3543 | } | |
3544 | ||
3545 | strncpy(session->shm_path, shm_path, | |
3546 | sizeof(session->shm_path)); | |
3547 | session->shm_path[sizeof(session->shm_path) - 1] = '\0'; | |
3548 | ||
3549 | return 0; | |
3550 | } | |
3551 | ||
2f77fc4b DG |
3552 | /* |
3553 | * Init command subsystem. | |
3554 | */ | |
3555 | void cmd_init(void) | |
3556 | { | |
3557 | /* | |
d88aee68 DG |
3558 | * Set network sequence index to 1 for streams to match a relayd |
3559 | * socket on the consumer side. | |
2f77fc4b | 3560 | */ |
d88aee68 DG |
3561 | pthread_mutex_lock(&relayd_net_seq_idx_lock); |
3562 | relayd_net_seq_idx = 1; | |
3563 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
3564 | |
3565 | DBG("Command subsystem initialized"); | |
3566 | } |