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