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