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