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