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