DBG("Creating kernel session");
- ret = kernel_create_session(session, kernel_tracer_fd);
+ ret = kernel_create_session(session);
if (ret < 0) {
ret = LTTNG_ERR_KERN_SESS_FAIL;
goto error_create;
goto error;
}
+ /* Kernel tracer check */
+ if (!kernel_tracer_is_initialized()) {
+ /* Basically, load kernel tracer modules */
+ ret = init_kernel_tracer();
+ if (ret != 0) {
+ goto error;
+ }
+ }
+
/* Consumer is in an ERROR state. Report back to client */
if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
ret = LTTNG_ERR_NO_KERNCONSUMERD;
/*
* Start a kernel session by opening all necessary streams.
*/
-static int start_kernel_session(struct ltt_kernel_session *ksess, int wpipe)
+static int start_kernel_session(struct ltt_kernel_session *ksess)
{
int ret;
struct ltt_kernel_channel *kchan;
}
/* Quiescent wait after starting trace */
- kernel_wait_quiescent(wpipe);
+ kernel_wait_quiescent();
ksess->active = 1;
goto error;
}
- kernel_wait_quiescent(kernel_tracer_fd);
+ kernel_wait_quiescent();
break;
}
case LTTNG_DOMAIN_UST:
goto error;
}
- kernel_wait_quiescent(kernel_tracer_fd);
+ kernel_wait_quiescent();
break;
}
case LTTNG_DOMAIN_UST:
goto error;
}
- kernel_wait_quiescent(kernel_tracer_fd);
+ kernel_wait_quiescent();
break;
}
case LTTNG_DOMAIN_UST:
switch (domain->type) {
case LTTNG_DOMAIN_KERNEL:
{
- if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd) != 1) {
+ if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
/* Sampling position of buffer is not supported */
WARN("Kernel tracer does not support buffer monitoring. "
"Setting the monitor interval timer to 0 "
goto error;
}
- kernel_wait_quiescent(kernel_tracer_fd);
+ kernel_wait_quiescent();
break;
}
case LTTNG_DOMAIN_UST:
goto error_unlock;
}
- kernel_wait_quiescent(kernel_tracer_fd);
+ kernel_wait_quiescent();
break;
}
case LTTNG_DOMAIN_UST:
goto error;
}
- kernel_wait_quiescent(kernel_tracer_fd);
+ kernel_wait_quiescent();
break;
}
case LTTNG_DOMAIN_UST:
switch (domain) {
case LTTNG_DOMAIN_KERNEL:
- nb_events = kernel_list_events(kernel_tracer_fd, events);
+ nb_events = kernel_list_events(events);
if (nb_events < 0) {
ret = LTTNG_ERR_KERN_LIST_FAIL;
goto error;
/* Kernel tracing */
if (ksession != NULL) {
DBG("Start kernel tracing session %s", session->name);
- ret = start_kernel_session(ksession, kernel_tracer_fd);
+ ret = start_kernel_session(ksession);
if (ret != LTTNG_OK) {
goto error;
}
goto error;
}
- kernel_wait_quiescent(kernel_tracer_fd);
+ kernel_wait_quiescent();
/* Flush metadata after stopping (if exists) */
if (ksession->metadata_stream_fd >= 0) {
struct lttng_ht *agent_apps_ht_by_sock = NULL;
-int kernel_tracer_fd = -1;
struct lttng_kernel_tracer_version kernel_tracer_version;
struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version;
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
+#include <sys/types.h>
#include <common/common.h>
#include <common/trace-chunk.h>
#include <common/kernel-ctl/kernel-ioctl.h>
#include <common/sessiond-comm/sessiond-comm.h>
+#include "lttng-sessiond.h"
+#include "lttng-syscall.h"
#include "consumer.h"
#include "kernel.h"
#include "kernel-consumer.h"
#include "kern-modules.h"
#include "utils.h"
#include "rotate.h"
+#include "modprobe.h"
/*
* Key used to reference a channel between the sessiond and the consumer. This
*/
static uint64_t next_kernel_channel_key;
+static const char *module_proc_lttng = "/proc/lttng";
+
+static int kernel_tracer_fd = -1;
+
#include <lttng/userspace-probe.h>
#include <lttng/userspace-probe-internal.h>
/*
* Create a new kernel session, register it to the kernel tracer and add it to
* the session daemon session.
*/
-int kernel_create_session(struct ltt_session *session, int tracer_fd)
+int kernel_create_session(struct ltt_session *session)
{
int ret;
struct ltt_kernel_session *lks;
}
/* Kernel tracer session creation */
- ret = kernctl_create_session(tracer_fd);
+ ret = kernctl_create_session(kernel_tracer_fd);
if (ret < 0) {
PERROR("ioctl kernel create session");
goto error;
/*
* Make a kernel wait to make sure in-flight probe have completed.
*/
-void kernel_wait_quiescent(int fd)
+void kernel_wait_quiescent(void)
{
int ret;
+ int fd = kernel_tracer_fd;
DBG("Kernel quiescent wait on %d", fd);
/*
* Get the event list from the kernel tracer and return the number of elements.
*/
-ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
+ssize_t kernel_list_events(struct lttng_event **events)
{
int fd, ret;
char *event;
assert(events);
- fd = kernctl_tracepoint_list(tracer_fd);
+ fd = kernctl_tracepoint_list(kernel_tracer_fd);
if (fd < 0) {
PERROR("kernel tracepoint list");
goto error;
/*
* Get kernel version and validate it.
*/
-int kernel_validate_version(int tracer_fd,
- struct lttng_kernel_tracer_version *version,
+int kernel_validate_version(struct lttng_kernel_tracer_version *version,
struct lttng_kernel_tracer_abi_version *abi_version)
{
int ret;
- ret = kernctl_tracer_version(tracer_fd, version);
+ ret = kernctl_tracer_version(kernel_tracer_fd, version);
if (ret < 0) {
ERR("Failed to retrieve the lttng-modules version");
goto error;
version->major, VERSION_MAJOR);
goto error_version;
}
- ret = kernctl_tracer_abi_version(tracer_fd, abi_version);
+ ret = kernctl_tracer_abi_version(kernel_tracer_fd, abi_version);
if (ret < 0) {
ERR("Failed to retrieve lttng-modules ABI version");
goto error;
* Return 1 on success, 0 when feature is not supported, negative value in case
* of errors.
*/
-int kernel_supports_ring_buffer_snapshot_sample_positions(int tracer_fd)
+int kernel_supports_ring_buffer_snapshot_sample_positions(void)
{
int ret = 0; // Not supported by default
struct lttng_kernel_tracer_abi_version abi;
- ret = kernctl_tracer_abi_version(tracer_fd, &abi);
+ ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
if (ret < 0) {
ERR("Failed to retrieve lttng-modules ABI version");
goto error;
rcu_read_unlock();
return ret;
}
+
+/*
+ * Setup necessary data for kernel tracer action.
+ */
+LTTNG_HIDDEN
+int init_kernel_tracer(void)
+{
+ int ret;
+ bool is_root = !getuid();
+
+ /* Modprobe lttng kernel modules */
+ ret = modprobe_lttng_control();
+ if (ret < 0) {
+ goto error;
+ }
+
+ /* Open debugfs lttng */
+ kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
+ if (kernel_tracer_fd < 0) {
+ DBG("Failed to open %s", module_proc_lttng);
+ goto error_open;
+ }
+
+ /* Validate kernel version */
+ ret = kernel_validate_version(&kernel_tracer_version,
+ &kernel_tracer_abi_version);
+ if (ret < 0) {
+ goto error_version;
+ }
+
+ ret = modprobe_lttng_data();
+ if (ret < 0) {
+ goto error_modules;
+ }
+
+ ret = kernel_supports_ring_buffer_snapshot_sample_positions();
+ if (ret < 0) {
+ goto error_modules;
+ }
+
+ if (ret < 1) {
+ WARN("Kernel tracer does not support buffer monitoring. "
+ "The monitoring timer of channels in the kernel domain "
+ "will be set to 0 (disabled).");
+ }
+
+ DBG("Kernel tracer fd %d", kernel_tracer_fd);
+
+ ret = syscall_init_table(kernel_tracer_fd);
+ if (ret < 0) {
+ ERR("Unable to populate syscall table. Syscall tracing won't "
+ "work for this session daemon.");
+ }
+ return 0;
+
+error_version:
+ modprobe_remove_lttng_control();
+ ret = close(kernel_tracer_fd);
+ if (ret) {
+ PERROR("close");
+ }
+ kernel_tracer_fd = -1;
+ return LTTNG_ERR_KERN_VERSION;
+
+error_modules:
+ ret = close(kernel_tracer_fd);
+ if (ret) {
+ PERROR("close");
+ }
+
+error_open:
+ modprobe_remove_lttng_control();
+
+error:
+ WARN("No kernel tracer available");
+ kernel_tracer_fd = -1;
+ if (!is_root) {
+ return LTTNG_ERR_NEED_ROOT_SESSIOND;
+ } else {
+ return LTTNG_ERR_KERN_NA;
+ }
+}
+
+LTTNG_HIDDEN
+void cleanup_kernel_tracer(void)
+{
+ int ret;
+
+ DBG2("Closing kernel fd");
+ if (kernel_tracer_fd >= 0) {
+ ret = close(kernel_tracer_fd);
+ if (ret) {
+ PERROR("close");
+ }
+ kernel_tracer_fd = -1;
+ }
+ DBG("Unloading kernel modules");
+ modprobe_remove_lttng_all();
+ free(syscall_table);
+}
+
+LTTNG_HIDDEN
+bool kernel_tracer_is_initialized(void)
+{
+ return kernel_tracer_fd >= 0;
+}
int kernel_add_channel_context(struct ltt_kernel_channel *chan,
struct ltt_kernel_context *ctx);
-int kernel_create_session(struct ltt_session *session, int tracer_fd);
+int kernel_create_session(struct ltt_session *session);
int kernel_create_channel(struct ltt_kernel_session *session,
struct lttng_channel *chan);
int kernel_create_event(struct lttng_event *ev, struct ltt_kernel_channel *channel,
int kernel_metadata_flush_buffer(int fd);
int kernel_start_session(struct ltt_kernel_session *session);
int kernel_stop_session(struct ltt_kernel_session *session);
-ssize_t kernel_list_events(int tracer_fd, struct lttng_event **event_list);
-void kernel_wait_quiescent(int fd);
-int kernel_validate_version(int tracer_fd,
- struct lttng_kernel_tracer_version *kernel_tracer_version,
+ssize_t kernel_list_events(struct lttng_event **event_list);
+void kernel_wait_quiescent(void);
+int kernel_validate_version(struct lttng_kernel_tracer_version *kernel_tracer_version,
struct lttng_kernel_tracer_abi_version *kernel_tracer_abi_version);
void kernel_destroy_session(struct ltt_kernel_session *ksess);
void kernel_free_session(struct ltt_kernel_session *ksess);
int init_kernel_workarounds(void);
ssize_t kernel_list_tracker_pids(struct ltt_kernel_session *session,
int **_pids);
-int kernel_supports_ring_buffer_snapshot_sample_positions(int tracer_fd);
+int kernel_supports_ring_buffer_snapshot_sample_positions(void);
+int init_kernel_tracer(void);
+void cleanup_kernel_tracer(void);
+bool kernel_tracer_is_initialized(void);
enum lttng_error_code kernel_create_channel_subdirectories(
const struct ltt_kernel_session *ksess);
default_global_apps_pipe[];
/* Set in main.c at boot time of the daemon */
-extern int kernel_tracer_fd;
extern struct lttng_kernel_tracer_version kernel_tracer_version;
extern struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version;
* Return 0 on success and the syscall table is allocated. On error, a negative
* value is returned.
*/
-int syscall_init_table(void)
+int syscall_init_table(int tracer_fd)
{
int ret, fd, err;
size_t nbmem;
DBG3("Syscall init system call table");
- fd = kernctl_syscall_list(kernel_tracer_fd);
+ fd = kernctl_syscall_list(tracer_fd);
if (fd < 0) {
ret = fd;
PERROR("kernelctl syscall list");
extern struct syscall *syscall_table;
/* Use to list kernel system calls. */
-int syscall_init_table(void);
+int syscall_init_table(int tracer_fd);
ssize_t syscall_table_list(struct lttng_event **events);
#endif /* LTTNG_SYSCALL_H */
#include "event.h"
#include "kernel.h"
#include "kernel-consumer.h"
-#include "modprobe.h"
#include "shm.h"
#include "ust-ctl.h"
#include "ust-consumer.h"
#include "notification-thread.h"
#include "notification-thread-commands.h"
#include "rotation-thread.h"
-#include "lttng-syscall.h"
#include "agent.h"
#include "ht-cleanup.h"
#include "sessiond-config.h"
*/
static struct ust_cmd_queue ust_cmd_queue;
-static const char *module_proc_lttng = "/proc/lttng";
-
/*
* Section name to look for in the daemon configuration file.
*/
wait_consumer(&ustconsumer32_data);
if (is_root && !config.no_kernel) {
- DBG2("Closing kernel fd");
- if (kernel_tracer_fd >= 0) {
- ret = close(kernel_tracer_fd);
- if (ret) {
- PERROR("close");
- }
- }
- DBG("Unloading kernel modules");
- modprobe_remove_lttng_all();
- free(syscall_table);
+ cleanup_kernel_tracer();
}
/*
run_as_destroy_worker();
}
-/*
- * Setup necessary data for kernel tracer action.
- */
-static int init_kernel_tracer(void)
-{
- int ret;
-
- /* Modprobe lttng kernel modules */
- ret = modprobe_lttng_control();
- if (ret < 0) {
- goto error;
- }
-
- /* Open debugfs lttng */
- kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
- if (kernel_tracer_fd < 0) {
- DBG("Failed to open %s", module_proc_lttng);
- goto error_open;
- }
-
- /* Validate kernel version */
- ret = kernel_validate_version(kernel_tracer_fd, &kernel_tracer_version,
- &kernel_tracer_abi_version);
- if (ret < 0) {
- goto error_version;
- }
-
- ret = modprobe_lttng_data();
- if (ret < 0) {
- goto error_modules;
- }
-
- ret = kernel_supports_ring_buffer_snapshot_sample_positions(
- kernel_tracer_fd);
- if (ret < 0) {
- goto error_modules;
- }
-
- if (ret < 1) {
- WARN("Kernel tracer does not support buffer monitoring. "
- "The monitoring timer of channels in the kernel domain "
- "will be set to 0 (disabled).");
- }
-
- DBG("Kernel tracer fd %d", kernel_tracer_fd);
- return 0;
-
-error_version:
- modprobe_remove_lttng_control();
- ret = close(kernel_tracer_fd);
- if (ret) {
- PERROR("close");
- }
- kernel_tracer_fd = -1;
- return LTTNG_ERR_KERN_VERSION;
-
-error_modules:
- ret = close(kernel_tracer_fd);
- if (ret) {
- PERROR("close");
- }
-
-error_open:
- modprobe_remove_lttng_control();
-
-error:
- WARN("No kernel tracer available");
- kernel_tracer_fd = -1;
- if (!is_root) {
- return LTTNG_ERR_NEED_ROOT_SESSIOND;
- } else {
- return LTTNG_ERR_KERN_NA;
- }
-}
-
static int string_match(const char *str1, const char *str2)
{
return (str1 && str2) && !strcmp(str1, str2);
/* Setup kernel tracer */
if (!config.no_kernel) {
init_kernel_tracer();
- if (kernel_tracer_fd >= 0) {
- ret = syscall_init_table();
- if (ret < 0) {
- ERR("Unable to populate syscall table. "
- "Syscall tracing won't work "
- "for this session daemon.");
- }
- }
}
/* Set ulimit for open files */
* buffers. Therefore, we reject triggers that require that
* mechanism to be available to be evaluated.
*/
- ret = kernel_supports_ring_buffer_snapshot_sample_positions(
- kernel_tracer_fd);
+ ret = kernel_supports_ring_buffer_snapshot_sample_positions();
break;
}
default: