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