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