2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 #include <sys/types.h>
12 #include <sys/socket.h>
15 #include <sys/types.h>
22 #include <semaphore.h>
27 #include <urcu/uatomic.h>
29 #include <urcu/compiler.h>
30 #include <lttng/urcu/urcu-ust.h>
32 #include <lttng/ust-utils.h>
33 #include <lttng/ust-events.h>
34 #include <lttng/ust-abi.h>
35 #include <lttng/ust-fork.h>
36 #include <lttng/ust-error.h>
37 #include <lttng/ust-ctl.h>
38 #include <lttng/ust-libc-wrapper.h>
39 #include <lttng/ust-thread.h>
40 #include <lttng/ust-tracer.h>
41 #include <urcu/tls-compat.h>
42 #include "common/ustcomm.h"
43 #include "common/ust-fd.h"
44 #include "common/logging.h"
45 #include "common/macros.h"
46 #include "tracepoint-internal.h"
47 #include "lttng-tracer-core.h"
48 #include "common/compat/pthread.h"
49 #include "common/procname.h"
50 #include "common/ringbuffer/rb-init.h"
51 #include "lttng-ust-statedump.h"
53 #include "common/ringbuffer/getcpu.h"
55 #include "ust-events-internal.h"
56 #include "context-internal.h"
57 #include "common/align.h"
58 #include "lttng-counter-client.h"
59 #include "lttng-rb-clients.h"
62 * Has lttng ust comm constructor been called ?
64 static int initialized
;
67 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
68 * Held when handling a command, also held by fork() to deal with
69 * removal of threads, and by exit path.
71 * The UST lock is the centralized mutex across UST tracing control and
74 * ust_exit_mutex must never nest in ust_mutex.
76 * ust_fork_mutex must never nest in ust_mutex.
78 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
79 * counter lazy initialization called by events within the statedump,
80 * which traces while the ust_mutex is held.
82 * ust_lock nests within the dynamic loader lock (within glibc) because
83 * it is taken within the library constructor.
85 * The ust fd tracker lock nests within the ust_mutex.
87 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
89 /* Allow nesting the ust_mutex within the same thread. */
90 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
93 * ust_exit_mutex protects thread_active variable wrt thread exit. It
94 * cannot be done by ust_mutex because pthread_cancel(), which takes an
95 * internal libc lock, cannot nest within ust_mutex.
97 * It never nests within a ust_mutex.
99 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
102 * ust_fork_mutex protects base address statedump tracing against forks. It
103 * prevents the dynamic loader lock to be taken (by base address statedump
104 * tracing) while a fork is happening, thus preventing deadlock issues with
105 * the dynamic loader lock.
107 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
109 /* Should the ust comm thread quit ? */
110 static int lttng_ust_comm_should_quit
;
113 * This variable can be tested by applications to check whether
114 * lttng-ust is loaded. They simply have to define their own
115 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
116 * library constructor.
118 int lttng_ust_loaded
__attribute__((weak
));
121 * Return 0 on success, -1 if should quit.
122 * The lock is taken in both cases.
127 sigset_t sig_all_blocked
, orig_mask
;
130 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
132 ERR("pthread_setcancelstate: %s", strerror(ret
));
134 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
135 ERR("pthread_setcancelstate: unexpected oldstate");
137 sigfillset(&sig_all_blocked
);
138 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
140 ERR("pthread_sigmask: %s", strerror(ret
));
142 if (!URCU_TLS(ust_mutex_nest
)++)
143 pthread_mutex_lock(&ust_mutex
);
144 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
146 ERR("pthread_sigmask: %s", strerror(ret
));
148 if (lttng_ust_comm_should_quit
) {
156 * ust_lock_nocheck() can be used in constructors/destructors, because
157 * they are already nested within the dynamic loader lock, and therefore
158 * have exclusive access against execution of liblttng-ust destructor.
161 void ust_lock_nocheck(void)
163 sigset_t sig_all_blocked
, orig_mask
;
166 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
168 ERR("pthread_setcancelstate: %s", strerror(ret
));
170 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
171 ERR("pthread_setcancelstate: unexpected oldstate");
173 sigfillset(&sig_all_blocked
);
174 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
176 ERR("pthread_sigmask: %s", strerror(ret
));
178 if (!URCU_TLS(ust_mutex_nest
)++)
179 pthread_mutex_lock(&ust_mutex
);
180 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
182 ERR("pthread_sigmask: %s", strerror(ret
));
189 void ust_unlock(void)
191 sigset_t sig_all_blocked
, orig_mask
;
194 sigfillset(&sig_all_blocked
);
195 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
197 ERR("pthread_sigmask: %s", strerror(ret
));
199 if (!--URCU_TLS(ust_mutex_nest
))
200 pthread_mutex_unlock(&ust_mutex
);
201 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
203 ERR("pthread_sigmask: %s", strerror(ret
));
205 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
207 ERR("pthread_setcancelstate: %s", strerror(ret
));
209 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
210 ERR("pthread_setcancelstate: unexpected oldstate");
215 * Wait for either of these before continuing to the main
217 * - the register_done message from sessiond daemon
218 * (will let the sessiond daemon enable sessions before main
220 * - sessiond daemon is not reachable.
221 * - timeout (ensuring applications are resilient to session
224 static sem_t constructor_wait
;
226 * Doing this for both the global and local sessiond.
229 sem_count_initial_value
= 4,
232 static int sem_count
= sem_count_initial_value
;
235 * Counting nesting within lttng-ust. Used to ensure that calling fork()
236 * from liblttng-ust does not execute the pre/post fork handlers.
238 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
241 * Info about socket and associated listener thread.
245 pthread_t ust_listener
; /* listener thread */
247 int registration_done
;
252 char sock_path
[PATH_MAX
];
256 char wait_shm_path
[PATH_MAX
];
258 /* Keep track of lazy state dump not performed yet. */
259 int statedump_pending
;
260 int initial_statedump_done
;
261 /* Keep procname for statedump */
262 char procname
[LTTNG_UST_ABI_PROCNAME_LEN
];
265 /* Socket from app (connect) to session daemon (listen) for communication */
266 struct sock_info global_apps
= {
271 .registration_done
= 0,
275 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
279 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
281 .statedump_pending
= 0,
282 .initial_statedump_done
= 0,
286 /* TODO: allow global_apps_sock_path override */
288 struct sock_info local_apps
= {
292 .registration_done
= 0,
293 .allowed
= 0, /* Check setuid bit first */
299 .statedump_pending
= 0,
300 .initial_statedump_done
= 0,
304 static int wait_poll_fallback
;
306 static const char *cmd_name_mapping
[] = {
307 [ LTTNG_UST_ABI_RELEASE
] = "Release",
308 [ LTTNG_UST_ABI_SESSION
] = "Create Session",
309 [ LTTNG_UST_ABI_TRACER_VERSION
] = "Get Tracer Version",
311 [ LTTNG_UST_ABI_TRACEPOINT_LIST
] = "Create Tracepoint List",
312 [ LTTNG_UST_ABI_WAIT_QUIESCENT
] = "Wait for Quiescent State",
313 [ LTTNG_UST_ABI_REGISTER_DONE
] = "Registration Done",
314 [ LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
316 [ LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
] = "Create event notifier group",
318 /* Session FD commands */
319 [ LTTNG_UST_ABI_CHANNEL
] = "Create Channel",
320 [ LTTNG_UST_ABI_SESSION_START
] = "Start Session",
321 [ LTTNG_UST_ABI_SESSION_STOP
] = "Stop Session",
323 /* Channel FD commands */
324 [ LTTNG_UST_ABI_STREAM
] = "Create Stream",
325 [ LTTNG_UST_ABI_EVENT
] = "Create Event",
327 /* Event and Channel FD commands */
328 [ LTTNG_UST_ABI_CONTEXT
] = "Create Context",
329 [ LTTNG_UST_ABI_FLUSH_BUFFER
] = "Flush Buffer",
331 /* Event, Channel and Session commands */
332 [ LTTNG_UST_ABI_ENABLE
] = "Enable",
333 [ LTTNG_UST_ABI_DISABLE
] = "Disable",
335 /* Tracepoint list commands */
336 [ LTTNG_UST_ABI_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
337 [ LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
339 /* Event FD commands */
340 [ LTTNG_UST_ABI_FILTER
] = "Create Filter",
341 [ LTTNG_UST_ABI_EXCLUSION
] = "Add exclusions to event",
343 /* Event notifier group commands */
344 [ LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
] = "Create event notifier",
346 /* Session and event notifier group commands */
347 [ LTTNG_UST_ABI_COUNTER
] = "Create Counter",
349 /* Counter commands */
350 [ LTTNG_UST_ABI_COUNTER_GLOBAL
] = "Create Counter Global",
351 [ LTTNG_UST_ABI_COUNTER_CPU
] = "Create Counter CPU",
354 static const char *str_timeout
;
355 static int got_timeout_env
;
357 static char *get_map_shm(struct sock_info
*sock_info
);
359 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
362 size_t copied
= 0, to_copy
= len
;
365 ret
= read(fd
, buf
+ copied
, to_copy
);
370 } while ((ret
> 0 && to_copy
> 0)
371 || (ret
< 0 && errno
== EINTR
));
378 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
382 const char *get_lttng_home_dir(void)
386 val
= (const char *) lttng_ust_getenv("LTTNG_HOME");
390 return (const char *) lttng_ust_getenv("HOME");
394 * Force a read (imply TLS fixup for dlopen) of TLS variables.
397 void lttng_fixup_nest_count_tls(void)
399 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
403 void lttng_fixup_ust_mutex_nest_tls(void)
405 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
409 * Fixup lttng-ust urcu TLS.
412 void lttng_fixup_lttng_ust_urcu_tls(void)
414 (void) lttng_ust_urcu_read_ongoing();
417 void lttng_ust_fixup_tls(void)
419 lttng_fixup_lttng_ust_urcu_tls();
420 lttng_fixup_ringbuffer_tls();
421 lttng_fixup_vtid_tls();
422 lttng_fixup_nest_count_tls();
423 lttng_fixup_procname_tls();
424 lttng_fixup_ust_mutex_nest_tls();
425 lttng_ust_fixup_perf_counter_tls();
426 lttng_ust_fixup_fd_tracker_tls();
427 lttng_fixup_cgroup_ns_tls();
428 lttng_fixup_ipc_ns_tls();
429 lttng_fixup_net_ns_tls();
430 lttng_fixup_time_ns_tls();
431 lttng_fixup_uts_ns_tls();
432 lttng_ust_fixup_ring_buffer_client_discard_tls();
433 lttng_ust_fixup_ring_buffer_client_discard_rt_tls();
434 lttng_ust_fixup_ring_buffer_client_overwrite_tls();
435 lttng_ust_fixup_ring_buffer_client_overwrite_rt_tls();
439 * LTTng-UST uses Global Dynamic model TLS variables rather than IE
440 * model because many versions of glibc don't preallocate a pool large
441 * enough for TLS variables IE model defined in other shared libraries,
442 * and causes issues when using LTTng-UST for Java tracing.
444 * Because of this use of Global Dynamic TLS variables, users wishing to
445 * trace from signal handlers need to explicitly trigger the lazy
446 * allocation of those variables for each thread before using them.
447 * This can be triggered by calling lttng_ust_init_thread().
449 void lttng_ust_init_thread(void)
452 * Because those TLS variables are global dynamic, we need to
453 * ensure those are initialized before a signal handler nesting over
454 * this thread attempts to use them.
456 lttng_ust_fixup_tls();
459 int lttng_get_notify_socket(void *owner
)
461 struct sock_info
*info
= owner
;
463 return info
->notify_socket
;
467 char* lttng_ust_sockinfo_get_procname(void *owner
)
469 struct sock_info
*info
= owner
;
471 return info
->procname
;
475 void print_cmd(int cmd
, int handle
)
477 const char *cmd_name
= "Unknown";
479 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
480 && cmd_name_mapping
[cmd
]) {
481 cmd_name
= cmd_name_mapping
[cmd
];
483 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
485 lttng_ust_obj_get_name(handle
), handle
);
489 int setup_global_apps(void)
492 assert(!global_apps
.wait_shm_mmap
);
494 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
495 if (!global_apps
.wait_shm_mmap
) {
496 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
497 global_apps
.allowed
= 0;
502 global_apps
.allowed
= 1;
503 lttng_pthread_getname_np(global_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
508 int setup_local_apps(void)
511 const char *home_dir
;
514 assert(!local_apps
.wait_shm_mmap
);
518 * Disallow per-user tracing for setuid binaries.
520 if (uid
!= geteuid()) {
521 assert(local_apps
.allowed
== 0);
525 home_dir
= get_lttng_home_dir();
527 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
528 assert(local_apps
.allowed
== 0);
532 local_apps
.allowed
= 1;
533 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
535 LTTNG_DEFAULT_HOME_RUNDIR
,
536 LTTNG_UST_SOCK_FILENAME
);
537 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
538 LTTNG_UST_WAIT_FILENAME
,
541 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
542 if (!local_apps
.wait_shm_mmap
) {
543 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
544 local_apps
.allowed
= 0;
549 lttng_pthread_getname_np(local_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
555 * Get socket timeout, in ms.
556 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
559 long get_timeout(void)
561 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
563 if (!got_timeout_env
) {
564 str_timeout
= lttng_ust_getenv("LTTNG_UST_REGISTER_TIMEOUT");
568 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
569 /* All negative values are considered as "-1". */
570 if (constructor_delay_ms
< -1)
571 constructor_delay_ms
= -1;
572 return constructor_delay_ms
;
575 /* Timeout for notify socket send and recv. */
577 long get_notify_sock_timeout(void)
579 return get_timeout();
582 /* Timeout for connecting to cmd and notify sockets. */
584 long get_connect_sock_timeout(void)
586 return get_timeout();
590 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
593 int get_constructor_timeout(struct timespec
*constructor_timeout
)
595 long constructor_delay_ms
;
598 constructor_delay_ms
= get_timeout();
600 switch (constructor_delay_ms
) {
601 case -1:/* fall-through */
603 return constructor_delay_ms
;
609 * If we are unable to find the current time, don't wait.
611 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
616 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
617 constructor_timeout
->tv_nsec
+=
618 (constructor_delay_ms
% 1000UL) * 1000000UL;
619 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
620 constructor_timeout
->tv_sec
++;
621 constructor_timeout
->tv_nsec
-= 1000000000UL;
623 /* Timeout wait (constructor_delay_ms). */
628 void get_allow_blocking(void)
630 const char *str_allow_blocking
=
631 lttng_ust_getenv("LTTNG_UST_ALLOW_BLOCKING");
633 if (str_allow_blocking
) {
634 DBG("%s environment variable is set",
635 "LTTNG_UST_ALLOW_BLOCKING");
636 lttng_ust_ringbuffer_set_allow_blocking();
641 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
643 return ustcomm_send_reg_msg(socket
,
646 lttng_ust_rb_alignof(uint8_t) * CHAR_BIT
,
647 lttng_ust_rb_alignof(uint16_t) * CHAR_BIT
,
648 lttng_ust_rb_alignof(uint32_t) * CHAR_BIT
,
649 lttng_ust_rb_alignof(uint64_t) * CHAR_BIT
,
650 lttng_ust_rb_alignof(unsigned long) * CHAR_BIT
);
654 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
658 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
661 DBG("message successfully sent");
664 if (len
== -ECONNRESET
) {
665 DBG("remote end closed connection");
670 DBG("incorrect message size: %zd", len
);
676 void decrement_sem_count(unsigned int count
)
680 assert(uatomic_read(&sem_count
) >= count
);
682 if (uatomic_read(&sem_count
) <= 0) {
686 ret
= uatomic_add_return(&sem_count
, -count
);
688 ret
= sem_post(&constructor_wait
);
694 int handle_register_done(struct sock_info
*sock_info
)
696 if (sock_info
->registration_done
)
698 sock_info
->registration_done
= 1;
700 decrement_sem_count(1);
701 if (!sock_info
->statedump_pending
) {
702 sock_info
->initial_statedump_done
= 1;
703 decrement_sem_count(1);
710 int handle_register_failed(struct sock_info
*sock_info
)
712 if (sock_info
->registration_done
)
714 sock_info
->registration_done
= 1;
715 sock_info
->initial_statedump_done
= 1;
717 decrement_sem_count(2);
723 * Only execute pending statedump after the constructor semaphore has
724 * been posted by the current listener thread. This means statedump will
725 * only be performed after the "registration done" command is received
726 * from this thread's session daemon.
728 * This ensures we don't run into deadlock issues with the dynamic
729 * loader mutex, which is held while the constructor is called and
730 * waiting on the constructor semaphore. All operations requiring this
731 * dynamic loader lock need to be postponed using this mechanism.
733 * In a scenario with two session daemons connected to the application,
734 * it is possible that the first listener thread which receives the
735 * registration done command issues its statedump while the dynamic
736 * loader lock is still held by the application constructor waiting on
737 * the semaphore. It will however be allowed to proceed when the
738 * second session daemon sends the registration done command to the
739 * second listener thread. This situation therefore does not produce
743 void handle_pending_statedump(struct sock_info
*sock_info
)
745 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
746 sock_info
->statedump_pending
= 0;
747 pthread_mutex_lock(&ust_fork_mutex
);
748 lttng_handle_pending_statedump(sock_info
);
749 pthread_mutex_unlock(&ust_fork_mutex
);
751 if (!sock_info
->initial_statedump_done
) {
752 sock_info
->initial_statedump_done
= 1;
753 decrement_sem_count(1);
759 const char *bytecode_type_str(uint32_t cmd
)
762 case LTTNG_UST_ABI_CAPTURE
:
764 case LTTNG_UST_ABI_FILTER
:
772 int handle_bytecode_recv(struct sock_info
*sock_info
,
773 int sock
, struct ustcomm_ust_msg
*lum
)
775 struct lttng_ust_bytecode_node
*bytecode
= NULL
;
776 enum lttng_ust_bytecode_type type
;
777 const struct lttng_ust_abi_objd_ops
*ops
;
778 uint32_t data_size
, data_size_max
, reloc_offset
;
784 case LTTNG_UST_ABI_FILTER
:
785 type
= LTTNG_UST_BYTECODE_TYPE_FILTER
;
786 data_size
= lum
->u
.filter
.data_size
;
787 data_size_max
= LTTNG_UST_ABI_FILTER_BYTECODE_MAX_LEN
;
788 reloc_offset
= lum
->u
.filter
.reloc_offset
;
789 seqnum
= lum
->u
.filter
.seqnum
;
791 case LTTNG_UST_ABI_CAPTURE
:
792 type
= LTTNG_UST_BYTECODE_TYPE_CAPTURE
;
793 data_size
= lum
->u
.capture
.data_size
;
794 data_size_max
= LTTNG_UST_ABI_CAPTURE_BYTECODE_MAX_LEN
;
795 reloc_offset
= lum
->u
.capture
.reloc_offset
;
796 seqnum
= lum
->u
.capture
.seqnum
;
802 if (data_size
> data_size_max
) {
803 ERR("Bytecode %s data size is too large: %u bytes",
804 bytecode_type_str(lum
->cmd
), data_size
);
809 if (reloc_offset
> data_size
) {
810 ERR("Bytecode %s reloc offset %u is not within data",
811 bytecode_type_str(lum
->cmd
), reloc_offset
);
816 /* Allocate the structure AND the `data[]` field. */
817 bytecode
= zmalloc(sizeof(*bytecode
) + data_size
);
823 bytecode
->bc
.len
= data_size
;
824 bytecode
->bc
.reloc_offset
= reloc_offset
;
825 bytecode
->bc
.seqnum
= seqnum
;
826 bytecode
->type
= type
;
828 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
, bytecode
->bc
.len
);
830 case 0: /* orderly shutdown */
834 if (len
== bytecode
->bc
.len
) {
835 DBG("Bytecode %s data received",
836 bytecode_type_str(lum
->cmd
));
838 } else if (len
< 0) {
839 DBG("Receive failed from lttng-sessiond with errno %d",
841 if (len
== -ECONNRESET
) {
842 ERR("%s remote end closed connection",
850 DBG("Incorrect %s bytecode data message size: %zd",
851 bytecode_type_str(lum
->cmd
), len
);
857 ops
= lttng_ust_abi_objd_ops(lum
->handle
);
864 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
865 (unsigned long) &bytecode
,
876 int handle_message(struct sock_info
*sock_info
,
877 int sock
, struct ustcomm_ust_msg
*lum
)
880 const struct lttng_ust_abi_objd_ops
*ops
;
881 struct ustcomm_ust_reply lur
;
882 union lttng_ust_abi_args args
;
883 char ctxstr
[LTTNG_UST_ABI_SYM_NAME_LEN
]; /* App context string. */
886 memset(&lur
, 0, sizeof(lur
));
889 ret
= -LTTNG_UST_ERR_EXITING
;
893 ops
= lttng_ust_abi_objd_ops(lum
->handle
);
900 case LTTNG_UST_ABI_REGISTER_DONE
:
901 if (lum
->handle
== LTTNG_UST_ABI_ROOT_HANDLE
)
902 ret
= handle_register_done(sock_info
);
906 case LTTNG_UST_ABI_RELEASE
:
907 if (lum
->handle
== LTTNG_UST_ABI_ROOT_HANDLE
)
910 ret
= lttng_ust_abi_objd_unref(lum
->handle
, 1);
912 case LTTNG_UST_ABI_CAPTURE
:
913 case LTTNG_UST_ABI_FILTER
:
914 ret
= handle_bytecode_recv(sock_info
, sock
, lum
);
918 case LTTNG_UST_ABI_EXCLUSION
:
920 /* Receive exclusion names */
921 struct lttng_ust_excluder_node
*node
;
924 count
= lum
->u
.exclusion
.count
;
926 /* There are no names to read */
930 node
= zmalloc(sizeof(*node
) +
931 count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
936 node
->excluder
.count
= count
;
937 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
938 count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
940 case 0: /* orderly shutdown */
945 if (len
== count
* LTTNG_UST_ABI_SYM_NAME_LEN
) {
946 DBG("Exclusion data received");
948 } else if (len
< 0) {
949 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
950 if (len
== -ECONNRESET
) {
951 ERR("%s remote end closed connection", sock_info
->name
);
960 DBG("Incorrect exclusion data message size: %zd", len
);
967 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
968 (unsigned long) &node
,
975 case LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
:
977 int event_notifier_notif_fd
, close_ret
;
979 len
= ustcomm_recv_event_notifier_notif_fd_from_sessiond(sock
,
980 &event_notifier_notif_fd
);
982 case 0: /* orderly shutdown */
989 DBG("Receive failed from lttng-sessiond with errno %d",
991 if (len
== -ECONNRESET
) {
992 ERR("%s remote end closed connection",
1000 DBG("Incorrect event notifier fd message size: %zd",
1006 args
.event_notifier_handle
.event_notifier_notif_fd
=
1007 event_notifier_notif_fd
;
1009 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1010 (unsigned long) &lum
->u
,
1014 if (args
.event_notifier_handle
.event_notifier_notif_fd
>= 0) {
1015 lttng_ust_lock_fd_tracker();
1016 close_ret
= close(args
.event_notifier_handle
.event_notifier_notif_fd
);
1017 lttng_ust_unlock_fd_tracker();
1023 case LTTNG_UST_ABI_CHANNEL
:
1028 len
= ustcomm_recv_channel_from_sessiond(sock
,
1029 &chan_data
, lum
->u
.channel
.len
,
1032 case 0: /* orderly shutdown */
1036 if (len
== lum
->u
.channel
.len
) {
1037 DBG("channel data received");
1039 } else if (len
< 0) {
1040 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1041 if (len
== -ECONNRESET
) {
1042 ERR("%s remote end closed connection", sock_info
->name
);
1049 DBG("incorrect channel data message size: %zd", len
);
1054 args
.channel
.chan_data
= chan_data
;
1055 args
.channel
.wakeup_fd
= wakeup_fd
;
1057 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1058 (unsigned long) &lum
->u
,
1062 if (args
.channel
.wakeup_fd
>= 0) {
1065 lttng_ust_lock_fd_tracker();
1066 close_ret
= close(args
.channel
.wakeup_fd
);
1067 lttng_ust_unlock_fd_tracker();
1068 args
.channel
.wakeup_fd
= -1;
1072 free(args
.channel
.chan_data
);
1075 case LTTNG_UST_ABI_STREAM
:
1079 /* Receive shm_fd, wakeup_fd */
1080 ret
= ustcomm_recv_stream_from_sessiond(sock
,
1082 &args
.stream
.shm_fd
,
1083 &args
.stream
.wakeup_fd
);
1089 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1090 (unsigned long) &lum
->u
,
1094 if (args
.stream
.shm_fd
>= 0) {
1095 lttng_ust_lock_fd_tracker();
1096 close_ret
= close(args
.stream
.shm_fd
);
1097 lttng_ust_unlock_fd_tracker();
1098 args
.stream
.shm_fd
= -1;
1102 if (args
.stream
.wakeup_fd
>= 0) {
1103 lttng_ust_lock_fd_tracker();
1104 close_ret
= close(args
.stream
.wakeup_fd
);
1105 lttng_ust_unlock_fd_tracker();
1106 args
.stream
.wakeup_fd
= -1;
1112 case LTTNG_UST_ABI_CONTEXT
:
1113 switch (lum
->u
.context
.ctx
) {
1114 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1117 size_t ctxlen
, recvlen
;
1119 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
1120 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
1121 if (ctxlen
>= LTTNG_UST_ABI_SYM_NAME_LEN
) {
1122 ERR("Application context string length size is too large: %zu bytes",
1127 strcpy(ctxstr
, "$app.");
1128 p
= &ctxstr
[strlen("$app.")];
1129 recvlen
= ctxlen
- strlen("$app.");
1130 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
1132 case 0: /* orderly shutdown */
1136 if (len
== recvlen
) {
1137 DBG("app context data received");
1139 } else if (len
< 0) {
1140 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1141 if (len
== -ECONNRESET
) {
1142 ERR("%s remote end closed connection", sock_info
->name
);
1149 DBG("incorrect app context data message size: %zd", len
);
1154 /* Put : between provider and ctxname. */
1155 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1156 args
.app_context
.ctxname
= ctxstr
;
1163 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1164 (unsigned long) &lum
->u
,
1170 case LTTNG_UST_ABI_COUNTER
:
1174 len
= ustcomm_recv_counter_from_sessiond(sock
,
1175 &counter_data
, lum
->u
.counter
.len
);
1177 case 0: /* orderly shutdown */
1181 if (len
== lum
->u
.counter
.len
) {
1182 DBG("counter data received");
1184 } else if (len
< 0) {
1185 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1186 if (len
== -ECONNRESET
) {
1187 ERR("%s remote end closed connection", sock_info
->name
);
1194 DBG("incorrect counter data message size: %zd", len
);
1199 args
.counter
.counter_data
= counter_data
;
1201 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1202 (unsigned long) &lum
->u
,
1206 free(args
.counter
.counter_data
);
1209 case LTTNG_UST_ABI_COUNTER_GLOBAL
:
1211 /* Receive shm_fd */
1212 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1213 &args
.counter_shm
.shm_fd
);
1219 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1220 (unsigned long) &lum
->u
,
1224 if (args
.counter_shm
.shm_fd
>= 0) {
1227 lttng_ust_lock_fd_tracker();
1228 close_ret
= close(args
.counter_shm
.shm_fd
);
1229 lttng_ust_unlock_fd_tracker();
1230 args
.counter_shm
.shm_fd
= -1;
1236 case LTTNG_UST_ABI_COUNTER_CPU
:
1238 /* Receive shm_fd */
1239 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1240 &args
.counter_shm
.shm_fd
);
1246 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1247 (unsigned long) &lum
->u
,
1251 if (args
.counter_shm
.shm_fd
>= 0) {
1254 lttng_ust_lock_fd_tracker();
1255 close_ret
= close(args
.counter_shm
.shm_fd
);
1256 lttng_ust_unlock_fd_tracker();
1257 args
.counter_shm
.shm_fd
= -1;
1263 case LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
:
1265 /* Receive struct lttng_ust_event_notifier */
1266 struct lttng_ust_abi_event_notifier event_notifier
;
1268 if (sizeof(event_notifier
) != lum
->u
.event_notifier
.len
) {
1269 DBG("incorrect event notifier data message size: %u", lum
->u
.event_notifier
.len
);
1273 len
= ustcomm_recv_unix_sock(sock
, &event_notifier
, sizeof(event_notifier
));
1275 case 0: /* orderly shutdown */
1279 if (len
== sizeof(event_notifier
)) {
1280 DBG("event notifier data received");
1282 } else if (len
< 0) {
1283 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1284 if (len
== -ECONNRESET
) {
1285 ERR("%s remote end closed connection", sock_info
->name
);
1292 DBG("incorrect event notifier data message size: %zd", len
);
1298 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1299 (unsigned long) &event_notifier
,
1308 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1309 (unsigned long) &lum
->u
,
1316 lur
.handle
= lum
->handle
;
1320 lur
.ret_code
= LTTNG_UST_OK
;
1323 * Use -LTTNG_UST_ERR as wildcard for UST internal
1324 * error that are not caused by the transport, except if
1325 * we already have a more precise error message to
1328 if (ret
> -LTTNG_UST_ERR
) {
1329 /* Translate code to UST error. */
1332 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1335 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1338 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1341 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1344 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1347 lur
.ret_code
= -LTTNG_UST_ERR
;
1356 case LTTNG_UST_ABI_TRACER_VERSION
:
1357 lur
.u
.version
= lum
->u
.version
;
1359 case LTTNG_UST_ABI_TRACEPOINT_LIST_GET
:
1360 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1364 DBG("Return value: %d", lur
.ret_val
);
1369 * Performed delayed statedump operations outside of the UST
1370 * lock. We need to take the dynamic loader lock before we take
1371 * the UST lock internally within handle_pending_statedump().
1373 handle_pending_statedump(sock_info
);
1376 ret
= -LTTNG_UST_ERR_EXITING
;
1380 ret
= send_reply(sock
, &lur
);
1382 DBG("error sending reply");
1387 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1390 if (lur
.ret_code
== LTTNG_UST_OK
) {
1392 case LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
:
1393 len
= ustcomm_send_unix_sock(sock
,
1394 &args
.field_list
.entry
,
1395 sizeof(args
.field_list
.entry
));
1400 if (len
!= sizeof(args
.field_list
.entry
)) {
1414 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1418 if (sock_info
->root_handle
!= -1) {
1419 ret
= lttng_ust_abi_objd_unref(sock_info
->root_handle
, 1);
1421 ERR("Error unref root handle");
1423 sock_info
->root_handle
= -1;
1425 sock_info
->registration_done
= 0;
1426 sock_info
->initial_statedump_done
= 0;
1429 * wait_shm_mmap, socket and notify socket are used by listener
1430 * threads outside of the ust lock, so we cannot tear them down
1431 * ourselves, because we cannot join on these threads. Leave
1432 * responsibility of cleaning up these resources to the OS
1438 if (sock_info
->socket
!= -1) {
1439 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1441 ERR("Error closing ust cmd socket");
1443 sock_info
->socket
= -1;
1445 if (sock_info
->notify_socket
!= -1) {
1446 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1448 ERR("Error closing ust notify socket");
1450 sock_info
->notify_socket
= -1;
1452 if (sock_info
->wait_shm_mmap
) {
1455 page_size
= LTTNG_UST_PAGE_SIZE
;
1456 if (page_size
<= 0) {
1460 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1462 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1464 ERR("Error unmapping wait shm");
1467 sock_info
->wait_shm_mmap
= NULL
;
1472 * Using fork to set umask in the child process (not multi-thread safe).
1473 * We deal with the shm_open vs ftruncate race (happening when the
1474 * sessiond owns the shm and does not let everybody modify it, to ensure
1475 * safety against shm_unlink) by simply letting the mmap fail and
1476 * retrying after a few seconds.
1477 * For global shm, everybody has rw access to it until the sessiond
1481 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1483 int wait_shm_fd
, ret
;
1487 * Try to open read-only.
1489 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1490 if (wait_shm_fd
>= 0) {
1493 size_t bytes_read
= 0;
1496 * Try to read the fd. If unable to do so, try opening
1500 len
= read(wait_shm_fd
,
1501 &((char *) &tmp_read
)[bytes_read
],
1502 sizeof(tmp_read
) - bytes_read
);
1506 } while ((len
< 0 && errno
== EINTR
)
1507 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1508 if (bytes_read
!= sizeof(tmp_read
)) {
1509 ret
= close(wait_shm_fd
);
1511 ERR("close wait_shm_fd");
1516 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1518 * Real-only open did not work, and it's not because the
1519 * entry was not present. It's a failure that prohibits
1522 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1528 * If the open failed because the file did not exist, or because
1529 * the file was not truncated yet, try creating it ourself.
1531 URCU_TLS(lttng_ust_nest_count
)++;
1533 URCU_TLS(lttng_ust_nest_count
)--;
1538 * Parent: wait for child to return, in which case the
1539 * shared memory map will have been created.
1541 pid
= wait(&status
);
1542 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1547 * Try to open read-only again after creation.
1549 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1550 if (wait_shm_fd
< 0) {
1552 * Real-only open did not work. It's a failure
1553 * that prohibits using shm.
1555 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1559 } else if (pid
== 0) {
1563 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1564 if (sock_info
->global
)
1565 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1567 * We're alone in a child process, so we can modify the
1568 * process-wide umask.
1570 umask(~create_mode
);
1572 * Try creating shm (or get rw access).
1573 * We don't do an exclusive open, because we allow other
1574 * processes to create+ftruncate it concurrently.
1576 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1577 O_RDWR
| O_CREAT
, create_mode
);
1578 if (wait_shm_fd
>= 0) {
1579 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1581 PERROR("ftruncate");
1582 _exit(EXIT_FAILURE
);
1584 _exit(EXIT_SUCCESS
);
1587 * For local shm, we need to have rw access to accept
1588 * opening it: this means the local sessiond will be
1589 * able to wake us up. For global shm, we open it even
1590 * if rw access is not granted, because the root.root
1591 * sessiond will be able to override all rights and wake
1594 if (!sock_info
->global
&& errno
!= EACCES
) {
1595 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1596 _exit(EXIT_FAILURE
);
1599 * The shm exists, but we cannot open it RW. Report
1602 _exit(EXIT_SUCCESS
);
1607 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1608 struct stat statbuf
;
1611 * Ensure that our user is the owner of the shm file for
1612 * local shm. If we do not own the file, it means our
1613 * sessiond will not have access to wake us up (there is
1614 * probably a rogue process trying to fake our
1615 * sessiond). Fallback to polling method in this case.
1617 ret
= fstat(wait_shm_fd
, &statbuf
);
1622 if (statbuf
.st_uid
!= getuid())
1628 ret
= close(wait_shm_fd
);
1630 PERROR("Error closing fd");
1636 char *get_map_shm(struct sock_info
*sock_info
)
1639 int wait_shm_fd
, ret
;
1640 char *wait_shm_mmap
;
1642 page_size
= sysconf(_SC_PAGE_SIZE
);
1643 if (page_size
<= 0) {
1647 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1651 lttng_ust_lock_fd_tracker();
1652 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1653 if (wait_shm_fd
< 0) {
1654 lttng_ust_unlock_fd_tracker();
1658 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1660 ret
= close(wait_shm_fd
);
1662 PERROR("Error closing fd");
1664 lttng_ust_unlock_fd_tracker();
1669 lttng_ust_unlock_fd_tracker();
1671 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1672 MAP_SHARED
, wait_shm_fd
, 0);
1674 /* close shm fd immediately after taking the mmap reference */
1675 lttng_ust_lock_fd_tracker();
1676 ret
= close(wait_shm_fd
);
1678 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1680 PERROR("Error closing fd");
1682 lttng_ust_unlock_fd_tracker();
1684 if (wait_shm_mmap
== MAP_FAILED
) {
1685 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1688 return wait_shm_mmap
;
1695 void wait_for_sessiond(struct sock_info
*sock_info
)
1697 /* Use ust_lock to check if we should quit. */
1701 if (wait_poll_fallback
) {
1706 assert(sock_info
->wait_shm_mmap
);
1708 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1709 /* Wait for futex wakeup */
1710 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1713 while (lttng_ust_futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1714 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1717 /* Value already changed. */
1720 /* Retry if interrupted by signal. */
1721 break; /* Get out of switch. */
1723 wait_poll_fallback
= 1;
1725 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1726 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1727 "Please upgrade your kernel "
1728 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1729 "mainline). LTTng-UST will use polling mode fallback.");
1730 if (ust_err_debug_enabled())
1748 * This thread does not allocate any resource, except within
1749 * handle_message, within mutex protection. This mutex protects against
1751 * The other moment it allocates resources is at socket connection, which
1752 * is also protected by the mutex.
1755 void *ust_listener_thread(void *arg
)
1757 struct sock_info
*sock_info
= arg
;
1758 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1761 lttng_ust_fixup_tls();
1763 * If available, add '-ust' to the end of this thread's
1766 ret
= lttng_ust_setustprocname();
1768 ERR("Unable to set UST process name");
1771 /* Restart trying to connect to the session daemon */
1773 if (prev_connect_failed
) {
1774 /* Wait for sessiond availability with pipe */
1775 wait_for_sessiond(sock_info
);
1779 * Sleep for 5 seconds before retrying after a
1780 * sequence of failure / wait / failure. This
1781 * deals with a killed or broken session daemon.
1787 prev_connect_failed
= 0;
1794 if (sock_info
->socket
!= -1) {
1795 /* FD tracker is updated by ustcomm_close_unix_sock() */
1796 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1798 ERR("Error closing %s ust cmd socket",
1801 sock_info
->socket
= -1;
1803 if (sock_info
->notify_socket
!= -1) {
1804 /* FD tracker is updated by ustcomm_close_unix_sock() */
1805 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1807 ERR("Error closing %s ust notify socket",
1810 sock_info
->notify_socket
= -1;
1815 * Register. We need to perform both connect and sending
1816 * registration message before doing the next connect otherwise
1817 * we may reach unix socket connect queue max limits and block
1818 * on the 2nd connect while the session daemon is awaiting the
1819 * first connect registration message.
1821 /* Connect cmd socket */
1822 lttng_ust_lock_fd_tracker();
1823 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1824 get_connect_sock_timeout());
1826 lttng_ust_unlock_fd_tracker();
1827 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1828 prev_connect_failed
= 1;
1831 * If we cannot find the sessiond daemon, don't delay
1832 * constructor execution.
1834 ret
= handle_register_failed(sock_info
);
1840 ret
= lttng_ust_add_fd_to_tracker(fd
);
1844 PERROR("close on sock_info->socket");
1847 lttng_ust_unlock_fd_tracker();
1852 sock_info
->socket
= ret
;
1853 lttng_ust_unlock_fd_tracker();
1857 * Unlock/relock ust lock because connect is blocking (with
1858 * timeout). Don't delay constructors on the ust lock for too
1866 * Create only one root handle per listener thread for the whole
1867 * process lifetime, so we ensure we get ID which is statically
1868 * assigned to the root handle.
1870 if (sock_info
->root_handle
== -1) {
1871 ret
= lttng_abi_create_root_handle();
1873 ERR("Error creating root handle");
1876 sock_info
->root_handle
= ret
;
1879 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1881 ERR("Error registering to %s ust cmd socket",
1883 prev_connect_failed
= 1;
1885 * If we cannot register to the sessiond daemon, don't
1886 * delay constructor execution.
1888 ret
= handle_register_failed(sock_info
);
1896 * Unlock/relock ust lock because connect is blocking (with
1897 * timeout). Don't delay constructors on the ust lock for too
1904 /* Connect notify socket */
1905 lttng_ust_lock_fd_tracker();
1906 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1907 get_connect_sock_timeout());
1909 lttng_ust_unlock_fd_tracker();
1910 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1911 prev_connect_failed
= 1;
1914 * If we cannot find the sessiond daemon, don't delay
1915 * constructor execution.
1917 ret
= handle_register_failed(sock_info
);
1924 ret
= lttng_ust_add_fd_to_tracker(fd
);
1928 PERROR("close on sock_info->notify_socket");
1931 lttng_ust_unlock_fd_tracker();
1936 sock_info
->notify_socket
= ret
;
1937 lttng_ust_unlock_fd_tracker();
1941 * Unlock/relock ust lock because connect is blocking (with
1942 * timeout). Don't delay constructors on the ust lock for too
1949 timeout
= get_notify_sock_timeout();
1952 * Give at least 10ms to sessiond to reply to
1957 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1960 WARN("Error setting socket receive timeout");
1962 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1965 WARN("Error setting socket send timeout");
1967 } else if (timeout
< -1) {
1968 WARN("Unsupported timeout value %ld", timeout
);
1971 ret
= register_to_sessiond(sock_info
->notify_socket
,
1972 USTCTL_SOCKET_NOTIFY
);
1974 ERR("Error registering to %s ust notify socket",
1976 prev_connect_failed
= 1;
1978 * If we cannot register to the sessiond daemon, don't
1979 * delay constructor execution.
1981 ret
= handle_register_failed(sock_info
);
1986 sock
= sock_info
->socket
;
1992 struct ustcomm_ust_msg lum
;
1994 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1996 case 0: /* orderly shutdown */
1997 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
2002 * Either sessiond has shutdown or refused us by closing the socket.
2003 * In either case, we don't want to delay construction execution,
2004 * and we need to wait before retry.
2006 prev_connect_failed
= 1;
2008 * If we cannot register to the sessiond daemon, don't
2009 * delay constructor execution.
2011 ret
= handle_register_failed(sock_info
);
2016 print_cmd(lum
.cmd
, lum
.handle
);
2017 ret
= handle_message(sock_info
, sock
, &lum
);
2019 ERR("Error handling message for %s socket",
2022 * Close socket if protocol error is
2030 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
2032 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
2034 if (len
== -ECONNRESET
) {
2035 DBG("%s remote end closed connection", sock_info
->name
);
2046 /* Cleanup socket handles before trying to reconnect */
2047 lttng_ust_abi_objd_table_owner_cleanup(sock_info
);
2049 goto restart
; /* try to reconnect */
2054 pthread_mutex_lock(&ust_exit_mutex
);
2055 sock_info
->thread_active
= 0;
2056 pthread_mutex_unlock(&ust_exit_mutex
);
2061 * Weak symbol to call when the ust malloc wrapper is not loaded.
2063 __attribute__((weak
))
2064 void lttng_ust_libc_wrapper_malloc_init(void)
2069 * sessiond monitoring thread: monitor presence of global and per-user
2070 * sessiond by polling the application common named pipe.
2073 void lttng_ust_init(void)
2074 __attribute__((constructor
));
2076 void lttng_ust_init(void)
2078 struct timespec constructor_timeout
;
2079 sigset_t sig_all_blocked
, orig_parent_mask
;
2080 pthread_attr_t thread_attr
;
2085 if (uatomic_xchg(&initialized
, 1) == 1)
2089 * Fixup interdependency between TLS fixup mutex (which happens
2090 * to be the dynamic linker mutex) and ust_lock, taken within
2093 lttng_ust_fixup_tls();
2095 lttng_ust_loaded
= 1;
2098 * We need to ensure that the liblttng-ust library is not unloaded to avoid
2099 * the unloading of code used by the ust_listener_threads as we can not
2100 * reliably know when they exited. To do that, manually load
2101 * liblttng-ust.so to increment the dynamic loader's internal refcount for
2102 * this library so it never becomes zero, thus never gets unloaded from the
2103 * address space of the process. Since we are already running in the
2104 * constructor of the LTTNG_UST_LIB_SONAME library, calling dlopen will
2105 * simply increment the refcount and no additionnal work is needed by the
2106 * dynamic loader as the shared library is already loaded in the address
2107 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
2108 * unloading of the UST library if its refcount becomes zero (which should
2109 * never happen). Do the return value check but discard the handle at the
2110 * end of the function as it's not needed.
2112 handle
= dlopen(LTTNG_UST_LIB_SONAME
, RTLD_LAZY
| RTLD_NODELETE
);
2114 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SONAME
);
2118 * We want precise control over the order in which we construct
2119 * our sub-libraries vs starting to receive commands from
2120 * sessiond (otherwise leading to errors when trying to create
2121 * sessiond before the init functions are completed).
2124 lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */
2125 lttng_ust_tp_init();
2126 lttng_ust_init_fd_tracker();
2127 lttng_ust_clock_init();
2128 lttng_ust_getcpu_init();
2129 lttng_ust_statedump_init();
2130 lttng_ust_ring_buffer_clients_init();
2131 lttng_ust_counter_clients_init();
2132 lttng_perf_counter_init();
2134 * Invoke ust malloc wrapper init before starting other threads.
2136 lttng_ust_libc_wrapper_malloc_init();
2138 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
2140 get_allow_blocking();
2142 ret
= sem_init(&constructor_wait
, 0, 0);
2147 ret
= setup_global_apps();
2149 assert(global_apps
.allowed
== 0);
2150 DBG("global apps setup returned %d", ret
);
2153 ret
= setup_local_apps();
2155 assert(local_apps
.allowed
== 0);
2156 DBG("local apps setup returned %d", ret
);
2159 /* A new thread created by pthread_create inherits the signal mask
2160 * from the parent. To avoid any signal being received by the
2161 * listener thread, we block all signals temporarily in the parent,
2162 * while we create the listener thread.
2164 sigfillset(&sig_all_blocked
);
2165 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
2167 ERR("pthread_sigmask: %s", strerror(ret
));
2170 ret
= pthread_attr_init(&thread_attr
);
2172 ERR("pthread_attr_init: %s", strerror(ret
));
2174 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
2176 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
2179 if (global_apps
.allowed
) {
2180 pthread_mutex_lock(&ust_exit_mutex
);
2181 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
2182 ust_listener_thread
, &global_apps
);
2184 ERR("pthread_create global: %s", strerror(ret
));
2186 global_apps
.thread_active
= 1;
2187 pthread_mutex_unlock(&ust_exit_mutex
);
2189 handle_register_done(&global_apps
);
2192 if (local_apps
.allowed
) {
2193 pthread_mutex_lock(&ust_exit_mutex
);
2194 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
2195 ust_listener_thread
, &local_apps
);
2197 ERR("pthread_create local: %s", strerror(ret
));
2199 local_apps
.thread_active
= 1;
2200 pthread_mutex_unlock(&ust_exit_mutex
);
2202 handle_register_done(&local_apps
);
2204 ret
= pthread_attr_destroy(&thread_attr
);
2206 ERR("pthread_attr_destroy: %s", strerror(ret
));
2209 /* Restore original signal mask in parent */
2210 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
2212 ERR("pthread_sigmask: %s", strerror(ret
));
2215 switch (timeout_mode
) {
2216 case 1: /* timeout wait */
2218 ret
= sem_timedwait(&constructor_wait
,
2219 &constructor_timeout
);
2220 } while (ret
< 0 && errno
== EINTR
);
2224 ERR("Timed out waiting for lttng-sessiond");
2227 PERROR("sem_timedwait");
2230 ERR("Unexpected error \"%s\" returned by sem_timedwait",
2235 case -1:/* wait forever */
2237 ret
= sem_wait(&constructor_wait
);
2238 } while (ret
< 0 && errno
== EINTR
);
2245 ERR("Unexpected error \"%s\" returned by sem_wait",
2250 case 0: /* no timeout */
2256 void lttng_ust_cleanup(int exiting
)
2258 cleanup_sock_info(&global_apps
, exiting
);
2259 cleanup_sock_info(&local_apps
, exiting
);
2260 local_apps
.allowed
= 0;
2261 global_apps
.allowed
= 0;
2263 * The teardown in this function all affect data structures
2264 * accessed under the UST lock by the listener thread. This
2265 * lock, along with the lttng_ust_comm_should_quit flag, ensure
2266 * that none of these threads are accessing this data at this
2269 lttng_ust_abi_exit();
2270 lttng_ust_abi_events_exit();
2271 lttng_perf_counter_exit();
2272 lttng_ust_ring_buffer_clients_exit();
2273 lttng_ust_counter_clients_exit();
2274 lttng_ust_statedump_destroy();
2275 lttng_ust_tp_exit();
2277 /* Reinitialize values for fork */
2278 sem_count
= sem_count_initial_value
;
2279 lttng_ust_comm_should_quit
= 0;
2285 void lttng_ust_exit(void)
2286 __attribute__((destructor
));
2288 void lttng_ust_exit(void)
2293 * Using pthread_cancel here because:
2294 * A) we don't want to hang application teardown.
2295 * B) the thread is not allocating any resource.
2299 * Require the communication thread to quit. Synchronize with
2300 * mutexes to ensure it is not in a mutex critical section when
2301 * pthread_cancel is later called.
2304 lttng_ust_comm_should_quit
= 1;
2307 pthread_mutex_lock(&ust_exit_mutex
);
2308 /* cancel threads */
2309 if (global_apps
.thread_active
) {
2310 ret
= pthread_cancel(global_apps
.ust_listener
);
2312 ERR("Error cancelling global ust listener thread: %s",
2315 global_apps
.thread_active
= 0;
2318 if (local_apps
.thread_active
) {
2319 ret
= pthread_cancel(local_apps
.ust_listener
);
2321 ERR("Error cancelling local ust listener thread: %s",
2324 local_apps
.thread_active
= 0;
2327 pthread_mutex_unlock(&ust_exit_mutex
);
2330 * Do NOT join threads: use of sys_futex makes it impossible to
2331 * join the threads without using async-cancel, but async-cancel
2332 * is delivered by a signal, which could hit the target thread
2333 * anywhere in its code path, including while the ust_lock() is
2334 * held, causing a deadlock for the other thread. Let the OS
2335 * cleanup the threads if there are stalled in a syscall.
2337 lttng_ust_cleanup(1);
2341 void ust_context_ns_reset(void)
2343 lttng_context_pid_ns_reset();
2344 lttng_context_cgroup_ns_reset();
2345 lttng_context_ipc_ns_reset();
2346 lttng_context_mnt_ns_reset();
2347 lttng_context_net_ns_reset();
2348 lttng_context_user_ns_reset();
2349 lttng_context_time_ns_reset();
2350 lttng_context_uts_ns_reset();
2354 void ust_context_vuids_reset(void)
2356 lttng_context_vuid_reset();
2357 lttng_context_veuid_reset();
2358 lttng_context_vsuid_reset();
2362 void ust_context_vgids_reset(void)
2364 lttng_context_vgid_reset();
2365 lttng_context_vegid_reset();
2366 lttng_context_vsgid_reset();
2370 * We exclude the worker threads across fork and clone (except
2371 * CLONE_VM), because these system calls only keep the forking thread
2372 * running in the child. Therefore, we don't want to call fork or clone
2373 * in the middle of an tracepoint or ust tracing state modification.
2374 * Holding this mutex protects these structures across fork and clone.
2376 void lttng_ust_before_fork(sigset_t
*save_sigset
)
2379 * Disable signals. This is to avoid that the child intervenes
2380 * before it is properly setup for tracing. It is safer to
2381 * disable all signals, because then we know we are not breaking
2382 * anything by restoring the original mask.
2387 /* Fixup lttng-ust TLS. */
2388 lttng_ust_fixup_tls();
2390 if (URCU_TLS(lttng_ust_nest_count
))
2392 /* Disable signals */
2393 sigfillset(&all_sigs
);
2394 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2396 PERROR("sigprocmask");
2399 pthread_mutex_lock(&ust_fork_mutex
);
2402 lttng_ust_urcu_before_fork();
2403 lttng_ust_lock_fd_tracker();
2407 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2411 DBG("process %d", getpid());
2412 lttng_perf_unlock();
2413 lttng_ust_unlock_fd_tracker();
2416 pthread_mutex_unlock(&ust_fork_mutex
);
2418 /* Restore signals */
2419 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2421 PERROR("sigprocmask");
2425 void lttng_ust_after_fork_parent(sigset_t
*restore_sigset
)
2427 if (URCU_TLS(lttng_ust_nest_count
))
2429 DBG("process %d", getpid());
2430 lttng_ust_urcu_after_fork_parent();
2431 /* Release mutexes and reenable signals */
2432 ust_after_fork_common(restore_sigset
);
2436 * After fork, in the child, we need to cleanup all the leftover state,
2437 * except the worker thread which already magically disappeared thanks
2438 * to the weird Linux fork semantics. After tyding up, we call
2439 * lttng_ust_init() again to start over as a new PID.
2441 * This is meant for forks() that have tracing in the child between the
2442 * fork and following exec call (if there is any).
2444 void lttng_ust_after_fork_child(sigset_t
*restore_sigset
)
2446 if (URCU_TLS(lttng_ust_nest_count
))
2448 lttng_context_vpid_reset();
2449 lttng_context_vtid_reset();
2450 lttng_ust_context_procname_reset();
2451 ust_context_ns_reset();
2452 ust_context_vuids_reset();
2453 ust_context_vgids_reset();
2454 DBG("process %d", getpid());
2455 /* Release urcu mutexes */
2456 lttng_ust_urcu_after_fork_child();
2457 lttng_ust_cleanup(0);
2458 /* Release mutexes and reenable signals */
2459 ust_after_fork_common(restore_sigset
);
2463 void lttng_ust_after_setns(void)
2465 ust_context_ns_reset();
2466 ust_context_vuids_reset();
2467 ust_context_vgids_reset();
2470 void lttng_ust_after_unshare(void)
2472 ust_context_ns_reset();
2473 ust_context_vuids_reset();
2474 ust_context_vgids_reset();
2477 void lttng_ust_after_setuid(void)
2479 ust_context_vuids_reset();
2482 void lttng_ust_after_seteuid(void)
2484 ust_context_vuids_reset();
2487 void lttng_ust_after_setreuid(void)
2489 ust_context_vuids_reset();
2492 void lttng_ust_after_setresuid(void)
2494 ust_context_vuids_reset();
2497 void lttng_ust_after_setgid(void)
2499 ust_context_vgids_reset();
2502 void lttng_ust_after_setegid(void)
2504 ust_context_vgids_reset();
2507 void lttng_ust_after_setregid(void)
2509 ust_context_vgids_reset();
2512 void lttng_ust_after_setresgid(void)
2514 ust_context_vgids_reset();
2517 void lttng_ust_sockinfo_session_enabled(void *owner
)
2519 struct sock_info
*sock_info
= owner
;
2520 sock_info
->statedump_pending
= 1;