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