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 = {}; | |
4058 | ||
4059 | tmp_session.extended.ptr = &tmp_extended; | |
4060 | ||
e32d7f27 JG |
4061 | if (!session_get(session)) { |
4062 | continue; | |
4063 | } | |
2f77fc4b DG |
4064 | /* |
4065 | * Only list the sessions the user can control. | |
4066 | */ | |
d7b377ed | 4067 | if (!session_access_ok(session, uid) || |
e32d7f27 JG |
4068 | session->destroyed) { |
4069 | session_put(session); | |
2f77fc4b DG |
4070 | continue; |
4071 | } | |
4072 | ||
4073 | struct ltt_kernel_session *ksess = session->kernel_session; | |
4074 | struct ltt_ust_session *usess = session->ust_session; | |
4075 | ||
4076 | if (session->consumer->type == CONSUMER_DST_NET || | |
4077 | (ksess && ksess->consumer->type == CONSUMER_DST_NET) || | |
4078 | (usess && usess->consumer->type == CONSUMER_DST_NET)) { | |
2d321d3e JR |
4079 | ret = build_network_session_path( |
4080 | tmp_session.path, sizeof(tmp_session.path), session); | |
2f77fc4b | 4081 | } else { |
2d321d3e | 4082 | ret = snprintf(tmp_session.path, sizeof(tmp_session.path), "%s", |
366a9222 | 4083 | session->consumer->dst.session_root_path); |
2f77fc4b DG |
4084 | } |
4085 | if (ret < 0) { | |
4086 | PERROR("snprintf session path"); | |
e32d7f27 | 4087 | session_put(session); |
2f77fc4b DG |
4088 | continue; |
4089 | } | |
4090 | ||
2d321d3e JR |
4091 | strncpy(tmp_session.name, session->name, NAME_MAX); |
4092 | tmp_session.name[NAME_MAX - 1] = '\0'; | |
4093 | tmp_session.enabled = session->active; | |
4094 | tmp_session.snapshot_mode = session->snapshot_mode; | |
4095 | tmp_session.live_timer_interval = session->live_timer; | |
4096 | LTTNG_OPTIONAL_SET(&tmp_extended.creation_time, (uint64_t) session->creation_time); | |
4097 | ret = lttng_session_serialize(&tmp_session, reply_payload); | |
4098 | if (ret) { | |
4099 | ret_code = LTTNG_ERR_FATAL; | |
4100 | goto error; | |
4101 | } | |
2f77fc4b | 4102 | i++; |
e32d7f27 | 4103 | session_put(session); |
2f77fc4b | 4104 | } |
2d321d3e JR |
4105 | |
4106 | if (i > UINT32_MAX) { | |
4107 | ret_code = LTTNG_ERR_OVERFLOW; | |
4108 | goto error; | |
4109 | } | |
4110 | ||
4111 | /* Update command reply header. */ | |
4112 | reply_command_header.count = (uint32_t) i; | |
4113 | memcpy(reply_payload->buffer.data + reply_command_header_offset, &reply_command_header, | |
4114 | sizeof(reply_command_header)); | |
4115 | ret_code = LTTNG_OK; | |
4116 | error: | |
4117 | return ret_code; | |
2f77fc4b DG |
4118 | } |
4119 | ||
806e2684 | 4120 | /* |
6d805429 | 4121 | * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning |
d3f14b8a | 4122 | * ready for trace analysis (or any kind of reader) or else 1 for pending data. |
806e2684 | 4123 | */ |
6d805429 | 4124 | int cmd_data_pending(struct ltt_session *session) |
806e2684 DG |
4125 | { |
4126 | int ret; | |
4127 | struct ltt_kernel_session *ksess = session->kernel_session; | |
4128 | struct ltt_ust_session *usess = session->ust_session; | |
4129 | ||
a0377dfe | 4130 | LTTNG_ASSERT(session); |
806e2684 | 4131 | |
5c408ad8 JD |
4132 | DBG("Data pending for session %s", session->name); |
4133 | ||
806e2684 | 4134 | /* Session MUST be stopped to ask for data availability. */ |
8382cf6f | 4135 | if (session->active) { |
806e2684 DG |
4136 | ret = LTTNG_ERR_SESSION_STARTED; |
4137 | goto error; | |
3a89d11a DG |
4138 | } else { |
4139 | /* | |
4140 | * If stopped, just make sure we've started before else the above call | |
4141 | * will always send that there is data pending. | |
4142 | * | |
4143 | * The consumer assumes that when the data pending command is received, | |
4144 | * the trace has been started before or else no output data is written | |
4145 | * by the streams which is a condition for data pending. So, this is | |
4146 | * *VERY* important that we don't ask the consumer before a start | |
4147 | * trace. | |
4148 | */ | |
8382cf6f | 4149 | if (!session->has_been_started) { |
3a89d11a DG |
4150 | ret = 0; |
4151 | goto error; | |
4152 | } | |
806e2684 DG |
4153 | } |
4154 | ||
92816cc3 JG |
4155 | /* A rotation is still pending, we have to wait. */ |
4156 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { | |
5c408ad8 JD |
4157 | DBG("Rotate still pending for session %s", session->name); |
4158 | ret = 1; | |
4159 | goto error; | |
4160 | } | |
4161 | ||
806e2684 | 4162 | if (ksess && ksess->consumer) { |
6d805429 DG |
4163 | ret = consumer_is_data_pending(ksess->id, ksess->consumer); |
4164 | if (ret == 1) { | |
806e2684 DG |
4165 | /* Data is still being extracted for the kernel. */ |
4166 | goto error; | |
4167 | } | |
4168 | } | |
4169 | ||
4170 | if (usess && usess->consumer) { | |
6d805429 DG |
4171 | ret = consumer_is_data_pending(usess->id, usess->consumer); |
4172 | if (ret == 1) { | |
806e2684 DG |
4173 | /* Data is still being extracted for the kernel. */ |
4174 | goto error; | |
4175 | } | |
4176 | } | |
4177 | ||
4178 | /* Data is ready to be read by a viewer */ | |
6d805429 | 4179 | ret = 0; |
806e2684 DG |
4180 | |
4181 | error: | |
4182 | return ret; | |
4183 | } | |
4184 | ||
6dc3064a DG |
4185 | /* |
4186 | * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library. | |
4187 | * | |
4188 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
4189 | */ | |
4190 | int cmd_snapshot_add_output(struct ltt_session *session, | |
df4f5a87 | 4191 | const struct lttng_snapshot_output *output, uint32_t *id) |
6dc3064a DG |
4192 | { |
4193 | int ret; | |
4194 | struct snapshot_output *new_output; | |
4195 | ||
a0377dfe FD |
4196 | LTTNG_ASSERT(session); |
4197 | LTTNG_ASSERT(output); | |
6dc3064a DG |
4198 | |
4199 | DBG("Cmd snapshot add output for session %s", session->name); | |
4200 | ||
4201 | /* | |
903ef685 | 4202 | * Can't create an output if the session is not set in no-output mode. |
6dc3064a DG |
4203 | */ |
4204 | if (session->output_traces) { | |
903ef685 | 4205 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
4206 | goto error; |
4207 | } | |
4208 | ||
54213acc JG |
4209 | if (session->has_non_mmap_channel) { |
4210 | ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED; | |
4211 | goto error; | |
4212 | } | |
4213 | ||
6dc3064a DG |
4214 | /* Only one output is allowed until we have the "tee" feature. */ |
4215 | if (session->snapshot.nb_output == 1) { | |
4216 | ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST; | |
4217 | goto error; | |
4218 | } | |
4219 | ||
4220 | new_output = snapshot_output_alloc(); | |
4221 | if (!new_output) { | |
4222 | ret = LTTNG_ERR_NOMEM; | |
4223 | goto error; | |
4224 | } | |
4225 | ||
b178f53e | 4226 | ret = snapshot_output_init(session, output->max_size, output->name, |
6dc3064a DG |
4227 | output->ctrl_url, output->data_url, session->consumer, new_output, |
4228 | &session->snapshot); | |
4229 | if (ret < 0) { | |
4230 | if (ret == -ENOMEM) { | |
4231 | ret = LTTNG_ERR_NOMEM; | |
4232 | } else { | |
4233 | ret = LTTNG_ERR_INVALID; | |
4234 | } | |
4235 | goto free_error; | |
4236 | } | |
4237 | ||
6dc3064a DG |
4238 | rcu_read_lock(); |
4239 | snapshot_add_output(&session->snapshot, new_output); | |
4240 | if (id) { | |
4241 | *id = new_output->id; | |
4242 | } | |
4243 | rcu_read_unlock(); | |
4244 | ||
4245 | return LTTNG_OK; | |
4246 | ||
4247 | free_error: | |
4248 | snapshot_output_destroy(new_output); | |
4249 | error: | |
4250 | return ret; | |
4251 | } | |
4252 | ||
4253 | /* | |
4254 | * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl. | |
4255 | * | |
4256 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
4257 | */ | |
4258 | int cmd_snapshot_del_output(struct ltt_session *session, | |
df4f5a87 | 4259 | const struct lttng_snapshot_output *output) |
6dc3064a DG |
4260 | { |
4261 | int ret; | |
eb240553 | 4262 | struct snapshot_output *sout = NULL; |
6dc3064a | 4263 | |
a0377dfe FD |
4264 | LTTNG_ASSERT(session); |
4265 | LTTNG_ASSERT(output); | |
6dc3064a | 4266 | |
6dc3064a DG |
4267 | rcu_read_lock(); |
4268 | ||
4269 | /* | |
d3f14b8a MD |
4270 | * Permission denied to create an output if the session is not |
4271 | * set in no output mode. | |
6dc3064a DG |
4272 | */ |
4273 | if (session->output_traces) { | |
903ef685 | 4274 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
4275 | goto error; |
4276 | } | |
4277 | ||
eb240553 DG |
4278 | if (output->id) { |
4279 | DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id, | |
4280 | session->name); | |
4281 | sout = snapshot_find_output_by_id(output->id, &session->snapshot); | |
4282 | } else if (*output->name != '\0') { | |
4283 | DBG("Cmd snapshot del output name %s for session %s", output->name, | |
4284 | session->name); | |
4285 | sout = snapshot_find_output_by_name(output->name, &session->snapshot); | |
4286 | } | |
6dc3064a DG |
4287 | if (!sout) { |
4288 | ret = LTTNG_ERR_INVALID; | |
4289 | goto error; | |
4290 | } | |
4291 | ||
4292 | snapshot_delete_output(&session->snapshot, sout); | |
4293 | snapshot_output_destroy(sout); | |
4294 | ret = LTTNG_OK; | |
4295 | ||
4296 | error: | |
4297 | rcu_read_unlock(); | |
4298 | return ret; | |
4299 | } | |
4300 | ||
4301 | /* | |
4302 | * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl. | |
4303 | * | |
4304 | * If no output is available, outputs is untouched and 0 is returned. | |
4305 | * | |
4306 | * Return the size of the newly allocated outputs or a negative LTTNG_ERR code. | |
4307 | */ | |
4308 | ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, | |
4309 | struct lttng_snapshot_output **outputs) | |
4310 | { | |
4311 | int ret, idx = 0; | |
b223ca94 | 4312 | struct lttng_snapshot_output *list = NULL; |
6dc3064a DG |
4313 | struct lttng_ht_iter iter; |
4314 | struct snapshot_output *output; | |
4315 | ||
a0377dfe FD |
4316 | LTTNG_ASSERT(session); |
4317 | LTTNG_ASSERT(outputs); | |
6dc3064a DG |
4318 | |
4319 | DBG("Cmd snapshot list outputs for session %s", session->name); | |
4320 | ||
4321 | /* | |
d3f14b8a MD |
4322 | * Permission denied to create an output if the session is not |
4323 | * set in no output mode. | |
6dc3064a DG |
4324 | */ |
4325 | if (session->output_traces) { | |
903ef685 JG |
4326 | ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
4327 | goto end; | |
6dc3064a DG |
4328 | } |
4329 | ||
4330 | if (session->snapshot.nb_output == 0) { | |
4331 | ret = 0; | |
903ef685 | 4332 | goto end; |
6dc3064a DG |
4333 | } |
4334 | ||
64803277 | 4335 | list = calloc<lttng_snapshot_output>(session->snapshot.nb_output); |
6dc3064a | 4336 | if (!list) { |
b223ca94 | 4337 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 4338 | goto end; |
6dc3064a DG |
4339 | } |
4340 | ||
4341 | /* Copy list from session to the new list object. */ | |
b223ca94 | 4342 | rcu_read_lock(); |
6dc3064a DG |
4343 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter, |
4344 | output, node.node) { | |
a0377dfe | 4345 | LTTNG_ASSERT(output->consumer); |
6dc3064a DG |
4346 | list[idx].id = output->id; |
4347 | list[idx].max_size = output->max_size; | |
6ce22875 MD |
4348 | if (lttng_strncpy(list[idx].name, output->name, |
4349 | sizeof(list[idx].name))) { | |
4350 | ret = -LTTNG_ERR_INVALID; | |
903ef685 | 4351 | goto error; |
6ce22875 | 4352 | } |
6dc3064a | 4353 | if (output->consumer->type == CONSUMER_DST_LOCAL) { |
6ce22875 | 4354 | if (lttng_strncpy(list[idx].ctrl_url, |
366a9222 | 4355 | output->consumer->dst.session_root_path, |
6ce22875 MD |
4356 | sizeof(list[idx].ctrl_url))) { |
4357 | ret = -LTTNG_ERR_INVALID; | |
903ef685 | 4358 | goto error; |
6ce22875 | 4359 | } |
6dc3064a DG |
4360 | } else { |
4361 | /* Control URI. */ | |
4362 | ret = uri_to_str_url(&output->consumer->dst.net.control, | |
4363 | list[idx].ctrl_url, sizeof(list[idx].ctrl_url)); | |
4364 | if (ret < 0) { | |
b223ca94 | 4365 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 4366 | goto error; |
6dc3064a DG |
4367 | } |
4368 | ||
4369 | /* Data URI. */ | |
4370 | ret = uri_to_str_url(&output->consumer->dst.net.data, | |
4371 | list[idx].data_url, sizeof(list[idx].data_url)); | |
4372 | if (ret < 0) { | |
b223ca94 | 4373 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 4374 | goto error; |
6dc3064a DG |
4375 | } |
4376 | } | |
4377 | idx++; | |
4378 | } | |
4379 | ||
4380 | *outputs = list; | |
b223ca94 JG |
4381 | list = NULL; |
4382 | ret = session->snapshot.nb_output; | |
6dc3064a | 4383 | error: |
903ef685 | 4384 | rcu_read_unlock(); |
b223ca94 | 4385 | free(list); |
903ef685 | 4386 | end: |
b223ca94 | 4387 | return ret; |
6dc3064a DG |
4388 | } |
4389 | ||
93ec662e JD |
4390 | /* |
4391 | * Check if we can regenerate the metadata for this session. | |
4392 | * Only kernel, UST per-uid and non-live sessions are supported. | |
4393 | * | |
4394 | * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise. | |
4395 | */ | |
4396 | static | |
eded6438 | 4397 | int check_regenerate_metadata_support(struct ltt_session *session) |
93ec662e JD |
4398 | { |
4399 | int ret; | |
4400 | ||
a0377dfe | 4401 | LTTNG_ASSERT(session); |
93ec662e JD |
4402 | |
4403 | if (session->live_timer != 0) { | |
4404 | ret = LTTNG_ERR_LIVE_SESSION; | |
4405 | goto end; | |
4406 | } | |
4407 | if (!session->active) { | |
4408 | ret = LTTNG_ERR_SESSION_NOT_STARTED; | |
4409 | goto end; | |
4410 | } | |
4411 | if (session->ust_session) { | |
4412 | switch (session->ust_session->buffer_type) { | |
4413 | case LTTNG_BUFFER_PER_UID: | |
4414 | break; | |
4415 | case LTTNG_BUFFER_PER_PID: | |
4416 | ret = LTTNG_ERR_PER_PID_SESSION; | |
4417 | goto end; | |
4418 | default: | |
a0377dfe | 4419 | abort(); |
93ec662e JD |
4420 | ret = LTTNG_ERR_UNK; |
4421 | goto end; | |
4422 | } | |
4423 | } | |
4424 | if (session->consumer->type == CONSUMER_DST_NET && | |
4425 | session->consumer->relay_minor_version < 8) { | |
4426 | ret = LTTNG_ERR_RELAYD_VERSION_FAIL; | |
4427 | goto end; | |
4428 | } | |
4429 | ret = 0; | |
4430 | ||
4431 | end: | |
4432 | return ret; | |
4433 | } | |
4434 | ||
93ec662e | 4435 | /* |
eded6438 | 4436 | * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library. |
93ec662e JD |
4437 | * |
4438 | * Ask the consumer to truncate the existing metadata file(s) and | |
4439 | * then regenerate the metadata. Live and per-pid sessions are not | |
4440 | * supported and return an error. | |
4441 | * | |
1136f41b | 4442 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
93ec662e | 4443 | */ |
eded6438 | 4444 | int cmd_regenerate_metadata(struct ltt_session *session) |
93ec662e JD |
4445 | { |
4446 | int ret; | |
4447 | ||
a0377dfe | 4448 | LTTNG_ASSERT(session); |
93ec662e | 4449 | |
eded6438 | 4450 | ret = check_regenerate_metadata_support(session); |
93ec662e JD |
4451 | if (ret) { |
4452 | goto end; | |
4453 | } | |
4454 | ||
4455 | if (session->kernel_session) { | |
eded6438 | 4456 | ret = kernctl_session_regenerate_metadata( |
93ec662e JD |
4457 | session->kernel_session->fd); |
4458 | if (ret < 0) { | |
4459 | ERR("Failed to regenerate the kernel metadata"); | |
4460 | goto end; | |
4461 | } | |
4462 | } | |
4463 | ||
4464 | if (session->ust_session) { | |
d7bfb9b0 | 4465 | ret = trace_ust_regenerate_metadata(session->ust_session); |
93ec662e JD |
4466 | if (ret < 0) { |
4467 | ERR("Failed to regenerate the UST metadata"); | |
4468 | goto end; | |
4469 | } | |
4470 | } | |
4471 | DBG("Cmd metadata regenerate for session %s", session->name); | |
4472 | ret = LTTNG_OK; | |
4473 | ||
4474 | end: | |
4475 | return ret; | |
4476 | } | |
4477 | ||
c2561365 JD |
4478 | /* |
4479 | * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library. | |
4480 | * | |
4481 | * Ask the tracer to regenerate a new statedump. | |
4482 | * | |
1136f41b | 4483 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
c2561365 JD |
4484 | */ |
4485 | int cmd_regenerate_statedump(struct ltt_session *session) | |
4486 | { | |
4487 | int ret; | |
4488 | ||
a0377dfe | 4489 | LTTNG_ASSERT(session); |
c2561365 JD |
4490 | |
4491 | if (!session->active) { | |
4492 | ret = LTTNG_ERR_SESSION_NOT_STARTED; | |
4493 | goto end; | |
4494 | } | |
c2561365 JD |
4495 | |
4496 | if (session->kernel_session) { | |
4497 | ret = kernctl_session_regenerate_statedump( | |
4498 | session->kernel_session->fd); | |
4499 | /* | |
4500 | * Currently, the statedump in kernel can only fail if out | |
4501 | * of memory. | |
4502 | */ | |
4503 | if (ret < 0) { | |
4504 | if (ret == -ENOMEM) { | |
4505 | ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM; | |
4506 | } else { | |
4507 | ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL; | |
4508 | } | |
4509 | ERR("Failed to regenerate the kernel statedump"); | |
4510 | goto end; | |
4511 | } | |
4512 | } | |
4513 | ||
4514 | if (session->ust_session) { | |
4515 | ret = ust_app_regenerate_statedump_all(session->ust_session); | |
4516 | /* | |
4517 | * Currently, the statedump in UST always returns 0. | |
4518 | */ | |
4519 | if (ret < 0) { | |
4520 | ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL; | |
4521 | ERR("Failed to regenerate the UST statedump"); | |
4522 | goto end; | |
4523 | } | |
4524 | } | |
4525 | DBG("Cmd regenerate statedump for session %s", session->name); | |
4526 | ret = LTTNG_OK; | |
4527 | ||
4528 | end: | |
4529 | return ret; | |
4530 | } | |
4531 | ||
989a0844 FD |
4532 | static |
4533 | enum lttng_error_code synchronize_tracer_notifier_register( | |
4534 | struct notification_thread_handle *notification_thread, | |
4535 | struct lttng_trigger *trigger, const struct lttng_credentials *cmd_creds) | |
70670472 | 4536 | { |
989a0844 | 4537 | enum lttng_error_code ret_code; |
7c1f6da2 JG |
4538 | const struct lttng_condition *condition = |
4539 | lttng_trigger_get_const_condition(trigger); | |
989a0844 FD |
4540 | const char *trigger_name; |
4541 | uid_t trigger_owner; | |
4542 | enum lttng_trigger_status trigger_status; | |
4543 | const enum lttng_domain_type trigger_domain = | |
4544 | lttng_trigger_get_underlying_domain_type_restriction( | |
4545 | trigger); | |
70670472 | 4546 | |
989a0844 | 4547 | trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner); |
a0377dfe | 4548 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
989a0844 | 4549 | |
a0377dfe FD |
4550 | LTTNG_ASSERT(condition); |
4551 | LTTNG_ASSERT(lttng_condition_get_type(condition) == | |
8dbb86b8 | 4552 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
989a0844 FD |
4553 | |
4554 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); | |
4555 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? | |
0efb2ad7 | 4556 | trigger_name : "(anonymous)"; |
989a0844 FD |
4557 | |
4558 | session_lock_list(); | |
4559 | switch (trigger_domain) { | |
4560 | case LTTNG_DOMAIN_KERNEL: | |
4561 | { | |
4562 | ret_code = kernel_register_event_notifier(trigger, cmd_creds); | |
4563 | if (ret_code != LTTNG_OK) { | |
4564 | enum lttng_error_code notif_thread_unregister_ret; | |
4565 | ||
4566 | notif_thread_unregister_ret = | |
4567 | notification_thread_command_unregister_trigger( | |
4568 | notification_thread, trigger); | |
4569 | ||
4570 | if (notif_thread_unregister_ret != LTTNG_OK) { | |
4571 | /* Return the original error code. */ | |
4572 | ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d", | |
4573 | trigger_name, | |
4574 | (int) trigger_owner, | |
4575 | ret_code); | |
4576 | } | |
4577 | } | |
4578 | break; | |
70670472 | 4579 | } |
989a0844 FD |
4580 | case LTTNG_DOMAIN_UST: |
4581 | ust_app_global_update_all_event_notifier_rules(); | |
4582 | break; | |
4583 | case LTTNG_DOMAIN_JUL: | |
4584 | case LTTNG_DOMAIN_LOG4J: | |
4585 | case LTTNG_DOMAIN_PYTHON: | |
4586 | { | |
4587 | /* Agent domains. */ | |
4588 | struct agent *agt = agent_find_by_event_notifier_domain( | |
4589 | trigger_domain); | |
70670472 | 4590 | |
989a0844 FD |
4591 | if (!agt) { |
4592 | agt = agent_create(trigger_domain); | |
4593 | if (!agt) { | |
4594 | ret_code = LTTNG_ERR_NOMEM; | |
4595 | goto end_unlock_session_list; | |
4596 | } | |
4597 | ||
412d7227 | 4598 | agent_add(agt, the_trigger_agents_ht_by_domain); |
989a0844 FD |
4599 | } |
4600 | ||
7966af57 | 4601 | ret_code = (lttng_error_code) trigger_agent_enable(trigger, agt); |
989a0844 FD |
4602 | if (ret_code != LTTNG_OK) { |
4603 | goto end_unlock_session_list; | |
4604 | } | |
4605 | ||
4606 | break; | |
4607 | } | |
4608 | case LTTNG_DOMAIN_NONE: | |
4609 | default: | |
4610 | abort(); | |
4611 | } | |
4612 | ||
4613 | ret_code = LTTNG_OK; | |
4614 | end_unlock_session_list: | |
4615 | session_unlock_list(); | |
70670472 JR |
4616 | return ret_code; |
4617 | } | |
4618 | ||
4619 | enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_creds, | |
746e08d7 | 4620 | struct lttng_trigger *trigger, |
0efb2ad7 | 4621 | bool is_trigger_anonymous, |
242388e4 JR |
4622 | struct notification_thread_handle *notification_thread, |
4623 | struct lttng_trigger **return_trigger) | |
b0880ae5 | 4624 | { |
70670472 | 4625 | enum lttng_error_code ret_code; |
70670472 JR |
4626 | const char *trigger_name; |
4627 | uid_t trigger_owner; | |
4628 | enum lttng_trigger_status trigger_status; | |
4629 | ||
4630 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); | |
ce0b1d61 | 4631 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? |
0efb2ad7 | 4632 | trigger_name : "(anonymous)"; |
ce0b1d61 | 4633 | |
70670472 JR |
4634 | trigger_status = lttng_trigger_get_owner_uid( |
4635 | trigger, &trigger_owner); | |
a0377dfe | 4636 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
70670472 JR |
4637 | |
4638 | DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", | |
4639 | trigger_name, (int) trigger_owner, | |
4640 | (int) lttng_credentials_get_uid(cmd_creds)); | |
b0880ae5 | 4641 | |
64eafdf6 JR |
4642 | /* |
4643 | * Validate the trigger credentials against the command credentials. | |
4644 | * Only the root user can register a trigger with non-matching | |
4645 | * credentials. | |
4646 | */ | |
4647 | if (!lttng_credentials_is_equal_uid( | |
4648 | lttng_trigger_get_credentials(trigger), | |
746e08d7 JG |
4649 | cmd_creds)) { |
4650 | if (lttng_credentials_get_uid(cmd_creds) != 0) { | |
70670472 JR |
4651 | ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", |
4652 | trigger_name, (int) trigger_owner, | |
4653 | (int) lttng_credentials_get_uid(cmd_creds)); | |
4654 | ret_code = LTTNG_ERR_INVALID_TRIGGER; | |
64eafdf6 JR |
4655 | goto end; |
4656 | } | |
4657 | } | |
3da864a9 | 4658 | |
58daac01 JR |
4659 | /* |
4660 | * The bytecode generation also serves as a validation step for the | |
4661 | * bytecode expressions. | |
4662 | */ | |
70670472 JR |
4663 | ret_code = lttng_trigger_generate_bytecode(trigger, cmd_creds); |
4664 | if (ret_code != LTTNG_OK) { | |
4665 | ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d", | |
4666 | trigger_name, (int) trigger_owner, ret_code); | |
58daac01 JR |
4667 | goto end; |
4668 | } | |
4669 | ||
242388e4 JR |
4670 | /* |
4671 | * A reference to the trigger is acquired by the notification thread. | |
4672 | * It is safe to return the same trigger to the caller since it the | |
4673 | * other user holds a reference. | |
4674 | * | |
4675 | * The trigger is modified during the execution of the | |
4676 | * "register trigger" command. However, by the time the command returns, | |
4677 | * it is safe to use without any locking as its properties are | |
4678 | * immutable. | |
4679 | */ | |
0efb2ad7 JG |
4680 | ret_code = notification_thread_command_register_trigger( |
4681 | notification_thread, trigger, is_trigger_anonymous); | |
70670472 | 4682 | if (ret_code != LTTNG_OK) { |
ce0b1d61 | 4683 | DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d", |
70670472 | 4684 | trigger_name, (int) trigger_owner, ret_code); |
44760c20 | 4685 | goto end; |
70670472 JR |
4686 | } |
4687 | ||
ce0b1d61 JG |
4688 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); |
4689 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? | |
0efb2ad7 | 4690 | trigger_name : "(anonymous)"; |
ce0b1d61 | 4691 | |
70670472 JR |
4692 | /* |
4693 | * Synchronize tracers if the trigger adds an event notifier. | |
4694 | */ | |
989a0844 FD |
4695 | if (lttng_trigger_needs_tracer_notifier(trigger)) { |
4696 | ret_code = synchronize_tracer_notifier_register(notification_thread, | |
4697 | trigger, cmd_creds); | |
4698 | if (ret_code != LTTNG_OK) { | |
4699 | ERR("Error registering tracer notifier: %s", | |
4700 | lttng_strerror(-ret_code)); | |
4701 | goto end; | |
70670472 JR |
4702 | } |
4703 | } | |
4704 | ||
746e08d7 JG |
4705 | /* |
4706 | * Return an updated trigger to the client. | |
4707 | * | |
4708 | * Since a modified version of the same trigger is returned, acquire a | |
4709 | * reference to the trigger so the caller doesn't have to care if those | |
4710 | * are distinct instances or not. | |
4711 | */ | |
39b95a70 JG |
4712 | if (ret_code == LTTNG_OK) { |
4713 | lttng_trigger_get(trigger); | |
4714 | *return_trigger = trigger; | |
4715 | /* Ownership of trigger was transferred to caller. */ | |
4716 | trigger = NULL; | |
4717 | } | |
b0880ae5 | 4718 | end: |
70670472 | 4719 | return ret_code; |
989a0844 FD |
4720 | } |
4721 | ||
4722 | static | |
4723 | enum lttng_error_code synchronize_tracer_notifier_unregister( | |
4724 | const struct lttng_trigger *trigger) | |
4725 | { | |
4726 | enum lttng_error_code ret_code; | |
4727 | const struct lttng_condition *condition = | |
4728 | lttng_trigger_get_const_condition(trigger); | |
4729 | const enum lttng_domain_type trigger_domain = | |
4730 | lttng_trigger_get_underlying_domain_type_restriction( | |
4731 | trigger); | |
4732 | ||
a0377dfe FD |
4733 | LTTNG_ASSERT(condition); |
4734 | LTTNG_ASSERT(lttng_condition_get_type(condition) == | |
8dbb86b8 | 4735 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
989a0844 FD |
4736 | |
4737 | session_lock_list(); | |
4738 | switch (trigger_domain) { | |
4739 | case LTTNG_DOMAIN_KERNEL: | |
4740 | ret_code = kernel_unregister_event_notifier(trigger); | |
e689039f JG |
4741 | if (ret_code != LTTNG_OK) { |
4742 | goto end_unlock_session_list; | |
4743 | } | |
4744 | ||
989a0844 FD |
4745 | break; |
4746 | case LTTNG_DOMAIN_UST: | |
4747 | ust_app_global_update_all_event_notifier_rules(); | |
4748 | break; | |
4749 | case LTTNG_DOMAIN_JUL: | |
4750 | case LTTNG_DOMAIN_LOG4J: | |
4751 | case LTTNG_DOMAIN_PYTHON: | |
4752 | { | |
4753 | /* Agent domains. */ | |
4754 | struct agent *agt = agent_find_by_event_notifier_domain( | |
4755 | trigger_domain); | |
4756 | ||
566190c4 JG |
4757 | /* |
4758 | * This trigger was never registered in the first place. Calling | |
4759 | * this function under those circumstances is an internal error. | |
4760 | */ | |
a0377dfe | 4761 | LTTNG_ASSERT(agt); |
7966af57 | 4762 | ret_code = (lttng_error_code) trigger_agent_disable(trigger, agt); |
989a0844 FD |
4763 | if (ret_code != LTTNG_OK) { |
4764 | goto end_unlock_session_list; | |
4765 | } | |
4766 | ||
4767 | break; | |
4768 | } | |
4769 | case LTTNG_DOMAIN_NONE: | |
4770 | default: | |
4771 | abort(); | |
4772 | } | |
4773 | ||
4774 | ret_code = LTTNG_OK; | |
4775 | ||
9b7cbebd JG |
4776 | end_unlock_session_list: |
4777 | session_unlock_list(); | |
4778 | return ret_code; | |
b0880ae5 JG |
4779 | } |
4780 | ||
70670472 | 4781 | enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd_creds, |
746e08d7 | 4782 | const struct lttng_trigger *trigger, |
b0880ae5 JG |
4783 | struct notification_thread_handle *notification_thread) |
4784 | { | |
70670472 | 4785 | enum lttng_error_code ret_code; |
70670472 JR |
4786 | const char *trigger_name; |
4787 | uid_t trigger_owner; | |
4788 | enum lttng_trigger_status trigger_status; | |
5c5373c3 | 4789 | struct lttng_trigger *sessiond_trigger = NULL; |
70670472 JR |
4790 | |
4791 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); | |
0efb2ad7 | 4792 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)"; |
989a0844 | 4793 | trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner); |
a0377dfe | 4794 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
70670472 JR |
4795 | |
4796 | DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", | |
4797 | trigger_name, (int) trigger_owner, | |
4798 | (int) lttng_credentials_get_uid(cmd_creds)); | |
b0880ae5 | 4799 | |
64eafdf6 JR |
4800 | /* |
4801 | * Validate the trigger credentials against the command credentials. | |
4802 | * Only the root user can unregister a trigger with non-matching | |
4803 | * credentials. | |
4804 | */ | |
4805 | if (!lttng_credentials_is_equal_uid( | |
4806 | lttng_trigger_get_credentials(trigger), | |
746e08d7 JG |
4807 | cmd_creds)) { |
4808 | if (lttng_credentials_get_uid(cmd_creds) != 0) { | |
70670472 JR |
4809 | ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", |
4810 | trigger_name, (int) trigger_owner, | |
4811 | (int) lttng_credentials_get_uid(cmd_creds)); | |
4812 | ret_code = LTTNG_ERR_INVALID_TRIGGER; | |
64eafdf6 JR |
4813 | goto end; |
4814 | } | |
4815 | } | |
3da864a9 | 4816 | |
5c5373c3 JR |
4817 | /* Fetch the sessiond side trigger object. */ |
4818 | ret_code = notification_thread_command_get_trigger( | |
4819 | notification_thread, trigger, &sessiond_trigger); | |
4820 | if (ret_code != LTTNG_OK) { | |
4821 | DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d", | |
4822 | trigger_name, (int) trigger_owner, ret_code); | |
4823 | goto end; | |
4824 | } | |
4825 | ||
a0377dfe | 4826 | LTTNG_ASSERT(sessiond_trigger); |
5c5373c3 JR |
4827 | |
4828 | /* | |
4829 | * From this point on, no matter what, consider the trigger | |
4830 | * unregistered. | |
4831 | * | |
4832 | * We set the unregistered state of the sessiond side trigger object in | |
4833 | * the client thread since we want to minimize the possibility of the | |
4834 | * notification thread being stalled due to a long execution of an | |
4835 | * action that required the trigger lock. | |
4836 | */ | |
4837 | lttng_trigger_set_as_unregistered(sessiond_trigger); | |
4838 | ||
70670472 JR |
4839 | ret_code = notification_thread_command_unregister_trigger(notification_thread, |
4840 | trigger); | |
4841 | if (ret_code != LTTNG_OK) { | |
ce0b1d61 | 4842 | DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d", |
70670472 | 4843 | trigger_name, (int) trigger_owner, ret_code); |
13839b27 | 4844 | goto end; |
70670472 JR |
4845 | } |
4846 | ||
4847 | /* | |
4848 | * Synchronize tracers if the trigger removes an event notifier. | |
44760c20 JR |
4849 | * Do this even if the trigger unregistration failed to at least stop |
4850 | * the tracers from producing notifications associated with this | |
4851 | * event notifier. | |
70670472 | 4852 | */ |
989a0844 FD |
4853 | if (lttng_trigger_needs_tracer_notifier(trigger)) { |
4854 | ret_code = synchronize_tracer_notifier_unregister(trigger); | |
4855 | if (ret_code != LTTNG_OK) { | |
4856 | ERR("Error unregistering trigger to tracer."); | |
4857 | goto end; | |
70670472 | 4858 | } |
9b7cbebd | 4859 | |
70670472 JR |
4860 | } |
4861 | ||
b0880ae5 | 4862 | end: |
5c5373c3 | 4863 | lttng_trigger_put(sessiond_trigger); |
70670472 | 4864 | return ret_code; |
989a0844 | 4865 | } |
b0880ae5 | 4866 | |
ddd915a3 | 4867 | enum lttng_error_code cmd_list_triggers(struct command_ctx *cmd_ctx, |
fbc9f37d JR |
4868 | struct notification_thread_handle *notification_thread, |
4869 | struct lttng_triggers **return_triggers) | |
4870 | { | |
f2bda80e | 4871 | int ret; |
fbc9f37d JR |
4872 | enum lttng_error_code ret_code; |
4873 | struct lttng_triggers *triggers = NULL; | |
4874 | ||
4875 | /* Get the set of triggers from the notification thread. */ | |
4876 | ret_code = notification_thread_command_list_triggers( | |
4877 | notification_thread, cmd_ctx->creds.uid, &triggers); | |
4878 | if (ret_code != LTTNG_OK) { | |
fbc9f37d JR |
4879 | goto end; |
4880 | } | |
4881 | ||
f2bda80e JG |
4882 | ret = lttng_triggers_remove_hidden_triggers(triggers); |
4883 | if (ret) { | |
4884 | ret_code = LTTNG_ERR_UNK; | |
4885 | goto end; | |
4886 | } | |
4887 | ||
fbc9f37d JR |
4888 | *return_triggers = triggers; |
4889 | triggers = NULL; | |
ddd915a3 | 4890 | ret_code = LTTNG_OK; |
fbc9f37d JR |
4891 | end: |
4892 | lttng_triggers_destroy(triggers); | |
ddd915a3 | 4893 | return ret_code; |
fbc9f37d | 4894 | } |
588c4b0d JG |
4895 | |
4896 | enum lttng_error_code cmd_execute_error_query(const struct lttng_credentials *cmd_creds, | |
4897 | const struct lttng_error_query *query, | |
4898 | struct lttng_error_query_results **_results, | |
4899 | struct notification_thread_handle *notification_thread) | |
4900 | { | |
4901 | enum lttng_error_code ret_code; | |
4902 | const struct lttng_trigger *query_target_trigger; | |
63dd3d7b | 4903 | const struct lttng_action *query_target_action = NULL; |
588c4b0d JG |
4904 | struct lttng_trigger *matching_trigger = NULL; |
4905 | const char *trigger_name; | |
4906 | uid_t trigger_owner; | |
4907 | enum lttng_trigger_status trigger_status; | |
4908 | struct lttng_error_query_results *results = NULL; | |
4909 | ||
4910 | switch (lttng_error_query_get_target_type(query)) { | |
4911 | case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER: | |
4912 | query_target_trigger = lttng_error_query_trigger_borrow_target(query); | |
4913 | break; | |
63dd3d7b JG |
4914 | case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION: |
4915 | query_target_trigger = | |
4916 | lttng_error_query_condition_borrow_target(query); | |
4917 | break; | |
588c4b0d JG |
4918 | case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION: |
4919 | query_target_trigger = lttng_error_query_action_borrow_trigger_target( | |
4920 | query); | |
4921 | break; | |
4922 | default: | |
4923 | abort(); | |
4924 | } | |
4925 | ||
a0377dfe | 4926 | LTTNG_ASSERT(query_target_trigger); |
588c4b0d JG |
4927 | |
4928 | ret_code = notification_thread_command_get_trigger(notification_thread, | |
4929 | query_target_trigger, &matching_trigger); | |
4930 | if (ret_code != LTTNG_OK) { | |
4931 | goto end; | |
4932 | } | |
4933 | ||
4934 | /* No longer needed. */ | |
4935 | query_target_trigger = NULL; | |
4936 | ||
4937 | if (lttng_error_query_get_target_type(query) == | |
4938 | LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION) { | |
4939 | /* Get the sessiond-side version of the target action. */ | |
4940 | query_target_action = | |
4941 | lttng_error_query_action_borrow_action_target( | |
4942 | query, matching_trigger); | |
4943 | } | |
4944 | ||
4945 | trigger_status = lttng_trigger_get_name(matching_trigger, &trigger_name); | |
4946 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? | |
0efb2ad7 | 4947 | trigger_name : "(anonymous)"; |
588c4b0d JG |
4948 | trigger_status = lttng_trigger_get_owner_uid(matching_trigger, |
4949 | &trigger_owner); | |
a0377dfe | 4950 | LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
588c4b0d JG |
4951 | |
4952 | results = lttng_error_query_results_create(); | |
4953 | if (!results) { | |
4954 | ret_code = LTTNG_ERR_NOMEM; | |
4955 | goto end; | |
4956 | } | |
4957 | ||
4958 | DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", | |
4959 | trigger_name, (int) trigger_owner, | |
4960 | (int) lttng_credentials_get_uid(cmd_creds)); | |
4961 | ||
4962 | /* | |
4963 | * Validate the trigger credentials against the command credentials. | |
4964 | * Only the root user can target a trigger with non-matching | |
4965 | * credentials. | |
4966 | */ | |
4967 | if (!lttng_credentials_is_equal_uid( | |
4968 | lttng_trigger_get_credentials(matching_trigger), | |
4969 | cmd_creds)) { | |
4970 | if (lttng_credentials_get_uid(cmd_creds) != 0) { | |
4971 | ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", | |
4972 | trigger_name, (int) trigger_owner, | |
4973 | (int) lttng_credentials_get_uid(cmd_creds)); | |
4974 | ret_code = LTTNG_ERR_INVALID_TRIGGER; | |
4975 | goto end; | |
4976 | } | |
4977 | } | |
4978 | ||
4979 | switch (lttng_error_query_get_target_type(query)) { | |
4980 | case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER: | |
4981 | trigger_status = lttng_trigger_add_error_results( | |
4982 | matching_trigger, results); | |
4983 | ||
4984 | switch (trigger_status) { | |
4985 | case LTTNG_TRIGGER_STATUS_OK: | |
4986 | break; | |
4987 | default: | |
4988 | ret_code = LTTNG_ERR_UNK; | |
4989 | goto end; | |
4990 | } | |
4991 | ||
4992 | break; | |
63dd3d7b JG |
4993 | case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION: |
4994 | { | |
4995 | trigger_status = lttng_trigger_condition_add_error_results( | |
4996 | matching_trigger, results); | |
4997 | ||
4998 | switch (trigger_status) { | |
4999 | case LTTNG_TRIGGER_STATUS_OK: | |
5000 | break; | |
5001 | default: | |
5002 | ret_code = LTTNG_ERR_UNK; | |
5003 | goto end; | |
5004 | } | |
5005 | ||
5006 | break; | |
5007 | } | |
588c4b0d JG |
5008 | case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION: |
5009 | { | |
5010 | const enum lttng_action_status action_status = | |
5011 | lttng_action_add_error_query_results( | |
5012 | query_target_action, results); | |
5013 | ||
5014 | switch (action_status) { | |
5015 | case LTTNG_ACTION_STATUS_OK: | |
5016 | break; | |
5017 | default: | |
5018 | ret_code = LTTNG_ERR_UNK; | |
5019 | goto end; | |
5020 | } | |
5021 | ||
5022 | break; | |
5023 | } | |
5024 | default: | |
ef4cf1d2 | 5025 | abort(); |
588c4b0d JG |
5026 | break; |
5027 | } | |
5028 | ||
5029 | *_results = results; | |
5030 | results = NULL; | |
5031 | ret_code = LTTNG_OK; | |
5032 | end: | |
5033 | lttng_trigger_put(matching_trigger); | |
5034 | lttng_error_query_results_destroy(results); | |
5035 | return ret_code; | |
5036 | } | |
5037 | ||
6dc3064a DG |
5038 | /* |
5039 | * Send relayd sockets from snapshot output to consumer. Ignore request if the | |
5040 | * snapshot output is *not* set with a remote destination. | |
5041 | * | |
9a654598 | 5042 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
6dc3064a | 5043 | */ |
9a654598 | 5044 | static enum lttng_error_code set_relayd_for_snapshot( |
348a81dc | 5045 | struct consumer_output *output, |
fb9a95c4 | 5046 | const struct ltt_session *session) |
6dc3064a | 5047 | { |
9a654598 | 5048 | enum lttng_error_code status = LTTNG_OK; |
6dc3064a DG |
5049 | struct lttng_ht_iter iter; |
5050 | struct consumer_socket *socket; | |
1e791a74 | 5051 | LTTNG_OPTIONAL(uint64_t) current_chunk_id = {}; |
6fa5fe7c | 5052 | const char *base_path; |
6dc3064a | 5053 | |
a0377dfe FD |
5054 | LTTNG_ASSERT(output); |
5055 | LTTNG_ASSERT(session); | |
6dc3064a DG |
5056 | |
5057 | DBG2("Set relayd object from snapshot output"); | |
5058 | ||
1e791a74 | 5059 | if (session->current_trace_chunk) { |
348a81dc JG |
5060 | enum lttng_trace_chunk_status chunk_status = |
5061 | lttng_trace_chunk_get_id( | |
5062 | session->current_trace_chunk, | |
5063 | ¤t_chunk_id.value); | |
1e791a74 | 5064 | |
348a81dc | 5065 | if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) { |
1e791a74 JG |
5066 | current_chunk_id.is_set = true; |
5067 | } else { | |
5068 | ERR("Failed to get current trace chunk id"); | |
5069 | status = LTTNG_ERR_UNK; | |
5070 | goto error; | |
5071 | } | |
5072 | } | |
5073 | ||
6dc3064a | 5074 | /* Ignore if snapshot consumer output is not network. */ |
348a81dc | 5075 | if (output->type != CONSUMER_DST_NET) { |
6dc3064a DG |
5076 | goto error; |
5077 | } | |
5078 | ||
6fa5fe7c MD |
5079 | /* |
5080 | * The snapshot record URI base path overrides the session | |
5081 | * base path. | |
5082 | */ | |
5083 | if (output->dst.net.control.subdir[0] != '\0') { | |
5084 | base_path = output->dst.net.control.subdir; | |
5085 | } else { | |
5086 | base_path = session->base_path; | |
5087 | } | |
5088 | ||
6dc3064a DG |
5089 | /* |
5090 | * For each consumer socket, create and send the relayd object of the | |
5091 | * snapshot output. | |
5092 | */ | |
5093 | rcu_read_lock(); | |
a46526fc | 5094 | cds_lfht_for_each_entry (output->socks->ht, &iter.iter, socket, node.node) { |
ecd0f96d | 5095 | pthread_mutex_lock(socket->lock); |
a46526fc JR |
5096 | status = send_consumer_relayd_sockets(*session, output, socket, base_path, |
5097 | current_chunk_id.is_set ? ¤t_chunk_id.value : NULL); | |
ecd0f96d | 5098 | pthread_mutex_unlock(socket->lock); |
9a654598 | 5099 | if (status != LTTNG_OK) { |
6dc3064a DG |
5100 | rcu_read_unlock(); |
5101 | goto error; | |
5102 | } | |
5103 | } | |
5104 | rcu_read_unlock(); | |
5105 | ||
5106 | error: | |
9a654598 | 5107 | return status; |
6dc3064a DG |
5108 | } |
5109 | ||
5110 | /* | |
5111 | * Record a kernel snapshot. | |
5112 | * | |
fac41e72 | 5113 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
6dc3064a | 5114 | */ |
fb9a95c4 JG |
5115 | static enum lttng_error_code record_kernel_snapshot( |
5116 | struct ltt_kernel_session *ksess, | |
348a81dc | 5117 | const struct consumer_output *output, |
fb9a95c4 | 5118 | const struct ltt_session *session, |
f46376a1 | 5119 | uint64_t nb_packets_per_stream) |
6dc3064a | 5120 | { |
9a654598 | 5121 | enum lttng_error_code status; |
6dc3064a | 5122 | |
a0377dfe FD |
5123 | LTTNG_ASSERT(ksess); |
5124 | LTTNG_ASSERT(output); | |
5125 | LTTNG_ASSERT(session); | |
6dc3064a | 5126 | |
348a81dc | 5127 | status = kernel_snapshot_record( |
f46376a1 | 5128 | ksess, output, nb_packets_per_stream); |
9a654598 | 5129 | return status; |
6dc3064a DG |
5130 | } |
5131 | ||
5132 | /* | |
5133 | * Record a UST snapshot. | |
5134 | * | |
9a654598 | 5135 | * Returns LTTNG_OK on success or a LTTNG_ERR error code. |
6dc3064a | 5136 | */ |
9a654598 | 5137 | static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess, |
348a81dc JG |
5138 | const struct consumer_output *output, |
5139 | const struct ltt_session *session, | |
f46376a1 | 5140 | uint64_t nb_packets_per_stream) |
6dc3064a | 5141 | { |
9a654598 | 5142 | enum lttng_error_code status; |
6dc3064a | 5143 | |
a0377dfe FD |
5144 | LTTNG_ASSERT(usess); |
5145 | LTTNG_ASSERT(output); | |
5146 | LTTNG_ASSERT(session); | |
6dc3064a | 5147 | |
348a81dc | 5148 | status = ust_app_snapshot_record( |
f46376a1 | 5149 | usess, output, nb_packets_per_stream); |
9a654598 | 5150 | return status; |
6dc3064a DG |
5151 | } |
5152 | ||
d07ceecd | 5153 | static |
fb9a95c4 JG |
5154 | uint64_t get_session_size_one_more_packet_per_stream( |
5155 | const struct ltt_session *session, uint64_t cur_nr_packets) | |
68808f4e | 5156 | { |
d07ceecd | 5157 | uint64_t tot_size = 0; |
68808f4e DG |
5158 | |
5159 | if (session->kernel_session) { | |
5160 | struct ltt_kernel_channel *chan; | |
fb9a95c4 JG |
5161 | const struct ltt_kernel_session *ksess = |
5162 | session->kernel_session; | |
68808f4e | 5163 | |
68808f4e | 5164 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { |
d07ceecd MD |
5165 | if (cur_nr_packets >= chan->channel->attr.num_subbuf) { |
5166 | /* | |
5167 | * Don't take channel into account if we | |
5168 | * already grab all its packets. | |
5169 | */ | |
5170 | continue; | |
68808f4e | 5171 | } |
d07ceecd MD |
5172 | tot_size += chan->channel->attr.subbuf_size |
5173 | * chan->stream_count; | |
68808f4e DG |
5174 | } |
5175 | } | |
5176 | ||
5177 | if (session->ust_session) { | |
fb9a95c4 | 5178 | const struct ltt_ust_session *usess = session->ust_session; |
68808f4e | 5179 | |
d07ceecd MD |
5180 | tot_size += ust_app_get_size_one_more_packet_per_stream(usess, |
5181 | cur_nr_packets); | |
68808f4e DG |
5182 | } |
5183 | ||
d07ceecd | 5184 | return tot_size; |
68808f4e DG |
5185 | } |
5186 | ||
5c786ded | 5187 | /* |
d07ceecd MD |
5188 | * Calculate the number of packets we can grab from each stream that |
5189 | * fits within the overall snapshot max size. | |
5190 | * | |
5191 | * Returns -1 on error, 0 means infinite number of packets, else > 0 is | |
5192 | * the number of packets per stream. | |
5193 | * | |
5194 | * TODO: this approach is not perfect: we consider the worse case | |
5195 | * (packet filling the sub-buffers) as an upper bound, but we could do | |
5196 | * better if we do this calculation while we actually grab the packet | |
5197 | * content: we would know how much padding we don't actually store into | |
5198 | * the file. | |
5199 | * | |
5200 | * This algorithm is currently bounded by the number of packets per | |
5201 | * stream. | |
5202 | * | |
5203 | * Since we call this algorithm before actually grabbing the data, it's | |
5204 | * an approximation: for instance, applications could appear/disappear | |
5205 | * in between this call and actually grabbing data. | |
5c786ded | 5206 | */ |
d07ceecd | 5207 | static |
fb9a95c4 JG |
5208 | int64_t get_session_nb_packets_per_stream(const struct ltt_session *session, |
5209 | uint64_t max_size) | |
5c786ded | 5210 | { |
d07ceecd MD |
5211 | int64_t size_left; |
5212 | uint64_t cur_nb_packets = 0; | |
5c786ded | 5213 | |
d07ceecd MD |
5214 | if (!max_size) { |
5215 | return 0; /* Infinite */ | |
5c786ded JD |
5216 | } |
5217 | ||
d07ceecd MD |
5218 | size_left = max_size; |
5219 | for (;;) { | |
5220 | uint64_t one_more_packet_tot_size; | |
5c786ded | 5221 | |
fb9a95c4 JG |
5222 | one_more_packet_tot_size = get_session_size_one_more_packet_per_stream( |
5223 | session, cur_nb_packets); | |
d07ceecd MD |
5224 | if (!one_more_packet_tot_size) { |
5225 | /* We are already grabbing all packets. */ | |
5226 | break; | |
5227 | } | |
5228 | size_left -= one_more_packet_tot_size; | |
5229 | if (size_left < 0) { | |
5230 | break; | |
5231 | } | |
5232 | cur_nb_packets++; | |
5c786ded | 5233 | } |
aecf2da5 | 5234 | if (!cur_nb_packets && size_left != max_size) { |
d07ceecd MD |
5235 | /* Not enough room to grab one packet of each stream, error. */ |
5236 | return -1; | |
5237 | } | |
5238 | return cur_nb_packets; | |
5c786ded JD |
5239 | } |
5240 | ||
fb9a95c4 | 5241 | static |
d2956687 | 5242 | enum lttng_error_code snapshot_record(struct ltt_session *session, |
f46376a1 | 5243 | const struct snapshot_output *snapshot_output) |
fb9a95c4 JG |
5244 | { |
5245 | int64_t nb_packets_per_stream; | |
d2956687 | 5246 | char snapshot_chunk_name[LTTNG_NAME_MAX]; |
348a81dc JG |
5247 | int ret; |
5248 | enum lttng_error_code ret_code = LTTNG_OK; | |
d2956687 | 5249 | struct lttng_trace_chunk *snapshot_trace_chunk; |
348a81dc JG |
5250 | struct consumer_output *original_ust_consumer_output = NULL; |
5251 | struct consumer_output *original_kernel_consumer_output = NULL; | |
5252 | struct consumer_output *snapshot_ust_consumer_output = NULL; | |
5253 | struct consumer_output *snapshot_kernel_consumer_output = NULL; | |
d2956687 | 5254 | |
348a81dc | 5255 | ret = snprintf(snapshot_chunk_name, sizeof(snapshot_chunk_name), |
d2956687 JG |
5256 | "%s-%s-%" PRIu64, |
5257 | snapshot_output->name, | |
5258 | snapshot_output->datetime, | |
5259 | snapshot_output->nb_snapshot); | |
348a81dc | 5260 | if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) { |
d2956687 | 5261 | ERR("Failed to format snapshot name"); |
348a81dc JG |
5262 | ret_code = LTTNG_ERR_INVALID; |
5263 | goto error; | |
d2956687 JG |
5264 | } |
5265 | DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"", | |
5266 | snapshot_output->name, session->name, | |
5267 | snapshot_chunk_name); | |
348a81dc JG |
5268 | if (!session->kernel_session && !session->ust_session) { |
5269 | ERR("Failed to record snapshot as no channels exist"); | |
5270 | ret_code = LTTNG_ERR_NO_CHANNEL; | |
5271 | goto error; | |
5272 | } | |
5273 | ||
5274 | if (session->kernel_session) { | |
5275 | original_kernel_consumer_output = | |
5276 | session->kernel_session->consumer; | |
5277 | snapshot_kernel_consumer_output = | |
5278 | consumer_copy_output(snapshot_output->consumer); | |
3b967712 MD |
5279 | strcpy(snapshot_kernel_consumer_output->chunk_path, |
5280 | snapshot_chunk_name); | |
bd666153 JR |
5281 | |
5282 | /* Copy the original domain subdir. */ | |
5283 | strcpy(snapshot_kernel_consumer_output->domain_subdir, | |
5284 | original_kernel_consumer_output->domain_subdir); | |
5285 | ||
348a81dc JG |
5286 | ret = consumer_copy_sockets(snapshot_kernel_consumer_output, |
5287 | original_kernel_consumer_output); | |
5288 | if (ret < 0) { | |
5289 | ERR("Failed to copy consumer sockets from snapshot output configuration"); | |
5290 | ret_code = LTTNG_ERR_NOMEM; | |
5291 | goto error; | |
5292 | } | |
5293 | ret_code = set_relayd_for_snapshot( | |
5294 | snapshot_kernel_consumer_output, session); | |
5295 | if (ret_code != LTTNG_OK) { | |
5296 | ERR("Failed to setup relay daemon for kernel tracer snapshot"); | |
5297 | goto error; | |
5298 | } | |
5299 | session->kernel_session->consumer = | |
5300 | snapshot_kernel_consumer_output; | |
5301 | } | |
5302 | if (session->ust_session) { | |
5303 | original_ust_consumer_output = session->ust_session->consumer; | |
5304 | snapshot_ust_consumer_output = | |
5305 | consumer_copy_output(snapshot_output->consumer); | |
3b967712 MD |
5306 | strcpy(snapshot_ust_consumer_output->chunk_path, |
5307 | snapshot_chunk_name); | |
bd666153 JR |
5308 | |
5309 | /* Copy the original domain subdir. */ | |
5310 | strcpy(snapshot_ust_consumer_output->domain_subdir, | |
5311 | original_ust_consumer_output->domain_subdir); | |
5312 | ||
348a81dc JG |
5313 | ret = consumer_copy_sockets(snapshot_ust_consumer_output, |
5314 | original_ust_consumer_output); | |
5315 | if (ret < 0) { | |
5316 | ERR("Failed to copy consumer sockets from snapshot output configuration"); | |
5317 | ret_code = LTTNG_ERR_NOMEM; | |
5318 | goto error; | |
5319 | } | |
5320 | ret_code = set_relayd_for_snapshot( | |
5321 | snapshot_ust_consumer_output, session); | |
5322 | if (ret_code != LTTNG_OK) { | |
5323 | ERR("Failed to setup relay daemon for userspace tracer snapshot"); | |
5324 | goto error; | |
5325 | } | |
5326 | session->ust_session->consumer = | |
5327 | snapshot_ust_consumer_output; | |
5328 | } | |
5329 | ||
d2956687 | 5330 | snapshot_trace_chunk = session_create_new_trace_chunk(session, |
348a81dc JG |
5331 | snapshot_kernel_consumer_output ?: |
5332 | snapshot_ust_consumer_output, | |
5333 | consumer_output_get_base_path( | |
5334 | snapshot_output->consumer), | |
d2956687 JG |
5335 | snapshot_chunk_name); |
5336 | if (!snapshot_trace_chunk) { | |
348a81dc JG |
5337 | ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"", |
5338 | session->name); | |
5339 | ret_code = LTTNG_ERR_CREATE_DIR_FAIL; | |
5340 | goto error; | |
d2956687 | 5341 | } |
a0377dfe | 5342 | LTTNG_ASSERT(!session->current_trace_chunk); |
d2956687 JG |
5343 | ret = session_set_trace_chunk(session, snapshot_trace_chunk, NULL); |
5344 | lttng_trace_chunk_put(snapshot_trace_chunk); | |
5345 | snapshot_trace_chunk = NULL; | |
5346 | if (ret) { | |
348a81dc JG |
5347 | ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"", |
5348 | session->name); | |
5349 | ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; | |
5350 | goto error; | |
d2956687 | 5351 | } |
fb9a95c4 JG |
5352 | |
5353 | nb_packets_per_stream = get_session_nb_packets_per_stream(session, | |
5354 | snapshot_output->max_size); | |
5355 | if (nb_packets_per_stream < 0) { | |
348a81dc | 5356 | ret_code = LTTNG_ERR_MAX_SIZE_INVALID; |
5151d412 | 5357 | goto error_close_trace_chunk; |
fb9a95c4 JG |
5358 | } |
5359 | ||
5360 | if (session->kernel_session) { | |
348a81dc JG |
5361 | ret_code = record_kernel_snapshot(session->kernel_session, |
5362 | snapshot_kernel_consumer_output, session, | |
f46376a1 | 5363 | nb_packets_per_stream); |
348a81dc | 5364 | if (ret_code != LTTNG_OK) { |
5151d412 | 5365 | goto error_close_trace_chunk; |
fb9a95c4 JG |
5366 | } |
5367 | } | |
5368 | ||
5369 | if (session->ust_session) { | |
348a81dc JG |
5370 | ret_code = record_ust_snapshot(session->ust_session, |
5371 | snapshot_ust_consumer_output, session, | |
f46376a1 | 5372 | nb_packets_per_stream); |
348a81dc | 5373 | if (ret_code != LTTNG_OK) { |
5151d412 | 5374 | goto error_close_trace_chunk; |
fb9a95c4 JG |
5375 | } |
5376 | } | |
d2956687 | 5377 | |
5151d412 | 5378 | error_close_trace_chunk: |
dbfee52c MD |
5379 | if (session_set_trace_chunk(session, NULL, &snapshot_trace_chunk)) { |
5380 | ERR("Failed to release the current trace chunk of session \"%s\"", | |
5381 | session->name); | |
5382 | ret_code = LTTNG_ERR_UNK; | |
5383 | } | |
5384 | ||
5385 | if (session_close_trace_chunk(session, snapshot_trace_chunk, | |
343defc2 | 5386 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION, NULL)) { |
d2956687 JG |
5387 | /* |
5388 | * Don't goto end; make sure the chunk is closed for the session | |
5389 | * to allow future snapshots. | |
5390 | */ | |
5391 | ERR("Failed to close snapshot trace chunk of session \"%s\"", | |
5392 | session->name); | |
348a81dc | 5393 | ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER; |
d2956687 | 5394 | } |
a49a9216 JG |
5395 | |
5396 | lttng_trace_chunk_put(snapshot_trace_chunk); | |
5397 | snapshot_trace_chunk = NULL; | |
348a81dc JG |
5398 | error: |
5399 | if (original_ust_consumer_output) { | |
5400 | session->ust_session->consumer = original_ust_consumer_output; | |
5401 | } | |
5402 | if (original_kernel_consumer_output) { | |
5403 | session->kernel_session->consumer = | |
5404 | original_kernel_consumer_output; | |
5405 | } | |
5406 | consumer_output_put(snapshot_ust_consumer_output); | |
5407 | consumer_output_put(snapshot_kernel_consumer_output); | |
5408 | return ret_code; | |
fb9a95c4 JG |
5409 | } |
5410 | ||
6dc3064a DG |
5411 | /* |
5412 | * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl. | |
5413 | * | |
5414 | * The wait parameter is ignored so this call always wait for the snapshot to | |
5415 | * complete before returning. | |
5416 | * | |
5417 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
5418 | */ | |
5419 | int cmd_snapshot_record(struct ltt_session *session, | |
f46376a1 MJ |
5420 | const struct lttng_snapshot_output *output, |
5421 | int wait __attribute__((unused))) | |
6dc3064a | 5422 | { |
9a654598 JG |
5423 | enum lttng_error_code cmd_ret = LTTNG_OK; |
5424 | int ret; | |
00e1dfc4 | 5425 | unsigned int snapshot_success = 0; |
10ba83fe | 5426 | char datetime[16]; |
2abe7969 | 5427 | struct snapshot_output *tmp_output = NULL; |
6dc3064a | 5428 | |
a0377dfe FD |
5429 | LTTNG_ASSERT(session); |
5430 | LTTNG_ASSERT(output); | |
6dc3064a DG |
5431 | |
5432 | DBG("Cmd snapshot record for session %s", session->name); | |
5433 | ||
10ba83fe JR |
5434 | /* Get the datetime for the snapshot output directory. */ |
5435 | ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime, | |
5436 | sizeof(datetime)); | |
5437 | if (!ret) { | |
9a654598 | 5438 | cmd_ret = LTTNG_ERR_INVALID; |
10ba83fe JR |
5439 | goto error; |
5440 | } | |
5441 | ||
6dc3064a | 5442 | /* |
d3f14b8a MD |
5443 | * Permission denied to create an output if the session is not |
5444 | * set in no output mode. | |
6dc3064a DG |
5445 | */ |
5446 | if (session->output_traces) { | |
9a654598 | 5447 | cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
5448 | goto error; |
5449 | } | |
5450 | ||
5451 | /* The session needs to be started at least once. */ | |
8382cf6f | 5452 | if (!session->has_been_started) { |
9a654598 | 5453 | cmd_ret = LTTNG_ERR_START_SESSION_ONCE; |
6dc3064a DG |
5454 | goto error; |
5455 | } | |
5456 | ||
5457 | /* Use temporary output for the session. */ | |
ba45d9f0 | 5458 | if (*output->ctrl_url != '\0') { |
2abe7969 JG |
5459 | tmp_output = snapshot_output_alloc(); |
5460 | if (!tmp_output) { | |
5461 | cmd_ret = LTTNG_ERR_NOMEM; | |
5462 | goto error; | |
5463 | } | |
5464 | ||
b178f53e JG |
5465 | ret = snapshot_output_init(session, output->max_size, |
5466 | output->name, | |
5467 | output->ctrl_url, output->data_url, | |
5468 | session->consumer, | |
2abe7969 | 5469 | tmp_output, NULL); |
6dc3064a DG |
5470 | if (ret < 0) { |
5471 | if (ret == -ENOMEM) { | |
9a654598 | 5472 | cmd_ret = LTTNG_ERR_NOMEM; |
6dc3064a | 5473 | } else { |
9a654598 | 5474 | cmd_ret = LTTNG_ERR_INVALID; |
6dc3064a DG |
5475 | } |
5476 | goto error; | |
5477 | } | |
1bfe7328 | 5478 | /* Use the global session count for the temporary snapshot. */ |
2abe7969 | 5479 | tmp_output->nb_snapshot = session->snapshot.nb_snapshot; |
10ba83fe JR |
5480 | |
5481 | /* Use the global datetime */ | |
2abe7969 | 5482 | memcpy(tmp_output->datetime, datetime, sizeof(datetime)); |
f46376a1 | 5483 | cmd_ret = snapshot_record(session, tmp_output); |
fb9a95c4 | 5484 | if (cmd_ret != LTTNG_OK) { |
804c90a8 JR |
5485 | goto error; |
5486 | } | |
804c90a8 JR |
5487 | snapshot_success = 1; |
5488 | } else { | |
5489 | struct snapshot_output *sout; | |
5490 | struct lttng_ht_iter iter; | |
68808f4e | 5491 | |
804c90a8 JR |
5492 | rcu_read_lock(); |
5493 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, | |
5494 | &iter.iter, sout, node.node) { | |
2abe7969 JG |
5495 | struct snapshot_output output_copy; |
5496 | ||
804c90a8 | 5497 | /* |
2abe7969 JG |
5498 | * Make a local copy of the output and override output |
5499 | * parameters with those provided as part of the | |
5500 | * command. | |
804c90a8 | 5501 | */ |
2abe7969 | 5502 | memcpy(&output_copy, sout, sizeof(output_copy)); |
1bfe7328 | 5503 | |
804c90a8 | 5504 | if (output->max_size != (uint64_t) -1ULL) { |
2abe7969 | 5505 | output_copy.max_size = output->max_size; |
6dc3064a | 5506 | } |
d07ceecd | 5507 | |
2abe7969 JG |
5508 | output_copy.nb_snapshot = session->snapshot.nb_snapshot; |
5509 | memcpy(output_copy.datetime, datetime, | |
5510 | sizeof(datetime)); | |
6dc3064a | 5511 | |
804c90a8 JR |
5512 | /* Use temporary name. */ |
5513 | if (*output->name != '\0') { | |
2abe7969 JG |
5514 | if (lttng_strncpy(output_copy.name, |
5515 | output->name, | |
5516 | sizeof(output_copy.name))) { | |
9a654598 | 5517 | cmd_ret = LTTNG_ERR_INVALID; |
cf3e357d MD |
5518 | rcu_read_unlock(); |
5519 | goto error; | |
5520 | } | |
804c90a8 | 5521 | } |
e1986656 | 5522 | |
f46376a1 | 5523 | cmd_ret = snapshot_record(session, &output_copy); |
fb9a95c4 JG |
5524 | if (cmd_ret != LTTNG_OK) { |
5525 | rcu_read_unlock(); | |
5526 | goto error; | |
6dc3064a | 5527 | } |
804c90a8 | 5528 | snapshot_success = 1; |
6dc3064a | 5529 | } |
804c90a8 | 5530 | rcu_read_unlock(); |
6dc3064a DG |
5531 | } |
5532 | ||
1bfe7328 DG |
5533 | if (snapshot_success) { |
5534 | session->snapshot.nb_snapshot++; | |
b67578cb | 5535 | } else { |
9a654598 | 5536 | cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL; |
1bfe7328 DG |
5537 | } |
5538 | ||
6dc3064a | 5539 | error: |
2abe7969 JG |
5540 | if (tmp_output) { |
5541 | snapshot_output_destroy(tmp_output); | |
5542 | } | |
9a654598 | 5543 | return cmd_ret; |
6dc3064a DG |
5544 | } |
5545 | ||
d7ba1388 MD |
5546 | /* |
5547 | * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread. | |
5548 | */ | |
5549 | int cmd_set_session_shm_path(struct ltt_session *session, | |
5550 | const char *shm_path) | |
5551 | { | |
5552 | /* Safety net */ | |
a0377dfe | 5553 | LTTNG_ASSERT(session); |
d7ba1388 MD |
5554 | |
5555 | /* | |
5556 | * Can only set shm path before session is started. | |
5557 | */ | |
5558 | if (session->has_been_started) { | |
5559 | return LTTNG_ERR_SESSION_STARTED; | |
5560 | } | |
5561 | ||
5562 | strncpy(session->shm_path, shm_path, | |
5563 | sizeof(session->shm_path)); | |
5564 | session->shm_path[sizeof(session->shm_path) - 1] = '\0'; | |
5565 | ||
7e397c55 | 5566 | return LTTNG_OK; |
d7ba1388 MD |
5567 | } |
5568 | ||
5c408ad8 JD |
5569 | /* |
5570 | * Command LTTNG_ROTATE_SESSION from the lttng-ctl library. | |
5571 | * | |
5572 | * Ask the consumer to rotate the session output directory. | |
5573 | * The session lock must be held. | |
5574 | * | |
d5a1b7aa | 5575 | * Returns LTTNG_OK on success or else a negative LTTng error code. |
5c408ad8 JD |
5576 | */ |
5577 | int cmd_rotate_session(struct ltt_session *session, | |
7fdbed1c | 5578 | struct lttng_rotate_session_return *rotate_return, |
343defc2 MD |
5579 | bool quiet_rotation, |
5580 | enum lttng_trace_chunk_command_type command) | |
5c408ad8 JD |
5581 | { |
5582 | int ret; | |
d2956687 | 5583 | uint64_t ongoing_rotation_chunk_id; |
d5a1b7aa | 5584 | enum lttng_error_code cmd_ret = LTTNG_OK; |
d2956687 JG |
5585 | struct lttng_trace_chunk *chunk_being_archived = NULL; |
5586 | struct lttng_trace_chunk *new_trace_chunk = NULL; | |
5587 | enum lttng_trace_chunk_status chunk_status; | |
3156892b JG |
5588 | bool failed_to_rotate = false; |
5589 | enum lttng_error_code rotation_fail_code = LTTNG_OK; | |
5c408ad8 | 5590 | |
a0377dfe | 5591 | LTTNG_ASSERT(session); |
5c408ad8 JD |
5592 | |
5593 | if (!session->has_been_started) { | |
d5a1b7aa | 5594 | cmd_ret = LTTNG_ERR_START_SESSION_ONCE; |
d68c9a04 | 5595 | goto end; |
5c408ad8 JD |
5596 | } |
5597 | ||
d48d65e1 MD |
5598 | /* |
5599 | * Explicit rotation is not supported for live sessions. | |
5600 | * However, live sessions can perform a quiet rotation on | |
5601 | * destroy. | |
5602 | * Rotation is not supported for snapshot traces (no output). | |
5603 | */ | |
5604 | if ((!quiet_rotation && session->live_timer) || | |
5605 | !session->output_traces) { | |
d5a1b7aa | 5606 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE; |
d68c9a04 | 5607 | goto end; |
5c408ad8 JD |
5608 | } |
5609 | ||
d2956687 | 5610 | /* Unsupported feature in lttng-relayd before 2.11. */ |
070b6a86 | 5611 | if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET && |
5c408ad8 JD |
5612 | (session->consumer->relay_major_version == 2 && |
5613 | session->consumer->relay_minor_version < 11)) { | |
d5a1b7aa | 5614 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY; |
d68c9a04 | 5615 | goto end; |
5c408ad8 JD |
5616 | } |
5617 | ||
a40a503f MD |
5618 | /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */ |
5619 | if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) { | |
5620 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL; | |
5621 | goto end; | |
5622 | } | |
5623 | ||
92816cc3 | 5624 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { |
92816cc3 JG |
5625 | DBG("Refusing to launch a rotation; a rotation is already in progress for session %s", |
5626 | session->name); | |
d5a1b7aa | 5627 | cmd_ret = LTTNG_ERR_ROTATION_PENDING; |
d68c9a04 | 5628 | goto end; |
5c408ad8 JD |
5629 | } |
5630 | ||
5631 | /* | |
5632 | * After a stop, we only allow one rotation to occur, the other ones are | |
5633 | * useless until a new start. | |
5634 | */ | |
5635 | if (session->rotated_after_last_stop) { | |
5636 | DBG("Session \"%s\" was already rotated after stop, refusing rotation", | |
5637 | session->name); | |
d5a1b7aa | 5638 | cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP; |
d68c9a04 | 5639 | goto end; |
5c408ad8 | 5640 | } |
b02f5986 MD |
5641 | |
5642 | /* | |
5643 | * After a stop followed by a clear, disallow following rotations a they would | |
5644 | * generate empty chunks. | |
5645 | */ | |
5646 | if (session->cleared_after_last_stop) { | |
5647 | DBG("Session \"%s\" was already cleared after stop, refusing rotation", | |
5648 | session->name); | |
5649 | cmd_ret = LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR; | |
5650 | goto end; | |
5651 | } | |
5652 | ||
d2956687 | 5653 | if (session->active) { |
348a81dc | 5654 | new_trace_chunk = session_create_new_trace_chunk(session, NULL, |
d2956687 JG |
5655 | NULL, NULL); |
5656 | if (!new_trace_chunk) { | |
5657 | cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
5658 | goto error; | |
5c408ad8 | 5659 | } |
0e270a1e | 5660 | } |
2961f09e | 5661 | |
3156892b JG |
5662 | /* |
5663 | * The current trace chunk becomes the chunk being archived. | |
5664 | * | |
5665 | * After this point, "chunk_being_archived" must absolutely | |
5666 | * be closed on the consumer(s), otherwise it will never be | |
5667 | * cleaned-up, which will result in a leak. | |
5668 | */ | |
d2956687 JG |
5669 | ret = session_set_trace_chunk(session, new_trace_chunk, |
5670 | &chunk_being_archived); | |
5671 | if (ret) { | |
5672 | cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; | |
b178f53e JG |
5673 | goto error; |
5674 | } | |
5675 | ||
5c408ad8 | 5676 | if (session->kernel_session) { |
d5a1b7aa JG |
5677 | cmd_ret = kernel_rotate_session(session); |
5678 | if (cmd_ret != LTTNG_OK) { | |
3156892b JG |
5679 | failed_to_rotate = true; |
5680 | rotation_fail_code = cmd_ret; | |
5c408ad8 JD |
5681 | } |
5682 | } | |
5683 | if (session->ust_session) { | |
d5a1b7aa JG |
5684 | cmd_ret = ust_app_rotate_session(session); |
5685 | if (cmd_ret != LTTNG_OK) { | |
3156892b JG |
5686 | failed_to_rotate = true; |
5687 | rotation_fail_code = cmd_ret; | |
5c408ad8 | 5688 | } |
92816cc3 | 5689 | } |
17dd1232 | 5690 | |
3b61d9ee JG |
5691 | if (!session->active) { |
5692 | session->rotated_after_last_stop = true; | |
5693 | } | |
5694 | ||
5695 | if (!chunk_being_archived) { | |
5696 | DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check", | |
5697 | session->name); | |
5698 | if (failed_to_rotate) { | |
5699 | cmd_ret = rotation_fail_code; | |
5700 | goto error; | |
5701 | } | |
5702 | cmd_ret = LTTNG_OK; | |
5703 | goto end; | |
5704 | } | |
5705 | ||
5706 | session->rotation_state = LTTNG_ROTATION_STATE_ONGOING; | |
5707 | chunk_status = lttng_trace_chunk_get_id(chunk_being_archived, | |
5708 | &ongoing_rotation_chunk_id); | |
a0377dfe | 5709 | LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); |
3b61d9ee | 5710 | |
bbc4768c | 5711 | ret = session_close_trace_chunk(session, chunk_being_archived, |
343defc2 | 5712 | command, session->last_chunk_path); |
d2956687 JG |
5713 | if (ret) { |
5714 | cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER; | |
5715 | goto error; | |
5716 | } | |
5717 | ||
3156892b JG |
5718 | if (failed_to_rotate) { |
5719 | cmd_ret = rotation_fail_code; | |
5720 | goto error; | |
5721 | } | |
5722 | ||
7fdbed1c | 5723 | session->quiet_rotation = quiet_rotation; |
92816cc3 JG |
5724 | ret = timer_session_rotation_pending_check_start(session, |
5725 | DEFAULT_ROTATE_PENDING_TIMER); | |
5726 | if (ret) { | |
d5a1b7aa | 5727 | cmd_ret = LTTNG_ERR_UNK; |
2961f09e | 5728 | goto error; |
5c408ad8 JD |
5729 | } |
5730 | ||
5c408ad8 | 5731 | if (rotate_return) { |
d2956687 | 5732 | rotate_return->rotation_id = ongoing_rotation_chunk_id; |
5c408ad8 JD |
5733 | } |
5734 | ||
d2956687 JG |
5735 | session->chunk_being_archived = chunk_being_archived; |
5736 | chunk_being_archived = NULL; | |
7fdbed1c JG |
5737 | if (!quiet_rotation) { |
5738 | ret = notification_thread_command_session_rotation_ongoing( | |
139a8d25 | 5739 | the_notification_thread_handle, session->id, |
7fdbed1c JG |
5740 | ongoing_rotation_chunk_id); |
5741 | if (ret != LTTNG_OK) { | |
5742 | ERR("Failed to notify notification thread that a session rotation is ongoing for session %s", | |
5743 | session->name); | |
7966af57 | 5744 | cmd_ret = (lttng_error_code) ret; |
7fdbed1c | 5745 | } |
2961f09e JG |
5746 | } |
5747 | ||
92816cc3 | 5748 | DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent", |
d2956687 | 5749 | session->name, ongoing_rotation_chunk_id); |
5c408ad8 | 5750 | end: |
d2956687 JG |
5751 | lttng_trace_chunk_put(new_trace_chunk); |
5752 | lttng_trace_chunk_put(chunk_being_archived); | |
d5a1b7aa | 5753 | ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret); |
5c408ad8 | 5754 | return ret; |
2961f09e | 5755 | error: |
2961f09e | 5756 | if (session_reset_rotation_state(session, |
d2956687 | 5757 | LTTNG_ROTATION_STATE_ERROR)) { |
2961f09e JG |
5758 | ERR("Failed to reset rotation state of session \"%s\"", |
5759 | session->name); | |
5760 | } | |
5761 | goto end; | |
5c408ad8 JD |
5762 | } |
5763 | ||
5764 | /* | |
d68c9a04 | 5765 | * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library. |
5c408ad8 JD |
5766 | * |
5767 | * Check if the session has finished its rotation. | |
5768 | * | |
d2956687 | 5769 | * Return LTTNG_OK on success or else an LTTNG_ERR code. |
5c408ad8 | 5770 | */ |
d68c9a04 JD |
5771 | int cmd_rotate_get_info(struct ltt_session *session, |
5772 | struct lttng_rotation_get_info_return *info_return, | |
5773 | uint64_t rotation_id) | |
5c408ad8 | 5774 | { |
d2956687 JG |
5775 | enum lttng_error_code cmd_ret = LTTNG_OK; |
5776 | enum lttng_rotation_state rotation_state; | |
5c408ad8 | 5777 | |
d68c9a04 | 5778 | DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64, session->name, |
d2956687 | 5779 | session->most_recent_chunk_id.value); |
5c408ad8 | 5780 | |
d2956687 JG |
5781 | if (session->chunk_being_archived) { |
5782 | enum lttng_trace_chunk_status chunk_status; | |
5783 | uint64_t chunk_id; | |
5784 | ||
5785 | chunk_status = lttng_trace_chunk_get_id( | |
5786 | session->chunk_being_archived, | |
5787 | &chunk_id); | |
a0377dfe | 5788 | LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); |
d2956687 JG |
5789 | |
5790 | rotation_state = rotation_id == chunk_id ? | |
5791 | LTTNG_ROTATION_STATE_ONGOING : | |
5792 | LTTNG_ROTATION_STATE_EXPIRED; | |
5793 | } else { | |
5794 | if (session->last_archived_chunk_id.is_set && | |
5795 | rotation_id != session->last_archived_chunk_id.value) { | |
5796 | rotation_state = LTTNG_ROTATION_STATE_EXPIRED; | |
5797 | } else { | |
5798 | rotation_state = session->rotation_state; | |
5799 | } | |
5c408ad8 JD |
5800 | } |
5801 | ||
d2956687 JG |
5802 | switch (rotation_state) { |
5803 | case LTTNG_ROTATION_STATE_NO_ROTATION: | |
83ed9e90 | 5804 | DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"", |
d2956687 JG |
5805 | session->name); |
5806 | goto end; | |
5807 | case LTTNG_ROTATION_STATE_EXPIRED: | |
5808 | DBG("Reporting that the rotation state of rotation id %" PRIu64 " of session \"%s\" has expired", | |
5809 | rotation_id, session->name); | |
5810 | break; | |
d68c9a04 | 5811 | case LTTNG_ROTATION_STATE_ONGOING: |
d2956687 | 5812 | DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending", |
d68c9a04 JD |
5813 | rotation_id, session->name); |
5814 | break; | |
5815 | case LTTNG_ROTATION_STATE_COMPLETED: | |
dd73d57b | 5816 | { |
d2956687 JG |
5817 | int fmt_ret; |
5818 | char *chunk_path; | |
dd73d57b JG |
5819 | char *current_tracing_path_reply; |
5820 | size_t current_tracing_path_reply_len; | |
5821 | ||
d2956687 JG |
5822 | DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed", |
5823 | rotation_id, session->name); | |
5824 | ||
dd73d57b JG |
5825 | switch (session_get_consumer_destination_type(session)) { |
5826 | case CONSUMER_DST_LOCAL: | |
5827 | current_tracing_path_reply = | |
5828 | info_return->location.local.absolute_path; | |
5829 | current_tracing_path_reply_len = | |
5830 | sizeof(info_return->location.local.absolute_path); | |
5831 | info_return->location_type = | |
05f8afa9 | 5832 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL; |
ecd1a12f MD |
5833 | fmt_ret = asprintf(&chunk_path, |
5834 | "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s", | |
5835 | session_get_base_path(session), | |
5836 | session->last_archived_chunk_name); | |
5837 | if (fmt_ret == -1) { | |
5838 | PERROR("Failed to format the path of the last archived trace chunk"); | |
5839 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; | |
5840 | cmd_ret = LTTNG_ERR_UNK; | |
5841 | goto end; | |
5842 | } | |
dd73d57b JG |
5843 | break; |
5844 | case CONSUMER_DST_NET: | |
09cfbe47 JG |
5845 | { |
5846 | uint16_t ctrl_port, data_port; | |
5847 | ||
dd73d57b JG |
5848 | current_tracing_path_reply = |
5849 | info_return->location.relay.relative_path; | |
5850 | current_tracing_path_reply_len = | |
5851 | sizeof(info_return->location.relay.relative_path); | |
5852 | /* Currently the only supported relay protocol. */ | |
5853 | info_return->location.relay.protocol = | |
05f8afa9 | 5854 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP; |
dd73d57b | 5855 | |
d2956687 | 5856 | fmt_ret = lttng_strncpy(info_return->location.relay.host, |
dd73d57b JG |
5857 | session_get_net_consumer_hostname(session), |
5858 | sizeof(info_return->location.relay.host)); | |
d2956687 JG |
5859 | if (fmt_ret) { |
5860 | ERR("Failed to copy host name to rotate_get_info reply"); | |
dd73d57b | 5861 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
d2956687 | 5862 | cmd_ret = LTTNG_ERR_SET_URL; |
dd73d57b JG |
5863 | goto end; |
5864 | } | |
5865 | ||
09cfbe47 JG |
5866 | session_get_net_consumer_ports(session, &ctrl_port, &data_port); |
5867 | info_return->location.relay.ports.control = ctrl_port; | |
5868 | info_return->location.relay.ports.data = data_port; | |
dd73d57b | 5869 | info_return->location_type = |
05f8afa9 | 5870 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY; |
ecd1a12f MD |
5871 | chunk_path = strdup(session->last_chunk_path); |
5872 | if (!chunk_path) { | |
5873 | ERR("Failed to allocate the path of the last archived trace chunk"); | |
5874 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; | |
5875 | cmd_ret = LTTNG_ERR_UNK; | |
5876 | goto end; | |
5877 | } | |
dd73d57b | 5878 | break; |
09cfbe47 | 5879 | } |
dd73d57b JG |
5880 | default: |
5881 | abort(); | |
5882 | } | |
d2956687 JG |
5883 | |
5884 | fmt_ret = lttng_strncpy(current_tracing_path_reply, | |
5885 | chunk_path, current_tracing_path_reply_len); | |
5886 | free(chunk_path); | |
5887 | if (fmt_ret) { | |
5888 | ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply"); | |
d68c9a04 | 5889 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
d2956687 | 5890 | cmd_ret = LTTNG_ERR_UNK; |
5c408ad8 JD |
5891 | goto end; |
5892 | } | |
dd73d57b | 5893 | |
d68c9a04 | 5894 | break; |
dd73d57b | 5895 | } |
d68c9a04 | 5896 | case LTTNG_ROTATION_STATE_ERROR: |
d2956687 | 5897 | DBG("Reporting that an error occurred during rotation %" PRIu64 " of session \"%s\"", |
d68c9a04 JD |
5898 | rotation_id, session->name); |
5899 | break; | |
5900 | default: | |
5901 | abort(); | |
5c408ad8 JD |
5902 | } |
5903 | ||
d2956687 | 5904 | cmd_ret = LTTNG_OK; |
5c408ad8 | 5905 | end: |
d2956687 JG |
5906 | info_return->status = (int32_t) rotation_state; |
5907 | return cmd_ret; | |
5c408ad8 JD |
5908 | } |
5909 | ||
259c2674 JD |
5910 | /* |
5911 | * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library. | |
5912 | * | |
5913 | * Configure the automatic rotation parameters. | |
66ea93b1 JG |
5914 | * 'activate' to true means activate the rotation schedule type with 'new_value'. |
5915 | * 'activate' to false means deactivate the rotation schedule and validate that | |
5916 | * 'new_value' has the same value as the currently active value. | |
259c2674 | 5917 | * |
1136f41b | 5918 | * Return LTTNG_OK on success or else a positive LTTNG_ERR code. |
259c2674 JD |
5919 | */ |
5920 | int cmd_rotation_set_schedule(struct ltt_session *session, | |
66ea93b1 JG |
5921 | bool activate, enum lttng_rotation_schedule_type schedule_type, |
5922 | uint64_t new_value, | |
90936dcf | 5923 | struct notification_thread_handle *notification_thread_handle) |
259c2674 JD |
5924 | { |
5925 | int ret; | |
66ea93b1 | 5926 | uint64_t *parameter_value; |
259c2674 | 5927 | |
a0377dfe | 5928 | LTTNG_ASSERT(session); |
259c2674 JD |
5929 | |
5930 | DBG("Cmd rotate set schedule session %s", session->name); | |
5931 | ||
92fe5ca1 | 5932 | if (session->live_timer || !session->output_traces) { |
66ea93b1 | 5933 | DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session"); |
259c2674 JD |
5934 | ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE; |
5935 | goto end; | |
5936 | } | |
5937 | ||
66ea93b1 JG |
5938 | switch (schedule_type) { |
5939 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: | |
5940 | parameter_value = &session->rotate_size; | |
5941 | break; | |
5942 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: | |
5943 | parameter_value = &session->rotate_timer_period; | |
5944 | if (new_value >= UINT_MAX) { | |
5945 | DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64 " > %u (UINT_MAX)", | |
5946 | new_value, UINT_MAX); | |
5947 | ret = LTTNG_ERR_INVALID; | |
5948 | goto end; | |
5949 | } | |
5950 | break; | |
5951 | default: | |
5952 | WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type"); | |
5953 | ret = LTTNG_ERR_INVALID; | |
259c2674 | 5954 | goto end; |
66ea93b1 JG |
5955 | } |
5956 | ||
5957 | /* Improper use of the API. */ | |
5958 | if (new_value == -1ULL) { | |
5959 | WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1"); | |
5960 | ret = LTTNG_ERR_INVALID; | |
259c2674 JD |
5961 | goto end; |
5962 | } | |
5963 | ||
66ea93b1 JG |
5964 | /* |
5965 | * As indicated in struct ltt_session's comments, a value of == 0 means | |
5966 | * this schedule rotation type is not in use. | |
5967 | * | |
5968 | * Reject the command if we were asked to activate a schedule that was | |
5969 | * already active. | |
5970 | */ | |
5971 | if (activate && *parameter_value != 0) { | |
5972 | DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active"); | |
5973 | ret = LTTNG_ERR_ROTATION_SCHEDULE_SET; | |
90936dcf | 5974 | goto end; |
66ea93b1 JG |
5975 | } |
5976 | ||
5977 | /* | |
5978 | * Reject the command if we were asked to deactivate a schedule that was | |
5979 | * not active. | |
5980 | */ | |
5981 | if (!activate && *parameter_value == 0) { | |
5982 | DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive"); | |
5983 | ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET; | |
90936dcf JD |
5984 | goto end; |
5985 | } | |
5986 | ||
66ea93b1 JG |
5987 | /* |
5988 | * Reject the command if we were asked to deactivate a schedule that | |
5989 | * doesn't exist. | |
5990 | */ | |
5991 | if (!activate && *parameter_value != new_value) { | |
5992 | DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided"); | |
5993 | ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET; | |
5994 | goto end; | |
5995 | } | |
259c2674 | 5996 | |
66ea93b1 JG |
5997 | *parameter_value = activate ? new_value : 0; |
5998 | ||
5999 | switch (schedule_type) { | |
6000 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: | |
6001 | if (activate && session->active) { | |
6002 | /* | |
6003 | * Only start the timer if the session is active, | |
6004 | * otherwise it will be started when the session starts. | |
6005 | */ | |
92816cc3 JG |
6006 | ret = timer_session_rotation_schedule_timer_start( |
6007 | session, new_value); | |
259c2674 | 6008 | if (ret) { |
66ea93b1 | 6009 | ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command"); |
259c2674 JD |
6010 | ret = LTTNG_ERR_UNK; |
6011 | goto end; | |
6012 | } | |
66ea93b1 | 6013 | } else { |
92816cc3 JG |
6014 | ret = timer_session_rotation_schedule_timer_stop( |
6015 | session); | |
66ea93b1 JG |
6016 | if (ret) { |
6017 | ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command"); | |
6018 | ret = LTTNG_ERR_UNK; | |
f3ce6946 | 6019 | goto end; |
66ea93b1 | 6020 | } |
259c2674 | 6021 | } |
66ea93b1 JG |
6022 | break; |
6023 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: | |
6024 | if (activate) { | |
6025 | ret = subscribe_session_consumed_size_rotation(session, | |
6026 | new_value, notification_thread_handle); | |
90936dcf | 6027 | if (ret) { |
66ea93b1 | 6028 | ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command"); |
90936dcf JD |
6029 | ret = LTTNG_ERR_UNK; |
6030 | goto end; | |
6031 | } | |
90936dcf | 6032 | } else { |
66ea93b1 JG |
6033 | ret = unsubscribe_session_consumed_size_rotation(session, |
6034 | notification_thread_handle); | |
90936dcf | 6035 | if (ret) { |
66ea93b1 | 6036 | ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command"); |
90936dcf JD |
6037 | ret = LTTNG_ERR_UNK; |
6038 | goto end; | |
6039 | } | |
66ea93b1 | 6040 | |
90936dcf | 6041 | } |
66ea93b1 JG |
6042 | break; |
6043 | default: | |
6044 | /* Would have been caught before. */ | |
6045 | abort(); | |
90936dcf JD |
6046 | } |
6047 | ||
259c2674 JD |
6048 | ret = LTTNG_OK; |
6049 | ||
6050 | goto end; | |
6051 | ||
6052 | end: | |
6053 | return ret; | |
6054 | } | |
6055 | ||
a503e1ef JG |
6056 | /* Wait for a given path to be removed before continuing. */ |
6057 | static enum lttng_error_code wait_on_path(void *path_data) | |
6058 | { | |
7966af57 | 6059 | const char *shm_path = (const char *) path_data; |
a503e1ef JG |
6060 | |
6061 | DBG("Waiting for the shm path at %s to be removed before completing session destruction", | |
6062 | shm_path); | |
6063 | while (true) { | |
6064 | int ret; | |
6065 | struct stat st; | |
6066 | ||
6067 | ret = stat(shm_path, &st); | |
6068 | if (ret) { | |
6069 | if (errno != ENOENT) { | |
6070 | PERROR("stat() returned an error while checking for the existence of the shm path"); | |
6071 | } else { | |
6072 | DBG("shm path no longer exists, completing the destruction of session"); | |
6073 | } | |
6074 | break; | |
6075 | } else { | |
6076 | if (!S_ISDIR(st.st_mode)) { | |
6077 | ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal", | |
6078 | shm_path); | |
6079 | break; | |
6080 | } | |
6081 | } | |
6082 | usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US); | |
6083 | } | |
6084 | return LTTNG_OK; | |
6085 | } | |
6086 | ||
6087 | /* | |
6088 | * Returns a pointer to a handler to run on completion of a command. | |
6089 | * Returns NULL if no handler has to be run for the last command executed. | |
6090 | */ | |
6091 | const struct cmd_completion_handler *cmd_pop_completion_handler(void) | |
6092 | { | |
6093 | struct cmd_completion_handler *handler = current_completion_handler; | |
6094 | ||
6095 | current_completion_handler = NULL; | |
6096 | return handler; | |
6097 | } | |
6098 | ||
2f77fc4b DG |
6099 | /* |
6100 | * Init command subsystem. | |
6101 | */ | |
6102 | void cmd_init(void) | |
6103 | { | |
6104 | /* | |
d88aee68 DG |
6105 | * Set network sequence index to 1 for streams to match a relayd |
6106 | * socket on the consumer side. | |
2f77fc4b | 6107 | */ |
d88aee68 DG |
6108 | pthread_mutex_lock(&relayd_net_seq_idx_lock); |
6109 | relayd_net_seq_idx = 1; | |
6110 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
6111 | |
6112 | DBG("Command subsystem initialized"); | |
6113 | } |