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