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