From 8ba26eaefd4104c2951d4a29bec7ddd2059bbeff Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Fri, 26 Mar 2010 15:36:40 -0400 Subject: [PATCH] liblttd cleanup and error handling fixes Signed-off-by: Mathieu Desnoyers --- liblttd/Makefile.am | 4 +- liblttd/liblttd.c | 150 +++++++++++++++------------ liblttd/liblttd.h | 157 +++++++++++++++------------- liblttd/liblttdutils.c | 229 ----------------------------------------- liblttd/liblttdutils.h | 38 ------- lttd/lttd.c | 17 +-- 6 files changed, 180 insertions(+), 415 deletions(-) delete mode 100644 liblttd/liblttdutils.c delete mode 100644 liblttd/liblttdutils.h diff --git a/liblttd/Makefile.am b/liblttd/Makefile.am index 43859be..1224923 100644 --- a/liblttd/Makefile.am +++ b/liblttd/Makefile.am @@ -1,7 +1,7 @@ lib_LTLIBRARIES = liblttd.la -liblttd_la_SOURCES = liblttd.c liblttdutils.c +liblttd_la_SOURCES = liblttd.c liblttdvfs.c liblttdinclude_HEADERS = \ - liblttd.h liblttdutils.h + liblttd.h liblttdvfs.h diff --git a/liblttd/liblttd.c b/liblttd/liblttd.c index 42226bc..91f3a27 100644 --- a/liblttd/liblttd.c +++ b/liblttd/liblttd.c @@ -1,4 +1,4 @@ -/* libttd +/* liblttd * * Linux Trace Toolkit Daemon * @@ -12,6 +12,20 @@ * Copyright 2010 - * Michael Sills-Lavoie * Oumarou Dicko + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #ifdef HAVE_CONFIG_H @@ -96,14 +110,14 @@ int open_buffer_file(struct liblttd_instance *instance, char *filename, int open_ret = 0; int ret = 0; - if(strncmp(filename, "flight-", sizeof("flight-")-1) != 0) { - if(instance->dump_flight_only) { + if (strncmp(filename, "flight-", sizeof("flight-")-1) != 0) { + if (instance->dump_flight_only) { printf_verbose("Skipping normal channel %s\n", path_channel); return 0; } } else { - if(instance->dump_normal_only) { + if (instance->dump_normal_only) { printf_verbose("Skipping flight channel %s\n", path_channel); return 0; @@ -117,17 +131,17 @@ int open_buffer_file(struct liblttd_instance *instance, char *filename, /* Open the channel in read mode */ instance->fd_pairs.pair[instance->fd_pairs.num_pairs-1].channel = open(path_channel, O_RDONLY | O_NONBLOCK); - if(instance->fd_pairs.pair[instance->fd_pairs.num_pairs-1].channel == -1) { + if (instance->fd_pairs.pair[instance->fd_pairs.num_pairs-1].channel == -1) { perror(path_channel); instance->fd_pairs.num_pairs--; return 0; /* continue */ } - if(instance->callbacks->on_open_channel) ret = instance->callbacks->on_open_channel( + if (instance->callbacks->on_open_channel) ret = instance->callbacks->on_open_channel( instance->callbacks, &instance->fd_pairs.pair[instance->fd_pairs.num_pairs-1], base_path_channel); - if(ret != 0) { + if (ret != 0) { open_ret = -1; close(instance->fd_pairs.pair[instance->fd_pairs.num_pairs-1].channel); instance->fd_pairs.num_pairs--; @@ -152,17 +166,17 @@ int open_channel_trace_pairs(struct liblttd_instance *instance, int open_ret = 0; - if(channel_dir == NULL) { + if (channel_dir == NULL) { perror(subchannel_name); open_ret = ENOENT; goto end; } printf_verbose("Calling : on new channels folder\n"); - if(instance->callbacks->on_new_channels_folder) ret = instance->callbacks-> + if (instance->callbacks->on_new_channels_folder) ret = instance->callbacks-> on_new_channels_folder(instance->callbacks, base_subchannel_name); - if(ret == -1) { + if (ret == -1) { open_ret = -1; goto end; } @@ -192,27 +206,27 @@ int open_channel_trace_pairs(struct liblttd_instance *instance, while((entry = readdir(channel_dir)) != NULL) { - if(entry->d_name[0] == '.') continue; + if (entry->d_name[0] == '.') continue; strncpy(path_channel_ptr, entry->d_name, PATH_MAX - path_channel_len); ret = stat(path_channel, &stat_buf); - if(ret == -1) { + if (ret == -1) { perror(path_channel); continue; } printf_verbose("Channel file : %s\n", path_channel); - if(S_ISDIR(stat_buf.st_mode)) { + if (S_ISDIR(stat_buf.st_mode)) { printf_verbose("Entering channel subdirectory...\n"); ret = open_channel_trace_pairs(instance, path_channel, base_subchannel_ptr); - if(ret < 0) continue; - } else if(S_ISREG(stat_buf.st_mode)) { + if (ret < 0) continue; + } else if (S_ISREG(stat_buf.st_mode)) { open_ret = open_buffer_file(instance, entry->d_name, path_channel, base_subchannel_ptr); - if(open_ret) + if (open_ret) goto end; } } @@ -231,33 +245,33 @@ int read_subbuffer(struct liblttd_instance *instance, struct fd_pair *pair) long ret; off_t offset; - err = ioctl(pair->channel, RELAY_GET_SB, &consumed_old); printf_verbose("cookie : %u\n", consumed_old); - if(err != 0) { + if (err != 0) { ret = errno; perror("Reserving sub buffer failed (everything is normal, it is due to concurrency)"); goto get_error; } err = ioctl(pair->channel, RELAY_GET_SB_SIZE, &len); - if(err != 0) { + if (err != 0) { ret = errno; perror("Getting sub-buffer len failed."); goto get_error; } - if(instance->callbacks->on_read_subbuffer) ret = instance->callbacks->on_read_subbuffer( - instance->callbacks, pair, len); + if (instance->callbacks->on_read_subbuffer) + ret = instance->callbacks->on_read_subbuffer( + instance->callbacks, pair, len); write_error: ret = 0; err = ioctl(pair->channel, RELAY_PUT_SB, &consumed_old); - if(err != 0) { + if (err != 0) { ret = errno; - if(errno == EFAULT) { + if (errno == EFAULT) { perror("Error in unreserving sub buffer\n"); - } else if(errno == EIO) { + } else if (errno == EIO) { /* Should never happen with newer LTTng versions */ perror("Reader has been pushed by the writer, last sub-buffer corrupted."); } @@ -274,7 +288,7 @@ int map_channels(struct liblttd_instance *instance, int idx_begin, int idx_end) int i,j; int ret=0; - if(instance->fd_pairs.num_pairs <= 0) { + if (instance->fd_pairs.num_pairs <= 0) { printf("No channel to read\n"); goto end; } @@ -285,18 +299,18 @@ int map_channels(struct liblttd_instance *instance, int idx_begin, int idx_end) struct fd_pair *pair = &instance->fd_pairs.pair[i]; ret = ioctl(pair->channel, RELAY_GET_N_SB, &pair->n_sb); - if(ret != 0) { + if (ret != 0) { perror("Error in getting the number of sub-buffers"); goto end; } ret = ioctl(pair->channel, RELAY_GET_MAX_SB_SIZE, &pair->max_sb_size); - if(ret != 0) { + if (ret != 0) { perror("Error in getting the max sub-buffer size"); goto end; } ret = pthread_mutex_init(&pair->mutex, NULL); /* Fast mutex */ - if(ret != 0) { + if (ret != 0) { perror("Error in mutex init"); goto end; } @@ -317,7 +331,7 @@ int unmap_channels(struct liblttd_instance *instance) int err_ret; err_ret = pthread_mutex_destroy(&pair->mutex); - if(err_ret != 0) { + if (err_ret != 0) { perror("Error in mutex destroy"); } ret |= err_ret; @@ -344,9 +358,9 @@ int read_inotify(struct liblttd_instance *instance) offset = 0; len = read(instance->inotify_fd, buf, sizeof(struct inotify_event) + PATH_MAX); - if(len < 0) { + if (len < 0) { - if(errno == EAGAIN) + if (errno == EAGAIN) return 0; /* another thread got the data before us */ printf("Error in read from inotify FD %s.\n", strerror(len)); @@ -355,7 +369,7 @@ int read_inotify(struct liblttd_instance *instance) while(offset < len) { ievent = (struct inotify_event *)&(buf[offset]); for(i=0; iinotify_watch_array.num; i++) { - if(instance->inotify_watch_array.elem[i].wd == ievent->wd && + if (instance->inotify_watch_array.elem[i].wd == ievent->wd && ievent->mask == IN_CREATE) { printf_verbose( "inotify wd %u event mask : %u for %s%s\n", @@ -365,13 +379,13 @@ int read_inotify(struct liblttd_instance *instance) old_num = instance->fd_pairs.num_pairs; strcpy(path_channel, instance->inotify_watch_array.elem[i].path_channel); strcat(path_channel, ievent->name); - if(ret = open_buffer_file(instance, ievent->name, path_channel, + if (ret = open_buffer_file(instance, ievent->name, path_channel, path_channel + (instance->inotify_watch_array.elem[i].base_path_channel - instance->inotify_watch_array.elem[i].path_channel))) { printf("Error opening buffer file\n"); return -1; } - if(ret = map_channels(instance, old_num, instance->fd_pairs.num_pairs)) { + if (ret = map_channels(instance, old_num, instance->fd_pairs.num_pairs)) { printf("Error mapping channel\n"); return -1; } @@ -383,7 +397,8 @@ int read_inotify(struct liblttd_instance *instance) } #endif //HAS_INOTIFY -/* read_channels +/* + * read_channels * * Thread worker. * @@ -448,11 +463,11 @@ int read_channels(struct liblttd_instance *instance, unsigned long thread_num) #endif //DEBUG /* Have we received a signal ? */ - if(instance->quit_program) break; + if (instance->quit_program) break; num_rdy = poll(pollfd, num_pollfd, -1); - if(num_rdy == -1) { + if (num_rdy == -1) { perror("Poll error"); goto free_fd; } @@ -511,7 +526,7 @@ int read_channels(struct liblttd_instance *instance, unsigned long thread_num) break; case POLLPRI: pthread_rwlock_rdlock(&instance->fd_pairs_lock); - if(pthread_mutex_trylock(&instance->fd_pairs.pair[i-inotify_fds].mutex) == 0) { + if (pthread_mutex_trylock(&instance->fd_pairs.pair[i-inotify_fds].mutex) == 0) { printf_verbose( "Urgent read on fd %d\n", pollfd[i].fd); @@ -519,10 +534,10 @@ int read_channels(struct liblttd_instance *instance, unsigned long thread_num) high_prio = 1; /* it's ok to have an unavailable sub-buffer */ ret = read_subbuffer(instance, &instance->fd_pairs.pair[i-inotify_fds]); - if(ret == EAGAIN) ret = 0; + if (ret == EAGAIN) ret = 0; ret = pthread_mutex_unlock(&instance->fd_pairs.pair[i-inotify_fds].mutex); - if(ret) + if (ret) printf("Error in mutex unlock : %s\n", strerror(ret)); } pthread_rwlock_unlock(&instance->fd_pairs_lock); @@ -530,24 +545,24 @@ int read_channels(struct liblttd_instance *instance, unsigned long thread_num) } } /* If every buffer FD has hung up, we end the read loop here */ - if(num_hup == num_pollfd - inotify_fds) break; + if (num_hup == num_pollfd - inotify_fds) break; - if(!high_prio) { + if (!high_prio) { for(i=inotify_fds;ifd_pairs_lock); - if(pthread_mutex_trylock(&instance->fd_pairs.pair[i-inotify_fds].mutex) == 0) { + if (pthread_mutex_trylock(&instance->fd_pairs.pair[i-inotify_fds].mutex) == 0) { /* Take care of low priority channels. */ printf_verbose( "Normal read on fd %d\n", pollfd[i].fd); /* it's ok to have an unavailable subbuffer */ ret = read_subbuffer(instance, &instance->fd_pairs.pair[i-inotify_fds]); - if(ret == EAGAIN) ret = 0; + if (ret == EAGAIN) ret = 0; ret = pthread_mutex_unlock(&instance->fd_pairs.pair[i-inotify_fds].mutex); - if(ret) + if (ret) printf("Error in mutex unlock : %s\n", strerror(ret)); } pthread_rwlock_unlock(&instance->fd_pairs_lock); @@ -558,7 +573,7 @@ int read_channels(struct liblttd_instance *instance, unsigned long thread_num) /* Update pollfd array if an entry was added to fd_pairs */ pthread_rwlock_rdlock(&instance->fd_pairs_lock); - if((inotify_fds + instance->fd_pairs.num_pairs) != num_pollfd) { + if ((inotify_fds + instance->fd_pairs.num_pairs) != num_pollfd) { pollfd = realloc(pollfd, (inotify_fds + instance->fd_pairs.num_pairs) * sizeof(struct pollfd)); for(i=num_pollfd-inotify_fds;ifd_pairs.num_pairs;i++) { @@ -595,11 +610,11 @@ void close_channel_trace_pairs(struct liblttd_instance *instance) for(i=0;ifd_pairs.num_pairs;i++) { ret = close(instance->fd_pairs.pair[i].channel); - if(ret == -1) perror("Close error on channel"); - if(instance->callbacks->on_close_channel) { + if (ret == -1) perror("Close error on channel"); + if (instance->callbacks->on_close_channel) { ret = instance->callbacks->on_close_channel( instance->callbacks, &instance->fd_pairs.pair[i]); - if(ret != 0) perror("Error on close channel callback"); + if (ret != 0) perror("Error on close channel callback"); } } free(instance->fd_pairs.pair); @@ -612,7 +627,7 @@ void * thread_main(void *arg) long ret = 0; struct liblttd_thread_data *thread_data = (struct liblttd_thread_data*) arg; - if(thread_data->instance->callbacks->on_new_thread) + if (thread_data->instance->callbacks->on_new_thread) ret = thread_data->instance->callbacks->on_new_thread( thread_data->instance->callbacks, thread_data->thread_num); @@ -621,7 +636,7 @@ void * thread_main(void *arg) } ret = read_channels(thread_data->instance, thread_data->thread_num); - if(thread_data->instance->callbacks->on_close_thread) + if (thread_data->instance->callbacks->on_close_thread) thread_data->instance->callbacks->on_close_thread( thread_data->instance->callbacks, thread_data->thread_num); @@ -637,7 +652,7 @@ int channels_init(struct liblttd_instance *instance) instance->inotify_fd = inotify_init(); fcntl(instance->inotify_fd, F_SETFL, O_NONBLOCK); - if(ret = open_channel_trace_pairs(instance, instance->channel_name, + if (ret = open_channel_trace_pairs(instance, instance->channel_name, instance->channel_name + strlen(instance->channel_name))) goto close_channel; @@ -647,13 +662,13 @@ int channels_init(struct liblttd_instance *instance) goto close_channel; } - if(ret = map_channels(instance, 0, instance->fd_pairs.num_pairs)) + if (ret = map_channels(instance, 0, instance->fd_pairs.num_pairs)) goto close_channel; return 0; close_channel: close_channel_trace_pairs(instance); - if(instance->inotify_fd >= 0) + if (instance->inotify_fd >= 0) close(instance->inotify_fd); return ret; } @@ -672,10 +687,10 @@ int liblttd_start_instance(struct liblttd_instance *instance) unsigned long i; void *tret; - if(!instance) + if (!instance) return -EINVAL; - if(ret = channels_init(instance)) + if (ret = channels_init(instance)) return ret; tids = malloc(sizeof(pthread_t) * instance->num_threads); @@ -686,7 +701,7 @@ int liblttd_start_instance(struct liblttd_instance *instance) thread_data->instance = instance; ret = pthread_create(&tids[i], NULL, thread_main, thread_data); - if(ret) { + if (ret) { perror("Error creating thread"); break; } @@ -694,11 +709,11 @@ int liblttd_start_instance(struct liblttd_instance *instance) for(i=0; inum_threads; i++) { ret = pthread_join(tids[i], &tret); - if(ret) { + if (ret) { perror("Error joining thread"); break; } - if((long)tret != 0) { + if ((long)tret != 0) { printf("Error %s occured in thread %ld\n", strerror((long)tret), i); } @@ -707,10 +722,10 @@ int liblttd_start_instance(struct liblttd_instance *instance) free(tids); ret = unmap_channels(instance); close_channel_trace_pairs(instance); - if(instance->inotify_fd >= 0) + if (instance->inotify_fd >= 0) close(instance->inotify_fd); - if(instance->callbacks->on_trace_end) + if (instance->callbacks->on_trace_end) instance->callbacks->on_trace_end(instance); delete_instance(instance); @@ -723,12 +738,17 @@ struct liblttd_instance * liblttd_new_instance( unsigned long n_threads, int flight_only, int normal_only, int verbose) { struct liblttd_instance * instance; - if(!channel_path || !callbacks) return NULL; - if(n_threads == 0) n_threads = 1; - if(flight_only && normal_only) return NULL; + + if (!channel_path || !callbacks) + return NULL; + if (n_threads == 0) + n_threads = 1; + if (flight_only && normal_only) + return NULL; instance = malloc(sizeof(struct liblttd_instance)); - if(!instance) return NULL; + if (!instance) + return NULL; instance->callbacks = callbacks; diff --git a/liblttd/liblttd.h b/liblttd/liblttd.h index d871dd7..0b6b058 100644 --- a/liblttd/liblttd.h +++ b/liblttd/liblttd.h @@ -1,21 +1,25 @@ -/* liblttd header file +/* + * liblttd header file * - * Copyright 2005 - + * Copyright 2005-2010 - * Mathieu Desnoyers * Copyright 2010- * Oumarou Dicko * Michael Sills-Lavoie * - * 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 - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. * - * This program is distributed in the hope that it will be useful, + * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef _LIBLTTD_H @@ -94,71 +98,77 @@ struct liblttd_instance { struct liblttd_callbacks { /** * on_open_channel - Is called after a channel file is open. - * @data: pointeur to the callbacks struct that has been passed to the - * lib. + * + * @data: pointer to the callbacks structure that has been passed to + * the lib. * @pair: structure that contains the data associated with the - * channel file descriptor. The lib user can use user_data to - * store the data associated to the specified channel. - * @relative_channel_path: represents a relative path to the channel - * file. This path is relative to the root folder of the trace channels. + * channel file descriptor. The library user can use user_data to + * store the data associated to the specified channel. + * @relative_channel_path: + * represents a relative path to the channel file. This path is + * relative to the root folder of the trace channels. * * Returns 0 if the callback succeeds else not 0. */ - int(*on_open_channel)(struct liblttd_callbacks *data, - struct fd_pair *pair, char *relative_channel_path); + int (*on_open_channel)(struct liblttd_callbacks *data, + struct fd_pair *pair, + char *relative_channel_path); /** * on_close_channel - Is called after a channel file is closed. - * @data: pointeur to the callbacks struct that has been passed to the - * lib. + * + * @data: pointer to the callbacks structure that has been passed to the + * lib. * @pair: structure that contains the data associated with the channel - * file descriptor. The lib user should clean user_data at this time. + * file descriptor. The lib user should clean user_data at this + * time. * * Returns 0 if the callback succeeds else not 0. * * After a channel file has been closed, it will never be read again. */ - int(*on_close_channel)(struct liblttd_callbacks *data, - struct fd_pair *pair); + int (*on_close_channel)(struct liblttd_callbacks *data, + struct fd_pair *pair); /** * on_new_channels_folder - Is called when the library enter in a new * subfolder while it is scanning the trace channel tree. It can be used * to create the output file structure of the trace. - * @data: pointeur to the callbacks struct that has been passed to the - * lib. - * @relative_folder_path: represents a relative path - * to the channel folder. This path is relative to the root - * folder of the trace channels. + * + * @data: pointer to the callbacks structure that has been passed to the + * library. + * @relative_folder_path: + * represents a relative path to the channel folder. This path is + * relative to the root folder of the trace channels. * * Returns 0 if the callback succeeds else not 0. */ - int(*on_new_channels_folder)(struct liblttd_callbacks *data, - char *relative_folder_path); + int (*on_new_channels_folder)(struct liblttd_callbacks *data, + char *relative_folder_path); /** * on_read_subbuffer - Is called after a subbuffer is a reserved. * - * @data: pointeur to the callbacks struct that has been passed to the - * lib. - * @pair: structure that contains the data associated with the - * channel file descriptor. - * @len: represents the length the data that has to be read. + * @data: pointer to the callbacks structure that has been passed to the + * library. + * @pair: structure that contains the data associated with the channel + * file descriptor. + * @len: represents the length the data that has to be read. * * Returns 0 if the callback succeeds else not 0. * - * It has to be thread safe, it'll be called by many threads. + * It has to be thread safe, because it is called by many threads. */ - int(*on_read_subbuffer)(struct liblttd_callbacks *data, - struct fd_pair *pair, unsigned int len); + int (*on_read_subbuffer)(struct liblttd_callbacks *data, + struct fd_pair *pair, unsigned int len); /** * on_trace_en - Is called at the very end of the tracing session. At * this time, all the channels have been closed and the threads have * been destroyed. * - * @instance: pointeur to the instance struct that has been passed to - * the lib. + * @instance: pointer to the instance structure that has been passed to + * the library. * * Returns 0 if the callback succeeds else not 0. * @@ -166,35 +176,35 @@ struct liblttd_callbacks { * again and the tracing instance will be deleted automatically by * liblttd. After this call, the user must not use the liblttd instance. */ - int(*on_trace_end)(struct liblttd_instance *instance); + int (*on_trace_end)(struct liblttd_instance *instance); /** * on_new_thread - Is called after a new thread has been created. * - * @data: pointeur to the callbacks struct that has been passed to the - * lib. + * @data: pointer to the callbacks structure that has been passed to the + * lib. * @thread_num: represents the id of the thread. * * Returns 0 if the callback succeeds else not 0. * - * It has to be thread safe, it'll be called by many threads. + * It has to be thread safe, because it is called by many threads. */ - int(*on_new_thread)(struct liblttd_callbacks *data, - unsigned long thread_num); + int (*on_new_thread)(struct liblttd_callbacks *data, + unsigned long thread_num); /** - * on_close_thread - Is Called just before a thread is destroyed. + * on_close_thread - Is called just before a thread is destroyed. * - * @data: pointeur to the callbacks struct that has been passed to the - * lib. + * @data: pointer to the callbacks structure that has been passed to the + * library. * @thread_num: represents the number of the thread. * * Returns 0 if the callback succeeds else not 0. * - * It has to be thread safe, it'll be called by many threads. + * It has to be thread safe, because it is called by many threads. */ - int(*on_close_thread)(struct liblttd_callbacks *data, - unsigned long thread_num); + int (*on_close_thread)(struct liblttd_callbacks *data, + unsigned long thread_num); /** * The library's data. @@ -205,34 +215,36 @@ struct liblttd_callbacks { /** * liblttd_new_instance - Is called to create a new tracing session. * - * @callbacks: Pointer to a callbacks struct that contain the user callbacks and - * data. + * @callbacks: Pointer to a callbacks structure that contain the user + * callbacks and data. * @channel_path: This argument is a path to the root folder of the trace's - * channels. - * @n_threads: This argument represents the number of threads that will be - * used by the library. - * @flight_only: If this argument to set to 1, only the channel that are in - * flight recorder mode will be recorded. - * @normal_only: If this argument to set to 1, only the channel that are in - * normal mode will be recorded. - * @verbose: If this argument to set to 1, more informations will be printed. + * channels. + * @n_threads: This argument represents the number of threads that will be + * used by the library. + * @flight_only: If this argument to set to 1, only the channel that are in + * flight recorder mode will be recorded. + * @normal_only: If this argument to set to 1, only the channel that are in + * normal mode will be recorded. + * @verbose: If this argument to set to 1, more informations will be + * printed. * * Returns the instance if the function succeeds else NULL. */ -struct liblttd_instance * liblttd_new_instance( - struct liblttd_callbacks *callbacks, char *channel_path, - unsigned long n_threads, int flight_only, int normal_only, int verbose); +struct liblttd_instance * +liblttd_new_instance(struct liblttd_callbacks *callbacks, char *channel_path, + unsigned long n_threads, int flight_only, int normal_only, + int verbose); /** * liblttd_start - Is called to start a new tracing session. * - * @instance: The tracing session instance that needs to be starded. + * @instance: The tracing session instance that needs to be started. * * Returns 0 if the function succeeds. * - * This is a blocking function. The caller will be bloked on it until the - * tracing session is stoped by the user usign liblttd_stop_instance or until - * the trace is stoped by LTTng directly. + * This is a blocking function. The caller will be blocked on it until the + * tracing session is stopped by the user using liblttd_stop_instance or until + * the trace is stopped by LTTng directly. */ int liblttd_start_instance(struct liblttd_instance *instance); @@ -243,12 +255,11 @@ int liblttd_start_instance(struct liblttd_instance *instance); * * Returns 0 if the function succeeds. * - * This function return immediately, it only tells liblttd to stop the instance. - * The on_trace_end callback will be called when the tracing session will really - * be stoped (after every thread will be done). The instance is deleted - * automatically by liblttd after on_trace_end is called. + * This function returns immediately, it only tells liblttd to stop the + * instance. The on_trace_end callback will be called when the tracing session + * will really be stopped (after every thread has finished using it). The + * instance is deleted automatically by liblttd after on_trace_end is called. */ int liblttd_stop_instance(struct liblttd_instance *instance); #endif /*_LIBLTTD_H */ - diff --git a/liblttd/liblttdutils.c b/liblttd/liblttdutils.c deleted file mode 100644 index 3368aba..0000000 --- a/liblttd/liblttdutils.c +++ /dev/null @@ -1,229 +0,0 @@ -/* liblttdutils - * - * Linux Trace Toolkit utility library - * - * This is a simple daemon implementation that reads a few relay+debugfs - * channels and save them in a trace. - * - * CPU hot-plugging is supported using inotify. - * - * Copyright 2005 - - * Mathieu Desnoyers - * Copyright 2010 - - * Michael Sills-Lavoie - * Oumarou Dicko - */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#define _REENTRANT -#define _GNU_SOURCE - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "liblttdutils.h" - -struct liblttdutils_channel_data { - int trace; -}; - -struct liblttdutils_data { - char path_trace[PATH_MAX]; - char *end_path_trace; - int path_trace_len; - int append_mode; - int verbose_mode; -}; - -static __thread int thread_pipe[2]; - -#define printf_verbose(fmt, args...) \ - do { \ - if (callbacks_data->verbose_mode) \ - printf(fmt, ##args); \ - } while (0) - -int liblttdutils_local_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 liblttdutils_channel_data *channel_data; - - pair->user_data = malloc(sizeof(struct liblttdutils_channel_data)); - channel_data = pair->user_data; - - struct liblttdutils_data* callbacks_data = data->user_data; - - strncpy(callbacks_data->end_path_trace, relative_channel_path, PATH_MAX - callbacks_data->path_trace_len); - printf_verbose("Creating trace file %s\n", callbacks_data->path_trace); - - ret = stat(callbacks_data->path_trace, &stat_buf); - if(ret == 0) { - if(callbacks_data->append_mode) { - printf_verbose("Appending to file %s as requested\n", - callbacks_data->path_trace); - - channel_data->trace = open(callbacks_data->path_trace, O_WRONLY, S_IRWXU|S_IRWXG|S_IRWXO); - if(channel_data->trace == -1) { - perror(callbacks_data->path_trace); - open_ret = -1; - goto end; - } - ret = lseek(channel_data->trace, 0, SEEK_END); - if (ret < 0) { - perror(callbacks_data->path_trace); - open_ret = -1; - close(channel_data->trace); - goto end; - } - } else { - printf("File %s exists, cannot open. Try append mode.\n", callbacks_data->path_trace); - open_ret = -1; - goto end; - } - } else { - if(errno == ENOENT) { - channel_data->trace = - open(callbacks_data->path_trace, O_WRONLY|O_CREAT|O_EXCL, S_IRWXU|S_IRWXG|S_IRWXO); - if(channel_data->trace == -1) { - perror(callbacks_data->path_trace); - open_ret = -1; - goto end; - } - } - } - -end: - return open_ret; - -} - -int liblttdutils_local_on_close_channel(struct liblttd_callbacks *data, struct fd_pair *pair) -{ - int ret; - ret = close(((struct liblttdutils_channel_data *)(pair->user_data))->trace); - free(pair->user_data); - return ret; -} - -int liblttdutils_local_on_new_channels_folder(struct liblttd_callbacks *data, char *relative_folder_path) -{ - int ret; - int open_ret = 0; - struct liblttdutils_data* callbacks_data = data->user_data; - - strncpy(callbacks_data->end_path_trace, relative_folder_path, PATH_MAX - callbacks_data->path_trace_len); - printf_verbose("Creating trace subdirectory %s\n", callbacks_data->path_trace); - - ret = mkdir(callbacks_data->path_trace, S_IRWXU|S_IRWXG|S_IRWXO); - if(ret == -1) { - if(errno != EEXIST) { - perror(callbacks_data->path_trace); - open_ret = -1; - goto end; - } - } - -end: - return open_ret; -} - -int liblttdutils_local_on_read_subbuffer(struct liblttd_callbacks *data, struct fd_pair *pair, unsigned int len) -{ - long ret; - off_t offset = 0; - - struct liblttdutils_data* callbacks_data = data->user_data; - - 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 liblttdutils_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 liblttdutils_local_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 liblttdutils_local_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; -} - -int liblttdutils_local_on_trace_end(struct liblttd_instance *instance) -{ - struct liblttd_callbacks *callbacks = instance->callbacks; - struct liblttdutils_data *data = callbacks->user_data; - - free(data); - free(callbacks); -} - -struct liblttd_callbacks* liblttdutils_local_new_callbacks(char* trace_name, - int append_mode, int verbose_mode) -{ - struct liblttdutils_data *data; - struct liblttd_callbacks *callbacks; - - if(!trace_name) return NULL; - - data = malloc(sizeof(struct liblttdutils_data)); - - strncpy(data->path_trace, trace_name, PATH_MAX-1); - data->path_trace_len = strlen(data->path_trace); - data->end_path_trace = data->path_trace + data->path_trace_len; - data->append_mode = append_mode; - data->verbose_mode = verbose_mode; - - callbacks = malloc(sizeof(struct liblttd_callbacks)); - - callbacks->on_open_channel = liblttdutils_local_on_open_channel; - callbacks->on_close_channel = liblttdutils_local_on_close_channel; - callbacks->on_new_channels_folder = liblttdutils_local_on_new_channels_folder; - callbacks->on_read_subbuffer = liblttdutils_local_on_read_subbuffer; - callbacks->on_trace_end = liblttdutils_local_on_trace_end; - callbacks->on_new_thread = liblttdutils_local_on_new_thread; - callbacks->on_close_thread = liblttdutils_local_on_close_thread; - callbacks->user_data = data; - - return callbacks; -} - diff --git a/liblttd/liblttdutils.h b/liblttd/liblttdutils.h deleted file mode 100644 index 78439d2..0000000 --- a/liblttd/liblttdutils.h +++ /dev/null @@ -1,38 +0,0 @@ -/* liblttdutils header file - * - * Copyright 2010- - * Oumarou Dicko - * Michael Sills-Lavoie - * - * 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 - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - */ - -#ifndef _LIBLTTDUTILS_H -#define _LIBLTTDUTILS_H - -#include "liblttd.h" - -/** - * liblttdutils_new_callbacks - Is a utility function called to create a new - * callbacks struct used by liblttd to write trace data to the disk. - * - * @trace_name: Directory name of the trace to write to. It will be created. - * @append_mode: Append to a possibly existing trace. - * @verbose_mode: Verbose mode. - * - * Returns the callbacks if the function succeeds else NULL. - */ -struct liblttd_callbacks* liblttdutils_local_new_callbacks(char* trace_name, - int append_mode, int verbose_mode); - -#endif /*_LIBLTTDUTILS_H */ - diff --git a/lttd/lttd.c b/lttd/lttd.c index b43b637..9422a1f 100644 --- a/lttd/lttd.c +++ b/lttd/lttd.c @@ -1,9 +1,10 @@ -/* 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. * @@ -24,7 +25,6 @@ * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * */ #ifdef HAVE_CONFIG_H @@ -40,7 +40,7 @@ #include #include -#include +#include struct liblttd_instance* instance; @@ -215,11 +215,12 @@ int main(int argc, char ** argv) } } - struct liblttd_callbacks* callbacks = liblttdutils_local_new_callbacks( - trace_name, append_mode, verbose_mode); + 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); + dump_flight_only, dump_normal_only, + verbose_mode); if(!instance) { perror("An error occured while creating the liblttd instance"); -- 2.34.1