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