4 * Linux Trace Toolkit library - Write trace to the virtual file system
6 * This is a simple daemonized library that reads a few LTTng debugfs channels
7 * and save them in a trace.
9 * CPU hot-plugging is supported using inotify.
11 * Copyright 2005-2010 -
12 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
14 * Michael Sills-Lavoie <michael.sills-lavoie@polymtl.ca>
15 * Oumarou Dicko <oumarou.dicko@polymtl.ca>
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.
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.
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
48 #include "liblttdvfs.h"
50 struct liblttdvfs_channel_data
{
54 struct liblttdvfs_data
{
55 char path_trace
[PATH_MAX
];
62 static __thread
int thread_pipe
[2];
64 #define printf_verbose(fmt, args...) \
66 if (callbacks_data->verbose_mode) \
67 printf(fmt, ##args); \
70 int liblttdvfs_on_open_channel(struct liblttd_callbacks
*data
, struct fd_pair
*pair
, char *relative_channel_path
)
75 struct liblttdvfs_channel_data
*channel_data
;
77 pair
->user_data
= malloc(sizeof(struct liblttdvfs_channel_data
));
78 channel_data
= pair
->user_data
;
80 struct liblttdvfs_data
* callbacks_data
= data
->user_data
;
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
);
85 ret
= stat(callbacks_data
->path_trace
, &stat_buf
);
87 if (callbacks_data
->append_mode
) {
88 printf_verbose("Appending to file %s as requested\n",
89 callbacks_data
->path_trace
);
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
);
97 ret
= lseek(channel_data
->trace
, 0, SEEK_END
);
99 perror(callbacks_data
->path_trace
);
101 close(channel_data
->trace
);
105 printf("File %s exists, cannot open. Try append mode.\n", callbacks_data
->path_trace
);
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
);
126 int liblttdvfs_on_close_channel(struct liblttd_callbacks
*data
, struct fd_pair
*pair
)
129 ret
= close(((struct liblttdvfs_channel_data
*)(pair
->user_data
))->trace
);
130 free(pair
->user_data
);
134 int liblttdvfs_on_new_channels_folder(struct liblttd_callbacks
*data
, char *relative_folder_path
)
138 struct liblttdvfs_data
* callbacks_data
= data
->user_data
;
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
);
143 ret
= mkdir(callbacks_data
->path_trace
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
145 if (errno
!= EEXIST
) {
146 perror(callbacks_data
->path_trace
);
156 int liblttdvfs_on_read_subbuffer(struct liblttd_callbacks
*data
, struct fd_pair
*pair
, unsigned int len
)
161 struct liblttdvfs_data
* callbacks_data
= data
->user_data
;
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
);
170 perror("Error in relay splice");
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
);
178 perror("Error in file splice");
188 int liblttdvfs_on_new_thread(struct liblttd_callbacks
*data
, unsigned long thread_num
)
191 ret
= pipe(thread_pipe
);
193 perror("Error creating pipe");
199 int liblttdvfs_on_close_thread(struct liblttd_callbacks
*data
, unsigned long thread_num
)
201 close(thread_pipe
[0]); /* close read end */
202 close(thread_pipe
[1]); /* close write end */
206 int liblttdvfs_on_trace_end(struct liblttd_instance
*instance
)
208 struct liblttd_callbacks
*callbacks
= instance
->callbacks
;
209 struct liblttdvfs_data
*data
= callbacks
->user_data
;
215 struct liblttd_callbacks
* liblttdvfs_new_callbacks(char* trace_name
,
216 int append_mode
, int verbose_mode
)
218 struct liblttdvfs_data
*data
;
219 struct liblttd_callbacks
*callbacks
;
224 data
= malloc(sizeof(struct liblttdvfs_data
));
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
;
234 callbacks
= malloc(sizeof(struct liblttd_callbacks
));
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
;