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