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