Commit | Line | Data |
---|---|---|
e4ef128f MD |
1 | /* |
2 | * liblttdvfs | |
3 | * | |
4 | * Linux Trace Toolkit library - Write trace to the virtual file system | |
5 | * | |
6 | * This is a simple daemonized library that reads a few LTTng debugfs channels | |
7 | * and save them in a trace. | |
8 | * | |
9 | * CPU hot-plugging is supported using inotify. | |
10 | * | |
11 | * Copyright 2005-2010 - | |
12 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
13 | * Copyright 2010 - | |
14 | * Michael Sills-Lavoie <michael.sills-lavoie@polymtl.ca> | |
15 | * Oumarou Dicko <oumarou.dicko@polymtl.ca> | |
16 | * | |
17 | * This library is free software; you can redistribute it and/or | |
18 | * modify it under the terms of the GNU Lesser General Public | |
19 | * License as published by the Free Software Foundation; either | |
20 | * version 2.1 of the License, or (at your option) any later version. | |
21 | * | |
22 | * This library is distributed in the hope that it will be useful, | |
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
25 | * Lesser General Public License for more details. | |
26 | * | |
27 | * You should have received a copy of the GNU Lesser General Public | |
28 | * License along with this library; if not, write to the Free Software | |
29 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
30 | */ | |
31 | ||
32 | #ifdef HAVE_CONFIG_H | |
33 | #include <config.h> | |
34 | #endif | |
35 | ||
36 | #define _REENTRANT | |
37 | #define _GNU_SOURCE | |
38 | ||
39 | #include <stdio.h> | |
40 | #include <stdlib.h> | |
41 | #include <string.h> | |
42 | #include <errno.h> | |
43 | #include <fcntl.h> | |
44 | #include <dirent.h> | |
45 | #include <pthread.h> | |
46 | #include <sys/stat.h> | |
47 | ||
48 | #include "liblttdvfs.h" | |
49 | ||
50 | struct liblttdvfs_channel_data { | |
51 | int trace; | |
52 | }; | |
53 | ||
54 | struct liblttdvfs_data { | |
55 | char path_trace[PATH_MAX]; | |
56 | char *end_path_trace; | |
57 | int path_trace_len; | |
58 | int append_mode; | |
59 | int verbose_mode; | |
60 | }; | |
61 | ||
62 | static __thread int thread_pipe[2]; | |
63 | ||
64 | #define printf_verbose(fmt, args...) \ | |
65 | do { \ | |
66 | if (callbacks_data->verbose_mode) \ | |
67 | printf(fmt, ##args); \ | |
68 | } while (0) | |
69 | ||
70 | int liblttdvfs_on_open_channel(struct liblttd_callbacks *data, struct fd_pair *pair, char *relative_channel_path) | |
71 | { | |
72 | int open_ret = 0; | |
73 | int ret; | |
74 | struct stat stat_buf; | |
75 | struct liblttdvfs_channel_data *channel_data; | |
76 | ||
77 | pair->user_data = malloc(sizeof(struct liblttdvfs_channel_data)); | |
78 | channel_data = pair->user_data; | |
79 | ||
80 | struct liblttdvfs_data* callbacks_data = data->user_data; | |
81 | ||
82 | strncpy(callbacks_data->end_path_trace, relative_channel_path, PATH_MAX - callbacks_data->path_trace_len); | |
83 | printf_verbose("Creating trace file %s\n", callbacks_data->path_trace); | |
84 | ||
85 | ret = stat(callbacks_data->path_trace, &stat_buf); | |
86 | if (ret == 0) { | |
87 | if (callbacks_data->append_mode) { | |
88 | printf_verbose("Appending to file %s as requested\n", | |
89 | callbacks_data->path_trace); | |
90 | ||
91 | channel_data->trace = open(callbacks_data->path_trace, O_WRONLY, S_IRWXU|S_IRWXG|S_IRWXO); | |
92 | if (channel_data->trace == -1) { | |
93 | perror(callbacks_data->path_trace); | |
94 | open_ret = -1; | |
95 | goto end; | |
96 | } | |
97 | ret = lseek(channel_data->trace, 0, SEEK_END); | |
98 | if (ret < 0) { | |
99 | perror(callbacks_data->path_trace); | |
100 | open_ret = -1; | |
101 | close(channel_data->trace); | |
102 | goto end; | |
103 | } | |
104 | } else { | |
105 | printf("File %s exists, cannot open. Try append mode.\n", callbacks_data->path_trace); | |
106 | open_ret = -1; | |
107 | goto end; | |
108 | } | |
109 | } else { | |
110 | if (errno == ENOENT) { | |
111 | channel_data->trace = | |
112 | open(callbacks_data->path_trace, O_WRONLY|O_CREAT|O_EXCL, S_IRWXU|S_IRWXG|S_IRWXO); | |
113 | if (channel_data->trace == -1) { | |
114 | perror(callbacks_data->path_trace); | |
115 | open_ret = -1; | |
116 | goto end; | |
117 | } | |
118 | } | |
119 | } | |
120 | ||
121 | end: | |
122 | return open_ret; | |
123 | ||
124 | } | |
125 | ||
126 | int liblttdvfs_on_close_channel(struct liblttd_callbacks *data, struct fd_pair *pair) | |
127 | { | |
128 | int ret; | |
129 | ret = close(((struct liblttdvfs_channel_data *)(pair->user_data))->trace); | |
130 | free(pair->user_data); | |
131 | return ret; | |
132 | } | |
133 | ||
134 | int liblttdvfs_on_new_channels_folder(struct liblttd_callbacks *data, char *relative_folder_path) | |
135 | { | |
136 | int ret; | |
137 | int open_ret = 0; | |
138 | struct liblttdvfs_data* callbacks_data = data->user_data; | |
139 | ||
140 | strncpy(callbacks_data->end_path_trace, relative_folder_path, PATH_MAX - callbacks_data->path_trace_len); | |
141 | printf_verbose("Creating trace subdirectory %s\n", callbacks_data->path_trace); | |
142 | ||
143 | ret = mkdir(callbacks_data->path_trace, S_IRWXU|S_IRWXG|S_IRWXO); | |
144 | if (ret == -1) { | |
145 | if (errno != EEXIST) { | |
146 | perror(callbacks_data->path_trace); | |
147 | open_ret = -1; | |
148 | goto end; | |
149 | } | |
150 | } | |
151 | ||
152 | end: | |
153 | return open_ret; | |
154 | } | |
155 | ||
156 | int liblttdvfs_on_read_subbuffer(struct liblttd_callbacks *data, struct fd_pair *pair, unsigned int len) | |
157 | { | |
158 | long ret; | |
159 | off_t offset = 0; | |
160 | ||
161 | struct liblttdvfs_data* callbacks_data = data->user_data; | |
162 | ||
163 | while (len > 0) { | |
164 | printf_verbose("splice chan to pipe offset %lu\n", | |
165 | (unsigned long)offset); | |
166 | ret = splice(pair->channel, &offset, thread_pipe[1], NULL, | |
167 | len, SPLICE_F_MOVE | SPLICE_F_MORE); | |
168 | printf_verbose("splice chan to pipe ret %ld\n", ret); | |
169 | if (ret < 0) { | |
170 | perror("Error in relay splice"); | |
171 | goto write_error; | |
172 | } | |
173 | ret = splice(thread_pipe[0], NULL, | |
174 | ((struct liblttdvfs_channel_data *)(pair->user_data))->trace, | |
175 | NULL, ret, SPLICE_F_MOVE | SPLICE_F_MORE); | |
176 | printf_verbose("splice pipe to file %ld\n", ret); | |
177 | if (ret < 0) { | |
178 | perror("Error in file splice"); | |
179 | goto write_error; | |
180 | } | |
181 | len -= ret; | |
182 | } | |
183 | ||
184 | write_error: | |
185 | return ret; | |
186 | } | |
187 | ||
188 | int liblttdvfs_on_new_thread(struct liblttd_callbacks *data, unsigned long thread_num) | |
189 | { | |
190 | int ret; | |
191 | ret = pipe(thread_pipe); | |
192 | if (ret < 0) { | |
193 | perror("Error creating pipe"); | |
194 | return ret; | |
195 | } | |
196 | return 0; | |
197 | } | |
198 | ||
199 | int liblttdvfs_on_close_thread(struct liblttd_callbacks *data, unsigned long thread_num) | |
200 | { | |
201 | close(thread_pipe[0]); /* close read end */ | |
202 | close(thread_pipe[1]); /* close write end */ | |
203 | return 0; | |
204 | } | |
205 | ||
206 | int liblttdvfs_on_trace_end(struct liblttd_instance *instance) | |
207 | { | |
208 | struct liblttd_callbacks *callbacks = instance->callbacks; | |
209 | struct liblttdvfs_data *data = callbacks->user_data; | |
210 | ||
211 | free(data); | |
212 | free(callbacks); | |
213 | } | |
214 | ||
215 | struct liblttd_callbacks* liblttdvfs_new_callbacks(char* trace_name, | |
216 | int append_mode, int verbose_mode) | |
217 | { | |
218 | struct liblttdvfs_data *data; | |
219 | struct liblttd_callbacks *callbacks; | |
220 | ||
221 | if (!trace_name) | |
222 | goto error; | |
223 | ||
224 | data = malloc(sizeof(struct liblttdvfs_data)); | |
225 | if (!data) | |
226 | goto error; | |
227 | ||
228 | strncpy(data->path_trace, trace_name, PATH_MAX-1); | |
229 | data->path_trace_len = strlen(data->path_trace); | |
230 | data->end_path_trace = data->path_trace + data->path_trace_len; | |
231 | data->append_mode = append_mode; | |
232 | data->verbose_mode = verbose_mode; | |
233 | ||
234 | callbacks = malloc(sizeof(struct liblttd_callbacks)); | |
235 | if (!callbacks) | |
236 | goto alloc_cb_error; | |
237 | ||
238 | callbacks->on_open_channel = liblttdvfs_on_open_channel; | |
239 | callbacks->on_close_channel = liblttdvfs_on_close_channel; | |
240 | callbacks->on_new_channels_folder = liblttdvfs_on_new_channels_folder; | |
241 | callbacks->on_read_subbuffer = liblttdvfs_on_read_subbuffer; | |
242 | callbacks->on_trace_end = liblttdvfs_on_trace_end; | |
243 | callbacks->on_new_thread = liblttdvfs_on_new_thread; | |
244 | callbacks->on_close_thread = liblttdvfs_on_close_thread; | |
245 | callbacks->user_data = data; | |
246 | ||
247 | return callbacks; | |
248 | ||
249 | /* Error handling */ | |
250 | alloc_cb_error: | |
251 | free(data); | |
252 | error: | |
253 | return NULL; | |
254 | } |