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>
28 #include <urcu/compiler.h>
29 #include <lttng/urcu/urcu-ust.h>
31 #include <lttng/ust-utils.h>
32 #include <lttng/ust-events.h>
33 #include <lttng/ust-abi.h>
34 #include <lttng/ust-fork.h>
35 #include <lttng/ust-error.h>
36 #include <lttng/ust-ctl.h>
37 #include <lttng/ust-libc-wrapper.h>
38 #include <lttng/ust-thread.h>
39 #include <lttng/ust-tracer.h>
40 #include <lttng/ust-common.h>
41 #include <urcu/tls-compat.h>
42 #include "common/compat/futex.h"
43 #include "common/ustcomm.h"
44 #include "common/ust-fd.h"
45 #include "common/logging.h"
46 #include "common/macros.h"
47 #include "common/tracepoint.h"
48 #include "lttng-tracer-core.h"
49 #include "common/compat/pthread.h"
50 #include "common/procname.h"
51 #include "common/ringbuffer/rb-init.h"
52 #include "lttng-ust-statedump.h"
54 #include "lib/lttng-ust/getcpu.h"
55 #include "common/getenv.h"
56 #include "lib/lttng-ust/events.h"
57 #include "context-internal.h"
58 #include "common/align.h"
59 #include "lttng-counter-client.h"
60 #include "lttng-rb-clients.h"
63 * Has lttng ust comm constructor been called ?
65 static int initialized
;
68 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
69 * Held when handling a command, also held by fork() to deal with
70 * removal of threads, and by exit path.
72 * The UST lock is the centralized mutex across UST tracing control and
75 * ust_exit_mutex must never nest in ust_mutex.
77 * ust_fork_mutex must never nest in ust_mutex.
79 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
80 * counter lazy initialization called by events within the statedump,
81 * which traces while the ust_mutex is held.
83 * ust_lock nests within the dynamic loader lock (within glibc) because
84 * it is taken within the library constructor.
86 * The ust fd tracker lock nests within the ust_mutex.
88 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
90 /* Allow nesting the ust_mutex within the same thread. */
91 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
94 * ust_exit_mutex protects thread_active variable wrt thread exit. It
95 * cannot be done by ust_mutex because pthread_cancel(), which takes an
96 * internal libc lock, cannot nest within ust_mutex.
98 * It never nests within a ust_mutex.
100 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
103 * ust_fork_mutex protects base address statedump tracing against forks. It
104 * prevents the dynamic loader lock to be taken (by base address statedump
105 * tracing) while a fork is happening, thus preventing deadlock issues with
106 * the dynamic loader lock.
108 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
110 /* Should the ust comm thread quit ? */
111 static int lttng_ust_comm_should_quit
;
114 * This variable can be tested by applications to check whether
115 * lttng-ust is loaded. They simply have to define their own
116 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
117 * library constructor.
119 int lttng_ust_loaded
__attribute__((weak
));
122 * Return 0 on success, -1 if should quit.
123 * The lock is taken in both cases.
128 sigset_t sig_all_blocked
, orig_mask
;
131 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
133 ERR("pthread_setcancelstate: %s", strerror(ret
));
135 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
136 ERR("pthread_setcancelstate: unexpected oldstate");
138 sigfillset(&sig_all_blocked
);
139 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
141 ERR("pthread_sigmask: %s", strerror(ret
));
143 if (!URCU_TLS(ust_mutex_nest
)++)
144 pthread_mutex_lock(&ust_mutex
);
145 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
147 ERR("pthread_sigmask: %s", strerror(ret
));
149 if (lttng_ust_comm_should_quit
) {
157 * ust_lock_nocheck() can be used in constructors/destructors, because
158 * they are already nested within the dynamic loader lock, and therefore
159 * have exclusive access against execution of liblttng-ust destructor.
162 void ust_lock_nocheck(void)
164 sigset_t sig_all_blocked
, orig_mask
;
167 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
169 ERR("pthread_setcancelstate: %s", strerror(ret
));
171 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
172 ERR("pthread_setcancelstate: unexpected oldstate");
174 sigfillset(&sig_all_blocked
);
175 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
177 ERR("pthread_sigmask: %s", strerror(ret
));
179 if (!URCU_TLS(ust_mutex_nest
)++)
180 pthread_mutex_lock(&ust_mutex
);
181 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
183 ERR("pthread_sigmask: %s", strerror(ret
));
190 void ust_unlock(void)
192 sigset_t sig_all_blocked
, orig_mask
;
195 sigfillset(&sig_all_blocked
);
196 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
198 ERR("pthread_sigmask: %s", strerror(ret
));
200 if (!--URCU_TLS(ust_mutex_nest
))
201 pthread_mutex_unlock(&ust_mutex
);
202 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
204 ERR("pthread_sigmask: %s", strerror(ret
));
206 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
208 ERR("pthread_setcancelstate: %s", strerror(ret
));
210 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
211 ERR("pthread_setcancelstate: unexpected oldstate");
216 * Wait for either of these before continuing to the main
218 * - the register_done message from sessiond daemon
219 * (will let the sessiond daemon enable sessions before main
221 * - sessiond daemon is not reachable.
222 * - timeout (ensuring applications are resilient to session
225 static sem_t constructor_wait
;
227 * Doing this for both the global and local sessiond.
230 sem_count_initial_value
= 4,
233 static int sem_count
= sem_count_initial_value
;
236 * Counting nesting within lttng-ust. Used to ensure that calling fork()
237 * from liblttng-ust does not execute the pre/post fork handlers.
239 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
242 * Info about socket and associated listener thread.
246 pthread_t ust_listener
; /* listener thread */
248 int registration_done
;
253 char sock_path
[PATH_MAX
];
257 char wait_shm_path
[PATH_MAX
];
259 /* Keep track of lazy state dump not performed yet. */
260 int statedump_pending
;
261 int initial_statedump_done
;
262 /* Keep procname for statedump */
263 char procname
[LTTNG_UST_ABI_PROCNAME_LEN
];
266 /* Socket from app (connect) to session daemon (listen) for communication */
267 struct sock_info global_apps
= {
272 .registration_done
= 0,
276 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
280 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
282 .statedump_pending
= 0,
283 .initial_statedump_done
= 0,
287 /* TODO: allow global_apps_sock_path override */
289 struct sock_info local_apps
= {
293 .registration_done
= 0,
294 .allowed
= 0, /* Check setuid bit first */
300 .statedump_pending
= 0,
301 .initial_statedump_done
= 0,
305 static int wait_poll_fallback
;
307 static const char *cmd_name_mapping
[] = {
308 [ LTTNG_UST_ABI_RELEASE
] = "Release",
309 [ LTTNG_UST_ABI_SESSION
] = "Create Session",
310 [ LTTNG_UST_ABI_TRACER_VERSION
] = "Get Tracer Version",
312 [ LTTNG_UST_ABI_TRACEPOINT_LIST
] = "Create Tracepoint List",
313 [ LTTNG_UST_ABI_WAIT_QUIESCENT
] = "Wait for Quiescent State",
314 [ LTTNG_UST_ABI_REGISTER_DONE
] = "Registration Done",
315 [ LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
317 [ LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
] = "Create event notifier group",
319 /* Session FD commands */
320 [ LTTNG_UST_ABI_CHANNEL
] = "Create Channel",
321 [ LTTNG_UST_ABI_SESSION_START
] = "Start Session",
322 [ LTTNG_UST_ABI_SESSION_STOP
] = "Stop Session",
324 /* Channel FD commands */
325 [ LTTNG_UST_ABI_STREAM
] = "Create Stream",
326 [ LTTNG_UST_ABI_EVENT
] = "Create Event",
328 /* Event and Channel FD commands */
329 [ LTTNG_UST_ABI_CONTEXT
] = "Create Context",
330 [ LTTNG_UST_ABI_FLUSH_BUFFER
] = "Flush Buffer",
332 /* Event, Channel and Session commands */
333 [ LTTNG_UST_ABI_ENABLE
] = "Enable",
334 [ LTTNG_UST_ABI_DISABLE
] = "Disable",
336 /* Tracepoint list commands */
337 [ LTTNG_UST_ABI_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
338 [ LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
340 /* Event FD commands */
341 [ LTTNG_UST_ABI_FILTER
] = "Create Filter",
342 [ LTTNG_UST_ABI_EXCLUSION
] = "Add exclusions to event",
344 /* Event notifier group commands */
345 [ LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
] = "Create event notifier",
347 /* Session and event notifier group commands */
348 [ LTTNG_UST_ABI_COUNTER
] = "Create Counter",
350 /* Counter commands */
351 [ LTTNG_UST_ABI_COUNTER_GLOBAL
] = "Create Counter Global",
352 [ LTTNG_UST_ABI_COUNTER_CPU
] = "Create Counter CPU",
355 static const char *str_timeout
;
356 static int got_timeout_env
;
358 static char *get_map_shm(struct sock_info
*sock_info
);
360 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
363 size_t copied
= 0, to_copy
= len
;
366 ret
= read(fd
, buf
+ copied
, to_copy
);
371 } while ((ret
> 0 && to_copy
> 0)
372 || (ret
< 0 && errno
== EINTR
));
379 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
383 const char *get_lttng_home_dir(void)
387 val
= (const char *) lttng_ust_getenv("LTTNG_HOME");
391 return (const char *) lttng_ust_getenv("HOME");
395 * Force a read (imply TLS fixup for dlopen) of TLS variables.
398 void lttng_fixup_nest_count_tls(void)
400 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
404 void lttng_fixup_ust_mutex_nest_tls(void)
406 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
410 * Fixup lttng-ust urcu TLS.
413 void lttng_fixup_lttng_ust_urcu_tls(void)
415 (void) lttng_ust_urcu_read_ongoing();
418 void lttng_ust_fixup_tls(void)
420 lttng_fixup_lttng_ust_urcu_tls();
421 lttng_fixup_ringbuffer_tls();
422 lttng_fixup_vtid_tls();
423 lttng_fixup_nest_count_tls();
424 lttng_fixup_procname_tls();
425 lttng_fixup_ust_mutex_nest_tls();
426 lttng_ust_fixup_perf_counter_tls();
427 lttng_ust_fixup_fd_tracker_tls();
428 lttng_fixup_cgroup_ns_tls();
429 lttng_fixup_ipc_ns_tls();
430 lttng_fixup_net_ns_tls();
431 lttng_fixup_time_ns_tls();
432 lttng_fixup_uts_ns_tls();
433 lttng_ust_fixup_ring_buffer_client_discard_tls();
434 lttng_ust_fixup_ring_buffer_client_discard_rt_tls();
435 lttng_ust_fixup_ring_buffer_client_overwrite_tls();
436 lttng_ust_fixup_ring_buffer_client_overwrite_rt_tls();
440 * LTTng-UST uses Global Dynamic model TLS variables rather than IE
441 * model because many versions of glibc don't preallocate a pool large
442 * enough for TLS variables IE model defined in other shared libraries,
443 * and causes issues when using LTTng-UST for Java tracing.
445 * Because of this use of Global Dynamic TLS variables, users wishing to
446 * trace from signal handlers need to explicitly trigger the lazy
447 * allocation of those variables for each thread before using them.
448 * This can be triggered by calling lttng_ust_init_thread().
450 void lttng_ust_init_thread(void)
453 * Because those TLS variables are global dynamic, we need to
454 * ensure those are initialized before a signal handler nesting over
455 * this thread attempts to use them.
457 lttng_ust_fixup_tls();
460 int lttng_get_notify_socket(void *owner
)
462 struct sock_info
*info
= owner
;
464 return info
->notify_socket
;
468 char* lttng_ust_sockinfo_get_procname(void *owner
)
470 struct sock_info
*info
= owner
;
472 return info
->procname
;
476 void print_cmd(int cmd
, int handle
)
478 const char *cmd_name
= "Unknown";
480 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
481 && cmd_name_mapping
[cmd
]) {
482 cmd_name
= cmd_name_mapping
[cmd
];
484 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
486 lttng_ust_obj_get_name(handle
), handle
);
490 int setup_global_apps(void)
493 assert(!global_apps
.wait_shm_mmap
);
495 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
496 if (!global_apps
.wait_shm_mmap
) {
497 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
498 global_apps
.allowed
= 0;
503 global_apps
.allowed
= 1;
504 lttng_pthread_getname_np(global_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
509 int setup_local_apps(void)
512 const char *home_dir
;
515 assert(!local_apps
.wait_shm_mmap
);
519 * Disallow per-user tracing for setuid binaries.
521 if (uid
!= geteuid()) {
522 assert(local_apps
.allowed
== 0);
526 home_dir
= get_lttng_home_dir();
528 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
529 assert(local_apps
.allowed
== 0);
533 local_apps
.allowed
= 1;
534 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
536 LTTNG_DEFAULT_HOME_RUNDIR
,
537 LTTNG_UST_SOCK_FILENAME
);
538 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
539 LTTNG_UST_WAIT_FILENAME
,
542 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
543 if (!local_apps
.wait_shm_mmap
) {
544 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
545 local_apps
.allowed
= 0;
550 lttng_pthread_getname_np(local_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
556 * Get socket timeout, in ms.
557 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
560 long get_timeout(void)
562 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
564 if (!got_timeout_env
) {
565 str_timeout
= lttng_ust_getenv("LTTNG_UST_REGISTER_TIMEOUT");
569 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
570 /* All negative values are considered as "-1". */
571 if (constructor_delay_ms
< -1)
572 constructor_delay_ms
= -1;
573 return constructor_delay_ms
;
576 /* Timeout for notify socket send and recv. */
578 long get_notify_sock_timeout(void)
580 return get_timeout();
583 /* Timeout for connecting to cmd and notify sockets. */
585 long get_connect_sock_timeout(void)
587 return get_timeout();
591 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
594 int get_constructor_timeout(struct timespec
*constructor_timeout
)
596 long constructor_delay_ms
;
599 constructor_delay_ms
= get_timeout();
601 switch (constructor_delay_ms
) {
602 case -1:/* fall-through */
604 return constructor_delay_ms
;
610 * If we are unable to find the current time, don't wait.
612 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
617 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
618 constructor_timeout
->tv_nsec
+=
619 (constructor_delay_ms
% 1000UL) * 1000000UL;
620 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
621 constructor_timeout
->tv_sec
++;
622 constructor_timeout
->tv_nsec
-= 1000000000UL;
624 /* Timeout wait (constructor_delay_ms). */
629 void get_allow_blocking(void)
631 const char *str_allow_blocking
=
632 lttng_ust_getenv("LTTNG_UST_ALLOW_BLOCKING");
634 if (str_allow_blocking
) {
635 DBG("%s environment variable is set",
636 "LTTNG_UST_ALLOW_BLOCKING");
637 lttng_ust_ringbuffer_set_allow_blocking();
642 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
644 return ustcomm_send_reg_msg(socket
,
647 lttng_ust_rb_alignof(uint8_t) * CHAR_BIT
,
648 lttng_ust_rb_alignof(uint16_t) * CHAR_BIT
,
649 lttng_ust_rb_alignof(uint32_t) * CHAR_BIT
,
650 lttng_ust_rb_alignof(uint64_t) * CHAR_BIT
,
651 lttng_ust_rb_alignof(unsigned long) * CHAR_BIT
);
655 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
659 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
662 DBG("message successfully sent");
665 if (len
== -ECONNRESET
) {
666 DBG("remote end closed connection");
671 DBG("incorrect message size: %zd", len
);
677 void decrement_sem_count(unsigned int count
)
681 assert(uatomic_read(&sem_count
) >= count
);
683 if (uatomic_read(&sem_count
) <= 0) {
687 ret
= uatomic_add_return(&sem_count
, -count
);
689 ret
= sem_post(&constructor_wait
);
695 int handle_register_done(struct sock_info
*sock_info
)
697 if (sock_info
->registration_done
)
699 sock_info
->registration_done
= 1;
701 decrement_sem_count(1);
702 if (!sock_info
->statedump_pending
) {
703 sock_info
->initial_statedump_done
= 1;
704 decrement_sem_count(1);
711 int handle_register_failed(struct sock_info
*sock_info
)
713 if (sock_info
->registration_done
)
715 sock_info
->registration_done
= 1;
716 sock_info
->initial_statedump_done
= 1;
718 decrement_sem_count(2);
724 * Only execute pending statedump after the constructor semaphore has
725 * been posted by the current listener thread. This means statedump will
726 * only be performed after the "registration done" command is received
727 * from this thread's session daemon.
729 * This ensures we don't run into deadlock issues with the dynamic
730 * loader mutex, which is held while the constructor is called and
731 * waiting on the constructor semaphore. All operations requiring this
732 * dynamic loader lock need to be postponed using this mechanism.
734 * In a scenario with two session daemons connected to the application,
735 * it is possible that the first listener thread which receives the
736 * registration done command issues its statedump while the dynamic
737 * loader lock is still held by the application constructor waiting on
738 * the semaphore. It will however be allowed to proceed when the
739 * second session daemon sends the registration done command to the
740 * second listener thread. This situation therefore does not produce
744 void handle_pending_statedump(struct sock_info
*sock_info
)
746 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
747 sock_info
->statedump_pending
= 0;
748 pthread_mutex_lock(&ust_fork_mutex
);
749 lttng_handle_pending_statedump(sock_info
);
750 pthread_mutex_unlock(&ust_fork_mutex
);
752 if (!sock_info
->initial_statedump_done
) {
753 sock_info
->initial_statedump_done
= 1;
754 decrement_sem_count(1);
760 const char *bytecode_type_str(uint32_t cmd
)
763 case LTTNG_UST_ABI_CAPTURE
:
765 case LTTNG_UST_ABI_FILTER
:
773 int handle_bytecode_recv(struct sock_info
*sock_info
,
774 int sock
, struct ustcomm_ust_msg
*lum
)
776 struct lttng_ust_bytecode_node
*bytecode
= NULL
;
777 enum lttng_ust_bytecode_type type
;
778 const struct lttng_ust_abi_objd_ops
*ops
;
779 uint32_t data_size
, data_size_max
, reloc_offset
;
785 case LTTNG_UST_ABI_FILTER
:
786 type
= LTTNG_UST_BYTECODE_TYPE_FILTER
;
787 data_size
= lum
->u
.filter
.data_size
;
788 data_size_max
= LTTNG_UST_ABI_FILTER_BYTECODE_MAX_LEN
;
789 reloc_offset
= lum
->u
.filter
.reloc_offset
;
790 seqnum
= lum
->u
.filter
.seqnum
;
792 case LTTNG_UST_ABI_CAPTURE
:
793 type
= LTTNG_UST_BYTECODE_TYPE_CAPTURE
;
794 data_size
= lum
->u
.capture
.data_size
;
795 data_size_max
= LTTNG_UST_ABI_CAPTURE_BYTECODE_MAX_LEN
;
796 reloc_offset
= lum
->u
.capture
.reloc_offset
;
797 seqnum
= lum
->u
.capture
.seqnum
;
803 if (data_size
> data_size_max
) {
804 ERR("Bytecode %s data size is too large: %u bytes",
805 bytecode_type_str(lum
->cmd
), data_size
);
810 if (reloc_offset
> data_size
) {
811 ERR("Bytecode %s reloc offset %u is not within data",
812 bytecode_type_str(lum
->cmd
), reloc_offset
);
817 /* Allocate the structure AND the `data[]` field. */
818 bytecode
= zmalloc(sizeof(*bytecode
) + data_size
);
824 bytecode
->bc
.len
= data_size
;
825 bytecode
->bc
.reloc_offset
= reloc_offset
;
826 bytecode
->bc
.seqnum
= seqnum
;
827 bytecode
->type
= type
;
829 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
, bytecode
->bc
.len
);
831 case 0: /* orderly shutdown */
835 if (len
== bytecode
->bc
.len
) {
836 DBG("Bytecode %s data received",
837 bytecode_type_str(lum
->cmd
));
839 } else if (len
< 0) {
840 DBG("Receive failed from lttng-sessiond with errno %d",
842 if (len
== -ECONNRESET
) {
843 ERR("%s remote end closed connection",
851 DBG("Incorrect %s bytecode data message size: %zd",
852 bytecode_type_str(lum
->cmd
), len
);
858 ops
= lttng_ust_abi_objd_ops(lum
->handle
);
865 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
866 (unsigned long) &bytecode
,
877 int handle_message(struct sock_info
*sock_info
,
878 int sock
, struct ustcomm_ust_msg
*lum
)
881 const struct lttng_ust_abi_objd_ops
*ops
;
882 struct ustcomm_ust_reply lur
;
883 union lttng_ust_abi_args args
;
884 char ctxstr
[LTTNG_UST_ABI_SYM_NAME_LEN
]; /* App context string. */
887 memset(&lur
, 0, sizeof(lur
));
890 ret
= -LTTNG_UST_ERR_EXITING
;
894 ops
= lttng_ust_abi_objd_ops(lum
->handle
);
901 case LTTNG_UST_ABI_REGISTER_DONE
:
902 if (lum
->handle
== LTTNG_UST_ABI_ROOT_HANDLE
)
903 ret
= handle_register_done(sock_info
);
907 case LTTNG_UST_ABI_RELEASE
:
908 if (lum
->handle
== LTTNG_UST_ABI_ROOT_HANDLE
)
911 ret
= lttng_ust_abi_objd_unref(lum
->handle
, 1);
913 case LTTNG_UST_ABI_CAPTURE
:
914 case LTTNG_UST_ABI_FILTER
:
915 ret
= handle_bytecode_recv(sock_info
, sock
, lum
);
919 case LTTNG_UST_ABI_EXCLUSION
:
921 /* Receive exclusion names */
922 struct lttng_ust_excluder_node
*node
;
925 count
= lum
->u
.exclusion
.count
;
927 /* There are no names to read */
931 node
= zmalloc(sizeof(*node
) +
932 count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
937 node
->excluder
.count
= count
;
938 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
939 count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
941 case 0: /* orderly shutdown */
946 if (len
== count
* LTTNG_UST_ABI_SYM_NAME_LEN
) {
947 DBG("Exclusion data received");
949 } else if (len
< 0) {
950 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
951 if (len
== -ECONNRESET
) {
952 ERR("%s remote end closed connection", sock_info
->name
);
961 DBG("Incorrect exclusion data message size: %zd", len
);
968 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
969 (unsigned long) &node
,
976 case LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
:
978 int event_notifier_notif_fd
, close_ret
;
980 len
= ustcomm_recv_event_notifier_notif_fd_from_sessiond(sock
,
981 &event_notifier_notif_fd
);
983 case 0: /* orderly shutdown */
990 DBG("Receive failed from lttng-sessiond with errno %d",
992 if (len
== -ECONNRESET
) {
993 ERR("%s remote end closed connection",
1001 DBG("Incorrect event notifier fd message size: %zd",
1007 args
.event_notifier_handle
.event_notifier_notif_fd
=
1008 event_notifier_notif_fd
;
1010 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1011 (unsigned long) &lum
->u
,
1015 if (args
.event_notifier_handle
.event_notifier_notif_fd
>= 0) {
1016 lttng_ust_lock_fd_tracker();
1017 close_ret
= close(args
.event_notifier_handle
.event_notifier_notif_fd
);
1018 lttng_ust_unlock_fd_tracker();
1024 case LTTNG_UST_ABI_CHANNEL
:
1029 len
= ustcomm_recv_channel_from_sessiond(sock
,
1030 &chan_data
, lum
->u
.channel
.len
,
1033 case 0: /* orderly shutdown */
1037 if (len
== lum
->u
.channel
.len
) {
1038 DBG("channel data received");
1040 } else if (len
< 0) {
1041 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1042 if (len
== -ECONNRESET
) {
1043 ERR("%s remote end closed connection", sock_info
->name
);
1050 DBG("incorrect channel data message size: %zd", len
);
1055 args
.channel
.chan_data
= chan_data
;
1056 args
.channel
.wakeup_fd
= wakeup_fd
;
1058 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1059 (unsigned long) &lum
->u
,
1063 if (args
.channel
.wakeup_fd
>= 0) {
1066 lttng_ust_lock_fd_tracker();
1067 close_ret
= close(args
.channel
.wakeup_fd
);
1068 lttng_ust_unlock_fd_tracker();
1069 args
.channel
.wakeup_fd
= -1;
1073 free(args
.channel
.chan_data
);
1076 case LTTNG_UST_ABI_STREAM
:
1080 /* Receive shm_fd, wakeup_fd */
1081 ret
= ustcomm_recv_stream_from_sessiond(sock
,
1083 &args
.stream
.shm_fd
,
1084 &args
.stream
.wakeup_fd
);
1090 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1091 (unsigned long) &lum
->u
,
1095 if (args
.stream
.shm_fd
>= 0) {
1096 lttng_ust_lock_fd_tracker();
1097 close_ret
= close(args
.stream
.shm_fd
);
1098 lttng_ust_unlock_fd_tracker();
1099 args
.stream
.shm_fd
= -1;
1103 if (args
.stream
.wakeup_fd
>= 0) {
1104 lttng_ust_lock_fd_tracker();
1105 close_ret
= close(args
.stream
.wakeup_fd
);
1106 lttng_ust_unlock_fd_tracker();
1107 args
.stream
.wakeup_fd
= -1;
1113 case LTTNG_UST_ABI_CONTEXT
:
1114 switch (lum
->u
.context
.ctx
) {
1115 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1118 size_t ctxlen
, recvlen
;
1120 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
1121 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
1122 if (ctxlen
>= LTTNG_UST_ABI_SYM_NAME_LEN
) {
1123 ERR("Application context string length size is too large: %zu bytes",
1128 strcpy(ctxstr
, "$app.");
1129 p
= &ctxstr
[strlen("$app.")];
1130 recvlen
= ctxlen
- strlen("$app.");
1131 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
1133 case 0: /* orderly shutdown */
1137 if (len
== recvlen
) {
1138 DBG("app context data received");
1140 } else if (len
< 0) {
1141 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1142 if (len
== -ECONNRESET
) {
1143 ERR("%s remote end closed connection", sock_info
->name
);
1150 DBG("incorrect app context data message size: %zd", len
);
1155 /* Put : between provider and ctxname. */
1156 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1157 args
.app_context
.ctxname
= ctxstr
;
1164 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1165 (unsigned long) &lum
->u
,
1171 case LTTNG_UST_ABI_COUNTER
:
1175 len
= ustcomm_recv_counter_from_sessiond(sock
,
1176 &counter_data
, lum
->u
.counter
.len
);
1178 case 0: /* orderly shutdown */
1182 if (len
== lum
->u
.counter
.len
) {
1183 DBG("counter data received");
1185 } else if (len
< 0) {
1186 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1187 if (len
== -ECONNRESET
) {
1188 ERR("%s remote end closed connection", sock_info
->name
);
1195 DBG("incorrect counter data message size: %zd", len
);
1200 args
.counter
.counter_data
= counter_data
;
1202 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1203 (unsigned long) &lum
->u
,
1207 free(args
.counter
.counter_data
);
1210 case LTTNG_UST_ABI_COUNTER_GLOBAL
:
1212 /* Receive shm_fd */
1213 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1214 &args
.counter_shm
.shm_fd
);
1220 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1221 (unsigned long) &lum
->u
,
1225 if (args
.counter_shm
.shm_fd
>= 0) {
1228 lttng_ust_lock_fd_tracker();
1229 close_ret
= close(args
.counter_shm
.shm_fd
);
1230 lttng_ust_unlock_fd_tracker();
1231 args
.counter_shm
.shm_fd
= -1;
1237 case LTTNG_UST_ABI_COUNTER_CPU
:
1239 /* Receive shm_fd */
1240 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1241 &args
.counter_shm
.shm_fd
);
1247 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1248 (unsigned long) &lum
->u
,
1252 if (args
.counter_shm
.shm_fd
>= 0) {
1255 lttng_ust_lock_fd_tracker();
1256 close_ret
= close(args
.counter_shm
.shm_fd
);
1257 lttng_ust_unlock_fd_tracker();
1258 args
.counter_shm
.shm_fd
= -1;
1264 case LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
:
1266 /* Receive struct lttng_ust_event_notifier */
1267 struct lttng_ust_abi_event_notifier event_notifier
;
1269 if (sizeof(event_notifier
) != lum
->u
.event_notifier
.len
) {
1270 DBG("incorrect event notifier data message size: %u", lum
->u
.event_notifier
.len
);
1274 len
= ustcomm_recv_unix_sock(sock
, &event_notifier
, sizeof(event_notifier
));
1276 case 0: /* orderly shutdown */
1280 if (len
== sizeof(event_notifier
)) {
1281 DBG("event notifier data received");
1283 } else if (len
< 0) {
1284 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1285 if (len
== -ECONNRESET
) {
1286 ERR("%s remote end closed connection", sock_info
->name
);
1293 DBG("incorrect event notifier data message size: %zd", len
);
1299 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1300 (unsigned long) &event_notifier
,
1309 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1310 (unsigned long) &lum
->u
,
1317 lur
.handle
= lum
->handle
;
1321 lur
.ret_code
= LTTNG_UST_OK
;
1324 * Use -LTTNG_UST_ERR as wildcard for UST internal
1325 * error that are not caused by the transport, except if
1326 * we already have a more precise error message to
1329 if (ret
> -LTTNG_UST_ERR
) {
1330 /* Translate code to UST error. */
1333 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1336 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1339 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1342 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1345 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1348 lur
.ret_code
= -LTTNG_UST_ERR
;
1357 case LTTNG_UST_ABI_TRACER_VERSION
:
1358 lur
.u
.version
= lum
->u
.version
;
1360 case LTTNG_UST_ABI_TRACEPOINT_LIST_GET
:
1361 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1365 DBG("Return value: %d", lur
.ret_val
);
1370 * Performed delayed statedump operations outside of the UST
1371 * lock. We need to take the dynamic loader lock before we take
1372 * the UST lock internally within handle_pending_statedump().
1374 handle_pending_statedump(sock_info
);
1377 ret
= -LTTNG_UST_ERR_EXITING
;
1381 ret
= send_reply(sock
, &lur
);
1383 DBG("error sending reply");
1388 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1391 if (lur
.ret_code
== LTTNG_UST_OK
) {
1393 case LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
:
1394 len
= ustcomm_send_unix_sock(sock
,
1395 &args
.field_list
.entry
,
1396 sizeof(args
.field_list
.entry
));
1401 if (len
!= sizeof(args
.field_list
.entry
)) {
1415 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1419 if (sock_info
->root_handle
!= -1) {
1420 ret
= lttng_ust_abi_objd_unref(sock_info
->root_handle
, 1);
1422 ERR("Error unref root handle");
1424 sock_info
->root_handle
= -1;
1426 sock_info
->registration_done
= 0;
1427 sock_info
->initial_statedump_done
= 0;
1430 * wait_shm_mmap, socket and notify socket are used by listener
1431 * threads outside of the ust lock, so we cannot tear them down
1432 * ourselves, because we cannot join on these threads. Leave
1433 * responsibility of cleaning up these resources to the OS
1439 if (sock_info
->socket
!= -1) {
1440 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1442 ERR("Error closing ust cmd socket");
1444 sock_info
->socket
= -1;
1446 if (sock_info
->notify_socket
!= -1) {
1447 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1449 ERR("Error closing ust notify socket");
1451 sock_info
->notify_socket
= -1;
1453 if (sock_info
->wait_shm_mmap
) {
1456 page_size
= LTTNG_UST_PAGE_SIZE
;
1457 if (page_size
<= 0) {
1461 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1463 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1465 ERR("Error unmapping wait shm");
1468 sock_info
->wait_shm_mmap
= NULL
;
1473 * Using fork to set umask in the child process (not multi-thread safe).
1474 * We deal with the shm_open vs ftruncate race (happening when the
1475 * sessiond owns the shm and does not let everybody modify it, to ensure
1476 * safety against shm_unlink) by simply letting the mmap fail and
1477 * retrying after a few seconds.
1478 * For global shm, everybody has rw access to it until the sessiond
1482 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1484 int wait_shm_fd
, ret
;
1488 * Try to open read-only.
1490 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1491 if (wait_shm_fd
>= 0) {
1494 size_t bytes_read
= 0;
1497 * Try to read the fd. If unable to do so, try opening
1501 len
= read(wait_shm_fd
,
1502 &((char *) &tmp_read
)[bytes_read
],
1503 sizeof(tmp_read
) - bytes_read
);
1507 } while ((len
< 0 && errno
== EINTR
)
1508 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1509 if (bytes_read
!= sizeof(tmp_read
)) {
1510 ret
= close(wait_shm_fd
);
1512 ERR("close wait_shm_fd");
1517 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1519 * Real-only open did not work, and it's not because the
1520 * entry was not present. It's a failure that prohibits
1523 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1529 * If the open failed because the file did not exist, or because
1530 * the file was not truncated yet, try creating it ourself.
1532 URCU_TLS(lttng_ust_nest_count
)++;
1534 URCU_TLS(lttng_ust_nest_count
)--;
1539 * Parent: wait for child to return, in which case the
1540 * shared memory map will have been created.
1542 pid
= wait(&status
);
1543 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1548 * Try to open read-only again after creation.
1550 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1551 if (wait_shm_fd
< 0) {
1553 * Real-only open did not work. It's a failure
1554 * that prohibits using shm.
1556 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1560 } else if (pid
== 0) {
1564 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1565 if (sock_info
->global
)
1566 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1568 * We're alone in a child process, so we can modify the
1569 * process-wide umask.
1571 umask(~create_mode
);
1573 * Try creating shm (or get rw access).
1574 * We don't do an exclusive open, because we allow other
1575 * processes to create+ftruncate it concurrently.
1577 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1578 O_RDWR
| O_CREAT
, create_mode
);
1579 if (wait_shm_fd
>= 0) {
1580 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1582 PERROR("ftruncate");
1583 _exit(EXIT_FAILURE
);
1585 _exit(EXIT_SUCCESS
);
1588 * For local shm, we need to have rw access to accept
1589 * opening it: this means the local sessiond will be
1590 * able to wake us up. For global shm, we open it even
1591 * if rw access is not granted, because the root.root
1592 * sessiond will be able to override all rights and wake
1595 if (!sock_info
->global
&& errno
!= EACCES
) {
1596 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1597 _exit(EXIT_FAILURE
);
1600 * The shm exists, but we cannot open it RW. Report
1603 _exit(EXIT_SUCCESS
);
1608 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1609 struct stat statbuf
;
1612 * Ensure that our user is the owner of the shm file for
1613 * local shm. If we do not own the file, it means our
1614 * sessiond will not have access to wake us up (there is
1615 * probably a rogue process trying to fake our
1616 * sessiond). Fallback to polling method in this case.
1618 ret
= fstat(wait_shm_fd
, &statbuf
);
1623 if (statbuf
.st_uid
!= getuid())
1629 ret
= close(wait_shm_fd
);
1631 PERROR("Error closing fd");
1637 char *get_map_shm(struct sock_info
*sock_info
)
1640 int wait_shm_fd
, ret
;
1641 char *wait_shm_mmap
;
1643 page_size
= sysconf(_SC_PAGE_SIZE
);
1644 if (page_size
<= 0) {
1648 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1652 lttng_ust_lock_fd_tracker();
1653 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1654 if (wait_shm_fd
< 0) {
1655 lttng_ust_unlock_fd_tracker();
1659 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1661 ret
= close(wait_shm_fd
);
1663 PERROR("Error closing fd");
1665 lttng_ust_unlock_fd_tracker();
1670 lttng_ust_unlock_fd_tracker();
1672 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1673 MAP_SHARED
, wait_shm_fd
, 0);
1675 /* close shm fd immediately after taking the mmap reference */
1676 lttng_ust_lock_fd_tracker();
1677 ret
= close(wait_shm_fd
);
1679 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1681 PERROR("Error closing fd");
1683 lttng_ust_unlock_fd_tracker();
1685 if (wait_shm_mmap
== MAP_FAILED
) {
1686 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1689 return wait_shm_mmap
;
1696 void wait_for_sessiond(struct sock_info
*sock_info
)
1698 /* Use ust_lock to check if we should quit. */
1702 if (wait_poll_fallback
) {
1707 assert(sock_info
->wait_shm_mmap
);
1709 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1710 /* Wait for futex wakeup */
1711 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1714 while (lttng_ust_futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1715 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1718 /* Value already changed. */
1721 /* Retry if interrupted by signal. */
1722 break; /* Get out of switch. */
1724 wait_poll_fallback
= 1;
1726 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1727 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1728 "Please upgrade your kernel "
1729 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1730 "mainline). LTTng-UST will use polling mode fallback.");
1731 if (lttng_ust_logging_debug_enabled())
1749 * This thread does not allocate any resource, except within
1750 * handle_message, within mutex protection. This mutex protects against
1752 * The other moment it allocates resources is at socket connection, which
1753 * is also protected by the mutex.
1756 void *ust_listener_thread(void *arg
)
1758 struct sock_info
*sock_info
= arg
;
1759 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1762 lttng_ust_fixup_tls();
1764 * If available, add '-ust' to the end of this thread's
1767 ret
= lttng_ust_setustprocname();
1769 ERR("Unable to set UST process name");
1772 /* Restart trying to connect to the session daemon */
1774 if (prev_connect_failed
) {
1775 /* Wait for sessiond availability with pipe */
1776 wait_for_sessiond(sock_info
);
1780 * Sleep for 5 seconds before retrying after a
1781 * sequence of failure / wait / failure. This
1782 * deals with a killed or broken session daemon.
1788 prev_connect_failed
= 0;
1795 if (sock_info
->socket
!= -1) {
1796 /* FD tracker is updated by ustcomm_close_unix_sock() */
1797 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1799 ERR("Error closing %s ust cmd socket",
1802 sock_info
->socket
= -1;
1804 if (sock_info
->notify_socket
!= -1) {
1805 /* FD tracker is updated by ustcomm_close_unix_sock() */
1806 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1808 ERR("Error closing %s ust notify socket",
1811 sock_info
->notify_socket
= -1;
1816 * Register. We need to perform both connect and sending
1817 * registration message before doing the next connect otherwise
1818 * we may reach unix socket connect queue max limits and block
1819 * on the 2nd connect while the session daemon is awaiting the
1820 * first connect registration message.
1822 /* Connect cmd socket */
1823 lttng_ust_lock_fd_tracker();
1824 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1825 get_connect_sock_timeout());
1827 lttng_ust_unlock_fd_tracker();
1828 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1829 prev_connect_failed
= 1;
1832 * If we cannot find the sessiond daemon, don't delay
1833 * constructor execution.
1835 ret
= handle_register_failed(sock_info
);
1841 ret
= lttng_ust_add_fd_to_tracker(fd
);
1845 PERROR("close on sock_info->socket");
1848 lttng_ust_unlock_fd_tracker();
1853 sock_info
->socket
= ret
;
1854 lttng_ust_unlock_fd_tracker();
1858 * Unlock/relock ust lock because connect is blocking (with
1859 * timeout). Don't delay constructors on the ust lock for too
1867 * Create only one root handle per listener thread for the whole
1868 * process lifetime, so we ensure we get ID which is statically
1869 * assigned to the root handle.
1871 if (sock_info
->root_handle
== -1) {
1872 ret
= lttng_abi_create_root_handle();
1874 ERR("Error creating root handle");
1877 sock_info
->root_handle
= ret
;
1880 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1882 ERR("Error registering to %s ust cmd socket",
1884 prev_connect_failed
= 1;
1886 * If we cannot register to the sessiond daemon, don't
1887 * delay constructor execution.
1889 ret
= handle_register_failed(sock_info
);
1897 * Unlock/relock ust lock because connect is blocking (with
1898 * timeout). Don't delay constructors on the ust lock for too
1905 /* Connect notify socket */
1906 lttng_ust_lock_fd_tracker();
1907 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1908 get_connect_sock_timeout());
1910 lttng_ust_unlock_fd_tracker();
1911 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1912 prev_connect_failed
= 1;
1915 * If we cannot find the sessiond daemon, don't delay
1916 * constructor execution.
1918 ret
= handle_register_failed(sock_info
);
1925 ret
= lttng_ust_add_fd_to_tracker(fd
);
1929 PERROR("close on sock_info->notify_socket");
1932 lttng_ust_unlock_fd_tracker();
1937 sock_info
->notify_socket
= ret
;
1938 lttng_ust_unlock_fd_tracker();
1942 * Unlock/relock ust lock because connect is blocking (with
1943 * timeout). Don't delay constructors on the ust lock for too
1950 timeout
= get_notify_sock_timeout();
1953 * Give at least 10ms to sessiond to reply to
1958 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1961 WARN("Error setting socket receive timeout");
1963 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1966 WARN("Error setting socket send timeout");
1968 } else if (timeout
< -1) {
1969 WARN("Unsupported timeout value %ld", timeout
);
1972 ret
= register_to_sessiond(sock_info
->notify_socket
,
1973 USTCTL_SOCKET_NOTIFY
);
1975 ERR("Error registering to %s ust notify socket",
1977 prev_connect_failed
= 1;
1979 * If we cannot register to the sessiond daemon, don't
1980 * delay constructor execution.
1982 ret
= handle_register_failed(sock_info
);
1987 sock
= sock_info
->socket
;
1993 struct ustcomm_ust_msg lum
;
1995 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1997 case 0: /* orderly shutdown */
1998 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
2003 * Either sessiond has shutdown or refused us by closing the socket.
2004 * In either case, we don't want to delay construction execution,
2005 * and we need to wait before retry.
2007 prev_connect_failed
= 1;
2009 * If we cannot register to the sessiond daemon, don't
2010 * delay constructor execution.
2012 ret
= handle_register_failed(sock_info
);
2017 print_cmd(lum
.cmd
, lum
.handle
);
2018 ret
= handle_message(sock_info
, sock
, &lum
);
2020 ERR("Error handling message for %s socket",
2023 * Close socket if protocol error is
2031 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
2033 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
2035 if (len
== -ECONNRESET
) {
2036 DBG("%s remote end closed connection", sock_info
->name
);
2047 /* Cleanup socket handles before trying to reconnect */
2048 lttng_ust_abi_objd_table_owner_cleanup(sock_info
);
2050 goto restart
; /* try to reconnect */
2055 pthread_mutex_lock(&ust_exit_mutex
);
2056 sock_info
->thread_active
= 0;
2057 pthread_mutex_unlock(&ust_exit_mutex
);
2062 * Weak symbol to call when the ust malloc wrapper is not loaded.
2064 __attribute__((weak
))
2065 void lttng_ust_libc_wrapper_malloc_ctor(void)
2070 * sessiond monitoring thread: monitor presence of global and per-user
2071 * sessiond by polling the application common named pipe.
2074 void lttng_ust_ctor(void)
2075 __attribute__((constructor
));
2077 void lttng_ust_ctor(void)
2079 struct timespec constructor_timeout
;
2080 sigset_t sig_all_blocked
, orig_parent_mask
;
2081 pthread_attr_t thread_attr
;
2086 if (uatomic_xchg(&initialized
, 1) == 1)
2090 * Fixup interdependency between TLS fixup mutex (which happens
2091 * to be the dynamic linker mutex) and ust_lock, taken within
2094 lttng_ust_fixup_tls();
2096 lttng_ust_loaded
= 1;
2099 * We need to ensure that the liblttng-ust library is not unloaded to avoid
2100 * the unloading of code used by the ust_listener_threads as we can not
2101 * reliably know when they exited. To do that, manually load
2102 * liblttng-ust.so to increment the dynamic loader's internal refcount for
2103 * this library so it never becomes zero, thus never gets unloaded from the
2104 * address space of the process. Since we are already running in the
2105 * constructor of the LTTNG_UST_LIB_SONAME library, calling dlopen will
2106 * simply increment the refcount and no additionnal work is needed by the
2107 * dynamic loader as the shared library is already loaded in the address
2108 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
2109 * unloading of the UST library if its refcount becomes zero (which should
2110 * never happen). Do the return value check but discard the handle at the
2111 * end of the function as it's not needed.
2113 handle
= dlopen(LTTNG_UST_LIB_SONAME
, RTLD_LAZY
| RTLD_NODELETE
);
2115 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SONAME
);
2119 * We want precise control over the order in which we construct
2120 * our sub-libraries vs starting to receive commands from
2121 * sessiond (otherwise leading to errors when trying to create
2122 * sessiond before the init functions are completed).
2126 * Both the logging and getenv lazy-initialization uses getenv()
2127 * internally and thus needs to be explicitly initialized in
2128 * liblttng-ust before we start any threads as an unsuspecting normally
2129 * single threaded application using liblttng-ust could be using
2130 * setenv() which is not thread-safe.
2132 lttng_ust_logging_init();
2133 lttng_ust_getenv_init();
2135 /* Call the liblttng-ust-common constructor. */
2136 lttng_ust_common_ctor();
2138 lttng_ust_tp_init();
2139 lttng_ust_clock_init();
2140 lttng_ust_getcpu_plugin_init();
2141 lttng_ust_statedump_init();
2142 lttng_ust_ring_buffer_clients_init();
2143 lttng_ust_counter_clients_init();
2144 lttng_perf_counter_init();
2146 * Invoke ust malloc wrapper init before starting other threads.
2148 lttng_ust_libc_wrapper_malloc_ctor();
2150 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
2152 get_allow_blocking();
2154 ret
= sem_init(&constructor_wait
, 0, 0);
2159 ret
= setup_global_apps();
2161 assert(global_apps
.allowed
== 0);
2162 DBG("global apps setup returned %d", ret
);
2165 ret
= setup_local_apps();
2167 assert(local_apps
.allowed
== 0);
2168 DBG("local apps setup returned %d", ret
);
2171 /* A new thread created by pthread_create inherits the signal mask
2172 * from the parent. To avoid any signal being received by the
2173 * listener thread, we block all signals temporarily in the parent,
2174 * while we create the listener thread.
2176 sigfillset(&sig_all_blocked
);
2177 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
2179 ERR("pthread_sigmask: %s", strerror(ret
));
2182 ret
= pthread_attr_init(&thread_attr
);
2184 ERR("pthread_attr_init: %s", strerror(ret
));
2186 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
2188 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
2191 if (global_apps
.allowed
) {
2192 pthread_mutex_lock(&ust_exit_mutex
);
2193 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
2194 ust_listener_thread
, &global_apps
);
2196 ERR("pthread_create global: %s", strerror(ret
));
2198 global_apps
.thread_active
= 1;
2199 pthread_mutex_unlock(&ust_exit_mutex
);
2201 handle_register_done(&global_apps
);
2204 if (local_apps
.allowed
) {
2205 pthread_mutex_lock(&ust_exit_mutex
);
2206 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
2207 ust_listener_thread
, &local_apps
);
2209 ERR("pthread_create local: %s", strerror(ret
));
2211 local_apps
.thread_active
= 1;
2212 pthread_mutex_unlock(&ust_exit_mutex
);
2214 handle_register_done(&local_apps
);
2216 ret
= pthread_attr_destroy(&thread_attr
);
2218 ERR("pthread_attr_destroy: %s", strerror(ret
));
2221 /* Restore original signal mask in parent */
2222 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
2224 ERR("pthread_sigmask: %s", strerror(ret
));
2227 switch (timeout_mode
) {
2228 case 1: /* timeout wait */
2230 ret
= sem_timedwait(&constructor_wait
,
2231 &constructor_timeout
);
2232 } while (ret
< 0 && errno
== EINTR
);
2236 ERR("Timed out waiting for lttng-sessiond");
2239 PERROR("sem_timedwait");
2242 ERR("Unexpected error \"%s\" returned by sem_timedwait",
2247 case -1:/* wait forever */
2249 ret
= sem_wait(&constructor_wait
);
2250 } while (ret
< 0 && errno
== EINTR
);
2257 ERR("Unexpected error \"%s\" returned by sem_wait",
2262 case 0: /* no timeout */
2268 void lttng_ust_cleanup(int exiting
)
2270 cleanup_sock_info(&global_apps
, exiting
);
2271 cleanup_sock_info(&local_apps
, exiting
);
2272 local_apps
.allowed
= 0;
2273 global_apps
.allowed
= 0;
2275 * The teardown in this function all affect data structures
2276 * accessed under the UST lock by the listener thread. This
2277 * lock, along with the lttng_ust_comm_should_quit flag, ensure
2278 * that none of these threads are accessing this data at this
2281 lttng_ust_abi_exit();
2282 lttng_ust_abi_events_exit();
2283 lttng_perf_counter_exit();
2284 lttng_ust_ring_buffer_clients_exit();
2285 lttng_ust_counter_clients_exit();
2286 lttng_ust_statedump_destroy();
2287 lttng_ust_tp_exit();
2289 /* Reinitialize values for fork */
2290 sem_count
= sem_count_initial_value
;
2291 lttng_ust_comm_should_quit
= 0;
2297 void lttng_ust_exit(void)
2298 __attribute__((destructor
));
2300 void lttng_ust_exit(void)
2305 * Using pthread_cancel here because:
2306 * A) we don't want to hang application teardown.
2307 * B) the thread is not allocating any resource.
2311 * Require the communication thread to quit. Synchronize with
2312 * mutexes to ensure it is not in a mutex critical section when
2313 * pthread_cancel is later called.
2316 lttng_ust_comm_should_quit
= 1;
2319 pthread_mutex_lock(&ust_exit_mutex
);
2320 /* cancel threads */
2321 if (global_apps
.thread_active
) {
2322 ret
= pthread_cancel(global_apps
.ust_listener
);
2324 ERR("Error cancelling global ust listener thread: %s",
2327 global_apps
.thread_active
= 0;
2330 if (local_apps
.thread_active
) {
2331 ret
= pthread_cancel(local_apps
.ust_listener
);
2333 ERR("Error cancelling local ust listener thread: %s",
2336 local_apps
.thread_active
= 0;
2339 pthread_mutex_unlock(&ust_exit_mutex
);
2342 * Do NOT join threads: use of sys_futex makes it impossible to
2343 * join the threads without using async-cancel, but async-cancel
2344 * is delivered by a signal, which could hit the target thread
2345 * anywhere in its code path, including while the ust_lock() is
2346 * held, causing a deadlock for the other thread. Let the OS
2347 * cleanup the threads if there are stalled in a syscall.
2349 lttng_ust_cleanup(1);
2353 void ust_context_ns_reset(void)
2355 lttng_context_pid_ns_reset();
2356 lttng_context_cgroup_ns_reset();
2357 lttng_context_ipc_ns_reset();
2358 lttng_context_mnt_ns_reset();
2359 lttng_context_net_ns_reset();
2360 lttng_context_user_ns_reset();
2361 lttng_context_time_ns_reset();
2362 lttng_context_uts_ns_reset();
2366 void ust_context_vuids_reset(void)
2368 lttng_context_vuid_reset();
2369 lttng_context_veuid_reset();
2370 lttng_context_vsuid_reset();
2374 void ust_context_vgids_reset(void)
2376 lttng_context_vgid_reset();
2377 lttng_context_vegid_reset();
2378 lttng_context_vsgid_reset();
2382 * We exclude the worker threads across fork and clone (except
2383 * CLONE_VM), because these system calls only keep the forking thread
2384 * running in the child. Therefore, we don't want to call fork or clone
2385 * in the middle of an tracepoint or ust tracing state modification.
2386 * Holding this mutex protects these structures across fork and clone.
2388 void lttng_ust_before_fork(sigset_t
*save_sigset
)
2391 * Disable signals. This is to avoid that the child intervenes
2392 * before it is properly setup for tracing. It is safer to
2393 * disable all signals, because then we know we are not breaking
2394 * anything by restoring the original mask.
2399 /* Fixup lttng-ust TLS. */
2400 lttng_ust_fixup_tls();
2402 if (URCU_TLS(lttng_ust_nest_count
))
2404 /* Disable signals */
2405 sigfillset(&all_sigs
);
2406 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2408 PERROR("sigprocmask");
2411 pthread_mutex_lock(&ust_fork_mutex
);
2414 lttng_ust_urcu_before_fork();
2415 lttng_ust_lock_fd_tracker();
2419 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2423 DBG("process %d", getpid());
2424 lttng_perf_unlock();
2425 lttng_ust_unlock_fd_tracker();
2428 pthread_mutex_unlock(&ust_fork_mutex
);
2430 /* Restore signals */
2431 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2433 PERROR("sigprocmask");
2437 void lttng_ust_after_fork_parent(sigset_t
*restore_sigset
)
2439 if (URCU_TLS(lttng_ust_nest_count
))
2441 DBG("process %d", getpid());
2442 lttng_ust_urcu_after_fork_parent();
2443 /* Release mutexes and reenable signals */
2444 ust_after_fork_common(restore_sigset
);
2448 * After fork, in the child, we need to cleanup all the leftover state,
2449 * except the worker thread which already magically disappeared thanks
2450 * to the weird Linux fork semantics. After tyding up, we call
2451 * lttng_ust_ctor() again to start over as a new PID.
2453 * This is meant for forks() that have tracing in the child between the
2454 * fork and following exec call (if there is any).
2456 void lttng_ust_after_fork_child(sigset_t
*restore_sigset
)
2458 if (URCU_TLS(lttng_ust_nest_count
))
2460 lttng_context_vpid_reset();
2461 lttng_context_vtid_reset();
2462 lttng_ust_context_procname_reset();
2463 ust_context_ns_reset();
2464 ust_context_vuids_reset();
2465 ust_context_vgids_reset();
2466 DBG("process %d", getpid());
2467 /* Release urcu mutexes */
2468 lttng_ust_urcu_after_fork_child();
2469 lttng_ust_cleanup(0);
2470 /* Release mutexes and reenable signals */
2471 ust_after_fork_common(restore_sigset
);
2475 void lttng_ust_after_setns(void)
2477 ust_context_ns_reset();
2478 ust_context_vuids_reset();
2479 ust_context_vgids_reset();
2482 void lttng_ust_after_unshare(void)
2484 ust_context_ns_reset();
2485 ust_context_vuids_reset();
2486 ust_context_vgids_reset();
2489 void lttng_ust_after_setuid(void)
2491 ust_context_vuids_reset();
2494 void lttng_ust_after_seteuid(void)
2496 ust_context_vuids_reset();
2499 void lttng_ust_after_setreuid(void)
2501 ust_context_vuids_reset();
2504 void lttng_ust_after_setresuid(void)
2506 ust_context_vuids_reset();
2509 void lttng_ust_after_setgid(void)
2511 ust_context_vgids_reset();
2514 void lttng_ust_after_setegid(void)
2516 ust_context_vgids_reset();
2519 void lttng_ust_after_setregid(void)
2521 ust_context_vgids_reset();
2524 void lttng_ust_after_setresgid(void)
2526 ust_context_vgids_reset();
2529 void lttng_ust_sockinfo_session_enabled(void *owner
)
2531 struct sock_info
*sock_info
= owner
;
2532 sock_info
->statedump_pending
= 1;