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 | ||
18 | #define _GNU_SOURCE | |
19 | #include <assert.h> | |
20 | #include <urcu/list.h> | |
21 | #include <urcu/uatomic.h> | |
22 | ||
23 | #include <common/defaults.h> | |
24 | #include <common/common.h> | |
25 | #include <common/sessiond-comm/sessiond-comm.h> | |
26 | #include <common/relayd/relayd.h> | |
27 | ||
28 | #include "channel.h" | |
29 | #include "consumer.h" | |
30 | #include "event.h" | |
31 | #include "kernel.h" | |
32 | #include "kernel-consumer.h" | |
33 | #include "lttng-sessiond.h" | |
34 | #include "utils.h" | |
35 | ||
36 | #include "cmd.h" | |
37 | ||
38 | /* | |
39 | * Used to keep a unique index for each relayd socket created where this value | |
40 | * is associated with streams on the consumer so it can match the right relayd | |
41 | * to send to. | |
42 | * | |
43 | * This value should be incremented atomically for safety purposes and future | |
44 | * possible concurrent access. | |
45 | */ | |
46 | static unsigned int relayd_net_seq_idx; | |
47 | ||
48 | /* | |
49 | * Create a session path used by list_lttng_sessions for the case that the | |
50 | * session consumer is on the network. | |
51 | */ | |
52 | static int build_network_session_path(char *dst, size_t size, | |
53 | struct ltt_session *session) | |
54 | { | |
55 | int ret, kdata_port, udata_port; | |
56 | struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL; | |
57 | char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX]; | |
58 | ||
59 | assert(session); | |
60 | assert(dst); | |
61 | ||
62 | memset(tmp_urls, 0, sizeof(tmp_urls)); | |
63 | memset(tmp_uurl, 0, sizeof(tmp_uurl)); | |
64 | ||
65 | kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT; | |
66 | ||
67 | if (session->kernel_session && session->kernel_session->consumer) { | |
68 | kuri = &session->kernel_session->consumer->dst.net.control; | |
69 | kdata_port = session->kernel_session->consumer->dst.net.data.port; | |
70 | } | |
71 | ||
72 | if (session->ust_session && session->ust_session->consumer) { | |
73 | uuri = &session->ust_session->consumer->dst.net.control; | |
74 | udata_port = session->ust_session->consumer->dst.net.data.port; | |
75 | } | |
76 | ||
77 | if (uuri == NULL && kuri == NULL) { | |
78 | uri = &session->consumer->dst.net.control; | |
79 | kdata_port = session->consumer->dst.net.data.port; | |
80 | } else if (kuri && uuri) { | |
81 | ret = uri_compare(kuri, uuri); | |
82 | if (ret) { | |
83 | /* Not Equal */ | |
84 | uri = kuri; | |
85 | /* Build uuri URL string */ | |
86 | ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl)); | |
87 | if (ret < 0) { | |
88 | goto error; | |
89 | } | |
90 | } else { | |
91 | uri = kuri; | |
92 | } | |
93 | } else if (kuri && uuri == NULL) { | |
94 | uri = kuri; | |
95 | } else if (uuri && kuri == NULL) { | |
96 | uri = uuri; | |
97 | } | |
98 | ||
99 | ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls)); | |
100 | if (ret < 0) { | |
101 | goto error; | |
102 | } | |
103 | ||
9aa9f900 DG |
104 | /* |
105 | * Do we have a UST url set. If yes, this means we have both kernel and UST | |
106 | * to print. | |
107 | */ | |
2f77fc4b DG |
108 | if (strlen(tmp_uurl) > 0) { |
109 | ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]", | |
110 | tmp_urls, kdata_port, tmp_uurl, udata_port); | |
111 | } else { | |
9aa9f900 DG |
112 | int dport; |
113 | if (kuri) { | |
114 | dport = kdata_port; | |
115 | } else { | |
116 | /* No kernel URI, use the UST port. */ | |
117 | dport = udata_port; | |
118 | } | |
119 | ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport); | |
2f77fc4b DG |
120 | } |
121 | ||
122 | error: | |
123 | return ret; | |
124 | } | |
125 | ||
126 | /* | |
127 | * Fill lttng_channel array of all channels. | |
128 | */ | |
129 | static void list_lttng_channels(int domain, struct ltt_session *session, | |
130 | struct lttng_channel *channels) | |
131 | { | |
132 | int i = 0; | |
133 | struct ltt_kernel_channel *kchan; | |
134 | ||
135 | DBG("Listing channels for session %s", session->name); | |
136 | ||
137 | switch (domain) { | |
138 | case LTTNG_DOMAIN_KERNEL: | |
139 | /* Kernel channels */ | |
140 | if (session->kernel_session != NULL) { | |
141 | cds_list_for_each_entry(kchan, | |
142 | &session->kernel_session->channel_list.head, list) { | |
143 | /* Copy lttng_channel struct to array */ | |
144 | memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel)); | |
145 | channels[i].enabled = kchan->enabled; | |
146 | i++; | |
147 | } | |
148 | } | |
149 | break; | |
150 | case LTTNG_DOMAIN_UST: | |
151 | { | |
152 | struct lttng_ht_iter iter; | |
153 | struct ltt_ust_channel *uchan; | |
154 | ||
2219ef35 | 155 | rcu_read_lock(); |
2f77fc4b DG |
156 | cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht, |
157 | &iter.iter, uchan, node.node) { | |
158 | strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN); | |
159 | channels[i].attr.overwrite = uchan->attr.overwrite; | |
160 | channels[i].attr.subbuf_size = uchan->attr.subbuf_size; | |
161 | channels[i].attr.num_subbuf = uchan->attr.num_subbuf; | |
162 | channels[i].attr.switch_timer_interval = | |
163 | uchan->attr.switch_timer_interval; | |
164 | channels[i].attr.read_timer_interval = | |
165 | uchan->attr.read_timer_interval; | |
166 | channels[i].enabled = uchan->enabled; | |
167 | switch (uchan->attr.output) { | |
168 | case LTTNG_UST_MMAP: | |
169 | default: | |
170 | channels[i].attr.output = LTTNG_EVENT_MMAP; | |
171 | break; | |
172 | } | |
173 | i++; | |
174 | } | |
2219ef35 | 175 | rcu_read_unlock(); |
2f77fc4b DG |
176 | break; |
177 | } | |
178 | default: | |
179 | break; | |
180 | } | |
181 | } | |
182 | ||
183 | /* | |
184 | * Create a list of ust global domain events. | |
185 | */ | |
186 | static int list_lttng_ust_global_events(char *channel_name, | |
187 | struct ltt_ust_domain_global *ust_global, struct lttng_event **events) | |
188 | { | |
189 | int i = 0, ret = 0; | |
190 | unsigned int nb_event = 0; | |
191 | struct lttng_ht_iter iter; | |
192 | struct lttng_ht_node_str *node; | |
193 | struct ltt_ust_channel *uchan; | |
194 | struct ltt_ust_event *uevent; | |
195 | struct lttng_event *tmp; | |
196 | ||
197 | DBG("Listing UST global events for channel %s", channel_name); | |
198 | ||
199 | rcu_read_lock(); | |
200 | ||
201 | lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter); | |
202 | node = lttng_ht_iter_get_node_str(&iter); | |
203 | if (node == NULL) { | |
f73fabfd | 204 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2f77fc4b DG |
205 | goto error; |
206 | } | |
207 | ||
208 | uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node); | |
209 | ||
210 | nb_event += lttng_ht_get_count(uchan->events); | |
211 | ||
212 | if (nb_event == 0) { | |
213 | ret = nb_event; | |
214 | goto error; | |
215 | } | |
216 | ||
217 | DBG3("Listing UST global %d events", nb_event); | |
218 | ||
219 | tmp = zmalloc(nb_event * sizeof(struct lttng_event)); | |
220 | if (tmp == NULL) { | |
f73fabfd | 221 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
222 | goto error; |
223 | } | |
224 | ||
225 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { | |
226 | strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN); | |
227 | tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
228 | tmp[i].enabled = uevent->enabled; | |
229 | ||
230 | switch (uevent->attr.instrumentation) { | |
231 | case LTTNG_UST_TRACEPOINT: | |
232 | tmp[i].type = LTTNG_EVENT_TRACEPOINT; | |
233 | break; | |
234 | case LTTNG_UST_PROBE: | |
235 | tmp[i].type = LTTNG_EVENT_PROBE; | |
236 | break; | |
237 | case LTTNG_UST_FUNCTION: | |
238 | tmp[i].type = LTTNG_EVENT_FUNCTION; | |
239 | break; | |
240 | } | |
241 | ||
242 | tmp[i].loglevel = uevent->attr.loglevel; | |
243 | switch (uevent->attr.loglevel_type) { | |
244 | case LTTNG_UST_LOGLEVEL_ALL: | |
245 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
246 | break; | |
247 | case LTTNG_UST_LOGLEVEL_RANGE: | |
248 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; | |
249 | break; | |
250 | case LTTNG_UST_LOGLEVEL_SINGLE: | |
251 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; | |
252 | break; | |
253 | } | |
254 | if (uevent->filter) { | |
255 | tmp[i].filter = 1; | |
256 | } | |
257 | i++; | |
258 | } | |
259 | ||
260 | ret = nb_event; | |
261 | *events = tmp; | |
262 | ||
263 | error: | |
264 | rcu_read_unlock(); | |
265 | return ret; | |
266 | } | |
267 | ||
268 | /* | |
269 | * Fill lttng_event array of all kernel events in the channel. | |
270 | */ | |
271 | static int list_lttng_kernel_events(char *channel_name, | |
272 | struct ltt_kernel_session *kernel_session, struct lttng_event **events) | |
273 | { | |
274 | int i = 0, ret; | |
275 | unsigned int nb_event; | |
276 | struct ltt_kernel_event *event; | |
277 | struct ltt_kernel_channel *kchan; | |
278 | ||
279 | kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session); | |
280 | if (kchan == NULL) { | |
f73fabfd | 281 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
2f77fc4b DG |
282 | goto error; |
283 | } | |
284 | ||
285 | nb_event = kchan->event_count; | |
286 | ||
287 | DBG("Listing events for channel %s", kchan->channel->name); | |
288 | ||
289 | if (nb_event == 0) { | |
f73fabfd | 290 | goto end; |
2f77fc4b DG |
291 | } |
292 | ||
293 | *events = zmalloc(nb_event * sizeof(struct lttng_event)); | |
294 | if (*events == NULL) { | |
f73fabfd | 295 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
296 | goto error; |
297 | } | |
298 | ||
299 | /* Kernel channels */ | |
300 | cds_list_for_each_entry(event, &kchan->events_list.head , list) { | |
301 | strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN); | |
302 | (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
303 | (*events)[i].enabled = event->enabled; | |
304 | ||
305 | switch (event->event->instrumentation) { | |
306 | case LTTNG_KERNEL_TRACEPOINT: | |
307 | (*events)[i].type = LTTNG_EVENT_TRACEPOINT; | |
308 | break; | |
309 | case LTTNG_KERNEL_KPROBE: | |
310 | case LTTNG_KERNEL_KRETPROBE: | |
311 | (*events)[i].type = LTTNG_EVENT_PROBE; | |
312 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, | |
313 | sizeof(struct lttng_kernel_kprobe)); | |
314 | break; | |
315 | case LTTNG_KERNEL_FUNCTION: | |
316 | (*events)[i].type = LTTNG_EVENT_FUNCTION; | |
317 | memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace, | |
318 | sizeof(struct lttng_kernel_function)); | |
319 | break; | |
320 | case LTTNG_KERNEL_NOOP: | |
321 | (*events)[i].type = LTTNG_EVENT_NOOP; | |
322 | break; | |
323 | case LTTNG_KERNEL_SYSCALL: | |
324 | (*events)[i].type = LTTNG_EVENT_SYSCALL; | |
325 | break; | |
326 | case LTTNG_KERNEL_ALL: | |
327 | assert(0); | |
328 | break; | |
329 | } | |
330 | i++; | |
331 | } | |
332 | ||
f73fabfd | 333 | end: |
2f77fc4b DG |
334 | return nb_event; |
335 | ||
336 | error: | |
f73fabfd DG |
337 | /* Negate the error code to differentiate the size from an error */ |
338 | return -ret; | |
2f77fc4b DG |
339 | } |
340 | ||
341 | /* | |
342 | * Add URI so the consumer output object. Set the correct path depending on the | |
343 | * domain adding the default trace directory. | |
344 | */ | |
345 | static int add_uri_to_consumer(struct consumer_output *consumer, | |
346 | struct lttng_uri *uri, int domain, const char *session_name) | |
347 | { | |
f73fabfd | 348 | int ret = LTTNG_OK; |
2f77fc4b DG |
349 | const char *default_trace_dir; |
350 | ||
351 | assert(uri); | |
352 | ||
353 | if (consumer == NULL) { | |
354 | DBG("No consumer detected. Don't add URI. Stopping."); | |
f73fabfd | 355 | ret = LTTNG_ERR_NO_CONSUMER; |
2f77fc4b DG |
356 | goto error; |
357 | } | |
358 | ||
359 | switch (domain) { | |
360 | case LTTNG_DOMAIN_KERNEL: | |
361 | default_trace_dir = DEFAULT_KERNEL_TRACE_DIR; | |
362 | break; | |
363 | case LTTNG_DOMAIN_UST: | |
364 | default_trace_dir = DEFAULT_UST_TRACE_DIR; | |
365 | break; | |
366 | default: | |
367 | /* | |
368 | * This case is possible is we try to add the URI to the global tracing | |
369 | * session consumer object which in this case there is no subdir. | |
370 | */ | |
371 | default_trace_dir = ""; | |
372 | } | |
373 | ||
374 | switch (uri->dtype) { | |
375 | case LTTNG_DST_IPV4: | |
376 | case LTTNG_DST_IPV6: | |
377 | DBG2("Setting network URI to consumer"); | |
378 | ||
379 | /* Set URI into consumer output object */ | |
380 | ret = consumer_set_network_uri(consumer, uri); | |
381 | if (ret < 0) { | |
f73fabfd | 382 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
383 | goto error; |
384 | } else if (ret == 1) { | |
385 | /* | |
386 | * URI was the same in the consumer so we do not append the subdir | |
387 | * again so to not duplicate output dir. | |
388 | */ | |
389 | goto error; | |
390 | } | |
391 | ||
392 | if (uri->stype == LTTNG_STREAM_CONTROL && strlen(uri->subdir) == 0) { | |
393 | ret = consumer_set_subdir(consumer, session_name); | |
394 | if (ret < 0) { | |
f73fabfd | 395 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
396 | goto error; |
397 | } | |
398 | } | |
399 | ||
400 | if (uri->stype == LTTNG_STREAM_CONTROL) { | |
401 | /* On a new subdir, reappend the default trace dir. */ | |
c30ce0b3 CB |
402 | strncat(consumer->subdir, default_trace_dir, |
403 | sizeof(consumer->subdir) - strlen(consumer->subdir) - 1); | |
2f77fc4b DG |
404 | DBG3("Append domain trace name to subdir %s", consumer->subdir); |
405 | } | |
406 | ||
407 | break; | |
408 | case LTTNG_DST_PATH: | |
409 | DBG2("Setting trace directory path from URI to %s", uri->dst.path); | |
410 | memset(consumer->dst.trace_path, 0, | |
411 | sizeof(consumer->dst.trace_path)); | |
412 | strncpy(consumer->dst.trace_path, uri->dst.path, | |
413 | sizeof(consumer->dst.trace_path)); | |
414 | /* Append default trace dir */ | |
415 | strncat(consumer->dst.trace_path, default_trace_dir, | |
c30ce0b3 CB |
416 | sizeof(consumer->dst.trace_path) - |
417 | strlen(consumer->dst.trace_path) - 1); | |
2f77fc4b DG |
418 | /* Flag consumer as local. */ |
419 | consumer->type = CONSUMER_DST_LOCAL; | |
420 | break; | |
421 | } | |
422 | ||
423 | error: | |
424 | return ret; | |
425 | } | |
426 | ||
427 | /* | |
428 | * Init tracing by creating trace directory and sending fds kernel consumer. | |
429 | */ | |
430 | static int init_kernel_tracing(struct ltt_kernel_session *session) | |
431 | { | |
432 | int ret = 0; | |
433 | struct lttng_ht_iter iter; | |
434 | struct consumer_socket *socket; | |
435 | ||
436 | assert(session); | |
437 | ||
2219ef35 DG |
438 | rcu_read_lock(); |
439 | ||
2f77fc4b DG |
440 | if (session->consumer_fds_sent == 0 && session->consumer != NULL) { |
441 | cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter, | |
442 | socket, node.node) { | |
443 | /* Code flow error */ | |
444 | assert(socket->fd >= 0); | |
445 | ||
446 | pthread_mutex_lock(socket->lock); | |
f50f23d9 | 447 | ret = kernel_consumer_send_session(socket, session); |
2f77fc4b DG |
448 | pthread_mutex_unlock(socket->lock); |
449 | if (ret < 0) { | |
f73fabfd | 450 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
2f77fc4b DG |
451 | goto error; |
452 | } | |
453 | } | |
454 | } | |
455 | ||
456 | error: | |
2219ef35 | 457 | rcu_read_unlock(); |
2f77fc4b DG |
458 | return ret; |
459 | } | |
460 | ||
461 | /* | |
462 | * Create a socket to the relayd using the URI. | |
463 | * | |
464 | * On success, the relayd_sock pointer is set to the created socket. | |
465 | * Else, it's stays untouched and a lttcomm error code is returned. | |
466 | */ | |
467 | static int create_connect_relayd(struct consumer_output *output, | |
468 | const char *session_name, struct lttng_uri *uri, | |
469 | struct lttcomm_sock **relayd_sock) | |
470 | { | |
471 | int ret; | |
472 | struct lttcomm_sock *sock; | |
473 | ||
474 | /* Create socket object from URI */ | |
475 | sock = lttcomm_alloc_sock_from_uri(uri); | |
476 | if (sock == NULL) { | |
f73fabfd | 477 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
478 | goto error; |
479 | } | |
480 | ||
481 | ret = lttcomm_create_sock(sock); | |
482 | if (ret < 0) { | |
f73fabfd | 483 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
484 | goto error; |
485 | } | |
486 | ||
487 | /* Connect to relayd so we can proceed with a session creation. */ | |
4db7b5a5 | 488 | health_poll_entry(); |
2f77fc4b | 489 | ret = relayd_connect(sock); |
4db7b5a5 | 490 | health_poll_exit(); |
2f77fc4b DG |
491 | if (ret < 0) { |
492 | ERR("Unable to reach lttng-relayd"); | |
f73fabfd | 493 | ret = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
2f77fc4b DG |
494 | goto free_sock; |
495 | } | |
496 | ||
497 | /* Create socket for control stream. */ | |
498 | if (uri->stype == LTTNG_STREAM_CONTROL) { | |
499 | DBG3("Creating relayd stream socket from URI"); | |
500 | ||
501 | /* Check relayd version */ | |
502 | ret = relayd_version_check(sock, RELAYD_VERSION_COMM_MAJOR, | |
503 | RELAYD_VERSION_COMM_MINOR); | |
504 | if (ret < 0) { | |
f73fabfd | 505 | ret = LTTNG_ERR_RELAYD_VERSION_FAIL; |
2f77fc4b DG |
506 | goto close_sock; |
507 | } | |
508 | } else if (uri->stype == LTTNG_STREAM_DATA) { | |
509 | DBG3("Creating relayd data socket from URI"); | |
510 | } else { | |
511 | /* Command is not valid */ | |
512 | ERR("Relayd invalid stream type: %d", uri->stype); | |
f73fabfd | 513 | ret = LTTNG_ERR_INVALID; |
2f77fc4b DG |
514 | goto close_sock; |
515 | } | |
516 | ||
517 | *relayd_sock = sock; | |
518 | ||
f73fabfd | 519 | return LTTNG_OK; |
2f77fc4b DG |
520 | |
521 | close_sock: | |
522 | if (sock) { | |
523 | (void) relayd_close(sock); | |
524 | } | |
525 | free_sock: | |
526 | if (sock) { | |
527 | lttcomm_destroy_sock(sock); | |
528 | } | |
529 | error: | |
530 | return ret; | |
531 | } | |
532 | ||
533 | /* | |
534 | * Connect to the relayd using URI and send the socket to the right consumer. | |
535 | */ | |
536 | static int send_consumer_relayd_socket(int domain, struct ltt_session *session, | |
537 | struct lttng_uri *relayd_uri, struct consumer_output *consumer, | |
f50f23d9 | 538 | struct consumer_socket *consumer_sock) |
2f77fc4b DG |
539 | { |
540 | int ret; | |
541 | struct lttcomm_sock *sock = NULL; | |
542 | ||
543 | /* Set the network sequence index if not set. */ | |
544 | if (consumer->net_seq_index == -1) { | |
545 | /* | |
546 | * Increment net_seq_idx because we are about to transfer the | |
547 | * new relayd socket to the consumer. | |
548 | */ | |
549 | uatomic_inc(&relayd_net_seq_idx); | |
550 | /* Assign unique key so the consumer can match streams */ | |
551 | uatomic_set(&consumer->net_seq_index, | |
552 | uatomic_read(&relayd_net_seq_idx)); | |
553 | } | |
554 | ||
555 | /* Connect to relayd and make version check if uri is the control. */ | |
556 | ret = create_connect_relayd(consumer, session->name, relayd_uri, &sock); | |
f73fabfd | 557 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
558 | goto close_sock; |
559 | } | |
560 | ||
561 | /* If the control socket is connected, network session is ready */ | |
562 | if (relayd_uri->stype == LTTNG_STREAM_CONTROL) { | |
563 | session->net_handle = 1; | |
564 | } | |
565 | ||
566 | /* Send relayd socket to consumer. */ | |
f50f23d9 | 567 | ret = consumer_send_relayd_socket(consumer_sock, sock, |
46e6455f | 568 | consumer, relayd_uri->stype, session->id); |
2f77fc4b | 569 | if (ret < 0) { |
f73fabfd | 570 | ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL; |
2f77fc4b DG |
571 | goto close_sock; |
572 | } | |
573 | ||
c890b720 DG |
574 | /* Flag that the corresponding socket was sent. */ |
575 | if (relayd_uri->stype == LTTNG_STREAM_CONTROL) { | |
fd5a50b0 | 576 | consumer_sock->control_sock_sent = 1; |
c890b720 | 577 | } else if (relayd_uri->stype == LTTNG_STREAM_DATA) { |
fd5a50b0 | 578 | consumer_sock->data_sock_sent = 1; |
c890b720 DG |
579 | } |
580 | ||
f73fabfd | 581 | ret = LTTNG_OK; |
2f77fc4b DG |
582 | |
583 | /* | |
584 | * Close socket which was dup on the consumer side. The session daemon does | |
585 | * NOT keep track of the relayd socket(s) once transfer to the consumer. | |
586 | */ | |
587 | ||
588 | close_sock: | |
589 | if (sock) { | |
590 | (void) relayd_close(sock); | |
591 | lttcomm_destroy_sock(sock); | |
592 | } | |
593 | ||
594 | return ret; | |
595 | } | |
596 | ||
597 | /* | |
598 | * Send both relayd sockets to a specific consumer and domain. This is a | |
599 | * helper function to facilitate sending the information to the consumer for a | |
600 | * session. | |
601 | */ | |
602 | static int send_consumer_relayd_sockets(int domain, | |
f50f23d9 DG |
603 | struct ltt_session *session, struct consumer_output *consumer, |
604 | struct consumer_socket *sock) | |
2f77fc4b | 605 | { |
dda67f6c | 606 | int ret = LTTNG_OK; |
2f77fc4b DG |
607 | |
608 | assert(session); | |
609 | assert(consumer); | |
610 | ||
2f77fc4b | 611 | /* Sending control relayd socket. */ |
fd5a50b0 | 612 | if (!sock->control_sock_sent) { |
c890b720 | 613 | ret = send_consumer_relayd_socket(domain, session, |
f50f23d9 | 614 | &consumer->dst.net.control, consumer, sock); |
c890b720 DG |
615 | if (ret != LTTNG_OK) { |
616 | goto error; | |
617 | } | |
2f77fc4b DG |
618 | } |
619 | ||
620 | /* Sending data relayd socket. */ | |
fd5a50b0 | 621 | if (!sock->data_sock_sent) { |
c890b720 | 622 | ret = send_consumer_relayd_socket(domain, session, |
f50f23d9 | 623 | &consumer->dst.net.data, consumer, sock); |
c890b720 DG |
624 | if (ret != LTTNG_OK) { |
625 | goto error; | |
626 | } | |
2f77fc4b DG |
627 | } |
628 | ||
2f77fc4b DG |
629 | error: |
630 | return ret; | |
631 | } | |
632 | ||
633 | /* | |
634 | * Setup relayd connections for a tracing session. First creates the socket to | |
635 | * the relayd and send them to the right domain consumer. Consumer type MUST be | |
636 | * network. | |
637 | */ | |
638 | static int setup_relayd(struct ltt_session *session) | |
639 | { | |
f73fabfd | 640 | int ret = LTTNG_OK; |
2f77fc4b DG |
641 | struct ltt_ust_session *usess; |
642 | struct ltt_kernel_session *ksess; | |
643 | struct consumer_socket *socket; | |
644 | struct lttng_ht_iter iter; | |
645 | ||
646 | assert(session); | |
647 | ||
648 | usess = session->ust_session; | |
649 | ksess = session->kernel_session; | |
650 | ||
651 | DBG2("Setting relayd for session %s", session->name); | |
652 | ||
2219ef35 DG |
653 | rcu_read_lock(); |
654 | ||
2f77fc4b DG |
655 | if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET |
656 | && usess->consumer->enabled) { | |
657 | /* For each consumer socket, send relayd sockets */ | |
658 | cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter, | |
659 | socket, node.node) { | |
660 | /* Code flow error */ | |
661 | assert(socket->fd >= 0); | |
662 | ||
663 | pthread_mutex_lock(socket->lock); | |
dda67f6c | 664 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session, |
f50f23d9 | 665 | usess->consumer, socket); |
2f77fc4b | 666 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 667 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
668 | goto error; |
669 | } | |
670 | } | |
671 | } | |
672 | ||
673 | if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET | |
674 | && ksess->consumer->enabled) { | |
675 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, | |
676 | socket, node.node) { | |
677 | /* Code flow error */ | |
678 | assert(socket->fd >= 0); | |
679 | ||
680 | pthread_mutex_lock(socket->lock); | |
dda67f6c | 681 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session, |
f50f23d9 | 682 | ksess->consumer, socket); |
2f77fc4b | 683 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 684 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
685 | goto error; |
686 | } | |
687 | } | |
688 | } | |
689 | ||
690 | error: | |
2219ef35 | 691 | rcu_read_unlock(); |
2f77fc4b DG |
692 | return ret; |
693 | } | |
694 | ||
9b6c7ec5 DG |
695 | /* |
696 | * Start a kernel session by opening all necessary streams. | |
697 | */ | |
698 | static int start_kernel_session(struct ltt_kernel_session *ksess, int wpipe) | |
699 | { | |
700 | int ret; | |
701 | struct ltt_kernel_channel *kchan; | |
702 | ||
703 | /* Open kernel metadata */ | |
704 | if (ksess->metadata == NULL) { | |
705 | ret = kernel_open_metadata(ksess); | |
706 | if (ret < 0) { | |
707 | ret = LTTNG_ERR_KERN_META_FAIL; | |
708 | goto error; | |
709 | } | |
710 | } | |
711 | ||
712 | /* Open kernel metadata stream */ | |
713 | if (ksess->metadata_stream_fd < 0) { | |
714 | ret = kernel_open_metadata_stream(ksess); | |
715 | if (ret < 0) { | |
716 | ERR("Kernel create metadata stream failed"); | |
717 | ret = LTTNG_ERR_KERN_STREAM_FAIL; | |
718 | goto error; | |
719 | } | |
720 | } | |
721 | ||
722 | /* For each channel */ | |
723 | cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) { | |
724 | if (kchan->stream_count == 0) { | |
725 | ret = kernel_open_channel_stream(kchan); | |
726 | if (ret < 0) { | |
727 | ret = LTTNG_ERR_KERN_STREAM_FAIL; | |
728 | goto error; | |
729 | } | |
730 | /* Update the stream global counter */ | |
731 | ksess->stream_count_global += ret; | |
732 | } | |
733 | } | |
734 | ||
735 | /* Setup kernel consumer socket and send fds to it */ | |
736 | ret = init_kernel_tracing(ksess); | |
737 | if (ret < 0) { | |
738 | ret = LTTNG_ERR_KERN_START_FAIL; | |
739 | goto error; | |
740 | } | |
741 | ||
742 | /* This start the kernel tracing */ | |
743 | ret = kernel_start_session(ksess); | |
744 | if (ret < 0) { | |
745 | ret = LTTNG_ERR_KERN_START_FAIL; | |
746 | goto error; | |
747 | } | |
748 | ||
749 | /* Quiescent wait after starting trace */ | |
784303b0 | 750 | kernel_wait_quiescent(kernel_tracer_fd); |
9b6c7ec5 DG |
751 | |
752 | ksess->started = 1; | |
753 | ||
754 | ret = LTTNG_OK; | |
755 | ||
756 | error: | |
757 | return ret; | |
758 | } | |
759 | ||
2f77fc4b DG |
760 | /* |
761 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. | |
762 | */ | |
763 | int cmd_disable_channel(struct ltt_session *session, int domain, | |
764 | char *channel_name) | |
765 | { | |
766 | int ret; | |
767 | struct ltt_ust_session *usess; | |
768 | ||
769 | usess = session->ust_session; | |
770 | ||
d861c907 DG |
771 | rcu_read_lock(); |
772 | ||
2f77fc4b DG |
773 | switch (domain) { |
774 | case LTTNG_DOMAIN_KERNEL: | |
775 | { | |
776 | ret = channel_kernel_disable(session->kernel_session, | |
777 | channel_name); | |
f73fabfd | 778 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
779 | goto error; |
780 | } | |
781 | ||
782 | kernel_wait_quiescent(kernel_tracer_fd); | |
783 | break; | |
784 | } | |
785 | case LTTNG_DOMAIN_UST: | |
786 | { | |
787 | struct ltt_ust_channel *uchan; | |
788 | struct lttng_ht *chan_ht; | |
789 | ||
790 | chan_ht = usess->domain_global.channels; | |
791 | ||
792 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); | |
793 | if (uchan == NULL) { | |
f73fabfd | 794 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2f77fc4b DG |
795 | goto error; |
796 | } | |
797 | ||
798 | ret = channel_ust_disable(usess, domain, uchan); | |
f73fabfd | 799 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
800 | goto error; |
801 | } | |
802 | break; | |
803 | } | |
804 | #if 0 | |
805 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
806 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
807 | case LTTNG_DOMAIN_UST_PID: | |
808 | #endif | |
809 | default: | |
f73fabfd | 810 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
811 | goto error; |
812 | } | |
813 | ||
f73fabfd | 814 | ret = LTTNG_OK; |
2f77fc4b DG |
815 | |
816 | error: | |
d861c907 | 817 | rcu_read_unlock(); |
2f77fc4b DG |
818 | return ret; |
819 | } | |
820 | ||
821 | /* | |
822 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. | |
823 | * | |
824 | * The wpipe arguments is used as a notifier for the kernel thread. | |
825 | */ | |
826 | int cmd_enable_channel(struct ltt_session *session, | |
827 | int domain, struct lttng_channel *attr, int wpipe) | |
828 | { | |
829 | int ret; | |
830 | struct ltt_ust_session *usess = session->ust_session; | |
831 | struct lttng_ht *chan_ht; | |
832 | ||
833 | assert(session); | |
834 | assert(attr); | |
835 | ||
836 | DBG("Enabling channel %s for session %s", attr->name, session->name); | |
837 | ||
d861c907 DG |
838 | rcu_read_lock(); |
839 | ||
2f77fc4b DG |
840 | switch (domain) { |
841 | case LTTNG_DOMAIN_KERNEL: | |
842 | { | |
843 | struct ltt_kernel_channel *kchan; | |
844 | ||
2f77fc4b DG |
845 | kchan = trace_kernel_get_channel_by_name(attr->name, |
846 | session->kernel_session); | |
847 | if (kchan == NULL) { | |
848 | ret = channel_kernel_create(session->kernel_session, attr, wpipe); | |
849 | } else { | |
850 | ret = channel_kernel_enable(session->kernel_session, kchan); | |
851 | } | |
852 | ||
f73fabfd | 853 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
854 | goto error; |
855 | } | |
856 | ||
784303b0 | 857 | kernel_wait_quiescent(kernel_tracer_fd); |
9b6c7ec5 DG |
858 | |
859 | /* | |
860 | * If the session was previously started, start as well this newly | |
861 | * created kernel session so the events/channels enabled *after* the | |
862 | * start actually work. | |
863 | */ | |
864 | if (session->started && !session->kernel_session->started) { | |
865 | ret = start_kernel_session(session->kernel_session, wpipe); | |
866 | if (ret != LTTNG_OK) { | |
867 | goto error; | |
868 | } | |
869 | } | |
2f77fc4b DG |
870 | break; |
871 | } | |
872 | case LTTNG_DOMAIN_UST: | |
873 | { | |
874 | struct ltt_ust_channel *uchan; | |
875 | ||
876 | chan_ht = usess->domain_global.channels; | |
877 | ||
878 | uchan = trace_ust_find_channel_by_name(chan_ht, attr->name); | |
879 | if (uchan == NULL) { | |
880 | ret = channel_ust_create(usess, domain, attr); | |
881 | } else { | |
882 | ret = channel_ust_enable(usess, domain, uchan); | |
883 | } | |
9b6c7ec5 DG |
884 | |
885 | /* Start the UST session if the session was already started. */ | |
886 | if (session->started && !usess->start_trace) { | |
887 | ret = ust_app_start_trace_all(usess); | |
888 | if (ret < 0) { | |
889 | ret = LTTNG_ERR_UST_START_FAIL; | |
890 | goto error; | |
891 | } | |
892 | ret = LTTNG_OK; | |
893 | usess->start_trace = 1; | |
894 | } | |
2f77fc4b DG |
895 | break; |
896 | } | |
897 | #if 0 | |
898 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
899 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
900 | case LTTNG_DOMAIN_UST_PID: | |
901 | #endif | |
902 | default: | |
f73fabfd | 903 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
904 | goto error; |
905 | } | |
906 | ||
907 | error: | |
d861c907 | 908 | rcu_read_unlock(); |
2f77fc4b DG |
909 | return ret; |
910 | } | |
911 | ||
912 | ||
913 | /* | |
914 | * Command LTTNG_DISABLE_EVENT processed by the client thread. | |
915 | */ | |
916 | int cmd_disable_event(struct ltt_session *session, int domain, | |
917 | char *channel_name, char *event_name) | |
918 | { | |
919 | int ret; | |
920 | ||
d861c907 DG |
921 | rcu_read_lock(); |
922 | ||
2f77fc4b DG |
923 | switch (domain) { |
924 | case LTTNG_DOMAIN_KERNEL: | |
925 | { | |
926 | struct ltt_kernel_channel *kchan; | |
927 | struct ltt_kernel_session *ksess; | |
928 | ||
929 | ksess = session->kernel_session; | |
930 | ||
931 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); | |
932 | if (kchan == NULL) { | |
f73fabfd | 933 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
2f77fc4b DG |
934 | goto error; |
935 | } | |
936 | ||
937 | ret = event_kernel_disable_tracepoint(ksess, kchan, event_name); | |
f73fabfd | 938 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
939 | goto error; |
940 | } | |
941 | ||
942 | kernel_wait_quiescent(kernel_tracer_fd); | |
943 | break; | |
944 | } | |
945 | case LTTNG_DOMAIN_UST: | |
946 | { | |
947 | struct ltt_ust_channel *uchan; | |
948 | struct ltt_ust_session *usess; | |
949 | ||
950 | usess = session->ust_session; | |
951 | ||
952 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
953 | channel_name); | |
954 | if (uchan == NULL) { | |
f73fabfd | 955 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2f77fc4b DG |
956 | goto error; |
957 | } | |
958 | ||
959 | ret = event_ust_disable_tracepoint(usess, domain, uchan, event_name); | |
f73fabfd | 960 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
961 | goto error; |
962 | } | |
963 | ||
964 | DBG3("Disable UST event %s in channel %s completed", event_name, | |
965 | channel_name); | |
966 | break; | |
967 | } | |
968 | #if 0 | |
969 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
970 | case LTTNG_DOMAIN_UST_PID: | |
971 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
972 | #endif | |
973 | default: | |
f73fabfd | 974 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
975 | goto error; |
976 | } | |
977 | ||
f73fabfd | 978 | ret = LTTNG_OK; |
2f77fc4b DG |
979 | |
980 | error: | |
d861c907 | 981 | rcu_read_unlock(); |
2f77fc4b DG |
982 | return ret; |
983 | } | |
984 | ||
985 | /* | |
986 | * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread. | |
987 | */ | |
988 | int cmd_disable_event_all(struct ltt_session *session, int domain, | |
989 | char *channel_name) | |
990 | { | |
991 | int ret; | |
992 | ||
d861c907 DG |
993 | rcu_read_lock(); |
994 | ||
2f77fc4b DG |
995 | switch (domain) { |
996 | case LTTNG_DOMAIN_KERNEL: | |
997 | { | |
998 | struct ltt_kernel_session *ksess; | |
999 | struct ltt_kernel_channel *kchan; | |
1000 | ||
1001 | ksess = session->kernel_session; | |
1002 | ||
1003 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); | |
1004 | if (kchan == NULL) { | |
f73fabfd | 1005 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
2f77fc4b DG |
1006 | goto error; |
1007 | } | |
1008 | ||
1009 | ret = event_kernel_disable_all(ksess, kchan); | |
f73fabfd | 1010 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1011 | goto error; |
1012 | } | |
1013 | ||
1014 | kernel_wait_quiescent(kernel_tracer_fd); | |
1015 | break; | |
1016 | } | |
1017 | case LTTNG_DOMAIN_UST: | |
1018 | { | |
1019 | struct ltt_ust_session *usess; | |
1020 | struct ltt_ust_channel *uchan; | |
1021 | ||
1022 | usess = session->ust_session; | |
1023 | ||
1024 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
1025 | channel_name); | |
1026 | if (uchan == NULL) { | |
f73fabfd | 1027 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2f77fc4b DG |
1028 | goto error; |
1029 | } | |
1030 | ||
1031 | ret = event_ust_disable_all_tracepoints(usess, domain, uchan); | |
1032 | if (ret != 0) { | |
1033 | goto error; | |
1034 | } | |
1035 | ||
1036 | DBG3("Disable all UST events in channel %s completed", channel_name); | |
1037 | ||
1038 | break; | |
1039 | } | |
1040 | #if 0 | |
1041 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
1042 | case LTTNG_DOMAIN_UST_PID: | |
1043 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
1044 | #endif | |
1045 | default: | |
f73fabfd | 1046 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1047 | goto error; |
1048 | } | |
1049 | ||
f73fabfd | 1050 | ret = LTTNG_OK; |
2f77fc4b DG |
1051 | |
1052 | error: | |
d861c907 | 1053 | rcu_read_unlock(); |
2f77fc4b DG |
1054 | return ret; |
1055 | } | |
1056 | ||
1057 | /* | |
1058 | * Command LTTNG_ADD_CONTEXT processed by the client thread. | |
1059 | */ | |
1060 | int cmd_add_context(struct ltt_session *session, int domain, | |
601d5acf | 1061 | char *channel_name, struct lttng_event_context *ctx, int kwpipe) |
2f77fc4b DG |
1062 | { |
1063 | int ret; | |
1064 | ||
1065 | switch (domain) { | |
1066 | case LTTNG_DOMAIN_KERNEL: | |
979e618e DG |
1067 | assert(session->kernel_session); |
1068 | ||
1069 | if (session->kernel_session->channel_count == 0) { | |
1070 | /* Create default channel */ | |
1071 | ret = channel_kernel_create(session->kernel_session, NULL, kwpipe); | |
1072 | if (ret != LTTNG_OK) { | |
1073 | goto error; | |
1074 | } | |
1075 | } | |
1076 | ||
2f77fc4b | 1077 | /* Add kernel context to kernel tracer */ |
601d5acf | 1078 | ret = context_kernel_add(session->kernel_session, ctx, channel_name); |
f73fabfd | 1079 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1080 | goto error; |
1081 | } | |
1082 | break; | |
1083 | case LTTNG_DOMAIN_UST: | |
1084 | { | |
1085 | struct ltt_ust_session *usess = session->ust_session; | |
2f77fc4b DG |
1086 | assert(usess); |
1087 | ||
979e618e DG |
1088 | unsigned int chan_count = |
1089 | lttng_ht_get_count(usess->domain_global.channels); | |
1090 | if (chan_count == 0) { | |
1091 | struct lttng_channel *attr; | |
1092 | /* Create default channel */ | |
1093 | attr = channel_new_default_attr(domain); | |
1094 | if (attr == NULL) { | |
1095 | ret = LTTNG_ERR_FATAL; | |
1096 | goto error; | |
1097 | } | |
1098 | ||
1099 | ret = channel_ust_create(usess, domain, attr); | |
1100 | if (ret != LTTNG_OK) { | |
1101 | free(attr); | |
1102 | goto error; | |
1103 | } | |
1104 | free(attr); | |
1105 | } | |
1106 | ||
601d5acf | 1107 | ret = context_ust_add(usess, domain, ctx, channel_name); |
f73fabfd | 1108 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1109 | goto error; |
1110 | } | |
1111 | break; | |
1112 | } | |
1113 | #if 0 | |
1114 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
1115 | case LTTNG_DOMAIN_UST_PID: | |
1116 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
1117 | #endif | |
1118 | default: | |
f73fabfd | 1119 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1120 | goto error; |
1121 | } | |
1122 | ||
f73fabfd | 1123 | ret = LTTNG_OK; |
2f77fc4b DG |
1124 | |
1125 | error: | |
1126 | return ret; | |
1127 | } | |
1128 | ||
2f77fc4b DG |
1129 | /* |
1130 | * Command LTTNG_ENABLE_EVENT processed by the client thread. | |
1131 | */ | |
1132 | int cmd_enable_event(struct ltt_session *session, int domain, | |
025faf73 DG |
1133 | char *channel_name, struct lttng_event *event, |
1134 | struct lttng_filter_bytecode *filter, int wpipe) | |
2f77fc4b DG |
1135 | { |
1136 | int ret; | |
1137 | struct lttng_channel *attr; | |
1138 | ||
1139 | assert(session); | |
1140 | assert(event); | |
1141 | assert(channel_name); | |
1142 | ||
d861c907 DG |
1143 | rcu_read_lock(); |
1144 | ||
2f77fc4b DG |
1145 | switch (domain) { |
1146 | case LTTNG_DOMAIN_KERNEL: | |
1147 | { | |
1148 | struct ltt_kernel_channel *kchan; | |
1149 | ||
1150 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
1151 | session->kernel_session); | |
1152 | if (kchan == NULL) { | |
1153 | attr = channel_new_default_attr(domain); | |
1154 | if (attr == NULL) { | |
f73fabfd | 1155 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1156 | goto error; |
1157 | } | |
1158 | strncpy(attr->name, channel_name, sizeof(attr->name)); | |
1159 | ||
1160 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 1161 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1162 | free(attr); |
1163 | goto error; | |
1164 | } | |
1165 | free(attr); | |
1166 | } | |
1167 | ||
1168 | /* Get the newly created kernel channel pointer */ | |
1169 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
1170 | session->kernel_session); | |
1171 | if (kchan == NULL) { | |
1172 | /* This sould not happen... */ | |
f73fabfd | 1173 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1174 | goto error; |
1175 | } | |
1176 | ||
1177 | ret = event_kernel_enable_tracepoint(session->kernel_session, kchan, | |
1178 | event); | |
f73fabfd | 1179 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1180 | goto error; |
1181 | } | |
1182 | ||
1183 | kernel_wait_quiescent(kernel_tracer_fd); | |
1184 | break; | |
1185 | } | |
1186 | case LTTNG_DOMAIN_UST: | |
1187 | { | |
1188 | struct ltt_ust_channel *uchan; | |
1189 | struct ltt_ust_session *usess = session->ust_session; | |
1190 | ||
1191 | assert(usess); | |
1192 | ||
1193 | /* Get channel from global UST domain */ | |
1194 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
1195 | channel_name); | |
1196 | if (uchan == NULL) { | |
1197 | /* Create default channel */ | |
1198 | attr = channel_new_default_attr(domain); | |
1199 | if (attr == NULL) { | |
f73fabfd | 1200 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1201 | goto error; |
1202 | } | |
1203 | strncpy(attr->name, channel_name, sizeof(attr->name)); | |
1204 | ||
1205 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 1206 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1207 | free(attr); |
1208 | goto error; | |
1209 | } | |
1210 | free(attr); | |
1211 | ||
1212 | /* Get the newly created channel reference back */ | |
1213 | uchan = trace_ust_find_channel_by_name( | |
1214 | usess->domain_global.channels, channel_name); | |
1215 | assert(uchan); | |
1216 | } | |
1217 | ||
1218 | /* At this point, the session and channel exist on the tracer */ | |
025faf73 | 1219 | ret = event_ust_enable_tracepoint(usess, domain, uchan, event, filter); |
f73fabfd | 1220 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1221 | goto error; |
1222 | } | |
1223 | break; | |
1224 | } | |
1225 | #if 0 | |
1226 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
1227 | case LTTNG_DOMAIN_UST_PID: | |
1228 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
1229 | #endif | |
1230 | default: | |
f73fabfd | 1231 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1232 | goto error; |
1233 | } | |
1234 | ||
f73fabfd | 1235 | ret = LTTNG_OK; |
2f77fc4b DG |
1236 | |
1237 | error: | |
d861c907 | 1238 | rcu_read_unlock(); |
2f77fc4b DG |
1239 | return ret; |
1240 | } | |
1241 | ||
1242 | /* | |
1243 | * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread. | |
1244 | */ | |
1245 | int cmd_enable_event_all(struct ltt_session *session, int domain, | |
025faf73 DG |
1246 | char *channel_name, int event_type, |
1247 | struct lttng_filter_bytecode *filter, int wpipe) | |
2f77fc4b DG |
1248 | { |
1249 | int ret; | |
1250 | struct lttng_channel *attr; | |
1251 | ||
1252 | assert(session); | |
1253 | assert(channel_name); | |
1254 | ||
d861c907 DG |
1255 | rcu_read_lock(); |
1256 | ||
2f77fc4b DG |
1257 | switch (domain) { |
1258 | case LTTNG_DOMAIN_KERNEL: | |
1259 | { | |
1260 | struct ltt_kernel_channel *kchan; | |
1261 | ||
1262 | assert(session->kernel_session); | |
1263 | ||
1264 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
1265 | session->kernel_session); | |
1266 | if (kchan == NULL) { | |
1267 | /* Create default channel */ | |
1268 | attr = channel_new_default_attr(domain); | |
1269 | if (attr == NULL) { | |
f73fabfd | 1270 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1271 | goto error; |
1272 | } | |
1273 | strncpy(attr->name, channel_name, sizeof(attr->name)); | |
1274 | ||
1275 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 1276 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1277 | free(attr); |
1278 | goto error; | |
1279 | } | |
1280 | free(attr); | |
1281 | ||
1282 | /* Get the newly created kernel channel pointer */ | |
1283 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
1284 | session->kernel_session); | |
1285 | assert(kchan); | |
1286 | } | |
1287 | ||
1288 | switch (event_type) { | |
1289 | case LTTNG_EVENT_SYSCALL: | |
1290 | ret = event_kernel_enable_all_syscalls(session->kernel_session, | |
1291 | kchan, kernel_tracer_fd); | |
1292 | break; | |
1293 | case LTTNG_EVENT_TRACEPOINT: | |
1294 | /* | |
1295 | * This call enables all LTTNG_KERNEL_TRACEPOINTS and | |
1296 | * events already registered to the channel. | |
1297 | */ | |
1298 | ret = event_kernel_enable_all_tracepoints(session->kernel_session, | |
1299 | kchan, kernel_tracer_fd); | |
1300 | break; | |
1301 | case LTTNG_EVENT_ALL: | |
1302 | /* Enable syscalls and tracepoints */ | |
1303 | ret = event_kernel_enable_all(session->kernel_session, | |
1304 | kchan, kernel_tracer_fd); | |
1305 | break; | |
1306 | default: | |
f73fabfd | 1307 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
2f77fc4b DG |
1308 | goto error; |
1309 | } | |
1310 | ||
1311 | /* Manage return value */ | |
f73fabfd | 1312 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1313 | goto error; |
1314 | } | |
1315 | ||
1316 | kernel_wait_quiescent(kernel_tracer_fd); | |
1317 | break; | |
1318 | } | |
1319 | case LTTNG_DOMAIN_UST: | |
1320 | { | |
1321 | struct ltt_ust_channel *uchan; | |
1322 | struct ltt_ust_session *usess = session->ust_session; | |
1323 | ||
1324 | assert(usess); | |
1325 | ||
1326 | /* Get channel from global UST domain */ | |
1327 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
1328 | channel_name); | |
1329 | if (uchan == NULL) { | |
1330 | /* Create default channel */ | |
1331 | attr = channel_new_default_attr(domain); | |
1332 | if (attr == NULL) { | |
f73fabfd | 1333 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1334 | goto error; |
1335 | } | |
1336 | strncpy(attr->name, channel_name, sizeof(attr->name)); | |
1337 | ||
1338 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 1339 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1340 | free(attr); |
1341 | goto error; | |
1342 | } | |
1343 | free(attr); | |
1344 | ||
1345 | /* Get the newly created channel reference back */ | |
1346 | uchan = trace_ust_find_channel_by_name( | |
1347 | usess->domain_global.channels, channel_name); | |
1348 | assert(uchan); | |
1349 | } | |
1350 | ||
1351 | /* At this point, the session and channel exist on the tracer */ | |
1352 | ||
1353 | switch (event_type) { | |
1354 | case LTTNG_EVENT_ALL: | |
1355 | case LTTNG_EVENT_TRACEPOINT: | |
025faf73 DG |
1356 | ret = event_ust_enable_all_tracepoints(usess, domain, uchan, |
1357 | filter); | |
f73fabfd | 1358 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1359 | goto error; |
1360 | } | |
1361 | break; | |
1362 | default: | |
f73fabfd | 1363 | ret = LTTNG_ERR_UST_ENABLE_FAIL; |
2f77fc4b DG |
1364 | goto error; |
1365 | } | |
1366 | ||
1367 | /* Manage return value */ | |
f73fabfd | 1368 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1369 | goto error; |
1370 | } | |
1371 | ||
1372 | break; | |
1373 | } | |
1374 | #if 0 | |
1375 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
1376 | case LTTNG_DOMAIN_UST_PID: | |
1377 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
1378 | #endif | |
1379 | default: | |
f73fabfd | 1380 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1381 | goto error; |
1382 | } | |
1383 | ||
f73fabfd | 1384 | ret = LTTNG_OK; |
2f77fc4b DG |
1385 | |
1386 | error: | |
d861c907 | 1387 | rcu_read_unlock(); |
2f77fc4b DG |
1388 | return ret; |
1389 | } | |
1390 | ||
1391 | ||
1392 | /* | |
1393 | * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. | |
1394 | */ | |
1395 | ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events) | |
1396 | { | |
1397 | int ret; | |
1398 | ssize_t nb_events = 0; | |
1399 | ||
1400 | switch (domain) { | |
1401 | case LTTNG_DOMAIN_KERNEL: | |
1402 | nb_events = kernel_list_events(kernel_tracer_fd, events); | |
1403 | if (nb_events < 0) { | |
f73fabfd | 1404 | ret = LTTNG_ERR_KERN_LIST_FAIL; |
2f77fc4b DG |
1405 | goto error; |
1406 | } | |
1407 | break; | |
1408 | case LTTNG_DOMAIN_UST: | |
1409 | nb_events = ust_app_list_events(events); | |
1410 | if (nb_events < 0) { | |
f73fabfd | 1411 | ret = LTTNG_ERR_UST_LIST_FAIL; |
2f77fc4b DG |
1412 | goto error; |
1413 | } | |
1414 | break; | |
1415 | default: | |
f73fabfd | 1416 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1417 | goto error; |
1418 | } | |
1419 | ||
1420 | return nb_events; | |
1421 | ||
1422 | error: | |
1423 | /* Return negative value to differentiate return code */ | |
1424 | return -ret; | |
1425 | } | |
1426 | ||
1427 | /* | |
1428 | * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread. | |
1429 | */ | |
1430 | ssize_t cmd_list_tracepoint_fields(int domain, | |
1431 | struct lttng_event_field **fields) | |
1432 | { | |
1433 | int ret; | |
1434 | ssize_t nb_fields = 0; | |
1435 | ||
1436 | switch (domain) { | |
1437 | case LTTNG_DOMAIN_UST: | |
1438 | nb_fields = ust_app_list_event_fields(fields); | |
1439 | if (nb_fields < 0) { | |
f73fabfd | 1440 | ret = LTTNG_ERR_UST_LIST_FAIL; |
2f77fc4b DG |
1441 | goto error; |
1442 | } | |
1443 | break; | |
1444 | case LTTNG_DOMAIN_KERNEL: | |
1445 | default: /* fall-through */ | |
f73fabfd | 1446 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1447 | goto error; |
1448 | } | |
1449 | ||
1450 | return nb_fields; | |
1451 | ||
1452 | error: | |
1453 | /* Return negative value to differentiate return code */ | |
1454 | return -ret; | |
1455 | } | |
1456 | ||
1457 | /* | |
1458 | * Command LTTNG_START_TRACE processed by the client thread. | |
1459 | */ | |
1460 | int cmd_start_trace(struct ltt_session *session) | |
1461 | { | |
1462 | int ret; | |
1463 | struct ltt_kernel_session *ksession; | |
1464 | struct ltt_ust_session *usess; | |
2f77fc4b DG |
1465 | |
1466 | assert(session); | |
1467 | ||
1468 | /* Ease our life a bit ;) */ | |
1469 | ksession = session->kernel_session; | |
1470 | usess = session->ust_session; | |
1471 | ||
1472 | if (session->enabled) { | |
1473 | /* Already started. */ | |
f73fabfd | 1474 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
2f77fc4b DG |
1475 | goto error; |
1476 | } | |
1477 | ||
1478 | session->enabled = 1; | |
1479 | ||
1480 | ret = setup_relayd(session); | |
f73fabfd | 1481 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1482 | ERR("Error setting up relayd for session %s", session->name); |
1483 | goto error; | |
1484 | } | |
1485 | ||
1486 | /* Kernel tracing */ | |
1487 | if (ksession != NULL) { | |
9b6c7ec5 DG |
1488 | ret = start_kernel_session(ksession, kernel_tracer_fd); |
1489 | if (ret != LTTNG_OK) { | |
2f77fc4b DG |
1490 | goto error; |
1491 | } | |
2f77fc4b DG |
1492 | } |
1493 | ||
1494 | /* Flag session that trace should start automatically */ | |
1495 | if (usess) { | |
1496 | usess->start_trace = 1; | |
1497 | ||
1498 | ret = ust_app_start_trace_all(usess); | |
1499 | if (ret < 0) { | |
f73fabfd | 1500 | ret = LTTNG_ERR_UST_START_FAIL; |
2f77fc4b DG |
1501 | goto error; |
1502 | } | |
1503 | } | |
1504 | ||
9b6c7ec5 DG |
1505 | session->started = 1; |
1506 | ||
f73fabfd | 1507 | ret = LTTNG_OK; |
2f77fc4b DG |
1508 | |
1509 | error: | |
1510 | return ret; | |
1511 | } | |
1512 | ||
1513 | /* | |
1514 | * Command LTTNG_STOP_TRACE processed by the client thread. | |
1515 | */ | |
1516 | int cmd_stop_trace(struct ltt_session *session) | |
1517 | { | |
1518 | int ret; | |
1519 | struct ltt_kernel_channel *kchan; | |
1520 | struct ltt_kernel_session *ksession; | |
1521 | struct ltt_ust_session *usess; | |
1522 | ||
1523 | assert(session); | |
1524 | ||
1525 | /* Short cut */ | |
1526 | ksession = session->kernel_session; | |
1527 | usess = session->ust_session; | |
1528 | ||
1529 | if (!session->enabled) { | |
f73fabfd | 1530 | ret = LTTNG_ERR_TRACE_ALREADY_STOPPED; |
2f77fc4b DG |
1531 | goto error; |
1532 | } | |
1533 | ||
1534 | session->enabled = 0; | |
1535 | ||
1536 | /* Kernel tracer */ | |
1537 | if (ksession) { | |
1538 | DBG("Stop kernel tracing"); | |
1539 | ||
1540 | /* Flush metadata if exist */ | |
1541 | if (ksession->metadata_stream_fd >= 0) { | |
1542 | ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd); | |
1543 | if (ret < 0) { | |
1544 | ERR("Kernel metadata flush failed"); | |
1545 | } | |
1546 | } | |
1547 | ||
1548 | /* Flush all buffers before stopping */ | |
1549 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { | |
1550 | ret = kernel_flush_buffer(kchan); | |
1551 | if (ret < 0) { | |
1552 | ERR("Kernel flush buffer error"); | |
1553 | } | |
1554 | } | |
1555 | ||
1556 | ret = kernel_stop_session(ksession); | |
1557 | if (ret < 0) { | |
f73fabfd | 1558 | ret = LTTNG_ERR_KERN_STOP_FAIL; |
2f77fc4b DG |
1559 | goto error; |
1560 | } | |
1561 | ||
1562 | kernel_wait_quiescent(kernel_tracer_fd); | |
9b6c7ec5 DG |
1563 | |
1564 | ksession->started = 0; | |
2f77fc4b DG |
1565 | } |
1566 | ||
1567 | if (usess) { | |
1568 | usess->start_trace = 0; | |
1569 | ||
1570 | ret = ust_app_stop_trace_all(usess); | |
1571 | if (ret < 0) { | |
f73fabfd | 1572 | ret = LTTNG_ERR_UST_STOP_FAIL; |
2f77fc4b DG |
1573 | goto error; |
1574 | } | |
1575 | } | |
1576 | ||
9b6c7ec5 DG |
1577 | session->started = 0; |
1578 | ||
f73fabfd | 1579 | ret = LTTNG_OK; |
2f77fc4b DG |
1580 | |
1581 | error: | |
1582 | return ret; | |
1583 | } | |
1584 | ||
1585 | /* | |
1586 | * Command LTTNG_SET_CONSUMER_URI processed by the client thread. | |
1587 | */ | |
1588 | int cmd_set_consumer_uri(int domain, struct ltt_session *session, | |
1589 | size_t nb_uri, struct lttng_uri *uris) | |
1590 | { | |
1591 | int ret, i; | |
1592 | struct ltt_kernel_session *ksess = session->kernel_session; | |
1593 | struct ltt_ust_session *usess = session->ust_session; | |
1594 | struct consumer_output *consumer = NULL; | |
1595 | ||
1596 | assert(session); | |
1597 | assert(uris); | |
1598 | assert(nb_uri > 0); | |
1599 | ||
1600 | /* Can't enable consumer after session started. */ | |
1601 | if (session->enabled) { | |
f73fabfd | 1602 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
2f77fc4b DG |
1603 | goto error; |
1604 | } | |
1605 | ||
2f77fc4b DG |
1606 | /* |
1607 | * This case switch makes sure the domain session has a temporary consumer | |
1608 | * so the URL can be set. | |
1609 | */ | |
1610 | switch (domain) { | |
1611 | case 0: | |
1612 | /* Code flow error. A session MUST always have a consumer object */ | |
1613 | assert(session->consumer); | |
1614 | /* | |
1615 | * The URL will be added to the tracing session consumer instead of a | |
1616 | * specific domain consumer. | |
1617 | */ | |
1618 | consumer = session->consumer; | |
1619 | break; | |
1620 | case LTTNG_DOMAIN_KERNEL: | |
1621 | /* Code flow error if we don't have a kernel session here. */ | |
1622 | assert(ksess); | |
1623 | ||
1624 | /* Create consumer output if none exists */ | |
1625 | consumer = ksess->tmp_consumer; | |
1626 | if (consumer == NULL) { | |
1627 | consumer = consumer_copy_output(ksess->consumer); | |
1628 | if (consumer == NULL) { | |
f73fabfd | 1629 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1630 | goto error; |
1631 | } | |
1632 | /* Trash the consumer subdir, we are about to set a new one. */ | |
1633 | memset(consumer->subdir, 0, sizeof(consumer->subdir)); | |
1634 | ksess->tmp_consumer = consumer; | |
1635 | } | |
1636 | ||
1637 | break; | |
1638 | case LTTNG_DOMAIN_UST: | |
1639 | /* Code flow error if we don't have a kernel session here. */ | |
1640 | assert(usess); | |
1641 | ||
1642 | /* Create consumer output if none exists */ | |
1643 | consumer = usess->tmp_consumer; | |
1644 | if (consumer == NULL) { | |
1645 | consumer = consumer_copy_output(usess->consumer); | |
1646 | if (consumer == NULL) { | |
f73fabfd | 1647 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1648 | goto error; |
1649 | } | |
1650 | /* Trash the consumer subdir, we are about to set a new one. */ | |
1651 | memset(consumer->subdir, 0, sizeof(consumer->subdir)); | |
1652 | usess->tmp_consumer = consumer; | |
1653 | } | |
1654 | ||
1655 | break; | |
1656 | } | |
1657 | ||
1658 | for (i = 0; i < nb_uri; i++) { | |
1659 | struct consumer_socket *socket; | |
1660 | struct lttng_ht_iter iter; | |
1661 | ||
1662 | ret = add_uri_to_consumer(consumer, &uris[i], domain, session->name); | |
1663 | if (ret < 0) { | |
1664 | goto error; | |
1665 | } | |
1666 | ||
fd5a50b0 DG |
1667 | /* Don't send relayd socket if URI is NOT remote */ |
1668 | if (uris[i].dtype == LTTNG_DST_PATH) { | |
2f77fc4b DG |
1669 | continue; |
1670 | } | |
1671 | ||
1672 | /* Try to send relayd URI to the consumer if exist. */ | |
1673 | rcu_read_lock(); | |
1674 | cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter, | |
1675 | socket, node.node) { | |
1676 | ||
1677 | /* A socket in the HT should never have a negative fd */ | |
1678 | assert(socket->fd >= 0); | |
1679 | ||
1680 | pthread_mutex_lock(socket->lock); | |
1681 | ret = send_consumer_relayd_socket(domain, session, &uris[i], | |
f50f23d9 | 1682 | consumer, socket); |
2f77fc4b | 1683 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 1684 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1685 | rcu_read_unlock(); |
1686 | goto error; | |
1687 | } | |
1688 | } | |
1689 | rcu_read_unlock(); | |
1690 | } | |
1691 | ||
1692 | /* All good! */ | |
f73fabfd | 1693 | ret = LTTNG_OK; |
2f77fc4b DG |
1694 | |
1695 | error: | |
1696 | return ret; | |
1697 | } | |
1698 | ||
1699 | /* | |
1700 | * Command LTTNG_CREATE_SESSION processed by the client thread. | |
1701 | */ | |
1702 | int cmd_create_session_uri(char *name, struct lttng_uri *uris, | |
1703 | size_t nb_uri, lttng_sock_cred *creds) | |
1704 | { | |
1705 | int ret; | |
1706 | char *path = NULL; | |
1707 | struct ltt_session *session; | |
1708 | ||
1709 | assert(name); | |
1710 | ||
1711 | /* | |
1712 | * Verify if the session already exist | |
1713 | * | |
1714 | * XXX: There is no need for the session lock list here since the caller | |
1715 | * (process_client_msg) is holding it. We might want to change that so a | |
1716 | * single command does not lock the entire session list. | |
1717 | */ | |
1718 | session = session_find_by_name(name); | |
1719 | if (session != NULL) { | |
f73fabfd | 1720 | ret = LTTNG_ERR_EXIST_SESS; |
2f77fc4b DG |
1721 | goto find_error; |
1722 | } | |
1723 | ||
1724 | /* Create tracing session in the registry */ | |
1725 | ret = session_create(name, path, LTTNG_SOCK_GET_UID_CRED(creds), | |
1726 | LTTNG_SOCK_GET_GID_CRED(creds)); | |
f73fabfd | 1727 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1728 | goto session_error; |
1729 | } | |
1730 | ||
1731 | /* | |
1732 | * Get the newly created session pointer back | |
1733 | * | |
1734 | * XXX: There is no need for the session lock list here since the caller | |
1735 | * (process_client_msg) is holding it. We might want to change that so a | |
1736 | * single command does not lock the entire session list. | |
1737 | */ | |
1738 | session = session_find_by_name(name); | |
1739 | assert(session); | |
1740 | ||
1741 | /* Create default consumer output for the session not yet created. */ | |
1742 | session->consumer = consumer_create_output(CONSUMER_DST_LOCAL); | |
1743 | if (session->consumer == NULL) { | |
f73fabfd | 1744 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1745 | goto consumer_error; |
1746 | } | |
1747 | ||
1748 | /* | |
1749 | * This means that the lttng_create_session call was called with the _path_ | |
1750 | * argument set to NULL. | |
1751 | */ | |
1752 | if (uris == NULL) { | |
1753 | /* | |
1754 | * At this point, we'll skip the consumer URI setup and create a | |
1755 | * session with a NULL path which will flag the session to NOT spawn a | |
1756 | * consumer. | |
1757 | */ | |
1758 | DBG("Create session %s with NO uri, skipping consumer setup", name); | |
1759 | goto end; | |
1760 | } | |
1761 | ||
1762 | session->start_consumer = 1; | |
1763 | ||
1764 | ret = cmd_set_consumer_uri(0, session, nb_uri, uris); | |
f73fabfd | 1765 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1766 | goto consumer_error; |
1767 | } | |
1768 | ||
1769 | session->consumer->enabled = 1; | |
1770 | ||
1771 | end: | |
f73fabfd | 1772 | return LTTNG_OK; |
2f77fc4b DG |
1773 | |
1774 | consumer_error: | |
1775 | session_destroy(session); | |
1776 | session_error: | |
1777 | find_error: | |
1778 | return ret; | |
1779 | } | |
1780 | ||
1781 | /* | |
1782 | * Command LTTNG_DESTROY_SESSION processed by the client thread. | |
1783 | */ | |
1784 | int cmd_destroy_session(struct ltt_session *session, int wpipe) | |
1785 | { | |
1786 | int ret; | |
1787 | struct ltt_ust_session *usess; | |
1788 | struct ltt_kernel_session *ksess; | |
1789 | ||
1790 | /* Safety net */ | |
1791 | assert(session); | |
1792 | ||
1793 | usess = session->ust_session; | |
1794 | ksess = session->kernel_session; | |
1795 | ||
1796 | /* Clean kernel session teardown */ | |
1797 | kernel_destroy_session(ksess); | |
1798 | ||
1799 | /* UST session teardown */ | |
1800 | if (usess) { | |
1801 | /* Close any relayd session */ | |
1802 | consumer_output_send_destroy_relayd(usess->consumer); | |
1803 | ||
1804 | /* Destroy every UST application related to this session. */ | |
1805 | ret = ust_app_destroy_trace_all(usess); | |
1806 | if (ret) { | |
1807 | ERR("Error in ust_app_destroy_trace_all"); | |
1808 | } | |
1809 | ||
1810 | /* Clean up the rest. */ | |
1811 | trace_ust_destroy_session(usess); | |
1812 | } | |
1813 | ||
1814 | /* | |
1815 | * Must notify the kernel thread here to update it's poll set in order to | |
1816 | * remove the channel(s)' fd just destroyed. | |
1817 | */ | |
1818 | ret = notify_thread_pipe(wpipe); | |
1819 | if (ret < 0) { | |
1820 | PERROR("write kernel poll pipe"); | |
1821 | } | |
1822 | ||
1823 | ret = session_destroy(session); | |
1824 | ||
1825 | return ret; | |
1826 | } | |
1827 | ||
1828 | /* | |
1829 | * Command LTTNG_CALIBRATE processed by the client thread. | |
1830 | */ | |
1831 | int cmd_calibrate(int domain, struct lttng_calibrate *calibrate) | |
1832 | { | |
1833 | int ret; | |
1834 | ||
1835 | switch (domain) { | |
1836 | case LTTNG_DOMAIN_KERNEL: | |
1837 | { | |
1838 | struct lttng_kernel_calibrate kcalibrate; | |
1839 | ||
1840 | kcalibrate.type = calibrate->type; | |
1841 | ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate); | |
1842 | if (ret < 0) { | |
f73fabfd | 1843 | ret = LTTNG_ERR_KERN_ENABLE_FAIL; |
2f77fc4b DG |
1844 | goto error; |
1845 | } | |
1846 | break; | |
1847 | } | |
1848 | case LTTNG_DOMAIN_UST: | |
1849 | { | |
1850 | struct lttng_ust_calibrate ucalibrate; | |
1851 | ||
1852 | ucalibrate.type = calibrate->type; | |
1853 | ret = ust_app_calibrate_glb(&ucalibrate); | |
1854 | if (ret < 0) { | |
f73fabfd | 1855 | ret = LTTNG_ERR_UST_CALIBRATE_FAIL; |
2f77fc4b DG |
1856 | goto error; |
1857 | } | |
1858 | break; | |
1859 | } | |
1860 | default: | |
f73fabfd | 1861 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1862 | goto error; |
1863 | } | |
1864 | ||
f73fabfd | 1865 | ret = LTTNG_OK; |
2f77fc4b DG |
1866 | |
1867 | error: | |
1868 | return ret; | |
1869 | } | |
1870 | ||
1871 | /* | |
1872 | * Command LTTNG_REGISTER_CONSUMER processed by the client thread. | |
1873 | */ | |
1874 | int cmd_register_consumer(struct ltt_session *session, int domain, | |
1875 | const char *sock_path, struct consumer_data *cdata) | |
1876 | { | |
1877 | int ret, sock; | |
1878 | struct consumer_socket *socket; | |
1879 | ||
1880 | assert(session); | |
1881 | assert(cdata); | |
1882 | assert(sock_path); | |
1883 | ||
1884 | switch (domain) { | |
1885 | case LTTNG_DOMAIN_KERNEL: | |
1886 | { | |
1887 | struct ltt_kernel_session *ksess = session->kernel_session; | |
1888 | ||
1889 | assert(ksess); | |
1890 | ||
1891 | /* Can't register a consumer if there is already one */ | |
1892 | if (ksess->consumer_fds_sent != 0) { | |
f73fabfd | 1893 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
2f77fc4b DG |
1894 | goto error; |
1895 | } | |
1896 | ||
1897 | sock = lttcomm_connect_unix_sock(sock_path); | |
1898 | if (sock < 0) { | |
f73fabfd | 1899 | ret = LTTNG_ERR_CONNECT_FAIL; |
2f77fc4b DG |
1900 | goto error; |
1901 | } | |
1902 | ||
1903 | socket = consumer_allocate_socket(sock); | |
1904 | if (socket == NULL) { | |
f66c074c DG |
1905 | ret = close(sock); |
1906 | if (ret < 0) { | |
1907 | PERROR("close register consumer"); | |
1908 | } | |
f73fabfd | 1909 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1910 | goto error; |
1911 | } | |
1912 | ||
1913 | socket->lock = zmalloc(sizeof(pthread_mutex_t)); | |
1914 | if (socket->lock == NULL) { | |
1915 | PERROR("zmalloc pthread mutex"); | |
f73fabfd | 1916 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1917 | goto error; |
1918 | } | |
1919 | pthread_mutex_init(socket->lock, NULL); | |
1920 | socket->registered = 1; | |
1921 | ||
1922 | rcu_read_lock(); | |
1923 | consumer_add_socket(socket, ksess->consumer); | |
1924 | rcu_read_unlock(); | |
1925 | ||
1926 | pthread_mutex_lock(&cdata->pid_mutex); | |
1927 | cdata->pid = -1; | |
1928 | pthread_mutex_unlock(&cdata->pid_mutex); | |
1929 | ||
1930 | break; | |
1931 | } | |
1932 | default: | |
1933 | /* TODO: Userspace tracing */ | |
f73fabfd | 1934 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1935 | goto error; |
1936 | } | |
1937 | ||
f73fabfd | 1938 | ret = LTTNG_OK; |
2f77fc4b DG |
1939 | |
1940 | error: | |
1941 | return ret; | |
1942 | } | |
1943 | ||
1944 | /* | |
1945 | * Command LTTNG_LIST_DOMAINS processed by the client thread. | |
1946 | */ | |
1947 | ssize_t cmd_list_domains(struct ltt_session *session, | |
1948 | struct lttng_domain **domains) | |
1949 | { | |
1950 | int ret, index = 0; | |
1951 | ssize_t nb_dom = 0; | |
1952 | ||
1953 | if (session->kernel_session != NULL) { | |
1954 | DBG3("Listing domains found kernel domain"); | |
1955 | nb_dom++; | |
1956 | } | |
1957 | ||
1958 | if (session->ust_session != NULL) { | |
1959 | DBG3("Listing domains found UST global domain"); | |
1960 | nb_dom++; | |
1961 | } | |
1962 | ||
1963 | *domains = zmalloc(nb_dom * sizeof(struct lttng_domain)); | |
1964 | if (*domains == NULL) { | |
f73fabfd | 1965 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1966 | goto error; |
1967 | } | |
1968 | ||
1969 | if (session->kernel_session != NULL) { | |
1970 | (*domains)[index].type = LTTNG_DOMAIN_KERNEL; | |
1971 | index++; | |
1972 | } | |
1973 | ||
1974 | if (session->ust_session != NULL) { | |
1975 | (*domains)[index].type = LTTNG_DOMAIN_UST; | |
1976 | index++; | |
1977 | } | |
1978 | ||
1979 | return nb_dom; | |
1980 | ||
1981 | error: | |
f73fabfd DG |
1982 | /* Return negative value to differentiate return code */ |
1983 | return -ret; | |
2f77fc4b DG |
1984 | } |
1985 | ||
1986 | ||
1987 | /* | |
1988 | * Command LTTNG_LIST_CHANNELS processed by the client thread. | |
1989 | */ | |
1990 | ssize_t cmd_list_channels(int domain, struct ltt_session *session, | |
1991 | struct lttng_channel **channels) | |
1992 | { | |
1993 | int ret; | |
1994 | ssize_t nb_chan = 0; | |
1995 | ||
1996 | switch (domain) { | |
1997 | case LTTNG_DOMAIN_KERNEL: | |
1998 | if (session->kernel_session != NULL) { | |
1999 | nb_chan = session->kernel_session->channel_count; | |
2000 | } | |
2001 | DBG3("Number of kernel channels %zd", nb_chan); | |
c7d620a2 DG |
2002 | if (nb_chan <= 0) { |
2003 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; | |
2004 | } | |
2f77fc4b DG |
2005 | break; |
2006 | case LTTNG_DOMAIN_UST: | |
2007 | if (session->ust_session != NULL) { | |
2008 | nb_chan = lttng_ht_get_count( | |
2009 | session->ust_session->domain_global.channels); | |
2010 | } | |
2011 | DBG3("Number of UST global channels %zd", nb_chan); | |
c7d620a2 DG |
2012 | if (nb_chan <= 0) { |
2013 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; | |
2014 | } | |
2f77fc4b DG |
2015 | break; |
2016 | default: | |
2017 | *channels = NULL; | |
f73fabfd | 2018 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2019 | goto error; |
2020 | } | |
2021 | ||
2022 | if (nb_chan > 0) { | |
2023 | *channels = zmalloc(nb_chan * sizeof(struct lttng_channel)); | |
2024 | if (*channels == NULL) { | |
f73fabfd | 2025 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2026 | goto error; |
2027 | } | |
2028 | ||
2029 | list_lttng_channels(domain, session, *channels); | |
2030 | } else { | |
2031 | *channels = NULL; | |
c7d620a2 DG |
2032 | /* Ret value was set in the domain switch case */ |
2033 | goto error; | |
2f77fc4b DG |
2034 | } |
2035 | ||
2036 | return nb_chan; | |
2037 | ||
2038 | error: | |
f73fabfd DG |
2039 | /* Return negative value to differentiate return code */ |
2040 | return -ret; | |
2f77fc4b DG |
2041 | } |
2042 | ||
2043 | /* | |
2044 | * Command LTTNG_LIST_EVENTS processed by the client thread. | |
2045 | */ | |
2046 | ssize_t cmd_list_events(int domain, struct ltt_session *session, | |
2047 | char *channel_name, struct lttng_event **events) | |
2048 | { | |
2049 | int ret = 0; | |
2050 | ssize_t nb_event = 0; | |
2051 | ||
2052 | switch (domain) { | |
2053 | case LTTNG_DOMAIN_KERNEL: | |
2054 | if (session->kernel_session != NULL) { | |
2055 | nb_event = list_lttng_kernel_events(channel_name, | |
2056 | session->kernel_session, events); | |
2057 | } | |
2058 | break; | |
2059 | case LTTNG_DOMAIN_UST: | |
2060 | { | |
2061 | if (session->ust_session != NULL) { | |
2062 | nb_event = list_lttng_ust_global_events(channel_name, | |
2063 | &session->ust_session->domain_global, events); | |
2064 | } | |
2065 | break; | |
2066 | } | |
2067 | default: | |
f73fabfd | 2068 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2069 | goto error; |
2070 | } | |
2071 | ||
f73fabfd | 2072 | return nb_event; |
2f77fc4b DG |
2073 | |
2074 | error: | |
f73fabfd DG |
2075 | /* Return negative value to differentiate return code */ |
2076 | return -ret; | |
2f77fc4b DG |
2077 | } |
2078 | ||
2079 | /* | |
2080 | * Using the session list, filled a lttng_session array to send back to the | |
2081 | * client for session listing. | |
2082 | * | |
2083 | * The session list lock MUST be acquired before calling this function. Use | |
2084 | * session_lock_list() and session_unlock_list(). | |
2085 | */ | |
2086 | void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid, | |
2087 | gid_t gid) | |
2088 | { | |
2089 | int ret; | |
2090 | unsigned int i = 0; | |
2091 | struct ltt_session *session; | |
2092 | struct ltt_session_list *list = session_get_list(); | |
2093 | ||
2094 | DBG("Getting all available session for UID %d GID %d", | |
2095 | uid, gid); | |
2096 | /* | |
2097 | * Iterate over session list and append data after the control struct in | |
2098 | * the buffer. | |
2099 | */ | |
2100 | cds_list_for_each_entry(session, &list->head, list) { | |
2101 | /* | |
2102 | * Only list the sessions the user can control. | |
2103 | */ | |
2104 | if (!session_access_ok(session, uid, gid)) { | |
2105 | continue; | |
2106 | } | |
2107 | ||
2108 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2109 | struct ltt_ust_session *usess = session->ust_session; | |
2110 | ||
2111 | if (session->consumer->type == CONSUMER_DST_NET || | |
2112 | (ksess && ksess->consumer->type == CONSUMER_DST_NET) || | |
2113 | (usess && usess->consumer->type == CONSUMER_DST_NET)) { | |
2114 | ret = build_network_session_path(sessions[i].path, | |
2115 | sizeof(session[i].path), session); | |
2116 | } else { | |
2117 | ret = snprintf(sessions[i].path, sizeof(session[i].path), "%s", | |
2118 | session->consumer->dst.trace_path); | |
2119 | } | |
2120 | if (ret < 0) { | |
2121 | PERROR("snprintf session path"); | |
2122 | continue; | |
2123 | } | |
2124 | ||
2125 | strncpy(sessions[i].name, session->name, NAME_MAX); | |
2126 | sessions[i].name[NAME_MAX - 1] = '\0'; | |
2127 | sessions[i].enabled = session->enabled; | |
2128 | i++; | |
2129 | } | |
2130 | } | |
2131 | ||
2132 | /* | |
2133 | * Command LTTNG_DISABLE_CONSUMER processed by the client thread. | |
2134 | */ | |
2135 | int cmd_disable_consumer(int domain, struct ltt_session *session) | |
2136 | { | |
2137 | int ret; | |
2138 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2139 | struct ltt_ust_session *usess = session->ust_session; | |
2140 | struct consumer_output *consumer; | |
2141 | ||
2142 | assert(session); | |
2143 | ||
2144 | if (session->enabled) { | |
2145 | /* Can't disable consumer on an already started session */ | |
f73fabfd | 2146 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
2f77fc4b DG |
2147 | goto error; |
2148 | } | |
2149 | ||
2150 | if (!session->start_consumer) { | |
f73fabfd | 2151 | ret = LTTNG_ERR_NO_CONSUMER; |
2f77fc4b DG |
2152 | goto error; |
2153 | } | |
2154 | ||
2155 | switch (domain) { | |
2156 | case 0: | |
2157 | DBG("Disable tracing session %s consumer", session->name); | |
2158 | consumer = session->consumer; | |
2159 | break; | |
2160 | case LTTNG_DOMAIN_KERNEL: | |
2161 | /* Code flow error if we don't have a kernel session here. */ | |
2162 | assert(ksess); | |
2163 | ||
2164 | DBG("Disabling kernel consumer"); | |
2165 | consumer = ksess->consumer; | |
2166 | ||
2167 | break; | |
2168 | case LTTNG_DOMAIN_UST: | |
2169 | /* Code flow error if we don't have a UST session here. */ | |
2170 | assert(usess); | |
2171 | ||
2172 | DBG("Disabling UST consumer"); | |
2173 | consumer = usess->consumer; | |
2174 | ||
2175 | break; | |
2176 | default: | |
f73fabfd | 2177 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
2178 | goto error; |
2179 | } | |
2180 | ||
2181 | if (consumer) { | |
2182 | consumer->enabled = 0; | |
2183 | /* Success at this point */ | |
f73fabfd | 2184 | ret = LTTNG_OK; |
2f77fc4b | 2185 | } else { |
f73fabfd | 2186 | ret = LTTNG_ERR_NO_CONSUMER; |
2f77fc4b DG |
2187 | } |
2188 | ||
2189 | error: | |
2190 | return ret; | |
2191 | } | |
2192 | ||
2193 | /* | |
2194 | * Command LTTNG_ENABLE_CONSUMER processed by the client thread. | |
2195 | */ | |
2196 | int cmd_enable_consumer(int domain, struct ltt_session *session) | |
2197 | { | |
2198 | int ret; | |
2199 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2200 | struct ltt_ust_session *usess = session->ust_session; | |
2201 | struct consumer_output *consumer = NULL; | |
2202 | ||
2203 | assert(session); | |
2204 | ||
2205 | /* Can't enable consumer after session started. */ | |
2206 | if (session->enabled) { | |
f73fabfd | 2207 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
2f77fc4b DG |
2208 | goto error; |
2209 | } | |
2210 | ||
2f77fc4b DG |
2211 | switch (domain) { |
2212 | case 0: | |
2213 | assert(session->consumer); | |
2214 | consumer = session->consumer; | |
2215 | break; | |
2216 | case LTTNG_DOMAIN_KERNEL: | |
2217 | /* Code flow error if we don't have a kernel session here. */ | |
2218 | assert(ksess); | |
2219 | ||
2220 | /* | |
2221 | * Check if we have already sent fds to the consumer. In that case, | |
2222 | * the enable-consumer command can't be used because a start trace | |
2223 | * had previously occured. | |
2224 | */ | |
2225 | if (ksess->consumer_fds_sent) { | |
f73fabfd | 2226 | ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL; |
2f77fc4b DG |
2227 | goto error; |
2228 | } | |
2229 | ||
2230 | consumer = ksess->tmp_consumer; | |
2231 | if (consumer == NULL) { | |
f73fabfd | 2232 | ret = LTTNG_OK; |
2f77fc4b DG |
2233 | /* No temp. consumer output exists. Using the current one. */ |
2234 | DBG3("No temporary consumer. Using default"); | |
2235 | consumer = ksess->consumer; | |
2236 | goto error; | |
2237 | } | |
2238 | ||
2239 | switch (consumer->type) { | |
2240 | case CONSUMER_DST_LOCAL: | |
2241 | DBG2("Consumer output is local. Creating directory(ies)"); | |
2242 | ||
2243 | /* Create directory(ies) */ | |
2244 | ret = run_as_mkdir_recursive(consumer->dst.trace_path, | |
2245 | S_IRWXU | S_IRWXG, session->uid, session->gid); | |
2246 | if (ret < 0) { | |
2247 | if (ret != -EEXIST) { | |
2248 | ERR("Trace directory creation error"); | |
f73fabfd | 2249 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2250 | goto error; |
2251 | } | |
2252 | } | |
2253 | break; | |
2254 | case CONSUMER_DST_NET: | |
2255 | DBG2("Consumer output is network. Validating URIs"); | |
2256 | /* Validate if we have both control and data path set. */ | |
2257 | if (!consumer->dst.net.control_isset) { | |
f73fabfd | 2258 | ret = LTTNG_ERR_URL_CTRL_MISS; |
2f77fc4b DG |
2259 | goto error; |
2260 | } | |
2261 | ||
2262 | if (!consumer->dst.net.data_isset) { | |
f73fabfd | 2263 | ret = LTTNG_ERR_URL_DATA_MISS; |
2f77fc4b DG |
2264 | goto error; |
2265 | } | |
2266 | ||
2267 | /* Check established network session state */ | |
2268 | if (session->net_handle == 0) { | |
f73fabfd | 2269 | ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL; |
2f77fc4b DG |
2270 | ERR("Session network handle is not set on enable-consumer"); |
2271 | goto error; | |
2272 | } | |
2273 | ||
2274 | break; | |
2275 | } | |
2276 | ||
2f77fc4b DG |
2277 | /* |
2278 | * @session-lock | |
2279 | * This is race free for now since the session lock is acquired before | |
2280 | * ending up in this function. No other threads can access this kernel | |
2281 | * session without this lock hence freeing the consumer output object | |
2282 | * is valid. | |
2283 | */ | |
2284 | rcu_read_lock(); | |
9a572cf5 | 2285 | /* Destroy current consumer. We are about to replace it */ |
2f77fc4b DG |
2286 | consumer_destroy_output(ksess->consumer); |
2287 | rcu_read_unlock(); | |
2288 | ksess->consumer = consumer; | |
2289 | ksess->tmp_consumer = NULL; | |
2290 | ||
2291 | break; | |
2292 | case LTTNG_DOMAIN_UST: | |
2293 | /* Code flow error if we don't have a UST session here. */ | |
2294 | assert(usess); | |
2295 | ||
2296 | /* | |
2297 | * Check if we have already sent fds to the consumer. In that case, | |
2298 | * the enable-consumer command can't be used because a start trace | |
2299 | * had previously occured. | |
2300 | */ | |
2301 | if (usess->start_trace) { | |
f73fabfd | 2302 | ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL; |
2f77fc4b DG |
2303 | goto error; |
2304 | } | |
2305 | ||
2306 | consumer = usess->tmp_consumer; | |
2307 | if (consumer == NULL) { | |
f73fabfd | 2308 | ret = LTTNG_OK; |
2f77fc4b DG |
2309 | /* No temp. consumer output exists. Using the current one. */ |
2310 | DBG3("No temporary consumer. Using default"); | |
2311 | consumer = usess->consumer; | |
2312 | goto error; | |
2313 | } | |
2314 | ||
2315 | switch (consumer->type) { | |
2316 | case CONSUMER_DST_LOCAL: | |
2317 | DBG2("Consumer output is local. Creating directory(ies)"); | |
2318 | ||
2319 | /* Create directory(ies) */ | |
2320 | ret = run_as_mkdir_recursive(consumer->dst.trace_path, | |
2321 | S_IRWXU | S_IRWXG, session->uid, session->gid); | |
2322 | if (ret < 0) { | |
2323 | if (ret != -EEXIST) { | |
2324 | ERR("Trace directory creation error"); | |
f73fabfd | 2325 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2326 | goto error; |
2327 | } | |
2328 | } | |
2329 | break; | |
2330 | case CONSUMER_DST_NET: | |
2331 | DBG2("Consumer output is network. Validating URIs"); | |
2332 | /* Validate if we have both control and data path set. */ | |
2333 | if (!consumer->dst.net.control_isset) { | |
f73fabfd | 2334 | ret = LTTNG_ERR_URL_CTRL_MISS; |
2f77fc4b DG |
2335 | goto error; |
2336 | } | |
2337 | ||
2338 | if (!consumer->dst.net.data_isset) { | |
f73fabfd | 2339 | ret = LTTNG_ERR_URL_DATA_MISS; |
2f77fc4b DG |
2340 | goto error; |
2341 | } | |
2342 | ||
2343 | /* Check established network session state */ | |
2344 | if (session->net_handle == 0) { | |
f73fabfd | 2345 | ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL; |
2f77fc4b DG |
2346 | DBG2("Session network handle is not set on enable-consumer"); |
2347 | goto error; | |
2348 | } | |
2349 | ||
2350 | if (consumer->net_seq_index == -1) { | |
f73fabfd | 2351 | ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL; |
2f77fc4b DG |
2352 | DBG2("Network index is not set on the consumer"); |
2353 | goto error; | |
2354 | } | |
2355 | ||
2356 | break; | |
2357 | } | |
2358 | ||
2f77fc4b DG |
2359 | /* |
2360 | * @session-lock | |
2361 | * This is race free for now since the session lock is acquired before | |
2362 | * ending up in this function. No other threads can access this kernel | |
2363 | * session without this lock hence freeing the consumer output object | |
2364 | * is valid. | |
2365 | */ | |
2366 | rcu_read_lock(); | |
9a572cf5 | 2367 | /* Destroy current consumer. We are about to replace it */ |
2f77fc4b DG |
2368 | consumer_destroy_output(usess->consumer); |
2369 | rcu_read_unlock(); | |
2370 | usess->consumer = consumer; | |
2371 | usess->tmp_consumer = NULL; | |
2372 | ||
2373 | break; | |
2374 | } | |
2375 | ||
4b35a6b3 DG |
2376 | session->start_consumer = 1; |
2377 | ||
2f77fc4b DG |
2378 | /* Enable it */ |
2379 | if (consumer) { | |
2380 | consumer->enabled = 1; | |
2381 | /* Success at this point */ | |
f73fabfd | 2382 | ret = LTTNG_OK; |
2f77fc4b DG |
2383 | } else { |
2384 | /* Should not really happend... */ | |
f73fabfd | 2385 | ret = LTTNG_ERR_NO_CONSUMER; |
2f77fc4b DG |
2386 | } |
2387 | ||
2388 | error: | |
2389 | return ret; | |
2390 | } | |
2391 | ||
806e2684 | 2392 | /* |
6d805429 DG |
2393 | * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning |
2394 | * ready for trace analysis (or anykind of reader) or else 1 for pending data. | |
806e2684 | 2395 | */ |
6d805429 | 2396 | int cmd_data_pending(struct ltt_session *session) |
806e2684 DG |
2397 | { |
2398 | int ret; | |
2399 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2400 | struct ltt_ust_session *usess = session->ust_session; | |
2401 | ||
2402 | assert(session); | |
2403 | ||
2404 | /* Session MUST be stopped to ask for data availability. */ | |
2405 | if (session->enabled) { | |
2406 | ret = LTTNG_ERR_SESSION_STARTED; | |
2407 | goto error; | |
2408 | } | |
2409 | ||
2410 | if (ksess && ksess->consumer) { | |
6d805429 DG |
2411 | ret = consumer_is_data_pending(ksess->id, ksess->consumer); |
2412 | if (ret == 1) { | |
806e2684 DG |
2413 | /* Data is still being extracted for the kernel. */ |
2414 | goto error; | |
2415 | } | |
2416 | } | |
2417 | ||
2418 | if (usess && usess->consumer) { | |
6d805429 DG |
2419 | ret = consumer_is_data_pending(usess->id, usess->consumer); |
2420 | if (ret == 1) { | |
806e2684 DG |
2421 | /* Data is still being extracted for the kernel. */ |
2422 | goto error; | |
2423 | } | |
2424 | } | |
2425 | ||
2426 | /* Data is ready to be read by a viewer */ | |
6d805429 | 2427 | ret = 0; |
806e2684 DG |
2428 | |
2429 | error: | |
2430 | return ret; | |
2431 | } | |
2432 | ||
2f77fc4b DG |
2433 | /* |
2434 | * Init command subsystem. | |
2435 | */ | |
2436 | void cmd_init(void) | |
2437 | { | |
2438 | /* | |
2439 | * Set network sequence index to 1 for streams to match a relayd socket on | |
2440 | * the consumer side. | |
2441 | */ | |
2442 | uatomic_set(&relayd_net_seq_idx, 1); | |
2443 | ||
2444 | DBG("Command subsystem initialized"); | |
2445 | } |