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