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