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