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