X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=ltt-sessiond%2Fmain.c;h=8c09339aba2c80d79f60fdb88afbf4481b3ee1e5;hp=825fa4371cb67b0316fcf9ce8b6e677dd1f0f115;hb=4c93ba8b8e253ffa3943f4999bd912b535c40b88;hpb=44d3bd014f6ad217cff7e7c3dfaad76b1927c37b diff --git a/ltt-sessiond/main.c b/ltt-sessiond/main.c index 825fa4371..8c09339ab 100644 --- a/ltt-sessiond/main.c +++ b/ltt-sessiond/main.c @@ -50,7 +50,7 @@ #include "kernel-ctl.h" #include "ltt-sessiond.h" #include "shm.h" -#include "traceable-app.h" +#include "ust-app.h" #include "ust-ctl.h" #include "utils.h" #include "ust-ctl.h" @@ -344,7 +344,7 @@ static void cleanup(void) } DBG("Closing all UST sockets"); - clean_traceable_apps_list(); + ust_app_clean_list(); pthread_mutex_destroy(&kconsumerd_pid_mutex); @@ -701,6 +701,12 @@ static void *thread_manage_kernel(void *data) while (1) { if (update_poll_flag == 1) { + /* + * Reset number of fd in the poll set. Always 2 since there is the thread + * quit pipe and the kernel pipe. + */ + events.nb_fd = 2; + ret = update_kernel_poll(&events); if (ret < 0) { goto error; @@ -836,6 +842,8 @@ static void *thread_manage_kconsumerd(void *data) goto error; } + DBG2("Receiving code from kconsumerd_err_sock"); + /* Getting status code from kconsumerd */ ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code)); @@ -855,7 +863,7 @@ static void *thread_manage_kconsumerd(void *data) sem_post(&kconsumerd_sem); DBG("Kconsumerd command socket ready"); } else { - DBG("Kconsumerd error when waiting for SOCK_READY : %s", + ERR("Kconsumerd error when waiting for SOCK_READY : %s", lttcomm_get_readable_code(-code)); goto error; } @@ -986,7 +994,7 @@ static void *thread_manage_apps(void *data) } /* Register applicaton to the session daemon */ - ret = register_traceable_app(&ust_cmd.reg_msg, + ret = ust_app_register(&ust_cmd.reg_msg, ust_cmd.sock); if (ret < 0) { /* Only critical ENOMEM error can be returned here */ @@ -999,7 +1007,7 @@ static void *thread_manage_apps(void *data) * If the registration is not possible, we simply * unregister the apps and continue */ - unregister_traceable_app(ust_cmd.sock); + ust_app_unregister(ust_cmd.sock); } else { /* * We just need here to monitor the close of the UST @@ -1028,7 +1036,7 @@ static void *thread_manage_apps(void *data) } /* Socket closed */ - unregister_traceable_app(pollfd); + ust_app_unregister(pollfd); break; } } @@ -1257,29 +1265,66 @@ error: static int spawn_kconsumerd_thread(void) { int ret; + struct timespec timeout; + + timeout.tv_sec = DEFAULT_SEM_WAIT_TIMEOUT; + timeout.tv_nsec = 0; /* Setup semaphore */ - sem_init(&kconsumerd_sem, 0, 0); + ret = sem_init(&kconsumerd_sem, 0, 0); + if (ret < 0) { + PERROR("sem_init kconsumerd_sem"); + goto error; + } ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL); if (ret != 0) { - perror("pthread_create kconsumerd"); + PERROR("pthread_create kconsumerd"); + ret = -1; goto error; } - /* Wait for the kconsumerd thread to be ready */ - sem_wait(&kconsumerd_sem); + /* Get time for sem_timedwait absolute timeout */ + ret = clock_gettime(CLOCK_REALTIME, &timeout); + if (ret < 0) { + PERROR("clock_gettime spawn kconsumerd"); + /* Infinite wait for the kconsumerd thread to be ready */ + ret = sem_wait(&kconsumerd_sem); + } else { + /* Normal timeout if the gettime was successful */ + timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT; + ret = sem_timedwait(&kconsumerd_sem, &timeout); + } + + if (ret < 0) { + if (errno == ETIMEDOUT) { + /* + * Call has timed out so we kill the kconsumerd_thread and return + * an error. + */ + ERR("The kconsumerd thread was never ready. Killing it"); + ret = pthread_cancel(kconsumerd_thread); + if (ret < 0) { + PERROR("pthread_cancel kconsumerd_thread"); + } + } else { + PERROR("semaphore wait failed kconsumerd thread"); + } + goto error; + } + pthread_mutex_lock(&kconsumerd_pid_mutex); if (kconsumerd_pid == 0) { ERR("Kconsumerd did not start"); + pthread_mutex_unlock(&kconsumerd_pid_mutex); goto error; } + pthread_mutex_unlock(&kconsumerd_pid_mutex); return 0; error: - ret = LTTCOMM_KERN_CONSUMER_FAIL; return ret; } @@ -1361,18 +1406,16 @@ static int start_kconsumerd(void) ret = spawn_kconsumerd(); if (ret < 0) { ERR("Spawning kconsumerd failed"); - ret = LTTCOMM_KERN_CONSUMER_FAIL; pthread_mutex_unlock(&kconsumerd_pid_mutex); goto error; } /* Setting up the global kconsumerd_pid */ kconsumerd_pid = ret; + DBG2("Kconsumerd pid %d", kconsumerd_pid); pthread_mutex_unlock(&kconsumerd_pid_mutex); - DBG("Kconsumerd pid %d", ret); - - DBG("Spawning kconsumerd thread"); + DBG2("Spawning kconsumerd thread"); ret = spawn_kconsumerd_thread(); if (ret < 0) { ERR("Fatal error spawning kconsumerd thread"); @@ -1432,12 +1475,13 @@ static int mount_debugfs(char *path) ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid()); if (ret < 0) { + PERROR("Cannot create debugfs path"); goto error; } ret = mount(type, path, type, 0, NULL); if (ret < 0) { - perror("mount debugfs"); + PERROR("Cannot mount debugfs"); goto error; } @@ -1487,6 +1531,7 @@ static void init_kernel_tracer(void) } ret = mount_debugfs(debugfs_path); if (ret < 0) { + perror("Cannot mount debugfs"); goto error; } } @@ -1565,18 +1610,19 @@ static int create_ust_session(struct ltt_session *session, struct lttng_domain *domain) { int ret; - struct ltt_ust_session *lus; - struct ltt_traceable_app *app; + struct ltt_ust_session *lus = NULL; + struct ust_app *app; switch (domain->type) { case LTTNG_DOMAIN_UST_PID: - app = traceable_app_get_by_pid(domain->attr.pid); + app = ust_app_get_by_pid(domain->attr.pid); if (app == NULL) { ret = LTTCOMM_APP_NOT_FOUND; goto error; } break; default: + ret = LTTCOMM_UNKNOWN_DOMAIN; goto error; } @@ -1739,6 +1785,9 @@ static void list_lttng_events(struct ltt_kernel_channel *kchan, case LTTNG_KERNEL_SYSCALL: events[i].type = LTTNG_EVENT_SYSCALL; break; + case LTTNG_KERNEL_ALL: + assert(0); + break; } i++; } @@ -1775,6 +1824,42 @@ error: return ret; } +/* + * Copy channel from attributes and set it in the application channel list. + */ +static int copy_ust_channel_to_app(struct ltt_ust_session *usess, + struct lttng_channel *attr, struct ust_app *app) +{ + int ret; + struct ltt_ust_channel *uchan, *new_chan; + + uchan = trace_ust_get_channel_by_name(attr->name, usess); + if (uchan == NULL) { + ret = LTTCOMM_FATAL; + goto error; + } + + new_chan = trace_ust_create_channel(attr, usess->path); + if (new_chan == NULL) { + PERROR("malloc ltt_ust_channel"); + ret = LTTCOMM_FATAL; + goto error; + } + + ret = channel_ust_copy(new_chan, uchan); + if (ret < 0) { + ret = LTTCOMM_FATAL; + goto error; + } + + /* Add channel to the ust app channel list */ + cds_list_add(&new_chan->list, &app->channels.head); + app->channels.count++; + +error: + return ret; +} + /* * Command LTTNG_ENABLE_CHANNEL processed by the client thread. */ @@ -1806,10 +1891,10 @@ static int cmd_enable_channel(struct ltt_session *session, } case LTTNG_DOMAIN_UST_PID: { - struct ltt_ust_event *uevent, *new_uevent; + int sock; + struct ltt_ust_channel *uchan; struct ltt_ust_session *usess; - struct ltt_ust_channel *uchan, *app_chan; - struct ltt_traceable_app *app; + struct ust_app *app; usess = trace_ust_get_session_by_pid(&session->ust_session_list, domain->attr.pid); @@ -1818,59 +1903,29 @@ static int cmd_enable_channel(struct ltt_session *session, goto error; } - app = traceable_app_get_by_pid(domain->attr.pid); + app = ust_app_get_by_pid(domain->attr.pid); if (app == NULL) { ret = LTTCOMM_APP_NOT_FOUND; goto error; } + sock = app->sock; uchan = trace_ust_get_channel_by_name(attr->name, usess); if (uchan == NULL) { - ret = channel_ust_create(usess, attr, app->sock); + ret = channel_ust_create(usess, attr, sock); } else { - ret = channel_ust_enable(usess, uchan, app->sock); + ret = channel_ust_enable(usess, uchan, sock); } if (ret != LTTCOMM_OK) { goto error; } - /*TODO: This should be put in an external function */ - - /* Copy UST channel to add to the traceable app */ - uchan = trace_ust_get_channel_by_name(attr->name, usess); - if (uchan == NULL) { - ret = LTTCOMM_FATAL; - goto error; - } - - app_chan = trace_ust_create_channel(attr, session->path); - if (app_chan == NULL) { - PERROR("malloc ltt_ust_channel"); - ret = LTTCOMM_FATAL; + ret = copy_ust_channel_to_app(usess, attr, app); + if (ret != LTTCOMM_OK) { goto error; } - memcpy(app_chan, uchan, sizeof(struct ltt_ust_channel)); - CDS_INIT_LIST_HEAD(&app_chan->events.head); - - cds_list_for_each_entry(uevent, &uchan->events.head, list) { - new_uevent = malloc(sizeof(struct ltt_ust_event)); - if (new_uevent == NULL) { - PERROR("malloc ltt_ust_event"); - ret = LTTCOMM_FATAL; - goto error; - } - - memcpy(new_uevent, uevent, sizeof(struct ltt_ust_event)); - cds_list_add(&new_uevent->list, &app_chan->events.head); - app_chan->events.count++; - } - - /* Add channel to traceable_app */ - cds_list_add(&app_chan->list, &app->channels.head); - app->channels.count++; - DBG("UST channel %s created for app sock %d with pid %d", attr->name, app->sock, domain->attr.pid); break; @@ -1904,7 +1959,7 @@ static int cmd_disable_event(struct ltt_session *session, int domain, goto error; } - ret = event_kernel_disable(session->kernel_session, kchan, event_name); + ret = event_kernel_disable_tracepoint(session->kernel_session, kchan, event_name); if (ret != LTTCOMM_OK) { goto error; } @@ -2021,7 +2076,7 @@ static int cmd_enable_event(struct ltt_session *session, int domain, goto error; } - ret = event_kernel_enable(session->kernel_session, kchan, event); + ret = event_kernel_enable_tracepoint(session->kernel_session, kchan, event); if (ret != LTTCOMM_OK) { goto error; } @@ -2071,18 +2126,28 @@ static int cmd_enable_event_all(struct ltt_session *session, int domain, goto error; } - if (event_type == LTTNG_KERNEL_SYSCALL) { - ret = event_kernel_enable_syscalls(session->kernel_session, + switch (event_type) { + case LTTNG_KERNEL_SYSCALL: + ret = event_kernel_enable_all_syscalls(session->kernel_session, kchan, kernel_tracer_fd); - } else { + break; + case LTTNG_KERNEL_TRACEPOINT: /* - * This call enables all LTTNG_KERNEL_TRACEPOINTS and events - * already registered to the channel. + * This call enables all LTTNG_KERNEL_TRACEPOINTS and + * events already registered to the channel. */ + ret = event_kernel_enable_all_tracepoints(session->kernel_session, + kchan, kernel_tracer_fd); + break; + case LTTNG_KERNEL_ALL: + /* Enable syscalls and tracepoints */ ret = event_kernel_enable_all(session->kernel_session, kchan, kernel_tracer_fd); + break; + default: + ret = LTTCOMM_KERN_ENABLE_FAIL; + goto error; } - if (ret != LTTCOMM_OK) { goto error; } @@ -2285,7 +2350,7 @@ static int cmd_destroy_session(struct ltt_session *session, char *name) perror("write kernel poll pipe"); } - ret = session_destroy(name); + ret = session_destroy(session); return ret; } @@ -2497,7 +2562,9 @@ static int process_client_msg(struct command_ctx *cmd_ctx) break; default: DBG("Getting session %s by name", cmd_ctx->lsm->session.name); + session_lock_list(); cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name); + session_unlock_list(); if (cmd_ctx->session == NULL) { if (cmd_ctx->lsm->session.name != NULL) { ret = LTTCOMM_SESS_NOT_FOUND; @@ -2539,14 +2606,17 @@ static int process_client_msg(struct command_ctx *cmd_ctx) } /* Start the kernel consumer daemon */ + pthread_mutex_lock(&kconsumerd_pid_mutex); if (kconsumerd_pid == 0 && cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { + pthread_mutex_unlock(&kconsumerd_pid_mutex); ret = start_kconsumerd(); if (ret < 0) { ret = LTTCOMM_KERN_CONSUMER_FAIL; goto error; } } + pthread_mutex_unlock(&kconsumerd_pid_mutex); } break; case LTTNG_DOMAIN_UST_PID: