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