Commit | Line | Data |
---|---|---|
2f77fc4b DG |
1 | /* |
2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> | |
bdf64013 | 3 | * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
2f77fc4b DG |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License, version 2 only, as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along with | |
15 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
16 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 | */ | |
18 | ||
6c1c0768 | 19 | #define _LGPL_SOURCE |
2f77fc4b | 20 | #include <assert.h> |
6dc3064a | 21 | #include <inttypes.h> |
2f77fc4b DG |
22 | #include <urcu/list.h> |
23 | #include <urcu/uatomic.h> | |
24 | ||
25 | #include <common/defaults.h> | |
26 | #include <common/common.h> | |
27 | #include <common/sessiond-comm/sessiond-comm.h> | |
28 | #include <common/relayd/relayd.h> | |
10a50311 | 29 | #include <common/utils.h> |
f5436bfc | 30 | #include <common/compat/string.h> |
93ec662e | 31 | #include <common/kernel-ctl/kernel-ctl.h> |
b0880ae5 JG |
32 | #include <common/dynamic-buffer.h> |
33 | #include <common/buffer-view.h> | |
34 | #include <lttng/trigger/trigger-internal.h> | |
35 | #include <lttng/condition/condition.h> | |
36 | #include <lttng/action/action.h> | |
6e21424e | 37 | #include <lttng/channel.h> |
b0880ae5 | 38 | #include <lttng/channel-internal.h> |
5c408ad8 | 39 | #include <lttng/rotate-internal.h> |
9f449915 | 40 | #include <common/string-utils/string-utils.h> |
2f77fc4b DG |
41 | |
42 | #include "channel.h" | |
43 | #include "consumer.h" | |
44 | #include "event.h" | |
8782cc74 | 45 | #include "health-sessiond.h" |
2f77fc4b DG |
46 | #include "kernel.h" |
47 | #include "kernel-consumer.h" | |
48 | #include "lttng-sessiond.h" | |
49 | #include "utils.h" | |
0dbc2034 | 50 | #include "lttng-syscall.h" |
7c1d2758 | 51 | #include "agent.h" |
93ec662e | 52 | #include "buffer-registry.h" |
b0880ae5 JG |
53 | #include "notification-thread.h" |
54 | #include "notification-thread-commands.h" | |
5c408ad8 JD |
55 | #include "rotate.h" |
56 | #include "rotation-thread.h" | |
57 | #include "sessiond-timer.h" | |
f28f9e44 | 58 | #include "agent-thread.h" |
2f77fc4b DG |
59 | |
60 | #include "cmd.h" | |
61 | ||
62 | /* | |
63 | * Used to keep a unique index for each relayd socket created where this value | |
64 | * is associated with streams on the consumer so it can match the right relayd | |
d88aee68 DG |
65 | * to send to. It must be accessed with the relayd_net_seq_idx_lock |
66 | * held. | |
2f77fc4b | 67 | */ |
d88aee68 DG |
68 | static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER; |
69 | static uint64_t relayd_net_seq_idx; | |
2f77fc4b | 70 | |
7076b56e JG |
71 | static int validate_ust_event_name(const char *); |
72 | static int cmd_enable_event_internal(struct ltt_session *session, | |
73 | struct lttng_domain *domain, | |
74 | char *channel_name, struct lttng_event *event, | |
75 | char *filter_expression, | |
76 | struct lttng_filter_bytecode *filter, | |
77 | struct lttng_event_exclusion *exclusion, | |
78 | int wpipe); | |
79 | ||
2f77fc4b DG |
80 | /* |
81 | * Create a session path used by list_lttng_sessions for the case that the | |
82 | * session consumer is on the network. | |
83 | */ | |
84 | static int build_network_session_path(char *dst, size_t size, | |
85 | struct ltt_session *session) | |
86 | { | |
87 | int ret, kdata_port, udata_port; | |
88 | struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL; | |
89 | char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX]; | |
90 | ||
91 | assert(session); | |
92 | assert(dst); | |
93 | ||
94 | memset(tmp_urls, 0, sizeof(tmp_urls)); | |
95 | memset(tmp_uurl, 0, sizeof(tmp_uurl)); | |
96 | ||
97 | kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT; | |
98 | ||
99 | if (session->kernel_session && session->kernel_session->consumer) { | |
100 | kuri = &session->kernel_session->consumer->dst.net.control; | |
101 | kdata_port = session->kernel_session->consumer->dst.net.data.port; | |
102 | } | |
103 | ||
104 | if (session->ust_session && session->ust_session->consumer) { | |
105 | uuri = &session->ust_session->consumer->dst.net.control; | |
106 | udata_port = session->ust_session->consumer->dst.net.data.port; | |
107 | } | |
108 | ||
109 | if (uuri == NULL && kuri == NULL) { | |
110 | uri = &session->consumer->dst.net.control; | |
111 | kdata_port = session->consumer->dst.net.data.port; | |
112 | } else if (kuri && uuri) { | |
113 | ret = uri_compare(kuri, uuri); | |
114 | if (ret) { | |
115 | /* Not Equal */ | |
116 | uri = kuri; | |
117 | /* Build uuri URL string */ | |
118 | ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl)); | |
119 | if (ret < 0) { | |
120 | goto error; | |
121 | } | |
122 | } else { | |
123 | uri = kuri; | |
124 | } | |
125 | } else if (kuri && uuri == NULL) { | |
126 | uri = kuri; | |
127 | } else if (uuri && kuri == NULL) { | |
128 | uri = uuri; | |
129 | } | |
130 | ||
131 | ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls)); | |
132 | if (ret < 0) { | |
133 | goto error; | |
134 | } | |
135 | ||
9aa9f900 DG |
136 | /* |
137 | * Do we have a UST url set. If yes, this means we have both kernel and UST | |
138 | * to print. | |
139 | */ | |
9d035200 | 140 | if (*tmp_uurl != '\0') { |
2f77fc4b DG |
141 | ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]", |
142 | tmp_urls, kdata_port, tmp_uurl, udata_port); | |
143 | } else { | |
9aa9f900 | 144 | int dport; |
bef08707 | 145 | if (kuri || (!kuri && !uuri)) { |
9aa9f900 DG |
146 | dport = kdata_port; |
147 | } else { | |
148 | /* No kernel URI, use the UST port. */ | |
149 | dport = udata_port; | |
150 | } | |
151 | ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport); | |
2f77fc4b DG |
152 | } |
153 | ||
154 | error: | |
155 | return ret; | |
156 | } | |
157 | ||
fb83fe64 JD |
158 | /* |
159 | * Get run-time attributes if the session has been started (discarded events, | |
160 | * lost packets). | |
161 | */ | |
162 | static int get_kernel_runtime_stats(struct ltt_session *session, | |
163 | struct ltt_kernel_channel *kchan, uint64_t *discarded_events, | |
164 | uint64_t *lost_packets) | |
165 | { | |
166 | int ret; | |
167 | ||
168 | if (!session->has_been_started) { | |
169 | ret = 0; | |
170 | *discarded_events = 0; | |
171 | *lost_packets = 0; | |
172 | goto end; | |
173 | } | |
174 | ||
e1f3997a | 175 | ret = consumer_get_discarded_events(session->id, kchan->key, |
fb83fe64 JD |
176 | session->kernel_session->consumer, |
177 | discarded_events); | |
178 | if (ret < 0) { | |
179 | goto end; | |
180 | } | |
181 | ||
e1f3997a | 182 | ret = consumer_get_lost_packets(session->id, kchan->key, |
fb83fe64 JD |
183 | session->kernel_session->consumer, |
184 | lost_packets); | |
185 | if (ret < 0) { | |
186 | goto end; | |
187 | } | |
188 | ||
189 | end: | |
190 | return ret; | |
191 | } | |
192 | ||
193 | /* | |
194 | * Get run-time attributes if the session has been started (discarded events, | |
195 | * lost packets). | |
196 | */ | |
197 | static int get_ust_runtime_stats(struct ltt_session *session, | |
198 | struct ltt_ust_channel *uchan, uint64_t *discarded_events, | |
199 | uint64_t *lost_packets) | |
200 | { | |
201 | int ret; | |
202 | struct ltt_ust_session *usess; | |
203 | ||
a91c5803 JG |
204 | if (!discarded_events || !lost_packets) { |
205 | ret = -1; | |
206 | goto end; | |
207 | } | |
208 | ||
fb83fe64 | 209 | usess = session->ust_session; |
a905c624 JG |
210 | assert(discarded_events); |
211 | assert(lost_packets); | |
fb83fe64 JD |
212 | |
213 | if (!usess || !session->has_been_started) { | |
214 | *discarded_events = 0; | |
215 | *lost_packets = 0; | |
216 | ret = 0; | |
217 | goto end; | |
218 | } | |
219 | ||
220 | if (usess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
221 | ret = ust_app_uid_get_channel_runtime_stats(usess->id, | |
222 | &usess->buffer_reg_uid_list, | |
223 | usess->consumer, uchan->id, | |
224 | uchan->attr.overwrite, | |
225 | discarded_events, | |
226 | lost_packets); | |
227 | } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) { | |
228 | ret = ust_app_pid_get_channel_runtime_stats(usess, | |
229 | uchan, usess->consumer, | |
230 | uchan->attr.overwrite, | |
231 | discarded_events, | |
232 | lost_packets); | |
233 | if (ret < 0) { | |
234 | goto end; | |
235 | } | |
236 | *discarded_events += uchan->per_pid_closed_app_discarded; | |
237 | *lost_packets += uchan->per_pid_closed_app_lost; | |
238 | } else { | |
239 | ERR("Unsupported buffer type"); | |
967bc289 | 240 | assert(0); |
fb83fe64 JD |
241 | ret = -1; |
242 | goto end; | |
243 | } | |
244 | ||
245 | end: | |
246 | return ret; | |
247 | } | |
248 | ||
2f77fc4b DG |
249 | /* |
250 | * Fill lttng_channel array of all channels. | |
251 | */ | |
d449df4a | 252 | static ssize_t list_lttng_channels(enum lttng_domain_type domain, |
fb83fe64 | 253 | struct ltt_session *session, struct lttng_channel *channels, |
cf0bcb51 | 254 | struct lttng_channel_extended *chan_exts) |
2f77fc4b | 255 | { |
d449df4a | 256 | int i = 0, ret = 0; |
2f77fc4b DG |
257 | struct ltt_kernel_channel *kchan; |
258 | ||
259 | DBG("Listing channels for session %s", session->name); | |
260 | ||
261 | switch (domain) { | |
262 | case LTTNG_DOMAIN_KERNEL: | |
263 | /* Kernel channels */ | |
264 | if (session->kernel_session != NULL) { | |
265 | cds_list_for_each_entry(kchan, | |
266 | &session->kernel_session->channel_list.head, list) { | |
fb83fe64 | 267 | uint64_t discarded_events, lost_packets; |
cf0bcb51 JG |
268 | struct lttng_channel_extended *extended; |
269 | ||
270 | extended = (struct lttng_channel_extended *) | |
271 | kchan->channel->attr.extended.ptr; | |
fb83fe64 JD |
272 | |
273 | ret = get_kernel_runtime_stats(session, kchan, | |
274 | &discarded_events, &lost_packets); | |
275 | if (ret < 0) { | |
276 | goto end; | |
277 | } | |
2f77fc4b DG |
278 | /* Copy lttng_channel struct to array */ |
279 | memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel)); | |
280 | channels[i].enabled = kchan->enabled; | |
fb83fe64 JD |
281 | chan_exts[i].discarded_events = |
282 | discarded_events; | |
283 | chan_exts[i].lost_packets = lost_packets; | |
cf0bcb51 JG |
284 | chan_exts[i].monitor_timer_interval = |
285 | extended->monitor_timer_interval; | |
491d1539 | 286 | chan_exts[i].blocking_timeout = 0; |
2f77fc4b DG |
287 | i++; |
288 | } | |
289 | } | |
290 | break; | |
291 | case LTTNG_DOMAIN_UST: | |
292 | { | |
293 | struct lttng_ht_iter iter; | |
294 | struct ltt_ust_channel *uchan; | |
295 | ||
e7fe706f | 296 | rcu_read_lock(); |
2f77fc4b DG |
297 | cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht, |
298 | &iter.iter, uchan, node.node) { | |
a91c5803 | 299 | uint64_t discarded_events = 0, lost_packets = 0; |
fb83fe64 | 300 | |
8ee609c8 MD |
301 | if (lttng_strncpy(channels[i].name, uchan->name, |
302 | LTTNG_SYMBOL_NAME_LEN)) { | |
303 | break; | |
304 | } | |
2f77fc4b DG |
305 | channels[i].attr.overwrite = uchan->attr.overwrite; |
306 | channels[i].attr.subbuf_size = uchan->attr.subbuf_size; | |
307 | channels[i].attr.num_subbuf = uchan->attr.num_subbuf; | |
308 | channels[i].attr.switch_timer_interval = | |
309 | uchan->attr.switch_timer_interval; | |
310 | channels[i].attr.read_timer_interval = | |
311 | uchan->attr.read_timer_interval; | |
312 | channels[i].enabled = uchan->enabled; | |
2f785fe7 DG |
313 | channels[i].attr.tracefile_size = uchan->tracefile_size; |
314 | channels[i].attr.tracefile_count = uchan->tracefile_count; | |
5e4435a4 JG |
315 | |
316 | /* | |
317 | * Map enum lttng_ust_output to enum lttng_event_output. | |
318 | */ | |
2f77fc4b DG |
319 | switch (uchan->attr.output) { |
320 | case LTTNG_UST_MMAP: | |
2f77fc4b DG |
321 | channels[i].attr.output = LTTNG_EVENT_MMAP; |
322 | break; | |
5e4435a4 JG |
323 | default: |
324 | /* | |
325 | * LTTNG_UST_MMAP is the only supported UST | |
326 | * output mode. | |
327 | */ | |
328 | assert(0); | |
329 | break; | |
2f77fc4b | 330 | } |
fb83fe64 | 331 | |
d449df4a MD |
332 | chan_exts[i].monitor_timer_interval = |
333 | uchan->monitor_timer_interval; | |
491d1539 MD |
334 | chan_exts[i].blocking_timeout = |
335 | uchan->attr.u.s.blocking_timeout; | |
d449df4a | 336 | |
fb83fe64 JD |
337 | ret = get_ust_runtime_stats(session, uchan, |
338 | &discarded_events, &lost_packets); | |
339 | if (ret < 0) { | |
340 | break; | |
341 | } | |
342 | chan_exts[i].discarded_events = discarded_events; | |
343 | chan_exts[i].lost_packets = lost_packets; | |
2f77fc4b DG |
344 | i++; |
345 | } | |
e7fe706f | 346 | rcu_read_unlock(); |
2f77fc4b DG |
347 | break; |
348 | } | |
349 | default: | |
350 | break; | |
351 | } | |
fb83fe64 JD |
352 | |
353 | end: | |
d449df4a MD |
354 | if (ret < 0) { |
355 | return -LTTNG_ERR_FATAL; | |
356 | } else { | |
357 | return LTTNG_OK; | |
358 | } | |
2f77fc4b DG |
359 | } |
360 | ||
b4e3ceb9 | 361 | static void increment_extended_len(const char *filter_expression, |
795d57ce | 362 | struct lttng_event_exclusion *exclusion, size_t *extended_len) |
b4e3ceb9 PP |
363 | { |
364 | *extended_len += sizeof(struct lttcomm_event_extended_header); | |
365 | ||
366 | if (filter_expression) { | |
367 | *extended_len += strlen(filter_expression) + 1; | |
368 | } | |
795d57ce PP |
369 | |
370 | if (exclusion) { | |
371 | *extended_len += exclusion->count * LTTNG_SYMBOL_NAME_LEN; | |
372 | } | |
b4e3ceb9 PP |
373 | } |
374 | ||
375 | static void append_extended_info(const char *filter_expression, | |
795d57ce | 376 | struct lttng_event_exclusion *exclusion, void **extended_at) |
b4e3ceb9 PP |
377 | { |
378 | struct lttcomm_event_extended_header extended_header; | |
379 | size_t filter_len = 0; | |
795d57ce | 380 | size_t nb_exclusions = 0; |
b4e3ceb9 PP |
381 | |
382 | if (filter_expression) { | |
383 | filter_len = strlen(filter_expression) + 1; | |
384 | } | |
385 | ||
795d57ce PP |
386 | if (exclusion) { |
387 | nb_exclusions = exclusion->count; | |
388 | } | |
389 | ||
390 | /* Set header fields */ | |
b4e3ceb9 | 391 | extended_header.filter_len = filter_len; |
795d57ce PP |
392 | extended_header.nb_exclusions = nb_exclusions; |
393 | ||
394 | /* Copy header */ | |
b4e3ceb9 PP |
395 | memcpy(*extended_at, &extended_header, sizeof(extended_header)); |
396 | *extended_at += sizeof(extended_header); | |
795d57ce PP |
397 | |
398 | /* Copy filter string */ | |
399 | if (filter_expression) { | |
400 | memcpy(*extended_at, filter_expression, filter_len); | |
401 | *extended_at += filter_len; | |
402 | } | |
403 | ||
404 | /* Copy exclusion names */ | |
405 | if (exclusion) { | |
406 | size_t len = nb_exclusions * LTTNG_SYMBOL_NAME_LEN; | |
407 | ||
408 | memcpy(*extended_at, &exclusion->names, len); | |
409 | *extended_at += len; | |
410 | } | |
b4e3ceb9 PP |
411 | } |
412 | ||
3c6a091f | 413 | /* |
022d91ba | 414 | * Create a list of agent domain events. |
3c6a091f DG |
415 | * |
416 | * Return number of events in list on success or else a negative value. | |
417 | */ | |
022d91ba | 418 | static int list_lttng_agent_events(struct agent *agt, |
b4e3ceb9 | 419 | struct lttng_event **events, size_t *total_size) |
3c6a091f DG |
420 | { |
421 | int i = 0, ret = 0; | |
422 | unsigned int nb_event = 0; | |
022d91ba | 423 | struct agent_event *event; |
3c6a091f DG |
424 | struct lttng_event *tmp_events; |
425 | struct lttng_ht_iter iter; | |
b4e3ceb9 PP |
426 | size_t extended_len = 0; |
427 | void *extended_at; | |
3c6a091f | 428 | |
022d91ba | 429 | assert(agt); |
3c6a091f DG |
430 | assert(events); |
431 | ||
022d91ba | 432 | DBG3("Listing agent events"); |
3c6a091f | 433 | |
e5bbf678 | 434 | rcu_read_lock(); |
022d91ba | 435 | nb_event = lttng_ht_get_count(agt->events); |
e5bbf678 | 436 | rcu_read_unlock(); |
3c6a091f DG |
437 | if (nb_event == 0) { |
438 | ret = nb_event; | |
b4e3ceb9 | 439 | *total_size = 0; |
3c6a091f DG |
440 | goto error; |
441 | } | |
442 | ||
b4e3ceb9 PP |
443 | /* Compute required extended infos size */ |
444 | extended_len = nb_event * sizeof(struct lttcomm_event_extended_header); | |
445 | ||
446 | /* | |
447 | * This is only valid because the commands which add events are | |
448 | * processed in the same thread as the listing. | |
449 | */ | |
450 | rcu_read_lock(); | |
451 | cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) { | |
795d57ce PP |
452 | increment_extended_len(event->filter_expression, NULL, |
453 | &extended_len); | |
b4e3ceb9 PP |
454 | } |
455 | rcu_read_unlock(); | |
456 | ||
457 | *total_size = nb_event * sizeof(*tmp_events) + extended_len; | |
458 | tmp_events = zmalloc(*total_size); | |
3c6a091f | 459 | if (!tmp_events) { |
022d91ba | 460 | PERROR("zmalloc agent events session"); |
3c6a091f DG |
461 | ret = -LTTNG_ERR_FATAL; |
462 | goto error; | |
463 | } | |
464 | ||
b4e3ceb9 PP |
465 | extended_at = ((uint8_t *) tmp_events) + |
466 | nb_event * sizeof(struct lttng_event); | |
467 | ||
3c6a091f | 468 | rcu_read_lock(); |
022d91ba | 469 | cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) { |
3c6a091f DG |
470 | strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name)); |
471 | tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0'; | |
472 | tmp_events[i].enabled = event->enabled; | |
2106efa0 | 473 | tmp_events[i].loglevel = event->loglevel_value; |
ff94328f | 474 | tmp_events[i].loglevel_type = event->loglevel_type; |
3c6a091f | 475 | i++; |
b4e3ceb9 PP |
476 | |
477 | /* Append extended info */ | |
795d57ce PP |
478 | append_extended_info(event->filter_expression, NULL, |
479 | &extended_at); | |
3c6a091f DG |
480 | } |
481 | rcu_read_unlock(); | |
482 | ||
483 | *events = tmp_events; | |
484 | ret = nb_event; | |
485 | ||
486 | error: | |
487 | assert(nb_event == i); | |
488 | return ret; | |
489 | } | |
490 | ||
2f77fc4b DG |
491 | /* |
492 | * Create a list of ust global domain events. | |
493 | */ | |
494 | static int list_lttng_ust_global_events(char *channel_name, | |
b4e3ceb9 PP |
495 | struct ltt_ust_domain_global *ust_global, |
496 | struct lttng_event **events, size_t *total_size) | |
2f77fc4b DG |
497 | { |
498 | int i = 0, ret = 0; | |
499 | unsigned int nb_event = 0; | |
500 | struct lttng_ht_iter iter; | |
501 | struct lttng_ht_node_str *node; | |
502 | struct ltt_ust_channel *uchan; | |
503 | struct ltt_ust_event *uevent; | |
504 | struct lttng_event *tmp; | |
b4e3ceb9 PP |
505 | size_t extended_len = 0; |
506 | void *extended_at; | |
2f77fc4b DG |
507 | |
508 | DBG("Listing UST global events for channel %s", channel_name); | |
509 | ||
510 | rcu_read_lock(); | |
511 | ||
512 | lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter); | |
513 | node = lttng_ht_iter_get_node_str(&iter); | |
514 | if (node == NULL) { | |
f73fabfd | 515 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
d31d3e8c | 516 | goto end; |
2f77fc4b DG |
517 | } |
518 | ||
519 | uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node); | |
520 | ||
3c442be3 | 521 | nb_event = lttng_ht_get_count(uchan->events); |
2f77fc4b DG |
522 | if (nb_event == 0) { |
523 | ret = nb_event; | |
b4e3ceb9 | 524 | *total_size = 0; |
d31d3e8c | 525 | goto end; |
2f77fc4b DG |
526 | } |
527 | ||
528 | DBG3("Listing UST global %d events", nb_event); | |
529 | ||
b4e3ceb9 PP |
530 | /* Compute required extended infos size */ |
531 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { | |
532 | if (uevent->internal) { | |
533 | nb_event--; | |
534 | continue; | |
535 | } | |
536 | ||
537 | increment_extended_len(uevent->filter_expression, | |
795d57ce | 538 | uevent->exclusion, &extended_len); |
b4e3ceb9 | 539 | } |
d31d3e8c PP |
540 | if (nb_event == 0) { |
541 | /* All events are internal, skip. */ | |
542 | ret = 0; | |
543 | *total_size = 0; | |
544 | goto end; | |
545 | } | |
b4e3ceb9 PP |
546 | |
547 | *total_size = nb_event * sizeof(struct lttng_event) + extended_len; | |
548 | tmp = zmalloc(*total_size); | |
2f77fc4b | 549 | if (tmp == NULL) { |
b12c38df JG |
550 | ret = -LTTNG_ERR_FATAL; |
551 | goto end; | |
2f77fc4b DG |
552 | } |
553 | ||
b4e3ceb9 PP |
554 | extended_at = ((uint8_t *) tmp) + nb_event * sizeof(struct lttng_event); |
555 | ||
2f77fc4b | 556 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { |
3c442be3 JG |
557 | if (uevent->internal) { |
558 | /* This event should remain hidden from clients */ | |
3c442be3 JG |
559 | continue; |
560 | } | |
2f77fc4b DG |
561 | strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN); |
562 | tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
563 | tmp[i].enabled = uevent->enabled; | |
564 | ||
565 | switch (uevent->attr.instrumentation) { | |
566 | case LTTNG_UST_TRACEPOINT: | |
567 | tmp[i].type = LTTNG_EVENT_TRACEPOINT; | |
568 | break; | |
569 | case LTTNG_UST_PROBE: | |
570 | tmp[i].type = LTTNG_EVENT_PROBE; | |
571 | break; | |
572 | case LTTNG_UST_FUNCTION: | |
573 | tmp[i].type = LTTNG_EVENT_FUNCTION; | |
574 | break; | |
575 | } | |
576 | ||
577 | tmp[i].loglevel = uevent->attr.loglevel; | |
578 | switch (uevent->attr.loglevel_type) { | |
579 | case LTTNG_UST_LOGLEVEL_ALL: | |
580 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
581 | break; | |
582 | case LTTNG_UST_LOGLEVEL_RANGE: | |
583 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; | |
584 | break; | |
585 | case LTTNG_UST_LOGLEVEL_SINGLE: | |
586 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; | |
587 | break; | |
588 | } | |
589 | if (uevent->filter) { | |
590 | tmp[i].filter = 1; | |
591 | } | |
4634f12e JI |
592 | if (uevent->exclusion) { |
593 | tmp[i].exclusion = 1; | |
594 | } | |
2f77fc4b | 595 | i++; |
b4e3ceb9 PP |
596 | |
597 | /* Append extended info */ | |
795d57ce PP |
598 | append_extended_info(uevent->filter_expression, |
599 | uevent->exclusion, &extended_at); | |
2f77fc4b DG |
600 | } |
601 | ||
602 | ret = nb_event; | |
603 | *events = tmp; | |
d31d3e8c | 604 | end: |
2f77fc4b DG |
605 | rcu_read_unlock(); |
606 | return ret; | |
607 | } | |
608 | ||
609 | /* | |
610 | * Fill lttng_event array of all kernel events in the channel. | |
611 | */ | |
612 | static int list_lttng_kernel_events(char *channel_name, | |
b4e3ceb9 PP |
613 | struct ltt_kernel_session *kernel_session, |
614 | struct lttng_event **events, size_t *total_size) | |
2f77fc4b DG |
615 | { |
616 | int i = 0, ret; | |
617 | unsigned int nb_event; | |
618 | struct ltt_kernel_event *event; | |
619 | struct ltt_kernel_channel *kchan; | |
b4e3ceb9 PP |
620 | size_t extended_len = 0; |
621 | void *extended_at; | |
2f77fc4b DG |
622 | |
623 | kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session); | |
624 | if (kchan == NULL) { | |
f73fabfd | 625 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
2f77fc4b DG |
626 | goto error; |
627 | } | |
628 | ||
629 | nb_event = kchan->event_count; | |
630 | ||
631 | DBG("Listing events for channel %s", kchan->channel->name); | |
632 | ||
633 | if (nb_event == 0) { | |
b4e3ceb9 | 634 | *total_size = 0; |
834978fd | 635 | *events = NULL; |
db906c12 | 636 | goto end; |
2f77fc4b DG |
637 | } |
638 | ||
b4e3ceb9 PP |
639 | /* Compute required extended infos size */ |
640 | cds_list_for_each_entry(event, &kchan->events_list.head, list) { | |
795d57ce PP |
641 | increment_extended_len(event->filter_expression, NULL, |
642 | &extended_len); | |
b4e3ceb9 PP |
643 | } |
644 | ||
645 | *total_size = nb_event * sizeof(struct lttng_event) + extended_len; | |
646 | *events = zmalloc(*total_size); | |
2f77fc4b | 647 | if (*events == NULL) { |
f73fabfd | 648 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
649 | goto error; |
650 | } | |
651 | ||
ab4aa612 | 652 | extended_at = ((void *) *events) + |
b4e3ceb9 PP |
653 | nb_event * sizeof(struct lttng_event); |
654 | ||
2f77fc4b DG |
655 | /* Kernel channels */ |
656 | cds_list_for_each_entry(event, &kchan->events_list.head , list) { | |
657 | strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN); | |
658 | (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
659 | (*events)[i].enabled = event->enabled; | |
00f992f2 JG |
660 | (*events)[i].filter = |
661 | (unsigned char) !!event->filter_expression; | |
2f77fc4b DG |
662 | |
663 | switch (event->event->instrumentation) { | |
664 | case LTTNG_KERNEL_TRACEPOINT: | |
665 | (*events)[i].type = LTTNG_EVENT_TRACEPOINT; | |
666 | break; | |
2f77fc4b | 667 | case LTTNG_KERNEL_KRETPROBE: |
1896972b DG |
668 | (*events)[i].type = LTTNG_EVENT_FUNCTION; |
669 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, | |
670 | sizeof(struct lttng_kernel_kprobe)); | |
671 | break; | |
672 | case LTTNG_KERNEL_KPROBE: | |
2f77fc4b DG |
673 | (*events)[i].type = LTTNG_EVENT_PROBE; |
674 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, | |
675 | sizeof(struct lttng_kernel_kprobe)); | |
676 | break; | |
677 | case LTTNG_KERNEL_FUNCTION: | |
678 | (*events)[i].type = LTTNG_EVENT_FUNCTION; | |
679 | memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace, | |
680 | sizeof(struct lttng_kernel_function)); | |
681 | break; | |
682 | case LTTNG_KERNEL_NOOP: | |
683 | (*events)[i].type = LTTNG_EVENT_NOOP; | |
684 | break; | |
685 | case LTTNG_KERNEL_SYSCALL: | |
686 | (*events)[i].type = LTTNG_EVENT_SYSCALL; | |
687 | break; | |
688 | case LTTNG_KERNEL_ALL: | |
689 | assert(0); | |
690 | break; | |
691 | } | |
692 | i++; | |
b4e3ceb9 PP |
693 | |
694 | /* Append extended info */ | |
795d57ce PP |
695 | append_extended_info(event->filter_expression, NULL, |
696 | &extended_at); | |
2f77fc4b DG |
697 | } |
698 | ||
db906c12 | 699 | end: |
2f77fc4b DG |
700 | return nb_event; |
701 | ||
702 | error: | |
f73fabfd DG |
703 | /* Negate the error code to differentiate the size from an error */ |
704 | return -ret; | |
2f77fc4b DG |
705 | } |
706 | ||
707 | /* | |
708 | * Add URI so the consumer output object. Set the correct path depending on the | |
709 | * domain adding the default trace directory. | |
710 | */ | |
711 | static int add_uri_to_consumer(struct consumer_output *consumer, | |
56a37563 JG |
712 | struct lttng_uri *uri, enum lttng_domain_type domain, |
713 | const char *session_name) | |
2f77fc4b | 714 | { |
f73fabfd | 715 | int ret = LTTNG_OK; |
2f77fc4b DG |
716 | const char *default_trace_dir; |
717 | ||
718 | assert(uri); | |
719 | ||
720 | if (consumer == NULL) { | |
721 | DBG("No consumer detected. Don't add URI. Stopping."); | |
f73fabfd | 722 | ret = LTTNG_ERR_NO_CONSUMER; |
2f77fc4b DG |
723 | goto error; |
724 | } | |
725 | ||
726 | switch (domain) { | |
727 | case LTTNG_DOMAIN_KERNEL: | |
728 | default_trace_dir = DEFAULT_KERNEL_TRACE_DIR; | |
729 | break; | |
730 | case LTTNG_DOMAIN_UST: | |
731 | default_trace_dir = DEFAULT_UST_TRACE_DIR; | |
732 | break; | |
733 | default: | |
734 | /* | |
735 | * This case is possible is we try to add the URI to the global tracing | |
736 | * session consumer object which in this case there is no subdir. | |
737 | */ | |
738 | default_trace_dir = ""; | |
739 | } | |
740 | ||
741 | switch (uri->dtype) { | |
742 | case LTTNG_DST_IPV4: | |
743 | case LTTNG_DST_IPV6: | |
744 | DBG2("Setting network URI to consumer"); | |
745 | ||
df75acac DG |
746 | if (consumer->type == CONSUMER_DST_NET) { |
747 | if ((uri->stype == LTTNG_STREAM_CONTROL && | |
785d2d0d DG |
748 | consumer->dst.net.control_isset) || |
749 | (uri->stype == LTTNG_STREAM_DATA && | |
750 | consumer->dst.net.data_isset)) { | |
df75acac DG |
751 | ret = LTTNG_ERR_URL_EXIST; |
752 | goto error; | |
753 | } | |
754 | } else { | |
755 | memset(&consumer->dst.net, 0, sizeof(consumer->dst.net)); | |
785d2d0d DG |
756 | } |
757 | ||
df75acac DG |
758 | consumer->type = CONSUMER_DST_NET; |
759 | ||
2f77fc4b DG |
760 | /* Set URI into consumer output object */ |
761 | ret = consumer_set_network_uri(consumer, uri); | |
762 | if (ret < 0) { | |
a74934ba | 763 | ret = -ret; |
2f77fc4b DG |
764 | goto error; |
765 | } else if (ret == 1) { | |
766 | /* | |
767 | * URI was the same in the consumer so we do not append the subdir | |
768 | * again so to not duplicate output dir. | |
769 | */ | |
f86c9f4e | 770 | ret = LTTNG_OK; |
2f77fc4b DG |
771 | goto error; |
772 | } | |
773 | ||
774 | if (uri->stype == LTTNG_STREAM_CONTROL && strlen(uri->subdir) == 0) { | |
775 | ret = consumer_set_subdir(consumer, session_name); | |
776 | if (ret < 0) { | |
f73fabfd | 777 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
778 | goto error; |
779 | } | |
780 | } | |
781 | ||
782 | if (uri->stype == LTTNG_STREAM_CONTROL) { | |
783 | /* On a new subdir, reappend the default trace dir. */ | |
c30ce0b3 CB |
784 | strncat(consumer->subdir, default_trace_dir, |
785 | sizeof(consumer->subdir) - strlen(consumer->subdir) - 1); | |
2f77fc4b DG |
786 | DBG3("Append domain trace name to subdir %s", consumer->subdir); |
787 | } | |
788 | ||
789 | break; | |
790 | case LTTNG_DST_PATH: | |
791 | DBG2("Setting trace directory path from URI to %s", uri->dst.path); | |
366a9222 JD |
792 | memset(consumer->dst.session_root_path, 0, |
793 | sizeof(consumer->dst.session_root_path)); | |
9ac05d92 MD |
794 | /* Explicit length checks for strcpy and strcat. */ |
795 | if (strlen(uri->dst.path) + strlen(default_trace_dir) | |
366a9222 | 796 | >= sizeof(consumer->dst.session_root_path)) { |
9ac05d92 MD |
797 | ret = LTTNG_ERR_FATAL; |
798 | goto error; | |
799 | } | |
366a9222 | 800 | strcpy(consumer->dst.session_root_path, uri->dst.path); |
2f77fc4b | 801 | /* Append default trace dir */ |
366a9222 | 802 | strcat(consumer->dst.session_root_path, default_trace_dir); |
2f77fc4b DG |
803 | /* Flag consumer as local. */ |
804 | consumer->type = CONSUMER_DST_LOCAL; | |
805 | break; | |
806 | } | |
807 | ||
a74934ba DG |
808 | ret = LTTNG_OK; |
809 | ||
2f77fc4b DG |
810 | error: |
811 | return ret; | |
812 | } | |
813 | ||
814 | /* | |
815 | * Init tracing by creating trace directory and sending fds kernel consumer. | |
816 | */ | |
817 | static int init_kernel_tracing(struct ltt_kernel_session *session) | |
818 | { | |
819 | int ret = 0; | |
820 | struct lttng_ht_iter iter; | |
821 | struct consumer_socket *socket; | |
822 | ||
823 | assert(session); | |
824 | ||
e7fe706f DG |
825 | rcu_read_lock(); |
826 | ||
2f77fc4b DG |
827 | if (session->consumer_fds_sent == 0 && session->consumer != NULL) { |
828 | cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter, | |
829 | socket, node.node) { | |
2f77fc4b | 830 | pthread_mutex_lock(socket->lock); |
f50f23d9 | 831 | ret = kernel_consumer_send_session(socket, session); |
2f77fc4b DG |
832 | pthread_mutex_unlock(socket->lock); |
833 | if (ret < 0) { | |
f73fabfd | 834 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
2f77fc4b DG |
835 | goto error; |
836 | } | |
837 | } | |
838 | } | |
839 | ||
840 | error: | |
e7fe706f | 841 | rcu_read_unlock(); |
2f77fc4b DG |
842 | return ret; |
843 | } | |
844 | ||
845 | /* | |
846 | * Create a socket to the relayd using the URI. | |
847 | * | |
848 | * On success, the relayd_sock pointer is set to the created socket. | |
849 | * Else, it's stays untouched and a lttcomm error code is returned. | |
850 | */ | |
6151a90f | 851 | static int create_connect_relayd(struct lttng_uri *uri, |
b31610f2 JD |
852 | struct lttcomm_relayd_sock **relayd_sock, |
853 | struct consumer_output *consumer) | |
2f77fc4b DG |
854 | { |
855 | int ret; | |
6151a90f | 856 | struct lttcomm_relayd_sock *rsock; |
2f77fc4b | 857 | |
6151a90f JD |
858 | rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR, |
859 | RELAYD_VERSION_COMM_MINOR); | |
860 | if (!rsock) { | |
f73fabfd | 861 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
862 | goto error; |
863 | } | |
864 | ||
ffe60014 DG |
865 | /* |
866 | * Connect to relayd so we can proceed with a session creation. This call | |
867 | * can possibly block for an arbitrary amount of time to set the health | |
868 | * state to be in poll execution. | |
869 | */ | |
870 | health_poll_entry(); | |
6151a90f | 871 | ret = relayd_connect(rsock); |
ffe60014 | 872 | health_poll_exit(); |
2f77fc4b DG |
873 | if (ret < 0) { |
874 | ERR("Unable to reach lttng-relayd"); | |
f73fabfd | 875 | ret = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
2f77fc4b DG |
876 | goto free_sock; |
877 | } | |
878 | ||
879 | /* Create socket for control stream. */ | |
880 | if (uri->stype == LTTNG_STREAM_CONTROL) { | |
881 | DBG3("Creating relayd stream socket from URI"); | |
882 | ||
883 | /* Check relayd version */ | |
6151a90f | 884 | ret = relayd_version_check(rsock); |
67d5aa28 JD |
885 | if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) { |
886 | goto close_sock; | |
887 | } else if (ret < 0) { | |
888 | ERR("Unable to reach lttng-relayd"); | |
889 | ret = LTTNG_ERR_RELAYD_CONNECT_FAIL; | |
2f77fc4b DG |
890 | goto close_sock; |
891 | } | |
b31610f2 JD |
892 | consumer->relay_major_version = rsock->major; |
893 | consumer->relay_minor_version = rsock->minor; | |
2f77fc4b DG |
894 | } else if (uri->stype == LTTNG_STREAM_DATA) { |
895 | DBG3("Creating relayd data socket from URI"); | |
896 | } else { | |
897 | /* Command is not valid */ | |
898 | ERR("Relayd invalid stream type: %d", uri->stype); | |
f73fabfd | 899 | ret = LTTNG_ERR_INVALID; |
2f77fc4b DG |
900 | goto close_sock; |
901 | } | |
902 | ||
6151a90f | 903 | *relayd_sock = rsock; |
2f77fc4b | 904 | |
f73fabfd | 905 | return LTTNG_OK; |
2f77fc4b DG |
906 | |
907 | close_sock: | |
6151a90f JD |
908 | /* The returned value is not useful since we are on an error path. */ |
909 | (void) relayd_close(rsock); | |
2f77fc4b | 910 | free_sock: |
6151a90f | 911 | free(rsock); |
2f77fc4b DG |
912 | error: |
913 | return ret; | |
914 | } | |
915 | ||
916 | /* | |
917 | * Connect to the relayd using URI and send the socket to the right consumer. | |
43fade62 JG |
918 | * |
919 | * The consumer socket lock must be held by the caller. | |
2f77fc4b | 920 | */ |
56a37563 JG |
921 | static int send_consumer_relayd_socket(enum lttng_domain_type domain, |
922 | unsigned int session_id, struct lttng_uri *relayd_uri, | |
923 | struct consumer_output *consumer, | |
d3e2ba59 JD |
924 | struct consumer_socket *consumer_sock, |
925 | char *session_name, char *hostname, int session_live_timer) | |
2f77fc4b DG |
926 | { |
927 | int ret; | |
6151a90f | 928 | struct lttcomm_relayd_sock *rsock = NULL; |
2f77fc4b | 929 | |
ffe60014 | 930 | /* Connect to relayd and make version check if uri is the control. */ |
b31610f2 | 931 | ret = create_connect_relayd(relayd_uri, &rsock, consumer); |
ffe60014 | 932 | if (ret != LTTNG_OK) { |
9e218353 | 933 | goto relayd_comm_error; |
ffe60014 | 934 | } |
6151a90f | 935 | assert(rsock); |
ffe60014 | 936 | |
2f77fc4b | 937 | /* Set the network sequence index if not set. */ |
d88aee68 DG |
938 | if (consumer->net_seq_index == (uint64_t) -1ULL) { |
939 | pthread_mutex_lock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
940 | /* |
941 | * Increment net_seq_idx because we are about to transfer the | |
942 | * new relayd socket to the consumer. | |
d88aee68 | 943 | * Assign unique key so the consumer can match streams. |
2f77fc4b | 944 | */ |
d88aee68 DG |
945 | consumer->net_seq_index = ++relayd_net_seq_idx; |
946 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
947 | } |
948 | ||
2f77fc4b | 949 | /* Send relayd socket to consumer. */ |
6151a90f | 950 | ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer, |
d3e2ba59 JD |
951 | relayd_uri->stype, session_id, |
952 | session_name, hostname, session_live_timer); | |
2f77fc4b | 953 | if (ret < 0) { |
f73fabfd | 954 | ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL; |
2f77fc4b DG |
955 | goto close_sock; |
956 | } | |
957 | ||
c890b720 DG |
958 | /* Flag that the corresponding socket was sent. */ |
959 | if (relayd_uri->stype == LTTNG_STREAM_CONTROL) { | |
ffe60014 | 960 | consumer_sock->control_sock_sent = 1; |
c890b720 | 961 | } else if (relayd_uri->stype == LTTNG_STREAM_DATA) { |
ffe60014 | 962 | consumer_sock->data_sock_sent = 1; |
c890b720 DG |
963 | } |
964 | ||
f73fabfd | 965 | ret = LTTNG_OK; |
2f77fc4b DG |
966 | |
967 | /* | |
968 | * Close socket which was dup on the consumer side. The session daemon does | |
969 | * NOT keep track of the relayd socket(s) once transfer to the consumer. | |
970 | */ | |
971 | ||
972 | close_sock: | |
ffe60014 DG |
973 | if (ret != LTTNG_OK) { |
974 | /* | |
d9078d0c DG |
975 | * The consumer output for this session should not be used anymore |
976 | * since the relayd connection failed thus making any tracing or/and | |
977 | * streaming not usable. | |
ffe60014 | 978 | */ |
d9078d0c | 979 | consumer->enabled = 0; |
ffe60014 | 980 | } |
9e218353 JR |
981 | (void) relayd_close(rsock); |
982 | free(rsock); | |
983 | ||
984 | relayd_comm_error: | |
2f77fc4b DG |
985 | return ret; |
986 | } | |
987 | ||
988 | /* | |
989 | * Send both relayd sockets to a specific consumer and domain. This is a | |
990 | * helper function to facilitate sending the information to the consumer for a | |
991 | * session. | |
43fade62 JG |
992 | * |
993 | * The consumer socket lock must be held by the caller. | |
2f77fc4b | 994 | */ |
56a37563 JG |
995 | static int send_consumer_relayd_sockets(enum lttng_domain_type domain, |
996 | unsigned int session_id, struct consumer_output *consumer, | |
997 | struct consumer_socket *sock, char *session_name, | |
998 | char *hostname, int session_live_timer) | |
2f77fc4b | 999 | { |
dda67f6c | 1000 | int ret = LTTNG_OK; |
2f77fc4b | 1001 | |
2f77fc4b | 1002 | assert(consumer); |
6dc3064a | 1003 | assert(sock); |
2f77fc4b | 1004 | |
2f77fc4b | 1005 | /* Sending control relayd socket. */ |
ffe60014 | 1006 | if (!sock->control_sock_sent) { |
6dc3064a | 1007 | ret = send_consumer_relayd_socket(domain, session_id, |
d3e2ba59 JD |
1008 | &consumer->dst.net.control, consumer, sock, |
1009 | session_name, hostname, session_live_timer); | |
c890b720 DG |
1010 | if (ret != LTTNG_OK) { |
1011 | goto error; | |
1012 | } | |
2f77fc4b DG |
1013 | } |
1014 | ||
1015 | /* Sending data relayd socket. */ | |
ffe60014 | 1016 | if (!sock->data_sock_sent) { |
6dc3064a | 1017 | ret = send_consumer_relayd_socket(domain, session_id, |
d3e2ba59 JD |
1018 | &consumer->dst.net.data, consumer, sock, |
1019 | session_name, hostname, session_live_timer); | |
c890b720 DG |
1020 | if (ret != LTTNG_OK) { |
1021 | goto error; | |
1022 | } | |
2f77fc4b DG |
1023 | } |
1024 | ||
2f77fc4b DG |
1025 | error: |
1026 | return ret; | |
1027 | } | |
1028 | ||
1029 | /* | |
1030 | * Setup relayd connections for a tracing session. First creates the socket to | |
1031 | * the relayd and send them to the right domain consumer. Consumer type MUST be | |
1032 | * network. | |
1033 | */ | |
ffe60014 | 1034 | int cmd_setup_relayd(struct ltt_session *session) |
2f77fc4b | 1035 | { |
f73fabfd | 1036 | int ret = LTTNG_OK; |
2f77fc4b DG |
1037 | struct ltt_ust_session *usess; |
1038 | struct ltt_kernel_session *ksess; | |
1039 | struct consumer_socket *socket; | |
1040 | struct lttng_ht_iter iter; | |
1041 | ||
1042 | assert(session); | |
1043 | ||
1044 | usess = session->ust_session; | |
1045 | ksess = session->kernel_session; | |
1046 | ||
785d2d0d | 1047 | DBG("Setting relayd for session %s", session->name); |
2f77fc4b | 1048 | |
e7fe706f DG |
1049 | rcu_read_lock(); |
1050 | ||
2f77fc4b DG |
1051 | if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET |
1052 | && usess->consumer->enabled) { | |
1053 | /* For each consumer socket, send relayd sockets */ | |
1054 | cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter, | |
1055 | socket, node.node) { | |
2f77fc4b | 1056 | pthread_mutex_lock(socket->lock); |
6dc3064a | 1057 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id, |
d3e2ba59 JD |
1058 | usess->consumer, socket, |
1059 | session->name, session->hostname, | |
1060 | session->live_timer); | |
2f77fc4b | 1061 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 1062 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1063 | goto error; |
1064 | } | |
6dc3064a DG |
1065 | /* Session is now ready for network streaming. */ |
1066 | session->net_handle = 1; | |
2f77fc4b | 1067 | } |
b31610f2 JD |
1068 | session->consumer->relay_major_version = |
1069 | usess->consumer->relay_major_version; | |
1070 | session->consumer->relay_minor_version = | |
1071 | usess->consumer->relay_minor_version; | |
2f77fc4b DG |
1072 | } |
1073 | ||
1074 | if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET | |
1075 | && ksess->consumer->enabled) { | |
1076 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, | |
1077 | socket, node.node) { | |
2f77fc4b | 1078 | pthread_mutex_lock(socket->lock); |
6dc3064a | 1079 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id, |
d3e2ba59 JD |
1080 | ksess->consumer, socket, |
1081 | session->name, session->hostname, | |
1082 | session->live_timer); | |
2f77fc4b | 1083 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 1084 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1085 | goto error; |
1086 | } | |
6dc3064a DG |
1087 | /* Session is now ready for network streaming. */ |
1088 | session->net_handle = 1; | |
2f77fc4b | 1089 | } |
b31610f2 JD |
1090 | session->consumer->relay_major_version = |
1091 | ksess->consumer->relay_major_version; | |
1092 | session->consumer->relay_minor_version = | |
1093 | ksess->consumer->relay_minor_version; | |
2f77fc4b DG |
1094 | } |
1095 | ||
1096 | error: | |
e7fe706f | 1097 | rcu_read_unlock(); |
2f77fc4b DG |
1098 | return ret; |
1099 | } | |
1100 | ||
9b6c7ec5 DG |
1101 | /* |
1102 | * Start a kernel session by opening all necessary streams. | |
1103 | */ | |
1104 | static int start_kernel_session(struct ltt_kernel_session *ksess, int wpipe) | |
1105 | { | |
1106 | int ret; | |
1107 | struct ltt_kernel_channel *kchan; | |
1108 | ||
1109 | /* Open kernel metadata */ | |
07b86b52 | 1110 | if (ksess->metadata == NULL && ksess->output_traces) { |
9b6c7ec5 DG |
1111 | ret = kernel_open_metadata(ksess); |
1112 | if (ret < 0) { | |
1113 | ret = LTTNG_ERR_KERN_META_FAIL; | |
1114 | goto error; | |
1115 | } | |
1116 | } | |
1117 | ||
1118 | /* Open kernel metadata stream */ | |
07b86b52 | 1119 | if (ksess->metadata && ksess->metadata_stream_fd < 0) { |
9b6c7ec5 DG |
1120 | ret = kernel_open_metadata_stream(ksess); |
1121 | if (ret < 0) { | |
1122 | ERR("Kernel create metadata stream failed"); | |
1123 | ret = LTTNG_ERR_KERN_STREAM_FAIL; | |
1124 | goto error; | |
1125 | } | |
1126 | } | |
1127 | ||
1128 | /* For each channel */ | |
1129 | cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) { | |
1130 | if (kchan->stream_count == 0) { | |
1131 | ret = kernel_open_channel_stream(kchan); | |
1132 | if (ret < 0) { | |
1133 | ret = LTTNG_ERR_KERN_STREAM_FAIL; | |
1134 | goto error; | |
1135 | } | |
1136 | /* Update the stream global counter */ | |
1137 | ksess->stream_count_global += ret; | |
1138 | } | |
1139 | } | |
1140 | ||
1141 | /* Setup kernel consumer socket and send fds to it */ | |
1142 | ret = init_kernel_tracing(ksess); | |
e43c41c5 | 1143 | if (ret != 0) { |
9b6c7ec5 DG |
1144 | ret = LTTNG_ERR_KERN_START_FAIL; |
1145 | goto error; | |
1146 | } | |
1147 | ||
1148 | /* This start the kernel tracing */ | |
1149 | ret = kernel_start_session(ksess); | |
1150 | if (ret < 0) { | |
1151 | ret = LTTNG_ERR_KERN_START_FAIL; | |
1152 | goto error; | |
1153 | } | |
1154 | ||
1155 | /* Quiescent wait after starting trace */ | |
784303b0 | 1156 | kernel_wait_quiescent(kernel_tracer_fd); |
9b6c7ec5 | 1157 | |
14fb1ebe | 1158 | ksess->active = 1; |
9b6c7ec5 DG |
1159 | |
1160 | ret = LTTNG_OK; | |
1161 | ||
1162 | error: | |
1163 | return ret; | |
1164 | } | |
1165 | ||
2f77fc4b DG |
1166 | /* |
1167 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. | |
1168 | */ | |
56a37563 JG |
1169 | int cmd_disable_channel(struct ltt_session *session, |
1170 | enum lttng_domain_type domain, char *channel_name) | |
2f77fc4b DG |
1171 | { |
1172 | int ret; | |
1173 | struct ltt_ust_session *usess; | |
1174 | ||
1175 | usess = session->ust_session; | |
1176 | ||
2223c96f DG |
1177 | rcu_read_lock(); |
1178 | ||
2f77fc4b DG |
1179 | switch (domain) { |
1180 | case LTTNG_DOMAIN_KERNEL: | |
1181 | { | |
1182 | ret = channel_kernel_disable(session->kernel_session, | |
1183 | channel_name); | |
f73fabfd | 1184 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1185 | goto error; |
1186 | } | |
1187 | ||
1188 | kernel_wait_quiescent(kernel_tracer_fd); | |
1189 | break; | |
1190 | } | |
1191 | case LTTNG_DOMAIN_UST: | |
1192 | { | |
1193 | struct ltt_ust_channel *uchan; | |
1194 | struct lttng_ht *chan_ht; | |
1195 | ||
1196 | chan_ht = usess->domain_global.channels; | |
1197 | ||
1198 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); | |
1199 | if (uchan == NULL) { | |
f73fabfd | 1200 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2f77fc4b DG |
1201 | goto error; |
1202 | } | |
1203 | ||
7972aab2 | 1204 | ret = channel_ust_disable(usess, uchan); |
f73fabfd | 1205 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1206 | goto error; |
1207 | } | |
1208 | break; | |
1209 | } | |
2f77fc4b | 1210 | default: |
f73fabfd | 1211 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
1212 | goto error; |
1213 | } | |
1214 | ||
f73fabfd | 1215 | ret = LTTNG_OK; |
2f77fc4b DG |
1216 | |
1217 | error: | |
2223c96f | 1218 | rcu_read_unlock(); |
2f77fc4b DG |
1219 | return ret; |
1220 | } | |
1221 | ||
ccf10263 MD |
1222 | /* |
1223 | * Command LTTNG_TRACK_PID processed by the client thread. | |
a9ad0c8f MD |
1224 | * |
1225 | * Called with session lock held. | |
ccf10263 | 1226 | */ |
56a37563 JG |
1227 | int cmd_track_pid(struct ltt_session *session, enum lttng_domain_type domain, |
1228 | int pid) | |
ccf10263 MD |
1229 | { |
1230 | int ret; | |
1231 | ||
1232 | rcu_read_lock(); | |
1233 | ||
1234 | switch (domain) { | |
1235 | case LTTNG_DOMAIN_KERNEL: | |
1236 | { | |
1237 | struct ltt_kernel_session *ksess; | |
1238 | ||
1239 | ksess = session->kernel_session; | |
1240 | ||
1241 | ret = kernel_track_pid(ksess, pid); | |
1242 | if (ret != LTTNG_OK) { | |
1243 | goto error; | |
1244 | } | |
1245 | ||
1246 | kernel_wait_quiescent(kernel_tracer_fd); | |
1247 | break; | |
1248 | } | |
ccf10263 | 1249 | case LTTNG_DOMAIN_UST: |
a9ad0c8f MD |
1250 | { |
1251 | struct ltt_ust_session *usess; | |
1252 | ||
1253 | usess = session->ust_session; | |
1254 | ||
1255 | ret = trace_ust_track_pid(usess, pid); | |
1256 | if (ret != LTTNG_OK) { | |
1257 | goto error; | |
1258 | } | |
1259 | break; | |
1260 | } | |
ccf10263 MD |
1261 | default: |
1262 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
1263 | goto error; | |
1264 | } | |
1265 | ||
1266 | ret = LTTNG_OK; | |
1267 | ||
1268 | error: | |
1269 | rcu_read_unlock(); | |
1270 | return ret; | |
1271 | } | |
1272 | ||
1273 | /* | |
1274 | * Command LTTNG_UNTRACK_PID processed by the client thread. | |
a9ad0c8f MD |
1275 | * |
1276 | * Called with session lock held. | |
ccf10263 | 1277 | */ |
56a37563 JG |
1278 | int cmd_untrack_pid(struct ltt_session *session, enum lttng_domain_type domain, |
1279 | int pid) | |
ccf10263 MD |
1280 | { |
1281 | int ret; | |
1282 | ||
1283 | rcu_read_lock(); | |
1284 | ||
1285 | switch (domain) { | |
1286 | case LTTNG_DOMAIN_KERNEL: | |
1287 | { | |
1288 | struct ltt_kernel_session *ksess; | |
1289 | ||
1290 | ksess = session->kernel_session; | |
1291 | ||
1292 | ret = kernel_untrack_pid(ksess, pid); | |
1293 | if (ret != LTTNG_OK) { | |
1294 | goto error; | |
1295 | } | |
1296 | ||
1297 | kernel_wait_quiescent(kernel_tracer_fd); | |
1298 | break; | |
1299 | } | |
ccf10263 | 1300 | case LTTNG_DOMAIN_UST: |
a9ad0c8f MD |
1301 | { |
1302 | struct ltt_ust_session *usess; | |
1303 | ||
1304 | usess = session->ust_session; | |
1305 | ||
1306 | ret = trace_ust_untrack_pid(usess, pid); | |
1307 | if (ret != LTTNG_OK) { | |
1308 | goto error; | |
1309 | } | |
1310 | break; | |
1311 | } | |
ccf10263 MD |
1312 | default: |
1313 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
1314 | goto error; | |
1315 | } | |
1316 | ||
1317 | ret = LTTNG_OK; | |
1318 | ||
1319 | error: | |
1320 | rcu_read_unlock(); | |
1321 | return ret; | |
1322 | } | |
1323 | ||
2f77fc4b DG |
1324 | /* |
1325 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. | |
1326 | * | |
1327 | * The wpipe arguments is used as a notifier for the kernel thread. | |
1328 | */ | |
1329 | int cmd_enable_channel(struct ltt_session *session, | |
7972aab2 | 1330 | struct lttng_domain *domain, struct lttng_channel *attr, int wpipe) |
2f77fc4b DG |
1331 | { |
1332 | int ret; | |
1333 | struct ltt_ust_session *usess = session->ust_session; | |
1334 | struct lttng_ht *chan_ht; | |
1f345e94 | 1335 | size_t len; |
2f77fc4b DG |
1336 | |
1337 | assert(session); | |
1338 | assert(attr); | |
7972aab2 | 1339 | assert(domain); |
2f77fc4b | 1340 | |
f5436bfc | 1341 | len = lttng_strnlen(attr->name, sizeof(attr->name)); |
1f345e94 PP |
1342 | |
1343 | /* Validate channel name */ | |
1344 | if (attr->name[0] == '.' || | |
1345 | memchr(attr->name, '/', len) != NULL) { | |
1346 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1347 | goto end; | |
1348 | } | |
1349 | ||
2f77fc4b DG |
1350 | DBG("Enabling channel %s for session %s", attr->name, session->name); |
1351 | ||
03b4fdcf DG |
1352 | rcu_read_lock(); |
1353 | ||
ecc48a90 JD |
1354 | /* |
1355 | * Don't try to enable a channel if the session has been started at | |
1356 | * some point in time before. The tracer does not allow it. | |
1357 | */ | |
8382cf6f | 1358 | if (session->has_been_started) { |
ecc48a90 JD |
1359 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
1360 | goto error; | |
1361 | } | |
1362 | ||
1363 | /* | |
1364 | * If the session is a live session, remove the switch timer, the | |
1365 | * live timer does the same thing but sends also synchronisation | |
1366 | * beacons for inactive streams. | |
1367 | */ | |
1368 | if (session->live_timer > 0) { | |
1369 | attr->attr.live_timer_interval = session->live_timer; | |
1370 | attr->attr.switch_timer_interval = 0; | |
1371 | } | |
1372 | ||
6e21424e JR |
1373 | /* Check for feature support */ |
1374 | switch (domain->type) { | |
1375 | case LTTNG_DOMAIN_KERNEL: | |
1376 | { | |
1377 | if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd) != 1) { | |
1378 | /* Sampling position of buffer is not supported */ | |
1379 | WARN("Kernel tracer does not support buffer monitoring. " | |
1380 | "Setting the monitor interval timer to 0 " | |
1381 | "(disabled) for channel '%s' of session '%s'", | |
1382 | attr-> name, session->name); | |
1383 | lttng_channel_set_monitor_timer_interval(attr, 0); | |
1384 | } | |
1385 | break; | |
1386 | } | |
1387 | case LTTNG_DOMAIN_UST: | |
f28f9e44 | 1388 | break; |
6e21424e JR |
1389 | case LTTNG_DOMAIN_JUL: |
1390 | case LTTNG_DOMAIN_LOG4J: | |
1391 | case LTTNG_DOMAIN_PYTHON: | |
f28f9e44 JG |
1392 | if (!agent_tracing_is_enabled()) { |
1393 | DBG("Attempted to enable a channel in an agent domain but the agent thread is not running"); | |
1394 | ret = LTTNG_ERR_AGENT_TRACING_DISABLED; | |
1395 | goto error; | |
1396 | } | |
6e21424e JR |
1397 | break; |
1398 | default: | |
1399 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
1400 | goto error; | |
1401 | } | |
1402 | ||
7972aab2 | 1403 | switch (domain->type) { |
2f77fc4b DG |
1404 | case LTTNG_DOMAIN_KERNEL: |
1405 | { | |
1406 | struct ltt_kernel_channel *kchan; | |
1407 | ||
2f77fc4b DG |
1408 | kchan = trace_kernel_get_channel_by_name(attr->name, |
1409 | session->kernel_session); | |
1410 | if (kchan == NULL) { | |
1411 | ret = channel_kernel_create(session->kernel_session, attr, wpipe); | |
85076754 MD |
1412 | if (attr->name[0] != '\0') { |
1413 | session->kernel_session->has_non_default_channel = 1; | |
1414 | } | |
2f77fc4b DG |
1415 | } else { |
1416 | ret = channel_kernel_enable(session->kernel_session, kchan); | |
1417 | } | |
1418 | ||
f73fabfd | 1419 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1420 | goto error; |
1421 | } | |
1422 | ||
784303b0 | 1423 | kernel_wait_quiescent(kernel_tracer_fd); |
2f77fc4b DG |
1424 | break; |
1425 | } | |
1426 | case LTTNG_DOMAIN_UST: | |
9232818f JG |
1427 | case LTTNG_DOMAIN_JUL: |
1428 | case LTTNG_DOMAIN_LOG4J: | |
1429 | case LTTNG_DOMAIN_PYTHON: | |
2f77fc4b DG |
1430 | { |
1431 | struct ltt_ust_channel *uchan; | |
1432 | ||
9232818f JG |
1433 | /* |
1434 | * FIXME | |
1435 | * | |
1436 | * Current agent implementation limitations force us to allow | |
1437 | * only one channel at once in "agent" subdomains. Each | |
1438 | * subdomain has a default channel name which must be strictly | |
1439 | * adhered to. | |
1440 | */ | |
1441 | if (domain->type == LTTNG_DOMAIN_JUL) { | |
1442 | if (strncmp(attr->name, DEFAULT_JUL_CHANNEL_NAME, | |
1443 | LTTNG_SYMBOL_NAME_LEN)) { | |
1444 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1445 | goto error; | |
1446 | } | |
1447 | } else if (domain->type == LTTNG_DOMAIN_LOG4J) { | |
1448 | if (strncmp(attr->name, DEFAULT_LOG4J_CHANNEL_NAME, | |
1449 | LTTNG_SYMBOL_NAME_LEN)) { | |
1450 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1451 | goto error; | |
1452 | } | |
1453 | } else if (domain->type == LTTNG_DOMAIN_PYTHON) { | |
1454 | if (strncmp(attr->name, DEFAULT_PYTHON_CHANNEL_NAME, | |
1455 | LTTNG_SYMBOL_NAME_LEN)) { | |
1456 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1457 | goto error; | |
1458 | } | |
1459 | } | |
1460 | ||
2f77fc4b DG |
1461 | chan_ht = usess->domain_global.channels; |
1462 | ||
1463 | uchan = trace_ust_find_channel_by_name(chan_ht, attr->name); | |
1464 | if (uchan == NULL) { | |
7972aab2 | 1465 | ret = channel_ust_create(usess, attr, domain->buf_type); |
85076754 MD |
1466 | if (attr->name[0] != '\0') { |
1467 | usess->has_non_default_channel = 1; | |
1468 | } | |
2f77fc4b | 1469 | } else { |
7972aab2 | 1470 | ret = channel_ust_enable(usess, uchan); |
2f77fc4b DG |
1471 | } |
1472 | break; | |
1473 | } | |
2f77fc4b | 1474 | default: |
f73fabfd | 1475 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
1476 | goto error; |
1477 | } | |
1478 | ||
1479 | error: | |
2223c96f | 1480 | rcu_read_unlock(); |
1f345e94 | 1481 | end: |
2f77fc4b DG |
1482 | return ret; |
1483 | } | |
1484 | ||
2f77fc4b DG |
1485 | /* |
1486 | * Command LTTNG_DISABLE_EVENT processed by the client thread. | |
1487 | */ | |
56a37563 JG |
1488 | int cmd_disable_event(struct ltt_session *session, |
1489 | enum lttng_domain_type domain, char *channel_name, | |
6e911cad | 1490 | struct lttng_event *event) |
2f77fc4b DG |
1491 | { |
1492 | int ret; | |
6e911cad MD |
1493 | char *event_name; |
1494 | ||
18a720cd MD |
1495 | DBG("Disable event command for event \'%s\'", event->name); |
1496 | ||
6e911cad MD |
1497 | event_name = event->name; |
1498 | ||
9b7431cf JG |
1499 | /* Error out on unhandled search criteria */ |
1500 | if (event->loglevel_type || event->loglevel != -1 || event->enabled | |
6e911cad | 1501 | || event->pid || event->filter || event->exclusion) { |
7076b56e JG |
1502 | ret = LTTNG_ERR_UNK; |
1503 | goto error; | |
6e911cad | 1504 | } |
2f77fc4b | 1505 | |
2223c96f DG |
1506 | rcu_read_lock(); |
1507 | ||
2f77fc4b DG |
1508 | switch (domain) { |
1509 | case LTTNG_DOMAIN_KERNEL: | |
1510 | { | |
1511 | struct ltt_kernel_channel *kchan; | |
1512 | struct ltt_kernel_session *ksess; | |
1513 | ||
1514 | ksess = session->kernel_session; | |
1515 | ||
85076754 MD |
1516 | /* |
1517 | * If a non-default channel has been created in the | |
1518 | * session, explicitely require that -c chan_name needs | |
1519 | * to be provided. | |
1520 | */ | |
1521 | if (ksess->has_non_default_channel && channel_name[0] == '\0') { | |
1522 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
7076b56e | 1523 | goto error_unlock; |
85076754 MD |
1524 | } |
1525 | ||
2f77fc4b DG |
1526 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); |
1527 | if (kchan == NULL) { | |
f73fabfd | 1528 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
7076b56e | 1529 | goto error_unlock; |
2f77fc4b DG |
1530 | } |
1531 | ||
6e911cad MD |
1532 | switch (event->type) { |
1533 | case LTTNG_EVENT_ALL: | |
9550ee81 | 1534 | case LTTNG_EVENT_TRACEPOINT: |
d0ae4ea8 | 1535 | case LTTNG_EVENT_SYSCALL: |
9550ee81 JR |
1536 | case LTTNG_EVENT_PROBE: |
1537 | case LTTNG_EVENT_FUNCTION: | |
1538 | case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */ | |
1539 | if (event_name[0] == '\0') { | |
1540 | ret = event_kernel_disable_event(kchan, | |
1541 | NULL, event->type); | |
29c62722 | 1542 | } else { |
d0ae4ea8 | 1543 | ret = event_kernel_disable_event(kchan, |
9550ee81 | 1544 | event_name, event->type); |
29c62722 | 1545 | } |
6e911cad | 1546 | if (ret != LTTNG_OK) { |
7076b56e | 1547 | goto error_unlock; |
6e911cad MD |
1548 | } |
1549 | break; | |
6e911cad MD |
1550 | default: |
1551 | ret = LTTNG_ERR_UNK; | |
7076b56e | 1552 | goto error_unlock; |
2f77fc4b DG |
1553 | } |
1554 | ||
1555 | kernel_wait_quiescent(kernel_tracer_fd); | |
1556 | break; | |
1557 | } | |
1558 | case LTTNG_DOMAIN_UST: | |
1559 | { | |
1560 | struct ltt_ust_channel *uchan; | |
1561 | struct ltt_ust_session *usess; | |
1562 | ||
1563 | usess = session->ust_session; | |
1564 | ||
7076b56e JG |
1565 | if (validate_ust_event_name(event_name)) { |
1566 | ret = LTTNG_ERR_INVALID_EVENT_NAME; | |
1567 | goto error_unlock; | |
1568 | } | |
1569 | ||
85076754 MD |
1570 | /* |
1571 | * If a non-default channel has been created in the | |
9550ee81 | 1572 | * session, explicitly require that -c chan_name needs |
85076754 MD |
1573 | * to be provided. |
1574 | */ | |
1575 | if (usess->has_non_default_channel && channel_name[0] == '\0') { | |
1576 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
7076b56e | 1577 | goto error_unlock; |
85076754 MD |
1578 | } |
1579 | ||
2f77fc4b DG |
1580 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
1581 | channel_name); | |
1582 | if (uchan == NULL) { | |
f73fabfd | 1583 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
7076b56e | 1584 | goto error_unlock; |
2f77fc4b DG |
1585 | } |
1586 | ||
6e911cad MD |
1587 | switch (event->type) { |
1588 | case LTTNG_EVENT_ALL: | |
b3639870 JR |
1589 | /* |
1590 | * An empty event name means that everything | |
1591 | * should be disabled. | |
1592 | */ | |
1593 | if (event->name[0] == '\0') { | |
1594 | ret = event_ust_disable_all_tracepoints(usess, uchan); | |
77d536b2 JR |
1595 | } else { |
1596 | ret = event_ust_disable_tracepoint(usess, uchan, | |
1597 | event_name); | |
1598 | } | |
6e911cad | 1599 | if (ret != LTTNG_OK) { |
7076b56e | 1600 | goto error_unlock; |
6e911cad MD |
1601 | } |
1602 | break; | |
1603 | default: | |
1604 | ret = LTTNG_ERR_UNK; | |
7076b56e | 1605 | goto error_unlock; |
2f77fc4b DG |
1606 | } |
1607 | ||
1608 | DBG3("Disable UST event %s in channel %s completed", event_name, | |
1609 | channel_name); | |
1610 | break; | |
1611 | } | |
5cdb6027 | 1612 | case LTTNG_DOMAIN_LOG4J: |
f20baf8e | 1613 | case LTTNG_DOMAIN_JUL: |
0e115563 | 1614 | case LTTNG_DOMAIN_PYTHON: |
f20baf8e | 1615 | { |
fefd409b | 1616 | struct agent *agt; |
f20baf8e DG |
1617 | struct ltt_ust_session *usess = session->ust_session; |
1618 | ||
1619 | assert(usess); | |
1620 | ||
6e911cad MD |
1621 | switch (event->type) { |
1622 | case LTTNG_EVENT_ALL: | |
1623 | break; | |
1624 | default: | |
1625 | ret = LTTNG_ERR_UNK; | |
7076b56e | 1626 | goto error_unlock; |
6e911cad MD |
1627 | } |
1628 | ||
5cdb6027 | 1629 | agt = trace_ust_find_agent(usess, domain); |
fefd409b DG |
1630 | if (!agt) { |
1631 | ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND; | |
7076b56e | 1632 | goto error_unlock; |
fefd409b | 1633 | } |
b3639870 JR |
1634 | /* |
1635 | * An empty event name means that everything | |
1636 | * should be disabled. | |
1637 | */ | |
1638 | if (event->name[0] == '\0') { | |
18a720cd MD |
1639 | ret = event_agent_disable_all(usess, agt); |
1640 | } else { | |
1641 | ret = event_agent_disable(usess, agt, event_name); | |
1642 | } | |
f20baf8e | 1643 | if (ret != LTTNG_OK) { |
7076b56e | 1644 | goto error_unlock; |
f20baf8e DG |
1645 | } |
1646 | ||
1647 | break; | |
1648 | } | |
2f77fc4b | 1649 | default: |
f73fabfd | 1650 | ret = LTTNG_ERR_UND; |
7076b56e | 1651 | goto error_unlock; |
2f77fc4b DG |
1652 | } |
1653 | ||
f73fabfd | 1654 | ret = LTTNG_OK; |
2f77fc4b | 1655 | |
7076b56e | 1656 | error_unlock: |
2223c96f | 1657 | rcu_read_unlock(); |
7076b56e | 1658 | error: |
2f77fc4b DG |
1659 | return ret; |
1660 | } | |
1661 | ||
2f77fc4b DG |
1662 | /* |
1663 | * Command LTTNG_ADD_CONTEXT processed by the client thread. | |
1664 | */ | |
56a37563 | 1665 | int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain, |
601d5acf | 1666 | char *channel_name, struct lttng_event_context *ctx, int kwpipe) |
2f77fc4b | 1667 | { |
d5979e4a | 1668 | int ret, chan_kern_created = 0, chan_ust_created = 0; |
bdf64013 JG |
1669 | char *app_ctx_provider_name = NULL, *app_ctx_name = NULL; |
1670 | ||
9a699f7b JR |
1671 | /* |
1672 | * Don't try to add a context if the session has been started at | |
1673 | * some point in time before. The tracer does not allow it and would | |
1674 | * result in a corrupted trace. | |
1675 | */ | |
1676 | if (session->has_been_started) { | |
1677 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; | |
1678 | goto end; | |
1679 | } | |
1680 | ||
bdf64013 JG |
1681 | if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { |
1682 | app_ctx_provider_name = ctx->u.app_ctx.provider_name; | |
1683 | app_ctx_name = ctx->u.app_ctx.ctx_name; | |
1684 | } | |
2f77fc4b DG |
1685 | |
1686 | switch (domain) { | |
1687 | case LTTNG_DOMAIN_KERNEL: | |
979e618e DG |
1688 | assert(session->kernel_session); |
1689 | ||
1690 | if (session->kernel_session->channel_count == 0) { | |
1691 | /* Create default channel */ | |
1692 | ret = channel_kernel_create(session->kernel_session, NULL, kwpipe); | |
1693 | if (ret != LTTNG_OK) { | |
1694 | goto error; | |
1695 | } | |
d5979e4a | 1696 | chan_kern_created = 1; |
979e618e | 1697 | } |
2f77fc4b | 1698 | /* Add kernel context to kernel tracer */ |
601d5acf | 1699 | ret = context_kernel_add(session->kernel_session, ctx, channel_name); |
f73fabfd | 1700 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1701 | goto error; |
1702 | } | |
1703 | break; | |
bdf64013 JG |
1704 | case LTTNG_DOMAIN_JUL: |
1705 | case LTTNG_DOMAIN_LOG4J: | |
1706 | { | |
1707 | /* | |
1708 | * Validate channel name. | |
1709 | * If no channel name is given and the domain is JUL or LOG4J, | |
1710 | * set it to the appropriate domain-specific channel name. If | |
1711 | * a name is provided but does not match the expexted channel | |
1712 | * name, return an error. | |
1713 | */ | |
1714 | if (domain == LTTNG_DOMAIN_JUL && *channel_name && | |
1715 | strcmp(channel_name, | |
1716 | DEFAULT_JUL_CHANNEL_NAME)) { | |
1717 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; | |
1718 | goto error; | |
1719 | } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name && | |
1720 | strcmp(channel_name, | |
1721 | DEFAULT_LOG4J_CHANNEL_NAME)) { | |
1722 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; | |
1723 | goto error; | |
1724 | } | |
a93b3916 | 1725 | /* break is _not_ missing here. */ |
bdf64013 | 1726 | } |
2f77fc4b DG |
1727 | case LTTNG_DOMAIN_UST: |
1728 | { | |
1729 | struct ltt_ust_session *usess = session->ust_session; | |
85076754 MD |
1730 | unsigned int chan_count; |
1731 | ||
2f77fc4b DG |
1732 | assert(usess); |
1733 | ||
85076754 | 1734 | chan_count = lttng_ht_get_count(usess->domain_global.channels); |
979e618e DG |
1735 | if (chan_count == 0) { |
1736 | struct lttng_channel *attr; | |
1737 | /* Create default channel */ | |
0a9c6494 | 1738 | attr = channel_new_default_attr(domain, usess->buffer_type); |
979e618e DG |
1739 | if (attr == NULL) { |
1740 | ret = LTTNG_ERR_FATAL; | |
1741 | goto error; | |
1742 | } | |
1743 | ||
7972aab2 | 1744 | ret = channel_ust_create(usess, attr, usess->buffer_type); |
979e618e DG |
1745 | if (ret != LTTNG_OK) { |
1746 | free(attr); | |
1747 | goto error; | |
1748 | } | |
cf0bcb51 | 1749 | channel_attr_destroy(attr); |
d5979e4a | 1750 | chan_ust_created = 1; |
979e618e DG |
1751 | } |
1752 | ||
601d5acf | 1753 | ret = context_ust_add(usess, domain, ctx, channel_name); |
bdf64013 JG |
1754 | free(app_ctx_provider_name); |
1755 | free(app_ctx_name); | |
1756 | app_ctx_name = NULL; | |
1757 | app_ctx_provider_name = NULL; | |
f73fabfd | 1758 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1759 | goto error; |
1760 | } | |
1761 | break; | |
1762 | } | |
2f77fc4b | 1763 | default: |
f73fabfd | 1764 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1765 | goto error; |
1766 | } | |
1767 | ||
bdf64013 JG |
1768 | ret = LTTNG_OK; |
1769 | goto end; | |
2f77fc4b DG |
1770 | |
1771 | error: | |
d5979e4a DG |
1772 | if (chan_kern_created) { |
1773 | struct ltt_kernel_channel *kchan = | |
1774 | trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME, | |
1775 | session->kernel_session); | |
1776 | /* Created previously, this should NOT fail. */ | |
1777 | assert(kchan); | |
1778 | kernel_destroy_channel(kchan); | |
1779 | } | |
1780 | ||
1781 | if (chan_ust_created) { | |
1782 | struct ltt_ust_channel *uchan = | |
1783 | trace_ust_find_channel_by_name( | |
1784 | session->ust_session->domain_global.channels, | |
1785 | DEFAULT_CHANNEL_NAME); | |
1786 | /* Created previously, this should NOT fail. */ | |
1787 | assert(uchan); | |
1788 | /* Remove from the channel list of the session. */ | |
1789 | trace_ust_delete_channel(session->ust_session->domain_global.channels, | |
1790 | uchan); | |
1791 | trace_ust_destroy_channel(uchan); | |
1792 | } | |
bdf64013 JG |
1793 | end: |
1794 | free(app_ctx_provider_name); | |
1795 | free(app_ctx_name); | |
2f77fc4b DG |
1796 | return ret; |
1797 | } | |
1798 | ||
dac8e046 JG |
1799 | static inline bool name_starts_with(const char *name, const char *prefix) |
1800 | { | |
1801 | const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN); | |
1802 | ||
1803 | return !strncmp(name, prefix, max_cmp_len); | |
1804 | } | |
1805 | ||
1806 | /* Perform userspace-specific event name validation */ | |
1807 | static int validate_ust_event_name(const char *name) | |
1808 | { | |
1809 | int ret = 0; | |
1810 | ||
1811 | if (!name) { | |
1812 | ret = -1; | |
1813 | goto end; | |
1814 | } | |
1815 | ||
1816 | /* | |
1817 | * Check name against all internal UST event component namespaces used | |
1818 | * by the agents. | |
1819 | */ | |
1820 | if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) || | |
1821 | name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) || | |
1822 | name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) { | |
1823 | ret = -1; | |
1824 | } | |
1825 | ||
1826 | end: | |
1827 | return ret; | |
1828 | } | |
88f06f15 | 1829 | |
2f77fc4b | 1830 | /* |
88f06f15 JG |
1831 | * Internal version of cmd_enable_event() with a supplemental |
1832 | * "internal_event" flag which is used to enable internal events which should | |
1833 | * be hidden from clients. Such events are used in the agent implementation to | |
1834 | * enable the events through which all "agent" events are funeled. | |
2f77fc4b | 1835 | */ |
88f06f15 JG |
1836 | static int _cmd_enable_event(struct ltt_session *session, |
1837 | struct lttng_domain *domain, | |
025faf73 | 1838 | char *channel_name, struct lttng_event *event, |
6b453b5e | 1839 | char *filter_expression, |
db8f1377 JI |
1840 | struct lttng_filter_bytecode *filter, |
1841 | struct lttng_event_exclusion *exclusion, | |
88f06f15 | 1842 | int wpipe, bool internal_event) |
2f77fc4b | 1843 | { |
9f449915 | 1844 | int ret = 0, channel_created = 0; |
cfedea03 | 1845 | struct lttng_channel *attr = NULL; |
2f77fc4b DG |
1846 | |
1847 | assert(session); | |
1848 | assert(event); | |
1849 | assert(channel_name); | |
1850 | ||
2a385866 JG |
1851 | /* If we have a filter, we must have its filter expression */ |
1852 | assert(!(!!filter_expression ^ !!filter)); | |
1853 | ||
9f449915 PP |
1854 | /* Normalize event name as a globbing pattern */ |
1855 | strutils_normalize_star_glob_pattern(event->name); | |
18a720cd | 1856 | |
9f449915 PP |
1857 | /* Normalize exclusion names as globbing patterns */ |
1858 | if (exclusion) { | |
1859 | size_t i; | |
f5ac4bd7 | 1860 | |
9f449915 PP |
1861 | for (i = 0; i < exclusion->count; i++) { |
1862 | char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i); | |
1863 | ||
1864 | strutils_normalize_star_glob_pattern(name); | |
1865 | } | |
930a2e99 JG |
1866 | } |
1867 | ||
9f449915 PP |
1868 | DBG("Enable event command for event \'%s\'", event->name); |
1869 | ||
1870 | rcu_read_lock(); | |
1871 | ||
7972aab2 | 1872 | switch (domain->type) { |
2f77fc4b DG |
1873 | case LTTNG_DOMAIN_KERNEL: |
1874 | { | |
1875 | struct ltt_kernel_channel *kchan; | |
1876 | ||
85076754 MD |
1877 | /* |
1878 | * If a non-default channel has been created in the | |
1879 | * session, explicitely require that -c chan_name needs | |
1880 | * to be provided. | |
1881 | */ | |
1882 | if (session->kernel_session->has_non_default_channel | |
1883 | && channel_name[0] == '\0') { | |
1884 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
1885 | goto error; | |
1886 | } | |
1887 | ||
2f77fc4b DG |
1888 | kchan = trace_kernel_get_channel_by_name(channel_name, |
1889 | session->kernel_session); | |
1890 | if (kchan == NULL) { | |
0a9c6494 DG |
1891 | attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, |
1892 | LTTNG_BUFFER_GLOBAL); | |
2f77fc4b | 1893 | if (attr == NULL) { |
f73fabfd | 1894 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1895 | goto error; |
1896 | } | |
04c17253 MD |
1897 | if (lttng_strncpy(attr->name, channel_name, |
1898 | sizeof(attr->name))) { | |
1899 | ret = LTTNG_ERR_INVALID; | |
04c17253 MD |
1900 | goto error; |
1901 | } | |
2f77fc4b DG |
1902 | |
1903 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 1904 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1905 | goto error; |
1906 | } | |
e5f5db7f | 1907 | channel_created = 1; |
2f77fc4b DG |
1908 | } |
1909 | ||
1910 | /* Get the newly created kernel channel pointer */ | |
1911 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
1912 | session->kernel_session); | |
1913 | if (kchan == NULL) { | |
1914 | /* This sould not happen... */ | |
f73fabfd | 1915 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1916 | goto error; |
1917 | } | |
1918 | ||
6e911cad MD |
1919 | switch (event->type) { |
1920 | case LTTNG_EVENT_ALL: | |
29c62722 | 1921 | { |
00a62084 MD |
1922 | char *filter_expression_a = NULL; |
1923 | struct lttng_filter_bytecode *filter_a = NULL; | |
1924 | ||
1925 | /* | |
1926 | * We need to duplicate filter_expression and filter, | |
1927 | * because ownership is passed to first enable | |
1928 | * event. | |
1929 | */ | |
1930 | if (filter_expression) { | |
1931 | filter_expression_a = strdup(filter_expression); | |
1932 | if (!filter_expression_a) { | |
1933 | ret = LTTNG_ERR_FATAL; | |
1934 | goto error; | |
1935 | } | |
1936 | } | |
1937 | if (filter) { | |
1938 | filter_a = zmalloc(sizeof(*filter_a) + filter->len); | |
1939 | if (!filter_a) { | |
1940 | free(filter_expression_a); | |
1941 | ret = LTTNG_ERR_FATAL; | |
1942 | goto error; | |
1943 | } | |
1944 | memcpy(filter_a, filter, sizeof(*filter_a) + filter->len); | |
1945 | } | |
29c62722 | 1946 | event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */ |
00a62084 MD |
1947 | ret = event_kernel_enable_event(kchan, event, |
1948 | filter_expression, filter); | |
a969e101 MD |
1949 | /* We have passed ownership */ |
1950 | filter_expression = NULL; | |
1951 | filter = NULL; | |
29c62722 MD |
1952 | if (ret != LTTNG_OK) { |
1953 | if (channel_created) { | |
1954 | /* Let's not leak a useless channel. */ | |
1955 | kernel_destroy_channel(kchan); | |
1956 | } | |
00a62084 MD |
1957 | free(filter_expression_a); |
1958 | free(filter_a); | |
29c62722 MD |
1959 | goto error; |
1960 | } | |
1961 | event->type = LTTNG_EVENT_SYSCALL; /* Hack */ | |
00a62084 MD |
1962 | ret = event_kernel_enable_event(kchan, event, |
1963 | filter_expression_a, filter_a); | |
60d21fa2 AB |
1964 | /* We have passed ownership */ |
1965 | filter_expression_a = NULL; | |
1966 | filter_a = NULL; | |
29c62722 MD |
1967 | if (ret != LTTNG_OK) { |
1968 | goto error; | |
1969 | } | |
1970 | break; | |
1971 | } | |
6e6ef3d7 DG |
1972 | case LTTNG_EVENT_PROBE: |
1973 | case LTTNG_EVENT_FUNCTION: | |
1974 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
6e911cad | 1975 | case LTTNG_EVENT_TRACEPOINT: |
00a62084 MD |
1976 | ret = event_kernel_enable_event(kchan, event, |
1977 | filter_expression, filter); | |
a969e101 MD |
1978 | /* We have passed ownership */ |
1979 | filter_expression = NULL; | |
1980 | filter = NULL; | |
6e911cad MD |
1981 | if (ret != LTTNG_OK) { |
1982 | if (channel_created) { | |
1983 | /* Let's not leak a useless channel. */ | |
1984 | kernel_destroy_channel(kchan); | |
1985 | } | |
1986 | goto error; | |
e5f5db7f | 1987 | } |
6e911cad MD |
1988 | break; |
1989 | case LTTNG_EVENT_SYSCALL: | |
00a62084 MD |
1990 | ret = event_kernel_enable_event(kchan, event, |
1991 | filter_expression, filter); | |
a969e101 MD |
1992 | /* We have passed ownership */ |
1993 | filter_expression = NULL; | |
1994 | filter = NULL; | |
e2b957af MD |
1995 | if (ret != LTTNG_OK) { |
1996 | goto error; | |
1997 | } | |
6e911cad MD |
1998 | break; |
1999 | default: | |
2000 | ret = LTTNG_ERR_UNK; | |
2f77fc4b DG |
2001 | goto error; |
2002 | } | |
2003 | ||
2004 | kernel_wait_quiescent(kernel_tracer_fd); | |
2005 | break; | |
2006 | } | |
2007 | case LTTNG_DOMAIN_UST: | |
2008 | { | |
2009 | struct ltt_ust_channel *uchan; | |
2010 | struct ltt_ust_session *usess = session->ust_session; | |
2011 | ||
2012 | assert(usess); | |
2013 | ||
85076754 MD |
2014 | /* |
2015 | * If a non-default channel has been created in the | |
2016 | * session, explicitely require that -c chan_name needs | |
2017 | * to be provided. | |
2018 | */ | |
2019 | if (usess->has_non_default_channel && channel_name[0] == '\0') { | |
2020 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
2021 | goto error; | |
2022 | } | |
2023 | ||
2f77fc4b DG |
2024 | /* Get channel from global UST domain */ |
2025 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
2026 | channel_name); | |
2027 | if (uchan == NULL) { | |
2028 | /* Create default channel */ | |
0a9c6494 DG |
2029 | attr = channel_new_default_attr(LTTNG_DOMAIN_UST, |
2030 | usess->buffer_type); | |
2f77fc4b | 2031 | if (attr == NULL) { |
f73fabfd | 2032 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2033 | goto error; |
2034 | } | |
04c17253 MD |
2035 | if (lttng_strncpy(attr->name, channel_name, |
2036 | sizeof(attr->name))) { | |
2037 | ret = LTTNG_ERR_INVALID; | |
04c17253 MD |
2038 | goto error; |
2039 | } | |
2f77fc4b DG |
2040 | |
2041 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 2042 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2043 | goto error; |
2044 | } | |
2f77fc4b DG |
2045 | |
2046 | /* Get the newly created channel reference back */ | |
2047 | uchan = trace_ust_find_channel_by_name( | |
2048 | usess->domain_global.channels, channel_name); | |
2049 | assert(uchan); | |
2050 | } | |
2051 | ||
141feb8c JG |
2052 | if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) { |
2053 | /* | |
2054 | * Don't allow users to add UST events to channels which | |
2055 | * are assigned to a userspace subdomain (JUL, Log4J, | |
2056 | * Python, etc.). | |
2057 | */ | |
2058 | ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN; | |
2059 | goto error; | |
2060 | } | |
2061 | ||
dac8e046 JG |
2062 | if (!internal_event) { |
2063 | /* | |
2064 | * Ensure the event name is not reserved for internal | |
2065 | * use. | |
2066 | */ | |
2067 | ret = validate_ust_event_name(event->name); | |
2068 | if (ret) { | |
2069 | WARN("Userspace event name %s failed validation.", | |
2070 | event->name ? | |
2071 | event->name : "NULL"); | |
2072 | ret = LTTNG_ERR_INVALID_EVENT_NAME; | |
2073 | goto error; | |
2074 | } | |
2075 | } | |
2076 | ||
2f77fc4b | 2077 | /* At this point, the session and channel exist on the tracer */ |
6b453b5e | 2078 | ret = event_ust_enable_tracepoint(usess, uchan, event, |
88f06f15 JG |
2079 | filter_expression, filter, exclusion, |
2080 | internal_event); | |
49d21f93 MD |
2081 | /* We have passed ownership */ |
2082 | filter_expression = NULL; | |
2083 | filter = NULL; | |
2084 | exclusion = NULL; | |
94382e15 JG |
2085 | if (ret == LTTNG_ERR_UST_EVENT_ENABLED) { |
2086 | goto already_enabled; | |
2087 | } else if (ret != LTTNG_OK) { | |
2f77fc4b DG |
2088 | goto error; |
2089 | } | |
2090 | break; | |
2091 | } | |
5cdb6027 | 2092 | case LTTNG_DOMAIN_LOG4J: |
f20baf8e | 2093 | case LTTNG_DOMAIN_JUL: |
0e115563 | 2094 | case LTTNG_DOMAIN_PYTHON: |
f20baf8e | 2095 | { |
da6c3a50 | 2096 | const char *default_event_name, *default_chan_name; |
fefd409b | 2097 | struct agent *agt; |
f20baf8e DG |
2098 | struct lttng_event uevent; |
2099 | struct lttng_domain tmp_dom; | |
2100 | struct ltt_ust_session *usess = session->ust_session; | |
2101 | ||
2102 | assert(usess); | |
2103 | ||
f28f9e44 JG |
2104 | if (!agent_tracing_is_enabled()) { |
2105 | DBG("Attempted to enable an event in an agent domain but the agent thread is not running"); | |
2106 | ret = LTTNG_ERR_AGENT_TRACING_DISABLED; | |
2107 | goto error; | |
2108 | } | |
2109 | ||
5cdb6027 | 2110 | agt = trace_ust_find_agent(usess, domain->type); |
fefd409b | 2111 | if (!agt) { |
5cdb6027 | 2112 | agt = agent_create(domain->type); |
fefd409b | 2113 | if (!agt) { |
e5b3c48c | 2114 | ret = LTTNG_ERR_NOMEM; |
fefd409b DG |
2115 | goto error; |
2116 | } | |
2117 | agent_add(agt, usess->agents); | |
2118 | } | |
2119 | ||
022d91ba | 2120 | /* Create the default tracepoint. */ |
996de3c7 | 2121 | memset(&uevent, 0, sizeof(uevent)); |
f20baf8e DG |
2122 | uevent.type = LTTNG_EVENT_TRACEPOINT; |
2123 | uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
51755dc8 JG |
2124 | default_event_name = event_get_default_agent_ust_name( |
2125 | domain->type); | |
da6c3a50 | 2126 | if (!default_event_name) { |
e5b3c48c | 2127 | ret = LTTNG_ERR_FATAL; |
da6c3a50 | 2128 | goto error; |
f43f95a9 | 2129 | } |
da6c3a50 | 2130 | strncpy(uevent.name, default_event_name, sizeof(uevent.name)); |
f20baf8e DG |
2131 | uevent.name[sizeof(uevent.name) - 1] = '\0'; |
2132 | ||
2133 | /* | |
2134 | * The domain type is changed because we are about to enable the | |
2135 | * default channel and event for the JUL domain that are hardcoded. | |
2136 | * This happens in the UST domain. | |
2137 | */ | |
2138 | memcpy(&tmp_dom, domain, sizeof(tmp_dom)); | |
2139 | tmp_dom.type = LTTNG_DOMAIN_UST; | |
2140 | ||
0e115563 DG |
2141 | switch (domain->type) { |
2142 | case LTTNG_DOMAIN_LOG4J: | |
da6c3a50 | 2143 | default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME; |
0e115563 DG |
2144 | break; |
2145 | case LTTNG_DOMAIN_JUL: | |
da6c3a50 | 2146 | default_chan_name = DEFAULT_JUL_CHANNEL_NAME; |
0e115563 DG |
2147 | break; |
2148 | case LTTNG_DOMAIN_PYTHON: | |
2149 | default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME; | |
2150 | break; | |
2151 | default: | |
e98a44b0 | 2152 | /* The switch/case we are in makes this impossible */ |
0e115563 | 2153 | assert(0); |
da6c3a50 DG |
2154 | } |
2155 | ||
971da06a | 2156 | { |
8404118c | 2157 | char *filter_expression_copy = NULL; |
51755dc8 | 2158 | struct lttng_filter_bytecode *filter_copy = NULL; |
971da06a JG |
2159 | |
2160 | if (filter) { | |
51755dc8 JG |
2161 | const size_t filter_size = sizeof( |
2162 | struct lttng_filter_bytecode) | |
2163 | + filter->len; | |
2164 | ||
2165 | filter_copy = zmalloc(filter_size); | |
971da06a | 2166 | if (!filter_copy) { |
018096a4 | 2167 | ret = LTTNG_ERR_NOMEM; |
b742e3e2 | 2168 | goto error; |
971da06a | 2169 | } |
51755dc8 | 2170 | memcpy(filter_copy, filter, filter_size); |
971da06a | 2171 | |
8404118c JG |
2172 | filter_expression_copy = |
2173 | strdup(filter_expression); | |
2174 | if (!filter_expression) { | |
2175 | ret = LTTNG_ERR_NOMEM; | |
51755dc8 JG |
2176 | } |
2177 | ||
2178 | if (!filter_expression_copy || !filter_copy) { | |
2179 | free(filter_expression_copy); | |
2180 | free(filter_copy); | |
2181 | goto error; | |
8404118c | 2182 | } |
971da06a JG |
2183 | } |
2184 | ||
88f06f15 | 2185 | ret = cmd_enable_event_internal(session, &tmp_dom, |
971da06a | 2186 | (char *) default_chan_name, |
8404118c JG |
2187 | &uevent, filter_expression_copy, |
2188 | filter_copy, NULL, wpipe); | |
971da06a JG |
2189 | } |
2190 | ||
94382e15 JG |
2191 | if (ret == LTTNG_ERR_UST_EVENT_ENABLED) { |
2192 | goto already_enabled; | |
2193 | } else if (ret != LTTNG_OK) { | |
f20baf8e DG |
2194 | goto error; |
2195 | } | |
2196 | ||
2197 | /* The wild card * means that everything should be enabled. */ | |
2198 | if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) { | |
8404118c JG |
2199 | ret = event_agent_enable_all(usess, agt, event, filter, |
2200 | filter_expression); | |
f20baf8e | 2201 | } else { |
8404118c JG |
2202 | ret = event_agent_enable(usess, agt, event, filter, |
2203 | filter_expression); | |
f20baf8e | 2204 | } |
51755dc8 JG |
2205 | filter = NULL; |
2206 | filter_expression = NULL; | |
f20baf8e DG |
2207 | if (ret != LTTNG_OK) { |
2208 | goto error; | |
2209 | } | |
2210 | ||
2211 | break; | |
2212 | } | |
2f77fc4b | 2213 | default: |
f73fabfd | 2214 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2215 | goto error; |
2216 | } | |
2217 | ||
f73fabfd | 2218 | ret = LTTNG_OK; |
2f77fc4b | 2219 | |
94382e15 | 2220 | already_enabled: |
2f77fc4b | 2221 | error: |
49d21f93 MD |
2222 | free(filter_expression); |
2223 | free(filter); | |
2224 | free(exclusion); | |
cf0bcb51 | 2225 | channel_attr_destroy(attr); |
2223c96f | 2226 | rcu_read_unlock(); |
2f77fc4b DG |
2227 | return ret; |
2228 | } | |
2229 | ||
88f06f15 JG |
2230 | /* |
2231 | * Command LTTNG_ENABLE_EVENT processed by the client thread. | |
2232 | * We own filter, exclusion, and filter_expression. | |
2233 | */ | |
2234 | int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain, | |
2235 | char *channel_name, struct lttng_event *event, | |
2236 | char *filter_expression, | |
2237 | struct lttng_filter_bytecode *filter, | |
2238 | struct lttng_event_exclusion *exclusion, | |
2239 | int wpipe) | |
2240 | { | |
2241 | return _cmd_enable_event(session, domain, channel_name, event, | |
2242 | filter_expression, filter, exclusion, wpipe, false); | |
2243 | } | |
2244 | ||
2245 | /* | |
2246 | * Enable an event which is internal to LTTng. An internal should | |
2247 | * never be made visible to clients and are immune to checks such as | |
2248 | * reserved names. | |
2249 | */ | |
2250 | static int cmd_enable_event_internal(struct ltt_session *session, | |
2251 | struct lttng_domain *domain, | |
2252 | char *channel_name, struct lttng_event *event, | |
2253 | char *filter_expression, | |
2254 | struct lttng_filter_bytecode *filter, | |
2255 | struct lttng_event_exclusion *exclusion, | |
2256 | int wpipe) | |
2257 | { | |
2258 | return _cmd_enable_event(session, domain, channel_name, event, | |
2259 | filter_expression, filter, exclusion, wpipe, true); | |
2260 | } | |
2261 | ||
2f77fc4b DG |
2262 | /* |
2263 | * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. | |
2264 | */ | |
56a37563 JG |
2265 | ssize_t cmd_list_tracepoints(enum lttng_domain_type domain, |
2266 | struct lttng_event **events) | |
2f77fc4b DG |
2267 | { |
2268 | int ret; | |
2269 | ssize_t nb_events = 0; | |
2270 | ||
2271 | switch (domain) { | |
2272 | case LTTNG_DOMAIN_KERNEL: | |
2273 | nb_events = kernel_list_events(kernel_tracer_fd, events); | |
2274 | if (nb_events < 0) { | |
f73fabfd | 2275 | ret = LTTNG_ERR_KERN_LIST_FAIL; |
2f77fc4b DG |
2276 | goto error; |
2277 | } | |
2278 | break; | |
2279 | case LTTNG_DOMAIN_UST: | |
2280 | nb_events = ust_app_list_events(events); | |
2281 | if (nb_events < 0) { | |
f73fabfd | 2282 | ret = LTTNG_ERR_UST_LIST_FAIL; |
2f77fc4b DG |
2283 | goto error; |
2284 | } | |
2285 | break; | |
5cdb6027 | 2286 | case LTTNG_DOMAIN_LOG4J: |
3c6a091f | 2287 | case LTTNG_DOMAIN_JUL: |
0e115563 | 2288 | case LTTNG_DOMAIN_PYTHON: |
f60140a1 | 2289 | nb_events = agent_list_events(events, domain); |
3c6a091f DG |
2290 | if (nb_events < 0) { |
2291 | ret = LTTNG_ERR_UST_LIST_FAIL; | |
2292 | goto error; | |
2293 | } | |
2294 | break; | |
2f77fc4b | 2295 | default: |
f73fabfd | 2296 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2297 | goto error; |
2298 | } | |
2299 | ||
2300 | return nb_events; | |
2301 | ||
2302 | error: | |
2303 | /* Return negative value to differentiate return code */ | |
2304 | return -ret; | |
2305 | } | |
2306 | ||
2307 | /* | |
2308 | * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread. | |
2309 | */ | |
56a37563 | 2310 | ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain, |
2f77fc4b DG |
2311 | struct lttng_event_field **fields) |
2312 | { | |
2313 | int ret; | |
2314 | ssize_t nb_fields = 0; | |
2315 | ||
2316 | switch (domain) { | |
2317 | case LTTNG_DOMAIN_UST: | |
2318 | nb_fields = ust_app_list_event_fields(fields); | |
2319 | if (nb_fields < 0) { | |
f73fabfd | 2320 | ret = LTTNG_ERR_UST_LIST_FAIL; |
2f77fc4b DG |
2321 | goto error; |
2322 | } | |
2323 | break; | |
2324 | case LTTNG_DOMAIN_KERNEL: | |
2325 | default: /* fall-through */ | |
f73fabfd | 2326 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2327 | goto error; |
2328 | } | |
2329 | ||
2330 | return nb_fields; | |
2331 | ||
2332 | error: | |
2333 | /* Return negative value to differentiate return code */ | |
2334 | return -ret; | |
2335 | } | |
2336 | ||
834978fd DG |
2337 | ssize_t cmd_list_syscalls(struct lttng_event **events) |
2338 | { | |
2339 | return syscall_table_list(events); | |
2340 | } | |
2341 | ||
a5dfbb9d MD |
2342 | /* |
2343 | * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread. | |
2344 | * | |
2345 | * Called with session lock held. | |
2346 | */ | |
2347 | ssize_t cmd_list_tracker_pids(struct ltt_session *session, | |
56a37563 | 2348 | enum lttng_domain_type domain, int32_t **pids) |
a5dfbb9d MD |
2349 | { |
2350 | int ret; | |
2351 | ssize_t nr_pids = 0; | |
2352 | ||
2353 | switch (domain) { | |
2354 | case LTTNG_DOMAIN_KERNEL: | |
2355 | { | |
2356 | struct ltt_kernel_session *ksess; | |
2357 | ||
2358 | ksess = session->kernel_session; | |
2359 | nr_pids = kernel_list_tracker_pids(ksess, pids); | |
2360 | if (nr_pids < 0) { | |
2361 | ret = LTTNG_ERR_KERN_LIST_FAIL; | |
2362 | goto error; | |
2363 | } | |
2364 | break; | |
2365 | } | |
2366 | case LTTNG_DOMAIN_UST: | |
2367 | { | |
2368 | struct ltt_ust_session *usess; | |
2369 | ||
2370 | usess = session->ust_session; | |
2371 | nr_pids = trace_ust_list_tracker_pids(usess, pids); | |
2372 | if (nr_pids < 0) { | |
2373 | ret = LTTNG_ERR_UST_LIST_FAIL; | |
2374 | goto error; | |
2375 | } | |
2376 | break; | |
2377 | } | |
2378 | case LTTNG_DOMAIN_LOG4J: | |
2379 | case LTTNG_DOMAIN_JUL: | |
2380 | case LTTNG_DOMAIN_PYTHON: | |
2381 | default: | |
2382 | ret = LTTNG_ERR_UND; | |
2383 | goto error; | |
2384 | } | |
2385 | ||
2386 | return nr_pids; | |
2387 | ||
2388 | error: | |
2389 | /* Return negative value to differentiate return code */ | |
2390 | return -ret; | |
2391 | } | |
2392 | ||
a1ae2ea5 JD |
2393 | static |
2394 | int domain_mkdir(const struct consumer_output *output, | |
2395 | const struct ltt_session *session, | |
2396 | uid_t uid, gid_t gid) | |
2397 | { | |
2398 | struct consumer_socket *socket; | |
2399 | struct lttng_ht_iter iter; | |
2400 | int ret; | |
2401 | char *path = NULL; | |
2402 | ||
2403 | if (!output || !output->socks) { | |
2404 | ERR("No consumer output found"); | |
2405 | ret = -1; | |
2406 | goto end; | |
2407 | } | |
2408 | ||
2409 | path = zmalloc(LTTNG_PATH_MAX * sizeof(char)); | |
2410 | if (!path) { | |
2411 | ERR("Cannot allocate mkdir path"); | |
2412 | ret = -1; | |
2413 | goto end; | |
2414 | } | |
2415 | ||
2416 | ret = snprintf(path, LTTNG_PATH_MAX, "%s%s%s", | |
2417 | session_get_base_path(session), | |
2418 | output->chunk_path, output->subdir); | |
2419 | if (ret < 0 || ret >= LTTNG_PATH_MAX) { | |
2420 | ERR("Format path"); | |
2421 | ret = -1; | |
2422 | goto end; | |
2423 | } | |
2424 | ||
2425 | DBG("Domain mkdir %s for session %" PRIu64, path, session->id); | |
2426 | rcu_read_lock(); | |
2427 | /* | |
2428 | * We have to iterate to find a socket, but we only need to send the | |
2429 | * rename command to one consumer, so we break after the first one. | |
2430 | */ | |
2431 | cds_lfht_for_each_entry(output->socks->ht, &iter.iter, socket, node.node) { | |
2432 | pthread_mutex_lock(socket->lock); | |
2433 | ret = consumer_mkdir(socket, session->id, output, path, uid, gid); | |
2434 | pthread_mutex_unlock(socket->lock); | |
2435 | if (ret) { | |
2436 | ERR("Consumer mkdir"); | |
2437 | ret = -1; | |
2438 | goto end_unlock; | |
2439 | } | |
2440 | break; | |
2441 | } | |
2442 | ||
2443 | ret = 0; | |
2444 | ||
2445 | end_unlock: | |
2446 | rcu_read_unlock(); | |
2447 | end: | |
2448 | free(path); | |
2449 | return ret; | |
2450 | } | |
2451 | ||
2452 | static | |
2453 | int session_mkdir(const struct ltt_session *session) | |
2454 | { | |
2455 | int ret; | |
2456 | struct consumer_output *output; | |
2457 | uid_t uid; | |
2458 | gid_t gid; | |
2459 | ||
2460 | /* | |
2461 | * Unsupported feature in lttng-relayd before 2.11, not an error since it | |
2462 | * is only needed for session rotation and the user will get an error | |
2463 | * on rotate. | |
2464 | */ | |
2465 | if (session->consumer->type == CONSUMER_DST_NET && | |
2466 | session->consumer->relay_major_version == 2 && | |
2467 | session->consumer->relay_minor_version < 11) { | |
2468 | ret = 0; | |
2469 | goto end; | |
2470 | } | |
2471 | ||
2472 | if (session->kernel_session) { | |
2473 | output = session->kernel_session->consumer; | |
2474 | uid = session->kernel_session->uid; | |
2475 | gid = session->kernel_session->gid; | |
2476 | ret = domain_mkdir(output, session, uid, gid); | |
2477 | if (ret) { | |
2478 | ERR("Mkdir kernel"); | |
2479 | goto end; | |
2480 | } | |
2481 | } | |
2482 | ||
2483 | if (session->ust_session) { | |
2484 | output = session->ust_session->consumer; | |
2485 | uid = session->ust_session->uid; | |
2486 | gid = session->ust_session->gid; | |
2487 | ret = domain_mkdir(output, session, uid, gid); | |
2488 | if (ret) { | |
2489 | ERR("Mkdir UST"); | |
2490 | goto end; | |
2491 | } | |
2492 | } | |
2493 | ||
2494 | ret = 0; | |
2495 | ||
2496 | end: | |
2497 | return ret; | |
2498 | } | |
2499 | ||
2f77fc4b DG |
2500 | /* |
2501 | * Command LTTNG_START_TRACE processed by the client thread. | |
a9ad0c8f MD |
2502 | * |
2503 | * Called with session mutex held. | |
2f77fc4b DG |
2504 | */ |
2505 | int cmd_start_trace(struct ltt_session *session) | |
2506 | { | |
2507 | int ret; | |
cde3e505 | 2508 | unsigned long nb_chan = 0; |
2f77fc4b DG |
2509 | struct ltt_kernel_session *ksession; |
2510 | struct ltt_ust_session *usess; | |
2f77fc4b DG |
2511 | |
2512 | assert(session); | |
2513 | ||
2514 | /* Ease our life a bit ;) */ | |
2515 | ksession = session->kernel_session; | |
2516 | usess = session->ust_session; | |
2517 | ||
8382cf6f DG |
2518 | /* Is the session already started? */ |
2519 | if (session->active) { | |
f73fabfd | 2520 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
2f77fc4b DG |
2521 | goto error; |
2522 | } | |
2523 | ||
cde3e505 DG |
2524 | /* |
2525 | * Starting a session without channel is useless since after that it's not | |
2526 | * possible to enable channel thus inform the client. | |
2527 | */ | |
2528 | if (usess && usess->domain_global.channels) { | |
2529 | nb_chan += lttng_ht_get_count(usess->domain_global.channels); | |
2530 | } | |
2531 | if (ksession) { | |
2532 | nb_chan += ksession->channel_count; | |
2533 | } | |
2534 | if (!nb_chan) { | |
2535 | ret = LTTNG_ERR_NO_CHANNEL; | |
2536 | goto error; | |
2537 | } | |
2538 | ||
5c408ad8 JD |
2539 | /* |
2540 | * Record the timestamp of the first time the session is started for | |
2541 | * an eventual session rotation call. | |
2542 | */ | |
c996624c | 2543 | if (!session->has_been_started) { |
5c408ad8 JD |
2544 | session->current_chunk_start_ts = time(NULL); |
2545 | if (session->current_chunk_start_ts == (time_t) -1) { | |
2546 | PERROR("Failed to retrieve the \"%s\" session's start time", | |
2547 | session->name); | |
2548 | ret = LTTNG_ERR_FATAL; | |
2549 | goto error; | |
2550 | } | |
c996624c JD |
2551 | if (!session->snapshot_mode && session->output_traces) { |
2552 | ret = session_mkdir(session); | |
2553 | if (ret) { | |
2554 | ERR("Failed to create the session directories"); | |
2555 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
2556 | goto error; | |
2557 | } | |
2558 | } | |
2559 | } | |
2560 | ||
2f77fc4b DG |
2561 | /* Kernel tracing */ |
2562 | if (ksession != NULL) { | |
c996624c | 2563 | DBG("Start kernel tracing session %s", session->name); |
9b6c7ec5 DG |
2564 | ret = start_kernel_session(ksession, kernel_tracer_fd); |
2565 | if (ret != LTTNG_OK) { | |
2f77fc4b DG |
2566 | goto error; |
2567 | } | |
2f77fc4b DG |
2568 | } |
2569 | ||
2570 | /* Flag session that trace should start automatically */ | |
2571 | if (usess) { | |
14fb1ebe DG |
2572 | /* |
2573 | * Even though the start trace might fail, flag this session active so | |
2574 | * other application coming in are started by default. | |
2575 | */ | |
2576 | usess->active = 1; | |
2f77fc4b DG |
2577 | |
2578 | ret = ust_app_start_trace_all(usess); | |
2579 | if (ret < 0) { | |
f73fabfd | 2580 | ret = LTTNG_ERR_UST_START_FAIL; |
2f77fc4b DG |
2581 | goto error; |
2582 | } | |
2583 | } | |
2584 | ||
8382cf6f DG |
2585 | /* Flag this after a successful start. */ |
2586 | session->has_been_started = 1; | |
2587 | session->active = 1; | |
9b6c7ec5 | 2588 | |
5c408ad8 JD |
2589 | /* |
2590 | * Clear the flag that indicates that a rotation was done while the | |
2591 | * session was stopped. | |
2592 | */ | |
2593 | session->rotated_after_last_stop = false; | |
2594 | ||
259c2674 JD |
2595 | if (session->rotate_timer_period) { |
2596 | ret = sessiond_rotate_timer_start(session, | |
2597 | session->rotate_timer_period); | |
2598 | if (ret < 0) { | |
2599 | ERR("Failed to enable rotate timer"); | |
2600 | ret = LTTNG_ERR_UNK; | |
2601 | goto error; | |
2602 | } | |
2603 | } | |
2604 | ||
f73fabfd | 2605 | ret = LTTNG_OK; |
2f77fc4b DG |
2606 | |
2607 | error: | |
2608 | return ret; | |
2609 | } | |
2610 | ||
5c408ad8 JD |
2611 | static |
2612 | int rename_active_chunk(struct ltt_session *session) | |
2613 | { | |
2614 | int ret; | |
2615 | ||
2616 | session->rotate_count++; | |
2617 | ||
2618 | /* | |
2619 | * The currently active tracing path is now the folder we | |
2620 | * want to rename. | |
2621 | */ | |
2622 | ret = lttng_strncpy(session->rotation_chunk.current_rotate_path, | |
2623 | session->rotation_chunk.active_tracing_path, | |
2624 | sizeof(session->rotation_chunk.current_rotate_path)); | |
2625 | if (ret) { | |
2626 | ERR("Failed to copy active tracing path"); | |
2627 | goto end; | |
2628 | } | |
2629 | ||
2630 | ret = rename_complete_chunk(session, time(NULL)); | |
2631 | if (ret < 0) { | |
2632 | ERR("Failed to rename current rotate path"); | |
2633 | goto end; | |
2634 | } | |
2635 | ||
2636 | /* | |
2637 | * We just renamed, the folder, we didn't do an actual rotation, so | |
2638 | * the active tracing path is now the renamed folder and we have to | |
2639 | * restore the rotate count. | |
2640 | */ | |
2641 | ret = lttng_strncpy(session->rotation_chunk.active_tracing_path, | |
2642 | session->rotation_chunk.current_rotate_path, | |
2643 | sizeof(session->rotation_chunk.active_tracing_path)); | |
2644 | if (ret) { | |
2645 | ERR("Failed to rename active session chunk tracing path"); | |
2646 | goto end; | |
2647 | } | |
2648 | end: | |
2649 | session->rotate_count--; | |
2650 | return ret; | |
2651 | } | |
2652 | ||
2f77fc4b DG |
2653 | /* |
2654 | * Command LTTNG_STOP_TRACE processed by the client thread. | |
2655 | */ | |
2656 | int cmd_stop_trace(struct ltt_session *session) | |
2657 | { | |
2658 | int ret; | |
2659 | struct ltt_kernel_channel *kchan; | |
2660 | struct ltt_kernel_session *ksession; | |
2661 | struct ltt_ust_session *usess; | |
5c408ad8 | 2662 | bool error_occured = false; |
2f77fc4b DG |
2663 | |
2664 | assert(session); | |
2665 | ||
5c408ad8 | 2666 | DBG("Begin stop session %s (id %" PRIu64 ")", session->name, session->id); |
2f77fc4b DG |
2667 | /* Short cut */ |
2668 | ksession = session->kernel_session; | |
2669 | usess = session->ust_session; | |
2670 | ||
8382cf6f DG |
2671 | /* Session is not active. Skip everythong and inform the client. */ |
2672 | if (!session->active) { | |
f73fabfd | 2673 | ret = LTTNG_ERR_TRACE_ALREADY_STOPPED; |
2f77fc4b DG |
2674 | goto error; |
2675 | } | |
2676 | ||
d88744a4 JD |
2677 | if (session->rotate_relay_pending_timer_enabled) { |
2678 | sessiond_timer_rotate_pending_stop(session); | |
2679 | } | |
2680 | ||
259c2674 JD |
2681 | if (session->rotate_timer_enabled) { |
2682 | sessiond_rotate_timer_stop(session); | |
2683 | } | |
2684 | ||
5c408ad8 JD |
2685 | if (session->rotate_count > 0 && !session->rotate_pending) { |
2686 | ret = rename_active_chunk(session); | |
2687 | if (ret) { | |
2688 | /* | |
2689 | * This error should not prevent the user from stopping | |
2690 | * the session. However, it will be reported at the end. | |
2691 | */ | |
2692 | error_occured = true; | |
2693 | } | |
2694 | } | |
2695 | ||
2f77fc4b | 2696 | /* Kernel tracer */ |
14fb1ebe | 2697 | if (ksession && ksession->active) { |
2f77fc4b DG |
2698 | DBG("Stop kernel tracing"); |
2699 | ||
566d8970 MD |
2700 | ret = kernel_stop_session(ksession); |
2701 | if (ret < 0) { | |
2702 | ret = LTTNG_ERR_KERN_STOP_FAIL; | |
2703 | goto error; | |
2704 | } | |
2705 | ||
2706 | kernel_wait_quiescent(kernel_tracer_fd); | |
2707 | ||
2708 | /* Flush metadata after stopping (if exists) */ | |
2f77fc4b DG |
2709 | if (ksession->metadata_stream_fd >= 0) { |
2710 | ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd); | |
2711 | if (ret < 0) { | |
2712 | ERR("Kernel metadata flush failed"); | |
2713 | } | |
2714 | } | |
2715 | ||
566d8970 | 2716 | /* Flush all buffers after stopping */ |
2f77fc4b DG |
2717 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { |
2718 | ret = kernel_flush_buffer(kchan); | |
2719 | if (ret < 0) { | |
2720 | ERR("Kernel flush buffer error"); | |
2721 | } | |
2722 | } | |
2723 | ||
14fb1ebe | 2724 | ksession->active = 0; |
5c408ad8 JD |
2725 | DBG("Kernel session stopped %s (id %" PRIu64 ")", session->name, |
2726 | session->id); | |
2f77fc4b DG |
2727 | } |
2728 | ||
14fb1ebe DG |
2729 | if (usess && usess->active) { |
2730 | /* | |
2731 | * Even though the stop trace might fail, flag this session inactive so | |
2732 | * other application coming in are not started by default. | |
2733 | */ | |
2734 | usess->active = 0; | |
2f77fc4b DG |
2735 | |
2736 | ret = ust_app_stop_trace_all(usess); | |
2737 | if (ret < 0) { | |
f73fabfd | 2738 | ret = LTTNG_ERR_UST_STOP_FAIL; |
2f77fc4b DG |
2739 | goto error; |
2740 | } | |
2741 | } | |
2742 | ||
8382cf6f DG |
2743 | /* Flag inactive after a successful stop. */ |
2744 | session->active = 0; | |
5c408ad8 | 2745 | ret = !error_occured ? LTTNG_OK : LTTNG_ERR_UNK; |
2f77fc4b DG |
2746 | |
2747 | error: | |
2748 | return ret; | |
2749 | } | |
2750 | ||
2751 | /* | |
2752 | * Command LTTNG_SET_CONSUMER_URI processed by the client thread. | |
2753 | */ | |
bda32d56 JG |
2754 | int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri, |
2755 | struct lttng_uri *uris) | |
2f77fc4b DG |
2756 | { |
2757 | int ret, i; | |
2758 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2759 | struct ltt_ust_session *usess = session->ust_session; | |
2f77fc4b DG |
2760 | |
2761 | assert(session); | |
2762 | assert(uris); | |
2763 | assert(nb_uri > 0); | |
2764 | ||
8382cf6f DG |
2765 | /* Can't set consumer URI if the session is active. */ |
2766 | if (session->active) { | |
f73fabfd | 2767 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
2f77fc4b DG |
2768 | goto error; |
2769 | } | |
2770 | ||
bda32d56 | 2771 | /* Set the "global" consumer URIs */ |
2f77fc4b | 2772 | for (i = 0; i < nb_uri; i++) { |
bda32d56 JG |
2773 | ret = add_uri_to_consumer(session->consumer, |
2774 | &uris[i], 0, session->name); | |
a74934ba | 2775 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2776 | goto error; |
2777 | } | |
2f77fc4b DG |
2778 | } |
2779 | ||
bda32d56 JG |
2780 | /* Set UST session URIs */ |
2781 | if (session->ust_session) { | |
2782 | for (i = 0; i < nb_uri; i++) { | |
2783 | ret = add_uri_to_consumer( | |
2784 | session->ust_session->consumer, | |
2785 | &uris[i], LTTNG_DOMAIN_UST, | |
2786 | session->name); | |
2787 | if (ret != LTTNG_OK) { | |
2788 | goto error; | |
2789 | } | |
2790 | } | |
2791 | } | |
2792 | ||
2793 | /* Set kernel session URIs */ | |
2794 | if (session->kernel_session) { | |
2795 | for (i = 0; i < nb_uri; i++) { | |
2796 | ret = add_uri_to_consumer( | |
2797 | session->kernel_session->consumer, | |
2798 | &uris[i], LTTNG_DOMAIN_KERNEL, | |
2799 | session->name); | |
2800 | if (ret != LTTNG_OK) { | |
2801 | goto error; | |
2802 | } | |
2803 | } | |
2804 | } | |
2805 | ||
7ab70fe0 DG |
2806 | /* |
2807 | * Make sure to set the session in output mode after we set URI since a | |
2808 | * session can be created without URL (thus flagged in no output mode). | |
2809 | */ | |
2810 | session->output_traces = 1; | |
2811 | if (ksess) { | |
2812 | ksess->output_traces = 1; | |
bda32d56 JG |
2813 | } |
2814 | ||
2815 | if (usess) { | |
7ab70fe0 DG |
2816 | usess->output_traces = 1; |
2817 | } | |
2818 | ||
2f77fc4b | 2819 | /* All good! */ |
f73fabfd | 2820 | ret = LTTNG_OK; |
2f77fc4b DG |
2821 | |
2822 | error: | |
2823 | return ret; | |
2824 | } | |
2825 | ||
2826 | /* | |
2827 | * Command LTTNG_CREATE_SESSION processed by the client thread. | |
2828 | */ | |
2829 | int cmd_create_session_uri(char *name, struct lttng_uri *uris, | |
ecc48a90 | 2830 | size_t nb_uri, lttng_sock_cred *creds, unsigned int live_timer) |
2f77fc4b DG |
2831 | { |
2832 | int ret; | |
2f77fc4b DG |
2833 | struct ltt_session *session; |
2834 | ||
2835 | assert(name); | |
2bba9e53 | 2836 | assert(creds); |
785d2d0d | 2837 | |
2f77fc4b DG |
2838 | /* |
2839 | * Verify if the session already exist | |
2840 | * | |
2841 | * XXX: There is no need for the session lock list here since the caller | |
2842 | * (process_client_msg) is holding it. We might want to change that so a | |
2843 | * single command does not lock the entire session list. | |
2844 | */ | |
2845 | session = session_find_by_name(name); | |
2846 | if (session != NULL) { | |
f73fabfd | 2847 | ret = LTTNG_ERR_EXIST_SESS; |
2f77fc4b DG |
2848 | goto find_error; |
2849 | } | |
2850 | ||
2851 | /* Create tracing session in the registry */ | |
dec56f6c | 2852 | ret = session_create(name, LTTNG_SOCK_GET_UID_CRED(creds), |
2f77fc4b | 2853 | LTTNG_SOCK_GET_GID_CRED(creds)); |
f73fabfd | 2854 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2855 | goto session_error; |
2856 | } | |
2857 | ||
2858 | /* | |
2859 | * Get the newly created session pointer back | |
2860 | * | |
2861 | * XXX: There is no need for the session lock list here since the caller | |
2862 | * (process_client_msg) is holding it. We might want to change that so a | |
2863 | * single command does not lock the entire session list. | |
2864 | */ | |
2865 | session = session_find_by_name(name); | |
2866 | assert(session); | |
2867 | ||
ecc48a90 | 2868 | session->live_timer = live_timer; |
2f77fc4b DG |
2869 | /* Create default consumer output for the session not yet created. */ |
2870 | session->consumer = consumer_create_output(CONSUMER_DST_LOCAL); | |
2871 | if (session->consumer == NULL) { | |
f73fabfd | 2872 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2873 | goto consumer_error; |
2874 | } | |
2875 | ||
2bba9e53 | 2876 | if (uris) { |
bda32d56 | 2877 | ret = cmd_set_consumer_uri(session, nb_uri, uris); |
2bba9e53 DG |
2878 | if (ret != LTTNG_OK) { |
2879 | goto consumer_error; | |
2880 | } | |
2881 | session->output_traces = 1; | |
2882 | } else { | |
2883 | session->output_traces = 0; | |
2884 | DBG2("Session %s created with no output", session->name); | |
2f77fc4b DG |
2885 | } |
2886 | ||
2887 | session->consumer->enabled = 1; | |
2888 | ||
f73fabfd | 2889 | return LTTNG_OK; |
2f77fc4b DG |
2890 | |
2891 | consumer_error: | |
2892 | session_destroy(session); | |
2893 | session_error: | |
2894 | find_error: | |
2895 | return ret; | |
2896 | } | |
2897 | ||
27babd3a DG |
2898 | /* |
2899 | * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread. | |
2900 | */ | |
2901 | int cmd_create_session_snapshot(char *name, struct lttng_uri *uris, | |
2902 | size_t nb_uri, lttng_sock_cred *creds) | |
2903 | { | |
2904 | int ret; | |
2905 | struct ltt_session *session; | |
2906 | struct snapshot_output *new_output = NULL; | |
2907 | ||
2908 | assert(name); | |
2909 | assert(creds); | |
2910 | ||
2911 | /* | |
2912 | * Create session in no output mode with URIs set to NULL. The uris we've | |
2913 | * received are for a default snapshot output if one. | |
2914 | */ | |
ae58b817 | 2915 | ret = cmd_create_session_uri(name, NULL, 0, creds, 0); |
27babd3a DG |
2916 | if (ret != LTTNG_OK) { |
2917 | goto error; | |
2918 | } | |
2919 | ||
2920 | /* Get the newly created session pointer back. This should NEVER fail. */ | |
2921 | session = session_find_by_name(name); | |
2922 | assert(session); | |
2923 | ||
2924 | /* Flag session for snapshot mode. */ | |
2925 | session->snapshot_mode = 1; | |
2926 | ||
2927 | /* Skip snapshot output creation if no URI is given. */ | |
2928 | if (nb_uri == 0) { | |
2929 | goto end; | |
2930 | } | |
2931 | ||
2932 | new_output = snapshot_output_alloc(); | |
2933 | if (!new_output) { | |
2934 | ret = LTTNG_ERR_NOMEM; | |
2935 | goto error_snapshot_alloc; | |
2936 | } | |
2937 | ||
2938 | ret = snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE, NULL, | |
2939 | uris, nb_uri, session->consumer, new_output, &session->snapshot); | |
2940 | if (ret < 0) { | |
2941 | if (ret == -ENOMEM) { | |
2942 | ret = LTTNG_ERR_NOMEM; | |
2943 | } else { | |
2944 | ret = LTTNG_ERR_INVALID; | |
2945 | } | |
2946 | goto error_snapshot; | |
2947 | } | |
2948 | ||
2949 | rcu_read_lock(); | |
2950 | snapshot_add_output(&session->snapshot, new_output); | |
2951 | rcu_read_unlock(); | |
2952 | ||
2953 | end: | |
2954 | return LTTNG_OK; | |
2955 | ||
2956 | error_snapshot: | |
2957 | snapshot_output_destroy(new_output); | |
2958 | error_snapshot_alloc: | |
2959 | session_destroy(session); | |
2960 | error: | |
2961 | return ret; | |
2962 | } | |
2963 | ||
2f77fc4b DG |
2964 | /* |
2965 | * Command LTTNG_DESTROY_SESSION processed by the client thread. | |
a9ad0c8f MD |
2966 | * |
2967 | * Called with session lock held. | |
2f77fc4b | 2968 | */ |
90936dcf JD |
2969 | int cmd_destroy_session(struct ltt_session *session, int wpipe, |
2970 | struct notification_thread_handle *notification_thread_handle) | |
2f77fc4b DG |
2971 | { |
2972 | int ret; | |
2973 | struct ltt_ust_session *usess; | |
2974 | struct ltt_kernel_session *ksess; | |
2975 | ||
2976 | /* Safety net */ | |
2977 | assert(session); | |
2978 | ||
2979 | usess = session->ust_session; | |
2980 | ksess = session->kernel_session; | |
2981 | ||
5c408ad8 JD |
2982 | DBG("Begin destroy session %s (id %" PRIu64 ")", session->name, session->id); |
2983 | ||
d88744a4 JD |
2984 | if (session->rotate_relay_pending_timer_enabled) { |
2985 | sessiond_timer_rotate_pending_stop(session); | |
2986 | } | |
2987 | ||
259c2674 JD |
2988 | if (session->rotate_timer_enabled) { |
2989 | sessiond_rotate_timer_stop(session); | |
2990 | } | |
2991 | ||
90936dcf JD |
2992 | if (session->rotate_size) { |
2993 | unsubscribe_session_consumed_size_rotation(session, notification_thread_handle); | |
2994 | session->rotate_size = 0; | |
2995 | } | |
2996 | ||
5c408ad8 JD |
2997 | /* |
2998 | * The rename of the current chunk is performed at stop, but if we rotated | |
2999 | * the session after the previous stop command, we need to rename the | |
3000 | * new (and empty) chunk that was started in between. | |
3001 | */ | |
3002 | if (session->rotated_after_last_stop) { | |
3003 | rename_active_chunk(session); | |
3004 | } | |
3005 | ||
2f77fc4b DG |
3006 | /* Clean kernel session teardown */ |
3007 | kernel_destroy_session(ksess); | |
3008 | ||
3009 | /* UST session teardown */ | |
3010 | if (usess) { | |
3011 | /* Close any relayd session */ | |
3012 | consumer_output_send_destroy_relayd(usess->consumer); | |
3013 | ||
3014 | /* Destroy every UST application related to this session. */ | |
3015 | ret = ust_app_destroy_trace_all(usess); | |
3016 | if (ret) { | |
3017 | ERR("Error in ust_app_destroy_trace_all"); | |
3018 | } | |
3019 | ||
3020 | /* Clean up the rest. */ | |
3021 | trace_ust_destroy_session(usess); | |
3022 | } | |
3023 | ||
3024 | /* | |
3025 | * Must notify the kernel thread here to update it's poll set in order to | |
3026 | * remove the channel(s)' fd just destroyed. | |
3027 | */ | |
3028 | ret = notify_thread_pipe(wpipe); | |
3029 | if (ret < 0) { | |
3030 | PERROR("write kernel poll pipe"); | |
3031 | } | |
3032 | ||
3033 | ret = session_destroy(session); | |
3034 | ||
3035 | return ret; | |
3036 | } | |
3037 | ||
2f77fc4b DG |
3038 | /* |
3039 | * Command LTTNG_REGISTER_CONSUMER processed by the client thread. | |
3040 | */ | |
56a37563 JG |
3041 | int cmd_register_consumer(struct ltt_session *session, |
3042 | enum lttng_domain_type domain, const char *sock_path, | |
3043 | struct consumer_data *cdata) | |
2f77fc4b DG |
3044 | { |
3045 | int ret, sock; | |
dd81b457 | 3046 | struct consumer_socket *socket = NULL; |
2f77fc4b DG |
3047 | |
3048 | assert(session); | |
3049 | assert(cdata); | |
3050 | assert(sock_path); | |
3051 | ||
3052 | switch (domain) { | |
3053 | case LTTNG_DOMAIN_KERNEL: | |
3054 | { | |
3055 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3056 | ||
3057 | assert(ksess); | |
3058 | ||
3059 | /* Can't register a consumer if there is already one */ | |
3060 | if (ksess->consumer_fds_sent != 0) { | |
f73fabfd | 3061 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
2f77fc4b DG |
3062 | goto error; |
3063 | } | |
3064 | ||
3065 | sock = lttcomm_connect_unix_sock(sock_path); | |
3066 | if (sock < 0) { | |
f73fabfd | 3067 | ret = LTTNG_ERR_CONNECT_FAIL; |
2f77fc4b DG |
3068 | goto error; |
3069 | } | |
4ce514c4 | 3070 | cdata->cmd_sock = sock; |
2f77fc4b | 3071 | |
4ce514c4 | 3072 | socket = consumer_allocate_socket(&cdata->cmd_sock); |
2f77fc4b | 3073 | if (socket == NULL) { |
f66c074c DG |
3074 | ret = close(sock); |
3075 | if (ret < 0) { | |
3076 | PERROR("close register consumer"); | |
3077 | } | |
4ce514c4 | 3078 | cdata->cmd_sock = -1; |
f73fabfd | 3079 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
3080 | goto error; |
3081 | } | |
3082 | ||
3083 | socket->lock = zmalloc(sizeof(pthread_mutex_t)); | |
3084 | if (socket->lock == NULL) { | |
3085 | PERROR("zmalloc pthread mutex"); | |
f73fabfd | 3086 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
3087 | goto error; |
3088 | } | |
3089 | pthread_mutex_init(socket->lock, NULL); | |
3090 | socket->registered = 1; | |
3091 | ||
3092 | rcu_read_lock(); | |
3093 | consumer_add_socket(socket, ksess->consumer); | |
3094 | rcu_read_unlock(); | |
3095 | ||
3096 | pthread_mutex_lock(&cdata->pid_mutex); | |
3097 | cdata->pid = -1; | |
3098 | pthread_mutex_unlock(&cdata->pid_mutex); | |
3099 | ||
3100 | break; | |
3101 | } | |
3102 | default: | |
3103 | /* TODO: Userspace tracing */ | |
f73fabfd | 3104 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
3105 | goto error; |
3106 | } | |
3107 | ||
dd81b457 | 3108 | return LTTNG_OK; |
2f77fc4b DG |
3109 | |
3110 | error: | |
dd81b457 DG |
3111 | if (socket) { |
3112 | consumer_destroy_socket(socket); | |
3113 | } | |
2f77fc4b DG |
3114 | return ret; |
3115 | } | |
3116 | ||
3117 | /* | |
3118 | * Command LTTNG_LIST_DOMAINS processed by the client thread. | |
3119 | */ | |
3120 | ssize_t cmd_list_domains(struct ltt_session *session, | |
3121 | struct lttng_domain **domains) | |
3122 | { | |
3123 | int ret, index = 0; | |
3124 | ssize_t nb_dom = 0; | |
fefd409b DG |
3125 | struct agent *agt; |
3126 | struct lttng_ht_iter iter; | |
2f77fc4b DG |
3127 | |
3128 | if (session->kernel_session != NULL) { | |
3129 | DBG3("Listing domains found kernel domain"); | |
3130 | nb_dom++; | |
3131 | } | |
3132 | ||
3133 | if (session->ust_session != NULL) { | |
3134 | DBG3("Listing domains found UST global domain"); | |
3135 | nb_dom++; | |
3c6a091f | 3136 | |
e0a74f01 | 3137 | rcu_read_lock(); |
fefd409b DG |
3138 | cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, |
3139 | agt, node.node) { | |
3140 | if (agt->being_used) { | |
3141 | nb_dom++; | |
3142 | } | |
3c6a091f | 3143 | } |
e0a74f01 | 3144 | rcu_read_unlock(); |
2f77fc4b DG |
3145 | } |
3146 | ||
fa64dfb4 JG |
3147 | if (!nb_dom) { |
3148 | goto end; | |
3149 | } | |
3150 | ||
2f77fc4b DG |
3151 | *domains = zmalloc(nb_dom * sizeof(struct lttng_domain)); |
3152 | if (*domains == NULL) { | |
f73fabfd | 3153 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
3154 | goto error; |
3155 | } | |
3156 | ||
3157 | if (session->kernel_session != NULL) { | |
3158 | (*domains)[index].type = LTTNG_DOMAIN_KERNEL; | |
b5edb9e8 PP |
3159 | |
3160 | /* Kernel session buffer type is always GLOBAL */ | |
3161 | (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL; | |
3162 | ||
2f77fc4b DG |
3163 | index++; |
3164 | } | |
3165 | ||
3166 | if (session->ust_session != NULL) { | |
3167 | (*domains)[index].type = LTTNG_DOMAIN_UST; | |
88c5f0d8 | 3168 | (*domains)[index].buf_type = session->ust_session->buffer_type; |
2f77fc4b | 3169 | index++; |
3c6a091f | 3170 | |
e0a74f01 | 3171 | rcu_read_lock(); |
fefd409b DG |
3172 | cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, |
3173 | agt, node.node) { | |
3174 | if (agt->being_used) { | |
3175 | (*domains)[index].type = agt->domain; | |
3176 | (*domains)[index].buf_type = session->ust_session->buffer_type; | |
3177 | index++; | |
3178 | } | |
3c6a091f | 3179 | } |
e0a74f01 | 3180 | rcu_read_unlock(); |
2f77fc4b | 3181 | } |
fa64dfb4 | 3182 | end: |
2f77fc4b DG |
3183 | return nb_dom; |
3184 | ||
3185 | error: | |
f73fabfd DG |
3186 | /* Return negative value to differentiate return code */ |
3187 | return -ret; | |
2f77fc4b DG |
3188 | } |
3189 | ||
3190 | ||
3191 | /* | |
3192 | * Command LTTNG_LIST_CHANNELS processed by the client thread. | |
3193 | */ | |
56a37563 JG |
3194 | ssize_t cmd_list_channels(enum lttng_domain_type domain, |
3195 | struct ltt_session *session, struct lttng_channel **channels) | |
2f77fc4b | 3196 | { |
53e367f9 | 3197 | ssize_t nb_chan = 0, payload_size = 0, ret; |
2f77fc4b DG |
3198 | |
3199 | switch (domain) { | |
3200 | case LTTNG_DOMAIN_KERNEL: | |
3201 | if (session->kernel_session != NULL) { | |
3202 | nb_chan = session->kernel_session->channel_count; | |
3203 | } | |
3204 | DBG3("Number of kernel channels %zd", nb_chan); | |
c7d620a2 | 3205 | if (nb_chan <= 0) { |
53e367f9 JG |
3206 | ret = -LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
3207 | goto end; | |
c7d620a2 | 3208 | } |
2f77fc4b DG |
3209 | break; |
3210 | case LTTNG_DOMAIN_UST: | |
3211 | if (session->ust_session != NULL) { | |
7ffc31a2 | 3212 | rcu_read_lock(); |
2f77fc4b | 3213 | nb_chan = lttng_ht_get_count( |
7ffc31a2 JG |
3214 | session->ust_session->domain_global.channels); |
3215 | rcu_read_unlock(); | |
2f77fc4b DG |
3216 | } |
3217 | DBG3("Number of UST global channels %zd", nb_chan); | |
ade7ce52 | 3218 | if (nb_chan < 0) { |
53e367f9 JG |
3219 | ret = -LTTNG_ERR_UST_CHAN_NOT_FOUND; |
3220 | goto end; | |
c7d620a2 | 3221 | } |
2f77fc4b DG |
3222 | break; |
3223 | default: | |
53e367f9 JG |
3224 | ret = -LTTNG_ERR_UND; |
3225 | goto end; | |
2f77fc4b DG |
3226 | } |
3227 | ||
3228 | if (nb_chan > 0) { | |
53e367f9 | 3229 | const size_t channel_size = sizeof(struct lttng_channel) + |
cf0bcb51 JG |
3230 | sizeof(struct lttng_channel_extended); |
3231 | struct lttng_channel_extended *channel_exts; | |
53e367f9 JG |
3232 | |
3233 | payload_size = nb_chan * channel_size; | |
3234 | *channels = zmalloc(payload_size); | |
2f77fc4b | 3235 | if (*channels == NULL) { |
53e367f9 JG |
3236 | ret = -LTTNG_ERR_FATAL; |
3237 | goto end; | |
2f77fc4b DG |
3238 | } |
3239 | ||
53e367f9 JG |
3240 | channel_exts = ((void *) *channels) + |
3241 | (nb_chan * sizeof(struct lttng_channel)); | |
d449df4a MD |
3242 | ret = list_lttng_channels(domain, session, *channels, channel_exts); |
3243 | if (ret != LTTNG_OK) { | |
3244 | free(*channels); | |
3245 | *channels = NULL; | |
3246 | goto end; | |
3247 | } | |
53e367f9 JG |
3248 | } else { |
3249 | *channels = NULL; | |
2f77fc4b DG |
3250 | } |
3251 | ||
53e367f9 JG |
3252 | ret = payload_size; |
3253 | end: | |
3254 | return ret; | |
2f77fc4b DG |
3255 | } |
3256 | ||
3257 | /* | |
3258 | * Command LTTNG_LIST_EVENTS processed by the client thread. | |
3259 | */ | |
56a37563 JG |
3260 | ssize_t cmd_list_events(enum lttng_domain_type domain, |
3261 | struct ltt_session *session, char *channel_name, | |
b4e3ceb9 | 3262 | struct lttng_event **events, size_t *total_size) |
2f77fc4b DG |
3263 | { |
3264 | int ret = 0; | |
3265 | ssize_t nb_event = 0; | |
3266 | ||
3267 | switch (domain) { | |
3268 | case LTTNG_DOMAIN_KERNEL: | |
3269 | if (session->kernel_session != NULL) { | |
3270 | nb_event = list_lttng_kernel_events(channel_name, | |
b4e3ceb9 PP |
3271 | session->kernel_session, events, |
3272 | total_size); | |
2f77fc4b DG |
3273 | } |
3274 | break; | |
3275 | case LTTNG_DOMAIN_UST: | |
3276 | { | |
3277 | if (session->ust_session != NULL) { | |
3278 | nb_event = list_lttng_ust_global_events(channel_name, | |
b4e3ceb9 PP |
3279 | &session->ust_session->domain_global, events, |
3280 | total_size); | |
2f77fc4b DG |
3281 | } |
3282 | break; | |
3283 | } | |
5cdb6027 | 3284 | case LTTNG_DOMAIN_LOG4J: |
3c6a091f | 3285 | case LTTNG_DOMAIN_JUL: |
0e115563 | 3286 | case LTTNG_DOMAIN_PYTHON: |
3c6a091f | 3287 | if (session->ust_session) { |
fefd409b DG |
3288 | struct lttng_ht_iter iter; |
3289 | struct agent *agt; | |
3290 | ||
b11feea5 | 3291 | rcu_read_lock(); |
fefd409b DG |
3292 | cds_lfht_for_each_entry(session->ust_session->agents->ht, |
3293 | &iter.iter, agt, node.node) { | |
1dfd9906 JG |
3294 | if (agt->domain == domain) { |
3295 | nb_event = list_lttng_agent_events( | |
b4e3ceb9 PP |
3296 | agt, events, |
3297 | total_size); | |
1dfd9906 JG |
3298 | break; |
3299 | } | |
fefd409b | 3300 | } |
b11feea5 | 3301 | rcu_read_unlock(); |
3c6a091f DG |
3302 | } |
3303 | break; | |
2f77fc4b | 3304 | default: |
f73fabfd | 3305 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
3306 | goto error; |
3307 | } | |
3308 | ||
f73fabfd | 3309 | return nb_event; |
2f77fc4b DG |
3310 | |
3311 | error: | |
f73fabfd DG |
3312 | /* Return negative value to differentiate return code */ |
3313 | return -ret; | |
2f77fc4b DG |
3314 | } |
3315 | ||
3316 | /* | |
3317 | * Using the session list, filled a lttng_session array to send back to the | |
3318 | * client for session listing. | |
3319 | * | |
3320 | * The session list lock MUST be acquired before calling this function. Use | |
3321 | * session_lock_list() and session_unlock_list(). | |
3322 | */ | |
3323 | void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid, | |
3324 | gid_t gid) | |
3325 | { | |
3326 | int ret; | |
3327 | unsigned int i = 0; | |
3328 | struct ltt_session *session; | |
3329 | struct ltt_session_list *list = session_get_list(); | |
3330 | ||
3331 | DBG("Getting all available session for UID %d GID %d", | |
3332 | uid, gid); | |
3333 | /* | |
3334 | * Iterate over session list and append data after the control struct in | |
3335 | * the buffer. | |
3336 | */ | |
3337 | cds_list_for_each_entry(session, &list->head, list) { | |
3338 | /* | |
3339 | * Only list the sessions the user can control. | |
3340 | */ | |
3341 | if (!session_access_ok(session, uid, gid)) { | |
3342 | continue; | |
3343 | } | |
3344 | ||
3345 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3346 | struct ltt_ust_session *usess = session->ust_session; | |
3347 | ||
3348 | if (session->consumer->type == CONSUMER_DST_NET || | |
3349 | (ksess && ksess->consumer->type == CONSUMER_DST_NET) || | |
3350 | (usess && usess->consumer->type == CONSUMER_DST_NET)) { | |
3351 | ret = build_network_session_path(sessions[i].path, | |
dec56f6c | 3352 | sizeof(sessions[i].path), session); |
2f77fc4b | 3353 | } else { |
dec56f6c | 3354 | ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s", |
366a9222 | 3355 | session->consumer->dst.session_root_path); |
2f77fc4b DG |
3356 | } |
3357 | if (ret < 0) { | |
3358 | PERROR("snprintf session path"); | |
3359 | continue; | |
3360 | } | |
3361 | ||
3362 | strncpy(sessions[i].name, session->name, NAME_MAX); | |
3363 | sessions[i].name[NAME_MAX - 1] = '\0'; | |
8382cf6f | 3364 | sessions[i].enabled = session->active; |
2cbf8fed | 3365 | sessions[i].snapshot_mode = session->snapshot_mode; |
8960e9cd | 3366 | sessions[i].live_timer_interval = session->live_timer; |
2f77fc4b DG |
3367 | i++; |
3368 | } | |
3369 | } | |
3370 | ||
806e2684 | 3371 | /* |
6d805429 | 3372 | * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning |
d3f14b8a | 3373 | * ready for trace analysis (or any kind of reader) or else 1 for pending data. |
806e2684 | 3374 | */ |
6d805429 | 3375 | int cmd_data_pending(struct ltt_session *session) |
806e2684 DG |
3376 | { |
3377 | int ret; | |
3378 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3379 | struct ltt_ust_session *usess = session->ust_session; | |
3380 | ||
3381 | assert(session); | |
3382 | ||
5c408ad8 JD |
3383 | DBG("Data pending for session %s", session->name); |
3384 | ||
806e2684 | 3385 | /* Session MUST be stopped to ask for data availability. */ |
8382cf6f | 3386 | if (session->active) { |
806e2684 DG |
3387 | ret = LTTNG_ERR_SESSION_STARTED; |
3388 | goto error; | |
3a89d11a DG |
3389 | } else { |
3390 | /* | |
3391 | * If stopped, just make sure we've started before else the above call | |
3392 | * will always send that there is data pending. | |
3393 | * | |
3394 | * The consumer assumes that when the data pending command is received, | |
3395 | * the trace has been started before or else no output data is written | |
3396 | * by the streams which is a condition for data pending. So, this is | |
3397 | * *VERY* important that we don't ask the consumer before a start | |
3398 | * trace. | |
3399 | */ | |
8382cf6f | 3400 | if (!session->has_been_started) { |
3a89d11a DG |
3401 | ret = 0; |
3402 | goto error; | |
3403 | } | |
806e2684 DG |
3404 | } |
3405 | ||
5c408ad8 JD |
3406 | /* |
3407 | * A rotation is still pending, we have to wait. | |
3408 | */ | |
3409 | if (session->rotate_pending) { | |
3410 | DBG("Rotate still pending for session %s", session->name); | |
3411 | ret = 1; | |
3412 | goto error; | |
3413 | } | |
3414 | ||
806e2684 | 3415 | if (ksess && ksess->consumer) { |
6d805429 DG |
3416 | ret = consumer_is_data_pending(ksess->id, ksess->consumer); |
3417 | if (ret == 1) { | |
806e2684 DG |
3418 | /* Data is still being extracted for the kernel. */ |
3419 | goto error; | |
3420 | } | |
3421 | } | |
3422 | ||
3423 | if (usess && usess->consumer) { | |
6d805429 DG |
3424 | ret = consumer_is_data_pending(usess->id, usess->consumer); |
3425 | if (ret == 1) { | |
806e2684 DG |
3426 | /* Data is still being extracted for the kernel. */ |
3427 | goto error; | |
3428 | } | |
3429 | } | |
3430 | ||
3431 | /* Data is ready to be read by a viewer */ | |
6d805429 | 3432 | ret = 0; |
806e2684 DG |
3433 | |
3434 | error: | |
3435 | return ret; | |
3436 | } | |
3437 | ||
6dc3064a DG |
3438 | /* |
3439 | * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library. | |
3440 | * | |
3441 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
3442 | */ | |
3443 | int cmd_snapshot_add_output(struct ltt_session *session, | |
3444 | struct lttng_snapshot_output *output, uint32_t *id) | |
3445 | { | |
3446 | int ret; | |
3447 | struct snapshot_output *new_output; | |
3448 | ||
3449 | assert(session); | |
3450 | assert(output); | |
3451 | ||
3452 | DBG("Cmd snapshot add output for session %s", session->name); | |
3453 | ||
3454 | /* | |
903ef685 | 3455 | * Can't create an output if the session is not set in no-output mode. |
6dc3064a DG |
3456 | */ |
3457 | if (session->output_traces) { | |
903ef685 | 3458 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
3459 | goto error; |
3460 | } | |
3461 | ||
3462 | /* Only one output is allowed until we have the "tee" feature. */ | |
3463 | if (session->snapshot.nb_output == 1) { | |
3464 | ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST; | |
3465 | goto error; | |
3466 | } | |
3467 | ||
3468 | new_output = snapshot_output_alloc(); | |
3469 | if (!new_output) { | |
3470 | ret = LTTNG_ERR_NOMEM; | |
3471 | goto error; | |
3472 | } | |
3473 | ||
3474 | ret = snapshot_output_init(output->max_size, output->name, | |
3475 | output->ctrl_url, output->data_url, session->consumer, new_output, | |
3476 | &session->snapshot); | |
3477 | if (ret < 0) { | |
3478 | if (ret == -ENOMEM) { | |
3479 | ret = LTTNG_ERR_NOMEM; | |
3480 | } else { | |
3481 | ret = LTTNG_ERR_INVALID; | |
3482 | } | |
3483 | goto free_error; | |
3484 | } | |
3485 | ||
6dc3064a DG |
3486 | rcu_read_lock(); |
3487 | snapshot_add_output(&session->snapshot, new_output); | |
3488 | if (id) { | |
3489 | *id = new_output->id; | |
3490 | } | |
3491 | rcu_read_unlock(); | |
3492 | ||
3493 | return LTTNG_OK; | |
3494 | ||
3495 | free_error: | |
3496 | snapshot_output_destroy(new_output); | |
3497 | error: | |
3498 | return ret; | |
3499 | } | |
3500 | ||
3501 | /* | |
3502 | * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl. | |
3503 | * | |
3504 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
3505 | */ | |
3506 | int cmd_snapshot_del_output(struct ltt_session *session, | |
3507 | struct lttng_snapshot_output *output) | |
3508 | { | |
3509 | int ret; | |
eb240553 | 3510 | struct snapshot_output *sout = NULL; |
6dc3064a DG |
3511 | |
3512 | assert(session); | |
3513 | assert(output); | |
3514 | ||
6dc3064a DG |
3515 | rcu_read_lock(); |
3516 | ||
3517 | /* | |
d3f14b8a MD |
3518 | * Permission denied to create an output if the session is not |
3519 | * set in no output mode. | |
6dc3064a DG |
3520 | */ |
3521 | if (session->output_traces) { | |
903ef685 | 3522 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
3523 | goto error; |
3524 | } | |
3525 | ||
eb240553 DG |
3526 | if (output->id) { |
3527 | DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id, | |
3528 | session->name); | |
3529 | sout = snapshot_find_output_by_id(output->id, &session->snapshot); | |
3530 | } else if (*output->name != '\0') { | |
3531 | DBG("Cmd snapshot del output name %s for session %s", output->name, | |
3532 | session->name); | |
3533 | sout = snapshot_find_output_by_name(output->name, &session->snapshot); | |
3534 | } | |
6dc3064a DG |
3535 | if (!sout) { |
3536 | ret = LTTNG_ERR_INVALID; | |
3537 | goto error; | |
3538 | } | |
3539 | ||
3540 | snapshot_delete_output(&session->snapshot, sout); | |
3541 | snapshot_output_destroy(sout); | |
3542 | ret = LTTNG_OK; | |
3543 | ||
3544 | error: | |
3545 | rcu_read_unlock(); | |
3546 | return ret; | |
3547 | } | |
3548 | ||
3549 | /* | |
3550 | * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl. | |
3551 | * | |
3552 | * If no output is available, outputs is untouched and 0 is returned. | |
3553 | * | |
3554 | * Return the size of the newly allocated outputs or a negative LTTNG_ERR code. | |
3555 | */ | |
3556 | ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, | |
3557 | struct lttng_snapshot_output **outputs) | |
3558 | { | |
3559 | int ret, idx = 0; | |
b223ca94 | 3560 | struct lttng_snapshot_output *list = NULL; |
6dc3064a DG |
3561 | struct lttng_ht_iter iter; |
3562 | struct snapshot_output *output; | |
3563 | ||
3564 | assert(session); | |
3565 | assert(outputs); | |
3566 | ||
3567 | DBG("Cmd snapshot list outputs for session %s", session->name); | |
3568 | ||
3569 | /* | |
d3f14b8a MD |
3570 | * Permission denied to create an output if the session is not |
3571 | * set in no output mode. | |
6dc3064a DG |
3572 | */ |
3573 | if (session->output_traces) { | |
903ef685 JG |
3574 | ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
3575 | goto end; | |
6dc3064a DG |
3576 | } |
3577 | ||
3578 | if (session->snapshot.nb_output == 0) { | |
3579 | ret = 0; | |
903ef685 | 3580 | goto end; |
6dc3064a DG |
3581 | } |
3582 | ||
3583 | list = zmalloc(session->snapshot.nb_output * sizeof(*list)); | |
3584 | if (!list) { | |
b223ca94 | 3585 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 3586 | goto end; |
6dc3064a DG |
3587 | } |
3588 | ||
3589 | /* Copy list from session to the new list object. */ | |
b223ca94 | 3590 | rcu_read_lock(); |
6dc3064a DG |
3591 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter, |
3592 | output, node.node) { | |
3593 | assert(output->consumer); | |
3594 | list[idx].id = output->id; | |
3595 | list[idx].max_size = output->max_size; | |
6ce22875 MD |
3596 | if (lttng_strncpy(list[idx].name, output->name, |
3597 | sizeof(list[idx].name))) { | |
3598 | ret = -LTTNG_ERR_INVALID; | |
903ef685 | 3599 | goto error; |
6ce22875 | 3600 | } |
6dc3064a | 3601 | if (output->consumer->type == CONSUMER_DST_LOCAL) { |
6ce22875 | 3602 | if (lttng_strncpy(list[idx].ctrl_url, |
366a9222 | 3603 | output->consumer->dst.session_root_path, |
6ce22875 MD |
3604 | sizeof(list[idx].ctrl_url))) { |
3605 | ret = -LTTNG_ERR_INVALID; | |
903ef685 | 3606 | goto error; |
6ce22875 | 3607 | } |
6dc3064a DG |
3608 | } else { |
3609 | /* Control URI. */ | |
3610 | ret = uri_to_str_url(&output->consumer->dst.net.control, | |
3611 | list[idx].ctrl_url, sizeof(list[idx].ctrl_url)); | |
3612 | if (ret < 0) { | |
b223ca94 | 3613 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 3614 | goto error; |
6dc3064a DG |
3615 | } |
3616 | ||
3617 | /* Data URI. */ | |
3618 | ret = uri_to_str_url(&output->consumer->dst.net.data, | |
3619 | list[idx].data_url, sizeof(list[idx].data_url)); | |
3620 | if (ret < 0) { | |
b223ca94 | 3621 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 3622 | goto error; |
6dc3064a DG |
3623 | } |
3624 | } | |
3625 | idx++; | |
3626 | } | |
3627 | ||
3628 | *outputs = list; | |
b223ca94 JG |
3629 | list = NULL; |
3630 | ret = session->snapshot.nb_output; | |
6dc3064a | 3631 | error: |
903ef685 | 3632 | rcu_read_unlock(); |
b223ca94 | 3633 | free(list); |
903ef685 | 3634 | end: |
b223ca94 | 3635 | return ret; |
6dc3064a DG |
3636 | } |
3637 | ||
93ec662e JD |
3638 | /* |
3639 | * Check if we can regenerate the metadata for this session. | |
3640 | * Only kernel, UST per-uid and non-live sessions are supported. | |
3641 | * | |
3642 | * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise. | |
3643 | */ | |
3644 | static | |
eded6438 | 3645 | int check_regenerate_metadata_support(struct ltt_session *session) |
93ec662e JD |
3646 | { |
3647 | int ret; | |
3648 | ||
3649 | assert(session); | |
3650 | ||
3651 | if (session->live_timer != 0) { | |
3652 | ret = LTTNG_ERR_LIVE_SESSION; | |
3653 | goto end; | |
3654 | } | |
3655 | if (!session->active) { | |
3656 | ret = LTTNG_ERR_SESSION_NOT_STARTED; | |
3657 | goto end; | |
3658 | } | |
3659 | if (session->ust_session) { | |
3660 | switch (session->ust_session->buffer_type) { | |
3661 | case LTTNG_BUFFER_PER_UID: | |
3662 | break; | |
3663 | case LTTNG_BUFFER_PER_PID: | |
3664 | ret = LTTNG_ERR_PER_PID_SESSION; | |
3665 | goto end; | |
3666 | default: | |
3667 | assert(0); | |
3668 | ret = LTTNG_ERR_UNK; | |
3669 | goto end; | |
3670 | } | |
3671 | } | |
3672 | if (session->consumer->type == CONSUMER_DST_NET && | |
3673 | session->consumer->relay_minor_version < 8) { | |
3674 | ret = LTTNG_ERR_RELAYD_VERSION_FAIL; | |
3675 | goto end; | |
3676 | } | |
3677 | ret = 0; | |
3678 | ||
3679 | end: | |
3680 | return ret; | |
3681 | } | |
3682 | ||
7802bae3 LL |
3683 | static |
3684 | int clear_metadata_file(int fd) | |
3685 | { | |
3686 | int ret; | |
a5df8828 | 3687 | off_t lseek_ret; |
7802bae3 | 3688 | |
a5df8828 GL |
3689 | lseek_ret = lseek(fd, 0, SEEK_SET); |
3690 | if (lseek_ret < 0) { | |
7802bae3 | 3691 | PERROR("lseek"); |
a5df8828 | 3692 | ret = -1; |
7802bae3 LL |
3693 | goto end; |
3694 | } | |
3695 | ||
3696 | ret = ftruncate(fd, 0); | |
3697 | if (ret < 0) { | |
3698 | PERROR("ftruncate"); | |
3699 | goto end; | |
3700 | } | |
3701 | ||
3702 | end: | |
3703 | return ret; | |
3704 | } | |
3705 | ||
93ec662e | 3706 | static |
eded6438 | 3707 | int ust_regenerate_metadata(struct ltt_ust_session *usess) |
93ec662e JD |
3708 | { |
3709 | int ret = 0; | |
3710 | struct buffer_reg_uid *uid_reg = NULL; | |
3711 | struct buffer_reg_session *session_reg = NULL; | |
3712 | ||
3713 | rcu_read_lock(); | |
3714 | cds_list_for_each_entry(uid_reg, &usess->buffer_reg_uid_list, lnode) { | |
3715 | struct ust_registry_session *registry; | |
3716 | struct ust_registry_channel *chan; | |
3717 | struct lttng_ht_iter iter_chan; | |
3718 | ||
3719 | session_reg = uid_reg->registry; | |
3720 | registry = session_reg->reg.ust; | |
3721 | ||
3722 | pthread_mutex_lock(®istry->lock); | |
3723 | registry->metadata_len_sent = 0; | |
3724 | memset(registry->metadata, 0, registry->metadata_alloc_len); | |
3725 | registry->metadata_len = 0; | |
3726 | registry->metadata_version++; | |
7802bae3 LL |
3727 | if (registry->metadata_fd > 0) { |
3728 | /* Clear the metadata file's content. */ | |
3729 | ret = clear_metadata_file(registry->metadata_fd); | |
3730 | if (ret) { | |
3731 | pthread_mutex_unlock(®istry->lock); | |
3732 | goto end; | |
3733 | } | |
3734 | } | |
3735 | ||
93ec662e JD |
3736 | ret = ust_metadata_session_statedump(registry, NULL, |
3737 | registry->major, registry->minor); | |
3738 | if (ret) { | |
3739 | pthread_mutex_unlock(®istry->lock); | |
3740 | ERR("Failed to generate session metadata (err = %d)", | |
3741 | ret); | |
3742 | goto end; | |
3743 | } | |
3744 | cds_lfht_for_each_entry(registry->channels->ht, &iter_chan.iter, | |
3745 | chan, node.node) { | |
3746 | struct ust_registry_event *event; | |
3747 | struct lttng_ht_iter iter_event; | |
3748 | ||
3749 | ret = ust_metadata_channel_statedump(registry, chan); | |
3750 | if (ret) { | |
3751 | pthread_mutex_unlock(®istry->lock); | |
3752 | ERR("Failed to generate channel metadata " | |
3753 | "(err = %d)", ret); | |
3754 | goto end; | |
3755 | } | |
3756 | cds_lfht_for_each_entry(chan->ht->ht, &iter_event.iter, | |
3757 | event, node.node) { | |
3758 | ret = ust_metadata_event_statedump(registry, | |
3759 | chan, event); | |
3760 | if (ret) { | |
3761 | pthread_mutex_unlock(®istry->lock); | |
3762 | ERR("Failed to generate event metadata " | |
3763 | "(err = %d)", ret); | |
3764 | goto end; | |
3765 | } | |
3766 | } | |
3767 | } | |
3768 | pthread_mutex_unlock(®istry->lock); | |
3769 | } | |
3770 | ||
3771 | end: | |
3772 | rcu_read_unlock(); | |
3773 | return ret; | |
3774 | } | |
3775 | ||
3776 | /* | |
eded6438 | 3777 | * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library. |
93ec662e JD |
3778 | * |
3779 | * Ask the consumer to truncate the existing metadata file(s) and | |
3780 | * then regenerate the metadata. Live and per-pid sessions are not | |
3781 | * supported and return an error. | |
3782 | * | |
3783 | * Return 0 on success or else a LTTNG_ERR code. | |
3784 | */ | |
eded6438 | 3785 | int cmd_regenerate_metadata(struct ltt_session *session) |
93ec662e JD |
3786 | { |
3787 | int ret; | |
3788 | ||
3789 | assert(session); | |
3790 | ||
eded6438 | 3791 | ret = check_regenerate_metadata_support(session); |
93ec662e JD |
3792 | if (ret) { |
3793 | goto end; | |
3794 | } | |
3795 | ||
3796 | if (session->kernel_session) { | |
eded6438 | 3797 | ret = kernctl_session_regenerate_metadata( |
93ec662e JD |
3798 | session->kernel_session->fd); |
3799 | if (ret < 0) { | |
3800 | ERR("Failed to regenerate the kernel metadata"); | |
3801 | goto end; | |
3802 | } | |
3803 | } | |
3804 | ||
3805 | if (session->ust_session) { | |
eded6438 | 3806 | ret = ust_regenerate_metadata(session->ust_session); |
93ec662e JD |
3807 | if (ret < 0) { |
3808 | ERR("Failed to regenerate the UST metadata"); | |
3809 | goto end; | |
3810 | } | |
3811 | } | |
3812 | DBG("Cmd metadata regenerate for session %s", session->name); | |
3813 | ret = LTTNG_OK; | |
3814 | ||
3815 | end: | |
3816 | return ret; | |
3817 | } | |
3818 | ||
c2561365 JD |
3819 | /* |
3820 | * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library. | |
3821 | * | |
3822 | * Ask the tracer to regenerate a new statedump. | |
3823 | * | |
3824 | * Return 0 on success or else a LTTNG_ERR code. | |
3825 | */ | |
3826 | int cmd_regenerate_statedump(struct ltt_session *session) | |
3827 | { | |
3828 | int ret; | |
3829 | ||
3830 | assert(session); | |
3831 | ||
3832 | if (!session->active) { | |
3833 | ret = LTTNG_ERR_SESSION_NOT_STARTED; | |
3834 | goto end; | |
3835 | } | |
c2561365 JD |
3836 | |
3837 | if (session->kernel_session) { | |
3838 | ret = kernctl_session_regenerate_statedump( | |
3839 | session->kernel_session->fd); | |
3840 | /* | |
3841 | * Currently, the statedump in kernel can only fail if out | |
3842 | * of memory. | |
3843 | */ | |
3844 | if (ret < 0) { | |
3845 | if (ret == -ENOMEM) { | |
3846 | ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM; | |
3847 | } else { | |
3848 | ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL; | |
3849 | } | |
3850 | ERR("Failed to regenerate the kernel statedump"); | |
3851 | goto end; | |
3852 | } | |
3853 | } | |
3854 | ||
3855 | if (session->ust_session) { | |
3856 | ret = ust_app_regenerate_statedump_all(session->ust_session); | |
3857 | /* | |
3858 | * Currently, the statedump in UST always returns 0. | |
3859 | */ | |
3860 | if (ret < 0) { | |
3861 | ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL; | |
3862 | ERR("Failed to regenerate the UST statedump"); | |
3863 | goto end; | |
3864 | } | |
3865 | } | |
3866 | DBG("Cmd regenerate statedump for session %s", session->name); | |
3867 | ret = LTTNG_OK; | |
3868 | ||
3869 | end: | |
3870 | return ret; | |
3871 | } | |
3872 | ||
b0880ae5 JG |
3873 | int cmd_register_trigger(struct command_ctx *cmd_ctx, int sock, |
3874 | struct notification_thread_handle *notification_thread) | |
3875 | { | |
3876 | int ret; | |
3877 | size_t trigger_len; | |
3878 | ssize_t sock_recv_len; | |
3879 | struct lttng_trigger *trigger = NULL; | |
3880 | struct lttng_buffer_view view; | |
3881 | struct lttng_dynamic_buffer trigger_buffer; | |
3882 | ||
3883 | lttng_dynamic_buffer_init(&trigger_buffer); | |
3884 | trigger_len = (size_t) cmd_ctx->lsm->u.trigger.length; | |
3885 | ret = lttng_dynamic_buffer_set_size(&trigger_buffer, trigger_len); | |
3886 | if (ret) { | |
3887 | ret = LTTNG_ERR_NOMEM; | |
3888 | goto end; | |
3889 | } | |
3890 | ||
3891 | sock_recv_len = lttcomm_recv_unix_sock(sock, trigger_buffer.data, | |
3892 | trigger_len); | |
3893 | if (sock_recv_len < 0 || sock_recv_len != trigger_len) { | |
3894 | ERR("Failed to receive \"register trigger\" command payload"); | |
3895 | /* TODO: should this be a new error enum ? */ | |
3896 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
3897 | goto end; | |
3898 | } | |
3899 | ||
3900 | view = lttng_buffer_view_from_dynamic_buffer(&trigger_buffer, 0, -1); | |
3901 | if (lttng_trigger_create_from_buffer(&view, &trigger) != | |
3902 | trigger_len) { | |
3903 | ERR("Invalid trigger payload received in \"register trigger\" command"); | |
3904 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
3905 | goto end; | |
3906 | } | |
3907 | ||
3908 | ret = notification_thread_command_register_trigger(notification_thread, | |
3909 | trigger); | |
587f9fd0 JG |
3910 | /* Ownership of trigger was transferred. */ |
3911 | trigger = NULL; | |
b0880ae5 | 3912 | end: |
587f9fd0 | 3913 | lttng_trigger_destroy(trigger); |
b0880ae5 JG |
3914 | lttng_dynamic_buffer_reset(&trigger_buffer); |
3915 | return ret; | |
3916 | } | |
3917 | ||
3918 | int cmd_unregister_trigger(struct command_ctx *cmd_ctx, int sock, | |
3919 | struct notification_thread_handle *notification_thread) | |
3920 | { | |
3921 | int ret; | |
3922 | size_t trigger_len; | |
3923 | ssize_t sock_recv_len; | |
3924 | struct lttng_trigger *trigger = NULL; | |
3925 | struct lttng_buffer_view view; | |
3926 | struct lttng_dynamic_buffer trigger_buffer; | |
3927 | ||
3928 | lttng_dynamic_buffer_init(&trigger_buffer); | |
3929 | trigger_len = (size_t) cmd_ctx->lsm->u.trigger.length; | |
3930 | ret = lttng_dynamic_buffer_set_size(&trigger_buffer, trigger_len); | |
3931 | if (ret) { | |
3932 | ret = LTTNG_ERR_NOMEM; | |
3933 | goto end; | |
3934 | } | |
3935 | ||
3936 | sock_recv_len = lttcomm_recv_unix_sock(sock, trigger_buffer.data, | |
3937 | trigger_len); | |
3938 | if (sock_recv_len < 0 || sock_recv_len != trigger_len) { | |
3939 | ERR("Failed to receive \"unregister trigger\" command payload"); | |
3940 | /* TODO: should this be a new error enum ? */ | |
3941 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
3942 | goto end; | |
3943 | } | |
3944 | ||
3945 | view = lttng_buffer_view_from_dynamic_buffer(&trigger_buffer, 0, -1); | |
3946 | if (lttng_trigger_create_from_buffer(&view, &trigger) != | |
3947 | trigger_len) { | |
3948 | ERR("Invalid trigger payload received in \"unregister trigger\" command"); | |
3949 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
3950 | goto end; | |
3951 | } | |
3952 | ||
3953 | ret = notification_thread_command_unregister_trigger(notification_thread, | |
3954 | trigger); | |
3955 | end: | |
587f9fd0 | 3956 | lttng_trigger_destroy(trigger); |
b0880ae5 JG |
3957 | lttng_dynamic_buffer_reset(&trigger_buffer); |
3958 | return ret; | |
3959 | } | |
3960 | ||
6dc3064a DG |
3961 | /* |
3962 | * Send relayd sockets from snapshot output to consumer. Ignore request if the | |
3963 | * snapshot output is *not* set with a remote destination. | |
3964 | * | |
a934f4af | 3965 | * Return 0 on success or a LTTNG_ERR code. |
6dc3064a DG |
3966 | */ |
3967 | static int set_relayd_for_snapshot(struct consumer_output *consumer, | |
3968 | struct snapshot_output *snap_output, struct ltt_session *session) | |
3969 | { | |
a934f4af | 3970 | int ret = LTTNG_OK; |
6dc3064a DG |
3971 | struct lttng_ht_iter iter; |
3972 | struct consumer_socket *socket; | |
3973 | ||
3974 | assert(consumer); | |
3975 | assert(snap_output); | |
3976 | assert(session); | |
3977 | ||
3978 | DBG2("Set relayd object from snapshot output"); | |
3979 | ||
3980 | /* Ignore if snapshot consumer output is not network. */ | |
3981 | if (snap_output->consumer->type != CONSUMER_DST_NET) { | |
3982 | goto error; | |
3983 | } | |
3984 | ||
3985 | /* | |
3986 | * For each consumer socket, create and send the relayd object of the | |
3987 | * snapshot output. | |
3988 | */ | |
3989 | rcu_read_lock(); | |
5eecee74 DG |
3990 | cds_lfht_for_each_entry(snap_output->consumer->socks->ht, &iter.iter, |
3991 | socket, node.node) { | |
ecd0f96d | 3992 | pthread_mutex_lock(socket->lock); |
6dc3064a | 3993 | ret = send_consumer_relayd_sockets(0, session->id, |
d3e2ba59 JD |
3994 | snap_output->consumer, socket, |
3995 | session->name, session->hostname, | |
3996 | session->live_timer); | |
ecd0f96d | 3997 | pthread_mutex_unlock(socket->lock); |
a934f4af | 3998 | if (ret != LTTNG_OK) { |
6dc3064a DG |
3999 | rcu_read_unlock(); |
4000 | goto error; | |
4001 | } | |
4002 | } | |
4003 | rcu_read_unlock(); | |
4004 | ||
4005 | error: | |
4006 | return ret; | |
4007 | } | |
4008 | ||
4009 | /* | |
4010 | * Record a kernel snapshot. | |
4011 | * | |
fac41e72 | 4012 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
6dc3064a DG |
4013 | */ |
4014 | static int record_kernel_snapshot(struct ltt_kernel_session *ksess, | |
5c786ded | 4015 | struct snapshot_output *output, struct ltt_session *session, |
d07ceecd | 4016 | int wait, uint64_t nb_packets_per_stream) |
6dc3064a DG |
4017 | { |
4018 | int ret; | |
4019 | ||
4020 | assert(ksess); | |
4021 | assert(output); | |
4022 | assert(session); | |
4023 | ||
10a50311 | 4024 | |
af706bb7 DG |
4025 | /* |
4026 | * Copy kernel session sockets so we can communicate with the right | |
4027 | * consumer for the snapshot record command. | |
4028 | */ | |
4029 | ret = consumer_copy_sockets(output->consumer, ksess->consumer); | |
4030 | if (ret < 0) { | |
a934f4af | 4031 | ret = LTTNG_ERR_NOMEM; |
af706bb7 | 4032 | goto error; |
6dc3064a DG |
4033 | } |
4034 | ||
4035 | ret = set_relayd_for_snapshot(ksess->consumer, output, session); | |
a934f4af | 4036 | if (ret != LTTNG_OK) { |
af706bb7 | 4037 | goto error_snapshot; |
6dc3064a DG |
4038 | } |
4039 | ||
d07ceecd | 4040 | ret = kernel_snapshot_record(ksess, output, wait, nb_packets_per_stream); |
2a06df8d | 4041 | if (ret != LTTNG_OK) { |
af706bb7 | 4042 | goto error_snapshot; |
6dc3064a DG |
4043 | } |
4044 | ||
a934f4af | 4045 | ret = LTTNG_OK; |
1371fc12 | 4046 | goto end; |
a934f4af | 4047 | |
af706bb7 DG |
4048 | error_snapshot: |
4049 | /* Clean up copied sockets so this output can use some other later on. */ | |
4050 | consumer_destroy_output_sockets(output->consumer); | |
6dc3064a | 4051 | error: |
1371fc12 | 4052 | end: |
6dc3064a DG |
4053 | return ret; |
4054 | } | |
4055 | ||
4056 | /* | |
4057 | * Record a UST snapshot. | |
4058 | * | |
a934f4af | 4059 | * Return 0 on success or a LTTNG_ERR error code. |
6dc3064a DG |
4060 | */ |
4061 | static int record_ust_snapshot(struct ltt_ust_session *usess, | |
5c786ded | 4062 | struct snapshot_output *output, struct ltt_session *session, |
d07ceecd | 4063 | int wait, uint64_t nb_packets_per_stream) |
6dc3064a DG |
4064 | { |
4065 | int ret; | |
4066 | ||
4067 | assert(usess); | |
4068 | assert(output); | |
4069 | assert(session); | |
4070 | ||
af706bb7 | 4071 | /* |
d3f14b8a | 4072 | * Copy UST session sockets so we can communicate with the right |
af706bb7 DG |
4073 | * consumer for the snapshot record command. |
4074 | */ | |
4075 | ret = consumer_copy_sockets(output->consumer, usess->consumer); | |
4076 | if (ret < 0) { | |
a934f4af | 4077 | ret = LTTNG_ERR_NOMEM; |
af706bb7 | 4078 | goto error; |
6dc3064a DG |
4079 | } |
4080 | ||
4081 | ret = set_relayd_for_snapshot(usess->consumer, output, session); | |
a934f4af | 4082 | if (ret != LTTNG_OK) { |
af706bb7 | 4083 | goto error_snapshot; |
6dc3064a DG |
4084 | } |
4085 | ||
d07ceecd | 4086 | ret = ust_app_snapshot_record(usess, output, wait, nb_packets_per_stream); |
6dc3064a | 4087 | if (ret < 0) { |
7badf927 DG |
4088 | switch (-ret) { |
4089 | case EINVAL: | |
a934f4af | 4090 | ret = LTTNG_ERR_INVALID; |
7badf927 | 4091 | break; |
7badf927 DG |
4092 | default: |
4093 | ret = LTTNG_ERR_SNAPSHOT_FAIL; | |
4094 | break; | |
5c786ded | 4095 | } |
af706bb7 | 4096 | goto error_snapshot; |
6dc3064a DG |
4097 | } |
4098 | ||
a934f4af DG |
4099 | ret = LTTNG_OK; |
4100 | ||
af706bb7 DG |
4101 | error_snapshot: |
4102 | /* Clean up copied sockets so this output can use some other later on. */ | |
4103 | consumer_destroy_output_sockets(output->consumer); | |
6dc3064a DG |
4104 | error: |
4105 | return ret; | |
4106 | } | |
4107 | ||
d07ceecd MD |
4108 | static |
4109 | uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session *session, | |
4110 | uint64_t cur_nr_packets) | |
68808f4e | 4111 | { |
d07ceecd | 4112 | uint64_t tot_size = 0; |
68808f4e DG |
4113 | |
4114 | if (session->kernel_session) { | |
4115 | struct ltt_kernel_channel *chan; | |
4116 | struct ltt_kernel_session *ksess = session->kernel_session; | |
4117 | ||
68808f4e | 4118 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { |
d07ceecd MD |
4119 | if (cur_nr_packets >= chan->channel->attr.num_subbuf) { |
4120 | /* | |
4121 | * Don't take channel into account if we | |
4122 | * already grab all its packets. | |
4123 | */ | |
4124 | continue; | |
68808f4e | 4125 | } |
d07ceecd MD |
4126 | tot_size += chan->channel->attr.subbuf_size |
4127 | * chan->stream_count; | |
68808f4e DG |
4128 | } |
4129 | } | |
4130 | ||
4131 | if (session->ust_session) { | |
68808f4e DG |
4132 | struct ltt_ust_session *usess = session->ust_session; |
4133 | ||
d07ceecd MD |
4134 | tot_size += ust_app_get_size_one_more_packet_per_stream(usess, |
4135 | cur_nr_packets); | |
68808f4e DG |
4136 | } |
4137 | ||
d07ceecd | 4138 | return tot_size; |
68808f4e DG |
4139 | } |
4140 | ||
5c786ded | 4141 | /* |
d07ceecd MD |
4142 | * Calculate the number of packets we can grab from each stream that |
4143 | * fits within the overall snapshot max size. | |
4144 | * | |
4145 | * Returns -1 on error, 0 means infinite number of packets, else > 0 is | |
4146 | * the number of packets per stream. | |
4147 | * | |
4148 | * TODO: this approach is not perfect: we consider the worse case | |
4149 | * (packet filling the sub-buffers) as an upper bound, but we could do | |
4150 | * better if we do this calculation while we actually grab the packet | |
4151 | * content: we would know how much padding we don't actually store into | |
4152 | * the file. | |
4153 | * | |
4154 | * This algorithm is currently bounded by the number of packets per | |
4155 | * stream. | |
4156 | * | |
4157 | * Since we call this algorithm before actually grabbing the data, it's | |
4158 | * an approximation: for instance, applications could appear/disappear | |
4159 | * in between this call and actually grabbing data. | |
5c786ded | 4160 | */ |
d07ceecd MD |
4161 | static |
4162 | int64_t get_session_nb_packets_per_stream(struct ltt_session *session, uint64_t max_size) | |
5c786ded | 4163 | { |
d07ceecd MD |
4164 | int64_t size_left; |
4165 | uint64_t cur_nb_packets = 0; | |
5c786ded | 4166 | |
d07ceecd MD |
4167 | if (!max_size) { |
4168 | return 0; /* Infinite */ | |
5c786ded JD |
4169 | } |
4170 | ||
d07ceecd MD |
4171 | size_left = max_size; |
4172 | for (;;) { | |
4173 | uint64_t one_more_packet_tot_size; | |
5c786ded | 4174 | |
d07ceecd MD |
4175 | one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(session, |
4176 | cur_nb_packets); | |
4177 | if (!one_more_packet_tot_size) { | |
4178 | /* We are already grabbing all packets. */ | |
4179 | break; | |
4180 | } | |
4181 | size_left -= one_more_packet_tot_size; | |
4182 | if (size_left < 0) { | |
4183 | break; | |
4184 | } | |
4185 | cur_nb_packets++; | |
5c786ded | 4186 | } |
d07ceecd MD |
4187 | if (!cur_nb_packets) { |
4188 | /* Not enough room to grab one packet of each stream, error. */ | |
4189 | return -1; | |
4190 | } | |
4191 | return cur_nb_packets; | |
5c786ded JD |
4192 | } |
4193 | ||
6dc3064a DG |
4194 | /* |
4195 | * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl. | |
4196 | * | |
4197 | * The wait parameter is ignored so this call always wait for the snapshot to | |
4198 | * complete before returning. | |
4199 | * | |
4200 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
4201 | */ | |
4202 | int cmd_snapshot_record(struct ltt_session *session, | |
4203 | struct lttng_snapshot_output *output, int wait) | |
4204 | { | |
4205 | int ret = LTTNG_OK; | |
e1986656 DG |
4206 | unsigned int use_tmp_output = 0; |
4207 | struct snapshot_output tmp_output; | |
00e1dfc4 | 4208 | unsigned int snapshot_success = 0; |
10ba83fe | 4209 | char datetime[16]; |
6dc3064a DG |
4210 | |
4211 | assert(session); | |
ba45d9f0 | 4212 | assert(output); |
6dc3064a DG |
4213 | |
4214 | DBG("Cmd snapshot record for session %s", session->name); | |
4215 | ||
10ba83fe JR |
4216 | /* Get the datetime for the snapshot output directory. */ |
4217 | ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime, | |
4218 | sizeof(datetime)); | |
4219 | if (!ret) { | |
4220 | ret = LTTNG_ERR_INVALID; | |
4221 | goto error; | |
4222 | } | |
4223 | ||
6dc3064a | 4224 | /* |
d3f14b8a MD |
4225 | * Permission denied to create an output if the session is not |
4226 | * set in no output mode. | |
6dc3064a DG |
4227 | */ |
4228 | if (session->output_traces) { | |
903ef685 | 4229 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
4230 | goto error; |
4231 | } | |
4232 | ||
4233 | /* The session needs to be started at least once. */ | |
8382cf6f | 4234 | if (!session->has_been_started) { |
6dc3064a DG |
4235 | ret = LTTNG_ERR_START_SESSION_ONCE; |
4236 | goto error; | |
4237 | } | |
4238 | ||
4239 | /* Use temporary output for the session. */ | |
ba45d9f0 | 4240 | if (*output->ctrl_url != '\0') { |
6dc3064a DG |
4241 | ret = snapshot_output_init(output->max_size, output->name, |
4242 | output->ctrl_url, output->data_url, session->consumer, | |
e1986656 | 4243 | &tmp_output, NULL); |
6dc3064a DG |
4244 | if (ret < 0) { |
4245 | if (ret == -ENOMEM) { | |
4246 | ret = LTTNG_ERR_NOMEM; | |
4247 | } else { | |
4248 | ret = LTTNG_ERR_INVALID; | |
4249 | } | |
4250 | goto error; | |
4251 | } | |
1bfe7328 DG |
4252 | /* Use the global session count for the temporary snapshot. */ |
4253 | tmp_output.nb_snapshot = session->snapshot.nb_snapshot; | |
10ba83fe JR |
4254 | |
4255 | /* Use the global datetime */ | |
4256 | memcpy(tmp_output.datetime, datetime, sizeof(datetime)); | |
e1986656 | 4257 | use_tmp_output = 1; |
6dc3064a DG |
4258 | } |
4259 | ||
804c90a8 JR |
4260 | if (use_tmp_output) { |
4261 | int64_t nb_packets_per_stream; | |
6dc3064a | 4262 | |
804c90a8 JR |
4263 | nb_packets_per_stream = get_session_nb_packets_per_stream(session, |
4264 | tmp_output.max_size); | |
4265 | if (nb_packets_per_stream < 0) { | |
4266 | ret = LTTNG_ERR_MAX_SIZE_INVALID; | |
4267 | goto error; | |
4268 | } | |
d07ceecd | 4269 | |
804c90a8 JR |
4270 | if (session->kernel_session) { |
4271 | ret = record_kernel_snapshot(session->kernel_session, | |
4272 | &tmp_output, session, | |
4273 | wait, nb_packets_per_stream); | |
4274 | if (ret != LTTNG_OK) { | |
d07ceecd MD |
4275 | goto error; |
4276 | } | |
804c90a8 JR |
4277 | } |
4278 | ||
4279 | if (session->ust_session) { | |
4280 | ret = record_ust_snapshot(session->ust_session, | |
4281 | &tmp_output, session, | |
d07ceecd | 4282 | wait, nb_packets_per_stream); |
a934f4af | 4283 | if (ret != LTTNG_OK) { |
6dc3064a DG |
4284 | goto error; |
4285 | } | |
804c90a8 | 4286 | } |
e1986656 | 4287 | |
804c90a8 JR |
4288 | snapshot_success = 1; |
4289 | } else { | |
4290 | struct snapshot_output *sout; | |
4291 | struct lttng_ht_iter iter; | |
68808f4e | 4292 | |
804c90a8 JR |
4293 | rcu_read_lock(); |
4294 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, | |
4295 | &iter.iter, sout, node.node) { | |
4296 | int64_t nb_packets_per_stream; | |
e1986656 | 4297 | |
804c90a8 JR |
4298 | /* |
4299 | * Make a local copy of the output and assign the possible | |
4300 | * temporary value given by the caller. | |
4301 | */ | |
4302 | memset(&tmp_output, 0, sizeof(tmp_output)); | |
4303 | memcpy(&tmp_output, sout, sizeof(tmp_output)); | |
1bfe7328 | 4304 | |
804c90a8 JR |
4305 | if (output->max_size != (uint64_t) -1ULL) { |
4306 | tmp_output.max_size = output->max_size; | |
6dc3064a | 4307 | } |
d07ceecd MD |
4308 | |
4309 | nb_packets_per_stream = get_session_nb_packets_per_stream(session, | |
4310 | tmp_output.max_size); | |
4311 | if (nb_packets_per_stream < 0) { | |
4312 | ret = LTTNG_ERR_MAX_SIZE_INVALID; | |
804c90a8 | 4313 | rcu_read_unlock(); |
d07ceecd MD |
4314 | goto error; |
4315 | } | |
6dc3064a | 4316 | |
804c90a8 JR |
4317 | /* Use temporary name. */ |
4318 | if (*output->name != '\0') { | |
cf3e357d MD |
4319 | if (lttng_strncpy(tmp_output.name, output->name, |
4320 | sizeof(tmp_output.name))) { | |
4321 | ret = LTTNG_ERR_INVALID; | |
4322 | rcu_read_unlock(); | |
4323 | goto error; | |
4324 | } | |
804c90a8 | 4325 | } |
e1986656 | 4326 | |
804c90a8 | 4327 | tmp_output.nb_snapshot = session->snapshot.nb_snapshot; |
10ba83fe | 4328 | memcpy(tmp_output.datetime, datetime, sizeof(datetime)); |
e1986656 | 4329 | |
804c90a8 JR |
4330 | if (session->kernel_session) { |
4331 | ret = record_kernel_snapshot(session->kernel_session, | |
4332 | &tmp_output, session, | |
4333 | wait, nb_packets_per_stream); | |
4334 | if (ret != LTTNG_OK) { | |
d07ceecd | 4335 | rcu_read_unlock(); |
68808f4e DG |
4336 | goto error; |
4337 | } | |
804c90a8 | 4338 | } |
68808f4e | 4339 | |
804c90a8 JR |
4340 | if (session->ust_session) { |
4341 | ret = record_ust_snapshot(session->ust_session, | |
4342 | &tmp_output, session, | |
d07ceecd | 4343 | wait, nb_packets_per_stream); |
a934f4af | 4344 | if (ret != LTTNG_OK) { |
6dc3064a DG |
4345 | rcu_read_unlock(); |
4346 | goto error; | |
4347 | } | |
4348 | } | |
804c90a8 | 4349 | snapshot_success = 1; |
6dc3064a | 4350 | } |
804c90a8 | 4351 | rcu_read_unlock(); |
6dc3064a DG |
4352 | } |
4353 | ||
1bfe7328 DG |
4354 | if (snapshot_success) { |
4355 | session->snapshot.nb_snapshot++; | |
b67578cb MD |
4356 | } else { |
4357 | ret = LTTNG_ERR_SNAPSHOT_FAIL; | |
1bfe7328 DG |
4358 | } |
4359 | ||
6dc3064a | 4360 | error: |
6dc3064a DG |
4361 | return ret; |
4362 | } | |
4363 | ||
d7ba1388 MD |
4364 | /* |
4365 | * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread. | |
4366 | */ | |
4367 | int cmd_set_session_shm_path(struct ltt_session *session, | |
4368 | const char *shm_path) | |
4369 | { | |
4370 | /* Safety net */ | |
4371 | assert(session); | |
4372 | ||
4373 | /* | |
4374 | * Can only set shm path before session is started. | |
4375 | */ | |
4376 | if (session->has_been_started) { | |
4377 | return LTTNG_ERR_SESSION_STARTED; | |
4378 | } | |
4379 | ||
4380 | strncpy(session->shm_path, shm_path, | |
4381 | sizeof(session->shm_path)); | |
4382 | session->shm_path[sizeof(session->shm_path) - 1] = '\0'; | |
4383 | ||
4384 | return 0; | |
4385 | } | |
4386 | ||
5c408ad8 JD |
4387 | /* |
4388 | * Command LTTNG_ROTATE_SESSION from the lttng-ctl library. | |
4389 | * | |
4390 | * Ask the consumer to rotate the session output directory. | |
4391 | * The session lock must be held. | |
4392 | * | |
4393 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
4394 | */ | |
4395 | int cmd_rotate_session(struct ltt_session *session, | |
4396 | struct lttng_rotate_session_return *rotate_return) | |
4397 | { | |
4398 | int ret; | |
4399 | size_t strf_ret; | |
4400 | struct tm *timeinfo; | |
fc58be13 | 4401 | char datetime[21]; |
5c408ad8 JD |
4402 | time_t now; |
4403 | bool ust_active = false; | |
4404 | ||
4405 | assert(session); | |
4406 | ||
4407 | if (!session->has_been_started) { | |
4408 | ret = -LTTNG_ERR_START_SESSION_ONCE; | |
d68c9a04 | 4409 | goto end; |
5c408ad8 JD |
4410 | } |
4411 | ||
4412 | if (session->live_timer || session->snapshot_mode || | |
4413 | !session->output_traces) { | |
4414 | ret = -LTTNG_ERR_ROTATION_NOT_AVAILABLE; | |
d68c9a04 | 4415 | goto end; |
5c408ad8 JD |
4416 | } |
4417 | ||
4418 | /* | |
4419 | * Unsupported feature in lttng-relayd before 2.11. | |
4420 | */ | |
4421 | if (session->consumer->type == CONSUMER_DST_NET && | |
4422 | (session->consumer->relay_major_version == 2 && | |
4423 | session->consumer->relay_minor_version < 11)) { | |
504521ea | 4424 | ret = -LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY; |
d68c9a04 | 4425 | goto end; |
5c408ad8 JD |
4426 | } |
4427 | ||
4428 | if (session->rotate_pending || session->rotate_pending_relay) { | |
4429 | ret = -LTTNG_ERR_ROTATION_PENDING; | |
4430 | DBG("Rotate already in progress"); | |
d68c9a04 | 4431 | goto end; |
5c408ad8 JD |
4432 | } |
4433 | ||
4434 | /* | |
4435 | * After a stop, we only allow one rotation to occur, the other ones are | |
4436 | * useless until a new start. | |
4437 | */ | |
4438 | if (session->rotated_after_last_stop) { | |
4439 | DBG("Session \"%s\" was already rotated after stop, refusing rotation", | |
4440 | session->name); | |
4441 | ret = -LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP; | |
d68c9a04 | 4442 | goto end; |
5c408ad8 JD |
4443 | } |
4444 | ||
4445 | /* Special case for the first rotation. */ | |
4446 | if (session->rotate_count == 0) { | |
4447 | const char *base_path = NULL; | |
4448 | ||
4449 | /* Either one of the two sessions is enough to get the root path. */ | |
4450 | if (session->kernel_session) { | |
4451 | base_path = session_get_base_path(session); | |
4452 | } else if (session->ust_session) { | |
4453 | base_path = session_get_base_path(session); | |
4454 | } else { | |
4455 | assert(0); | |
4456 | } | |
4457 | assert(base_path); | |
4458 | ret = lttng_strncpy(session->rotation_chunk.current_rotate_path, | |
4459 | base_path, | |
4460 | sizeof(session->rotation_chunk.current_rotate_path)); | |
4461 | if (ret) { | |
4462 | ERR("Failed to copy session base path to current rotation chunk path"); | |
4463 | ret = -LTTNG_ERR_UNK; | |
d68c9a04 | 4464 | goto end; |
5c408ad8 JD |
4465 | } |
4466 | } else { | |
4467 | /* | |
4468 | * The currently active tracing path is now the folder we | |
4469 | * want to rotate. | |
4470 | */ | |
4471 | ret = lttng_strncpy(session->rotation_chunk.current_rotate_path, | |
4472 | session->rotation_chunk.active_tracing_path, | |
4473 | sizeof(session->rotation_chunk.current_rotate_path)); | |
4474 | if (ret) { | |
4475 | ERR("Failed to copy the active tracing path to the current rotate path"); | |
4476 | ret = -LTTNG_ERR_UNK; | |
d68c9a04 | 4477 | goto end; |
5c408ad8 JD |
4478 | } |
4479 | } | |
4480 | DBG("Current rotate path %s", session->rotation_chunk.current_rotate_path); | |
4481 | ||
4482 | session->rotate_count++; | |
4483 | session->rotate_pending = true; | |
d68c9a04 | 4484 | session->rotation_state = LTTNG_ROTATION_STATE_ONGOING; |
5c408ad8 JD |
4485 | |
4486 | /* | |
4487 | * Create the path name for the next chunk. | |
4488 | */ | |
4489 | now = time(NULL); | |
4490 | if (now == (time_t) -1) { | |
4491 | ret = -LTTNG_ERR_ROTATION_NOT_AVAILABLE; | |
d68c9a04 | 4492 | goto end; |
5c408ad8 JD |
4493 | } |
4494 | session->last_chunk_start_ts = session->current_chunk_start_ts; | |
4495 | session->current_chunk_start_ts = now; | |
4496 | ||
4497 | timeinfo = localtime(&now); | |
4498 | if (!timeinfo) { | |
4499 | PERROR("Failed to sample local time in rotate session command"); | |
4500 | ret = -LTTNG_ERR_UNK; | |
d68c9a04 | 4501 | goto end; |
5c408ad8 | 4502 | } |
fc58be13 | 4503 | strf_ret = strftime(datetime, sizeof(datetime), "%Y%m%dT%H%M%S%z", |
5c408ad8 JD |
4504 | timeinfo); |
4505 | if (!strf_ret) { | |
4506 | ERR("Failed to format local time timestamp in rotate session command"); | |
4507 | ret = -LTTNG_ERR_UNK; | |
d68c9a04 | 4508 | goto end; |
5c408ad8 JD |
4509 | } |
4510 | if (session->kernel_session) { | |
4511 | /* | |
4512 | * The active path for the next rotation/destroy. | |
4513 | * Ex: ~/lttng-traces/auto-20170922-111748/20170922-111754-42 | |
4514 | */ | |
4515 | ret = snprintf(session->rotation_chunk.active_tracing_path, | |
4516 | sizeof(session->rotation_chunk.active_tracing_path), | |
4517 | "%s/%s-%" PRIu64, | |
4518 | session_get_base_path(session), | |
4519 | datetime, session->rotate_count + 1); | |
4520 | if (ret < 0 || ret == sizeof(session->rotation_chunk.active_tracing_path)) { | |
4521 | ERR("Failed to format active kernel tracing path in rotate session command"); | |
4522 | ret = -LTTNG_ERR_UNK; | |
d68c9a04 | 4523 | goto end; |
5c408ad8 JD |
4524 | } |
4525 | /* | |
4526 | * The sub-directory for the consumer | |
4527 | * Ex: /20170922-111754-42/kernel | |
4528 | */ | |
4529 | ret = snprintf(session->kernel_session->consumer->chunk_path, | |
4530 | sizeof(session->kernel_session->consumer->chunk_path), | |
4531 | "/%s-%" PRIu64, datetime, | |
4532 | session->rotate_count + 1); | |
4533 | if (ret < 0 || ret == sizeof(session->kernel_session->consumer->chunk_path)) { | |
4534 | ERR("Failed to format the kernel consumer's sub-directory in rotate session command"); | |
4535 | ret = -LTTNG_ERR_UNK; | |
d68c9a04 | 4536 | goto end; |
5c408ad8 JD |
4537 | } |
4538 | /* | |
4539 | * Create the new chunk folder, before the rotation begins so we don't | |
4540 | * race with the consumer/tracer activity. | |
4541 | */ | |
4542 | ret = domain_mkdir(session->kernel_session->consumer, session, | |
4543 | session->kernel_session->uid, | |
4544 | session->kernel_session->gid); | |
4545 | if (ret) { | |
4546 | ERR("Failed to create kernel session tracing path at %s", | |
d68c9a04 JD |
4547 | session->kernel_session->consumer->chunk_path); |
4548 | ret = -LTTNG_ERR_CREATE_DIR_FAIL; | |
4549 | goto end; | |
5c408ad8 JD |
4550 | } |
4551 | ret = kernel_rotate_session(session); | |
4552 | if (ret != LTTNG_OK) { | |
d68c9a04 JD |
4553 | ret = -ret; |
4554 | goto end; | |
5c408ad8 JD |
4555 | } |
4556 | } | |
4557 | if (session->ust_session) { | |
4558 | ret = snprintf(session->rotation_chunk.active_tracing_path, | |
4559 | PATH_MAX, "%s/%s-%" PRIu64, | |
4560 | session_get_base_path(session), | |
4561 | datetime, session->rotate_count + 1); | |
4562 | if (ret < 0) { | |
4563 | ERR("Failed to format active UST tracing path in rotate session command"); | |
4564 | ret = -LTTNG_ERR_UNK; | |
d68c9a04 | 4565 | goto end; |
5c408ad8 JD |
4566 | } |
4567 | ret = snprintf(session->ust_session->consumer->chunk_path, | |
4568 | PATH_MAX, "/%s-%" PRIu64, datetime, | |
4569 | session->rotate_count + 1); | |
4570 | if (ret < 0) { | |
4571 | ERR("Failed to format the UST consumer's sub-directory in rotate session command"); | |
4572 | ret = -LTTNG_ERR_UNK; | |
d68c9a04 | 4573 | goto end; |
5c408ad8 JD |
4574 | } |
4575 | /* | |
4576 | * Create the new chunk folder, before the rotation begins so we don't | |
4577 | * race with the consumer/tracer activity. | |
4578 | */ | |
4579 | ret = domain_mkdir(session->ust_session->consumer, session, | |
4580 | session->ust_session->uid, | |
4581 | session->ust_session->gid); | |
b019620d JG |
4582 | if (ret) { |
4583 | ret = -LTTNG_ERR_CREATE_DIR_FAIL; | |
4584 | goto end; | |
4585 | } | |
5c408ad8 JD |
4586 | ret = ust_app_rotate_session(session, &ust_active); |
4587 | if (ret != LTTNG_OK) { | |
d68c9a04 | 4588 | goto end; |
5c408ad8 JD |
4589 | } |
4590 | /* | |
4591 | * Handle the case where we did not start a rotation on any channel. | |
4592 | * The consumer will never wake up the rotation thread to perform the | |
4593 | * rename, so we have to do it here while we hold the session and | |
4594 | * session_list locks. | |
4595 | */ | |
4596 | if (!session->kernel_session && !ust_active) { | |
4597 | ret = rename_complete_chunk(session, now); | |
4598 | if (ret < 0) { | |
4599 | ERR("Failed to rename completed rotation chunk"); | |
4600 | goto end; | |
4601 | } | |
4602 | session->rotate_pending = false; | |
d68c9a04 | 4603 | session->rotation_state = LTTNG_ROTATION_STATE_COMPLETED; |
5c408ad8 JD |
4604 | } |
4605 | } | |
4606 | ||
4607 | if (!session->active) { | |
4608 | session->rotated_after_last_stop = true; | |
4609 | } | |
4610 | ||
4611 | if (rotate_return) { | |
d68c9a04 | 4612 | rotate_return->rotation_id = session->rotate_count; |
5c408ad8 JD |
4613 | } |
4614 | ||
259c2674 | 4615 | DBG("Cmd rotate session %s, rotate_id %" PRIu64 " sent", session->name, |
5c408ad8 JD |
4616 | session->rotate_count); |
4617 | ret = LTTNG_OK; | |
4618 | ||
5c408ad8 JD |
4619 | end: |
4620 | return ret; | |
4621 | } | |
4622 | ||
4623 | /* | |
d68c9a04 | 4624 | * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library. |
5c408ad8 JD |
4625 | * |
4626 | * Check if the session has finished its rotation. | |
4627 | * | |
4628 | * Return 0 on success or else a LTTNG_ERR code. | |
4629 | */ | |
d68c9a04 JD |
4630 | int cmd_rotate_get_info(struct ltt_session *session, |
4631 | struct lttng_rotation_get_info_return *info_return, | |
4632 | uint64_t rotation_id) | |
5c408ad8 JD |
4633 | { |
4634 | int ret; | |
4635 | ||
4636 | assert(session); | |
4637 | ||
d68c9a04 | 4638 | DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64, session->name, |
5c408ad8 JD |
4639 | session->rotate_count); |
4640 | ||
d68c9a04 JD |
4641 | if (session->rotate_count != rotation_id) { |
4642 | info_return->status = (int32_t) LTTNG_ROTATION_STATE_EXPIRED; | |
5c408ad8 JD |
4643 | ret = LTTNG_OK; |
4644 | goto end; | |
4645 | } | |
4646 | ||
d68c9a04 JD |
4647 | switch (session->rotation_state) { |
4648 | case LTTNG_ROTATION_STATE_ONGOING: | |
4649 | DBG("Reporting that rotation id %" PRIu64 " of session %s is still pending", | |
4650 | rotation_id, session->name); | |
4651 | break; | |
4652 | case LTTNG_ROTATION_STATE_COMPLETED: | |
dd73d57b JG |
4653 | { |
4654 | char *current_tracing_path_reply; | |
4655 | size_t current_tracing_path_reply_len; | |
4656 | ||
4657 | switch (session_get_consumer_destination_type(session)) { | |
4658 | case CONSUMER_DST_LOCAL: | |
4659 | current_tracing_path_reply = | |
4660 | info_return->location.local.absolute_path; | |
4661 | current_tracing_path_reply_len = | |
4662 | sizeof(info_return->location.local.absolute_path); | |
4663 | info_return->location_type = | |
05f8afa9 | 4664 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL; |
dd73d57b JG |
4665 | break; |
4666 | case CONSUMER_DST_NET: | |
4667 | current_tracing_path_reply = | |
4668 | info_return->location.relay.relative_path; | |
4669 | current_tracing_path_reply_len = | |
4670 | sizeof(info_return->location.relay.relative_path); | |
4671 | /* Currently the only supported relay protocol. */ | |
4672 | info_return->location.relay.protocol = | |
05f8afa9 | 4673 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP; |
dd73d57b JG |
4674 | |
4675 | ret = lttng_strncpy(info_return->location.relay.host, | |
4676 | session_get_net_consumer_hostname(session), | |
4677 | sizeof(info_return->location.relay.host)); | |
4678 | if (ret) { | |
4679 | ERR("Failed to host name to rotate_get_info reply"); | |
4680 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; | |
4681 | ret = -LTTNG_ERR_UNK; | |
4682 | goto end; | |
4683 | } | |
4684 | ||
4685 | session_get_net_consumer_ports(session, | |
4686 | &info_return->location.relay.ports.control, | |
4687 | &info_return->location.relay.ports.data); | |
4688 | info_return->location_type = | |
05f8afa9 | 4689 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY; |
dd73d57b JG |
4690 | break; |
4691 | default: | |
4692 | abort(); | |
4693 | } | |
4694 | ret = lttng_strncpy(current_tracing_path_reply, | |
5c408ad8 | 4695 | session->rotation_chunk.current_rotate_path, |
dd73d57b | 4696 | current_tracing_path_reply_len); |
5c408ad8 | 4697 | if (ret) { |
dd73d57b | 4698 | ERR("Failed to copy current tracing path to rotate_get_info reply"); |
d68c9a04 JD |
4699 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
4700 | ret = -LTTNG_ERR_UNK; | |
5c408ad8 JD |
4701 | goto end; |
4702 | } | |
dd73d57b | 4703 | |
d68c9a04 | 4704 | break; |
dd73d57b | 4705 | } |
d68c9a04 JD |
4706 | case LTTNG_ROTATION_STATE_ERROR: |
4707 | DBG("Reporting that an error occurred during rotation %" PRIu64 " of session %s", | |
4708 | rotation_id, session->name); | |
4709 | break; | |
4710 | default: | |
4711 | abort(); | |
5c408ad8 JD |
4712 | } |
4713 | ||
d68c9a04 | 4714 | info_return->status = (int32_t) session->rotation_state; |
5c408ad8 | 4715 | ret = LTTNG_OK; |
5c408ad8 JD |
4716 | end: |
4717 | return ret; | |
4718 | } | |
4719 | ||
259c2674 JD |
4720 | /* |
4721 | * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library. | |
4722 | * | |
4723 | * Configure the automatic rotation parameters. | |
4724 | * Set to -1ULL to disable them. | |
4725 | * | |
4726 | * Return 0 on success or else an LTTNG_ERR code. | |
4727 | */ | |
4728 | int cmd_rotation_set_schedule(struct ltt_session *session, | |
90936dcf JD |
4729 | uint64_t timer_us, uint64_t size, |
4730 | struct notification_thread_handle *notification_thread_handle) | |
259c2674 JD |
4731 | { |
4732 | int ret; | |
4733 | ||
4734 | assert(session); | |
4735 | ||
4736 | DBG("Cmd rotate set schedule session %s", session->name); | |
4737 | ||
4738 | if (session->live_timer || session->snapshot_mode || | |
4739 | !session->output_traces) { | |
4740 | ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE; | |
4741 | goto end; | |
4742 | } | |
4743 | ||
4744 | /* Trying to override an already active timer. */ | |
4745 | if (timer_us && timer_us != -1ULL && session->rotate_timer_period) { | |
4746 | ret = LTTNG_ERR_ROTATION_TIMER_SET; | |
4747 | goto end; | |
4748 | /* Trying to disable an inactive timer. */ | |
4749 | } else if (timer_us == -1ULL && !session->rotate_timer_period) { | |
4750 | ret = LTTNG_ERR_ROTATION_NO_TIMER_SET; | |
4751 | goto end; | |
4752 | } | |
4753 | ||
90936dcf JD |
4754 | if (size && size != -1ULL && session->rotate_size) { |
4755 | ret = LTTNG_ERR_ROTATION_SIZE_SET; | |
4756 | goto end; | |
4757 | } else if (size == -1ULL && !session->rotate_size) { | |
4758 | ret = LTTNG_ERR_ROTATION_NO_SIZE_SET; | |
4759 | goto end; | |
4760 | } | |
4761 | ||
259c2674 JD |
4762 | if (timer_us && !session->rotate_timer_period) { |
4763 | if (timer_us > UINT_MAX) { | |
4764 | ret = LTTNG_ERR_INVALID; | |
4765 | goto end; | |
4766 | } | |
4767 | ||
4768 | session->rotate_timer_period = timer_us; | |
4769 | /* | |
4770 | * Only start the timer if the session is active, otherwise | |
4771 | * it will be started when the session starts. | |
4772 | */ | |
4773 | if (session->active) { | |
4774 | ret = sessiond_rotate_timer_start(session, timer_us); | |
4775 | if (ret) { | |
4776 | ERR("Failed to enable rotate timer"); | |
4777 | ret = LTTNG_ERR_UNK; | |
4778 | goto end; | |
4779 | } | |
4780 | } | |
4781 | } else if (timer_us == -1ULL && session->rotate_timer_period > 0) { | |
4782 | sessiond_rotate_timer_stop(session); | |
4783 | session->rotate_timer_period = 0; | |
4784 | } | |
4785 | ||
90936dcf JD |
4786 | if (size > 0) { |
4787 | if (size == -1ULL) { | |
4788 | ret = unsubscribe_session_consumed_size_rotation(session, | |
4789 | notification_thread_handle); | |
4790 | if (ret) { | |
4791 | ret = LTTNG_ERR_UNK; | |
4792 | goto end; | |
4793 | } | |
4794 | session->rotate_size = 0; | |
4795 | } else { | |
4796 | ret = subscribe_session_consumed_size_rotation(session, | |
4797 | size, notification_thread_handle); | |
4798 | if (ret) { | |
4799 | PERROR("Subscribe to session usage"); | |
4800 | ret = LTTNG_ERR_UNK; | |
4801 | goto end; | |
4802 | } | |
4803 | session->rotate_size = size; | |
4804 | } | |
4805 | } | |
4806 | ||
259c2674 JD |
4807 | ret = LTTNG_OK; |
4808 | ||
4809 | goto end; | |
4810 | ||
4811 | end: | |
4812 | return ret; | |
4813 | } | |
4814 | ||
5c408ad8 JD |
4815 | /* |
4816 | * Command ROTATE_GET_CURRENT_PATH from the lttng-ctl library. | |
4817 | * | |
4818 | * Configure the automatic rotation parameters. | |
4819 | * Set to -1ULL to disable them. | |
4820 | * | |
4821 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
4822 | */ | |
d68c9a04 JD |
4823 | int cmd_session_get_current_output(struct ltt_session *session, |
4824 | struct lttng_session_get_current_output_return *output_return) | |
5c408ad8 JD |
4825 | { |
4826 | int ret; | |
d68c9a04 | 4827 | const char *path; |
5c408ad8 | 4828 | |
d68c9a04 JD |
4829 | if (!session->snapshot_mode) { |
4830 | if (session->rotate_count == 0) { | |
4831 | if (session->kernel_session) { | |
4832 | path = session_get_base_path(session); | |
4833 | } else if (session->ust_session) { | |
4834 | path = session_get_base_path(session); | |
4835 | } else { | |
4836 | abort(); | |
4837 | } | |
4838 | assert(path); | |
4839 | } else { | |
4840 | path = session->rotation_chunk.active_tracing_path; | |
4841 | } | |
4842 | } else { | |
4843 | /* | |
4844 | * A snapshot session does not have a "current" trace archive | |
4845 | * location. | |
4846 | */ | |
4847 | path = ""; | |
5c408ad8 JD |
4848 | } |
4849 | ||
d68c9a04 JD |
4850 | DBG("Cmd get current output for session %s, returning %s", |
4851 | session->name, path); | |
4852 | ||
4853 | ret = lttng_strncpy(output_return->path, | |
4854 | path, | |
4855 | sizeof(output_return->path)); | |
4856 | if (ret) { | |
4857 | ERR("Failed to copy trace output path to session get current output command reply"); | |
4858 | ret = -LTTNG_ERR_UNK; | |
4859 | goto end; | |
5c408ad8 JD |
4860 | } |
4861 | ||
4862 | ret = LTTNG_OK; | |
5c408ad8 JD |
4863 | end: |
4864 | return ret; | |
4865 | } | |
4866 | ||
2f77fc4b DG |
4867 | /* |
4868 | * Init command subsystem. | |
4869 | */ | |
4870 | void cmd_init(void) | |
4871 | { | |
4872 | /* | |
d88aee68 DG |
4873 | * Set network sequence index to 1 for streams to match a relayd |
4874 | * socket on the consumer side. | |
2f77fc4b | 4875 | */ |
d88aee68 DG |
4876 | pthread_mutex_lock(&relayd_net_seq_idx_lock); |
4877 | relayd_net_seq_idx = 1; | |
4878 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
4879 | |
4880 | DBG("Command subsystem initialized"); | |
4881 | } |