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