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