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