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