4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; only
10 * version 2.1 of the License.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <sys/types.h>
26 #include <sys/socket.h>
29 #include <sys/types.h>
36 #include <semaphore.h>
41 #include <urcu/uatomic.h>
43 #include <urcu/compiler.h>
44 #include <lttng/urcu/urcu-ust.h>
46 #include <lttng/align.h>
47 #include <lttng/ust-events.h>
48 #include <lttng/ust-abi.h>
49 #include <lttng/ust.h>
50 #include <lttng/ust-error.h>
51 #include <lttng/ust-ctl.h>
52 #include <urcu/tls-compat.h>
55 #include <usterr-signal-safe.h>
57 #include "tracepoint-internal.h"
58 #include "lttng-tracer-core.h"
60 #include "../libringbuffer/rb-init.h"
61 #include "lttng-ust-statedump.h"
63 #include "../libringbuffer/getcpu.h"
65 #include "ust-events-internal.h"
67 /* Concatenate lttng ust shared library name with its major version number. */
68 #define LTTNG_UST_LIB_SO_NAME "liblttng-ust.so." __ust_stringify(CONFIG_LTTNG_UST_LIBRARY_VERSION_MAJOR)
71 * Has lttng ust comm constructor been called ?
73 static int initialized
;
76 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
77 * Held when handling a command, also held by fork() to deal with
78 * removal of threads, and by exit path.
80 * The UST lock is the centralized mutex across UST tracing control and
83 * ust_exit_mutex must never nest in ust_mutex.
85 * ust_fork_mutex must never nest in ust_mutex.
87 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
88 * counter lazy initialization called by events within the statedump,
89 * which traces while the ust_mutex is held.
91 * ust_lock nests within the dynamic loader lock (within glibc) because
92 * it is taken within the library constructor.
94 * The ust fd tracker lock nests within the ust_mutex.
96 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
98 /* Allow nesting the ust_mutex within the same thread. */
99 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
102 * ust_exit_mutex protects thread_active variable wrt thread exit. It
103 * cannot be done by ust_mutex because pthread_cancel(), which takes an
104 * internal libc lock, cannot nest within ust_mutex.
106 * It never nests within a ust_mutex.
108 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
111 * ust_fork_mutex protects base address statedump tracing against forks. It
112 * prevents the dynamic loader lock to be taken (by base address statedump
113 * tracing) while a fork is happening, thus preventing deadlock issues with
114 * the dynamic loader lock.
116 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
118 /* Should the ust comm thread quit ? */
119 static int lttng_ust_comm_should_quit
;
122 * This variable can be tested by applications to check whether
123 * lttng-ust is loaded. They simply have to define their own
124 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
125 * library constructor.
127 int lttng_ust_loaded
__attribute__((weak
));
130 * Return 0 on success, -1 if should quit.
131 * The lock is taken in both cases.
136 sigset_t sig_all_blocked
, orig_mask
;
139 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
141 ERR("pthread_setcancelstate: %s", strerror(ret
));
143 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
144 ERR("pthread_setcancelstate: unexpected oldstate");
146 sigfillset(&sig_all_blocked
);
147 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
149 ERR("pthread_sigmask: %s", strerror(ret
));
151 if (!URCU_TLS(ust_mutex_nest
)++)
152 pthread_mutex_lock(&ust_mutex
);
153 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
155 ERR("pthread_sigmask: %s", strerror(ret
));
157 if (lttng_ust_comm_should_quit
) {
165 * ust_lock_nocheck() can be used in constructors/destructors, because
166 * they are already nested within the dynamic loader lock, and therefore
167 * have exclusive access against execution of liblttng-ust destructor.
170 void ust_lock_nocheck(void)
172 sigset_t sig_all_blocked
, orig_mask
;
175 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
177 ERR("pthread_setcancelstate: %s", strerror(ret
));
179 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
180 ERR("pthread_setcancelstate: unexpected oldstate");
182 sigfillset(&sig_all_blocked
);
183 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
185 ERR("pthread_sigmask: %s", strerror(ret
));
187 if (!URCU_TLS(ust_mutex_nest
)++)
188 pthread_mutex_lock(&ust_mutex
);
189 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
191 ERR("pthread_sigmask: %s", strerror(ret
));
198 void ust_unlock(void)
200 sigset_t sig_all_blocked
, orig_mask
;
203 sigfillset(&sig_all_blocked
);
204 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
206 ERR("pthread_sigmask: %s", strerror(ret
));
208 if (!--URCU_TLS(ust_mutex_nest
))
209 pthread_mutex_unlock(&ust_mutex
);
210 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
212 ERR("pthread_sigmask: %s", strerror(ret
));
214 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
216 ERR("pthread_setcancelstate: %s", strerror(ret
));
218 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
219 ERR("pthread_setcancelstate: unexpected oldstate");
224 * Wait for either of these before continuing to the main
226 * - the register_done message from sessiond daemon
227 * (will let the sessiond daemon enable sessions before main
229 * - sessiond daemon is not reachable.
230 * - timeout (ensuring applications are resilient to session
233 static sem_t constructor_wait
;
235 * Doing this for both the global and local sessiond.
238 sem_count_initial_value
= 4,
241 static int sem_count
= sem_count_initial_value
;
244 * Counting nesting within lttng-ust. Used to ensure that calling fork()
245 * from liblttng-ust does not execute the pre/post fork handlers.
247 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
250 * Info about socket and associated listener thread.
254 pthread_t ust_listener
; /* listener thread */
256 int registration_done
;
261 char sock_path
[PATH_MAX
];
265 char wait_shm_path
[PATH_MAX
];
267 /* Keep track of lazy state dump not performed yet. */
268 int statedump_pending
;
269 int initial_statedump_done
;
270 /* Keep procname for statedump */
271 char procname
[LTTNG_UST_ABI_PROCNAME_LEN
];
274 /* Socket from app (connect) to session daemon (listen) for communication */
275 struct sock_info global_apps
= {
280 .registration_done
= 0,
284 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
288 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
290 .statedump_pending
= 0,
291 .initial_statedump_done
= 0,
295 /* TODO: allow global_apps_sock_path override */
297 struct sock_info local_apps
= {
301 .registration_done
= 0,
302 .allowed
= 0, /* Check setuid bit first */
308 .statedump_pending
= 0,
309 .initial_statedump_done
= 0,
313 static int wait_poll_fallback
;
315 static const char *cmd_name_mapping
[] = {
316 [ LTTNG_UST_RELEASE
] = "Release",
317 [ LTTNG_UST_SESSION
] = "Create Session",
318 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
320 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
321 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
322 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
323 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
325 [ LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
] = "Create event notifier group",
327 /* Session FD commands */
328 [ LTTNG_UST_CHANNEL
] = "Create Channel",
329 [ LTTNG_UST_SESSION_START
] = "Start Session",
330 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
332 /* Channel FD commands */
333 [ LTTNG_UST_STREAM
] = "Create Stream",
334 [ LTTNG_UST_EVENT
] = "Create Event",
336 /* Event and Channel FD commands */
337 [ LTTNG_UST_CONTEXT
] = "Create Context",
338 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
340 /* Event, Channel and Session commands */
341 [ LTTNG_UST_ENABLE
] = "Enable",
342 [ LTTNG_UST_DISABLE
] = "Disable",
344 /* Tracepoint list commands */
345 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
346 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
348 /* Event FD commands */
349 [ LTTNG_UST_FILTER
] = "Create Filter",
350 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
352 /* Event notifier group commands */
353 [ LTTNG_UST_EVENT_NOTIFIER_CREATE
] = "Create event notifier",
355 /* Session and event notifier group commands */
356 [ LTTNG_UST_COUNTER
] = "Create Counter",
358 /* Counter commands */
359 [ LTTNG_UST_COUNTER_GLOBAL
] = "Create Counter Global",
360 [ LTTNG_UST_COUNTER_CPU
] = "Create Counter CPU",
363 static const char *str_timeout
;
364 static int got_timeout_env
;
366 extern void lttng_ring_buffer_client_overwrite_init(void);
367 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
368 extern void lttng_ring_buffer_client_discard_init(void);
369 extern void lttng_ring_buffer_client_discard_rt_init(void);
370 extern void lttng_ring_buffer_metadata_client_init(void);
371 extern void lttng_ring_buffer_client_overwrite_exit(void);
372 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
373 extern void lttng_ring_buffer_client_discard_exit(void);
374 extern void lttng_ring_buffer_client_discard_rt_exit(void);
375 extern void lttng_ring_buffer_metadata_client_exit(void);
376 extern void lttng_counter_client_percpu_32_modular_init(void);
377 extern void lttng_counter_client_percpu_32_modular_exit(void);
378 extern void lttng_counter_client_percpu_64_modular_init(void);
379 extern void lttng_counter_client_percpu_64_modular_exit(void);
381 static char *get_map_shm(struct sock_info
*sock_info
);
383 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
386 size_t copied
= 0, to_copy
= len
;
389 ret
= read(fd
, buf
+ copied
, to_copy
);
394 } while ((ret
> 0 && to_copy
> 0)
395 || (ret
< 0 && errno
== EINTR
));
402 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
406 const char *get_lttng_home_dir(void)
410 val
= (const char *) lttng_getenv("LTTNG_HOME");
414 return (const char *) lttng_getenv("HOME");
418 * Force a read (imply TLS fixup for dlopen) of TLS variables.
421 void lttng_fixup_nest_count_tls(void)
423 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
427 void lttng_fixup_ust_mutex_nest_tls(void)
429 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
436 void lttng_fixup_urcu_bp_tls(void)
438 (void) lttng_ust_urcu_read_ongoing();
441 void lttng_ust_fixup_tls(void)
443 lttng_fixup_urcu_bp_tls();
444 lttng_fixup_ringbuffer_tls();
445 lttng_fixup_vtid_tls();
446 lttng_fixup_nest_count_tls();
447 lttng_fixup_procname_tls();
448 lttng_fixup_ust_mutex_nest_tls();
449 lttng_ust_fixup_perf_counter_tls();
450 lttng_ust_fixup_fd_tracker_tls();
451 lttng_fixup_cgroup_ns_tls();
452 lttng_fixup_ipc_ns_tls();
453 lttng_fixup_net_ns_tls();
454 lttng_fixup_time_ns_tls();
455 lttng_fixup_uts_ns_tls();
458 int lttng_get_notify_socket(void *owner
)
460 struct sock_info
*info
= owner
;
462 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_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_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_alignof(uint8_t) * CHAR_BIT
,
647 lttng_alignof(uint16_t) * CHAR_BIT
,
648 lttng_alignof(uint32_t) * CHAR_BIT
,
649 lttng_alignof(uint64_t) * CHAR_BIT
,
650 lttng_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_CAPTURE
:
764 case LTTNG_UST_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
;
776 enum lttng_ust_bytecode_node_type type
;
777 const struct lttng_ust_objd_ops
*ops
;
778 uint32_t data_size
, data_size_max
, reloc_offset
;
784 case LTTNG_UST_FILTER
:
785 type
= LTTNG_UST_BYTECODE_NODE_TYPE_FILTER
;
786 data_size
= lum
->u
.filter
.data_size
;
787 data_size_max
= FILTER_BYTECODE_MAX_LEN
;
788 reloc_offset
= lum
->u
.filter
.reloc_offset
;
789 seqnum
= lum
->u
.filter
.seqnum
;
791 case LTTNG_UST_CAPTURE
:
792 type
= LTTNG_UST_BYTECODE_NODE_TYPE_CAPTURE
;
793 data_size
= lum
->u
.capture
.data_size
;
794 data_size_max
= 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 */
832 goto error_free_bytecode
;
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",
845 goto error_free_bytecode
;
848 goto error_free_bytecode
;
850 DBG("Incorrect %s bytecode data message size: %zd",
851 bytecode_type_str(lum
->cmd
), len
);
853 goto error_free_bytecode
;
857 ops
= objd_ops(lum
->handle
);
860 goto error_free_bytecode
;
864 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
865 (unsigned long) bytecode
,
868 goto error_free_bytecode
;
869 /* don't free bytecode if everything went fine. */
872 goto error_free_bytecode
;
884 int handle_message(struct sock_info
*sock_info
,
885 int sock
, struct ustcomm_ust_msg
*lum
)
888 const struct lttng_ust_objd_ops
*ops
;
889 struct ustcomm_ust_reply lur
;
891 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
894 memset(&lur
, 0, sizeof(lur
));
897 ret
= -LTTNG_UST_ERR_EXITING
;
901 ops
= objd_ops(lum
->handle
);
908 case LTTNG_UST_REGISTER_DONE
:
909 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
910 ret
= handle_register_done(sock_info
);
914 case LTTNG_UST_RELEASE
:
915 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
918 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
920 case LTTNG_UST_CAPTURE
:
921 case LTTNG_UST_FILTER
:
922 ret
= handle_bytecode_recv(sock_info
, sock
, lum
);
926 case LTTNG_UST_EXCLUSION
:
928 /* Receive exclusion names */
929 struct lttng_ust_excluder_node
*node
;
932 count
= lum
->u
.exclusion
.count
;
934 /* There are no names to read */
938 node
= zmalloc(sizeof(*node
) +
939 count
* LTTNG_UST_SYM_NAME_LEN
);
944 node
->excluder
.count
= count
;
945 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
946 count
* LTTNG_UST_SYM_NAME_LEN
);
948 case 0: /* orderly shutdown */
953 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
954 DBG("Exclusion data received");
956 } else if (len
< 0) {
957 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
958 if (len
== -ECONNRESET
) {
959 ERR("%s remote end closed connection", sock_info
->name
);
968 DBG("Incorrect exclusion data message size: %zd", len
);
975 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
976 (unsigned long) node
,
981 /* Don't free exclusion data if everything went fine. */
988 case LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
:
990 int event_notifier_notif_fd
;
992 len
= ustcomm_recv_event_notifier_notif_fd_from_sessiond(sock
,
993 &event_notifier_notif_fd
);
995 case 0: /* orderly shutdown */
1002 DBG("Receive failed from lttng-sessiond with errno %d",
1004 if (len
== -ECONNRESET
) {
1005 ERR("%s remote end closed connection",
1013 DBG("Incorrect event notifier fd message size: %zd",
1019 args
.event_notifier_handle
.event_notifier_notif_fd
=
1020 event_notifier_notif_fd
;
1022 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1023 (unsigned long) &lum
->u
,
1029 case LTTNG_UST_CHANNEL
:
1034 len
= ustcomm_recv_channel_from_sessiond(sock
,
1035 &chan_data
, lum
->u
.channel
.len
,
1038 case 0: /* orderly shutdown */
1042 if (len
== lum
->u
.channel
.len
) {
1043 DBG("channel data received");
1045 } else if (len
< 0) {
1046 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1047 if (len
== -ECONNRESET
) {
1048 ERR("%s remote end closed connection", sock_info
->name
);
1055 DBG("incorrect channel data message size: %zd", len
);
1060 args
.channel
.chan_data
= chan_data
;
1061 args
.channel
.wakeup_fd
= wakeup_fd
;
1063 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1064 (unsigned long) &lum
->u
,
1070 case LTTNG_UST_STREAM
:
1072 /* Receive shm_fd, wakeup_fd */
1073 ret
= ustcomm_recv_stream_from_sessiond(sock
,
1075 &args
.stream
.shm_fd
,
1076 &args
.stream
.wakeup_fd
);
1082 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1083 (unsigned long) &lum
->u
,
1089 case LTTNG_UST_CONTEXT
:
1090 switch (lum
->u
.context
.ctx
) {
1091 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
1094 size_t ctxlen
, recvlen
;
1096 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
1097 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
1098 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
1099 ERR("Application context string length size is too large: %zu bytes",
1104 strcpy(ctxstr
, "$app.");
1105 p
= &ctxstr
[strlen("$app.")];
1106 recvlen
= ctxlen
- strlen("$app.");
1107 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
1109 case 0: /* orderly shutdown */
1113 if (len
== recvlen
) {
1114 DBG("app context data received");
1116 } else if (len
< 0) {
1117 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1118 if (len
== -ECONNRESET
) {
1119 ERR("%s remote end closed connection", sock_info
->name
);
1126 DBG("incorrect app context data message size: %zd", len
);
1131 /* Put : between provider and ctxname. */
1132 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1133 args
.app_context
.ctxname
= ctxstr
;
1140 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1141 (unsigned long) &lum
->u
,
1147 case LTTNG_UST_COUNTER
:
1151 len
= ustcomm_recv_counter_from_sessiond(sock
,
1152 &counter_data
, lum
->u
.counter
.len
);
1154 case 0: /* orderly shutdown */
1158 if (len
== lum
->u
.counter
.len
) {
1159 DBG("counter data received");
1161 } else if (len
< 0) {
1162 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1163 if (len
== -ECONNRESET
) {
1164 ERR("%s remote end closed connection", sock_info
->name
);
1171 DBG("incorrect counter data message size: %zd", len
);
1176 args
.counter
.counter_data
= counter_data
;
1178 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1179 (unsigned long) &lum
->u
,
1185 case LTTNG_UST_COUNTER_GLOBAL
:
1187 /* Receive shm_fd */
1188 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1189 &args
.counter_shm
.shm_fd
);
1195 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1196 (unsigned long) &lum
->u
,
1202 case LTTNG_UST_COUNTER_CPU
:
1204 /* Receive shm_fd */
1205 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1206 &args
.counter_shm
.shm_fd
);
1212 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1213 (unsigned long) &lum
->u
,
1222 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1223 (unsigned long) &lum
->u
,
1230 lur
.handle
= lum
->handle
;
1234 lur
.ret_code
= LTTNG_UST_OK
;
1237 * Use -LTTNG_UST_ERR as wildcard for UST internal
1238 * error that are not caused by the transport, except if
1239 * we already have a more precise error message to
1242 if (ret
> -LTTNG_UST_ERR
) {
1243 /* Translate code to UST error. */
1246 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1249 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1252 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1255 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1258 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1261 lur
.ret_code
= -LTTNG_UST_ERR
;
1270 case LTTNG_UST_TRACER_VERSION
:
1271 lur
.u
.version
= lum
->u
.version
;
1273 case LTTNG_UST_TRACEPOINT_LIST_GET
:
1274 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1278 DBG("Return value: %d", lur
.ret_val
);
1283 * Performed delayed statedump operations outside of the UST
1284 * lock. We need to take the dynamic loader lock before we take
1285 * the UST lock internally within handle_pending_statedump().
1287 handle_pending_statedump(sock_info
);
1290 ret
= -LTTNG_UST_ERR_EXITING
;
1294 ret
= send_reply(sock
, &lur
);
1296 DBG("error sending reply");
1301 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1304 if (lur
.ret_code
== LTTNG_UST_OK
) {
1306 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1307 len
= ustcomm_send_unix_sock(sock
,
1308 &args
.field_list
.entry
,
1309 sizeof(args
.field_list
.entry
));
1314 if (len
!= sizeof(args
.field_list
.entry
)) {
1328 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1332 if (sock_info
->root_handle
!= -1) {
1333 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1335 ERR("Error unref root handle");
1337 sock_info
->root_handle
= -1;
1339 sock_info
->registration_done
= 0;
1340 sock_info
->initial_statedump_done
= 0;
1343 * wait_shm_mmap, socket and notify socket are used by listener
1344 * threads outside of the ust lock, so we cannot tear them down
1345 * ourselves, because we cannot join on these threads. Leave
1346 * responsibility of cleaning up these resources to the OS
1352 if (sock_info
->socket
!= -1) {
1353 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1355 ERR("Error closing ust cmd socket");
1357 sock_info
->socket
= -1;
1359 if (sock_info
->notify_socket
!= -1) {
1360 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1362 ERR("Error closing ust notify socket");
1364 sock_info
->notify_socket
= -1;
1366 if (sock_info
->wait_shm_mmap
) {
1369 page_size
= LTTNG_UST_PAGE_SIZE
;
1370 if (page_size
<= 0) {
1374 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1376 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1378 ERR("Error unmapping wait shm");
1381 sock_info
->wait_shm_mmap
= NULL
;
1386 * Using fork to set umask in the child process (not multi-thread safe).
1387 * We deal with the shm_open vs ftruncate race (happening when the
1388 * sessiond owns the shm and does not let everybody modify it, to ensure
1389 * safety against shm_unlink) by simply letting the mmap fail and
1390 * retrying after a few seconds.
1391 * For global shm, everybody has rw access to it until the sessiond
1395 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1397 int wait_shm_fd
, ret
;
1401 * Try to open read-only.
1403 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1404 if (wait_shm_fd
>= 0) {
1407 size_t bytes_read
= 0;
1410 * Try to read the fd. If unable to do so, try opening
1414 len
= read(wait_shm_fd
,
1415 &((char *) &tmp_read
)[bytes_read
],
1416 sizeof(tmp_read
) - bytes_read
);
1420 } while ((len
< 0 && errno
== EINTR
)
1421 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1422 if (bytes_read
!= sizeof(tmp_read
)) {
1423 ret
= close(wait_shm_fd
);
1425 ERR("close wait_shm_fd");
1430 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1432 * Real-only open did not work, and it's not because the
1433 * entry was not present. It's a failure that prohibits
1436 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1442 * If the open failed because the file did not exist, or because
1443 * the file was not truncated yet, try creating it ourself.
1445 URCU_TLS(lttng_ust_nest_count
)++;
1447 URCU_TLS(lttng_ust_nest_count
)--;
1452 * Parent: wait for child to return, in which case the
1453 * shared memory map will have been created.
1455 pid
= wait(&status
);
1456 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1461 * Try to open read-only again after creation.
1463 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1464 if (wait_shm_fd
< 0) {
1466 * Real-only open did not work. It's a failure
1467 * that prohibits using shm.
1469 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1473 } else if (pid
== 0) {
1477 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1478 if (sock_info
->global
)
1479 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1481 * We're alone in a child process, so we can modify the
1482 * process-wide umask.
1484 umask(~create_mode
);
1486 * Try creating shm (or get rw access).
1487 * We don't do an exclusive open, because we allow other
1488 * processes to create+ftruncate it concurrently.
1490 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1491 O_RDWR
| O_CREAT
, create_mode
);
1492 if (wait_shm_fd
>= 0) {
1493 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1495 PERROR("ftruncate");
1496 _exit(EXIT_FAILURE
);
1498 _exit(EXIT_SUCCESS
);
1501 * For local shm, we need to have rw access to accept
1502 * opening it: this means the local sessiond will be
1503 * able to wake us up. For global shm, we open it even
1504 * if rw access is not granted, because the root.root
1505 * sessiond will be able to override all rights and wake
1508 if (!sock_info
->global
&& errno
!= EACCES
) {
1509 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1510 _exit(EXIT_FAILURE
);
1513 * The shm exists, but we cannot open it RW. Report
1516 _exit(EXIT_SUCCESS
);
1521 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1522 struct stat statbuf
;
1525 * Ensure that our user is the owner of the shm file for
1526 * local shm. If we do not own the file, it means our
1527 * sessiond will not have access to wake us up (there is
1528 * probably a rogue process trying to fake our
1529 * sessiond). Fallback to polling method in this case.
1531 ret
= fstat(wait_shm_fd
, &statbuf
);
1536 if (statbuf
.st_uid
!= getuid())
1542 ret
= close(wait_shm_fd
);
1544 PERROR("Error closing fd");
1550 char *get_map_shm(struct sock_info
*sock_info
)
1553 int wait_shm_fd
, ret
;
1554 char *wait_shm_mmap
;
1556 page_size
= sysconf(_SC_PAGE_SIZE
);
1557 if (page_size
<= 0) {
1561 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1565 lttng_ust_lock_fd_tracker();
1566 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1567 if (wait_shm_fd
< 0) {
1568 lttng_ust_unlock_fd_tracker();
1572 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1574 ret
= close(wait_shm_fd
);
1576 PERROR("Error closing fd");
1578 lttng_ust_unlock_fd_tracker();
1583 lttng_ust_unlock_fd_tracker();
1585 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1586 MAP_SHARED
, wait_shm_fd
, 0);
1588 /* close shm fd immediately after taking the mmap reference */
1589 lttng_ust_lock_fd_tracker();
1590 ret
= close(wait_shm_fd
);
1592 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1594 PERROR("Error closing fd");
1596 lttng_ust_unlock_fd_tracker();
1598 if (wait_shm_mmap
== MAP_FAILED
) {
1599 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1602 return wait_shm_mmap
;
1609 void wait_for_sessiond(struct sock_info
*sock_info
)
1611 /* Use ust_lock to check if we should quit. */
1615 if (wait_poll_fallback
) {
1620 assert(sock_info
->wait_shm_mmap
);
1622 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1623 /* Wait for futex wakeup */
1624 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1627 while (lttng_ust_futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1628 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1631 /* Value already changed. */
1634 /* Retry if interrupted by signal. */
1635 break; /* Get out of switch. */
1637 wait_poll_fallback
= 1;
1639 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1640 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1641 "Please upgrade your kernel "
1642 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1643 "mainline). LTTng-UST will use polling mode fallback.");
1662 * This thread does not allocate any resource, except within
1663 * handle_message, within mutex protection. This mutex protects against
1665 * The other moment it allocates resources is at socket connection, which
1666 * is also protected by the mutex.
1669 void *ust_listener_thread(void *arg
)
1671 struct sock_info
*sock_info
= arg
;
1672 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1675 lttng_ust_fixup_tls();
1677 * If available, add '-ust' to the end of this thread's
1680 ret
= lttng_ust_setustprocname();
1682 ERR("Unable to set UST process name");
1685 /* Restart trying to connect to the session daemon */
1687 if (prev_connect_failed
) {
1688 /* Wait for sessiond availability with pipe */
1689 wait_for_sessiond(sock_info
);
1693 * Sleep for 5 seconds before retrying after a
1694 * sequence of failure / wait / failure. This
1695 * deals with a killed or broken session daemon.
1701 prev_connect_failed
= 0;
1708 if (sock_info
->socket
!= -1) {
1709 /* FD tracker is updated by ustcomm_close_unix_sock() */
1710 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1712 ERR("Error closing %s ust cmd socket",
1715 sock_info
->socket
= -1;
1717 if (sock_info
->notify_socket
!= -1) {
1718 /* FD tracker is updated by ustcomm_close_unix_sock() */
1719 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1721 ERR("Error closing %s ust notify socket",
1724 sock_info
->notify_socket
= -1;
1729 * Register. We need to perform both connect and sending
1730 * registration message before doing the next connect otherwise
1731 * we may reach unix socket connect queue max limits and block
1732 * on the 2nd connect while the session daemon is awaiting the
1733 * first connect registration message.
1735 /* Connect cmd socket */
1736 lttng_ust_lock_fd_tracker();
1737 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1738 get_connect_sock_timeout());
1740 lttng_ust_unlock_fd_tracker();
1741 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1742 prev_connect_failed
= 1;
1745 * If we cannot find the sessiond daemon, don't delay
1746 * constructor execution.
1748 ret
= handle_register_failed(sock_info
);
1754 ret
= lttng_ust_add_fd_to_tracker(fd
);
1758 PERROR("close on sock_info->socket");
1761 lttng_ust_unlock_fd_tracker();
1766 sock_info
->socket
= ret
;
1767 lttng_ust_unlock_fd_tracker();
1771 * Unlock/relock ust lock because connect is blocking (with
1772 * timeout). Don't delay constructors on the ust lock for too
1780 * Create only one root handle per listener thread for the whole
1781 * process lifetime, so we ensure we get ID which is statically
1782 * assigned to the root handle.
1784 if (sock_info
->root_handle
== -1) {
1785 ret
= lttng_abi_create_root_handle();
1787 ERR("Error creating root handle");
1790 sock_info
->root_handle
= ret
;
1793 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1795 ERR("Error registering to %s ust cmd socket",
1797 prev_connect_failed
= 1;
1799 * If we cannot register to the sessiond daemon, don't
1800 * delay constructor execution.
1802 ret
= handle_register_failed(sock_info
);
1810 * Unlock/relock ust lock because connect is blocking (with
1811 * timeout). Don't delay constructors on the ust lock for too
1818 /* Connect notify socket */
1819 lttng_ust_lock_fd_tracker();
1820 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1821 get_connect_sock_timeout());
1823 lttng_ust_unlock_fd_tracker();
1824 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1825 prev_connect_failed
= 1;
1828 * If we cannot find the sessiond daemon, don't delay
1829 * constructor execution.
1831 ret
= handle_register_failed(sock_info
);
1838 ret
= lttng_ust_add_fd_to_tracker(fd
);
1842 PERROR("close on sock_info->notify_socket");
1845 lttng_ust_unlock_fd_tracker();
1850 sock_info
->notify_socket
= ret
;
1851 lttng_ust_unlock_fd_tracker();
1855 * Unlock/relock ust lock because connect is blocking (with
1856 * timeout). Don't delay constructors on the ust lock for too
1863 timeout
= get_notify_sock_timeout();
1866 * Give at least 10ms to sessiond to reply to
1871 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1874 WARN("Error setting socket receive timeout");
1876 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1879 WARN("Error setting socket send timeout");
1881 } else if (timeout
< -1) {
1882 WARN("Unsupported timeout value %ld", timeout
);
1885 ret
= register_to_sessiond(sock_info
->notify_socket
,
1886 USTCTL_SOCKET_NOTIFY
);
1888 ERR("Error registering to %s ust notify socket",
1890 prev_connect_failed
= 1;
1892 * If we cannot register to the sessiond daemon, don't
1893 * delay constructor execution.
1895 ret
= handle_register_failed(sock_info
);
1900 sock
= sock_info
->socket
;
1906 struct ustcomm_ust_msg lum
;
1908 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1910 case 0: /* orderly shutdown */
1911 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1916 * Either sessiond has shutdown or refused us by closing the socket.
1917 * In either case, we don't want to delay construction execution,
1918 * and we need to wait before retry.
1920 prev_connect_failed
= 1;
1922 * If we cannot register to the sessiond daemon, don't
1923 * delay constructor execution.
1925 ret
= handle_register_failed(sock_info
);
1930 print_cmd(lum
.cmd
, lum
.handle
);
1931 ret
= handle_message(sock_info
, sock
, &lum
);
1933 ERR("Error handling message for %s socket",
1936 * Close socket if protocol error is
1944 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1946 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1948 if (len
== -ECONNRESET
) {
1949 DBG("%s remote end closed connection", sock_info
->name
);
1960 /* Cleanup socket handles before trying to reconnect */
1961 lttng_ust_objd_table_owner_cleanup(sock_info
);
1963 goto restart
; /* try to reconnect */
1968 pthread_mutex_lock(&ust_exit_mutex
);
1969 sock_info
->thread_active
= 0;
1970 pthread_mutex_unlock(&ust_exit_mutex
);
1975 * Weak symbol to call when the ust malloc wrapper is not loaded.
1977 __attribute__((weak
))
1978 void lttng_ust_malloc_wrapper_init(void)
1983 * sessiond monitoring thread: monitor presence of global and per-user
1984 * sessiond by polling the application common named pipe.
1986 void __attribute__((constructor
)) lttng_ust_init(void)
1988 struct timespec constructor_timeout
;
1989 sigset_t sig_all_blocked
, orig_parent_mask
;
1990 pthread_attr_t thread_attr
;
1995 if (uatomic_xchg(&initialized
, 1) == 1)
1999 * Fixup interdependency between TLS fixup mutex (which happens
2000 * to be the dynamic linker mutex) and ust_lock, taken within
2003 lttng_ust_fixup_tls();
2005 lttng_ust_loaded
= 1;
2008 * We need to ensure that the liblttng-ust library is not unloaded to avoid
2009 * the unloading of code used by the ust_listener_threads as we can not
2010 * reliably know when they exited. To do that, manually load
2011 * liblttng-ust.so to increment the dynamic loader's internal refcount for
2012 * this library so it never becomes zero, thus never gets unloaded from the
2013 * address space of the process. Since we are already running in the
2014 * constructor of the LTTNG_UST_LIB_SO_NAME library, calling dlopen will
2015 * simply increment the refcount and no additionnal work is needed by the
2016 * dynamic loader as the shared library is already loaded in the address
2017 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
2018 * unloading of the UST library if its refcount becomes zero (which should
2019 * never happen). Do the return value check but discard the handle at the
2020 * end of the function as it's not needed.
2022 handle
= dlopen(LTTNG_UST_LIB_SO_NAME
, RTLD_LAZY
| RTLD_NODELETE
);
2024 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SO_NAME
);
2028 * We want precise control over the order in which we construct
2029 * our sub-libraries vs starting to receive commands from
2030 * sessiond (otherwise leading to errors when trying to create
2031 * sessiond before the init functions are completed).
2034 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2036 lttng_ust_init_fd_tracker();
2037 lttng_ust_clock_init();
2038 lttng_ust_getcpu_init();
2039 lttng_ust_statedump_init();
2040 lttng_ring_buffer_metadata_client_init();
2041 lttng_ring_buffer_client_overwrite_init();
2042 lttng_ring_buffer_client_overwrite_rt_init();
2043 lttng_ring_buffer_client_discard_init();
2044 lttng_ring_buffer_client_discard_rt_init();
2045 lttng_counter_client_percpu_32_modular_init();
2046 lttng_counter_client_percpu_64_modular_init();
2047 lttng_perf_counter_init();
2049 * Invoke ust malloc wrapper init before starting other threads.
2051 lttng_ust_malloc_wrapper_init();
2053 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
2055 get_allow_blocking();
2057 ret
= sem_init(&constructor_wait
, 0, 0);
2062 ret
= setup_global_apps();
2064 assert(global_apps
.allowed
== 0);
2065 DBG("global apps setup returned %d", ret
);
2068 ret
= setup_local_apps();
2070 assert(local_apps
.allowed
== 0);
2071 DBG("local apps setup returned %d", ret
);
2074 /* A new thread created by pthread_create inherits the signal mask
2075 * from the parent. To avoid any signal being received by the
2076 * listener thread, we block all signals temporarily in the parent,
2077 * while we create the listener thread.
2079 sigfillset(&sig_all_blocked
);
2080 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
2082 ERR("pthread_sigmask: %s", strerror(ret
));
2085 ret
= pthread_attr_init(&thread_attr
);
2087 ERR("pthread_attr_init: %s", strerror(ret
));
2089 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
2091 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
2094 if (global_apps
.allowed
) {
2095 pthread_mutex_lock(&ust_exit_mutex
);
2096 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
2097 ust_listener_thread
, &global_apps
);
2099 ERR("pthread_create global: %s", strerror(ret
));
2101 global_apps
.thread_active
= 1;
2102 pthread_mutex_unlock(&ust_exit_mutex
);
2104 handle_register_done(&global_apps
);
2107 if (local_apps
.allowed
) {
2108 pthread_mutex_lock(&ust_exit_mutex
);
2109 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
2110 ust_listener_thread
, &local_apps
);
2112 ERR("pthread_create local: %s", strerror(ret
));
2114 local_apps
.thread_active
= 1;
2115 pthread_mutex_unlock(&ust_exit_mutex
);
2117 handle_register_done(&local_apps
);
2119 ret
= pthread_attr_destroy(&thread_attr
);
2121 ERR("pthread_attr_destroy: %s", strerror(ret
));
2124 /* Restore original signal mask in parent */
2125 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
2127 ERR("pthread_sigmask: %s", strerror(ret
));
2130 switch (timeout_mode
) {
2131 case 1: /* timeout wait */
2133 ret
= sem_timedwait(&constructor_wait
,
2134 &constructor_timeout
);
2135 } while (ret
< 0 && errno
== EINTR
);
2139 ERR("Timed out waiting for lttng-sessiond");
2142 PERROR("sem_timedwait");
2145 ERR("Unexpected error \"%s\" returned by sem_timedwait",
2150 case -1:/* wait forever */
2152 ret
= sem_wait(&constructor_wait
);
2153 } while (ret
< 0 && errno
== EINTR
);
2160 ERR("Unexpected error \"%s\" returned by sem_wait",
2165 case 0: /* no timeout */
2171 void lttng_ust_cleanup(int exiting
)
2173 cleanup_sock_info(&global_apps
, exiting
);
2174 cleanup_sock_info(&local_apps
, exiting
);
2175 local_apps
.allowed
= 0;
2176 global_apps
.allowed
= 0;
2178 * The teardown in this function all affect data structures
2179 * accessed under the UST lock by the listener thread. This
2180 * lock, along with the lttng_ust_comm_should_quit flag, ensure
2181 * that none of these threads are accessing this data at this
2184 lttng_ust_abi_exit();
2185 lttng_ust_events_exit();
2186 lttng_perf_counter_exit();
2187 lttng_ring_buffer_client_discard_rt_exit();
2188 lttng_ring_buffer_client_discard_exit();
2189 lttng_ring_buffer_client_overwrite_rt_exit();
2190 lttng_ring_buffer_client_overwrite_exit();
2191 lttng_ring_buffer_metadata_client_exit();
2192 lttng_counter_client_percpu_32_modular_exit();
2193 lttng_counter_client_percpu_64_modular_exit();
2194 lttng_ust_statedump_destroy();
2197 /* Reinitialize values for fork */
2198 sem_count
= sem_count_initial_value
;
2199 lttng_ust_comm_should_quit
= 0;
2204 void __attribute__((destructor
)) lttng_ust_exit(void)
2209 * Using pthread_cancel here because:
2210 * A) we don't want to hang application teardown.
2211 * B) the thread is not allocating any resource.
2215 * Require the communication thread to quit. Synchronize with
2216 * mutexes to ensure it is not in a mutex critical section when
2217 * pthread_cancel is later called.
2220 lttng_ust_comm_should_quit
= 1;
2223 pthread_mutex_lock(&ust_exit_mutex
);
2224 /* cancel threads */
2225 if (global_apps
.thread_active
) {
2226 ret
= pthread_cancel(global_apps
.ust_listener
);
2228 ERR("Error cancelling global ust listener thread: %s",
2231 global_apps
.thread_active
= 0;
2234 if (local_apps
.thread_active
) {
2235 ret
= pthread_cancel(local_apps
.ust_listener
);
2237 ERR("Error cancelling local ust listener thread: %s",
2240 local_apps
.thread_active
= 0;
2243 pthread_mutex_unlock(&ust_exit_mutex
);
2246 * Do NOT join threads: use of sys_futex makes it impossible to
2247 * join the threads without using async-cancel, but async-cancel
2248 * is delivered by a signal, which could hit the target thread
2249 * anywhere in its code path, including while the ust_lock() is
2250 * held, causing a deadlock for the other thread. Let the OS
2251 * cleanup the threads if there are stalled in a syscall.
2253 lttng_ust_cleanup(1);
2257 void ust_context_ns_reset(void)
2259 lttng_context_pid_ns_reset();
2260 lttng_context_cgroup_ns_reset();
2261 lttng_context_ipc_ns_reset();
2262 lttng_context_mnt_ns_reset();
2263 lttng_context_net_ns_reset();
2264 lttng_context_user_ns_reset();
2265 lttng_context_time_ns_reset();
2266 lttng_context_uts_ns_reset();
2270 void ust_context_vuids_reset(void)
2272 lttng_context_vuid_reset();
2273 lttng_context_veuid_reset();
2274 lttng_context_vsuid_reset();
2278 void ust_context_vgids_reset(void)
2280 lttng_context_vgid_reset();
2281 lttng_context_vegid_reset();
2282 lttng_context_vsgid_reset();
2286 * We exclude the worker threads across fork and clone (except
2287 * CLONE_VM), because these system calls only keep the forking thread
2288 * running in the child. Therefore, we don't want to call fork or clone
2289 * in the middle of an tracepoint or ust tracing state modification.
2290 * Holding this mutex protects these structures across fork and clone.
2292 void ust_before_fork(sigset_t
*save_sigset
)
2295 * Disable signals. This is to avoid that the child intervenes
2296 * before it is properly setup for tracing. It is safer to
2297 * disable all signals, because then we know we are not breaking
2298 * anything by restoring the original mask.
2303 /* Fixup lttng-ust TLS. */
2304 lttng_ust_fixup_tls();
2306 if (URCU_TLS(lttng_ust_nest_count
))
2308 /* Disable signals */
2309 sigfillset(&all_sigs
);
2310 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2312 PERROR("sigprocmask");
2315 pthread_mutex_lock(&ust_fork_mutex
);
2318 lttng_ust_urcu_before_fork();
2319 if (lttng_ust_liburcu_bp_before_fork
)
2320 lttng_ust_liburcu_bp_before_fork();
2321 lttng_ust_lock_fd_tracker();
2325 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2329 DBG("process %d", getpid());
2330 lttng_perf_unlock();
2331 lttng_ust_unlock_fd_tracker();
2334 pthread_mutex_unlock(&ust_fork_mutex
);
2336 /* Restore signals */
2337 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2339 PERROR("sigprocmask");
2343 void ust_after_fork_parent(sigset_t
*restore_sigset
)
2345 if (URCU_TLS(lttng_ust_nest_count
))
2347 DBG("process %d", getpid());
2348 lttng_ust_urcu_after_fork_parent();
2349 if (lttng_ust_liburcu_bp_after_fork_parent
)
2350 lttng_ust_liburcu_bp_after_fork_parent();
2351 /* Release mutexes and reenable signals */
2352 ust_after_fork_common(restore_sigset
);
2356 * After fork, in the child, we need to cleanup all the leftover state,
2357 * except the worker thread which already magically disappeared thanks
2358 * to the weird Linux fork semantics. After tyding up, we call
2359 * lttng_ust_init() again to start over as a new PID.
2361 * This is meant for forks() that have tracing in the child between the
2362 * fork and following exec call (if there is any).
2364 void ust_after_fork_child(sigset_t
*restore_sigset
)
2366 if (URCU_TLS(lttng_ust_nest_count
))
2368 lttng_context_vpid_reset();
2369 lttng_context_vtid_reset();
2370 lttng_context_procname_reset();
2371 ust_context_ns_reset();
2372 ust_context_vuids_reset();
2373 ust_context_vgids_reset();
2374 DBG("process %d", getpid());
2375 /* Release urcu mutexes */
2376 lttng_ust_urcu_after_fork_child();
2377 if (lttng_ust_liburcu_bp_after_fork_child
)
2378 lttng_ust_liburcu_bp_after_fork_child();
2379 lttng_ust_cleanup(0);
2380 /* Release mutexes and reenable signals */
2381 ust_after_fork_common(restore_sigset
);
2385 void ust_after_setns(void)
2387 ust_context_ns_reset();
2388 ust_context_vuids_reset();
2389 ust_context_vgids_reset();
2392 void ust_after_unshare(void)
2394 ust_context_ns_reset();
2395 ust_context_vuids_reset();
2396 ust_context_vgids_reset();
2399 void ust_after_setuid(void)
2401 ust_context_vuids_reset();
2404 void ust_after_seteuid(void)
2406 ust_context_vuids_reset();
2409 void ust_after_setreuid(void)
2411 ust_context_vuids_reset();
2414 void ust_after_setresuid(void)
2416 ust_context_vuids_reset();
2419 void ust_after_setgid(void)
2421 ust_context_vgids_reset();
2424 void ust_after_setegid(void)
2426 ust_context_vgids_reset();
2429 void ust_after_setregid(void)
2431 ust_context_vgids_reset();
2434 void ust_after_setresgid(void)
2436 ust_context_vgids_reset();
2439 void lttng_ust_sockinfo_session_enabled(void *owner
)
2441 struct sock_info
*sock_info
= owner
;
2442 sock_info
->statedump_pending
= 1;