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