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