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