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