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