Commit | Line | Data |
---|---|---|
2f77fc4b DG |
1 | /* |
2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> | |
bdf64013 | 3 | * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
2f77fc4b DG |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License, version 2 only, as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along with | |
15 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
16 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 | */ | |
18 | ||
6c1c0768 | 19 | #define _LGPL_SOURCE |
2f77fc4b | 20 | #include <assert.h> |
6dc3064a | 21 | #include <inttypes.h> |
2f77fc4b DG |
22 | #include <urcu/list.h> |
23 | #include <urcu/uatomic.h> | |
a503e1ef | 24 | #include <sys/stat.h> |
d2956687 | 25 | #include <stdio.h> |
2f77fc4b DG |
26 | |
27 | #include <common/defaults.h> | |
28 | #include <common/common.h> | |
29 | #include <common/sessiond-comm/sessiond-comm.h> | |
30 | #include <common/relayd/relayd.h> | |
10a50311 | 31 | #include <common/utils.h> |
f5436bfc | 32 | #include <common/compat/string.h> |
93ec662e | 33 | #include <common/kernel-ctl/kernel-ctl.h> |
b0880ae5 JG |
34 | #include <common/dynamic-buffer.h> |
35 | #include <common/buffer-view.h> | |
82b69413 | 36 | #include <common/trace-chunk.h> |
3e3665b8 | 37 | #include <lttng/location-internal.h> |
b0880ae5 JG |
38 | #include <lttng/trigger/trigger-internal.h> |
39 | #include <lttng/condition/condition.h> | |
40 | #include <lttng/action/action.h> | |
6e21424e | 41 | #include <lttng/channel.h> |
b0880ae5 | 42 | #include <lttng/channel-internal.h> |
5c408ad8 | 43 | #include <lttng/rotate-internal.h> |
17dd1232 | 44 | #include <lttng/location-internal.h> |
b178f53e | 45 | #include <lttng/session-internal.h> |
3c02e545 | 46 | #include <lttng/userspace-probe-internal.h> |
b178f53e | 47 | #include <lttng/session-descriptor-internal.h> |
9f449915 | 48 | #include <common/string-utils/string-utils.h> |
2f77fc4b DG |
49 | |
50 | #include "channel.h" | |
51 | #include "consumer.h" | |
52 | #include "event.h" | |
8782cc74 | 53 | #include "health-sessiond.h" |
2f77fc4b DG |
54 | #include "kernel.h" |
55 | #include "kernel-consumer.h" | |
56 | #include "lttng-sessiond.h" | |
57 | #include "utils.h" | |
0dbc2034 | 58 | #include "lttng-syscall.h" |
7c1d2758 | 59 | #include "agent.h" |
93ec662e | 60 | #include "buffer-registry.h" |
b0880ae5 JG |
61 | #include "notification-thread.h" |
62 | #include "notification-thread-commands.h" | |
5c408ad8 JD |
63 | #include "rotate.h" |
64 | #include "rotation-thread.h" | |
8e319828 | 65 | #include "timer.h" |
f28f9e44 | 66 | #include "agent-thread.h" |
2f77fc4b DG |
67 | |
68 | #include "cmd.h" | |
69 | ||
a503e1ef JG |
70 | /* Sleep for 100ms between each check for the shm path's deletion. */ |
71 | #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000 | |
72 | ||
3e3665b8 JG |
73 | struct cmd_destroy_session_reply_context { |
74 | int reply_sock_fd; | |
75 | bool implicit_rotation_on_destroy; | |
76 | }; | |
77 | ||
a503e1ef JG |
78 | static enum lttng_error_code wait_on_path(void *path); |
79 | ||
80 | /* | |
81 | * Command completion handler that is used by the destroy command | |
82 | * when a session that has a non-default shm_path is being destroyed. | |
83 | * | |
84 | * See comment in cmd_destroy_session() for the rationale. | |
85 | */ | |
86 | static struct destroy_completion_handler { | |
87 | struct cmd_completion_handler handler; | |
88 | char shm_path[member_sizeof(struct ltt_session, shm_path)]; | |
89 | } destroy_completion_handler = { | |
90 | .handler = { | |
91 | .run = wait_on_path, | |
92 | .data = destroy_completion_handler.shm_path | |
93 | }, | |
94 | .shm_path = { 0 }, | |
95 | }; | |
96 | ||
97 | static struct cmd_completion_handler *current_completion_handler; | |
98 | ||
2f77fc4b DG |
99 | /* |
100 | * Used to keep a unique index for each relayd socket created where this value | |
101 | * is associated with streams on the consumer so it can match the right relayd | |
d88aee68 DG |
102 | * to send to. It must be accessed with the relayd_net_seq_idx_lock |
103 | * held. | |
2f77fc4b | 104 | */ |
d88aee68 DG |
105 | static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER; |
106 | static uint64_t relayd_net_seq_idx; | |
2f77fc4b | 107 | |
7076b56e JG |
108 | static int validate_ust_event_name(const char *); |
109 | static int cmd_enable_event_internal(struct ltt_session *session, | |
110 | struct lttng_domain *domain, | |
111 | char *channel_name, struct lttng_event *event, | |
112 | char *filter_expression, | |
113 | struct lttng_filter_bytecode *filter, | |
114 | struct lttng_event_exclusion *exclusion, | |
115 | int wpipe); | |
116 | ||
2f77fc4b DG |
117 | /* |
118 | * Create a session path used by list_lttng_sessions for the case that the | |
119 | * session consumer is on the network. | |
120 | */ | |
121 | static int build_network_session_path(char *dst, size_t size, | |
122 | struct ltt_session *session) | |
123 | { | |
124 | int ret, kdata_port, udata_port; | |
125 | struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL; | |
126 | char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX]; | |
127 | ||
128 | assert(session); | |
129 | assert(dst); | |
130 | ||
131 | memset(tmp_urls, 0, sizeof(tmp_urls)); | |
132 | memset(tmp_uurl, 0, sizeof(tmp_uurl)); | |
133 | ||
134 | kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT; | |
135 | ||
136 | if (session->kernel_session && session->kernel_session->consumer) { | |
137 | kuri = &session->kernel_session->consumer->dst.net.control; | |
138 | kdata_port = session->kernel_session->consumer->dst.net.data.port; | |
139 | } | |
140 | ||
141 | if (session->ust_session && session->ust_session->consumer) { | |
142 | uuri = &session->ust_session->consumer->dst.net.control; | |
143 | udata_port = session->ust_session->consumer->dst.net.data.port; | |
144 | } | |
145 | ||
146 | if (uuri == NULL && kuri == NULL) { | |
147 | uri = &session->consumer->dst.net.control; | |
148 | kdata_port = session->consumer->dst.net.data.port; | |
149 | } else if (kuri && uuri) { | |
150 | ret = uri_compare(kuri, uuri); | |
151 | if (ret) { | |
152 | /* Not Equal */ | |
153 | uri = kuri; | |
154 | /* Build uuri URL string */ | |
155 | ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl)); | |
156 | if (ret < 0) { | |
157 | goto error; | |
158 | } | |
159 | } else { | |
160 | uri = kuri; | |
161 | } | |
162 | } else if (kuri && uuri == NULL) { | |
163 | uri = kuri; | |
164 | } else if (uuri && kuri == NULL) { | |
165 | uri = uuri; | |
166 | } | |
167 | ||
168 | ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls)); | |
169 | if (ret < 0) { | |
170 | goto error; | |
171 | } | |
172 | ||
9aa9f900 DG |
173 | /* |
174 | * Do we have a UST url set. If yes, this means we have both kernel and UST | |
175 | * to print. | |
176 | */ | |
9d035200 | 177 | if (*tmp_uurl != '\0') { |
2f77fc4b DG |
178 | ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]", |
179 | tmp_urls, kdata_port, tmp_uurl, udata_port); | |
180 | } else { | |
9aa9f900 | 181 | int dport; |
bef08707 | 182 | if (kuri || (!kuri && !uuri)) { |
9aa9f900 DG |
183 | dport = kdata_port; |
184 | } else { | |
185 | /* No kernel URI, use the UST port. */ | |
186 | dport = udata_port; | |
187 | } | |
188 | ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport); | |
2f77fc4b DG |
189 | } |
190 | ||
191 | error: | |
192 | return ret; | |
193 | } | |
194 | ||
fb83fe64 JD |
195 | /* |
196 | * Get run-time attributes if the session has been started (discarded events, | |
197 | * lost packets). | |
198 | */ | |
199 | static int get_kernel_runtime_stats(struct ltt_session *session, | |
200 | struct ltt_kernel_channel *kchan, uint64_t *discarded_events, | |
201 | uint64_t *lost_packets) | |
202 | { | |
203 | int ret; | |
204 | ||
205 | if (!session->has_been_started) { | |
206 | ret = 0; | |
207 | *discarded_events = 0; | |
208 | *lost_packets = 0; | |
209 | goto end; | |
210 | } | |
211 | ||
e1f3997a | 212 | ret = consumer_get_discarded_events(session->id, kchan->key, |
fb83fe64 JD |
213 | session->kernel_session->consumer, |
214 | discarded_events); | |
215 | if (ret < 0) { | |
216 | goto end; | |
217 | } | |
218 | ||
e1f3997a | 219 | ret = consumer_get_lost_packets(session->id, kchan->key, |
fb83fe64 JD |
220 | session->kernel_session->consumer, |
221 | lost_packets); | |
222 | if (ret < 0) { | |
223 | goto end; | |
224 | } | |
225 | ||
226 | end: | |
227 | return ret; | |
228 | } | |
229 | ||
230 | /* | |
231 | * Get run-time attributes if the session has been started (discarded events, | |
232 | * lost packets). | |
233 | */ | |
234 | static int get_ust_runtime_stats(struct ltt_session *session, | |
235 | struct ltt_ust_channel *uchan, uint64_t *discarded_events, | |
236 | uint64_t *lost_packets) | |
237 | { | |
238 | int ret; | |
239 | struct ltt_ust_session *usess; | |
240 | ||
a91c5803 JG |
241 | if (!discarded_events || !lost_packets) { |
242 | ret = -1; | |
243 | goto end; | |
244 | } | |
245 | ||
fb83fe64 | 246 | usess = session->ust_session; |
a905c624 JG |
247 | assert(discarded_events); |
248 | assert(lost_packets); | |
fb83fe64 JD |
249 | |
250 | if (!usess || !session->has_been_started) { | |
251 | *discarded_events = 0; | |
252 | *lost_packets = 0; | |
253 | ret = 0; | |
254 | goto end; | |
255 | } | |
256 | ||
257 | if (usess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
258 | ret = ust_app_uid_get_channel_runtime_stats(usess->id, | |
259 | &usess->buffer_reg_uid_list, | |
260 | usess->consumer, uchan->id, | |
261 | uchan->attr.overwrite, | |
262 | discarded_events, | |
263 | lost_packets); | |
264 | } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) { | |
265 | ret = ust_app_pid_get_channel_runtime_stats(usess, | |
266 | uchan, usess->consumer, | |
267 | uchan->attr.overwrite, | |
268 | discarded_events, | |
269 | lost_packets); | |
270 | if (ret < 0) { | |
271 | goto end; | |
272 | } | |
273 | *discarded_events += uchan->per_pid_closed_app_discarded; | |
274 | *lost_packets += uchan->per_pid_closed_app_lost; | |
275 | } else { | |
276 | ERR("Unsupported buffer type"); | |
967bc289 | 277 | assert(0); |
fb83fe64 JD |
278 | ret = -1; |
279 | goto end; | |
280 | } | |
281 | ||
282 | end: | |
283 | return ret; | |
284 | } | |
285 | ||
2f77fc4b DG |
286 | /* |
287 | * Fill lttng_channel array of all channels. | |
288 | */ | |
d449df4a | 289 | static ssize_t list_lttng_channels(enum lttng_domain_type domain, |
fb83fe64 | 290 | struct ltt_session *session, struct lttng_channel *channels, |
cf0bcb51 | 291 | struct lttng_channel_extended *chan_exts) |
2f77fc4b | 292 | { |
d449df4a | 293 | int i = 0, ret = 0; |
2f77fc4b DG |
294 | struct ltt_kernel_channel *kchan; |
295 | ||
296 | DBG("Listing channels for session %s", session->name); | |
297 | ||
298 | switch (domain) { | |
299 | case LTTNG_DOMAIN_KERNEL: | |
300 | /* Kernel channels */ | |
301 | if (session->kernel_session != NULL) { | |
302 | cds_list_for_each_entry(kchan, | |
303 | &session->kernel_session->channel_list.head, list) { | |
fb83fe64 | 304 | uint64_t discarded_events, lost_packets; |
cf0bcb51 JG |
305 | struct lttng_channel_extended *extended; |
306 | ||
307 | extended = (struct lttng_channel_extended *) | |
308 | kchan->channel->attr.extended.ptr; | |
fb83fe64 JD |
309 | |
310 | ret = get_kernel_runtime_stats(session, kchan, | |
311 | &discarded_events, &lost_packets); | |
312 | if (ret < 0) { | |
313 | goto end; | |
314 | } | |
2f77fc4b DG |
315 | /* Copy lttng_channel struct to array */ |
316 | memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel)); | |
317 | channels[i].enabled = kchan->enabled; | |
fb83fe64 JD |
318 | chan_exts[i].discarded_events = |
319 | discarded_events; | |
320 | chan_exts[i].lost_packets = lost_packets; | |
cf0bcb51 JG |
321 | chan_exts[i].monitor_timer_interval = |
322 | extended->monitor_timer_interval; | |
491d1539 | 323 | chan_exts[i].blocking_timeout = 0; |
2f77fc4b DG |
324 | i++; |
325 | } | |
326 | } | |
327 | break; | |
328 | case LTTNG_DOMAIN_UST: | |
329 | { | |
330 | struct lttng_ht_iter iter; | |
331 | struct ltt_ust_channel *uchan; | |
332 | ||
e7fe706f | 333 | rcu_read_lock(); |
2f77fc4b DG |
334 | cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht, |
335 | &iter.iter, uchan, node.node) { | |
a91c5803 | 336 | uint64_t discarded_events = 0, lost_packets = 0; |
fb83fe64 | 337 | |
8ee609c8 MD |
338 | if (lttng_strncpy(channels[i].name, uchan->name, |
339 | LTTNG_SYMBOL_NAME_LEN)) { | |
340 | break; | |
341 | } | |
2f77fc4b DG |
342 | channels[i].attr.overwrite = uchan->attr.overwrite; |
343 | channels[i].attr.subbuf_size = uchan->attr.subbuf_size; | |
344 | channels[i].attr.num_subbuf = uchan->attr.num_subbuf; | |
345 | channels[i].attr.switch_timer_interval = | |
346 | uchan->attr.switch_timer_interval; | |
347 | channels[i].attr.read_timer_interval = | |
348 | uchan->attr.read_timer_interval; | |
349 | channels[i].enabled = uchan->enabled; | |
2f785fe7 DG |
350 | channels[i].attr.tracefile_size = uchan->tracefile_size; |
351 | channels[i].attr.tracefile_count = uchan->tracefile_count; | |
5e4435a4 JG |
352 | |
353 | /* | |
354 | * Map enum lttng_ust_output to enum lttng_event_output. | |
355 | */ | |
2f77fc4b DG |
356 | switch (uchan->attr.output) { |
357 | case LTTNG_UST_MMAP: | |
2f77fc4b DG |
358 | channels[i].attr.output = LTTNG_EVENT_MMAP; |
359 | break; | |
5e4435a4 JG |
360 | default: |
361 | /* | |
362 | * LTTNG_UST_MMAP is the only supported UST | |
363 | * output mode. | |
364 | */ | |
365 | assert(0); | |
366 | break; | |
2f77fc4b | 367 | } |
fb83fe64 | 368 | |
d449df4a MD |
369 | chan_exts[i].monitor_timer_interval = |
370 | uchan->monitor_timer_interval; | |
491d1539 MD |
371 | chan_exts[i].blocking_timeout = |
372 | uchan->attr.u.s.blocking_timeout; | |
d449df4a | 373 | |
fb83fe64 JD |
374 | ret = get_ust_runtime_stats(session, uchan, |
375 | &discarded_events, &lost_packets); | |
376 | if (ret < 0) { | |
377 | break; | |
378 | } | |
379 | chan_exts[i].discarded_events = discarded_events; | |
380 | chan_exts[i].lost_packets = lost_packets; | |
2f77fc4b DG |
381 | i++; |
382 | } | |
e7fe706f | 383 | rcu_read_unlock(); |
2f77fc4b DG |
384 | break; |
385 | } | |
386 | default: | |
387 | break; | |
388 | } | |
fb83fe64 JD |
389 | |
390 | end: | |
d449df4a MD |
391 | if (ret < 0) { |
392 | return -LTTNG_ERR_FATAL; | |
393 | } else { | |
394 | return LTTNG_OK; | |
395 | } | |
2f77fc4b DG |
396 | } |
397 | ||
3c02e545 FD |
398 | static int increment_extended_len(const char *filter_expression, |
399 | struct lttng_event_exclusion *exclusion, | |
400 | const struct lttng_userspace_probe_location *probe_location, | |
401 | size_t *extended_len) | |
b4e3ceb9 | 402 | { |
3c02e545 FD |
403 | int ret = 0; |
404 | ||
b4e3ceb9 PP |
405 | *extended_len += sizeof(struct lttcomm_event_extended_header); |
406 | ||
407 | if (filter_expression) { | |
408 | *extended_len += strlen(filter_expression) + 1; | |
409 | } | |
795d57ce PP |
410 | |
411 | if (exclusion) { | |
412 | *extended_len += exclusion->count * LTTNG_SYMBOL_NAME_LEN; | |
413 | } | |
3c02e545 FD |
414 | |
415 | if (probe_location) { | |
416 | ret = lttng_userspace_probe_location_serialize(probe_location, | |
417 | NULL, NULL); | |
418 | if (ret < 0) { | |
419 | goto end; | |
420 | } | |
421 | *extended_len += ret; | |
422 | } | |
423 | ret = 0; | |
424 | end: | |
425 | return ret; | |
b4e3ceb9 PP |
426 | } |
427 | ||
3c02e545 FD |
428 | static int append_extended_info(const char *filter_expression, |
429 | struct lttng_event_exclusion *exclusion, | |
430 | struct lttng_userspace_probe_location *probe_location, | |
431 | void **extended_at) | |
b4e3ceb9 | 432 | { |
3c02e545 | 433 | int ret = 0; |
b4e3ceb9 | 434 | size_t filter_len = 0; |
795d57ce | 435 | size_t nb_exclusions = 0; |
3c02e545 FD |
436 | size_t userspace_probe_location_len = 0; |
437 | struct lttng_dynamic_buffer location_buffer; | |
438 | struct lttcomm_event_extended_header extended_header; | |
b4e3ceb9 PP |
439 | |
440 | if (filter_expression) { | |
441 | filter_len = strlen(filter_expression) + 1; | |
442 | } | |
443 | ||
795d57ce PP |
444 | if (exclusion) { |
445 | nb_exclusions = exclusion->count; | |
446 | } | |
447 | ||
3c02e545 FD |
448 | if (probe_location) { |
449 | lttng_dynamic_buffer_init(&location_buffer); | |
450 | ret = lttng_userspace_probe_location_serialize(probe_location, | |
451 | &location_buffer, NULL); | |
452 | if (ret < 0) { | |
453 | ret = -1; | |
454 | goto end; | |
455 | } | |
456 | userspace_probe_location_len = location_buffer.size; | |
457 | } | |
458 | ||
795d57ce | 459 | /* Set header fields */ |
b4e3ceb9 | 460 | extended_header.filter_len = filter_len; |
795d57ce | 461 | extended_header.nb_exclusions = nb_exclusions; |
3c02e545 | 462 | extended_header.userspace_probe_location_len = userspace_probe_location_len; |
795d57ce PP |
463 | |
464 | /* Copy header */ | |
b4e3ceb9 PP |
465 | memcpy(*extended_at, &extended_header, sizeof(extended_header)); |
466 | *extended_at += sizeof(extended_header); | |
795d57ce PP |
467 | |
468 | /* Copy filter string */ | |
469 | if (filter_expression) { | |
470 | memcpy(*extended_at, filter_expression, filter_len); | |
471 | *extended_at += filter_len; | |
472 | } | |
473 | ||
474 | /* Copy exclusion names */ | |
475 | if (exclusion) { | |
476 | size_t len = nb_exclusions * LTTNG_SYMBOL_NAME_LEN; | |
477 | ||
478 | memcpy(*extended_at, &exclusion->names, len); | |
479 | *extended_at += len; | |
480 | } | |
3c02e545 FD |
481 | |
482 | if (probe_location) { | |
483 | memcpy(*extended_at, location_buffer.data, location_buffer.size); | |
484 | *extended_at += location_buffer.size; | |
485 | lttng_dynamic_buffer_reset(&location_buffer); | |
486 | } | |
487 | ret = 0; | |
488 | end: | |
489 | return ret; | |
b4e3ceb9 PP |
490 | } |
491 | ||
3c6a091f | 492 | /* |
022d91ba | 493 | * Create a list of agent domain events. |
3c6a091f DG |
494 | * |
495 | * Return number of events in list on success or else a negative value. | |
496 | */ | |
022d91ba | 497 | static int list_lttng_agent_events(struct agent *agt, |
b4e3ceb9 | 498 | struct lttng_event **events, size_t *total_size) |
3c6a091f DG |
499 | { |
500 | int i = 0, ret = 0; | |
501 | unsigned int nb_event = 0; | |
022d91ba | 502 | struct agent_event *event; |
47e52862 | 503 | struct lttng_event *tmp_events = NULL; |
3c6a091f | 504 | struct lttng_ht_iter iter; |
b4e3ceb9 PP |
505 | size_t extended_len = 0; |
506 | void *extended_at; | |
3c6a091f | 507 | |
022d91ba | 508 | assert(agt); |
3c6a091f DG |
509 | assert(events); |
510 | ||
022d91ba | 511 | DBG3("Listing agent events"); |
3c6a091f | 512 | |
e5bbf678 | 513 | rcu_read_lock(); |
022d91ba | 514 | nb_event = lttng_ht_get_count(agt->events); |
e5bbf678 | 515 | rcu_read_unlock(); |
3c6a091f DG |
516 | if (nb_event == 0) { |
517 | ret = nb_event; | |
b4e3ceb9 | 518 | *total_size = 0; |
3c6a091f DG |
519 | goto error; |
520 | } | |
521 | ||
b4e3ceb9 PP |
522 | /* Compute required extended infos size */ |
523 | extended_len = nb_event * sizeof(struct lttcomm_event_extended_header); | |
524 | ||
525 | /* | |
526 | * This is only valid because the commands which add events are | |
527 | * processed in the same thread as the listing. | |
528 | */ | |
529 | rcu_read_lock(); | |
530 | cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) { | |
3c02e545 | 531 | ret = increment_extended_len(event->filter_expression, NULL, NULL, |
795d57ce | 532 | &extended_len); |
3c02e545 FD |
533 | if (ret) { |
534 | DBG("Error computing the length of extended info message"); | |
535 | ret = -LTTNG_ERR_FATAL; | |
536 | goto error; | |
537 | } | |
b4e3ceb9 PP |
538 | } |
539 | rcu_read_unlock(); | |
540 | ||
541 | *total_size = nb_event * sizeof(*tmp_events) + extended_len; | |
542 | tmp_events = zmalloc(*total_size); | |
3c6a091f | 543 | if (!tmp_events) { |
022d91ba | 544 | PERROR("zmalloc agent events session"); |
3c6a091f DG |
545 | ret = -LTTNG_ERR_FATAL; |
546 | goto error; | |
547 | } | |
548 | ||
b4e3ceb9 PP |
549 | extended_at = ((uint8_t *) tmp_events) + |
550 | nb_event * sizeof(struct lttng_event); | |
551 | ||
3c6a091f | 552 | rcu_read_lock(); |
022d91ba | 553 | cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) { |
3c6a091f DG |
554 | strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name)); |
555 | tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0'; | |
556 | tmp_events[i].enabled = event->enabled; | |
2106efa0 | 557 | tmp_events[i].loglevel = event->loglevel_value; |
ff94328f | 558 | tmp_events[i].loglevel_type = event->loglevel_type; |
3c6a091f | 559 | i++; |
b4e3ceb9 PP |
560 | |
561 | /* Append extended info */ | |
3c02e545 | 562 | ret = append_extended_info(event->filter_expression, NULL, NULL, |
795d57ce | 563 | &extended_at); |
3c02e545 FD |
564 | if (ret) { |
565 | DBG("Error appending extended info message"); | |
566 | ret = -LTTNG_ERR_FATAL; | |
567 | goto error; | |
568 | } | |
3c6a091f | 569 | } |
3c6a091f DG |
570 | |
571 | *events = tmp_events; | |
572 | ret = nb_event; | |
3c02e545 | 573 | assert(nb_event == i); |
3c6a091f | 574 | |
47e52862 | 575 | end: |
3c02e545 | 576 | rcu_read_unlock(); |
3c6a091f | 577 | return ret; |
47e52862 JG |
578 | error: |
579 | free(tmp_events); | |
580 | goto end; | |
3c6a091f DG |
581 | } |
582 | ||
2f77fc4b DG |
583 | /* |
584 | * Create a list of ust global domain events. | |
585 | */ | |
586 | static int list_lttng_ust_global_events(char *channel_name, | |
b4e3ceb9 PP |
587 | struct ltt_ust_domain_global *ust_global, |
588 | struct lttng_event **events, size_t *total_size) | |
2f77fc4b DG |
589 | { |
590 | int i = 0, ret = 0; | |
591 | unsigned int nb_event = 0; | |
592 | struct lttng_ht_iter iter; | |
593 | struct lttng_ht_node_str *node; | |
594 | struct ltt_ust_channel *uchan; | |
595 | struct ltt_ust_event *uevent; | |
596 | struct lttng_event *tmp; | |
b4e3ceb9 PP |
597 | size_t extended_len = 0; |
598 | void *extended_at; | |
2f77fc4b DG |
599 | |
600 | DBG("Listing UST global events for channel %s", channel_name); | |
601 | ||
602 | rcu_read_lock(); | |
603 | ||
604 | lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter); | |
605 | node = lttng_ht_iter_get_node_str(&iter); | |
606 | if (node == NULL) { | |
f73fabfd | 607 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
d31d3e8c | 608 | goto end; |
2f77fc4b DG |
609 | } |
610 | ||
611 | uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node); | |
612 | ||
3c442be3 | 613 | nb_event = lttng_ht_get_count(uchan->events); |
2f77fc4b DG |
614 | if (nb_event == 0) { |
615 | ret = nb_event; | |
b4e3ceb9 | 616 | *total_size = 0; |
d31d3e8c | 617 | goto end; |
2f77fc4b DG |
618 | } |
619 | ||
620 | DBG3("Listing UST global %d events", nb_event); | |
621 | ||
b4e3ceb9 PP |
622 | /* Compute required extended infos size */ |
623 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { | |
624 | if (uevent->internal) { | |
625 | nb_event--; | |
626 | continue; | |
627 | } | |
628 | ||
3c02e545 FD |
629 | ret = increment_extended_len(uevent->filter_expression, |
630 | uevent->exclusion, NULL, &extended_len); | |
631 | if (ret) { | |
632 | DBG("Error computing the length of extended info message"); | |
633 | ret = -LTTNG_ERR_FATAL; | |
634 | goto end; | |
635 | } | |
b4e3ceb9 | 636 | } |
d31d3e8c PP |
637 | if (nb_event == 0) { |
638 | /* All events are internal, skip. */ | |
639 | ret = 0; | |
640 | *total_size = 0; | |
641 | goto end; | |
642 | } | |
b4e3ceb9 PP |
643 | |
644 | *total_size = nb_event * sizeof(struct lttng_event) + extended_len; | |
645 | tmp = zmalloc(*total_size); | |
2f77fc4b | 646 | if (tmp == NULL) { |
b12c38df JG |
647 | ret = -LTTNG_ERR_FATAL; |
648 | goto end; | |
2f77fc4b DG |
649 | } |
650 | ||
b4e3ceb9 PP |
651 | extended_at = ((uint8_t *) tmp) + nb_event * sizeof(struct lttng_event); |
652 | ||
2f77fc4b | 653 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { |
3c442be3 JG |
654 | if (uevent->internal) { |
655 | /* This event should remain hidden from clients */ | |
3c442be3 JG |
656 | continue; |
657 | } | |
2f77fc4b DG |
658 | strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN); |
659 | tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
660 | tmp[i].enabled = uevent->enabled; | |
661 | ||
662 | switch (uevent->attr.instrumentation) { | |
663 | case LTTNG_UST_TRACEPOINT: | |
664 | tmp[i].type = LTTNG_EVENT_TRACEPOINT; | |
665 | break; | |
666 | case LTTNG_UST_PROBE: | |
667 | tmp[i].type = LTTNG_EVENT_PROBE; | |
668 | break; | |
669 | case LTTNG_UST_FUNCTION: | |
670 | tmp[i].type = LTTNG_EVENT_FUNCTION; | |
671 | break; | |
672 | } | |
673 | ||
674 | tmp[i].loglevel = uevent->attr.loglevel; | |
675 | switch (uevent->attr.loglevel_type) { | |
676 | case LTTNG_UST_LOGLEVEL_ALL: | |
677 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
678 | break; | |
679 | case LTTNG_UST_LOGLEVEL_RANGE: | |
680 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; | |
681 | break; | |
682 | case LTTNG_UST_LOGLEVEL_SINGLE: | |
683 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; | |
684 | break; | |
685 | } | |
686 | if (uevent->filter) { | |
687 | tmp[i].filter = 1; | |
688 | } | |
4634f12e JI |
689 | if (uevent->exclusion) { |
690 | tmp[i].exclusion = 1; | |
691 | } | |
2f77fc4b | 692 | i++; |
b4e3ceb9 PP |
693 | |
694 | /* Append extended info */ | |
3c02e545 FD |
695 | ret = append_extended_info(uevent->filter_expression, |
696 | uevent->exclusion, NULL, &extended_at); | |
697 | if (ret) { | |
698 | DBG("Error appending extended info message"); | |
699 | ret = -LTTNG_ERR_FATAL; | |
700 | goto end; | |
701 | } | |
2f77fc4b DG |
702 | } |
703 | ||
704 | ret = nb_event; | |
705 | *events = tmp; | |
d31d3e8c | 706 | end: |
2f77fc4b DG |
707 | rcu_read_unlock(); |
708 | return ret; | |
709 | } | |
710 | ||
711 | /* | |
712 | * Fill lttng_event array of all kernel events in the channel. | |
713 | */ | |
714 | static int list_lttng_kernel_events(char *channel_name, | |
b4e3ceb9 PP |
715 | struct ltt_kernel_session *kernel_session, |
716 | struct lttng_event **events, size_t *total_size) | |
2f77fc4b DG |
717 | { |
718 | int i = 0, ret; | |
719 | unsigned int nb_event; | |
720 | struct ltt_kernel_event *event; | |
721 | struct ltt_kernel_channel *kchan; | |
b4e3ceb9 PP |
722 | size_t extended_len = 0; |
723 | void *extended_at; | |
2f77fc4b DG |
724 | |
725 | kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session); | |
726 | if (kchan == NULL) { | |
f73fabfd | 727 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
2f77fc4b DG |
728 | goto error; |
729 | } | |
730 | ||
731 | nb_event = kchan->event_count; | |
732 | ||
733 | DBG("Listing events for channel %s", kchan->channel->name); | |
734 | ||
735 | if (nb_event == 0) { | |
b4e3ceb9 | 736 | *total_size = 0; |
834978fd | 737 | *events = NULL; |
db906c12 | 738 | goto end; |
2f77fc4b DG |
739 | } |
740 | ||
b4e3ceb9 PP |
741 | /* Compute required extended infos size */ |
742 | cds_list_for_each_entry(event, &kchan->events_list.head, list) { | |
3c02e545 FD |
743 | ret = increment_extended_len(event->filter_expression, NULL, |
744 | event->userspace_probe_location, | |
795d57ce | 745 | &extended_len); |
3c02e545 FD |
746 | if (ret) { |
747 | DBG("Error computing the length of extended info message"); | |
748 | ret = -LTTNG_ERR_FATAL; | |
749 | goto error; | |
750 | } | |
b4e3ceb9 PP |
751 | } |
752 | ||
753 | *total_size = nb_event * sizeof(struct lttng_event) + extended_len; | |
754 | *events = zmalloc(*total_size); | |
2f77fc4b | 755 | if (*events == NULL) { |
3c02e545 | 756 | ret = -LTTNG_ERR_FATAL; |
2f77fc4b DG |
757 | goto error; |
758 | } | |
759 | ||
ab4aa612 | 760 | extended_at = ((void *) *events) + |
b4e3ceb9 PP |
761 | nb_event * sizeof(struct lttng_event); |
762 | ||
2f77fc4b DG |
763 | /* Kernel channels */ |
764 | cds_list_for_each_entry(event, &kchan->events_list.head , list) { | |
765 | strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN); | |
766 | (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
767 | (*events)[i].enabled = event->enabled; | |
00f992f2 JG |
768 | (*events)[i].filter = |
769 | (unsigned char) !!event->filter_expression; | |
2f77fc4b DG |
770 | |
771 | switch (event->event->instrumentation) { | |
772 | case LTTNG_KERNEL_TRACEPOINT: | |
773 | (*events)[i].type = LTTNG_EVENT_TRACEPOINT; | |
774 | break; | |
2f77fc4b | 775 | case LTTNG_KERNEL_KRETPROBE: |
1896972b DG |
776 | (*events)[i].type = LTTNG_EVENT_FUNCTION; |
777 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, | |
778 | sizeof(struct lttng_kernel_kprobe)); | |
779 | break; | |
780 | case LTTNG_KERNEL_KPROBE: | |
2f77fc4b DG |
781 | (*events)[i].type = LTTNG_EVENT_PROBE; |
782 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, | |
783 | sizeof(struct lttng_kernel_kprobe)); | |
784 | break; | |
b955b4d4 FD |
785 | case LTTNG_KERNEL_UPROBE: |
786 | (*events)[i].type = LTTNG_EVENT_USERSPACE_PROBE; | |
787 | break; | |
2f77fc4b DG |
788 | case LTTNG_KERNEL_FUNCTION: |
789 | (*events)[i].type = LTTNG_EVENT_FUNCTION; | |
790 | memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace, | |
791 | sizeof(struct lttng_kernel_function)); | |
792 | break; | |
793 | case LTTNG_KERNEL_NOOP: | |
794 | (*events)[i].type = LTTNG_EVENT_NOOP; | |
795 | break; | |
796 | case LTTNG_KERNEL_SYSCALL: | |
797 | (*events)[i].type = LTTNG_EVENT_SYSCALL; | |
798 | break; | |
799 | case LTTNG_KERNEL_ALL: | |
1ab8c2ad FD |
800 | /* fall-through. */ |
801 | default: | |
2f77fc4b DG |
802 | assert(0); |
803 | break; | |
804 | } | |
805 | i++; | |
b4e3ceb9 PP |
806 | |
807 | /* Append extended info */ | |
3c02e545 FD |
808 | ret = append_extended_info(event->filter_expression, NULL, |
809 | event->userspace_probe_location, &extended_at); | |
810 | if (ret) { | |
811 | DBG("Error appending extended info message"); | |
812 | ret = -LTTNG_ERR_FATAL; | |
813 | goto error; | |
814 | } | |
2f77fc4b DG |
815 | } |
816 | ||
db906c12 | 817 | end: |
2f77fc4b DG |
818 | return nb_event; |
819 | ||
820 | error: | |
f73fabfd DG |
821 | /* Negate the error code to differentiate the size from an error */ |
822 | return -ret; | |
2f77fc4b DG |
823 | } |
824 | ||
825 | /* | |
826 | * Add URI so the consumer output object. Set the correct path depending on the | |
827 | * domain adding the default trace directory. | |
828 | */ | |
b178f53e JG |
829 | static enum lttng_error_code add_uri_to_consumer( |
830 | const struct ltt_session *session, | |
831 | struct consumer_output *consumer, | |
832 | struct lttng_uri *uri, enum lttng_domain_type domain) | |
2f77fc4b | 833 | { |
b178f53e JG |
834 | int ret; |
835 | enum lttng_error_code ret_code = LTTNG_OK; | |
2f77fc4b DG |
836 | |
837 | assert(uri); | |
838 | ||
839 | if (consumer == NULL) { | |
840 | DBG("No consumer detected. Don't add URI. Stopping."); | |
b178f53e | 841 | ret_code = LTTNG_ERR_NO_CONSUMER; |
2f77fc4b DG |
842 | goto error; |
843 | } | |
844 | ||
845 | switch (domain) { | |
846 | case LTTNG_DOMAIN_KERNEL: | |
b178f53e JG |
847 | ret = lttng_strncpy(consumer->domain_subdir, |
848 | DEFAULT_KERNEL_TRACE_DIR, | |
849 | sizeof(consumer->domain_subdir)); | |
2f77fc4b DG |
850 | break; |
851 | case LTTNG_DOMAIN_UST: | |
b178f53e JG |
852 | ret = lttng_strncpy(consumer->domain_subdir, |
853 | DEFAULT_UST_TRACE_DIR, | |
854 | sizeof(consumer->domain_subdir)); | |
2f77fc4b DG |
855 | break; |
856 | default: | |
857 | /* | |
b178f53e JG |
858 | * This case is possible is we try to add the URI to the global |
859 | * tracing session consumer object which in this case there is | |
860 | * no subdir. | |
2f77fc4b | 861 | */ |
b178f53e JG |
862 | memset(consumer->domain_subdir, 0, |
863 | sizeof(consumer->domain_subdir)); | |
864 | ret = 0; | |
865 | } | |
866 | if (ret) { | |
867 | ERR("Failed to initialize consumer output domain subdirectory"); | |
868 | ret_code = LTTNG_ERR_FATAL; | |
869 | goto error; | |
2f77fc4b DG |
870 | } |
871 | ||
872 | switch (uri->dtype) { | |
873 | case LTTNG_DST_IPV4: | |
874 | case LTTNG_DST_IPV6: | |
875 | DBG2("Setting network URI to consumer"); | |
876 | ||
df75acac DG |
877 | if (consumer->type == CONSUMER_DST_NET) { |
878 | if ((uri->stype == LTTNG_STREAM_CONTROL && | |
785d2d0d DG |
879 | consumer->dst.net.control_isset) || |
880 | (uri->stype == LTTNG_STREAM_DATA && | |
881 | consumer->dst.net.data_isset)) { | |
b178f53e | 882 | ret_code = LTTNG_ERR_URL_EXIST; |
df75acac DG |
883 | goto error; |
884 | } | |
885 | } else { | |
b178f53e | 886 | memset(&consumer->dst, 0, sizeof(consumer->dst)); |
785d2d0d DG |
887 | } |
888 | ||
2f77fc4b | 889 | /* Set URI into consumer output object */ |
b178f53e | 890 | ret = consumer_set_network_uri(session, consumer, uri); |
2f77fc4b | 891 | if (ret < 0) { |
b178f53e | 892 | ret_code = -ret; |
2f77fc4b DG |
893 | goto error; |
894 | } else if (ret == 1) { | |
895 | /* | |
896 | * URI was the same in the consumer so we do not append the subdir | |
897 | * again so to not duplicate output dir. | |
898 | */ | |
b178f53e | 899 | ret_code = LTTNG_OK; |
2f77fc4b DG |
900 | goto error; |
901 | } | |
2f77fc4b DG |
902 | break; |
903 | case LTTNG_DST_PATH: | |
b178f53e JG |
904 | if (*uri->dst.path != '/' || strstr(uri->dst.path, "../")) { |
905 | ret_code = LTTNG_ERR_INVALID; | |
9ac05d92 MD |
906 | goto error; |
907 | } | |
b178f53e JG |
908 | DBG2("Setting trace directory path from URI to %s", |
909 | uri->dst.path); | |
910 | memset(&consumer->dst, 0, sizeof(consumer->dst)); | |
911 | ||
912 | ret = lttng_strncpy(consumer->dst.session_root_path, | |
913 | uri->dst.path, | |
914 | sizeof(consumer->dst.session_root_path)); | |
2f77fc4b DG |
915 | consumer->type = CONSUMER_DST_LOCAL; |
916 | break; | |
917 | } | |
918 | ||
b178f53e | 919 | ret_code = LTTNG_OK; |
2f77fc4b | 920 | error: |
b178f53e | 921 | return ret_code; |
2f77fc4b DG |
922 | } |
923 | ||
924 | /* | |
925 | * Init tracing by creating trace directory and sending fds kernel consumer. | |
926 | */ | |
927 | static int init_kernel_tracing(struct ltt_kernel_session *session) | |
928 | { | |
929 | int ret = 0; | |
930 | struct lttng_ht_iter iter; | |
931 | struct consumer_socket *socket; | |
932 | ||
933 | assert(session); | |
934 | ||
e7fe706f DG |
935 | rcu_read_lock(); |
936 | ||
2f77fc4b DG |
937 | if (session->consumer_fds_sent == 0 && session->consumer != NULL) { |
938 | cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter, | |
939 | socket, node.node) { | |
2f77fc4b | 940 | pthread_mutex_lock(socket->lock); |
f50f23d9 | 941 | ret = kernel_consumer_send_session(socket, session); |
2f77fc4b DG |
942 | pthread_mutex_unlock(socket->lock); |
943 | if (ret < 0) { | |
f73fabfd | 944 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
2f77fc4b DG |
945 | goto error; |
946 | } | |
947 | } | |
948 | } | |
949 | ||
950 | error: | |
e7fe706f | 951 | rcu_read_unlock(); |
2f77fc4b DG |
952 | return ret; |
953 | } | |
954 | ||
955 | /* | |
956 | * Create a socket to the relayd using the URI. | |
957 | * | |
958 | * On success, the relayd_sock pointer is set to the created socket. | |
9a654598 | 959 | * Else, it remains untouched and an LTTng error code is returned. |
2f77fc4b | 960 | */ |
9a654598 | 961 | static enum lttng_error_code create_connect_relayd(struct lttng_uri *uri, |
b31610f2 JD |
962 | struct lttcomm_relayd_sock **relayd_sock, |
963 | struct consumer_output *consumer) | |
2f77fc4b DG |
964 | { |
965 | int ret; | |
9a654598 | 966 | enum lttng_error_code status = LTTNG_OK; |
6151a90f | 967 | struct lttcomm_relayd_sock *rsock; |
2f77fc4b | 968 | |
6151a90f JD |
969 | rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR, |
970 | RELAYD_VERSION_COMM_MINOR); | |
971 | if (!rsock) { | |
9a654598 | 972 | status = LTTNG_ERR_FATAL; |
2f77fc4b DG |
973 | goto error; |
974 | } | |
975 | ||
ffe60014 DG |
976 | /* |
977 | * Connect to relayd so we can proceed with a session creation. This call | |
978 | * can possibly block for an arbitrary amount of time to set the health | |
979 | * state to be in poll execution. | |
980 | */ | |
981 | health_poll_entry(); | |
6151a90f | 982 | ret = relayd_connect(rsock); |
ffe60014 | 983 | health_poll_exit(); |
2f77fc4b DG |
984 | if (ret < 0) { |
985 | ERR("Unable to reach lttng-relayd"); | |
9a654598 | 986 | status = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
2f77fc4b DG |
987 | goto free_sock; |
988 | } | |
989 | ||
990 | /* Create socket for control stream. */ | |
991 | if (uri->stype == LTTNG_STREAM_CONTROL) { | |
992 | DBG3("Creating relayd stream socket from URI"); | |
993 | ||
994 | /* Check relayd version */ | |
6151a90f | 995 | ret = relayd_version_check(rsock); |
67d5aa28 | 996 | if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) { |
9a654598 | 997 | status = LTTNG_ERR_RELAYD_VERSION_FAIL; |
67d5aa28 JD |
998 | goto close_sock; |
999 | } else if (ret < 0) { | |
1000 | ERR("Unable to reach lttng-relayd"); | |
9a654598 | 1001 | status = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
2f77fc4b DG |
1002 | goto close_sock; |
1003 | } | |
b31610f2 JD |
1004 | consumer->relay_major_version = rsock->major; |
1005 | consumer->relay_minor_version = rsock->minor; | |
2f77fc4b DG |
1006 | } else if (uri->stype == LTTNG_STREAM_DATA) { |
1007 | DBG3("Creating relayd data socket from URI"); | |
1008 | } else { | |
1009 | /* Command is not valid */ | |
1010 | ERR("Relayd invalid stream type: %d", uri->stype); | |
9a654598 | 1011 | status = LTTNG_ERR_INVALID; |
2f77fc4b DG |
1012 | goto close_sock; |
1013 | } | |
1014 | ||
6151a90f | 1015 | *relayd_sock = rsock; |
2f77fc4b | 1016 | |
9a654598 | 1017 | return status; |
2f77fc4b DG |
1018 | |
1019 | close_sock: | |
6151a90f JD |
1020 | /* The returned value is not useful since we are on an error path. */ |
1021 | (void) relayd_close(rsock); | |
2f77fc4b | 1022 | free_sock: |
6151a90f | 1023 | free(rsock); |
2f77fc4b | 1024 | error: |
9a654598 | 1025 | return status; |
2f77fc4b DG |
1026 | } |
1027 | ||
1028 | /* | |
1029 | * Connect to the relayd using URI and send the socket to the right consumer. | |
43fade62 JG |
1030 | * |
1031 | * The consumer socket lock must be held by the caller. | |
9a654598 JG |
1032 | * |
1033 | * Returns LTTNG_OK on success or an LTTng error code on failure. | |
2f77fc4b | 1034 | */ |
9a654598 JG |
1035 | static enum lttng_error_code send_consumer_relayd_socket( |
1036 | unsigned int session_id, | |
3044f922 | 1037 | struct lttng_uri *relayd_uri, |
56a37563 | 1038 | struct consumer_output *consumer, |
d3e2ba59 | 1039 | struct consumer_socket *consumer_sock, |
fb9a95c4 | 1040 | const char *session_name, const char *hostname, |
6fa5fe7c | 1041 | const char *base_path, int session_live_timer, |
db1da059 JG |
1042 | const uint64_t *current_chunk_id, |
1043 | time_t session_creation_time) | |
2f77fc4b DG |
1044 | { |
1045 | int ret; | |
6151a90f | 1046 | struct lttcomm_relayd_sock *rsock = NULL; |
9a654598 | 1047 | enum lttng_error_code status; |
2f77fc4b | 1048 | |
ffe60014 | 1049 | /* Connect to relayd and make version check if uri is the control. */ |
9a654598 JG |
1050 | status = create_connect_relayd(relayd_uri, &rsock, consumer); |
1051 | if (status != LTTNG_OK) { | |
9e218353 | 1052 | goto relayd_comm_error; |
ffe60014 | 1053 | } |
6151a90f | 1054 | assert(rsock); |
ffe60014 | 1055 | |
2f77fc4b | 1056 | /* Set the network sequence index if not set. */ |
d88aee68 DG |
1057 | if (consumer->net_seq_index == (uint64_t) -1ULL) { |
1058 | pthread_mutex_lock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
1059 | /* |
1060 | * Increment net_seq_idx because we are about to transfer the | |
1061 | * new relayd socket to the consumer. | |
d88aee68 | 1062 | * Assign unique key so the consumer can match streams. |
2f77fc4b | 1063 | */ |
d88aee68 DG |
1064 | consumer->net_seq_index = ++relayd_net_seq_idx; |
1065 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
1066 | } |
1067 | ||
2f77fc4b | 1068 | /* Send relayd socket to consumer. */ |
6151a90f | 1069 | ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer, |
d3e2ba59 | 1070 | relayd_uri->stype, session_id, |
6fa5fe7c MD |
1071 | session_name, hostname, base_path, |
1072 | session_live_timer, current_chunk_id, | |
1073 | session_creation_time); | |
2f77fc4b | 1074 | if (ret < 0) { |
9a654598 | 1075 | status = LTTNG_ERR_ENABLE_CONSUMER_FAIL; |
2f77fc4b DG |
1076 | goto close_sock; |
1077 | } | |
1078 | ||
c890b720 DG |
1079 | /* Flag that the corresponding socket was sent. */ |
1080 | if (relayd_uri->stype == LTTNG_STREAM_CONTROL) { | |
ffe60014 | 1081 | consumer_sock->control_sock_sent = 1; |
c890b720 | 1082 | } else if (relayd_uri->stype == LTTNG_STREAM_DATA) { |
ffe60014 | 1083 | consumer_sock->data_sock_sent = 1; |
c890b720 DG |
1084 | } |
1085 | ||
2f77fc4b DG |
1086 | /* |
1087 | * Close socket which was dup on the consumer side. The session daemon does | |
1088 | * NOT keep track of the relayd socket(s) once transfer to the consumer. | |
1089 | */ | |
1090 | ||
1091 | close_sock: | |
9a654598 | 1092 | if (status != LTTNG_OK) { |
ffe60014 | 1093 | /* |
d9078d0c DG |
1094 | * The consumer output for this session should not be used anymore |
1095 | * since the relayd connection failed thus making any tracing or/and | |
1096 | * streaming not usable. | |
ffe60014 | 1097 | */ |
d9078d0c | 1098 | consumer->enabled = 0; |
ffe60014 | 1099 | } |
9e218353 JR |
1100 | (void) relayd_close(rsock); |
1101 | free(rsock); | |
1102 | ||
1103 | relayd_comm_error: | |
9a654598 | 1104 | return status; |
2f77fc4b DG |
1105 | } |
1106 | ||
1107 | /* | |
1108 | * Send both relayd sockets to a specific consumer and domain. This is a | |
1109 | * helper function to facilitate sending the information to the consumer for a | |
1110 | * session. | |
43fade62 JG |
1111 | * |
1112 | * The consumer socket lock must be held by the caller. | |
9a654598 JG |
1113 | * |
1114 | * Returns LTTNG_OK, or an LTTng error code on failure. | |
2f77fc4b | 1115 | */ |
9a654598 JG |
1116 | static enum lttng_error_code send_consumer_relayd_sockets( |
1117 | enum lttng_domain_type domain, | |
56a37563 | 1118 | unsigned int session_id, struct consumer_output *consumer, |
fb9a95c4 | 1119 | struct consumer_socket *sock, const char *session_name, |
6fa5fe7c | 1120 | const char *hostname, const char *base_path, int session_live_timer, |
db1da059 | 1121 | const uint64_t *current_chunk_id, time_t session_creation_time) |
2f77fc4b | 1122 | { |
9a654598 | 1123 | enum lttng_error_code status = LTTNG_OK; |
2f77fc4b | 1124 | |
2f77fc4b | 1125 | assert(consumer); |
6dc3064a | 1126 | assert(sock); |
2f77fc4b | 1127 | |
2f77fc4b | 1128 | /* Sending control relayd socket. */ |
ffe60014 | 1129 | if (!sock->control_sock_sent) { |
9a654598 | 1130 | status = send_consumer_relayd_socket(session_id, |
d3e2ba59 | 1131 | &consumer->dst.net.control, consumer, sock, |
6fa5fe7c | 1132 | session_name, hostname, base_path, session_live_timer, |
db1da059 | 1133 | current_chunk_id, session_creation_time); |
9a654598 | 1134 | if (status != LTTNG_OK) { |
c890b720 DG |
1135 | goto error; |
1136 | } | |
2f77fc4b DG |
1137 | } |
1138 | ||
1139 | /* Sending data relayd socket. */ | |
ffe60014 | 1140 | if (!sock->data_sock_sent) { |
9a654598 | 1141 | status = send_consumer_relayd_socket(session_id, |
d3e2ba59 | 1142 | &consumer->dst.net.data, consumer, sock, |
6fa5fe7c | 1143 | session_name, hostname, base_path, session_live_timer, |
db1da059 | 1144 | current_chunk_id, session_creation_time); |
9a654598 | 1145 | if (status != LTTNG_OK) { |
c890b720 DG |
1146 | goto error; |
1147 | } | |
2f77fc4b DG |
1148 | } |
1149 | ||
2f77fc4b | 1150 | error: |
9a654598 | 1151 | return status; |
2f77fc4b DG |
1152 | } |
1153 | ||
1154 | /* | |
1155 | * Setup relayd connections for a tracing session. First creates the socket to | |
1156 | * the relayd and send them to the right domain consumer. Consumer type MUST be | |
1157 | * network. | |
1158 | */ | |
ffe60014 | 1159 | int cmd_setup_relayd(struct ltt_session *session) |
2f77fc4b | 1160 | { |
f73fabfd | 1161 | int ret = LTTNG_OK; |
2f77fc4b DG |
1162 | struct ltt_ust_session *usess; |
1163 | struct ltt_kernel_session *ksess; | |
1164 | struct consumer_socket *socket; | |
1165 | struct lttng_ht_iter iter; | |
1e791a74 | 1166 | LTTNG_OPTIONAL(uint64_t) current_chunk_id = {}; |
2f77fc4b | 1167 | |
1e791a74 | 1168 | assert(session); |
2f77fc4b DG |
1169 | |
1170 | usess = session->ust_session; | |
1171 | ksess = session->kernel_session; | |
1172 | ||
785d2d0d | 1173 | DBG("Setting relayd for session %s", session->name); |
2f77fc4b | 1174 | |
1e791a74 JG |
1175 | if (session->current_trace_chunk) { |
1176 | enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id( | |
1177 | session->current_trace_chunk, ¤t_chunk_id.value); | |
1178 | ||
1179 | if (status == LTTNG_TRACE_CHUNK_STATUS_OK) { | |
1180 | current_chunk_id.is_set = true; | |
1181 | } else { | |
1182 | ERR("Failed to get current trace chunk id"); | |
1183 | ret = LTTNG_ERR_UNK; | |
1184 | goto error; | |
1185 | } | |
1186 | } | |
1187 | ||
e7fe706f DG |
1188 | rcu_read_lock(); |
1189 | ||
2f77fc4b DG |
1190 | if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET |
1191 | && usess->consumer->enabled) { | |
1192 | /* For each consumer socket, send relayd sockets */ | |
1193 | cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter, | |
1194 | socket, node.node) { | |
2f77fc4b | 1195 | pthread_mutex_lock(socket->lock); |
6dc3064a | 1196 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id, |
d3e2ba59 JD |
1197 | usess->consumer, socket, |
1198 | session->name, session->hostname, | |
6fa5fe7c | 1199 | session->base_path, |
1e791a74 | 1200 | session->live_timer, |
db1da059 JG |
1201 | current_chunk_id.is_set ? ¤t_chunk_id.value : NULL, |
1202 | session->creation_time); | |
2f77fc4b | 1203 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 1204 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1205 | goto error; |
1206 | } | |
6dc3064a DG |
1207 | /* Session is now ready for network streaming. */ |
1208 | session->net_handle = 1; | |
2f77fc4b | 1209 | } |
b31610f2 JD |
1210 | session->consumer->relay_major_version = |
1211 | usess->consumer->relay_major_version; | |
1212 | session->consumer->relay_minor_version = | |
1213 | usess->consumer->relay_minor_version; | |
2f77fc4b DG |
1214 | } |
1215 | ||
1216 | if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET | |
1217 | && ksess->consumer->enabled) { | |
1218 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, | |
1219 | socket, node.node) { | |
2f77fc4b | 1220 | pthread_mutex_lock(socket->lock); |
6dc3064a | 1221 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id, |
d3e2ba59 JD |
1222 | ksess->consumer, socket, |
1223 | session->name, session->hostname, | |
6fa5fe7c | 1224 | session->base_path, |
1e791a74 | 1225 | session->live_timer, |
db1da059 JG |
1226 | current_chunk_id.is_set ? ¤t_chunk_id.value : NULL, |
1227 | session->creation_time); | |
2f77fc4b | 1228 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 1229 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1230 | goto error; |
1231 | } | |
6dc3064a DG |
1232 | /* Session is now ready for network streaming. */ |
1233 | session->net_handle = 1; | |
2f77fc4b | 1234 | } |
b31610f2 JD |
1235 | session->consumer->relay_major_version = |
1236 | ksess->consumer->relay_major_version; | |
1237 | session->consumer->relay_minor_version = | |
1238 | ksess->consumer->relay_minor_version; | |
2f77fc4b DG |
1239 | } |
1240 | ||
1241 | error: | |
e7fe706f | 1242 | rcu_read_unlock(); |
2f77fc4b DG |
1243 | return ret; |
1244 | } | |
1245 | ||
9b6c7ec5 DG |
1246 | /* |
1247 | * Start a kernel session by opening all necessary streams. | |
1248 | */ | |
1249 | static int start_kernel_session(struct ltt_kernel_session *ksess, int wpipe) | |
1250 | { | |
1251 | int ret; | |
1252 | struct ltt_kernel_channel *kchan; | |
1253 | ||
1254 | /* Open kernel metadata */ | |
07b86b52 | 1255 | if (ksess->metadata == NULL && ksess->output_traces) { |
9b6c7ec5 DG |
1256 | ret = kernel_open_metadata(ksess); |
1257 | if (ret < 0) { | |
1258 | ret = LTTNG_ERR_KERN_META_FAIL; | |
1259 | goto error; | |
1260 | } | |
1261 | } | |
1262 | ||
1263 | /* Open kernel metadata stream */ | |
07b86b52 | 1264 | if (ksess->metadata && ksess->metadata_stream_fd < 0) { |
9b6c7ec5 DG |
1265 | ret = kernel_open_metadata_stream(ksess); |
1266 | if (ret < 0) { | |
1267 | ERR("Kernel create metadata stream failed"); | |
1268 | ret = LTTNG_ERR_KERN_STREAM_FAIL; | |
1269 | goto error; | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | /* For each channel */ | |
1274 | cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) { | |
1275 | if (kchan->stream_count == 0) { | |
1276 | ret = kernel_open_channel_stream(kchan); | |
1277 | if (ret < 0) { | |
1278 | ret = LTTNG_ERR_KERN_STREAM_FAIL; | |
1279 | goto error; | |
1280 | } | |
1281 | /* Update the stream global counter */ | |
1282 | ksess->stream_count_global += ret; | |
1283 | } | |
1284 | } | |
1285 | ||
1286 | /* Setup kernel consumer socket and send fds to it */ | |
1287 | ret = init_kernel_tracing(ksess); | |
e43c41c5 | 1288 | if (ret != 0) { |
9b6c7ec5 DG |
1289 | ret = LTTNG_ERR_KERN_START_FAIL; |
1290 | goto error; | |
1291 | } | |
1292 | ||
1293 | /* This start the kernel tracing */ | |
1294 | ret = kernel_start_session(ksess); | |
1295 | if (ret < 0) { | |
1296 | ret = LTTNG_ERR_KERN_START_FAIL; | |
1297 | goto error; | |
1298 | } | |
1299 | ||
1300 | /* Quiescent wait after starting trace */ | |
462ae7ae | 1301 | kernel_wait_quiescent(wpipe); |
9b6c7ec5 | 1302 | |
14fb1ebe | 1303 | ksess->active = 1; |
9b6c7ec5 DG |
1304 | |
1305 | ret = LTTNG_OK; | |
1306 | ||
1307 | error: | |
1308 | return ret; | |
1309 | } | |
1310 | ||
2f77fc4b DG |
1311 | /* |
1312 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. | |
1313 | */ | |
56a37563 JG |
1314 | int cmd_disable_channel(struct ltt_session *session, |
1315 | enum lttng_domain_type domain, char *channel_name) | |
2f77fc4b DG |
1316 | { |
1317 | int ret; | |
1318 | struct ltt_ust_session *usess; | |
1319 | ||
1320 | usess = session->ust_session; | |
1321 | ||
2223c96f DG |
1322 | rcu_read_lock(); |
1323 | ||
2f77fc4b DG |
1324 | switch (domain) { |
1325 | case LTTNG_DOMAIN_KERNEL: | |
1326 | { | |
1327 | ret = channel_kernel_disable(session->kernel_session, | |
1328 | channel_name); | |
f73fabfd | 1329 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1330 | goto error; |
1331 | } | |
1332 | ||
1333 | kernel_wait_quiescent(kernel_tracer_fd); | |
1334 | break; | |
1335 | } | |
1336 | case LTTNG_DOMAIN_UST: | |
1337 | { | |
1338 | struct ltt_ust_channel *uchan; | |
1339 | struct lttng_ht *chan_ht; | |
1340 | ||
1341 | chan_ht = usess->domain_global.channels; | |
1342 | ||
1343 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); | |
1344 | if (uchan == NULL) { | |
f73fabfd | 1345 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2f77fc4b DG |
1346 | goto error; |
1347 | } | |
1348 | ||
7972aab2 | 1349 | ret = channel_ust_disable(usess, uchan); |
f73fabfd | 1350 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1351 | goto error; |
1352 | } | |
1353 | break; | |
1354 | } | |
2f77fc4b | 1355 | default: |
f73fabfd | 1356 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
1357 | goto error; |
1358 | } | |
1359 | ||
f73fabfd | 1360 | ret = LTTNG_OK; |
2f77fc4b DG |
1361 | |
1362 | error: | |
2223c96f | 1363 | rcu_read_unlock(); |
2f77fc4b DG |
1364 | return ret; |
1365 | } | |
1366 | ||
ccf10263 MD |
1367 | /* |
1368 | * Command LTTNG_TRACK_PID processed by the client thread. | |
a9ad0c8f MD |
1369 | * |
1370 | * Called with session lock held. | |
ccf10263 | 1371 | */ |
56a37563 JG |
1372 | int cmd_track_pid(struct ltt_session *session, enum lttng_domain_type domain, |
1373 | int pid) | |
ccf10263 MD |
1374 | { |
1375 | int ret; | |
1376 | ||
1377 | rcu_read_lock(); | |
1378 | ||
1379 | switch (domain) { | |
1380 | case LTTNG_DOMAIN_KERNEL: | |
1381 | { | |
1382 | struct ltt_kernel_session *ksess; | |
1383 | ||
1384 | ksess = session->kernel_session; | |
1385 | ||
1386 | ret = kernel_track_pid(ksess, pid); | |
1387 | if (ret != LTTNG_OK) { | |
1388 | goto error; | |
1389 | } | |
1390 | ||
1391 | kernel_wait_quiescent(kernel_tracer_fd); | |
1392 | break; | |
1393 | } | |
ccf10263 | 1394 | case LTTNG_DOMAIN_UST: |
a9ad0c8f MD |
1395 | { |
1396 | struct ltt_ust_session *usess; | |
1397 | ||
1398 | usess = session->ust_session; | |
1399 | ||
1400 | ret = trace_ust_track_pid(usess, pid); | |
1401 | if (ret != LTTNG_OK) { | |
1402 | goto error; | |
1403 | } | |
1404 | break; | |
1405 | } | |
ccf10263 MD |
1406 | default: |
1407 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
1408 | goto error; | |
1409 | } | |
1410 | ||
1411 | ret = LTTNG_OK; | |
1412 | ||
1413 | error: | |
1414 | rcu_read_unlock(); | |
1415 | return ret; | |
1416 | } | |
1417 | ||
1418 | /* | |
1419 | * Command LTTNG_UNTRACK_PID processed by the client thread. | |
a9ad0c8f MD |
1420 | * |
1421 | * Called with session lock held. | |
ccf10263 | 1422 | */ |
56a37563 JG |
1423 | int cmd_untrack_pid(struct ltt_session *session, enum lttng_domain_type domain, |
1424 | int pid) | |
ccf10263 MD |
1425 | { |
1426 | int ret; | |
1427 | ||
1428 | rcu_read_lock(); | |
1429 | ||
1430 | switch (domain) { | |
1431 | case LTTNG_DOMAIN_KERNEL: | |
1432 | { | |
1433 | struct ltt_kernel_session *ksess; | |
1434 | ||
1435 | ksess = session->kernel_session; | |
1436 | ||
1437 | ret = kernel_untrack_pid(ksess, pid); | |
1438 | if (ret != LTTNG_OK) { | |
1439 | goto error; | |
1440 | } | |
1441 | ||
1442 | kernel_wait_quiescent(kernel_tracer_fd); | |
1443 | break; | |
1444 | } | |
ccf10263 | 1445 | case LTTNG_DOMAIN_UST: |
a9ad0c8f MD |
1446 | { |
1447 | struct ltt_ust_session *usess; | |
1448 | ||
1449 | usess = session->ust_session; | |
1450 | ||
1451 | ret = trace_ust_untrack_pid(usess, pid); | |
1452 | if (ret != LTTNG_OK) { | |
1453 | goto error; | |
1454 | } | |
1455 | break; | |
1456 | } | |
ccf10263 MD |
1457 | default: |
1458 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
1459 | goto error; | |
1460 | } | |
1461 | ||
1462 | ret = LTTNG_OK; | |
1463 | ||
1464 | error: | |
1465 | rcu_read_unlock(); | |
1466 | return ret; | |
1467 | } | |
1468 | ||
2f77fc4b DG |
1469 | /* |
1470 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. | |
1471 | * | |
1472 | * The wpipe arguments is used as a notifier for the kernel thread. | |
1473 | */ | |
1474 | int cmd_enable_channel(struct ltt_session *session, | |
7972aab2 | 1475 | struct lttng_domain *domain, struct lttng_channel *attr, int wpipe) |
2f77fc4b DG |
1476 | { |
1477 | int ret; | |
1478 | struct ltt_ust_session *usess = session->ust_session; | |
1479 | struct lttng_ht *chan_ht; | |
1f345e94 | 1480 | size_t len; |
2f77fc4b DG |
1481 | |
1482 | assert(session); | |
1483 | assert(attr); | |
7972aab2 | 1484 | assert(domain); |
2f77fc4b | 1485 | |
f5436bfc | 1486 | len = lttng_strnlen(attr->name, sizeof(attr->name)); |
1f345e94 PP |
1487 | |
1488 | /* Validate channel name */ | |
1489 | if (attr->name[0] == '.' || | |
1490 | memchr(attr->name, '/', len) != NULL) { | |
1491 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1492 | goto end; | |
1493 | } | |
1494 | ||
2f77fc4b DG |
1495 | DBG("Enabling channel %s for session %s", attr->name, session->name); |
1496 | ||
03b4fdcf DG |
1497 | rcu_read_lock(); |
1498 | ||
ecc48a90 JD |
1499 | /* |
1500 | * Don't try to enable a channel if the session has been started at | |
1501 | * some point in time before. The tracer does not allow it. | |
1502 | */ | |
8382cf6f | 1503 | if (session->has_been_started) { |
ecc48a90 JD |
1504 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
1505 | goto error; | |
1506 | } | |
1507 | ||
1508 | /* | |
1509 | * If the session is a live session, remove the switch timer, the | |
1510 | * live timer does the same thing but sends also synchronisation | |
1511 | * beacons for inactive streams. | |
1512 | */ | |
1513 | if (session->live_timer > 0) { | |
1514 | attr->attr.live_timer_interval = session->live_timer; | |
1515 | attr->attr.switch_timer_interval = 0; | |
1516 | } | |
1517 | ||
6e21424e JR |
1518 | /* Check for feature support */ |
1519 | switch (domain->type) { | |
1520 | case LTTNG_DOMAIN_KERNEL: | |
1521 | { | |
1522 | if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd) != 1) { | |
1523 | /* Sampling position of buffer is not supported */ | |
1524 | WARN("Kernel tracer does not support buffer monitoring. " | |
1525 | "Setting the monitor interval timer to 0 " | |
1526 | "(disabled) for channel '%s' of session '%s'", | |
1527 | attr-> name, session->name); | |
1528 | lttng_channel_set_monitor_timer_interval(attr, 0); | |
1529 | } | |
1530 | break; | |
1531 | } | |
1532 | case LTTNG_DOMAIN_UST: | |
f28f9e44 | 1533 | break; |
6e21424e JR |
1534 | case LTTNG_DOMAIN_JUL: |
1535 | case LTTNG_DOMAIN_LOG4J: | |
1536 | case LTTNG_DOMAIN_PYTHON: | |
f28f9e44 JG |
1537 | if (!agent_tracing_is_enabled()) { |
1538 | DBG("Attempted to enable a channel in an agent domain but the agent thread is not running"); | |
1539 | ret = LTTNG_ERR_AGENT_TRACING_DISABLED; | |
1540 | goto error; | |
1541 | } | |
6e21424e JR |
1542 | break; |
1543 | default: | |
1544 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
1545 | goto error; | |
1546 | } | |
1547 | ||
7972aab2 | 1548 | switch (domain->type) { |
2f77fc4b DG |
1549 | case LTTNG_DOMAIN_KERNEL: |
1550 | { | |
1551 | struct ltt_kernel_channel *kchan; | |
1552 | ||
2f77fc4b DG |
1553 | kchan = trace_kernel_get_channel_by_name(attr->name, |
1554 | session->kernel_session); | |
1555 | if (kchan == NULL) { | |
54213acc JG |
1556 | if (session->snapshot.nb_output > 0 || |
1557 | session->snapshot_mode) { | |
1558 | /* Enforce mmap output for snapshot sessions. */ | |
1559 | attr->attr.output = LTTNG_EVENT_MMAP; | |
1560 | } | |
2f77fc4b | 1561 | ret = channel_kernel_create(session->kernel_session, attr, wpipe); |
85076754 MD |
1562 | if (attr->name[0] != '\0') { |
1563 | session->kernel_session->has_non_default_channel = 1; | |
1564 | } | |
2f77fc4b DG |
1565 | } else { |
1566 | ret = channel_kernel_enable(session->kernel_session, kchan); | |
1567 | } | |
1568 | ||
f73fabfd | 1569 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1570 | goto error; |
1571 | } | |
1572 | ||
784303b0 | 1573 | kernel_wait_quiescent(kernel_tracer_fd); |
2f77fc4b DG |
1574 | break; |
1575 | } | |
1576 | case LTTNG_DOMAIN_UST: | |
9232818f JG |
1577 | case LTTNG_DOMAIN_JUL: |
1578 | case LTTNG_DOMAIN_LOG4J: | |
1579 | case LTTNG_DOMAIN_PYTHON: | |
2f77fc4b DG |
1580 | { |
1581 | struct ltt_ust_channel *uchan; | |
1582 | ||
9232818f JG |
1583 | /* |
1584 | * FIXME | |
1585 | * | |
1586 | * Current agent implementation limitations force us to allow | |
1587 | * only one channel at once in "agent" subdomains. Each | |
1588 | * subdomain has a default channel name which must be strictly | |
1589 | * adhered to. | |
1590 | */ | |
1591 | if (domain->type == LTTNG_DOMAIN_JUL) { | |
1592 | if (strncmp(attr->name, DEFAULT_JUL_CHANNEL_NAME, | |
1593 | LTTNG_SYMBOL_NAME_LEN)) { | |
1594 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1595 | goto error; | |
1596 | } | |
1597 | } else if (domain->type == LTTNG_DOMAIN_LOG4J) { | |
1598 | if (strncmp(attr->name, DEFAULT_LOG4J_CHANNEL_NAME, | |
1599 | LTTNG_SYMBOL_NAME_LEN)) { | |
1600 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1601 | goto error; | |
1602 | } | |
1603 | } else if (domain->type == LTTNG_DOMAIN_PYTHON) { | |
1604 | if (strncmp(attr->name, DEFAULT_PYTHON_CHANNEL_NAME, | |
1605 | LTTNG_SYMBOL_NAME_LEN)) { | |
1606 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1607 | goto error; | |
1608 | } | |
1609 | } | |
1610 | ||
2f77fc4b DG |
1611 | chan_ht = usess->domain_global.channels; |
1612 | ||
1613 | uchan = trace_ust_find_channel_by_name(chan_ht, attr->name); | |
1614 | if (uchan == NULL) { | |
7972aab2 | 1615 | ret = channel_ust_create(usess, attr, domain->buf_type); |
85076754 MD |
1616 | if (attr->name[0] != '\0') { |
1617 | usess->has_non_default_channel = 1; | |
1618 | } | |
2f77fc4b | 1619 | } else { |
7972aab2 | 1620 | ret = channel_ust_enable(usess, uchan); |
2f77fc4b DG |
1621 | } |
1622 | break; | |
1623 | } | |
2f77fc4b | 1624 | default: |
f73fabfd | 1625 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
1626 | goto error; |
1627 | } | |
1628 | ||
54213acc JG |
1629 | if (ret == LTTNG_OK && attr->attr.output != LTTNG_EVENT_MMAP) { |
1630 | session->has_non_mmap_channel = true; | |
1631 | } | |
2f77fc4b | 1632 | error: |
2223c96f | 1633 | rcu_read_unlock(); |
1f345e94 | 1634 | end: |
2f77fc4b DG |
1635 | return ret; |
1636 | } | |
1637 | ||
2f77fc4b DG |
1638 | /* |
1639 | * Command LTTNG_DISABLE_EVENT processed by the client thread. | |
1640 | */ | |
56a37563 JG |
1641 | int cmd_disable_event(struct ltt_session *session, |
1642 | enum lttng_domain_type domain, char *channel_name, | |
6e911cad | 1643 | struct lttng_event *event) |
2f77fc4b DG |
1644 | { |
1645 | int ret; | |
6e911cad MD |
1646 | char *event_name; |
1647 | ||
18a720cd MD |
1648 | DBG("Disable event command for event \'%s\'", event->name); |
1649 | ||
6e911cad MD |
1650 | event_name = event->name; |
1651 | ||
9b7431cf JG |
1652 | /* Error out on unhandled search criteria */ |
1653 | if (event->loglevel_type || event->loglevel != -1 || event->enabled | |
6e911cad | 1654 | || event->pid || event->filter || event->exclusion) { |
7076b56e JG |
1655 | ret = LTTNG_ERR_UNK; |
1656 | goto error; | |
6e911cad | 1657 | } |
2f77fc4b | 1658 | |
2223c96f DG |
1659 | rcu_read_lock(); |
1660 | ||
2f77fc4b DG |
1661 | switch (domain) { |
1662 | case LTTNG_DOMAIN_KERNEL: | |
1663 | { | |
1664 | struct ltt_kernel_channel *kchan; | |
1665 | struct ltt_kernel_session *ksess; | |
1666 | ||
1667 | ksess = session->kernel_session; | |
1668 | ||
85076754 MD |
1669 | /* |
1670 | * If a non-default channel has been created in the | |
1671 | * session, explicitely require that -c chan_name needs | |
1672 | * to be provided. | |
1673 | */ | |
1674 | if (ksess->has_non_default_channel && channel_name[0] == '\0') { | |
1675 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
7076b56e | 1676 | goto error_unlock; |
85076754 MD |
1677 | } |
1678 | ||
2f77fc4b DG |
1679 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); |
1680 | if (kchan == NULL) { | |
f73fabfd | 1681 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
7076b56e | 1682 | goto error_unlock; |
2f77fc4b DG |
1683 | } |
1684 | ||
6e911cad MD |
1685 | switch (event->type) { |
1686 | case LTTNG_EVENT_ALL: | |
9550ee81 | 1687 | case LTTNG_EVENT_TRACEPOINT: |
d0ae4ea8 | 1688 | case LTTNG_EVENT_SYSCALL: |
9550ee81 JR |
1689 | case LTTNG_EVENT_PROBE: |
1690 | case LTTNG_EVENT_FUNCTION: | |
1691 | case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */ | |
1692 | if (event_name[0] == '\0') { | |
1693 | ret = event_kernel_disable_event(kchan, | |
1694 | NULL, event->type); | |
29c62722 | 1695 | } else { |
d0ae4ea8 | 1696 | ret = event_kernel_disable_event(kchan, |
9550ee81 | 1697 | event_name, event->type); |
29c62722 | 1698 | } |
6e911cad | 1699 | if (ret != LTTNG_OK) { |
7076b56e | 1700 | goto error_unlock; |
6e911cad MD |
1701 | } |
1702 | break; | |
6e911cad MD |
1703 | default: |
1704 | ret = LTTNG_ERR_UNK; | |
7076b56e | 1705 | goto error_unlock; |
2f77fc4b DG |
1706 | } |
1707 | ||
1708 | kernel_wait_quiescent(kernel_tracer_fd); | |
1709 | break; | |
1710 | } | |
1711 | case LTTNG_DOMAIN_UST: | |
1712 | { | |
1713 | struct ltt_ust_channel *uchan; | |
1714 | struct ltt_ust_session *usess; | |
1715 | ||
1716 | usess = session->ust_session; | |
1717 | ||
7076b56e JG |
1718 | if (validate_ust_event_name(event_name)) { |
1719 | ret = LTTNG_ERR_INVALID_EVENT_NAME; | |
1720 | goto error_unlock; | |
1721 | } | |
1722 | ||
85076754 MD |
1723 | /* |
1724 | * If a non-default channel has been created in the | |
9550ee81 | 1725 | * session, explicitly require that -c chan_name needs |
85076754 MD |
1726 | * to be provided. |
1727 | */ | |
1728 | if (usess->has_non_default_channel && channel_name[0] == '\0') { | |
1729 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
7076b56e | 1730 | goto error_unlock; |
85076754 MD |
1731 | } |
1732 | ||
2f77fc4b DG |
1733 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
1734 | channel_name); | |
1735 | if (uchan == NULL) { | |
f73fabfd | 1736 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
7076b56e | 1737 | goto error_unlock; |
2f77fc4b DG |
1738 | } |
1739 | ||
6e911cad MD |
1740 | switch (event->type) { |
1741 | case LTTNG_EVENT_ALL: | |
b3639870 JR |
1742 | /* |
1743 | * An empty event name means that everything | |
1744 | * should be disabled. | |
1745 | */ | |
1746 | if (event->name[0] == '\0') { | |
1747 | ret = event_ust_disable_all_tracepoints(usess, uchan); | |
77d536b2 JR |
1748 | } else { |
1749 | ret = event_ust_disable_tracepoint(usess, uchan, | |
1750 | event_name); | |
1751 | } | |
6e911cad | 1752 | if (ret != LTTNG_OK) { |
7076b56e | 1753 | goto error_unlock; |
6e911cad MD |
1754 | } |
1755 | break; | |
1756 | default: | |
1757 | ret = LTTNG_ERR_UNK; | |
7076b56e | 1758 | goto error_unlock; |
2f77fc4b DG |
1759 | } |
1760 | ||
1761 | DBG3("Disable UST event %s in channel %s completed", event_name, | |
1762 | channel_name); | |
1763 | break; | |
1764 | } | |
5cdb6027 | 1765 | case LTTNG_DOMAIN_LOG4J: |
f20baf8e | 1766 | case LTTNG_DOMAIN_JUL: |
0e115563 | 1767 | case LTTNG_DOMAIN_PYTHON: |
f20baf8e | 1768 | { |
fefd409b | 1769 | struct agent *agt; |
f20baf8e DG |
1770 | struct ltt_ust_session *usess = session->ust_session; |
1771 | ||
1772 | assert(usess); | |
1773 | ||
6e911cad MD |
1774 | switch (event->type) { |
1775 | case LTTNG_EVENT_ALL: | |
1776 | break; | |
1777 | default: | |
1778 | ret = LTTNG_ERR_UNK; | |
7076b56e | 1779 | goto error_unlock; |
6e911cad MD |
1780 | } |
1781 | ||
5cdb6027 | 1782 | agt = trace_ust_find_agent(usess, domain); |
fefd409b DG |
1783 | if (!agt) { |
1784 | ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND; | |
7076b56e | 1785 | goto error_unlock; |
fefd409b | 1786 | } |
b3639870 JR |
1787 | /* |
1788 | * An empty event name means that everything | |
1789 | * should be disabled. | |
1790 | */ | |
1791 | if (event->name[0] == '\0') { | |
18a720cd MD |
1792 | ret = event_agent_disable_all(usess, agt); |
1793 | } else { | |
1794 | ret = event_agent_disable(usess, agt, event_name); | |
1795 | } | |
f20baf8e | 1796 | if (ret != LTTNG_OK) { |
7076b56e | 1797 | goto error_unlock; |
f20baf8e DG |
1798 | } |
1799 | ||
1800 | break; | |
1801 | } | |
2f77fc4b | 1802 | default: |
f73fabfd | 1803 | ret = LTTNG_ERR_UND; |
7076b56e | 1804 | goto error_unlock; |
2f77fc4b DG |
1805 | } |
1806 | ||
f73fabfd | 1807 | ret = LTTNG_OK; |
2f77fc4b | 1808 | |
7076b56e | 1809 | error_unlock: |
2223c96f | 1810 | rcu_read_unlock(); |
7076b56e | 1811 | error: |
2f77fc4b DG |
1812 | return ret; |
1813 | } | |
1814 | ||
2f77fc4b DG |
1815 | /* |
1816 | * Command LTTNG_ADD_CONTEXT processed by the client thread. | |
1817 | */ | |
56a37563 | 1818 | int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain, |
601d5acf | 1819 | char *channel_name, struct lttng_event_context *ctx, int kwpipe) |
2f77fc4b | 1820 | { |
d5979e4a | 1821 | int ret, chan_kern_created = 0, chan_ust_created = 0; |
bdf64013 JG |
1822 | char *app_ctx_provider_name = NULL, *app_ctx_name = NULL; |
1823 | ||
9a699f7b JR |
1824 | /* |
1825 | * Don't try to add a context if the session has been started at | |
1826 | * some point in time before. The tracer does not allow it and would | |
1827 | * result in a corrupted trace. | |
1828 | */ | |
1829 | if (session->has_been_started) { | |
1830 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; | |
1831 | goto end; | |
1832 | } | |
1833 | ||
bdf64013 JG |
1834 | if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { |
1835 | app_ctx_provider_name = ctx->u.app_ctx.provider_name; | |
1836 | app_ctx_name = ctx->u.app_ctx.ctx_name; | |
1837 | } | |
2f77fc4b DG |
1838 | |
1839 | switch (domain) { | |
1840 | case LTTNG_DOMAIN_KERNEL: | |
979e618e DG |
1841 | assert(session->kernel_session); |
1842 | ||
1843 | if (session->kernel_session->channel_count == 0) { | |
1844 | /* Create default channel */ | |
1845 | ret = channel_kernel_create(session->kernel_session, NULL, kwpipe); | |
1846 | if (ret != LTTNG_OK) { | |
1847 | goto error; | |
1848 | } | |
d5979e4a | 1849 | chan_kern_created = 1; |
979e618e | 1850 | } |
2f77fc4b | 1851 | /* Add kernel context to kernel tracer */ |
601d5acf | 1852 | ret = context_kernel_add(session->kernel_session, ctx, channel_name); |
f73fabfd | 1853 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1854 | goto error; |
1855 | } | |
1856 | break; | |
bdf64013 JG |
1857 | case LTTNG_DOMAIN_JUL: |
1858 | case LTTNG_DOMAIN_LOG4J: | |
1859 | { | |
1860 | /* | |
1861 | * Validate channel name. | |
1862 | * If no channel name is given and the domain is JUL or LOG4J, | |
1863 | * set it to the appropriate domain-specific channel name. If | |
1864 | * a name is provided but does not match the expexted channel | |
1865 | * name, return an error. | |
1866 | */ | |
1867 | if (domain == LTTNG_DOMAIN_JUL && *channel_name && | |
1868 | strcmp(channel_name, | |
1869 | DEFAULT_JUL_CHANNEL_NAME)) { | |
1870 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; | |
1871 | goto error; | |
1872 | } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name && | |
1873 | strcmp(channel_name, | |
1874 | DEFAULT_LOG4J_CHANNEL_NAME)) { | |
1875 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; | |
1876 | goto error; | |
1877 | } | |
a93b3916 | 1878 | /* break is _not_ missing here. */ |
bdf64013 | 1879 | } |
2f77fc4b DG |
1880 | case LTTNG_DOMAIN_UST: |
1881 | { | |
1882 | struct ltt_ust_session *usess = session->ust_session; | |
85076754 MD |
1883 | unsigned int chan_count; |
1884 | ||
2f77fc4b DG |
1885 | assert(usess); |
1886 | ||
85076754 | 1887 | chan_count = lttng_ht_get_count(usess->domain_global.channels); |
979e618e DG |
1888 | if (chan_count == 0) { |
1889 | struct lttng_channel *attr; | |
1890 | /* Create default channel */ | |
0a9c6494 | 1891 | attr = channel_new_default_attr(domain, usess->buffer_type); |
979e618e DG |
1892 | if (attr == NULL) { |
1893 | ret = LTTNG_ERR_FATAL; | |
1894 | goto error; | |
1895 | } | |
1896 | ||
7972aab2 | 1897 | ret = channel_ust_create(usess, attr, usess->buffer_type); |
979e618e DG |
1898 | if (ret != LTTNG_OK) { |
1899 | free(attr); | |
1900 | goto error; | |
1901 | } | |
cf0bcb51 | 1902 | channel_attr_destroy(attr); |
d5979e4a | 1903 | chan_ust_created = 1; |
979e618e DG |
1904 | } |
1905 | ||
601d5acf | 1906 | ret = context_ust_add(usess, domain, ctx, channel_name); |
bdf64013 JG |
1907 | free(app_ctx_provider_name); |
1908 | free(app_ctx_name); | |
1909 | app_ctx_name = NULL; | |
1910 | app_ctx_provider_name = NULL; | |
f73fabfd | 1911 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1912 | goto error; |
1913 | } | |
1914 | break; | |
1915 | } | |
2f77fc4b | 1916 | default: |
f73fabfd | 1917 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1918 | goto error; |
1919 | } | |
1920 | ||
bdf64013 JG |
1921 | ret = LTTNG_OK; |
1922 | goto end; | |
2f77fc4b DG |
1923 | |
1924 | error: | |
d5979e4a DG |
1925 | if (chan_kern_created) { |
1926 | struct ltt_kernel_channel *kchan = | |
1927 | trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME, | |
1928 | session->kernel_session); | |
1929 | /* Created previously, this should NOT fail. */ | |
1930 | assert(kchan); | |
1931 | kernel_destroy_channel(kchan); | |
1932 | } | |
1933 | ||
1934 | if (chan_ust_created) { | |
1935 | struct ltt_ust_channel *uchan = | |
1936 | trace_ust_find_channel_by_name( | |
1937 | session->ust_session->domain_global.channels, | |
1938 | DEFAULT_CHANNEL_NAME); | |
1939 | /* Created previously, this should NOT fail. */ | |
1940 | assert(uchan); | |
1941 | /* Remove from the channel list of the session. */ | |
1942 | trace_ust_delete_channel(session->ust_session->domain_global.channels, | |
1943 | uchan); | |
1944 | trace_ust_destroy_channel(uchan); | |
1945 | } | |
bdf64013 JG |
1946 | end: |
1947 | free(app_ctx_provider_name); | |
1948 | free(app_ctx_name); | |
2f77fc4b DG |
1949 | return ret; |
1950 | } | |
1951 | ||
dac8e046 JG |
1952 | static inline bool name_starts_with(const char *name, const char *prefix) |
1953 | { | |
1954 | const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN); | |
1955 | ||
1956 | return !strncmp(name, prefix, max_cmp_len); | |
1957 | } | |
1958 | ||
1959 | /* Perform userspace-specific event name validation */ | |
1960 | static int validate_ust_event_name(const char *name) | |
1961 | { | |
1962 | int ret = 0; | |
1963 | ||
1964 | if (!name) { | |
1965 | ret = -1; | |
1966 | goto end; | |
1967 | } | |
1968 | ||
1969 | /* | |
1970 | * Check name against all internal UST event component namespaces used | |
1971 | * by the agents. | |
1972 | */ | |
1973 | if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) || | |
1974 | name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) || | |
1975 | name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) { | |
1976 | ret = -1; | |
1977 | } | |
1978 | ||
1979 | end: | |
1980 | return ret; | |
1981 | } | |
88f06f15 | 1982 | |
2f77fc4b | 1983 | /* |
88f06f15 JG |
1984 | * Internal version of cmd_enable_event() with a supplemental |
1985 | * "internal_event" flag which is used to enable internal events which should | |
1986 | * be hidden from clients. Such events are used in the agent implementation to | |
1987 | * enable the events through which all "agent" events are funeled. | |
2f77fc4b | 1988 | */ |
88f06f15 JG |
1989 | static int _cmd_enable_event(struct ltt_session *session, |
1990 | struct lttng_domain *domain, | |
025faf73 | 1991 | char *channel_name, struct lttng_event *event, |
6b453b5e | 1992 | char *filter_expression, |
db8f1377 JI |
1993 | struct lttng_filter_bytecode *filter, |
1994 | struct lttng_event_exclusion *exclusion, | |
88f06f15 | 1995 | int wpipe, bool internal_event) |
2f77fc4b | 1996 | { |
9f449915 | 1997 | int ret = 0, channel_created = 0; |
cfedea03 | 1998 | struct lttng_channel *attr = NULL; |
2f77fc4b DG |
1999 | |
2000 | assert(session); | |
2001 | assert(event); | |
2002 | assert(channel_name); | |
2003 | ||
2a385866 JG |
2004 | /* If we have a filter, we must have its filter expression */ |
2005 | assert(!(!!filter_expression ^ !!filter)); | |
2006 | ||
9f449915 PP |
2007 | /* Normalize event name as a globbing pattern */ |
2008 | strutils_normalize_star_glob_pattern(event->name); | |
18a720cd | 2009 | |
9f449915 PP |
2010 | /* Normalize exclusion names as globbing patterns */ |
2011 | if (exclusion) { | |
2012 | size_t i; | |
f5ac4bd7 | 2013 | |
9f449915 PP |
2014 | for (i = 0; i < exclusion->count; i++) { |
2015 | char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i); | |
2016 | ||
2017 | strutils_normalize_star_glob_pattern(name); | |
2018 | } | |
930a2e99 JG |
2019 | } |
2020 | ||
9f449915 PP |
2021 | DBG("Enable event command for event \'%s\'", event->name); |
2022 | ||
2023 | rcu_read_lock(); | |
2024 | ||
7972aab2 | 2025 | switch (domain->type) { |
2f77fc4b DG |
2026 | case LTTNG_DOMAIN_KERNEL: |
2027 | { | |
2028 | struct ltt_kernel_channel *kchan; | |
2029 | ||
85076754 MD |
2030 | /* |
2031 | * If a non-default channel has been created in the | |
2032 | * session, explicitely require that -c chan_name needs | |
2033 | * to be provided. | |
2034 | */ | |
2035 | if (session->kernel_session->has_non_default_channel | |
2036 | && channel_name[0] == '\0') { | |
2037 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
2038 | goto error; | |
2039 | } | |
2040 | ||
2f77fc4b DG |
2041 | kchan = trace_kernel_get_channel_by_name(channel_name, |
2042 | session->kernel_session); | |
2043 | if (kchan == NULL) { | |
0a9c6494 DG |
2044 | attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, |
2045 | LTTNG_BUFFER_GLOBAL); | |
2f77fc4b | 2046 | if (attr == NULL) { |
f73fabfd | 2047 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2048 | goto error; |
2049 | } | |
04c17253 MD |
2050 | if (lttng_strncpy(attr->name, channel_name, |
2051 | sizeof(attr->name))) { | |
2052 | ret = LTTNG_ERR_INVALID; | |
04c17253 MD |
2053 | goto error; |
2054 | } | |
2f77fc4b DG |
2055 | |
2056 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 2057 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2058 | goto error; |
2059 | } | |
e5f5db7f | 2060 | channel_created = 1; |
2f77fc4b DG |
2061 | } |
2062 | ||
2063 | /* Get the newly created kernel channel pointer */ | |
2064 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
2065 | session->kernel_session); | |
2066 | if (kchan == NULL) { | |
2067 | /* This sould not happen... */ | |
f73fabfd | 2068 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2069 | goto error; |
2070 | } | |
2071 | ||
6e911cad MD |
2072 | switch (event->type) { |
2073 | case LTTNG_EVENT_ALL: | |
29c62722 | 2074 | { |
00a62084 MD |
2075 | char *filter_expression_a = NULL; |
2076 | struct lttng_filter_bytecode *filter_a = NULL; | |
2077 | ||
2078 | /* | |
2079 | * We need to duplicate filter_expression and filter, | |
2080 | * because ownership is passed to first enable | |
2081 | * event. | |
2082 | */ | |
2083 | if (filter_expression) { | |
2084 | filter_expression_a = strdup(filter_expression); | |
2085 | if (!filter_expression_a) { | |
2086 | ret = LTTNG_ERR_FATAL; | |
2087 | goto error; | |
2088 | } | |
2089 | } | |
2090 | if (filter) { | |
2091 | filter_a = zmalloc(sizeof(*filter_a) + filter->len); | |
2092 | if (!filter_a) { | |
2093 | free(filter_expression_a); | |
2094 | ret = LTTNG_ERR_FATAL; | |
2095 | goto error; | |
2096 | } | |
2097 | memcpy(filter_a, filter, sizeof(*filter_a) + filter->len); | |
2098 | } | |
29c62722 | 2099 | event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */ |
00a62084 MD |
2100 | ret = event_kernel_enable_event(kchan, event, |
2101 | filter_expression, filter); | |
a969e101 MD |
2102 | /* We have passed ownership */ |
2103 | filter_expression = NULL; | |
2104 | filter = NULL; | |
29c62722 MD |
2105 | if (ret != LTTNG_OK) { |
2106 | if (channel_created) { | |
2107 | /* Let's not leak a useless channel. */ | |
2108 | kernel_destroy_channel(kchan); | |
2109 | } | |
00a62084 MD |
2110 | free(filter_expression_a); |
2111 | free(filter_a); | |
29c62722 MD |
2112 | goto error; |
2113 | } | |
2114 | event->type = LTTNG_EVENT_SYSCALL; /* Hack */ | |
00a62084 MD |
2115 | ret = event_kernel_enable_event(kchan, event, |
2116 | filter_expression_a, filter_a); | |
60d21fa2 AB |
2117 | /* We have passed ownership */ |
2118 | filter_expression_a = NULL; | |
2119 | filter_a = NULL; | |
29c62722 MD |
2120 | if (ret != LTTNG_OK) { |
2121 | goto error; | |
2122 | } | |
2123 | break; | |
2124 | } | |
6e6ef3d7 | 2125 | case LTTNG_EVENT_PROBE: |
dcabc190 | 2126 | case LTTNG_EVENT_USERSPACE_PROBE: |
6e6ef3d7 DG |
2127 | case LTTNG_EVENT_FUNCTION: |
2128 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
6e911cad | 2129 | case LTTNG_EVENT_TRACEPOINT: |
00a62084 MD |
2130 | ret = event_kernel_enable_event(kchan, event, |
2131 | filter_expression, filter); | |
a969e101 MD |
2132 | /* We have passed ownership */ |
2133 | filter_expression = NULL; | |
2134 | filter = NULL; | |
6e911cad MD |
2135 | if (ret != LTTNG_OK) { |
2136 | if (channel_created) { | |
2137 | /* Let's not leak a useless channel. */ | |
2138 | kernel_destroy_channel(kchan); | |
2139 | } | |
2140 | goto error; | |
e5f5db7f | 2141 | } |
6e911cad MD |
2142 | break; |
2143 | case LTTNG_EVENT_SYSCALL: | |
00a62084 MD |
2144 | ret = event_kernel_enable_event(kchan, event, |
2145 | filter_expression, filter); | |
a969e101 MD |
2146 | /* We have passed ownership */ |
2147 | filter_expression = NULL; | |
2148 | filter = NULL; | |
e2b957af MD |
2149 | if (ret != LTTNG_OK) { |
2150 | goto error; | |
2151 | } | |
6e911cad MD |
2152 | break; |
2153 | default: | |
2154 | ret = LTTNG_ERR_UNK; | |
2f77fc4b DG |
2155 | goto error; |
2156 | } | |
2157 | ||
2158 | kernel_wait_quiescent(kernel_tracer_fd); | |
2159 | break; | |
2160 | } | |
2161 | case LTTNG_DOMAIN_UST: | |
2162 | { | |
2163 | struct ltt_ust_channel *uchan; | |
2164 | struct ltt_ust_session *usess = session->ust_session; | |
2165 | ||
2166 | assert(usess); | |
2167 | ||
85076754 MD |
2168 | /* |
2169 | * If a non-default channel has been created in the | |
2170 | * session, explicitely require that -c chan_name needs | |
2171 | * to be provided. | |
2172 | */ | |
2173 | if (usess->has_non_default_channel && channel_name[0] == '\0') { | |
2174 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
2175 | goto error; | |
2176 | } | |
2177 | ||
2f77fc4b DG |
2178 | /* Get channel from global UST domain */ |
2179 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
2180 | channel_name); | |
2181 | if (uchan == NULL) { | |
2182 | /* Create default channel */ | |
0a9c6494 DG |
2183 | attr = channel_new_default_attr(LTTNG_DOMAIN_UST, |
2184 | usess->buffer_type); | |
2f77fc4b | 2185 | if (attr == NULL) { |
f73fabfd | 2186 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2187 | goto error; |
2188 | } | |
04c17253 MD |
2189 | if (lttng_strncpy(attr->name, channel_name, |
2190 | sizeof(attr->name))) { | |
2191 | ret = LTTNG_ERR_INVALID; | |
04c17253 MD |
2192 | goto error; |
2193 | } | |
2f77fc4b DG |
2194 | |
2195 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 2196 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2197 | goto error; |
2198 | } | |
2f77fc4b DG |
2199 | |
2200 | /* Get the newly created channel reference back */ | |
2201 | uchan = trace_ust_find_channel_by_name( | |
2202 | usess->domain_global.channels, channel_name); | |
2203 | assert(uchan); | |
2204 | } | |
2205 | ||
141feb8c JG |
2206 | if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) { |
2207 | /* | |
2208 | * Don't allow users to add UST events to channels which | |
2209 | * are assigned to a userspace subdomain (JUL, Log4J, | |
2210 | * Python, etc.). | |
2211 | */ | |
2212 | ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN; | |
2213 | goto error; | |
2214 | } | |
2215 | ||
dac8e046 JG |
2216 | if (!internal_event) { |
2217 | /* | |
2218 | * Ensure the event name is not reserved for internal | |
2219 | * use. | |
2220 | */ | |
2221 | ret = validate_ust_event_name(event->name); | |
2222 | if (ret) { | |
2223 | WARN("Userspace event name %s failed validation.", | |
bbcab087 | 2224 | event->name); |
dac8e046 JG |
2225 | ret = LTTNG_ERR_INVALID_EVENT_NAME; |
2226 | goto error; | |
2227 | } | |
2228 | } | |
2229 | ||
2f77fc4b | 2230 | /* At this point, the session and channel exist on the tracer */ |
6b453b5e | 2231 | ret = event_ust_enable_tracepoint(usess, uchan, event, |
88f06f15 JG |
2232 | filter_expression, filter, exclusion, |
2233 | internal_event); | |
49d21f93 MD |
2234 | /* We have passed ownership */ |
2235 | filter_expression = NULL; | |
2236 | filter = NULL; | |
2237 | exclusion = NULL; | |
94382e15 JG |
2238 | if (ret == LTTNG_ERR_UST_EVENT_ENABLED) { |
2239 | goto already_enabled; | |
2240 | } else if (ret != LTTNG_OK) { | |
2f77fc4b DG |
2241 | goto error; |
2242 | } | |
2243 | break; | |
2244 | } | |
5cdb6027 | 2245 | case LTTNG_DOMAIN_LOG4J: |
f20baf8e | 2246 | case LTTNG_DOMAIN_JUL: |
0e115563 | 2247 | case LTTNG_DOMAIN_PYTHON: |
f20baf8e | 2248 | { |
da6c3a50 | 2249 | const char *default_event_name, *default_chan_name; |
fefd409b | 2250 | struct agent *agt; |
f20baf8e DG |
2251 | struct lttng_event uevent; |
2252 | struct lttng_domain tmp_dom; | |
2253 | struct ltt_ust_session *usess = session->ust_session; | |
2254 | ||
2255 | assert(usess); | |
2256 | ||
f28f9e44 JG |
2257 | if (!agent_tracing_is_enabled()) { |
2258 | DBG("Attempted to enable an event in an agent domain but the agent thread is not running"); | |
2259 | ret = LTTNG_ERR_AGENT_TRACING_DISABLED; | |
2260 | goto error; | |
2261 | } | |
2262 | ||
5cdb6027 | 2263 | agt = trace_ust_find_agent(usess, domain->type); |
fefd409b | 2264 | if (!agt) { |
5cdb6027 | 2265 | agt = agent_create(domain->type); |
fefd409b | 2266 | if (!agt) { |
e5b3c48c | 2267 | ret = LTTNG_ERR_NOMEM; |
fefd409b DG |
2268 | goto error; |
2269 | } | |
2270 | agent_add(agt, usess->agents); | |
2271 | } | |
2272 | ||
022d91ba | 2273 | /* Create the default tracepoint. */ |
996de3c7 | 2274 | memset(&uevent, 0, sizeof(uevent)); |
f20baf8e DG |
2275 | uevent.type = LTTNG_EVENT_TRACEPOINT; |
2276 | uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
51755dc8 JG |
2277 | default_event_name = event_get_default_agent_ust_name( |
2278 | domain->type); | |
da6c3a50 | 2279 | if (!default_event_name) { |
e5b3c48c | 2280 | ret = LTTNG_ERR_FATAL; |
da6c3a50 | 2281 | goto error; |
f43f95a9 | 2282 | } |
da6c3a50 | 2283 | strncpy(uevent.name, default_event_name, sizeof(uevent.name)); |
f20baf8e DG |
2284 | uevent.name[sizeof(uevent.name) - 1] = '\0'; |
2285 | ||
2286 | /* | |
2287 | * The domain type is changed because we are about to enable the | |
2288 | * default channel and event for the JUL domain that are hardcoded. | |
2289 | * This happens in the UST domain. | |
2290 | */ | |
2291 | memcpy(&tmp_dom, domain, sizeof(tmp_dom)); | |
2292 | tmp_dom.type = LTTNG_DOMAIN_UST; | |
2293 | ||
0e115563 DG |
2294 | switch (domain->type) { |
2295 | case LTTNG_DOMAIN_LOG4J: | |
da6c3a50 | 2296 | default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME; |
0e115563 DG |
2297 | break; |
2298 | case LTTNG_DOMAIN_JUL: | |
da6c3a50 | 2299 | default_chan_name = DEFAULT_JUL_CHANNEL_NAME; |
0e115563 DG |
2300 | break; |
2301 | case LTTNG_DOMAIN_PYTHON: | |
2302 | default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME; | |
2303 | break; | |
2304 | default: | |
e98a44b0 | 2305 | /* The switch/case we are in makes this impossible */ |
0e115563 | 2306 | assert(0); |
da6c3a50 DG |
2307 | } |
2308 | ||
971da06a | 2309 | { |
8404118c | 2310 | char *filter_expression_copy = NULL; |
51755dc8 | 2311 | struct lttng_filter_bytecode *filter_copy = NULL; |
971da06a JG |
2312 | |
2313 | if (filter) { | |
51755dc8 JG |
2314 | const size_t filter_size = sizeof( |
2315 | struct lttng_filter_bytecode) | |
2316 | + filter->len; | |
2317 | ||
2318 | filter_copy = zmalloc(filter_size); | |
971da06a | 2319 | if (!filter_copy) { |
018096a4 | 2320 | ret = LTTNG_ERR_NOMEM; |
b742e3e2 | 2321 | goto error; |
971da06a | 2322 | } |
51755dc8 | 2323 | memcpy(filter_copy, filter, filter_size); |
971da06a | 2324 | |
8404118c JG |
2325 | filter_expression_copy = |
2326 | strdup(filter_expression); | |
2327 | if (!filter_expression) { | |
2328 | ret = LTTNG_ERR_NOMEM; | |
51755dc8 JG |
2329 | } |
2330 | ||
2331 | if (!filter_expression_copy || !filter_copy) { | |
2332 | free(filter_expression_copy); | |
2333 | free(filter_copy); | |
2334 | goto error; | |
8404118c | 2335 | } |
971da06a JG |
2336 | } |
2337 | ||
88f06f15 | 2338 | ret = cmd_enable_event_internal(session, &tmp_dom, |
971da06a | 2339 | (char *) default_chan_name, |
8404118c JG |
2340 | &uevent, filter_expression_copy, |
2341 | filter_copy, NULL, wpipe); | |
971da06a JG |
2342 | } |
2343 | ||
94382e15 JG |
2344 | if (ret == LTTNG_ERR_UST_EVENT_ENABLED) { |
2345 | goto already_enabled; | |
2346 | } else if (ret != LTTNG_OK) { | |
f20baf8e DG |
2347 | goto error; |
2348 | } | |
2349 | ||
2350 | /* The wild card * means that everything should be enabled. */ | |
2351 | if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) { | |
8404118c JG |
2352 | ret = event_agent_enable_all(usess, agt, event, filter, |
2353 | filter_expression); | |
f20baf8e | 2354 | } else { |
8404118c JG |
2355 | ret = event_agent_enable(usess, agt, event, filter, |
2356 | filter_expression); | |
f20baf8e | 2357 | } |
51755dc8 JG |
2358 | filter = NULL; |
2359 | filter_expression = NULL; | |
f20baf8e DG |
2360 | if (ret != LTTNG_OK) { |
2361 | goto error; | |
2362 | } | |
2363 | ||
2364 | break; | |
2365 | } | |
2f77fc4b | 2366 | default: |
f73fabfd | 2367 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2368 | goto error; |
2369 | } | |
2370 | ||
f73fabfd | 2371 | ret = LTTNG_OK; |
2f77fc4b | 2372 | |
94382e15 | 2373 | already_enabled: |
2f77fc4b | 2374 | error: |
49d21f93 MD |
2375 | free(filter_expression); |
2376 | free(filter); | |
2377 | free(exclusion); | |
cf0bcb51 | 2378 | channel_attr_destroy(attr); |
2223c96f | 2379 | rcu_read_unlock(); |
2f77fc4b DG |
2380 | return ret; |
2381 | } | |
2382 | ||
88f06f15 JG |
2383 | /* |
2384 | * Command LTTNG_ENABLE_EVENT processed by the client thread. | |
2385 | * We own filter, exclusion, and filter_expression. | |
2386 | */ | |
2387 | int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain, | |
2388 | char *channel_name, struct lttng_event *event, | |
2389 | char *filter_expression, | |
2390 | struct lttng_filter_bytecode *filter, | |
2391 | struct lttng_event_exclusion *exclusion, | |
2392 | int wpipe) | |
2393 | { | |
2394 | return _cmd_enable_event(session, domain, channel_name, event, | |
2395 | filter_expression, filter, exclusion, wpipe, false); | |
2396 | } | |
2397 | ||
2398 | /* | |
2399 | * Enable an event which is internal to LTTng. An internal should | |
2400 | * never be made visible to clients and are immune to checks such as | |
2401 | * reserved names. | |
2402 | */ | |
2403 | static int cmd_enable_event_internal(struct ltt_session *session, | |
2404 | struct lttng_domain *domain, | |
2405 | char *channel_name, struct lttng_event *event, | |
2406 | char *filter_expression, | |
2407 | struct lttng_filter_bytecode *filter, | |
2408 | struct lttng_event_exclusion *exclusion, | |
2409 | int wpipe) | |
2410 | { | |
2411 | return _cmd_enable_event(session, domain, channel_name, event, | |
2412 | filter_expression, filter, exclusion, wpipe, true); | |
2413 | } | |
2414 | ||
2f77fc4b DG |
2415 | /* |
2416 | * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. | |
2417 | */ | |
56a37563 JG |
2418 | ssize_t cmd_list_tracepoints(enum lttng_domain_type domain, |
2419 | struct lttng_event **events) | |
2f77fc4b DG |
2420 | { |
2421 | int ret; | |
2422 | ssize_t nb_events = 0; | |
2423 | ||
2424 | switch (domain) { | |
2425 | case LTTNG_DOMAIN_KERNEL: | |
2426 | nb_events = kernel_list_events(kernel_tracer_fd, events); | |
2427 | if (nb_events < 0) { | |
f73fabfd | 2428 | ret = LTTNG_ERR_KERN_LIST_FAIL; |
2f77fc4b DG |
2429 | goto error; |
2430 | } | |
2431 | break; | |
2432 | case LTTNG_DOMAIN_UST: | |
2433 | nb_events = ust_app_list_events(events); | |
2434 | if (nb_events < 0) { | |
f73fabfd | 2435 | ret = LTTNG_ERR_UST_LIST_FAIL; |
2f77fc4b DG |
2436 | goto error; |
2437 | } | |
2438 | break; | |
5cdb6027 | 2439 | case LTTNG_DOMAIN_LOG4J: |
3c6a091f | 2440 | case LTTNG_DOMAIN_JUL: |
0e115563 | 2441 | case LTTNG_DOMAIN_PYTHON: |
f60140a1 | 2442 | nb_events = agent_list_events(events, domain); |
3c6a091f DG |
2443 | if (nb_events < 0) { |
2444 | ret = LTTNG_ERR_UST_LIST_FAIL; | |
2445 | goto error; | |
2446 | } | |
2447 | break; | |
2f77fc4b | 2448 | default: |
f73fabfd | 2449 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2450 | goto error; |
2451 | } | |
2452 | ||
2453 | return nb_events; | |
2454 | ||
2455 | error: | |
2456 | /* Return negative value to differentiate return code */ | |
2457 | return -ret; | |
2458 | } | |
2459 | ||
2460 | /* | |
2461 | * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread. | |
2462 | */ | |
56a37563 | 2463 | ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain, |
2f77fc4b DG |
2464 | struct lttng_event_field **fields) |
2465 | { | |
2466 | int ret; | |
2467 | ssize_t nb_fields = 0; | |
2468 | ||
2469 | switch (domain) { | |
2470 | case LTTNG_DOMAIN_UST: | |
2471 | nb_fields = ust_app_list_event_fields(fields); | |
2472 | if (nb_fields < 0) { | |
f73fabfd | 2473 | ret = LTTNG_ERR_UST_LIST_FAIL; |
2f77fc4b DG |
2474 | goto error; |
2475 | } | |
2476 | break; | |
2477 | case LTTNG_DOMAIN_KERNEL: | |
2478 | default: /* fall-through */ | |
f73fabfd | 2479 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2480 | goto error; |
2481 | } | |
2482 | ||
2483 | return nb_fields; | |
2484 | ||
2485 | error: | |
2486 | /* Return negative value to differentiate return code */ | |
2487 | return -ret; | |
2488 | } | |
2489 | ||
834978fd DG |
2490 | ssize_t cmd_list_syscalls(struct lttng_event **events) |
2491 | { | |
2492 | return syscall_table_list(events); | |
2493 | } | |
2494 | ||
a5dfbb9d MD |
2495 | /* |
2496 | * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread. | |
2497 | * | |
2498 | * Called with session lock held. | |
2499 | */ | |
2500 | ssize_t cmd_list_tracker_pids(struct ltt_session *session, | |
56a37563 | 2501 | enum lttng_domain_type domain, int32_t **pids) |
a5dfbb9d MD |
2502 | { |
2503 | int ret; | |
2504 | ssize_t nr_pids = 0; | |
2505 | ||
2506 | switch (domain) { | |
2507 | case LTTNG_DOMAIN_KERNEL: | |
2508 | { | |
2509 | struct ltt_kernel_session *ksess; | |
2510 | ||
2511 | ksess = session->kernel_session; | |
2512 | nr_pids = kernel_list_tracker_pids(ksess, pids); | |
2513 | if (nr_pids < 0) { | |
2514 | ret = LTTNG_ERR_KERN_LIST_FAIL; | |
2515 | goto error; | |
2516 | } | |
2517 | break; | |
2518 | } | |
2519 | case LTTNG_DOMAIN_UST: | |
2520 | { | |
2521 | struct ltt_ust_session *usess; | |
2522 | ||
2523 | usess = session->ust_session; | |
2524 | nr_pids = trace_ust_list_tracker_pids(usess, pids); | |
2525 | if (nr_pids < 0) { | |
2526 | ret = LTTNG_ERR_UST_LIST_FAIL; | |
2527 | goto error; | |
2528 | } | |
2529 | break; | |
2530 | } | |
2531 | case LTTNG_DOMAIN_LOG4J: | |
2532 | case LTTNG_DOMAIN_JUL: | |
2533 | case LTTNG_DOMAIN_PYTHON: | |
2534 | default: | |
2535 | ret = LTTNG_ERR_UND; | |
2536 | goto error; | |
2537 | } | |
2538 | ||
2539 | return nr_pids; | |
2540 | ||
2541 | error: | |
2542 | /* Return negative value to differentiate return code */ | |
2543 | return -ret; | |
2544 | } | |
2545 | ||
2f77fc4b DG |
2546 | /* |
2547 | * Command LTTNG_START_TRACE processed by the client thread. | |
a9ad0c8f MD |
2548 | * |
2549 | * Called with session mutex held. | |
2f77fc4b DG |
2550 | */ |
2551 | int cmd_start_trace(struct ltt_session *session) | |
2552 | { | |
82b69413 | 2553 | enum lttng_error_code ret; |
cde3e505 | 2554 | unsigned long nb_chan = 0; |
2f77fc4b DG |
2555 | struct ltt_kernel_session *ksession; |
2556 | struct ltt_ust_session *usess; | |
2f77fc4b DG |
2557 | |
2558 | assert(session); | |
2559 | ||
2560 | /* Ease our life a bit ;) */ | |
2561 | ksession = session->kernel_session; | |
2562 | usess = session->ust_session; | |
2563 | ||
8382cf6f DG |
2564 | /* Is the session already started? */ |
2565 | if (session->active) { | |
f73fabfd | 2566 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
2f77fc4b DG |
2567 | goto error; |
2568 | } | |
2569 | ||
cde3e505 DG |
2570 | /* |
2571 | * Starting a session without channel is useless since after that it's not | |
2572 | * possible to enable channel thus inform the client. | |
2573 | */ | |
2574 | if (usess && usess->domain_global.channels) { | |
2575 | nb_chan += lttng_ht_get_count(usess->domain_global.channels); | |
2576 | } | |
2577 | if (ksession) { | |
2578 | nb_chan += ksession->channel_count; | |
2579 | } | |
2580 | if (!nb_chan) { | |
2581 | ret = LTTNG_ERR_NO_CHANNEL; | |
2582 | goto error; | |
2583 | } | |
2584 | ||
e2b6b28e JG |
2585 | if (session->output_traces && !session->current_trace_chunk && |
2586 | session_output_supports_trace_chunks(session)) { | |
d2956687 JG |
2587 | struct lttng_trace_chunk *trace_chunk; |
2588 | ||
2589 | trace_chunk = session_create_new_trace_chunk( | |
348a81dc | 2590 | session, NULL, NULL, NULL); |
d2956687 JG |
2591 | if (!trace_chunk) { |
2592 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
2593 | goto error; | |
2594 | } | |
2595 | assert(!session->current_trace_chunk); | |
2596 | ret = session_set_trace_chunk(session, trace_chunk, NULL); | |
2597 | lttng_trace_chunk_put(trace_chunk); | |
2598 | if (ret) { | |
2599 | ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; | |
5c408ad8 JD |
2600 | goto error; |
2601 | } | |
c996624c JD |
2602 | } |
2603 | ||
2f77fc4b DG |
2604 | /* Kernel tracing */ |
2605 | if (ksession != NULL) { | |
c996624c | 2606 | DBG("Start kernel tracing session %s", session->name); |
9b6c7ec5 DG |
2607 | ret = start_kernel_session(ksession, kernel_tracer_fd); |
2608 | if (ret != LTTNG_OK) { | |
2f77fc4b DG |
2609 | goto error; |
2610 | } | |
2f77fc4b DG |
2611 | } |
2612 | ||
2613 | /* Flag session that trace should start automatically */ | |
2614 | if (usess) { | |
82b69413 JG |
2615 | int int_ret = ust_app_start_trace_all(usess); |
2616 | ||
2617 | if (int_ret < 0) { | |
f73fabfd | 2618 | ret = LTTNG_ERR_UST_START_FAIL; |
2f77fc4b DG |
2619 | goto error; |
2620 | } | |
2621 | } | |
2622 | ||
8382cf6f DG |
2623 | /* Flag this after a successful start. */ |
2624 | session->has_been_started = 1; | |
2625 | session->active = 1; | |
9b6c7ec5 | 2626 | |
5c408ad8 JD |
2627 | /* |
2628 | * Clear the flag that indicates that a rotation was done while the | |
2629 | * session was stopped. | |
2630 | */ | |
2631 | session->rotated_after_last_stop = false; | |
2632 | ||
259c2674 | 2633 | if (session->rotate_timer_period) { |
82b69413 JG |
2634 | int int_ret = timer_session_rotation_schedule_timer_start( |
2635 | session, session->rotate_timer_period); | |
2636 | ||
2637 | if (int_ret < 0) { | |
259c2674 JD |
2638 | ERR("Failed to enable rotate timer"); |
2639 | ret = LTTNG_ERR_UNK; | |
2640 | goto error; | |
2641 | } | |
2642 | } | |
2643 | ||
f73fabfd | 2644 | ret = LTTNG_OK; |
2f77fc4b DG |
2645 | |
2646 | error: | |
2647 | return ret; | |
2648 | } | |
2649 | ||
2650 | /* | |
2651 | * Command LTTNG_STOP_TRACE processed by the client thread. | |
2652 | */ | |
2653 | int cmd_stop_trace(struct ltt_session *session) | |
2654 | { | |
2655 | int ret; | |
2656 | struct ltt_kernel_channel *kchan; | |
2657 | struct ltt_kernel_session *ksession; | |
2658 | struct ltt_ust_session *usess; | |
a9577b76 | 2659 | bool error_occurred = false; |
2f77fc4b DG |
2660 | |
2661 | assert(session); | |
2662 | ||
5c408ad8 | 2663 | DBG("Begin stop session %s (id %" PRIu64 ")", session->name, session->id); |
2f77fc4b DG |
2664 | /* Short cut */ |
2665 | ksession = session->kernel_session; | |
2666 | usess = session->ust_session; | |
2667 | ||
8382cf6f DG |
2668 | /* Session is not active. Skip everythong and inform the client. */ |
2669 | if (!session->active) { | |
f73fabfd | 2670 | ret = LTTNG_ERR_TRACE_ALREADY_STOPPED; |
2f77fc4b DG |
2671 | goto error; |
2672 | } | |
2673 | ||
2f77fc4b | 2674 | /* Kernel tracer */ |
14fb1ebe | 2675 | if (ksession && ksession->active) { |
2f77fc4b DG |
2676 | DBG("Stop kernel tracing"); |
2677 | ||
566d8970 MD |
2678 | ret = kernel_stop_session(ksession); |
2679 | if (ret < 0) { | |
2680 | ret = LTTNG_ERR_KERN_STOP_FAIL; | |
2681 | goto error; | |
2682 | } | |
2683 | ||
2684 | kernel_wait_quiescent(kernel_tracer_fd); | |
2685 | ||
2686 | /* Flush metadata after stopping (if exists) */ | |
2f77fc4b DG |
2687 | if (ksession->metadata_stream_fd >= 0) { |
2688 | ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd); | |
2689 | if (ret < 0) { | |
2690 | ERR("Kernel metadata flush failed"); | |
2691 | } | |
2692 | } | |
2693 | ||
566d8970 | 2694 | /* Flush all buffers after stopping */ |
2f77fc4b DG |
2695 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { |
2696 | ret = kernel_flush_buffer(kchan); | |
2697 | if (ret < 0) { | |
2698 | ERR("Kernel flush buffer error"); | |
2699 | } | |
2700 | } | |
2701 | ||
14fb1ebe | 2702 | ksession->active = 0; |
5c408ad8 JD |
2703 | DBG("Kernel session stopped %s (id %" PRIu64 ")", session->name, |
2704 | session->id); | |
2f77fc4b DG |
2705 | } |
2706 | ||
14fb1ebe | 2707 | if (usess && usess->active) { |
2f77fc4b DG |
2708 | ret = ust_app_stop_trace_all(usess); |
2709 | if (ret < 0) { | |
f73fabfd | 2710 | ret = LTTNG_ERR_UST_STOP_FAIL; |
2f77fc4b DG |
2711 | goto error; |
2712 | } | |
2713 | } | |
2714 | ||
8382cf6f DG |
2715 | /* Flag inactive after a successful stop. */ |
2716 | session->active = 0; | |
a9577b76 | 2717 | ret = !error_occurred ? LTTNG_OK : LTTNG_ERR_UNK; |
2f77fc4b DG |
2718 | |
2719 | error: | |
2720 | return ret; | |
2721 | } | |
2722 | ||
2723 | /* | |
2724 | * Command LTTNG_SET_CONSUMER_URI processed by the client thread. | |
2725 | */ | |
bda32d56 JG |
2726 | int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri, |
2727 | struct lttng_uri *uris) | |
2f77fc4b DG |
2728 | { |
2729 | int ret, i; | |
2730 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2731 | struct ltt_ust_session *usess = session->ust_session; | |
2f77fc4b DG |
2732 | |
2733 | assert(session); | |
2734 | assert(uris); | |
2735 | assert(nb_uri > 0); | |
2736 | ||
8382cf6f DG |
2737 | /* Can't set consumer URI if the session is active. */ |
2738 | if (session->active) { | |
f73fabfd | 2739 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
2f77fc4b DG |
2740 | goto error; |
2741 | } | |
2742 | ||
bda32d56 | 2743 | /* Set the "global" consumer URIs */ |
2f77fc4b | 2744 | for (i = 0; i < nb_uri; i++) { |
b178f53e JG |
2745 | ret = add_uri_to_consumer(session, |
2746 | session->consumer, | |
2747 | &uris[i], LTTNG_DOMAIN_NONE); | |
a74934ba | 2748 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2749 | goto error; |
2750 | } | |
2f77fc4b DG |
2751 | } |
2752 | ||
bda32d56 JG |
2753 | /* Set UST session URIs */ |
2754 | if (session->ust_session) { | |
2755 | for (i = 0; i < nb_uri; i++) { | |
b178f53e | 2756 | ret = add_uri_to_consumer(session, |
bda32d56 | 2757 | session->ust_session->consumer, |
b178f53e | 2758 | &uris[i], LTTNG_DOMAIN_UST); |
bda32d56 JG |
2759 | if (ret != LTTNG_OK) { |
2760 | goto error; | |
2761 | } | |
2762 | } | |
2763 | } | |
2764 | ||
2765 | /* Set kernel session URIs */ | |
2766 | if (session->kernel_session) { | |
2767 | for (i = 0; i < nb_uri; i++) { | |
b178f53e | 2768 | ret = add_uri_to_consumer(session, |
bda32d56 | 2769 | session->kernel_session->consumer, |
b178f53e | 2770 | &uris[i], LTTNG_DOMAIN_KERNEL); |
bda32d56 JG |
2771 | if (ret != LTTNG_OK) { |
2772 | goto error; | |
2773 | } | |
2774 | } | |
2775 | } | |
2776 | ||
7ab70fe0 DG |
2777 | /* |
2778 | * Make sure to set the session in output mode after we set URI since a | |
2779 | * session can be created without URL (thus flagged in no output mode). | |
2780 | */ | |
2781 | session->output_traces = 1; | |
2782 | if (ksess) { | |
2783 | ksess->output_traces = 1; | |
bda32d56 JG |
2784 | } |
2785 | ||
2786 | if (usess) { | |
7ab70fe0 DG |
2787 | usess->output_traces = 1; |
2788 | } | |
2789 | ||
2f77fc4b | 2790 | /* All good! */ |
f73fabfd | 2791 | ret = LTTNG_OK; |
2f77fc4b DG |
2792 | |
2793 | error: | |
2794 | return ret; | |
2795 | } | |
2796 | ||
b178f53e JG |
2797 | static |
2798 | enum lttng_error_code set_session_output_from_descriptor( | |
2799 | struct ltt_session *session, | |
2800 | const struct lttng_session_descriptor *descriptor) | |
2f77fc4b DG |
2801 | { |
2802 | int ret; | |
b178f53e JG |
2803 | enum lttng_error_code ret_code = LTTNG_OK; |
2804 | enum lttng_session_descriptor_type session_type = | |
2805 | lttng_session_descriptor_get_type(descriptor); | |
2806 | enum lttng_session_descriptor_output_type output_type = | |
2807 | lttng_session_descriptor_get_output_type(descriptor); | |
2808 | struct lttng_uri uris[2] = {}; | |
2809 | size_t uri_count = 0; | |
2810 | ||
2811 | switch (output_type) { | |
2812 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE: | |
2813 | goto end; | |
2814 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL: | |
2815 | lttng_session_descriptor_get_local_output_uri(descriptor, | |
2816 | &uris[0]); | |
2817 | uri_count = 1; | |
2818 | break; | |
2819 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK: | |
2820 | lttng_session_descriptor_get_network_output_uris(descriptor, | |
2821 | &uris[0], &uris[1]); | |
2822 | uri_count = 2; | |
2823 | break; | |
2824 | default: | |
2825 | ret_code = LTTNG_ERR_INVALID; | |
e32d7f27 | 2826 | goto end; |
2f77fc4b DG |
2827 | } |
2828 | ||
b178f53e JG |
2829 | switch (session_type) { |
2830 | case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT: | |
2831 | { | |
2832 | struct snapshot_output *new_output = NULL; | |
2833 | ||
2834 | new_output = snapshot_output_alloc(); | |
2835 | if (!new_output) { | |
2836 | ret_code = LTTNG_ERR_NOMEM; | |
2837 | goto end; | |
2838 | } | |
2839 | ||
2840 | ret = snapshot_output_init_with_uri(session, | |
2841 | DEFAULT_SNAPSHOT_MAX_SIZE, | |
2842 | NULL, uris, uri_count, session->consumer, | |
2843 | new_output, &session->snapshot); | |
2844 | if (ret < 0) { | |
2845 | ret_code = (ret == -ENOMEM) ? | |
2846 | LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID; | |
2847 | snapshot_output_destroy(new_output); | |
2848 | goto end; | |
2849 | } | |
2850 | snapshot_add_output(&session->snapshot, new_output); | |
2851 | break; | |
2852 | } | |
2853 | case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR: | |
2854 | case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE: | |
2855 | { | |
2856 | ret_code = cmd_set_consumer_uri(session, uri_count, uris); | |
2857 | break; | |
2858 | } | |
2859 | default: | |
2860 | ret_code = LTTNG_ERR_INVALID; | |
e32d7f27 | 2861 | goto end; |
2f77fc4b | 2862 | } |
b178f53e JG |
2863 | end: |
2864 | return ret_code; | |
2865 | } | |
2866 | ||
2867 | static | |
2868 | enum lttng_error_code cmd_create_session_from_descriptor( | |
2869 | struct lttng_session_descriptor *descriptor, | |
2870 | const lttng_sock_cred *creds, | |
2871 | const char *home_path) | |
2872 | { | |
2873 | int ret; | |
2874 | enum lttng_error_code ret_code; | |
2875 | const char *session_name; | |
2876 | struct ltt_session *new_session = NULL; | |
2877 | enum lttng_session_descriptor_status descriptor_status; | |
6fa5fe7c | 2878 | const char *base_path; |
2f77fc4b | 2879 | |
e32d7f27 | 2880 | session_lock_list(); |
b178f53e JG |
2881 | if (home_path) { |
2882 | if (*home_path != '/') { | |
2883 | ERR("Home path provided by client is not absolute"); | |
2884 | ret_code = LTTNG_ERR_INVALID; | |
2885 | goto end; | |
2886 | } | |
2887 | } | |
2f77fc4b | 2888 | |
b178f53e JG |
2889 | descriptor_status = lttng_session_descriptor_get_session_name( |
2890 | descriptor, &session_name); | |
2891 | switch (descriptor_status) { | |
2892 | case LTTNG_SESSION_DESCRIPTOR_STATUS_OK: | |
2893 | break; | |
2894 | case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET: | |
2895 | session_name = NULL; | |
2896 | break; | |
2897 | default: | |
2898 | ret_code = LTTNG_ERR_INVALID; | |
2899 | goto end; | |
2900 | } | |
6fa5fe7c MD |
2901 | ret = lttng_session_descriptor_get_base_path(descriptor, &base_path); |
2902 | if (ret) { | |
2903 | ret_code = LTTNG_ERR_INVALID; | |
2904 | goto end; | |
2905 | } | |
b178f53e | 2906 | ret_code = session_create(session_name, creds->uid, creds->gid, |
6fa5fe7c | 2907 | base_path, &new_session); |
b178f53e | 2908 | if (ret_code != LTTNG_OK) { |
e32d7f27 | 2909 | goto end; |
2f77fc4b DG |
2910 | } |
2911 | ||
b178f53e JG |
2912 | if (!session_name) { |
2913 | ret = lttng_session_descriptor_set_session_name(descriptor, | |
2914 | new_session->name); | |
2915 | if (ret) { | |
2916 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
2917 | goto end; | |
2918 | } | |
2919 | } | |
2920 | ||
2921 | if (!lttng_session_descriptor_is_output_destination_initialized( | |
2922 | descriptor)) { | |
2923 | /* | |
2924 | * Only include the session's creation time in the output | |
2925 | * destination if the name of the session itself was | |
2926 | * not auto-generated. | |
2927 | */ | |
2928 | ret_code = lttng_session_descriptor_set_default_output( | |
2929 | descriptor, | |
2930 | session_name ? &new_session->creation_time : NULL, | |
2931 | home_path); | |
2932 | if (ret_code != LTTNG_OK) { | |
e32d7f27 | 2933 | goto end; |
2bba9e53 | 2934 | } |
2bba9e53 | 2935 | } else { |
b178f53e JG |
2936 | new_session->has_user_specified_directory = |
2937 | lttng_session_descriptor_has_output_directory( | |
2938 | descriptor); | |
2f77fc4b DG |
2939 | } |
2940 | ||
b178f53e JG |
2941 | switch (lttng_session_descriptor_get_type(descriptor)) { |
2942 | case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT: | |
2943 | new_session->snapshot_mode = 1; | |
2944 | break; | |
2945 | case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE: | |
2946 | new_session->live_timer = | |
2947 | lttng_session_descriptor_live_get_timer_interval( | |
2948 | descriptor); | |
2949 | break; | |
2950 | default: | |
2951 | break; | |
2952 | } | |
2f77fc4b | 2953 | |
b178f53e JG |
2954 | ret_code = set_session_output_from_descriptor(new_session, descriptor); |
2955 | if (ret_code != LTTNG_OK) { | |
2956 | goto end; | |
2957 | } | |
2958 | new_session->consumer->enabled = 1; | |
2959 | ret_code = LTTNG_OK; | |
e32d7f27 | 2960 | end: |
b178f53e JG |
2961 | /* Release reference provided by the session_create function. */ |
2962 | session_put(new_session); | |
2963 | if (ret_code != LTTNG_OK && new_session) { | |
2964 | /* Release the global reference on error. */ | |
2965 | session_destroy(new_session); | |
e32d7f27 | 2966 | } |
b178f53e JG |
2967 | session_unlock_list(); |
2968 | return ret_code; | |
2f77fc4b DG |
2969 | } |
2970 | ||
b178f53e JG |
2971 | enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx, int sock, |
2972 | struct lttng_session_descriptor **return_descriptor) | |
27babd3a DG |
2973 | { |
2974 | int ret; | |
b178f53e JG |
2975 | size_t payload_size; |
2976 | struct lttng_dynamic_buffer payload; | |
2977 | struct lttng_buffer_view home_dir_view; | |
2978 | struct lttng_buffer_view session_descriptor_view; | |
2979 | struct lttng_session_descriptor *session_descriptor = NULL; | |
2980 | enum lttng_error_code ret_code; | |
2981 | ||
2982 | lttng_dynamic_buffer_init(&payload); | |
2983 | if (cmd_ctx->lsm->u.create_session.home_dir_size >= | |
2984 | LTTNG_PATH_MAX) { | |
2985 | ret_code = LTTNG_ERR_INVALID; | |
2986 | goto error; | |
27babd3a | 2987 | } |
b178f53e JG |
2988 | if (cmd_ctx->lsm->u.create_session.session_descriptor_size > |
2989 | LTTNG_SESSION_DESCRIPTOR_MAX_LEN) { | |
2990 | ret_code = LTTNG_ERR_INVALID; | |
2991 | goto error; | |
27babd3a DG |
2992 | } |
2993 | ||
b178f53e JG |
2994 | payload_size = cmd_ctx->lsm->u.create_session.home_dir_size + |
2995 | cmd_ctx->lsm->u.create_session.session_descriptor_size; | |
2996 | ret = lttng_dynamic_buffer_set_size(&payload, payload_size); | |
2997 | if (ret) { | |
2998 | ret_code = LTTNG_ERR_NOMEM; | |
2999 | goto error; | |
27babd3a DG |
3000 | } |
3001 | ||
b178f53e JG |
3002 | ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size); |
3003 | if (ret <= 0) { | |
3004 | ERR("Reception of session descriptor failed, aborting."); | |
3005 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
3006 | goto error; | |
27babd3a DG |
3007 | } |
3008 | ||
b178f53e JG |
3009 | home_dir_view = lttng_buffer_view_from_dynamic_buffer( |
3010 | &payload, | |
3011 | 0, | |
3012 | cmd_ctx->lsm->u.create_session.home_dir_size); | |
3013 | session_descriptor_view = lttng_buffer_view_from_dynamic_buffer( | |
3014 | &payload, | |
3015 | cmd_ctx->lsm->u.create_session.home_dir_size, | |
3016 | cmd_ctx->lsm->u.create_session.session_descriptor_size); | |
27babd3a | 3017 | |
b178f53e JG |
3018 | ret = lttng_session_descriptor_create_from_buffer( |
3019 | &session_descriptor_view, &session_descriptor); | |
3020 | if (ret < 0) { | |
3021 | ERR("Failed to create session descriptor from payload of \"create session\" command"); | |
3022 | ret_code = LTTNG_ERR_INVALID; | |
3023 | goto error; | |
3024 | } | |
27babd3a | 3025 | |
b178f53e JG |
3026 | /* |
3027 | * Sets the descriptor's auto-generated properties (name, output) if | |
3028 | * needed. | |
3029 | */ | |
3030 | ret_code = cmd_create_session_from_descriptor(session_descriptor, | |
3031 | &cmd_ctx->creds, | |
3032 | home_dir_view.size ? home_dir_view.data : NULL); | |
3033 | if (ret_code != LTTNG_OK) { | |
3034 | goto error; | |
e32d7f27 | 3035 | } |
b178f53e JG |
3036 | |
3037 | ret_code = LTTNG_OK; | |
3038 | *return_descriptor = session_descriptor; | |
3039 | session_descriptor = NULL; | |
3040 | error: | |
3041 | lttng_dynamic_buffer_reset(&payload); | |
3042 | lttng_session_descriptor_destroy(session_descriptor); | |
3043 | return ret_code; | |
27babd3a DG |
3044 | } |
3045 | ||
3e3665b8 JG |
3046 | static |
3047 | void cmd_destroy_session_reply(const struct ltt_session *session, | |
3048 | void *_reply_context) | |
3049 | { | |
3050 | int ret; | |
3051 | ssize_t comm_ret; | |
3052 | const struct cmd_destroy_session_reply_context *reply_context = | |
3053 | _reply_context; | |
3054 | struct lttng_dynamic_buffer payload; | |
3055 | struct lttcomm_session_destroy_command_header cmd_header; | |
3056 | struct lttng_trace_archive_location *location = NULL; | |
3057 | struct lttcomm_lttng_msg llm = { | |
3058 | .cmd_type = LTTNG_DESTROY_SESSION, | |
3059 | .ret_code = LTTNG_OK, | |
3060 | .pid = UINT32_MAX, | |
3061 | .cmd_header_size = | |
3062 | sizeof(struct lttcomm_session_destroy_command_header), | |
3063 | .data_size = 0, | |
3064 | }; | |
3065 | size_t payload_size_before_location; | |
3066 | ||
3067 | lttng_dynamic_buffer_init(&payload); | |
3068 | ||
3069 | ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm)); | |
3070 | if (ret) { | |
3071 | ERR("Failed to append session destruction message"); | |
3072 | goto error; | |
3073 | } | |
3074 | ||
3075 | cmd_header.rotation_state = | |
3076 | (int32_t) (reply_context->implicit_rotation_on_destroy ? | |
3077 | session->rotation_state : | |
3078 | LTTNG_ROTATION_STATE_NO_ROTATION); | |
3079 | ret = lttng_dynamic_buffer_append(&payload, &cmd_header, | |
3080 | sizeof(cmd_header)); | |
3081 | if (ret) { | |
3082 | ERR("Failed to append session destruction command header"); | |
3083 | goto error; | |
3084 | } | |
3085 | ||
3086 | if (!reply_context->implicit_rotation_on_destroy) { | |
3087 | DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply", | |
3088 | session->name); | |
3089 | goto send_reply; | |
3090 | } | |
3091 | if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) { | |
3092 | DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply", | |
3093 | session->name); | |
3094 | goto send_reply; | |
3095 | } | |
3096 | ||
3097 | location = session_get_trace_archive_location(session); | |
3098 | if (!location) { | |
3099 | ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"", | |
3100 | session->name); | |
3101 | goto error; | |
3102 | } | |
3103 | ||
3104 | payload_size_before_location = payload.size; | |
3105 | comm_ret = lttng_trace_archive_location_serialize(location, | |
3106 | &payload); | |
3107 | if (comm_ret < 0) { | |
3108 | ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"", | |
3109 | session->name); | |
3110 | goto error; | |
3111 | } | |
3112 | /* Update the message to indicate the location's length. */ | |
3113 | ((struct lttcomm_lttng_msg *) payload.data)->data_size = | |
3114 | payload.size - payload_size_before_location; | |
3115 | send_reply: | |
3116 | comm_ret = lttcomm_send_unix_sock(reply_context->reply_sock_fd, | |
3117 | payload.data, payload.size); | |
3118 | if (comm_ret != (ssize_t) payload.size) { | |
3119 | ERR("Failed to send result of the destruction of session \"%s\" to client", | |
3120 | session->name); | |
3121 | } | |
3122 | error: | |
3123 | ret = close(reply_context->reply_sock_fd); | |
3124 | if (ret) { | |
3125 | PERROR("Failed to close client socket in deferred session destroy reply"); | |
3126 | } | |
3127 | lttng_dynamic_buffer_reset(&payload); | |
3128 | free(_reply_context); | |
3129 | } | |
3130 | ||
2f77fc4b DG |
3131 | /* |
3132 | * Command LTTNG_DESTROY_SESSION processed by the client thread. | |
a9ad0c8f MD |
3133 | * |
3134 | * Called with session lock held. | |
2f77fc4b | 3135 | */ |
e32d7f27 | 3136 | int cmd_destroy_session(struct ltt_session *session, |
3e3665b8 JG |
3137 | struct notification_thread_handle *notification_thread_handle, |
3138 | int *sock_fd) | |
2f77fc4b DG |
3139 | { |
3140 | int ret; | |
3e3665b8 JG |
3141 | struct cmd_destroy_session_reply_context *reply_context = NULL; |
3142 | ||
3143 | if (sock_fd) { | |
3144 | reply_context = zmalloc(sizeof(*reply_context)); | |
3145 | if (!reply_context) { | |
3146 | ret = LTTNG_ERR_NOMEM; | |
3147 | goto end; | |
3148 | } | |
3149 | reply_context->reply_sock_fd = *sock_fd; | |
3150 | } | |
2f77fc4b DG |
3151 | |
3152 | /* Safety net */ | |
3153 | assert(session); | |
3154 | ||
3e3665b8 JG |
3155 | DBG("Begin destroy session %s (id %" PRIu64 ")", session->name, |
3156 | session->id); | |
3157 | if (session->active) { | |
3158 | DBG("Session \"%s\" is active, attempting to stop it before destroying it", | |
3159 | session->name); | |
3160 | ret = cmd_stop_trace(session); | |
3161 | if (ret != LTTNG_OK && ret != LTTNG_ERR_TRACE_ALREADY_STOPPED) { | |
3162 | /* Carry on with the destruction of the session. */ | |
3163 | ERR("Failed to stop session \"%s\" as part of its destruction: %s", | |
3164 | session->name, lttng_strerror(-ret)); | |
3165 | } | |
3166 | } | |
5c408ad8 | 3167 | |
92816cc3 JG |
3168 | if (session->rotation_schedule_timer_enabled) { |
3169 | if (timer_session_rotation_schedule_timer_stop( | |
3170 | session)) { | |
3171 | ERR("Failed to stop the \"rotation schedule\" timer of session %s", | |
3172 | session->name); | |
3173 | } | |
259c2674 JD |
3174 | } |
3175 | ||
90936dcf JD |
3176 | if (session->rotate_size) { |
3177 | unsubscribe_session_consumed_size_rotation(session, notification_thread_handle); | |
3178 | session->rotate_size = 0; | |
3179 | } | |
3180 | ||
d2956687 JG |
3181 | if (session->most_recent_chunk_id.is_set && |
3182 | session->most_recent_chunk_id.value != 0 && | |
b5893d8e JG |
3183 | session->current_trace_chunk && session->output_traces) { |
3184 | /* | |
3185 | * Perform a last rotation on destruction if rotations have | |
3186 | * occurred during the session's lifetime. | |
3187 | */ | |
7fdbed1c | 3188 | ret = cmd_rotate_session(session, NULL, false); |
d2956687 JG |
3189 | if (ret != LTTNG_OK) { |
3190 | ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s", | |
3191 | session->name, lttng_strerror(-ret)); | |
124473a3 | 3192 | } |
3e3665b8 JG |
3193 | if (reply_context) { |
3194 | reply_context->implicit_rotation_on_destroy = true; | |
3195 | } | |
7fdbed1c JG |
3196 | } else if (session->has_been_started && session->current_trace_chunk && |
3197 | session_output_supports_trace_chunks(session)) { | |
3198 | /* | |
3199 | * The user has not triggered a session rotation. However, to | |
3200 | * ensure all data has been consumed, the session is rotated | |
3201 | * to a 'null' trace chunk before it is destroyed. | |
3202 | * | |
3203 | * This is a "quiet" rotation meaning that no notification is | |
3204 | * emitted and no renaming of the current trace chunk takes | |
3205 | * place. | |
3206 | */ | |
3207 | ret = cmd_rotate_session(session, NULL, true); | |
3208 | if (ret != LTTNG_OK) { | |
3209 | ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s", | |
3210 | session->name, lttng_strerror(-ret)); | |
3211 | } | |
3212 | } | |
5c408ad8 | 3213 | |
a503e1ef JG |
3214 | if (session->shm_path[0]) { |
3215 | /* | |
3216 | * When a session is created with an explicit shm_path, | |
3217 | * the consumer daemon will create its shared memory files | |
3218 | * at that location and will *not* unlink them. This is normal | |
3219 | * as the intention of that feature is to make it possible | |
3220 | * to retrieve the content of those files should a crash occur. | |
3221 | * | |
3222 | * To ensure the content of those files can be used, the | |
3223 | * sessiond daemon will replicate the content of the metadata | |
3224 | * cache in a metadata file. | |
3225 | * | |
3226 | * On clean-up, it is expected that the consumer daemon will | |
3227 | * unlink the shared memory files and that the session daemon | |
3228 | * will unlink the metadata file. Then, the session's directory | |
3229 | * in the shm path can be removed. | |
3230 | * | |
3231 | * Unfortunately, a flaw in the design of the sessiond's and | |
3232 | * consumerd's tear down of channels makes it impossible to | |
3233 | * determine when the sessiond _and_ the consumerd have both | |
3234 | * destroyed their representation of a channel. For one, the | |
3235 | * unlinking, close, and rmdir happen in deferred 'call_rcu' | |
3236 | * callbacks in both daemons. | |
3237 | * | |
3238 | * However, it is also impossible for the sessiond to know when | |
3239 | * the consumer daemon is done destroying its channel(s) since | |
3240 | * it occurs as a reaction to the closing of the channel's file | |
3241 | * descriptor. There is no resulting communication initiated | |
3242 | * from the consumerd to the sessiond to confirm that the | |
3243 | * operation is completed (and was successful). | |
3244 | * | |
3245 | * Until this is all fixed, the session daemon checks for the | |
3246 | * removal of the session's shm path which makes it possible | |
3247 | * to safely advertise a session as having been destroyed. | |
3248 | * | |
3249 | * Prior to this fix, it was not possible to reliably save | |
3250 | * a session making use of the --shm-path option, destroy it, | |
3251 | * and load it again. This is because the creation of the | |
3252 | * session would fail upon seeing the session's shm path | |
3253 | * already in existence. | |
3254 | * | |
3255 | * Note that none of the error paths in the check for the | |
3256 | * directory's existence return an error. This is normal | |
3257 | * as there isn't much that can be done. The session will | |
3258 | * be destroyed properly, except that we can't offer the | |
3259 | * guarantee that the same session can be re-created. | |
3260 | */ | |
3261 | current_completion_handler = &destroy_completion_handler.handler; | |
3262 | ret = lttng_strncpy(destroy_completion_handler.shm_path, | |
3263 | session->shm_path, | |
3264 | sizeof(destroy_completion_handler.shm_path)); | |
3265 | assert(!ret); | |
3266 | } | |
e32d7f27 JG |
3267 | |
3268 | /* | |
3269 | * The session is destroyed. However, note that the command context | |
3270 | * still holds a reference to the session, thus delaying its destruction | |
3271 | * _at least_ up to the point when that reference is released. | |
3272 | */ | |
3273 | session_destroy(session); | |
3e3665b8 JG |
3274 | if (reply_context) { |
3275 | ret = session_add_destroy_notifier(session, | |
3276 | cmd_destroy_session_reply, | |
3277 | (void *) reply_context); | |
3278 | if (ret) { | |
3279 | ret = LTTNG_ERR_FATAL; | |
3280 | goto end; | |
3281 | } else { | |
3282 | *sock_fd = -1; | |
3283 | } | |
3284 | } | |
3285 | ret = LTTNG_OK; | |
3286 | end: | |
2f77fc4b DG |
3287 | return ret; |
3288 | } | |
3289 | ||
2f77fc4b DG |
3290 | /* |
3291 | * Command LTTNG_REGISTER_CONSUMER processed by the client thread. | |
3292 | */ | |
56a37563 JG |
3293 | int cmd_register_consumer(struct ltt_session *session, |
3294 | enum lttng_domain_type domain, const char *sock_path, | |
3295 | struct consumer_data *cdata) | |
2f77fc4b DG |
3296 | { |
3297 | int ret, sock; | |
dd81b457 | 3298 | struct consumer_socket *socket = NULL; |
2f77fc4b DG |
3299 | |
3300 | assert(session); | |
3301 | assert(cdata); | |
3302 | assert(sock_path); | |
3303 | ||
3304 | switch (domain) { | |
3305 | case LTTNG_DOMAIN_KERNEL: | |
3306 | { | |
3307 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3308 | ||
3309 | assert(ksess); | |
3310 | ||
3311 | /* Can't register a consumer if there is already one */ | |
3312 | if (ksess->consumer_fds_sent != 0) { | |
f73fabfd | 3313 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
2f77fc4b DG |
3314 | goto error; |
3315 | } | |
3316 | ||
3317 | sock = lttcomm_connect_unix_sock(sock_path); | |
3318 | if (sock < 0) { | |
f73fabfd | 3319 | ret = LTTNG_ERR_CONNECT_FAIL; |
2f77fc4b DG |
3320 | goto error; |
3321 | } | |
4ce514c4 | 3322 | cdata->cmd_sock = sock; |
2f77fc4b | 3323 | |
4ce514c4 | 3324 | socket = consumer_allocate_socket(&cdata->cmd_sock); |
2f77fc4b | 3325 | if (socket == NULL) { |
f66c074c DG |
3326 | ret = close(sock); |
3327 | if (ret < 0) { | |
3328 | PERROR("close register consumer"); | |
3329 | } | |
4ce514c4 | 3330 | cdata->cmd_sock = -1; |
f73fabfd | 3331 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
3332 | goto error; |
3333 | } | |
3334 | ||
3335 | socket->lock = zmalloc(sizeof(pthread_mutex_t)); | |
3336 | if (socket->lock == NULL) { | |
3337 | PERROR("zmalloc pthread mutex"); | |
f73fabfd | 3338 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
3339 | goto error; |
3340 | } | |
3341 | pthread_mutex_init(socket->lock, NULL); | |
3342 | socket->registered = 1; | |
3343 | ||
3344 | rcu_read_lock(); | |
3345 | consumer_add_socket(socket, ksess->consumer); | |
3346 | rcu_read_unlock(); | |
3347 | ||
3348 | pthread_mutex_lock(&cdata->pid_mutex); | |
3349 | cdata->pid = -1; | |
3350 | pthread_mutex_unlock(&cdata->pid_mutex); | |
3351 | ||
3352 | break; | |
3353 | } | |
3354 | default: | |
3355 | /* TODO: Userspace tracing */ | |
f73fabfd | 3356 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
3357 | goto error; |
3358 | } | |
3359 | ||
dd81b457 | 3360 | return LTTNG_OK; |
2f77fc4b DG |
3361 | |
3362 | error: | |
dd81b457 DG |
3363 | if (socket) { |
3364 | consumer_destroy_socket(socket); | |
3365 | } | |
2f77fc4b DG |
3366 | return ret; |
3367 | } | |
3368 | ||
3369 | /* | |
3370 | * Command LTTNG_LIST_DOMAINS processed by the client thread. | |
3371 | */ | |
3372 | ssize_t cmd_list_domains(struct ltt_session *session, | |
3373 | struct lttng_domain **domains) | |
3374 | { | |
3375 | int ret, index = 0; | |
3376 | ssize_t nb_dom = 0; | |
fefd409b DG |
3377 | struct agent *agt; |
3378 | struct lttng_ht_iter iter; | |
2f77fc4b DG |
3379 | |
3380 | if (session->kernel_session != NULL) { | |
3381 | DBG3("Listing domains found kernel domain"); | |
3382 | nb_dom++; | |
3383 | } | |
3384 | ||
3385 | if (session->ust_session != NULL) { | |
3386 | DBG3("Listing domains found UST global domain"); | |
3387 | nb_dom++; | |
3c6a091f | 3388 | |
e0a74f01 | 3389 | rcu_read_lock(); |
fefd409b DG |
3390 | cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, |
3391 | agt, node.node) { | |
3392 | if (agt->being_used) { | |
3393 | nb_dom++; | |
3394 | } | |
3c6a091f | 3395 | } |
e0a74f01 | 3396 | rcu_read_unlock(); |
2f77fc4b DG |
3397 | } |
3398 | ||
fa64dfb4 JG |
3399 | if (!nb_dom) { |
3400 | goto end; | |
3401 | } | |
3402 | ||
2f77fc4b DG |
3403 | *domains = zmalloc(nb_dom * sizeof(struct lttng_domain)); |
3404 | if (*domains == NULL) { | |
f73fabfd | 3405 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
3406 | goto error; |
3407 | } | |
3408 | ||
3409 | if (session->kernel_session != NULL) { | |
3410 | (*domains)[index].type = LTTNG_DOMAIN_KERNEL; | |
b5edb9e8 PP |
3411 | |
3412 | /* Kernel session buffer type is always GLOBAL */ | |
3413 | (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL; | |
3414 | ||
2f77fc4b DG |
3415 | index++; |
3416 | } | |
3417 | ||
3418 | if (session->ust_session != NULL) { | |
3419 | (*domains)[index].type = LTTNG_DOMAIN_UST; | |
88c5f0d8 | 3420 | (*domains)[index].buf_type = session->ust_session->buffer_type; |
2f77fc4b | 3421 | index++; |
3c6a091f | 3422 | |
e0a74f01 | 3423 | rcu_read_lock(); |
fefd409b DG |
3424 | cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, |
3425 | agt, node.node) { | |
3426 | if (agt->being_used) { | |
3427 | (*domains)[index].type = agt->domain; | |
3428 | (*domains)[index].buf_type = session->ust_session->buffer_type; | |
3429 | index++; | |
3430 | } | |
3c6a091f | 3431 | } |
e0a74f01 | 3432 | rcu_read_unlock(); |
2f77fc4b | 3433 | } |
fa64dfb4 | 3434 | end: |
2f77fc4b DG |
3435 | return nb_dom; |
3436 | ||
3437 | error: | |
f73fabfd DG |
3438 | /* Return negative value to differentiate return code */ |
3439 | return -ret; | |
2f77fc4b DG |
3440 | } |
3441 | ||
3442 | ||
3443 | /* | |
3444 | * Command LTTNG_LIST_CHANNELS processed by the client thread. | |
3445 | */ | |
56a37563 JG |
3446 | ssize_t cmd_list_channels(enum lttng_domain_type domain, |
3447 | struct ltt_session *session, struct lttng_channel **channels) | |
2f77fc4b | 3448 | { |
53e367f9 | 3449 | ssize_t nb_chan = 0, payload_size = 0, ret; |
2f77fc4b DG |
3450 | |
3451 | switch (domain) { | |
3452 | case LTTNG_DOMAIN_KERNEL: | |
3453 | if (session->kernel_session != NULL) { | |
3454 | nb_chan = session->kernel_session->channel_count; | |
3455 | } | |
3456 | DBG3("Number of kernel channels %zd", nb_chan); | |
c7d620a2 | 3457 | if (nb_chan <= 0) { |
53e367f9 JG |
3458 | ret = -LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
3459 | goto end; | |
c7d620a2 | 3460 | } |
2f77fc4b DG |
3461 | break; |
3462 | case LTTNG_DOMAIN_UST: | |
3463 | if (session->ust_session != NULL) { | |
7ffc31a2 | 3464 | rcu_read_lock(); |
2f77fc4b | 3465 | nb_chan = lttng_ht_get_count( |
7ffc31a2 JG |
3466 | session->ust_session->domain_global.channels); |
3467 | rcu_read_unlock(); | |
2f77fc4b DG |
3468 | } |
3469 | DBG3("Number of UST global channels %zd", nb_chan); | |
ade7ce52 | 3470 | if (nb_chan < 0) { |
53e367f9 JG |
3471 | ret = -LTTNG_ERR_UST_CHAN_NOT_FOUND; |
3472 | goto end; | |
c7d620a2 | 3473 | } |
2f77fc4b DG |
3474 | break; |
3475 | default: | |
53e367f9 JG |
3476 | ret = -LTTNG_ERR_UND; |
3477 | goto end; | |
2f77fc4b DG |
3478 | } |
3479 | ||
3480 | if (nb_chan > 0) { | |
53e367f9 | 3481 | const size_t channel_size = sizeof(struct lttng_channel) + |
cf0bcb51 JG |
3482 | sizeof(struct lttng_channel_extended); |
3483 | struct lttng_channel_extended *channel_exts; | |
53e367f9 JG |
3484 | |
3485 | payload_size = nb_chan * channel_size; | |
3486 | *channels = zmalloc(payload_size); | |
2f77fc4b | 3487 | if (*channels == NULL) { |
53e367f9 JG |
3488 | ret = -LTTNG_ERR_FATAL; |
3489 | goto end; | |
2f77fc4b DG |
3490 | } |
3491 | ||
53e367f9 JG |
3492 | channel_exts = ((void *) *channels) + |
3493 | (nb_chan * sizeof(struct lttng_channel)); | |
d449df4a MD |
3494 | ret = list_lttng_channels(domain, session, *channels, channel_exts); |
3495 | if (ret != LTTNG_OK) { | |
3496 | free(*channels); | |
3497 | *channels = NULL; | |
3498 | goto end; | |
3499 | } | |
53e367f9 JG |
3500 | } else { |
3501 | *channels = NULL; | |
2f77fc4b DG |
3502 | } |
3503 | ||
53e367f9 JG |
3504 | ret = payload_size; |
3505 | end: | |
3506 | return ret; | |
2f77fc4b DG |
3507 | } |
3508 | ||
3509 | /* | |
3510 | * Command LTTNG_LIST_EVENTS processed by the client thread. | |
3511 | */ | |
56a37563 JG |
3512 | ssize_t cmd_list_events(enum lttng_domain_type domain, |
3513 | struct ltt_session *session, char *channel_name, | |
b4e3ceb9 | 3514 | struct lttng_event **events, size_t *total_size) |
2f77fc4b DG |
3515 | { |
3516 | int ret = 0; | |
3517 | ssize_t nb_event = 0; | |
3518 | ||
3519 | switch (domain) { | |
3520 | case LTTNG_DOMAIN_KERNEL: | |
3521 | if (session->kernel_session != NULL) { | |
3522 | nb_event = list_lttng_kernel_events(channel_name, | |
b4e3ceb9 PP |
3523 | session->kernel_session, events, |
3524 | total_size); | |
2f77fc4b DG |
3525 | } |
3526 | break; | |
3527 | case LTTNG_DOMAIN_UST: | |
3528 | { | |
3529 | if (session->ust_session != NULL) { | |
3530 | nb_event = list_lttng_ust_global_events(channel_name, | |
b4e3ceb9 PP |
3531 | &session->ust_session->domain_global, events, |
3532 | total_size); | |
2f77fc4b DG |
3533 | } |
3534 | break; | |
3535 | } | |
5cdb6027 | 3536 | case LTTNG_DOMAIN_LOG4J: |
3c6a091f | 3537 | case LTTNG_DOMAIN_JUL: |
0e115563 | 3538 | case LTTNG_DOMAIN_PYTHON: |
3c6a091f | 3539 | if (session->ust_session) { |
fefd409b DG |
3540 | struct lttng_ht_iter iter; |
3541 | struct agent *agt; | |
3542 | ||
b11feea5 | 3543 | rcu_read_lock(); |
fefd409b DG |
3544 | cds_lfht_for_each_entry(session->ust_session->agents->ht, |
3545 | &iter.iter, agt, node.node) { | |
1dfd9906 JG |
3546 | if (agt->domain == domain) { |
3547 | nb_event = list_lttng_agent_events( | |
b4e3ceb9 PP |
3548 | agt, events, |
3549 | total_size); | |
1dfd9906 JG |
3550 | break; |
3551 | } | |
fefd409b | 3552 | } |
b11feea5 | 3553 | rcu_read_unlock(); |
3c6a091f DG |
3554 | } |
3555 | break; | |
2f77fc4b | 3556 | default: |
f73fabfd | 3557 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
3558 | goto error; |
3559 | } | |
3560 | ||
f73fabfd | 3561 | return nb_event; |
2f77fc4b DG |
3562 | |
3563 | error: | |
f73fabfd DG |
3564 | /* Return negative value to differentiate return code */ |
3565 | return -ret; | |
2f77fc4b DG |
3566 | } |
3567 | ||
3568 | /* | |
3569 | * Using the session list, filled a lttng_session array to send back to the | |
3570 | * client for session listing. | |
3571 | * | |
3572 | * The session list lock MUST be acquired before calling this function. Use | |
3573 | * session_lock_list() and session_unlock_list(). | |
3574 | */ | |
b178f53e JG |
3575 | void cmd_list_lttng_sessions(struct lttng_session *sessions, |
3576 | size_t session_count, uid_t uid, gid_t gid) | |
2f77fc4b DG |
3577 | { |
3578 | int ret; | |
3579 | unsigned int i = 0; | |
3580 | struct ltt_session *session; | |
3581 | struct ltt_session_list *list = session_get_list(); | |
b178f53e JG |
3582 | struct lttng_session_extended *extended = |
3583 | (typeof(extended)) (&sessions[session_count]); | |
2f77fc4b DG |
3584 | |
3585 | DBG("Getting all available session for UID %d GID %d", | |
3586 | uid, gid); | |
3587 | /* | |
3588 | * Iterate over session list and append data after the control struct in | |
3589 | * the buffer. | |
3590 | */ | |
3591 | cds_list_for_each_entry(session, &list->head, list) { | |
e32d7f27 JG |
3592 | if (!session_get(session)) { |
3593 | continue; | |
3594 | } | |
2f77fc4b DG |
3595 | /* |
3596 | * Only list the sessions the user can control. | |
3597 | */ | |
e32d7f27 JG |
3598 | if (!session_access_ok(session, uid, gid) || |
3599 | session->destroyed) { | |
3600 | session_put(session); | |
2f77fc4b DG |
3601 | continue; |
3602 | } | |
3603 | ||
3604 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3605 | struct ltt_ust_session *usess = session->ust_session; | |
3606 | ||
3607 | if (session->consumer->type == CONSUMER_DST_NET || | |
3608 | (ksess && ksess->consumer->type == CONSUMER_DST_NET) || | |
3609 | (usess && usess->consumer->type == CONSUMER_DST_NET)) { | |
3610 | ret = build_network_session_path(sessions[i].path, | |
dec56f6c | 3611 | sizeof(sessions[i].path), session); |
2f77fc4b | 3612 | } else { |
dec56f6c | 3613 | ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s", |
366a9222 | 3614 | session->consumer->dst.session_root_path); |
2f77fc4b DG |
3615 | } |
3616 | if (ret < 0) { | |
3617 | PERROR("snprintf session path"); | |
e32d7f27 | 3618 | session_put(session); |
2f77fc4b DG |
3619 | continue; |
3620 | } | |
3621 | ||
3622 | strncpy(sessions[i].name, session->name, NAME_MAX); | |
3623 | sessions[i].name[NAME_MAX - 1] = '\0'; | |
8382cf6f | 3624 | sessions[i].enabled = session->active; |
2cbf8fed | 3625 | sessions[i].snapshot_mode = session->snapshot_mode; |
8960e9cd | 3626 | sessions[i].live_timer_interval = session->live_timer; |
b178f53e JG |
3627 | extended[i].creation_time.value = (uint64_t) session->creation_time; |
3628 | extended[i].creation_time.is_set = 1; | |
2f77fc4b | 3629 | i++; |
e32d7f27 | 3630 | session_put(session); |
2f77fc4b DG |
3631 | } |
3632 | } | |
3633 | ||
806e2684 | 3634 | /* |
6d805429 | 3635 | * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning |
d3f14b8a | 3636 | * ready for trace analysis (or any kind of reader) or else 1 for pending data. |
806e2684 | 3637 | */ |
6d805429 | 3638 | int cmd_data_pending(struct ltt_session *session) |
806e2684 DG |
3639 | { |
3640 | int ret; | |
3641 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3642 | struct ltt_ust_session *usess = session->ust_session; | |
3643 | ||
3644 | assert(session); | |
3645 | ||
5c408ad8 JD |
3646 | DBG("Data pending for session %s", session->name); |
3647 | ||
806e2684 | 3648 | /* Session MUST be stopped to ask for data availability. */ |
8382cf6f | 3649 | if (session->active) { |
806e2684 DG |
3650 | ret = LTTNG_ERR_SESSION_STARTED; |
3651 | goto error; | |
3a89d11a DG |
3652 | } else { |
3653 | /* | |
3654 | * If stopped, just make sure we've started before else the above call | |
3655 | * will always send that there is data pending. | |
3656 | * | |
3657 | * The consumer assumes that when the data pending command is received, | |
3658 | * the trace has been started before or else no output data is written | |
3659 | * by the streams which is a condition for data pending. So, this is | |
3660 | * *VERY* important that we don't ask the consumer before a start | |
3661 | * trace. | |
3662 | */ | |
8382cf6f | 3663 | if (!session->has_been_started) { |
3a89d11a DG |
3664 | ret = 0; |
3665 | goto error; | |
3666 | } | |
806e2684 DG |
3667 | } |
3668 | ||
92816cc3 JG |
3669 | /* A rotation is still pending, we have to wait. */ |
3670 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { | |
5c408ad8 JD |
3671 | DBG("Rotate still pending for session %s", session->name); |
3672 | ret = 1; | |
3673 | goto error; | |
3674 | } | |
3675 | ||
806e2684 | 3676 | if (ksess && ksess->consumer) { |
6d805429 DG |
3677 | ret = consumer_is_data_pending(ksess->id, ksess->consumer); |
3678 | if (ret == 1) { | |
806e2684 DG |
3679 | /* Data is still being extracted for the kernel. */ |
3680 | goto error; | |
3681 | } | |
3682 | } | |
3683 | ||
3684 | if (usess && usess->consumer) { | |
6d805429 DG |
3685 | ret = consumer_is_data_pending(usess->id, usess->consumer); |
3686 | if (ret == 1) { | |
806e2684 DG |
3687 | /* Data is still being extracted for the kernel. */ |
3688 | goto error; | |
3689 | } | |
3690 | } | |
3691 | ||
3692 | /* Data is ready to be read by a viewer */ | |
6d805429 | 3693 | ret = 0; |
806e2684 DG |
3694 | |
3695 | error: | |
3696 | return ret; | |
3697 | } | |
3698 | ||
6dc3064a DG |
3699 | /* |
3700 | * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library. | |
3701 | * | |
3702 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
3703 | */ | |
3704 | int cmd_snapshot_add_output(struct ltt_session *session, | |
3705 | struct lttng_snapshot_output *output, uint32_t *id) | |
3706 | { | |
3707 | int ret; | |
3708 | struct snapshot_output *new_output; | |
3709 | ||
3710 | assert(session); | |
3711 | assert(output); | |
3712 | ||
3713 | DBG("Cmd snapshot add output for session %s", session->name); | |
3714 | ||
3715 | /* | |
903ef685 | 3716 | * Can't create an output if the session is not set in no-output mode. |
6dc3064a DG |
3717 | */ |
3718 | if (session->output_traces) { | |
903ef685 | 3719 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
3720 | goto error; |
3721 | } | |
3722 | ||
54213acc JG |
3723 | if (session->has_non_mmap_channel) { |
3724 | ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED; | |
3725 | goto error; | |
3726 | } | |
3727 | ||
6dc3064a DG |
3728 | /* Only one output is allowed until we have the "tee" feature. */ |
3729 | if (session->snapshot.nb_output == 1) { | |
3730 | ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST; | |
3731 | goto error; | |
3732 | } | |
3733 | ||
3734 | new_output = snapshot_output_alloc(); | |
3735 | if (!new_output) { | |
3736 | ret = LTTNG_ERR_NOMEM; | |
3737 | goto error; | |
3738 | } | |
3739 | ||
b178f53e | 3740 | ret = snapshot_output_init(session, output->max_size, output->name, |
6dc3064a DG |
3741 | output->ctrl_url, output->data_url, session->consumer, new_output, |
3742 | &session->snapshot); | |
3743 | if (ret < 0) { | |
3744 | if (ret == -ENOMEM) { | |
3745 | ret = LTTNG_ERR_NOMEM; | |
3746 | } else { | |
3747 | ret = LTTNG_ERR_INVALID; | |
3748 | } | |
3749 | goto free_error; | |
3750 | } | |
3751 | ||
6dc3064a DG |
3752 | rcu_read_lock(); |
3753 | snapshot_add_output(&session->snapshot, new_output); | |
3754 | if (id) { | |
3755 | *id = new_output->id; | |
3756 | } | |
3757 | rcu_read_unlock(); | |
3758 | ||
3759 | return LTTNG_OK; | |
3760 | ||
3761 | free_error: | |
3762 | snapshot_output_destroy(new_output); | |
3763 | error: | |
3764 | return ret; | |
3765 | } | |
3766 | ||
3767 | /* | |
3768 | * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl. | |
3769 | * | |
3770 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
3771 | */ | |
3772 | int cmd_snapshot_del_output(struct ltt_session *session, | |
3773 | struct lttng_snapshot_output *output) | |
3774 | { | |
3775 | int ret; | |
eb240553 | 3776 | struct snapshot_output *sout = NULL; |
6dc3064a DG |
3777 | |
3778 | assert(session); | |
3779 | assert(output); | |
3780 | ||
6dc3064a DG |
3781 | rcu_read_lock(); |
3782 | ||
3783 | /* | |
d3f14b8a MD |
3784 | * Permission denied to create an output if the session is not |
3785 | * set in no output mode. | |
6dc3064a DG |
3786 | */ |
3787 | if (session->output_traces) { | |
903ef685 | 3788 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
3789 | goto error; |
3790 | } | |
3791 | ||
eb240553 DG |
3792 | if (output->id) { |
3793 | DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id, | |
3794 | session->name); | |
3795 | sout = snapshot_find_output_by_id(output->id, &session->snapshot); | |
3796 | } else if (*output->name != '\0') { | |
3797 | DBG("Cmd snapshot del output name %s for session %s", output->name, | |
3798 | session->name); | |
3799 | sout = snapshot_find_output_by_name(output->name, &session->snapshot); | |
3800 | } | |
6dc3064a DG |
3801 | if (!sout) { |
3802 | ret = LTTNG_ERR_INVALID; | |
3803 | goto error; | |
3804 | } | |
3805 | ||
3806 | snapshot_delete_output(&session->snapshot, sout); | |
3807 | snapshot_output_destroy(sout); | |
3808 | ret = LTTNG_OK; | |
3809 | ||
3810 | error: | |
3811 | rcu_read_unlock(); | |
3812 | return ret; | |
3813 | } | |
3814 | ||
3815 | /* | |
3816 | * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl. | |
3817 | * | |
3818 | * If no output is available, outputs is untouched and 0 is returned. | |
3819 | * | |
3820 | * Return the size of the newly allocated outputs or a negative LTTNG_ERR code. | |
3821 | */ | |
3822 | ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, | |
3823 | struct lttng_snapshot_output **outputs) | |
3824 | { | |
3825 | int ret, idx = 0; | |
b223ca94 | 3826 | struct lttng_snapshot_output *list = NULL; |
6dc3064a DG |
3827 | struct lttng_ht_iter iter; |
3828 | struct snapshot_output *output; | |
3829 | ||
3830 | assert(session); | |
3831 | assert(outputs); | |
3832 | ||
3833 | DBG("Cmd snapshot list outputs for session %s", session->name); | |
3834 | ||
3835 | /* | |
d3f14b8a MD |
3836 | * Permission denied to create an output if the session is not |
3837 | * set in no output mode. | |
6dc3064a DG |
3838 | */ |
3839 | if (session->output_traces) { | |
903ef685 JG |
3840 | ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
3841 | goto end; | |
6dc3064a DG |
3842 | } |
3843 | ||
3844 | if (session->snapshot.nb_output == 0) { | |
3845 | ret = 0; | |
903ef685 | 3846 | goto end; |
6dc3064a DG |
3847 | } |
3848 | ||
3849 | list = zmalloc(session->snapshot.nb_output * sizeof(*list)); | |
3850 | if (!list) { | |
b223ca94 | 3851 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 3852 | goto end; |
6dc3064a DG |
3853 | } |
3854 | ||
3855 | /* Copy list from session to the new list object. */ | |
b223ca94 | 3856 | rcu_read_lock(); |
6dc3064a DG |
3857 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter, |
3858 | output, node.node) { | |
3859 | assert(output->consumer); | |
3860 | list[idx].id = output->id; | |
3861 | list[idx].max_size = output->max_size; | |
6ce22875 MD |
3862 | if (lttng_strncpy(list[idx].name, output->name, |
3863 | sizeof(list[idx].name))) { | |
3864 | ret = -LTTNG_ERR_INVALID; | |
903ef685 | 3865 | goto error; |
6ce22875 | 3866 | } |
6dc3064a | 3867 | if (output->consumer->type == CONSUMER_DST_LOCAL) { |
6ce22875 | 3868 | if (lttng_strncpy(list[idx].ctrl_url, |
366a9222 | 3869 | output->consumer->dst.session_root_path, |
6ce22875 MD |
3870 | sizeof(list[idx].ctrl_url))) { |
3871 | ret = -LTTNG_ERR_INVALID; | |
903ef685 | 3872 | goto error; |
6ce22875 | 3873 | } |
6dc3064a DG |
3874 | } else { |
3875 | /* Control URI. */ | |
3876 | ret = uri_to_str_url(&output->consumer->dst.net.control, | |
3877 | list[idx].ctrl_url, sizeof(list[idx].ctrl_url)); | |
3878 | if (ret < 0) { | |
b223ca94 | 3879 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 3880 | goto error; |
6dc3064a DG |
3881 | } |
3882 | ||
3883 | /* Data URI. */ | |
3884 | ret = uri_to_str_url(&output->consumer->dst.net.data, | |
3885 | list[idx].data_url, sizeof(list[idx].data_url)); | |
3886 | if (ret < 0) { | |
b223ca94 | 3887 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 3888 | goto error; |
6dc3064a DG |
3889 | } |
3890 | } | |
3891 | idx++; | |
3892 | } | |
3893 | ||
3894 | *outputs = list; | |
b223ca94 JG |
3895 | list = NULL; |
3896 | ret = session->snapshot.nb_output; | |
6dc3064a | 3897 | error: |
903ef685 | 3898 | rcu_read_unlock(); |
b223ca94 | 3899 | free(list); |
903ef685 | 3900 | end: |
b223ca94 | 3901 | return ret; |
6dc3064a DG |
3902 | } |
3903 | ||
93ec662e JD |
3904 | /* |
3905 | * Check if we can regenerate the metadata for this session. | |
3906 | * Only kernel, UST per-uid and non-live sessions are supported. | |
3907 | * | |
3908 | * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise. | |
3909 | */ | |
3910 | static | |
eded6438 | 3911 | int check_regenerate_metadata_support(struct ltt_session *session) |
93ec662e JD |
3912 | { |
3913 | int ret; | |
3914 | ||
3915 | assert(session); | |
3916 | ||
3917 | if (session->live_timer != 0) { | |
3918 | ret = LTTNG_ERR_LIVE_SESSION; | |
3919 | goto end; | |
3920 | } | |
3921 | if (!session->active) { | |
3922 | ret = LTTNG_ERR_SESSION_NOT_STARTED; | |
3923 | goto end; | |
3924 | } | |
3925 | if (session->ust_session) { | |
3926 | switch (session->ust_session->buffer_type) { | |
3927 | case LTTNG_BUFFER_PER_UID: | |
3928 | break; | |
3929 | case LTTNG_BUFFER_PER_PID: | |
3930 | ret = LTTNG_ERR_PER_PID_SESSION; | |
3931 | goto end; | |
3932 | default: | |
3933 | assert(0); | |
3934 | ret = LTTNG_ERR_UNK; | |
3935 | goto end; | |
3936 | } | |
3937 | } | |
3938 | if (session->consumer->type == CONSUMER_DST_NET && | |
3939 | session->consumer->relay_minor_version < 8) { | |
3940 | ret = LTTNG_ERR_RELAYD_VERSION_FAIL; | |
3941 | goto end; | |
3942 | } | |
3943 | ret = 0; | |
3944 | ||
3945 | end: | |
3946 | return ret; | |
3947 | } | |
3948 | ||
7802bae3 LL |
3949 | static |
3950 | int clear_metadata_file(int fd) | |
3951 | { | |
3952 | int ret; | |
a5df8828 | 3953 | off_t lseek_ret; |
7802bae3 | 3954 | |
a5df8828 GL |
3955 | lseek_ret = lseek(fd, 0, SEEK_SET); |
3956 | if (lseek_ret < 0) { | |
7802bae3 | 3957 | PERROR("lseek"); |
a5df8828 | 3958 | ret = -1; |
7802bae3 LL |
3959 | goto end; |
3960 | } | |
3961 | ||
3962 | ret = ftruncate(fd, 0); | |
3963 | if (ret < 0) { | |
3964 | PERROR("ftruncate"); | |
3965 | goto end; | |
3966 | } | |
3967 | ||
3968 | end: | |
3969 | return ret; | |
3970 | } | |
3971 | ||
93ec662e | 3972 | static |
eded6438 | 3973 | int ust_regenerate_metadata(struct ltt_ust_session *usess) |
93ec662e JD |
3974 | { |
3975 | int ret = 0; | |
3976 | struct buffer_reg_uid *uid_reg = NULL; | |
3977 | struct buffer_reg_session *session_reg = NULL; | |
3978 | ||
3979 | rcu_read_lock(); | |
3980 | cds_list_for_each_entry(uid_reg, &usess->buffer_reg_uid_list, lnode) { | |
3981 | struct ust_registry_session *registry; | |
3982 | struct ust_registry_channel *chan; | |
3983 | struct lttng_ht_iter iter_chan; | |
3984 | ||
3985 | session_reg = uid_reg->registry; | |
3986 | registry = session_reg->reg.ust; | |
3987 | ||
3988 | pthread_mutex_lock(®istry->lock); | |
3989 | registry->metadata_len_sent = 0; | |
3990 | memset(registry->metadata, 0, registry->metadata_alloc_len); | |
3991 | registry->metadata_len = 0; | |
3992 | registry->metadata_version++; | |
7802bae3 LL |
3993 | if (registry->metadata_fd > 0) { |
3994 | /* Clear the metadata file's content. */ | |
3995 | ret = clear_metadata_file(registry->metadata_fd); | |
3996 | if (ret) { | |
3997 | pthread_mutex_unlock(®istry->lock); | |
3998 | goto end; | |
3999 | } | |
4000 | } | |
4001 | ||
93ec662e JD |
4002 | ret = ust_metadata_session_statedump(registry, NULL, |
4003 | registry->major, registry->minor); | |
4004 | if (ret) { | |
4005 | pthread_mutex_unlock(®istry->lock); | |
4006 | ERR("Failed to generate session metadata (err = %d)", | |
4007 | ret); | |
4008 | goto end; | |
4009 | } | |
4010 | cds_lfht_for_each_entry(registry->channels->ht, &iter_chan.iter, | |
4011 | chan, node.node) { | |
4012 | struct ust_registry_event *event; | |
4013 | struct lttng_ht_iter iter_event; | |
4014 | ||
4015 | ret = ust_metadata_channel_statedump(registry, chan); | |
4016 | if (ret) { | |
4017 | pthread_mutex_unlock(®istry->lock); | |
4018 | ERR("Failed to generate channel metadata " | |
4019 | "(err = %d)", ret); | |
4020 | goto end; | |
4021 | } | |
4022 | cds_lfht_for_each_entry(chan->ht->ht, &iter_event.iter, | |
4023 | event, node.node) { | |
4024 | ret = ust_metadata_event_statedump(registry, | |
4025 | chan, event); | |
4026 | if (ret) { | |
4027 | pthread_mutex_unlock(®istry->lock); | |
4028 | ERR("Failed to generate event metadata " | |
4029 | "(err = %d)", ret); | |
4030 | goto end; | |
4031 | } | |
4032 | } | |
4033 | } | |
4034 | pthread_mutex_unlock(®istry->lock); | |
4035 | } | |
4036 | ||
4037 | end: | |
4038 | rcu_read_unlock(); | |
4039 | return ret; | |
4040 | } | |
4041 | ||
4042 | /* | |
eded6438 | 4043 | * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library. |
93ec662e JD |
4044 | * |
4045 | * Ask the consumer to truncate the existing metadata file(s) and | |
4046 | * then regenerate the metadata. Live and per-pid sessions are not | |
4047 | * supported and return an error. | |
4048 | * | |
4049 | * Return 0 on success or else a LTTNG_ERR code. | |
4050 | */ | |
eded6438 | 4051 | int cmd_regenerate_metadata(struct ltt_session *session) |
93ec662e JD |
4052 | { |
4053 | int ret; | |
4054 | ||
4055 | assert(session); | |
4056 | ||
eded6438 | 4057 | ret = check_regenerate_metadata_support(session); |
93ec662e JD |
4058 | if (ret) { |
4059 | goto end; | |
4060 | } | |
4061 | ||
4062 | if (session->kernel_session) { | |
eded6438 | 4063 | ret = kernctl_session_regenerate_metadata( |
93ec662e JD |
4064 | session->kernel_session->fd); |
4065 | if (ret < 0) { | |
4066 | ERR("Failed to regenerate the kernel metadata"); | |
4067 | goto end; | |
4068 | } | |
4069 | } | |
4070 | ||
4071 | if (session->ust_session) { | |
eded6438 | 4072 | ret = ust_regenerate_metadata(session->ust_session); |
93ec662e JD |
4073 | if (ret < 0) { |
4074 | ERR("Failed to regenerate the UST metadata"); | |
4075 | goto end; | |
4076 | } | |
4077 | } | |
4078 | DBG("Cmd metadata regenerate for session %s", session->name); | |
4079 | ret = LTTNG_OK; | |
4080 | ||
4081 | end: | |
4082 | return ret; | |
4083 | } | |
4084 | ||
c2561365 JD |
4085 | /* |
4086 | * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library. | |
4087 | * | |
4088 | * Ask the tracer to regenerate a new statedump. | |
4089 | * | |
4090 | * Return 0 on success or else a LTTNG_ERR code. | |
4091 | */ | |
4092 | int cmd_regenerate_statedump(struct ltt_session *session) | |
4093 | { | |
4094 | int ret; | |
4095 | ||
4096 | assert(session); | |
4097 | ||
4098 | if (!session->active) { | |
4099 | ret = LTTNG_ERR_SESSION_NOT_STARTED; | |
4100 | goto end; | |
4101 | } | |
c2561365 JD |
4102 | |
4103 | if (session->kernel_session) { | |
4104 | ret = kernctl_session_regenerate_statedump( | |
4105 | session->kernel_session->fd); | |
4106 | /* | |
4107 | * Currently, the statedump in kernel can only fail if out | |
4108 | * of memory. | |
4109 | */ | |
4110 | if (ret < 0) { | |
4111 | if (ret == -ENOMEM) { | |
4112 | ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM; | |
4113 | } else { | |
4114 | ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL; | |
4115 | } | |
4116 | ERR("Failed to regenerate the kernel statedump"); | |
4117 | goto end; | |
4118 | } | |
4119 | } | |
4120 | ||
4121 | if (session->ust_session) { | |
4122 | ret = ust_app_regenerate_statedump_all(session->ust_session); | |
4123 | /* | |
4124 | * Currently, the statedump in UST always returns 0. | |
4125 | */ | |
4126 | if (ret < 0) { | |
4127 | ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL; | |
4128 | ERR("Failed to regenerate the UST statedump"); | |
4129 | goto end; | |
4130 | } | |
4131 | } | |
4132 | DBG("Cmd regenerate statedump for session %s", session->name); | |
4133 | ret = LTTNG_OK; | |
4134 | ||
4135 | end: | |
4136 | return ret; | |
4137 | } | |
4138 | ||
b0880ae5 JG |
4139 | int cmd_register_trigger(struct command_ctx *cmd_ctx, int sock, |
4140 | struct notification_thread_handle *notification_thread) | |
4141 | { | |
4142 | int ret; | |
4143 | size_t trigger_len; | |
4144 | ssize_t sock_recv_len; | |
4145 | struct lttng_trigger *trigger = NULL; | |
4146 | struct lttng_buffer_view view; | |
4147 | struct lttng_dynamic_buffer trigger_buffer; | |
4148 | ||
4149 | lttng_dynamic_buffer_init(&trigger_buffer); | |
4150 | trigger_len = (size_t) cmd_ctx->lsm->u.trigger.length; | |
4151 | ret = lttng_dynamic_buffer_set_size(&trigger_buffer, trigger_len); | |
4152 | if (ret) { | |
4153 | ret = LTTNG_ERR_NOMEM; | |
4154 | goto end; | |
4155 | } | |
4156 | ||
4157 | sock_recv_len = lttcomm_recv_unix_sock(sock, trigger_buffer.data, | |
4158 | trigger_len); | |
4159 | if (sock_recv_len < 0 || sock_recv_len != trigger_len) { | |
4160 | ERR("Failed to receive \"register trigger\" command payload"); | |
4161 | /* TODO: should this be a new error enum ? */ | |
4162 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
4163 | goto end; | |
4164 | } | |
4165 | ||
4166 | view = lttng_buffer_view_from_dynamic_buffer(&trigger_buffer, 0, -1); | |
4167 | if (lttng_trigger_create_from_buffer(&view, &trigger) != | |
4168 | trigger_len) { | |
4169 | ERR("Invalid trigger payload received in \"register trigger\" command"); | |
4170 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
4171 | goto end; | |
4172 | } | |
4173 | ||
4174 | ret = notification_thread_command_register_trigger(notification_thread, | |
4175 | trigger); | |
587f9fd0 JG |
4176 | /* Ownership of trigger was transferred. */ |
4177 | trigger = NULL; | |
b0880ae5 | 4178 | end: |
587f9fd0 | 4179 | lttng_trigger_destroy(trigger); |
b0880ae5 JG |
4180 | lttng_dynamic_buffer_reset(&trigger_buffer); |
4181 | return ret; | |
4182 | } | |
4183 | ||
4184 | int cmd_unregister_trigger(struct command_ctx *cmd_ctx, int sock, | |
4185 | struct notification_thread_handle *notification_thread) | |
4186 | { | |
4187 | int ret; | |
4188 | size_t trigger_len; | |
4189 | ssize_t sock_recv_len; | |
4190 | struct lttng_trigger *trigger = NULL; | |
4191 | struct lttng_buffer_view view; | |
4192 | struct lttng_dynamic_buffer trigger_buffer; | |
4193 | ||
4194 | lttng_dynamic_buffer_init(&trigger_buffer); | |
4195 | trigger_len = (size_t) cmd_ctx->lsm->u.trigger.length; | |
4196 | ret = lttng_dynamic_buffer_set_size(&trigger_buffer, trigger_len); | |
4197 | if (ret) { | |
4198 | ret = LTTNG_ERR_NOMEM; | |
4199 | goto end; | |
4200 | } | |
4201 | ||
4202 | sock_recv_len = lttcomm_recv_unix_sock(sock, trigger_buffer.data, | |
4203 | trigger_len); | |
4204 | if (sock_recv_len < 0 || sock_recv_len != trigger_len) { | |
4205 | ERR("Failed to receive \"unregister trigger\" command payload"); | |
4206 | /* TODO: should this be a new error enum ? */ | |
4207 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
4208 | goto end; | |
4209 | } | |
4210 | ||
4211 | view = lttng_buffer_view_from_dynamic_buffer(&trigger_buffer, 0, -1); | |
4212 | if (lttng_trigger_create_from_buffer(&view, &trigger) != | |
4213 | trigger_len) { | |
4214 | ERR("Invalid trigger payload received in \"unregister trigger\" command"); | |
4215 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
4216 | goto end; | |
4217 | } | |
4218 | ||
4219 | ret = notification_thread_command_unregister_trigger(notification_thread, | |
4220 | trigger); | |
4221 | end: | |
587f9fd0 | 4222 | lttng_trigger_destroy(trigger); |
b0880ae5 JG |
4223 | lttng_dynamic_buffer_reset(&trigger_buffer); |
4224 | return ret; | |
4225 | } | |
4226 | ||
6dc3064a DG |
4227 | /* |
4228 | * Send relayd sockets from snapshot output to consumer. Ignore request if the | |
4229 | * snapshot output is *not* set with a remote destination. | |
4230 | * | |
9a654598 | 4231 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
6dc3064a | 4232 | */ |
9a654598 | 4233 | static enum lttng_error_code set_relayd_for_snapshot( |
348a81dc | 4234 | struct consumer_output *output, |
fb9a95c4 | 4235 | const struct ltt_session *session) |
6dc3064a | 4236 | { |
9a654598 | 4237 | enum lttng_error_code status = LTTNG_OK; |
6dc3064a DG |
4238 | struct lttng_ht_iter iter; |
4239 | struct consumer_socket *socket; | |
1e791a74 | 4240 | LTTNG_OPTIONAL(uint64_t) current_chunk_id = {}; |
6fa5fe7c | 4241 | const char *base_path; |
6dc3064a | 4242 | |
348a81dc | 4243 | assert(output); |
6dc3064a DG |
4244 | assert(session); |
4245 | ||
4246 | DBG2("Set relayd object from snapshot output"); | |
4247 | ||
1e791a74 | 4248 | if (session->current_trace_chunk) { |
348a81dc JG |
4249 | enum lttng_trace_chunk_status chunk_status = |
4250 | lttng_trace_chunk_get_id( | |
4251 | session->current_trace_chunk, | |
4252 | ¤t_chunk_id.value); | |
1e791a74 | 4253 | |
348a81dc | 4254 | if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) { |
1e791a74 JG |
4255 | current_chunk_id.is_set = true; |
4256 | } else { | |
4257 | ERR("Failed to get current trace chunk id"); | |
4258 | status = LTTNG_ERR_UNK; | |
4259 | goto error; | |
4260 | } | |
4261 | } | |
4262 | ||
6dc3064a | 4263 | /* Ignore if snapshot consumer output is not network. */ |
348a81dc | 4264 | if (output->type != CONSUMER_DST_NET) { |
6dc3064a DG |
4265 | goto error; |
4266 | } | |
4267 | ||
6fa5fe7c MD |
4268 | /* |
4269 | * The snapshot record URI base path overrides the session | |
4270 | * base path. | |
4271 | */ | |
4272 | if (output->dst.net.control.subdir[0] != '\0') { | |
4273 | base_path = output->dst.net.control.subdir; | |
4274 | } else { | |
4275 | base_path = session->base_path; | |
4276 | } | |
4277 | ||
6dc3064a DG |
4278 | /* |
4279 | * For each consumer socket, create and send the relayd object of the | |
4280 | * snapshot output. | |
4281 | */ | |
4282 | rcu_read_lock(); | |
348a81dc | 4283 | cds_lfht_for_each_entry(output->socks->ht, &iter.iter, |
5eecee74 | 4284 | socket, node.node) { |
ecd0f96d | 4285 | pthread_mutex_lock(socket->lock); |
9a654598 | 4286 | status = send_consumer_relayd_sockets(0, session->id, |
348a81dc | 4287 | output, socket, |
d3e2ba59 | 4288 | session->name, session->hostname, |
6fa5fe7c | 4289 | base_path, |
1e791a74 | 4290 | session->live_timer, |
db1da059 JG |
4291 | current_chunk_id.is_set ? ¤t_chunk_id.value : NULL, |
4292 | session->creation_time); | |
ecd0f96d | 4293 | pthread_mutex_unlock(socket->lock); |
9a654598 | 4294 | if (status != LTTNG_OK) { |
6dc3064a DG |
4295 | rcu_read_unlock(); |
4296 | goto error; | |
4297 | } | |
4298 | } | |
4299 | rcu_read_unlock(); | |
4300 | ||
4301 | error: | |
9a654598 | 4302 | return status; |
6dc3064a DG |
4303 | } |
4304 | ||
4305 | /* | |
4306 | * Record a kernel snapshot. | |
4307 | * | |
fac41e72 | 4308 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
6dc3064a | 4309 | */ |
fb9a95c4 JG |
4310 | static enum lttng_error_code record_kernel_snapshot( |
4311 | struct ltt_kernel_session *ksess, | |
348a81dc | 4312 | const struct consumer_output *output, |
fb9a95c4 | 4313 | const struct ltt_session *session, |
d07ceecd | 4314 | int wait, uint64_t nb_packets_per_stream) |
6dc3064a | 4315 | { |
9a654598 | 4316 | enum lttng_error_code status; |
6dc3064a DG |
4317 | |
4318 | assert(ksess); | |
4319 | assert(output); | |
4320 | assert(session); | |
4321 | ||
348a81dc JG |
4322 | status = kernel_snapshot_record( |
4323 | ksess, output, wait, nb_packets_per_stream); | |
9a654598 | 4324 | return status; |
6dc3064a DG |
4325 | } |
4326 | ||
4327 | /* | |
4328 | * Record a UST snapshot. | |
4329 | * | |
9a654598 | 4330 | * Returns LTTNG_OK on success or a LTTNG_ERR error code. |
6dc3064a | 4331 | */ |
9a654598 | 4332 | static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess, |
348a81dc JG |
4333 | const struct consumer_output *output, |
4334 | const struct ltt_session *session, | |
4335 | int wait, uint64_t nb_packets_per_stream) | |
6dc3064a | 4336 | { |
9a654598 | 4337 | enum lttng_error_code status; |
6dc3064a DG |
4338 | |
4339 | assert(usess); | |
4340 | assert(output); | |
4341 | assert(session); | |
4342 | ||
348a81dc JG |
4343 | status = ust_app_snapshot_record( |
4344 | usess, output, wait, nb_packets_per_stream); | |
9a654598 | 4345 | return status; |
6dc3064a DG |
4346 | } |
4347 | ||
d07ceecd | 4348 | static |
fb9a95c4 JG |
4349 | uint64_t get_session_size_one_more_packet_per_stream( |
4350 | const struct ltt_session *session, uint64_t cur_nr_packets) | |
68808f4e | 4351 | { |
d07ceecd | 4352 | uint64_t tot_size = 0; |
68808f4e DG |
4353 | |
4354 | if (session->kernel_session) { | |
4355 | struct ltt_kernel_channel *chan; | |
fb9a95c4 JG |
4356 | const struct ltt_kernel_session *ksess = |
4357 | session->kernel_session; | |
68808f4e | 4358 | |
68808f4e | 4359 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { |
d07ceecd MD |
4360 | if (cur_nr_packets >= chan->channel->attr.num_subbuf) { |
4361 | /* | |
4362 | * Don't take channel into account if we | |
4363 | * already grab all its packets. | |
4364 | */ | |
4365 | continue; | |
68808f4e | 4366 | } |
d07ceecd MD |
4367 | tot_size += chan->channel->attr.subbuf_size |
4368 | * chan->stream_count; | |
68808f4e DG |
4369 | } |
4370 | } | |
4371 | ||
4372 | if (session->ust_session) { | |
fb9a95c4 | 4373 | const struct ltt_ust_session *usess = session->ust_session; |
68808f4e | 4374 | |
d07ceecd MD |
4375 | tot_size += ust_app_get_size_one_more_packet_per_stream(usess, |
4376 | cur_nr_packets); | |
68808f4e DG |
4377 | } |
4378 | ||
d07ceecd | 4379 | return tot_size; |
68808f4e DG |
4380 | } |
4381 | ||
5c786ded | 4382 | /* |
d07ceecd MD |
4383 | * Calculate the number of packets we can grab from each stream that |
4384 | * fits within the overall snapshot max size. | |
4385 | * | |
4386 | * Returns -1 on error, 0 means infinite number of packets, else > 0 is | |
4387 | * the number of packets per stream. | |
4388 | * | |
4389 | * TODO: this approach is not perfect: we consider the worse case | |
4390 | * (packet filling the sub-buffers) as an upper bound, but we could do | |
4391 | * better if we do this calculation while we actually grab the packet | |
4392 | * content: we would know how much padding we don't actually store into | |
4393 | * the file. | |
4394 | * | |
4395 | * This algorithm is currently bounded by the number of packets per | |
4396 | * stream. | |
4397 | * | |
4398 | * Since we call this algorithm before actually grabbing the data, it's | |
4399 | * an approximation: for instance, applications could appear/disappear | |
4400 | * in between this call and actually grabbing data. | |
5c786ded | 4401 | */ |
d07ceecd | 4402 | static |
fb9a95c4 JG |
4403 | int64_t get_session_nb_packets_per_stream(const struct ltt_session *session, |
4404 | uint64_t max_size) | |
5c786ded | 4405 | { |
d07ceecd MD |
4406 | int64_t size_left; |
4407 | uint64_t cur_nb_packets = 0; | |
5c786ded | 4408 | |
d07ceecd MD |
4409 | if (!max_size) { |
4410 | return 0; /* Infinite */ | |
5c786ded JD |
4411 | } |
4412 | ||
d07ceecd MD |
4413 | size_left = max_size; |
4414 | for (;;) { | |
4415 | uint64_t one_more_packet_tot_size; | |
5c786ded | 4416 | |
fb9a95c4 JG |
4417 | one_more_packet_tot_size = get_session_size_one_more_packet_per_stream( |
4418 | session, cur_nb_packets); | |
d07ceecd MD |
4419 | if (!one_more_packet_tot_size) { |
4420 | /* We are already grabbing all packets. */ | |
4421 | break; | |
4422 | } | |
4423 | size_left -= one_more_packet_tot_size; | |
4424 | if (size_left < 0) { | |
4425 | break; | |
4426 | } | |
4427 | cur_nb_packets++; | |
5c786ded | 4428 | } |
d07ceecd MD |
4429 | if (!cur_nb_packets) { |
4430 | /* Not enough room to grab one packet of each stream, error. */ | |
4431 | return -1; | |
4432 | } | |
4433 | return cur_nb_packets; | |
5c786ded JD |
4434 | } |
4435 | ||
fb9a95c4 | 4436 | static |
d2956687 | 4437 | enum lttng_error_code snapshot_record(struct ltt_session *session, |
fb9a95c4 JG |
4438 | const struct snapshot_output *snapshot_output, int wait) |
4439 | { | |
4440 | int64_t nb_packets_per_stream; | |
d2956687 | 4441 | char snapshot_chunk_name[LTTNG_NAME_MAX]; |
348a81dc JG |
4442 | int ret; |
4443 | enum lttng_error_code ret_code = LTTNG_OK; | |
d2956687 | 4444 | struct lttng_trace_chunk *snapshot_trace_chunk; |
348a81dc JG |
4445 | struct consumer_output *original_ust_consumer_output = NULL; |
4446 | struct consumer_output *original_kernel_consumer_output = NULL; | |
4447 | struct consumer_output *snapshot_ust_consumer_output = NULL; | |
4448 | struct consumer_output *snapshot_kernel_consumer_output = NULL; | |
d2956687 | 4449 | |
348a81dc | 4450 | ret = snprintf(snapshot_chunk_name, sizeof(snapshot_chunk_name), |
d2956687 JG |
4451 | "%s-%s-%" PRIu64, |
4452 | snapshot_output->name, | |
4453 | snapshot_output->datetime, | |
4454 | snapshot_output->nb_snapshot); | |
348a81dc | 4455 | if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) { |
d2956687 | 4456 | ERR("Failed to format snapshot name"); |
348a81dc JG |
4457 | ret_code = LTTNG_ERR_INVALID; |
4458 | goto error; | |
d2956687 JG |
4459 | } |
4460 | DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"", | |
4461 | snapshot_output->name, session->name, | |
4462 | snapshot_chunk_name); | |
348a81dc JG |
4463 | if (!session->kernel_session && !session->ust_session) { |
4464 | ERR("Failed to record snapshot as no channels exist"); | |
4465 | ret_code = LTTNG_ERR_NO_CHANNEL; | |
4466 | goto error; | |
4467 | } | |
4468 | ||
4469 | if (session->kernel_session) { | |
4470 | original_kernel_consumer_output = | |
4471 | session->kernel_session->consumer; | |
4472 | snapshot_kernel_consumer_output = | |
4473 | consumer_copy_output(snapshot_output->consumer); | |
4474 | ret = consumer_copy_sockets(snapshot_kernel_consumer_output, | |
4475 | original_kernel_consumer_output); | |
4476 | if (ret < 0) { | |
4477 | ERR("Failed to copy consumer sockets from snapshot output configuration"); | |
4478 | ret_code = LTTNG_ERR_NOMEM; | |
4479 | goto error; | |
4480 | } | |
4481 | ret_code = set_relayd_for_snapshot( | |
4482 | snapshot_kernel_consumer_output, session); | |
4483 | if (ret_code != LTTNG_OK) { | |
4484 | ERR("Failed to setup relay daemon for kernel tracer snapshot"); | |
4485 | goto error; | |
4486 | } | |
4487 | session->kernel_session->consumer = | |
4488 | snapshot_kernel_consumer_output; | |
4489 | } | |
4490 | if (session->ust_session) { | |
4491 | original_ust_consumer_output = session->ust_session->consumer; | |
4492 | snapshot_ust_consumer_output = | |
4493 | consumer_copy_output(snapshot_output->consumer); | |
4494 | ret = consumer_copy_sockets(snapshot_ust_consumer_output, | |
4495 | original_ust_consumer_output); | |
4496 | if (ret < 0) { | |
4497 | ERR("Failed to copy consumer sockets from snapshot output configuration"); | |
4498 | ret_code = LTTNG_ERR_NOMEM; | |
4499 | goto error; | |
4500 | } | |
4501 | ret_code = set_relayd_for_snapshot( | |
4502 | snapshot_ust_consumer_output, session); | |
4503 | if (ret_code != LTTNG_OK) { | |
4504 | ERR("Failed to setup relay daemon for userspace tracer snapshot"); | |
4505 | goto error; | |
4506 | } | |
4507 | session->ust_session->consumer = | |
4508 | snapshot_ust_consumer_output; | |
4509 | } | |
4510 | ||
d2956687 | 4511 | snapshot_trace_chunk = session_create_new_trace_chunk(session, |
348a81dc JG |
4512 | snapshot_kernel_consumer_output ?: |
4513 | snapshot_ust_consumer_output, | |
4514 | consumer_output_get_base_path( | |
4515 | snapshot_output->consumer), | |
d2956687 JG |
4516 | snapshot_chunk_name); |
4517 | if (!snapshot_trace_chunk) { | |
348a81dc JG |
4518 | ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"", |
4519 | session->name); | |
4520 | ret_code = LTTNG_ERR_CREATE_DIR_FAIL; | |
4521 | goto error; | |
d2956687 JG |
4522 | } |
4523 | assert(!session->current_trace_chunk); | |
4524 | ret = session_set_trace_chunk(session, snapshot_trace_chunk, NULL); | |
4525 | lttng_trace_chunk_put(snapshot_trace_chunk); | |
4526 | snapshot_trace_chunk = NULL; | |
4527 | if (ret) { | |
348a81dc JG |
4528 | ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"", |
4529 | session->name); | |
4530 | ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; | |
4531 | goto error; | |
d2956687 | 4532 | } |
fb9a95c4 JG |
4533 | |
4534 | nb_packets_per_stream = get_session_nb_packets_per_stream(session, | |
4535 | snapshot_output->max_size); | |
4536 | if (nb_packets_per_stream < 0) { | |
348a81dc JG |
4537 | ret_code = LTTNG_ERR_MAX_SIZE_INVALID; |
4538 | goto error; | |
fb9a95c4 JG |
4539 | } |
4540 | ||
4541 | if (session->kernel_session) { | |
348a81dc JG |
4542 | ret_code = record_kernel_snapshot(session->kernel_session, |
4543 | snapshot_kernel_consumer_output, session, | |
fb9a95c4 | 4544 | wait, nb_packets_per_stream); |
348a81dc JG |
4545 | if (ret_code != LTTNG_OK) { |
4546 | goto error; | |
fb9a95c4 JG |
4547 | } |
4548 | } | |
4549 | ||
4550 | if (session->ust_session) { | |
348a81dc JG |
4551 | ret_code = record_ust_snapshot(session->ust_session, |
4552 | snapshot_ust_consumer_output, session, | |
fb9a95c4 | 4553 | wait, nb_packets_per_stream); |
348a81dc JG |
4554 | if (ret_code != LTTNG_OK) { |
4555 | goto error; | |
fb9a95c4 JG |
4556 | } |
4557 | } | |
d2956687 | 4558 | |
bbc4768c JG |
4559 | if (session_close_trace_chunk( |
4560 | session, session->current_trace_chunk, NULL)) { | |
d2956687 JG |
4561 | /* |
4562 | * Don't goto end; make sure the chunk is closed for the session | |
4563 | * to allow future snapshots. | |
4564 | */ | |
4565 | ERR("Failed to close snapshot trace chunk of session \"%s\"", | |
4566 | session->name); | |
348a81dc | 4567 | ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER; |
d2956687 JG |
4568 | } |
4569 | if (session_set_trace_chunk(session, NULL, NULL)) { | |
4570 | ERR("Failed to release the current trace chunk of session \"%s\"", | |
4571 | session->name); | |
348a81dc | 4572 | ret_code = LTTNG_ERR_UNK; |
d2956687 | 4573 | } |
348a81dc JG |
4574 | error: |
4575 | if (original_ust_consumer_output) { | |
4576 | session->ust_session->consumer = original_ust_consumer_output; | |
4577 | } | |
4578 | if (original_kernel_consumer_output) { | |
4579 | session->kernel_session->consumer = | |
4580 | original_kernel_consumer_output; | |
4581 | } | |
4582 | consumer_output_put(snapshot_ust_consumer_output); | |
4583 | consumer_output_put(snapshot_kernel_consumer_output); | |
4584 | return ret_code; | |
fb9a95c4 JG |
4585 | } |
4586 | ||
6dc3064a DG |
4587 | /* |
4588 | * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl. | |
4589 | * | |
4590 | * The wait parameter is ignored so this call always wait for the snapshot to | |
4591 | * complete before returning. | |
4592 | * | |
4593 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
4594 | */ | |
4595 | int cmd_snapshot_record(struct ltt_session *session, | |
4596 | struct lttng_snapshot_output *output, int wait) | |
4597 | { | |
9a654598 JG |
4598 | enum lttng_error_code cmd_ret = LTTNG_OK; |
4599 | int ret; | |
00e1dfc4 | 4600 | unsigned int snapshot_success = 0; |
10ba83fe | 4601 | char datetime[16]; |
2abe7969 | 4602 | struct snapshot_output *tmp_output = NULL; |
6dc3064a DG |
4603 | |
4604 | assert(session); | |
ba45d9f0 | 4605 | assert(output); |
6dc3064a DG |
4606 | |
4607 | DBG("Cmd snapshot record for session %s", session->name); | |
4608 | ||
10ba83fe JR |
4609 | /* Get the datetime for the snapshot output directory. */ |
4610 | ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime, | |
4611 | sizeof(datetime)); | |
4612 | if (!ret) { | |
9a654598 | 4613 | cmd_ret = LTTNG_ERR_INVALID; |
10ba83fe JR |
4614 | goto error; |
4615 | } | |
4616 | ||
6dc3064a | 4617 | /* |
d3f14b8a MD |
4618 | * Permission denied to create an output if the session is not |
4619 | * set in no output mode. | |
6dc3064a DG |
4620 | */ |
4621 | if (session->output_traces) { | |
9a654598 | 4622 | cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
4623 | goto error; |
4624 | } | |
4625 | ||
4626 | /* The session needs to be started at least once. */ | |
8382cf6f | 4627 | if (!session->has_been_started) { |
9a654598 | 4628 | cmd_ret = LTTNG_ERR_START_SESSION_ONCE; |
6dc3064a DG |
4629 | goto error; |
4630 | } | |
4631 | ||
4632 | /* Use temporary output for the session. */ | |
ba45d9f0 | 4633 | if (*output->ctrl_url != '\0') { |
2abe7969 JG |
4634 | tmp_output = snapshot_output_alloc(); |
4635 | if (!tmp_output) { | |
4636 | cmd_ret = LTTNG_ERR_NOMEM; | |
4637 | goto error; | |
4638 | } | |
4639 | ||
b178f53e JG |
4640 | ret = snapshot_output_init(session, output->max_size, |
4641 | output->name, | |
4642 | output->ctrl_url, output->data_url, | |
4643 | session->consumer, | |
2abe7969 | 4644 | tmp_output, NULL); |
6dc3064a DG |
4645 | if (ret < 0) { |
4646 | if (ret == -ENOMEM) { | |
9a654598 | 4647 | cmd_ret = LTTNG_ERR_NOMEM; |
6dc3064a | 4648 | } else { |
9a654598 | 4649 | cmd_ret = LTTNG_ERR_INVALID; |
6dc3064a DG |
4650 | } |
4651 | goto error; | |
4652 | } | |
1bfe7328 | 4653 | /* Use the global session count for the temporary snapshot. */ |
2abe7969 | 4654 | tmp_output->nb_snapshot = session->snapshot.nb_snapshot; |
10ba83fe JR |
4655 | |
4656 | /* Use the global datetime */ | |
2abe7969 JG |
4657 | memcpy(tmp_output->datetime, datetime, sizeof(datetime)); |
4658 | cmd_ret = snapshot_record(session, tmp_output, wait); | |
fb9a95c4 | 4659 | if (cmd_ret != LTTNG_OK) { |
804c90a8 JR |
4660 | goto error; |
4661 | } | |
804c90a8 JR |
4662 | snapshot_success = 1; |
4663 | } else { | |
4664 | struct snapshot_output *sout; | |
4665 | struct lttng_ht_iter iter; | |
68808f4e | 4666 | |
804c90a8 JR |
4667 | rcu_read_lock(); |
4668 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, | |
4669 | &iter.iter, sout, node.node) { | |
2abe7969 JG |
4670 | struct snapshot_output output_copy; |
4671 | ||
804c90a8 | 4672 | /* |
2abe7969 JG |
4673 | * Make a local copy of the output and override output |
4674 | * parameters with those provided as part of the | |
4675 | * command. | |
804c90a8 | 4676 | */ |
2abe7969 | 4677 | memcpy(&output_copy, sout, sizeof(output_copy)); |
1bfe7328 | 4678 | |
804c90a8 | 4679 | if (output->max_size != (uint64_t) -1ULL) { |
2abe7969 | 4680 | output_copy.max_size = output->max_size; |
6dc3064a | 4681 | } |
d07ceecd | 4682 | |
2abe7969 JG |
4683 | output_copy.nb_snapshot = session->snapshot.nb_snapshot; |
4684 | memcpy(output_copy.datetime, datetime, | |
4685 | sizeof(datetime)); | |
6dc3064a | 4686 | |
804c90a8 JR |
4687 | /* Use temporary name. */ |
4688 | if (*output->name != '\0') { | |
2abe7969 JG |
4689 | if (lttng_strncpy(output_copy.name, |
4690 | output->name, | |
4691 | sizeof(output_copy.name))) { | |
9a654598 | 4692 | cmd_ret = LTTNG_ERR_INVALID; |
cf3e357d MD |
4693 | rcu_read_unlock(); |
4694 | goto error; | |
4695 | } | |
804c90a8 | 4696 | } |
e1986656 | 4697 | |
2abe7969 | 4698 | cmd_ret = snapshot_record(session, &output_copy, wait); |
fb9a95c4 JG |
4699 | if (cmd_ret != LTTNG_OK) { |
4700 | rcu_read_unlock(); | |
4701 | goto error; | |
6dc3064a | 4702 | } |
804c90a8 | 4703 | snapshot_success = 1; |
6dc3064a | 4704 | } |
804c90a8 | 4705 | rcu_read_unlock(); |
6dc3064a DG |
4706 | } |
4707 | ||
1bfe7328 DG |
4708 | if (snapshot_success) { |
4709 | session->snapshot.nb_snapshot++; | |
b67578cb | 4710 | } else { |
9a654598 | 4711 | cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL; |
1bfe7328 DG |
4712 | } |
4713 | ||
6dc3064a | 4714 | error: |
2abe7969 JG |
4715 | if (tmp_output) { |
4716 | snapshot_output_destroy(tmp_output); | |
4717 | } | |
9a654598 | 4718 | return cmd_ret; |
6dc3064a DG |
4719 | } |
4720 | ||
d7ba1388 MD |
4721 | /* |
4722 | * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread. | |
4723 | */ | |
4724 | int cmd_set_session_shm_path(struct ltt_session *session, | |
4725 | const char *shm_path) | |
4726 | { | |
4727 | /* Safety net */ | |
4728 | assert(session); | |
4729 | ||
4730 | /* | |
4731 | * Can only set shm path before session is started. | |
4732 | */ | |
4733 | if (session->has_been_started) { | |
4734 | return LTTNG_ERR_SESSION_STARTED; | |
4735 | } | |
4736 | ||
4737 | strncpy(session->shm_path, shm_path, | |
4738 | sizeof(session->shm_path)); | |
4739 | session->shm_path[sizeof(session->shm_path) - 1] = '\0'; | |
4740 | ||
4741 | return 0; | |
4742 | } | |
4743 | ||
5c408ad8 JD |
4744 | /* |
4745 | * Command LTTNG_ROTATE_SESSION from the lttng-ctl library. | |
4746 | * | |
4747 | * Ask the consumer to rotate the session output directory. | |
4748 | * The session lock must be held. | |
4749 | * | |
d5a1b7aa | 4750 | * Returns LTTNG_OK on success or else a negative LTTng error code. |
5c408ad8 JD |
4751 | */ |
4752 | int cmd_rotate_session(struct ltt_session *session, | |
7fdbed1c JG |
4753 | struct lttng_rotate_session_return *rotate_return, |
4754 | bool quiet_rotation) | |
5c408ad8 JD |
4755 | { |
4756 | int ret; | |
d2956687 | 4757 | uint64_t ongoing_rotation_chunk_id; |
d5a1b7aa | 4758 | enum lttng_error_code cmd_ret = LTTNG_OK; |
d2956687 JG |
4759 | struct lttng_trace_chunk *chunk_being_archived = NULL; |
4760 | struct lttng_trace_chunk *new_trace_chunk = NULL; | |
4761 | enum lttng_trace_chunk_status chunk_status; | |
5c408ad8 JD |
4762 | |
4763 | assert(session); | |
4764 | ||
4765 | if (!session->has_been_started) { | |
d5a1b7aa | 4766 | cmd_ret = LTTNG_ERR_START_SESSION_ONCE; |
d68c9a04 | 4767 | goto end; |
5c408ad8 JD |
4768 | } |
4769 | ||
d48d65e1 MD |
4770 | /* |
4771 | * Explicit rotation is not supported for live sessions. | |
4772 | * However, live sessions can perform a quiet rotation on | |
4773 | * destroy. | |
4774 | * Rotation is not supported for snapshot traces (no output). | |
4775 | */ | |
4776 | if ((!quiet_rotation && session->live_timer) || | |
4777 | !session->output_traces) { | |
d5a1b7aa | 4778 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE; |
d68c9a04 | 4779 | goto end; |
5c408ad8 JD |
4780 | } |
4781 | ||
d2956687 | 4782 | /* Unsupported feature in lttng-relayd before 2.11. */ |
5c408ad8 JD |
4783 | if (session->consumer->type == CONSUMER_DST_NET && |
4784 | (session->consumer->relay_major_version == 2 && | |
4785 | session->consumer->relay_minor_version < 11)) { | |
d5a1b7aa | 4786 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY; |
d68c9a04 | 4787 | goto end; |
5c408ad8 JD |
4788 | } |
4789 | ||
92816cc3 | 4790 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { |
92816cc3 JG |
4791 | DBG("Refusing to launch a rotation; a rotation is already in progress for session %s", |
4792 | session->name); | |
d5a1b7aa | 4793 | cmd_ret = LTTNG_ERR_ROTATION_PENDING; |
d68c9a04 | 4794 | goto end; |
5c408ad8 JD |
4795 | } |
4796 | ||
4797 | /* | |
4798 | * After a stop, we only allow one rotation to occur, the other ones are | |
4799 | * useless until a new start. | |
4800 | */ | |
4801 | if (session->rotated_after_last_stop) { | |
4802 | DBG("Session \"%s\" was already rotated after stop, refusing rotation", | |
4803 | session->name); | |
d5a1b7aa | 4804 | cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP; |
d68c9a04 | 4805 | goto end; |
5c408ad8 JD |
4806 | } |
4807 | ||
d2956687 | 4808 | session->rotation_state = LTTNG_ROTATION_STATE_ONGOING; |
92816cc3 | 4809 | |
d2956687 | 4810 | if (session->active) { |
348a81dc | 4811 | new_trace_chunk = session_create_new_trace_chunk(session, NULL, |
d2956687 JG |
4812 | NULL, NULL); |
4813 | if (!new_trace_chunk) { | |
4814 | cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
4815 | goto error; | |
5c408ad8 | 4816 | } |
d2956687 | 4817 | } |
2961f09e | 4818 | |
d2956687 JG |
4819 | /* The current trace chunk becomes the chunk being archived. */ |
4820 | ret = session_set_trace_chunk(session, new_trace_chunk, | |
4821 | &chunk_being_archived); | |
4822 | if (ret) { | |
4823 | cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; | |
b178f53e JG |
4824 | goto error; |
4825 | } | |
4826 | ||
d2956687 JG |
4827 | assert(chunk_being_archived); |
4828 | chunk_status = lttng_trace_chunk_get_id(chunk_being_archived, | |
4829 | &ongoing_rotation_chunk_id); | |
4830 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); | |
4831 | ||
5c408ad8 | 4832 | if (session->kernel_session) { |
d5a1b7aa JG |
4833 | cmd_ret = kernel_rotate_session(session); |
4834 | if (cmd_ret != LTTNG_OK) { | |
2961f09e | 4835 | goto error; |
5c408ad8 JD |
4836 | } |
4837 | } | |
4838 | if (session->ust_session) { | |
d5a1b7aa JG |
4839 | cmd_ret = ust_app_rotate_session(session); |
4840 | if (cmd_ret != LTTNG_OK) { | |
2961f09e | 4841 | goto error; |
5c408ad8 | 4842 | } |
92816cc3 | 4843 | } |
17dd1232 | 4844 | |
bbc4768c | 4845 | ret = session_close_trace_chunk(session, chunk_being_archived, |
7fdbed1c JG |
4846 | quiet_rotation ? |
4847 | NULL : | |
4848 | &((enum lttng_trace_chunk_command_type){ | |
4849 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED})); | |
d2956687 JG |
4850 | if (ret) { |
4851 | cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER; | |
4852 | goto error; | |
4853 | } | |
4854 | ||
7fdbed1c | 4855 | session->quiet_rotation = quiet_rotation; |
92816cc3 JG |
4856 | ret = timer_session_rotation_pending_check_start(session, |
4857 | DEFAULT_ROTATE_PENDING_TIMER); | |
4858 | if (ret) { | |
d5a1b7aa | 4859 | cmd_ret = LTTNG_ERR_UNK; |
2961f09e | 4860 | goto error; |
5c408ad8 JD |
4861 | } |
4862 | ||
4863 | if (!session->active) { | |
4864 | session->rotated_after_last_stop = true; | |
4865 | } | |
4866 | ||
4867 | if (rotate_return) { | |
d2956687 | 4868 | rotate_return->rotation_id = ongoing_rotation_chunk_id; |
5c408ad8 JD |
4869 | } |
4870 | ||
d2956687 JG |
4871 | session->chunk_being_archived = chunk_being_archived; |
4872 | chunk_being_archived = NULL; | |
7fdbed1c JG |
4873 | if (!quiet_rotation) { |
4874 | ret = notification_thread_command_session_rotation_ongoing( | |
4875 | notification_thread_handle, | |
4876 | session->name, session->uid, session->gid, | |
4877 | ongoing_rotation_chunk_id); | |
4878 | if (ret != LTTNG_OK) { | |
4879 | ERR("Failed to notify notification thread that a session rotation is ongoing for session %s", | |
4880 | session->name); | |
4881 | cmd_ret = ret; | |
4882 | } | |
2961f09e JG |
4883 | } |
4884 | ||
92816cc3 | 4885 | DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent", |
d2956687 | 4886 | session->name, ongoing_rotation_chunk_id); |
5c408ad8 | 4887 | end: |
d2956687 JG |
4888 | lttng_trace_chunk_put(new_trace_chunk); |
4889 | lttng_trace_chunk_put(chunk_being_archived); | |
d5a1b7aa | 4890 | ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret); |
5c408ad8 | 4891 | return ret; |
2961f09e | 4892 | error: |
2961f09e | 4893 | if (session_reset_rotation_state(session, |
d2956687 | 4894 | LTTNG_ROTATION_STATE_ERROR)) { |
2961f09e JG |
4895 | ERR("Failed to reset rotation state of session \"%s\"", |
4896 | session->name); | |
4897 | } | |
4898 | goto end; | |
5c408ad8 JD |
4899 | } |
4900 | ||
4901 | /* | |
d68c9a04 | 4902 | * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library. |
5c408ad8 JD |
4903 | * |
4904 | * Check if the session has finished its rotation. | |
4905 | * | |
d2956687 | 4906 | * Return LTTNG_OK on success or else an LTTNG_ERR code. |
5c408ad8 | 4907 | */ |
d68c9a04 JD |
4908 | int cmd_rotate_get_info(struct ltt_session *session, |
4909 | struct lttng_rotation_get_info_return *info_return, | |
4910 | uint64_t rotation_id) | |
5c408ad8 | 4911 | { |
d2956687 JG |
4912 | enum lttng_error_code cmd_ret = LTTNG_OK; |
4913 | enum lttng_rotation_state rotation_state; | |
5c408ad8 | 4914 | |
d68c9a04 | 4915 | DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64, session->name, |
d2956687 | 4916 | session->most_recent_chunk_id.value); |
5c408ad8 | 4917 | |
d2956687 JG |
4918 | if (session->chunk_being_archived) { |
4919 | enum lttng_trace_chunk_status chunk_status; | |
4920 | uint64_t chunk_id; | |
4921 | ||
4922 | chunk_status = lttng_trace_chunk_get_id( | |
4923 | session->chunk_being_archived, | |
4924 | &chunk_id); | |
4925 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); | |
4926 | ||
4927 | rotation_state = rotation_id == chunk_id ? | |
4928 | LTTNG_ROTATION_STATE_ONGOING : | |
4929 | LTTNG_ROTATION_STATE_EXPIRED; | |
4930 | } else { | |
4931 | if (session->last_archived_chunk_id.is_set && | |
4932 | rotation_id != session->last_archived_chunk_id.value) { | |
4933 | rotation_state = LTTNG_ROTATION_STATE_EXPIRED; | |
4934 | } else { | |
4935 | rotation_state = session->rotation_state; | |
4936 | } | |
5c408ad8 JD |
4937 | } |
4938 | ||
d2956687 JG |
4939 | switch (rotation_state) { |
4940 | case LTTNG_ROTATION_STATE_NO_ROTATION: | |
4941 | DBG("Reporting that no rotation has occured within the lifetime of session \"%s\"", | |
4942 | session->name); | |
4943 | goto end; | |
4944 | case LTTNG_ROTATION_STATE_EXPIRED: | |
4945 | DBG("Reporting that the rotation state of rotation id %" PRIu64 " of session \"%s\" has expired", | |
4946 | rotation_id, session->name); | |
4947 | break; | |
d68c9a04 | 4948 | case LTTNG_ROTATION_STATE_ONGOING: |
d2956687 | 4949 | DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending", |
d68c9a04 JD |
4950 | rotation_id, session->name); |
4951 | break; | |
4952 | case LTTNG_ROTATION_STATE_COMPLETED: | |
dd73d57b | 4953 | { |
d2956687 JG |
4954 | int fmt_ret; |
4955 | char *chunk_path; | |
dd73d57b JG |
4956 | char *current_tracing_path_reply; |
4957 | size_t current_tracing_path_reply_len; | |
4958 | ||
d2956687 JG |
4959 | DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed", |
4960 | rotation_id, session->name); | |
4961 | ||
dd73d57b JG |
4962 | switch (session_get_consumer_destination_type(session)) { |
4963 | case CONSUMER_DST_LOCAL: | |
4964 | current_tracing_path_reply = | |
4965 | info_return->location.local.absolute_path; | |
4966 | current_tracing_path_reply_len = | |
4967 | sizeof(info_return->location.local.absolute_path); | |
4968 | info_return->location_type = | |
05f8afa9 | 4969 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL; |
dd73d57b JG |
4970 | break; |
4971 | case CONSUMER_DST_NET: | |
4972 | current_tracing_path_reply = | |
4973 | info_return->location.relay.relative_path; | |
4974 | current_tracing_path_reply_len = | |
4975 | sizeof(info_return->location.relay.relative_path); | |
4976 | /* Currently the only supported relay protocol. */ | |
4977 | info_return->location.relay.protocol = | |
05f8afa9 | 4978 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP; |
dd73d57b | 4979 | |
d2956687 | 4980 | fmt_ret = lttng_strncpy(info_return->location.relay.host, |
dd73d57b JG |
4981 | session_get_net_consumer_hostname(session), |
4982 | sizeof(info_return->location.relay.host)); | |
d2956687 JG |
4983 | if (fmt_ret) { |
4984 | ERR("Failed to copy host name to rotate_get_info reply"); | |
dd73d57b | 4985 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
d2956687 | 4986 | cmd_ret = LTTNG_ERR_SET_URL; |
dd73d57b JG |
4987 | goto end; |
4988 | } | |
4989 | ||
4990 | session_get_net_consumer_ports(session, | |
4991 | &info_return->location.relay.ports.control, | |
4992 | &info_return->location.relay.ports.data); | |
4993 | info_return->location_type = | |
05f8afa9 | 4994 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY; |
dd73d57b JG |
4995 | break; |
4996 | default: | |
4997 | abort(); | |
4998 | } | |
d2956687 JG |
4999 | fmt_ret = asprintf(&chunk_path, |
5000 | "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s", | |
5001 | session_get_base_path(session), | |
5002 | session->last_archived_chunk_name); | |
5003 | if (fmt_ret == -1) { | |
5004 | PERROR("Failed to format the path of the last archived trace chunk"); | |
5005 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; | |
5006 | cmd_ret = LTTNG_ERR_UNK; | |
5007 | goto end; | |
5008 | } | |
5009 | ||
5010 | fmt_ret = lttng_strncpy(current_tracing_path_reply, | |
5011 | chunk_path, current_tracing_path_reply_len); | |
5012 | free(chunk_path); | |
5013 | if (fmt_ret) { | |
5014 | ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply"); | |
d68c9a04 | 5015 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
d2956687 | 5016 | cmd_ret = LTTNG_ERR_UNK; |
5c408ad8 JD |
5017 | goto end; |
5018 | } | |
dd73d57b | 5019 | |
d68c9a04 | 5020 | break; |
dd73d57b | 5021 | } |
d68c9a04 | 5022 | case LTTNG_ROTATION_STATE_ERROR: |
d2956687 | 5023 | DBG("Reporting that an error occurred during rotation %" PRIu64 " of session \"%s\"", |
d68c9a04 JD |
5024 | rotation_id, session->name); |
5025 | break; | |
5026 | default: | |
5027 | abort(); | |
5c408ad8 JD |
5028 | } |
5029 | ||
d2956687 | 5030 | cmd_ret = LTTNG_OK; |
5c408ad8 | 5031 | end: |
d2956687 JG |
5032 | info_return->status = (int32_t) rotation_state; |
5033 | return cmd_ret; | |
5c408ad8 JD |
5034 | } |
5035 | ||
259c2674 JD |
5036 | /* |
5037 | * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library. | |
5038 | * | |
5039 | * Configure the automatic rotation parameters. | |
66ea93b1 JG |
5040 | * 'activate' to true means activate the rotation schedule type with 'new_value'. |
5041 | * 'activate' to false means deactivate the rotation schedule and validate that | |
5042 | * 'new_value' has the same value as the currently active value. | |
259c2674 | 5043 | * |
66ea93b1 | 5044 | * Return 0 on success or else a positive LTTNG_ERR code. |
259c2674 JD |
5045 | */ |
5046 | int cmd_rotation_set_schedule(struct ltt_session *session, | |
66ea93b1 JG |
5047 | bool activate, enum lttng_rotation_schedule_type schedule_type, |
5048 | uint64_t new_value, | |
90936dcf | 5049 | struct notification_thread_handle *notification_thread_handle) |
259c2674 JD |
5050 | { |
5051 | int ret; | |
66ea93b1 | 5052 | uint64_t *parameter_value; |
259c2674 JD |
5053 | |
5054 | assert(session); | |
5055 | ||
5056 | DBG("Cmd rotate set schedule session %s", session->name); | |
5057 | ||
92fe5ca1 | 5058 | if (session->live_timer || !session->output_traces) { |
66ea93b1 | 5059 | DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session"); |
259c2674 JD |
5060 | ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE; |
5061 | goto end; | |
5062 | } | |
5063 | ||
66ea93b1 JG |
5064 | switch (schedule_type) { |
5065 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: | |
5066 | parameter_value = &session->rotate_size; | |
5067 | break; | |
5068 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: | |
5069 | parameter_value = &session->rotate_timer_period; | |
5070 | if (new_value >= UINT_MAX) { | |
5071 | DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64 " > %u (UINT_MAX)", | |
5072 | new_value, UINT_MAX); | |
5073 | ret = LTTNG_ERR_INVALID; | |
5074 | goto end; | |
5075 | } | |
5076 | break; | |
5077 | default: | |
5078 | WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type"); | |
5079 | ret = LTTNG_ERR_INVALID; | |
259c2674 | 5080 | goto end; |
66ea93b1 JG |
5081 | } |
5082 | ||
5083 | /* Improper use of the API. */ | |
5084 | if (new_value == -1ULL) { | |
5085 | WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1"); | |
5086 | ret = LTTNG_ERR_INVALID; | |
259c2674 JD |
5087 | goto end; |
5088 | } | |
5089 | ||
66ea93b1 JG |
5090 | /* |
5091 | * As indicated in struct ltt_session's comments, a value of == 0 means | |
5092 | * this schedule rotation type is not in use. | |
5093 | * | |
5094 | * Reject the command if we were asked to activate a schedule that was | |
5095 | * already active. | |
5096 | */ | |
5097 | if (activate && *parameter_value != 0) { | |
5098 | DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active"); | |
5099 | ret = LTTNG_ERR_ROTATION_SCHEDULE_SET; | |
90936dcf | 5100 | goto end; |
66ea93b1 JG |
5101 | } |
5102 | ||
5103 | /* | |
5104 | * Reject the command if we were asked to deactivate a schedule that was | |
5105 | * not active. | |
5106 | */ | |
5107 | if (!activate && *parameter_value == 0) { | |
5108 | DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive"); | |
5109 | ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET; | |
90936dcf JD |
5110 | goto end; |
5111 | } | |
5112 | ||
66ea93b1 JG |
5113 | /* |
5114 | * Reject the command if we were asked to deactivate a schedule that | |
5115 | * doesn't exist. | |
5116 | */ | |
5117 | if (!activate && *parameter_value != new_value) { | |
5118 | DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided"); | |
5119 | ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET; | |
5120 | goto end; | |
5121 | } | |
259c2674 | 5122 | |
66ea93b1 JG |
5123 | *parameter_value = activate ? new_value : 0; |
5124 | ||
5125 | switch (schedule_type) { | |
5126 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: | |
5127 | if (activate && session->active) { | |
5128 | /* | |
5129 | * Only start the timer if the session is active, | |
5130 | * otherwise it will be started when the session starts. | |
5131 | */ | |
92816cc3 JG |
5132 | ret = timer_session_rotation_schedule_timer_start( |
5133 | session, new_value); | |
259c2674 | 5134 | if (ret) { |
66ea93b1 | 5135 | ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command"); |
259c2674 JD |
5136 | ret = LTTNG_ERR_UNK; |
5137 | goto end; | |
5138 | } | |
66ea93b1 | 5139 | } else { |
92816cc3 JG |
5140 | ret = timer_session_rotation_schedule_timer_stop( |
5141 | session); | |
66ea93b1 JG |
5142 | if (ret) { |
5143 | ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command"); | |
5144 | ret = LTTNG_ERR_UNK; | |
f3ce6946 | 5145 | goto end; |
66ea93b1 | 5146 | } |
259c2674 | 5147 | } |
66ea93b1 JG |
5148 | break; |
5149 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: | |
5150 | if (activate) { | |
5151 | ret = subscribe_session_consumed_size_rotation(session, | |
5152 | new_value, notification_thread_handle); | |
90936dcf | 5153 | if (ret) { |
66ea93b1 | 5154 | ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command"); |
90936dcf JD |
5155 | ret = LTTNG_ERR_UNK; |
5156 | goto end; | |
5157 | } | |
90936dcf | 5158 | } else { |
66ea93b1 JG |
5159 | ret = unsubscribe_session_consumed_size_rotation(session, |
5160 | notification_thread_handle); | |
90936dcf | 5161 | if (ret) { |
66ea93b1 | 5162 | ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command"); |
90936dcf JD |
5163 | ret = LTTNG_ERR_UNK; |
5164 | goto end; | |
5165 | } | |
66ea93b1 | 5166 | |
90936dcf | 5167 | } |
66ea93b1 JG |
5168 | break; |
5169 | default: | |
5170 | /* Would have been caught before. */ | |
5171 | abort(); | |
90936dcf JD |
5172 | } |
5173 | ||
259c2674 JD |
5174 | ret = LTTNG_OK; |
5175 | ||
5176 | goto end; | |
5177 | ||
5178 | end: | |
5179 | return ret; | |
5180 | } | |
5181 | ||
a503e1ef JG |
5182 | /* Wait for a given path to be removed before continuing. */ |
5183 | static enum lttng_error_code wait_on_path(void *path_data) | |
5184 | { | |
5185 | const char *shm_path = path_data; | |
5186 | ||
5187 | DBG("Waiting for the shm path at %s to be removed before completing session destruction", | |
5188 | shm_path); | |
5189 | while (true) { | |
5190 | int ret; | |
5191 | struct stat st; | |
5192 | ||
5193 | ret = stat(shm_path, &st); | |
5194 | if (ret) { | |
5195 | if (errno != ENOENT) { | |
5196 | PERROR("stat() returned an error while checking for the existence of the shm path"); | |
5197 | } else { | |
5198 | DBG("shm path no longer exists, completing the destruction of session"); | |
5199 | } | |
5200 | break; | |
5201 | } else { | |
5202 | if (!S_ISDIR(st.st_mode)) { | |
5203 | ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal", | |
5204 | shm_path); | |
5205 | break; | |
5206 | } | |
5207 | } | |
5208 | usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US); | |
5209 | } | |
5210 | return LTTNG_OK; | |
5211 | } | |
5212 | ||
5213 | /* | |
5214 | * Returns a pointer to a handler to run on completion of a command. | |
5215 | * Returns NULL if no handler has to be run for the last command executed. | |
5216 | */ | |
5217 | const struct cmd_completion_handler *cmd_pop_completion_handler(void) | |
5218 | { | |
5219 | struct cmd_completion_handler *handler = current_completion_handler; | |
5220 | ||
5221 | current_completion_handler = NULL; | |
5222 | return handler; | |
5223 | } | |
5224 | ||
2f77fc4b DG |
5225 | /* |
5226 | * Init command subsystem. | |
5227 | */ | |
5228 | void cmd_init(void) | |
5229 | { | |
5230 | /* | |
d88aee68 DG |
5231 | * Set network sequence index to 1 for streams to match a relayd |
5232 | * socket on the consumer side. | |
2f77fc4b | 5233 | */ |
d88aee68 DG |
5234 | pthread_mutex_lock(&relayd_net_seq_idx_lock); |
5235 | relayd_net_seq_idx = 1; | |
5236 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
5237 | |
5238 | DBG("Command subsystem initialized"); | |
5239 | } |