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