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