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