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