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