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