X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;f=lttd%2Flttd.c;h=9422a1f499cd37149156d56a1b50e6da5477a9d6;hb=432a1b04774579cefbed4126a66427cb730c5a0a;hp=ec757ae219180a09b89faf6302e12ac1faa4a7a1;hpb=008e25154ca2db688516bafcb134c984f08a5f10;p=ltt-control.git diff --git a/lttd/lttd.c b/lttd/lttd.c index ec757ae..9422a1f 100644 --- a/lttd/lttd.c +++ b/lttd/lttd.c @@ -1,13 +1,16 @@ -/* lttd +/* + * lttd * * Linux Trace Toolkit Daemon * - * This is a simple daemon that reads a few relay+debugfs channels and save - * them in a trace. + * This is a simple daemon that reads a few LTTng debugfs channels and saves + * them in a trace on the virtual file system. * * CPU hot-plugging is supported using inotify. * * Copyright 2009-2010 - Mathieu Desnoyers + * Copyright 2010 - Michael Sills-Lavoie + * Copyright 2010 - Oumarou Dicko * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,22 +36,14 @@ #include #include -#include #include #include -#include -#include -#include #include +#include -struct lttd_channel_data { - int trace; -}; +struct liblttd_instance* instance; -static char path_trace[PATH_MAX]; -static char *end_path_trace; -static int path_trace_len = 0; static char *trace_name = NULL; static char *channel_name = NULL; static int daemon_mode = 0; @@ -58,13 +53,6 @@ static int dump_flight_only = 0; static int dump_normal_only = 0; static int verbose_mode = 0; -static __thread int thread_pipe[2]; - -#define printf_verbose(fmt, args...) \ - do { \ - if (verbose_mode) \ - printf(fmt, ##args); \ - } while (0) /* Args : * @@ -191,135 +179,7 @@ void show_info(void) static void handler(int signo) { printf("Signal %d received : exiting cleanly\n", signo); - liblttd_stop(); -} - -int lttd_on_open_channel(struct liblttd_callbacks *data, struct fd_pair *pair, char *relative_channel_path) -{ - int open_ret = 0; - int ret; - struct stat stat_buf; - struct lttd_channel_data *channel_data; - - pair->user_data = malloc(sizeof(struct lttd_channel_data)); - channel_data = pair->user_data; - - strncpy(end_path_trace, relative_channel_path, PATH_MAX - path_trace_len); - printf_verbose("Creating trace file %s\n", path_trace); - - ret = stat(path_trace, &stat_buf); - if(ret == 0) { - if(append_mode) { - printf_verbose("Appending to file %s as requested\n", - path_trace); - - channel_data->trace = open(path_trace, O_WRONLY, S_IRWXU|S_IRWXG|S_IRWXO); - if(channel_data->trace == -1) { - perror(path_trace); - open_ret = -1; - goto end; - } - ret = lseek(channel_data->trace, 0, SEEK_END); - if (ret < 0) { - perror(path_trace); - open_ret = -1; - close(channel_data->trace); - goto end; - } - } else { - printf("File %s exists, cannot open. Try append mode.\n", path_trace); - open_ret = -1; - goto end; - } - } else { - if(errno == ENOENT) { - channel_data->trace = open(path_trace, O_WRONLY|O_CREAT|O_EXCL, S_IRWXU|S_IRWXG|S_IRWXO); - if(channel_data->trace == -1) { - perror(path_trace); - open_ret = -1; - goto end; - } - } - } - -end: - return open_ret; - -} - -int lttd_on_close_channel(struct liblttd_callbacks *data, struct fd_pair *pair) -{ - int ret; - ret = close(((struct lttd_channel_data *)(pair->user_data))->trace); - free(pair->user_data); - return ret; -} - -int lttd_on_new_channels_folder(struct liblttd_callbacks *data, char *relative_folder_path) -{ - int ret; - int open_ret = 0; - - strncpy(end_path_trace, relative_folder_path, PATH_MAX - path_trace_len); - printf_verbose("Creating trace subdirectory %s\n", path_trace); - - ret = mkdir(path_trace, S_IRWXU|S_IRWXG|S_IRWXO); - if(ret == -1) { - if(errno != EEXIST) { - perror(path_trace); - open_ret = -1; - goto end; - } - } - -end: - return open_ret; -} - -int lttd_on_read_subbuffer(struct liblttd_callbacks *data, struct fd_pair *pair, unsigned int len) -{ - long ret; - off_t offset = 0; - - while (len > 0) { - printf_verbose("splice chan to pipe offset %lu\n", - (unsigned long)offset); - ret = splice(pair->channel, &offset, thread_pipe[1], NULL, - len, SPLICE_F_MOVE | SPLICE_F_MORE); - printf_verbose("splice chan to pipe ret %ld\n", ret); - if (ret < 0) { - perror("Error in relay splice"); - goto write_error; - } - ret = splice(thread_pipe[0], NULL, - ((struct lttd_channel_data *)(pair->user_data))->trace, - NULL, ret, SPLICE_F_MOVE | SPLICE_F_MORE); - printf_verbose("splice pipe to file %ld\n", ret); - if (ret < 0) { - perror("Error in file splice"); - goto write_error; - } - len -= ret; - } - -write_error: - return ret; -} - -int on_new_thread(struct liblttd_callbacks *data, unsigned long thread_num) { - int ret; - ret = pipe(thread_pipe); - if (ret < 0) { - perror("Error creating pipe"); - return ret; - } - return 0; -} - -int on_close_thread(struct liblttd_callbacks *data, unsigned long thread_num) { - close(thread_pipe[0]); /* close read end */ - close(thread_pipe[1]); /* close write end */ - return 0; + liblttd_stop_instance(instance); } int main(int argc, char ** argv) @@ -327,17 +187,6 @@ int main(int argc, char ** argv) int ret = 0; struct sigaction act; - struct liblttd_callbacks callbacks = { - lttd_on_open_channel, - lttd_on_close_channel, - lttd_on_new_channels_folder, - lttd_on_read_subbuffer, - NULL, - on_new_thread, - on_close_thread, - NULL - }; - ret = parse_arguments(argc, argv); if(ret != 0) show_arguments(); @@ -366,12 +215,19 @@ int main(int argc, char ** argv) } } - strncpy(path_trace, trace_name, PATH_MAX-1); - path_trace_len = strlen(path_trace); - end_path_trace = path_trace + path_trace_len; + struct liblttd_callbacks* callbacks = + liblttdvfs_new_callbacks(trace_name, append_mode, verbose_mode); + + instance = liblttd_new_instance(callbacks, channel_name, num_threads, + dump_flight_only, dump_normal_only, + verbose_mode); + + if(!instance) { + perror("An error occured while creating the liblttd instance"); + return ret; + } - liblttd_start(channel_name, num_threads, dump_flight_only, dump_normal_only, - verbose_mode, &callbacks); + liblttd_start_instance(instance); return ret; }