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
24 #include <sys/types.h>
25 #include <sys/socket.h>
28 #include <sys/types.h>
34 #include <semaphore.h>
39 #include <urcu/uatomic.h>
40 #include <urcu/futex.h>
41 #include <urcu/compiler.h>
43 #include <lttng/ust-events.h>
44 #include <lttng/ust-abi.h>
45 #include <lttng/ust.h>
46 #include <lttng/ust-error.h>
47 #include <lttng/ust-ctl.h>
48 #include <urcu/tls-compat.h>
51 #include <usterr-signal-safe.h>
53 #include "tracepoint-internal.h"
54 #include "lttng-tracer-core.h"
56 #include "../libringbuffer/rb-init.h"
57 #include "lttng-ust-statedump.h"
59 #include "../libringbuffer/getcpu.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 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
88 /* Allow nesting the ust_mutex within the same thread. */
89 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
92 * ust_exit_mutex protects thread_active variable wrt thread exit. It
93 * cannot be done by ust_mutex because pthread_cancel(), which takes an
94 * internal libc lock, cannot nest within ust_mutex.
96 * It never nests within a ust_mutex.
98 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
101 * ust_fork_mutex protects base address statedump tracing against forks. It
102 * prevents the dynamic loader lock to be taken (by base address statedump
103 * tracing) while a fork is happening, thus preventing deadlock issues with
104 * the dynamic loader lock.
106 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
108 /* Should the ust comm thread quit ? */
109 static int lttng_ust_comm_should_quit
;
112 * This variable can be tested by applications to check whether
113 * lttng-ust is loaded. They simply have to define their own
114 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
115 * library constructor.
117 int lttng_ust_loaded
__attribute__((weak
));
120 * Return 0 on success, -1 if should quit.
121 * The lock is taken in both cases.
126 sigset_t sig_all_blocked
, orig_mask
;
129 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
131 ERR("pthread_setcancelstate: %s", strerror(ret
));
133 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
134 ERR("pthread_setcancelstate: unexpected oldstate");
136 sigfillset(&sig_all_blocked
);
137 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
139 ERR("pthread_sigmask: %s", strerror(ret
));
141 if (!URCU_TLS(ust_mutex_nest
)++)
142 pthread_mutex_lock(&ust_mutex
);
143 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
145 ERR("pthread_sigmask: %s", strerror(ret
));
147 if (lttng_ust_comm_should_quit
) {
155 * ust_lock_nocheck() can be used in constructors/destructors, because
156 * they are already nested within the dynamic loader lock, and therefore
157 * have exclusive access against execution of liblttng-ust destructor.
160 void ust_lock_nocheck(void)
162 sigset_t sig_all_blocked
, orig_mask
;
165 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
167 ERR("pthread_setcancelstate: %s", strerror(ret
));
169 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
170 ERR("pthread_setcancelstate: unexpected oldstate");
172 sigfillset(&sig_all_blocked
);
173 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
175 ERR("pthread_sigmask: %s", strerror(ret
));
177 if (!URCU_TLS(ust_mutex_nest
)++)
178 pthread_mutex_lock(&ust_mutex
);
179 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
181 ERR("pthread_sigmask: %s", strerror(ret
));
188 void ust_unlock(void)
190 sigset_t sig_all_blocked
, orig_mask
;
193 sigfillset(&sig_all_blocked
);
194 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
196 ERR("pthread_sigmask: %s", strerror(ret
));
198 if (!--URCU_TLS(ust_mutex_nest
))
199 pthread_mutex_unlock(&ust_mutex
);
200 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
202 ERR("pthread_sigmask: %s", strerror(ret
));
204 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
206 ERR("pthread_setcancelstate: %s", strerror(ret
));
208 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
209 ERR("pthread_setcancelstate: unexpected oldstate");
214 * Wait for either of these before continuing to the main
216 * - the register_done message from sessiond daemon
217 * (will let the sessiond daemon enable sessions before main
219 * - sessiond daemon is not reachable.
220 * - timeout (ensuring applications are resilient to session
223 static sem_t constructor_wait
;
225 * Doing this for both the global and local sessiond.
227 static int sem_count
= { 2 };
230 * Counting nesting within lttng-ust. Used to ensure that calling fork()
231 * from liblttng-ust does not execute the pre/post fork handlers.
233 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
236 * Info about socket and associated listener thread.
240 pthread_t ust_listener
; /* listener thread */
242 int constructor_sem_posted
;
247 char sock_path
[PATH_MAX
];
251 char wait_shm_path
[PATH_MAX
];
253 /* Keep track of lazy state dump not performed yet. */
254 int statedump_pending
;
257 /* Socket from app (connect) to session daemon (listen) for communication */
258 struct sock_info global_apps
= {
266 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
270 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
272 .statedump_pending
= 0,
275 /* TODO: allow global_apps_sock_path override */
277 struct sock_info local_apps
= {
281 .allowed
= 0, /* Check setuid bit first */
287 .statedump_pending
= 0,
290 static int wait_poll_fallback
;
292 static const char *cmd_name_mapping
[] = {
293 [ LTTNG_UST_RELEASE
] = "Release",
294 [ LTTNG_UST_SESSION
] = "Create Session",
295 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
297 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
298 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
299 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
300 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
302 /* Session FD commands */
303 [ LTTNG_UST_CHANNEL
] = "Create Channel",
304 [ LTTNG_UST_SESSION_START
] = "Start Session",
305 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
307 /* Channel FD commands */
308 [ LTTNG_UST_STREAM
] = "Create Stream",
309 [ LTTNG_UST_EVENT
] = "Create Event",
311 /* Event and Channel FD commands */
312 [ LTTNG_UST_CONTEXT
] = "Create Context",
313 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
315 /* Event, Channel and Session commands */
316 [ LTTNG_UST_ENABLE
] = "Enable",
317 [ LTTNG_UST_DISABLE
] = "Disable",
319 /* Tracepoint list commands */
320 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
321 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
323 /* Event FD commands */
324 [ LTTNG_UST_FILTER
] = "Create Filter",
325 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
328 static const char *str_timeout
;
329 static int got_timeout_env
;
331 extern void lttng_ring_buffer_client_overwrite_init(void);
332 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
333 extern void lttng_ring_buffer_client_discard_init(void);
334 extern void lttng_ring_buffer_client_discard_rt_init(void);
335 extern void lttng_ring_buffer_metadata_client_init(void);
336 extern void lttng_ring_buffer_client_overwrite_exit(void);
337 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
338 extern void lttng_ring_buffer_client_discard_exit(void);
339 extern void lttng_ring_buffer_client_discard_rt_exit(void);
340 extern void lttng_ring_buffer_metadata_client_exit(void);
342 static char *get_map_shm(struct sock_info
*sock_info
);
344 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
347 size_t copied
= 0, to_copy
= len
;
350 ret
= read(fd
, buf
+ copied
, to_copy
);
355 } while ((ret
> 0 && to_copy
> 0)
356 || (ret
< 0 && errno
== EINTR
));
363 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
367 const char *get_lttng_home_dir(void)
371 val
= (const char *) lttng_getenv("LTTNG_HOME");
375 return (const char *) lttng_getenv("HOME");
379 * Force a read (imply TLS fixup for dlopen) of TLS variables.
382 void lttng_fixup_nest_count_tls(void)
384 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
388 void lttng_fixup_ust_mutex_nest_tls(void)
390 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
397 void lttng_fixup_urcu_bp_tls(void)
403 void lttng_ust_fixup_tls(void)
405 lttng_fixup_urcu_bp_tls();
406 lttng_fixup_ringbuffer_tls();
407 lttng_fixup_vtid_tls();
408 lttng_fixup_nest_count_tls();
409 lttng_fixup_procname_tls();
410 lttng_fixup_ust_mutex_nest_tls();
411 lttng_ust_fixup_fd_tracker_tls();
414 int lttng_get_notify_socket(void *owner
)
416 struct sock_info
*info
= owner
;
418 return info
->notify_socket
;
422 void print_cmd(int cmd
, int handle
)
424 const char *cmd_name
= "Unknown";
426 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
427 && cmd_name_mapping
[cmd
]) {
428 cmd_name
= cmd_name_mapping
[cmd
];
430 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
432 lttng_ust_obj_get_name(handle
), handle
);
436 int setup_global_apps(void)
439 assert(!global_apps
.wait_shm_mmap
);
441 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
442 if (!global_apps
.wait_shm_mmap
) {
443 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
444 global_apps
.allowed
= 0;
449 global_apps
.allowed
= 1;
454 int setup_local_apps(void)
457 const char *home_dir
;
460 assert(!local_apps
.wait_shm_mmap
);
464 * Disallow per-user tracing for setuid binaries.
466 if (uid
!= geteuid()) {
467 assert(local_apps
.allowed
== 0);
471 home_dir
= get_lttng_home_dir();
473 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
474 assert(local_apps
.allowed
== 0);
478 local_apps
.allowed
= 1;
479 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
481 LTTNG_DEFAULT_HOME_RUNDIR
,
482 LTTNG_UST_SOCK_FILENAME
);
483 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
484 LTTNG_UST_WAIT_FILENAME
,
487 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
488 if (!local_apps
.wait_shm_mmap
) {
489 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
490 local_apps
.allowed
= 0;
499 * Get socket timeout, in ms.
500 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
503 long get_timeout(void)
505 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
507 if (!got_timeout_env
) {
508 str_timeout
= lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT");
512 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
513 /* All negative values are considered as "-1". */
514 if (constructor_delay_ms
< -1)
515 constructor_delay_ms
= -1;
516 return constructor_delay_ms
;
519 /* Timeout for notify socket send and recv. */
521 long get_notify_sock_timeout(void)
523 return get_timeout();
526 /* Timeout for connecting to cmd and notify sockets. */
528 long get_connect_sock_timeout(void)
530 return get_timeout();
534 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
537 int get_constructor_timeout(struct timespec
*constructor_timeout
)
539 long constructor_delay_ms
;
542 constructor_delay_ms
= get_timeout();
544 switch (constructor_delay_ms
) {
545 case -1:/* fall-through */
547 return constructor_delay_ms
;
553 * If we are unable to find the current time, don't wait.
555 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
560 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
561 constructor_timeout
->tv_nsec
+=
562 (constructor_delay_ms
% 1000UL) * 1000000UL;
563 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
564 constructor_timeout
->tv_sec
++;
565 constructor_timeout
->tv_nsec
-= 1000000000UL;
567 /* Timeout wait (constructor_delay_ms). */
572 void get_allow_blocking(void)
574 const char *str_allow_blocking
=
575 lttng_getenv("LTTNG_UST_ALLOW_BLOCKING");
577 if (str_allow_blocking
) {
578 DBG("%s environment variable is set",
579 "LTTNG_UST_ALLOW_BLOCKING");
580 lttng_ust_ringbuffer_set_allow_blocking();
585 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
587 return ustcomm_send_reg_msg(socket
,
590 lttng_alignof(uint8_t) * CHAR_BIT
,
591 lttng_alignof(uint16_t) * CHAR_BIT
,
592 lttng_alignof(uint32_t) * CHAR_BIT
,
593 lttng_alignof(uint64_t) * CHAR_BIT
,
594 lttng_alignof(unsigned long) * CHAR_BIT
);
598 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
602 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
605 DBG("message successfully sent");
608 if (len
== -ECONNRESET
) {
609 DBG("remote end closed connection");
614 DBG("incorrect message size: %zd", len
);
620 int handle_register_done(struct sock_info
*sock_info
)
624 if (sock_info
->constructor_sem_posted
)
626 sock_info
->constructor_sem_posted
= 1;
627 if (uatomic_read(&sem_count
) <= 0) {
630 ret
= uatomic_add_return(&sem_count
, -1);
632 ret
= sem_post(&constructor_wait
);
639 * Only execute pending statedump after the constructor semaphore has
640 * been posted by each listener thread. This means statedump will only
641 * be performed after the "registration done" command is received from
642 * each session daemon the application is connected to.
644 * This ensures we don't run into deadlock issues with the dynamic
645 * loader mutex, which is held while the constructor is called and
646 * waiting on the constructor semaphore. All operations requiring this
647 * dynamic loader lock need to be postponed using this mechanism.
650 void handle_pending_statedump(struct sock_info
*sock_info
)
652 int ctor_passed
= sock_info
->constructor_sem_posted
;
654 if (ctor_passed
&& sock_info
->statedump_pending
) {
655 sock_info
->statedump_pending
= 0;
656 pthread_mutex_lock(&ust_fork_mutex
);
657 lttng_handle_pending_statedump(sock_info
);
658 pthread_mutex_unlock(&ust_fork_mutex
);
663 int handle_message(struct sock_info
*sock_info
,
664 int sock
, struct ustcomm_ust_msg
*lum
)
667 const struct lttng_ust_objd_ops
*ops
;
668 struct ustcomm_ust_reply lur
;
670 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
673 memset(&lur
, 0, sizeof(lur
));
676 ret
= -LTTNG_UST_ERR_EXITING
;
680 ops
= objd_ops(lum
->handle
);
687 case LTTNG_UST_REGISTER_DONE
:
688 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
689 ret
= handle_register_done(sock_info
);
693 case LTTNG_UST_RELEASE
:
694 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
697 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
699 case LTTNG_UST_FILTER
:
701 /* Receive filter data */
702 struct lttng_ust_filter_bytecode_node
*bytecode
;
704 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
705 ERR("Filter data size is too large: %u bytes",
706 lum
->u
.filter
.data_size
);
711 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
712 ERR("Filter reloc offset %u is not within data",
713 lum
->u
.filter
.reloc_offset
);
718 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
723 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
724 lum
->u
.filter
.data_size
);
726 case 0: /* orderly shutdown */
731 if (len
== lum
->u
.filter
.data_size
) {
732 DBG("filter data received");
734 } else if (len
< 0) {
735 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
736 if (len
== -ECONNRESET
) {
737 ERR("%s remote end closed connection", sock_info
->name
);
746 DBG("incorrect filter data message size: %zd", len
);
752 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
753 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
754 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
756 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
757 (unsigned long) bytecode
,
762 /* don't free bytecode if everything went fine. */
769 case LTTNG_UST_EXCLUSION
:
771 /* Receive exclusion names */
772 struct lttng_ust_excluder_node
*node
;
775 count
= lum
->u
.exclusion
.count
;
777 /* There are no names to read */
781 node
= zmalloc(sizeof(*node
) +
782 count
* LTTNG_UST_SYM_NAME_LEN
);
787 node
->excluder
.count
= count
;
788 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
789 count
* LTTNG_UST_SYM_NAME_LEN
);
791 case 0: /* orderly shutdown */
796 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
797 DBG("Exclusion data received");
799 } else if (len
< 0) {
800 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
801 if (len
== -ECONNRESET
) {
802 ERR("%s remote end closed connection", sock_info
->name
);
811 DBG("Incorrect exclusion data message size: %zd", len
);
818 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
819 (unsigned long) node
,
824 /* Don't free exclusion data if everything went fine. */
831 case LTTNG_UST_CHANNEL
:
836 len
= ustcomm_recv_channel_from_sessiond(sock
,
837 &chan_data
, lum
->u
.channel
.len
,
840 case 0: /* orderly shutdown */
844 if (len
== lum
->u
.channel
.len
) {
845 DBG("channel data received");
847 } else if (len
< 0) {
848 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
849 if (len
== -ECONNRESET
) {
850 ERR("%s remote end closed connection", sock_info
->name
);
857 DBG("incorrect channel data message size: %zd", len
);
862 args
.channel
.chan_data
= chan_data
;
863 args
.channel
.wakeup_fd
= wakeup_fd
;
865 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
866 (unsigned long) &lum
->u
,
872 case LTTNG_UST_STREAM
:
874 /* Receive shm_fd, wakeup_fd */
875 ret
= ustcomm_recv_stream_from_sessiond(sock
,
878 &args
.stream
.wakeup_fd
);
884 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
885 (unsigned long) &lum
->u
,
891 case LTTNG_UST_CONTEXT
:
892 switch (lum
->u
.context
.ctx
) {
893 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
896 size_t ctxlen
, recvlen
;
898 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
899 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
900 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
901 ERR("Application context string length size is too large: %zu bytes",
906 strcpy(ctxstr
, "$app.");
907 p
= &ctxstr
[strlen("$app.")];
908 recvlen
= ctxlen
- strlen("$app.");
909 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
911 case 0: /* orderly shutdown */
915 if (len
== recvlen
) {
916 DBG("app context data received");
918 } else if (len
< 0) {
919 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
920 if (len
== -ECONNRESET
) {
921 ERR("%s remote end closed connection", sock_info
->name
);
928 DBG("incorrect app context data message size: %zd", len
);
933 /* Put : between provider and ctxname. */
934 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
935 args
.app_context
.ctxname
= ctxstr
;
942 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
943 (unsigned long) &lum
->u
,
951 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
952 (unsigned long) &lum
->u
,
959 lur
.handle
= lum
->handle
;
963 lur
.ret_code
= LTTNG_UST_OK
;
966 * Use -LTTNG_UST_ERR as wildcard for UST internal
967 * error that are not caused by the transport, except if
968 * we already have a more precise error message to
971 if (ret
> -LTTNG_UST_ERR
) {
972 /* Translate code to UST error. */
975 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
978 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
981 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
984 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
987 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
990 lur
.ret_code
= -LTTNG_UST_ERR
;
999 case LTTNG_UST_TRACER_VERSION
:
1000 lur
.u
.version
= lum
->u
.version
;
1002 case LTTNG_UST_TRACEPOINT_LIST_GET
:
1003 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1007 DBG("Return value: %d", lur
.ret_val
);
1012 * Performed delayed statedump operations outside of the UST
1013 * lock. We need to take the dynamic loader lock before we take
1014 * the UST lock internally within handle_pending_statedump().
1016 handle_pending_statedump(sock_info
);
1019 ret
= -LTTNG_UST_ERR_EXITING
;
1023 ret
= send_reply(sock
, &lur
);
1025 DBG("error sending reply");
1030 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1033 if (lur
.ret_code
== LTTNG_UST_OK
) {
1035 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1036 len
= ustcomm_send_unix_sock(sock
,
1037 &args
.field_list
.entry
,
1038 sizeof(args
.field_list
.entry
));
1043 if (len
!= sizeof(args
.field_list
.entry
)) {
1057 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1061 if (sock_info
->root_handle
!= -1) {
1062 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1064 ERR("Error unref root handle");
1066 sock_info
->root_handle
= -1;
1068 sock_info
->constructor_sem_posted
= 0;
1071 * wait_shm_mmap, socket and notify socket are used by listener
1072 * threads outside of the ust lock, so we cannot tear them down
1073 * ourselves, because we cannot join on these threads. Leave
1074 * responsibility of cleaning up these resources to the OS
1080 if (sock_info
->socket
!= -1) {
1081 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1083 ERR("Error closing ust cmd socket");
1085 sock_info
->socket
= -1;
1087 if (sock_info
->notify_socket
!= -1) {
1088 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1090 ERR("Error closing ust notify socket");
1092 sock_info
->notify_socket
= -1;
1094 if (sock_info
->wait_shm_mmap
) {
1097 page_size
= sysconf(_SC_PAGE_SIZE
);
1098 if (page_size
<= 0) {
1102 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1104 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1106 ERR("Error unmapping wait shm");
1109 sock_info
->wait_shm_mmap
= NULL
;
1114 * Using fork to set umask in the child process (not multi-thread safe).
1115 * We deal with the shm_open vs ftruncate race (happening when the
1116 * sessiond owns the shm and does not let everybody modify it, to ensure
1117 * safety against shm_unlink) by simply letting the mmap fail and
1118 * retrying after a few seconds.
1119 * For global shm, everybody has rw access to it until the sessiond
1123 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1125 int wait_shm_fd
, ret
;
1129 * Try to open read-only.
1131 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1132 if (wait_shm_fd
>= 0) {
1135 size_t bytes_read
= 0;
1138 * Try to read the fd. If unable to do so, try opening
1142 len
= read(wait_shm_fd
,
1143 &((char *) &tmp_read
)[bytes_read
],
1144 sizeof(tmp_read
) - bytes_read
);
1148 } while ((len
< 0 && errno
== EINTR
)
1149 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1150 if (bytes_read
!= sizeof(tmp_read
)) {
1151 ret
= close(wait_shm_fd
);
1153 ERR("close wait_shm_fd");
1158 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1160 * Real-only open did not work, and it's not because the
1161 * entry was not present. It's a failure that prohibits
1164 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1170 * If the open failed because the file did not exist, or because
1171 * the file was not truncated yet, try creating it ourself.
1173 URCU_TLS(lttng_ust_nest_count
)++;
1175 URCU_TLS(lttng_ust_nest_count
)--;
1180 * Parent: wait for child to return, in which case the
1181 * shared memory map will have been created.
1183 pid
= wait(&status
);
1184 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1189 * Try to open read-only again after creation.
1191 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1192 if (wait_shm_fd
< 0) {
1194 * Real-only open did not work. It's a failure
1195 * that prohibits using shm.
1197 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1201 } else if (pid
== 0) {
1205 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1206 if (sock_info
->global
)
1207 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1209 * We're alone in a child process, so we can modify the
1210 * process-wide umask.
1212 umask(~create_mode
);
1214 * Try creating shm (or get rw access).
1215 * We don't do an exclusive open, because we allow other
1216 * processes to create+ftruncate it concurrently.
1218 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1219 O_RDWR
| O_CREAT
, create_mode
);
1220 if (wait_shm_fd
>= 0) {
1221 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1223 PERROR("ftruncate");
1224 _exit(EXIT_FAILURE
);
1226 _exit(EXIT_SUCCESS
);
1229 * For local shm, we need to have rw access to accept
1230 * opening it: this means the local sessiond will be
1231 * able to wake us up. For global shm, we open it even
1232 * if rw access is not granted, because the root.root
1233 * sessiond will be able to override all rights and wake
1236 if (!sock_info
->global
&& errno
!= EACCES
) {
1237 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1238 _exit(EXIT_FAILURE
);
1241 * The shm exists, but we cannot open it RW. Report
1244 _exit(EXIT_SUCCESS
);
1249 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1250 struct stat statbuf
;
1253 * Ensure that our user is the owner of the shm file for
1254 * local shm. If we do not own the file, it means our
1255 * sessiond will not have access to wake us up (there is
1256 * probably a rogue process trying to fake our
1257 * sessiond). Fallback to polling method in this case.
1259 ret
= fstat(wait_shm_fd
, &statbuf
);
1264 if (statbuf
.st_uid
!= getuid())
1270 ret
= close(wait_shm_fd
);
1272 PERROR("Error closing fd");
1278 char *get_map_shm(struct sock_info
*sock_info
)
1281 int wait_shm_fd
, ret
;
1282 char *wait_shm_mmap
;
1284 page_size
= sysconf(_SC_PAGE_SIZE
);
1285 if (page_size
<= 0) {
1289 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1293 lttng_ust_lock_fd_tracker();
1294 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1295 if (wait_shm_fd
< 0) {
1296 lttng_ust_unlock_fd_tracker();
1300 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1302 ret
= close(wait_shm_fd
);
1304 PERROR("Error closing fd");
1306 lttng_ust_unlock_fd_tracker();
1311 lttng_ust_unlock_fd_tracker();
1313 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1314 MAP_SHARED
, wait_shm_fd
, 0);
1316 /* close shm fd immediately after taking the mmap reference */
1317 lttng_ust_lock_fd_tracker();
1318 ret
= close(wait_shm_fd
);
1320 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1322 PERROR("Error closing fd");
1324 lttng_ust_unlock_fd_tracker();
1326 if (wait_shm_mmap
== MAP_FAILED
) {
1327 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1330 return wait_shm_mmap
;
1337 void wait_for_sessiond(struct sock_info
*sock_info
)
1339 /* Use ust_lock to check if we should quit. */
1343 if (wait_poll_fallback
) {
1348 assert(sock_info
->wait_shm_mmap
);
1350 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1351 /* Wait for futex wakeup */
1352 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1355 while (futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1356 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1359 /* Value already changed. */
1362 /* Retry if interrupted by signal. */
1363 break; /* Get out of switch. */
1365 wait_poll_fallback
= 1;
1367 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1368 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1369 "Please upgrade your kernel "
1370 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1371 "mainline). LTTng-UST will use polling mode fallback.");
1390 * This thread does not allocate any resource, except within
1391 * handle_message, within mutex protection. This mutex protects against
1393 * The other moment it allocates resources is at socket connection, which
1394 * is also protected by the mutex.
1397 void *ust_listener_thread(void *arg
)
1399 struct sock_info
*sock_info
= arg
;
1400 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1403 lttng_ust_fixup_tls();
1405 * If available, add '-ust' to the end of this thread's
1408 ret
= lttng_ust_setustprocname();
1410 ERR("Unable to set UST process name");
1413 /* Restart trying to connect to the session daemon */
1415 if (prev_connect_failed
) {
1416 /* Wait for sessiond availability with pipe */
1417 wait_for_sessiond(sock_info
);
1421 * Sleep for 5 seconds before retrying after a
1422 * sequence of failure / wait / failure. This
1423 * deals with a killed or broken session daemon.
1429 prev_connect_failed
= 0;
1436 if (sock_info
->socket
!= -1) {
1437 /* FD tracker is updated by ustcomm_close_unix_sock() */
1438 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1440 ERR("Error closing %s ust cmd socket",
1443 sock_info
->socket
= -1;
1445 if (sock_info
->notify_socket
!= -1) {
1446 /* FD tracker is updated by ustcomm_close_unix_sock() */
1447 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1449 ERR("Error closing %s ust notify socket",
1452 sock_info
->notify_socket
= -1;
1457 * Register. We need to perform both connect and sending
1458 * registration message before doing the next connect otherwise
1459 * we may reach unix socket connect queue max limits and block
1460 * on the 2nd connect while the session daemon is awaiting the
1461 * first connect registration message.
1463 /* Connect cmd socket */
1464 lttng_ust_lock_fd_tracker();
1465 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1466 get_connect_sock_timeout());
1468 lttng_ust_unlock_fd_tracker();
1469 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1470 prev_connect_failed
= 1;
1473 * If we cannot find the sessiond daemon, don't delay
1474 * constructor execution.
1476 ret
= handle_register_done(sock_info
);
1482 ret
= lttng_ust_add_fd_to_tracker(fd
);
1486 PERROR("close on sock_info->socket");
1489 lttng_ust_unlock_fd_tracker();
1494 sock_info
->socket
= ret
;
1495 lttng_ust_unlock_fd_tracker();
1499 * Unlock/relock ust lock because connect is blocking (with
1500 * timeout). Don't delay constructors on the ust lock for too
1508 * Create only one root handle per listener thread for the whole
1509 * process lifetime, so we ensure we get ID which is statically
1510 * assigned to the root handle.
1512 if (sock_info
->root_handle
== -1) {
1513 ret
= lttng_abi_create_root_handle();
1515 ERR("Error creating root handle");
1518 sock_info
->root_handle
= ret
;
1521 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1523 ERR("Error registering to %s ust cmd socket",
1525 prev_connect_failed
= 1;
1527 * If we cannot register to the sessiond daemon, don't
1528 * delay constructor execution.
1530 ret
= handle_register_done(sock_info
);
1538 * Unlock/relock ust lock because connect is blocking (with
1539 * timeout). Don't delay constructors on the ust lock for too
1546 /* Connect notify socket */
1547 lttng_ust_lock_fd_tracker();
1548 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1549 get_connect_sock_timeout());
1551 lttng_ust_unlock_fd_tracker();
1552 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1553 prev_connect_failed
= 1;
1556 * If we cannot find the sessiond daemon, don't delay
1557 * constructor execution.
1559 ret
= handle_register_done(sock_info
);
1566 ret
= lttng_ust_add_fd_to_tracker(fd
);
1570 PERROR("close on sock_info->notify_socket");
1573 lttng_ust_unlock_fd_tracker();
1578 sock_info
->notify_socket
= ret
;
1579 lttng_ust_unlock_fd_tracker();
1583 * Unlock/relock ust lock because connect is blocking (with
1584 * timeout). Don't delay constructors on the ust lock for too
1591 timeout
= get_notify_sock_timeout();
1594 * Give at least 10ms to sessiond to reply to
1599 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1602 WARN("Error setting socket receive timeout");
1604 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1607 WARN("Error setting socket send timeout");
1609 } else if (timeout
< -1) {
1610 WARN("Unsupported timeout value %ld", timeout
);
1613 ret
= register_to_sessiond(sock_info
->notify_socket
,
1614 USTCTL_SOCKET_NOTIFY
);
1616 ERR("Error registering to %s ust notify socket",
1618 prev_connect_failed
= 1;
1620 * If we cannot register to the sessiond daemon, don't
1621 * delay constructor execution.
1623 ret
= handle_register_done(sock_info
);
1628 sock
= sock_info
->socket
;
1634 struct ustcomm_ust_msg lum
;
1636 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1638 case 0: /* orderly shutdown */
1639 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1644 * Either sessiond has shutdown or refused us by closing the socket.
1645 * In either case, we don't want to delay construction execution,
1646 * and we need to wait before retry.
1648 prev_connect_failed
= 1;
1650 * If we cannot register to the sessiond daemon, don't
1651 * delay constructor execution.
1653 ret
= handle_register_done(sock_info
);
1658 print_cmd(lum
.cmd
, lum
.handle
);
1659 ret
= handle_message(sock_info
, sock
, &lum
);
1661 ERR("Error handling message for %s socket",
1664 * Close socket if protocol error is
1672 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1674 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1676 if (len
== -ECONNRESET
) {
1677 DBG("%s remote end closed connection", sock_info
->name
);
1688 /* Cleanup socket handles before trying to reconnect */
1689 lttng_ust_objd_table_owner_cleanup(sock_info
);
1691 goto restart
; /* try to reconnect */
1696 pthread_mutex_lock(&ust_exit_mutex
);
1697 sock_info
->thread_active
= 0;
1698 pthread_mutex_unlock(&ust_exit_mutex
);
1703 * Weak symbol to call when the ust malloc wrapper is not loaded.
1705 __attribute__((weak
))
1706 void lttng_ust_malloc_wrapper_init(void)
1711 * sessiond monitoring thread: monitor presence of global and per-user
1712 * sessiond by polling the application common named pipe.
1714 void __attribute__((constructor
)) lttng_ust_init(void)
1716 struct timespec constructor_timeout
;
1717 sigset_t sig_all_blocked
, orig_parent_mask
;
1718 pthread_attr_t thread_attr
;
1722 if (uatomic_xchg(&initialized
, 1) == 1)
1726 * Fixup interdependency between TLS fixup mutex (which happens
1727 * to be the dynamic linker mutex) and ust_lock, taken within
1730 lttng_ust_fixup_tls();
1732 lttng_ust_loaded
= 1;
1735 * We want precise control over the order in which we construct
1736 * our sub-libraries vs starting to receive commands from
1737 * sessiond (otherwise leading to errors when trying to create
1738 * sessiond before the init functions are completed).
1741 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
1743 lttng_ust_init_fd_tracker();
1744 lttng_ust_clock_init();
1745 lttng_ust_getcpu_init();
1746 lttng_ust_statedump_init();
1747 lttng_ring_buffer_metadata_client_init();
1748 lttng_ring_buffer_client_overwrite_init();
1749 lttng_ring_buffer_client_overwrite_rt_init();
1750 lttng_ring_buffer_client_discard_init();
1751 lttng_ring_buffer_client_discard_rt_init();
1752 lttng_perf_counter_init();
1754 * Invoke ust malloc wrapper init before starting other threads.
1756 lttng_ust_malloc_wrapper_init();
1758 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1760 get_allow_blocking();
1762 ret
= sem_init(&constructor_wait
, 0, 0);
1767 ret
= setup_global_apps();
1769 assert(global_apps
.allowed
== 0);
1770 DBG("global apps setup returned %d", ret
);
1773 ret
= setup_local_apps();
1775 assert(local_apps
.allowed
== 0);
1776 DBG("local apps setup returned %d", ret
);
1779 /* A new thread created by pthread_create inherits the signal mask
1780 * from the parent. To avoid any signal being received by the
1781 * listener thread, we block all signals temporarily in the parent,
1782 * while we create the listener thread.
1784 sigfillset(&sig_all_blocked
);
1785 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1787 ERR("pthread_sigmask: %s", strerror(ret
));
1790 ret
= pthread_attr_init(&thread_attr
);
1792 ERR("pthread_attr_init: %s", strerror(ret
));
1794 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1796 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1799 if (global_apps
.allowed
) {
1800 pthread_mutex_lock(&ust_exit_mutex
);
1801 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1802 ust_listener_thread
, &global_apps
);
1804 ERR("pthread_create global: %s", strerror(ret
));
1806 global_apps
.thread_active
= 1;
1807 pthread_mutex_unlock(&ust_exit_mutex
);
1809 handle_register_done(&global_apps
);
1812 if (local_apps
.allowed
) {
1813 pthread_mutex_lock(&ust_exit_mutex
);
1814 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1815 ust_listener_thread
, &local_apps
);
1817 ERR("pthread_create local: %s", strerror(ret
));
1819 local_apps
.thread_active
= 1;
1820 pthread_mutex_unlock(&ust_exit_mutex
);
1822 handle_register_done(&local_apps
);
1824 ret
= pthread_attr_destroy(&thread_attr
);
1826 ERR("pthread_attr_destroy: %s", strerror(ret
));
1829 /* Restore original signal mask in parent */
1830 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1832 ERR("pthread_sigmask: %s", strerror(ret
));
1835 switch (timeout_mode
) {
1836 case 1: /* timeout wait */
1838 ret
= sem_timedwait(&constructor_wait
,
1839 &constructor_timeout
);
1840 } while (ret
< 0 && errno
== EINTR
);
1844 ERR("Timed out waiting for lttng-sessiond");
1847 PERROR("sem_timedwait");
1850 ERR("Unexpected error \"%s\" returned by sem_timedwait",
1855 case -1:/* wait forever */
1857 ret
= sem_wait(&constructor_wait
);
1858 } while (ret
< 0 && errno
== EINTR
);
1865 ERR("Unexpected error \"%s\" returned by sem_wait",
1870 case 0: /* no timeout */
1876 void lttng_ust_cleanup(int exiting
)
1878 cleanup_sock_info(&global_apps
, exiting
);
1879 cleanup_sock_info(&local_apps
, exiting
);
1880 local_apps
.allowed
= 0;
1881 global_apps
.allowed
= 0;
1883 * The teardown in this function all affect data structures
1884 * accessed under the UST lock by the listener thread. This
1885 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1886 * that none of these threads are accessing this data at this
1889 lttng_ust_abi_exit();
1890 lttng_ust_events_exit();
1891 lttng_perf_counter_exit();
1892 lttng_ring_buffer_client_discard_rt_exit();
1893 lttng_ring_buffer_client_discard_exit();
1894 lttng_ring_buffer_client_overwrite_rt_exit();
1895 lttng_ring_buffer_client_overwrite_exit();
1896 lttng_ring_buffer_metadata_client_exit();
1897 lttng_ust_statedump_destroy();
1900 /* Reinitialize values for fork */
1902 lttng_ust_comm_should_quit
= 0;
1907 void __attribute__((destructor
)) lttng_ust_exit(void)
1912 * Using pthread_cancel here because:
1913 * A) we don't want to hang application teardown.
1914 * B) the thread is not allocating any resource.
1918 * Require the communication thread to quit. Synchronize with
1919 * mutexes to ensure it is not in a mutex critical section when
1920 * pthread_cancel is later called.
1923 lttng_ust_comm_should_quit
= 1;
1926 pthread_mutex_lock(&ust_exit_mutex
);
1927 /* cancel threads */
1928 if (global_apps
.thread_active
) {
1929 ret
= pthread_cancel(global_apps
.ust_listener
);
1931 ERR("Error cancelling global ust listener thread: %s",
1934 global_apps
.thread_active
= 0;
1937 if (local_apps
.thread_active
) {
1938 ret
= pthread_cancel(local_apps
.ust_listener
);
1940 ERR("Error cancelling local ust listener thread: %s",
1943 local_apps
.thread_active
= 0;
1946 pthread_mutex_unlock(&ust_exit_mutex
);
1949 * Do NOT join threads: use of sys_futex makes it impossible to
1950 * join the threads without using async-cancel, but async-cancel
1951 * is delivered by a signal, which could hit the target thread
1952 * anywhere in its code path, including while the ust_lock() is
1953 * held, causing a deadlock for the other thread. Let the OS
1954 * cleanup the threads if there are stalled in a syscall.
1956 lttng_ust_cleanup(1);
1960 * We exclude the worker threads across fork and clone (except
1961 * CLONE_VM), because these system calls only keep the forking thread
1962 * running in the child. Therefore, we don't want to call fork or clone
1963 * in the middle of an tracepoint or ust tracing state modification.
1964 * Holding this mutex protects these structures across fork and clone.
1966 void ust_before_fork(sigset_t
*save_sigset
)
1969 * Disable signals. This is to avoid that the child intervenes
1970 * before it is properly setup for tracing. It is safer to
1971 * disable all signals, because then we know we are not breaking
1972 * anything by restoring the original mask.
1977 /* Fixup lttng-ust TLS. */
1978 lttng_ust_fixup_tls();
1980 if (URCU_TLS(lttng_ust_nest_count
))
1982 /* Disable signals */
1983 sigfillset(&all_sigs
);
1984 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
1986 PERROR("sigprocmask");
1989 pthread_mutex_lock(&ust_fork_mutex
);
1992 rcu_bp_before_fork();
1995 static void ust_after_fork_common(sigset_t
*restore_sigset
)
1999 DBG("process %d", getpid());
2002 pthread_mutex_unlock(&ust_fork_mutex
);
2004 /* Restore signals */
2005 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2007 PERROR("sigprocmask");
2011 void ust_after_fork_parent(sigset_t
*restore_sigset
)
2013 if (URCU_TLS(lttng_ust_nest_count
))
2015 DBG("process %d", getpid());
2016 rcu_bp_after_fork_parent();
2017 /* Release mutexes and reenable signals */
2018 ust_after_fork_common(restore_sigset
);
2022 * After fork, in the child, we need to cleanup all the leftover state,
2023 * except the worker thread which already magically disappeared thanks
2024 * to the weird Linux fork semantics. After tyding up, we call
2025 * lttng_ust_init() again to start over as a new PID.
2027 * This is meant for forks() that have tracing in the child between the
2028 * fork and following exec call (if there is any).
2030 void ust_after_fork_child(sigset_t
*restore_sigset
)
2032 if (URCU_TLS(lttng_ust_nest_count
))
2034 lttng_context_vpid_reset();
2035 lttng_context_vtid_reset();
2036 lttng_context_procname_reset();
2037 DBG("process %d", getpid());
2038 /* Release urcu mutexes */
2039 rcu_bp_after_fork_child();
2040 lttng_ust_cleanup(0);
2041 /* Release mutexes and reenable signals */
2042 ust_after_fork_common(restore_sigset
);
2046 void lttng_ust_sockinfo_session_enabled(void *owner
)
2048 struct sock_info
*sock_info
= owner
;
2049 sock_info
->statedump_pending
= 1;