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