| 1 | /* lttd |
| 2 | * |
| 3 | * Linux Trace Toolkit Daemon |
| 4 | * |
| 5 | * This is a simple daemon that reads a few relay+debugfs channels and save |
| 6 | * them in a trace. |
| 7 | * |
| 8 | * CPU hot-plugging is supported using inotify. |
| 9 | * |
| 10 | * Copyright 2009-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along |
| 23 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 24 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 25 | */ |
| 26 | |
| 27 | #ifdef HAVE_CONFIG_H |
| 28 | #include <config.h> |
| 29 | #endif |
| 30 | |
| 31 | #define _REENTRANT |
| 32 | #define _GNU_SOURCE |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | #include <signal.h> |
| 38 | #include <errno.h> |
| 39 | #include <fcntl.h> |
| 40 | #include <dirent.h> |
| 41 | #include <sys/stat.h> |
| 42 | |
| 43 | #include <liblttd/liblttd.h> |
| 44 | |
| 45 | struct lttd_channel_data { |
| 46 | int trace; |
| 47 | }; |
| 48 | |
| 49 | static char path_trace[PATH_MAX]; |
| 50 | static char *end_path_trace; |
| 51 | static int path_trace_len = 0; |
| 52 | static char *trace_name = NULL; |
| 53 | static char *channel_name = NULL; |
| 54 | static int daemon_mode = 0; |
| 55 | static int append_mode = 0; |
| 56 | static unsigned long num_threads = 1; |
| 57 | static int dump_flight_only = 0; |
| 58 | static int dump_normal_only = 0; |
| 59 | static int verbose_mode = 0; |
| 60 | |
| 61 | static __thread int thread_pipe[2]; |
| 62 | |
| 63 | #define printf_verbose(fmt, args...) \ |
| 64 | do { \ |
| 65 | if (verbose_mode) \ |
| 66 | printf(fmt, ##args); \ |
| 67 | } while (0) |
| 68 | |
| 69 | /* Args : |
| 70 | * |
| 71 | * -t directory Directory name of the trace to write to. Will be created. |
| 72 | * -c directory Root directory of the debugfs trace channels. |
| 73 | * -d Run in background (daemon). |
| 74 | * -a Trace append mode. |
| 75 | * -s Send SIGUSR1 to parent when ready for IO. |
| 76 | */ |
| 77 | void show_arguments(void) |
| 78 | { |
| 79 | printf("Please use the following arguments :\n"); |
| 80 | printf("\n"); |
| 81 | printf("-t directory Directory name of the trace to write to.\n" |
| 82 | " It will be created.\n"); |
| 83 | printf("-c directory Root directory of the debugfs trace channels.\n"); |
| 84 | printf("-d Run in background (daemon).\n"); |
| 85 | printf("-a Append to an possibly existing trace.\n"); |
| 86 | printf("-N Number of threads to start.\n"); |
| 87 | printf("-f Dump only flight recorder channels.\n"); |
| 88 | printf("-n Dump only normal channels.\n"); |
| 89 | printf("-v Verbose mode.\n"); |
| 90 | printf("\n"); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | /* parse_arguments |
| 95 | * |
| 96 | * Parses the command line arguments. |
| 97 | * |
| 98 | * Returns 1 if the arguments were correct, but doesn't ask for program |
| 99 | * continuation. Returns -1 if the arguments are incorrect, or 0 if OK. |
| 100 | */ |
| 101 | int parse_arguments(int argc, char **argv) |
| 102 | { |
| 103 | int ret = 0; |
| 104 | int argn = 1; |
| 105 | |
| 106 | if(argc == 2) { |
| 107 | if(strcmp(argv[1], "-h") == 0) { |
| 108 | return 1; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | while(argn < argc) { |
| 113 | |
| 114 | switch(argv[argn][0]) { |
| 115 | case '-': |
| 116 | switch(argv[argn][1]) { |
| 117 | case 't': |
| 118 | if(argn+1 < argc) { |
| 119 | trace_name = argv[argn+1]; |
| 120 | argn++; |
| 121 | } |
| 122 | break; |
| 123 | case 'c': |
| 124 | if(argn+1 < argc) { |
| 125 | channel_name = argv[argn+1]; |
| 126 | argn++; |
| 127 | } |
| 128 | break; |
| 129 | case 'd': |
| 130 | daemon_mode = 1; |
| 131 | break; |
| 132 | case 'a': |
| 133 | append_mode = 1; |
| 134 | break; |
| 135 | case 'N': |
| 136 | if(argn+1 < argc) { |
| 137 | num_threads = strtoul(argv[argn+1], NULL, 0); |
| 138 | argn++; |
| 139 | } |
| 140 | break; |
| 141 | case 'f': |
| 142 | dump_flight_only = 1; |
| 143 | break; |
| 144 | case 'n': |
| 145 | dump_normal_only = 1; |
| 146 | break; |
| 147 | case 'v': |
| 148 | verbose_mode = 1; |
| 149 | break; |
| 150 | default: |
| 151 | printf("Invalid argument '%s'.\n", argv[argn]); |
| 152 | printf("\n"); |
| 153 | ret = -1; |
| 154 | } |
| 155 | break; |
| 156 | default: |
| 157 | printf("Invalid argument '%s'.\n", argv[argn]); |
| 158 | printf("\n"); |
| 159 | ret = -1; |
| 160 | } |
| 161 | argn++; |
| 162 | } |
| 163 | |
| 164 | if(trace_name == NULL) { |
| 165 | printf("Please specify a trace name.\n"); |
| 166 | printf("\n"); |
| 167 | ret = -1; |
| 168 | } |
| 169 | |
| 170 | if(channel_name == NULL) { |
| 171 | printf("Please specify a channel name.\n"); |
| 172 | printf("\n"); |
| 173 | ret = -1; |
| 174 | } |
| 175 | |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | void show_info(void) |
| 180 | { |
| 181 | printf("Linux Trace Toolkit Trace Daemon " VERSION "\n"); |
| 182 | printf("\n"); |
| 183 | printf("Reading from debugfs directory : %s\n", channel_name); |
| 184 | printf("Writing to trace directory : %s\n", trace_name); |
| 185 | printf("\n"); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | /* signal handling */ |
| 190 | |
| 191 | static void handler(int signo) |
| 192 | { |
| 193 | printf("Signal %d received : exiting cleanly\n", signo); |
| 194 | liblttd_stop(); |
| 195 | } |
| 196 | |
| 197 | int lttd_on_open_channel(struct liblttd_callbacks *data, struct fd_pair *pair, char *relative_channel_path) |
| 198 | { |
| 199 | int open_ret = 0; |
| 200 | int ret; |
| 201 | struct stat stat_buf; |
| 202 | struct lttd_channel_data *channel_data; |
| 203 | |
| 204 | pair->user_data = malloc(sizeof(struct lttd_channel_data)); |
| 205 | channel_data = pair->user_data; |
| 206 | |
| 207 | strncpy(end_path_trace, relative_channel_path, PATH_MAX - path_trace_len); |
| 208 | printf_verbose("Creating trace file %s\n", path_trace); |
| 209 | |
| 210 | ret = stat(path_trace, &stat_buf); |
| 211 | if(ret == 0) { |
| 212 | if(append_mode) { |
| 213 | printf_verbose("Appending to file %s as requested\n", |
| 214 | path_trace); |
| 215 | |
| 216 | channel_data->trace = open(path_trace, O_WRONLY, S_IRWXU|S_IRWXG|S_IRWXO); |
| 217 | if(channel_data->trace == -1) { |
| 218 | perror(path_trace); |
| 219 | open_ret = -1; |
| 220 | goto end; |
| 221 | } |
| 222 | ret = lseek(channel_data->trace, 0, SEEK_END); |
| 223 | if (ret < 0) { |
| 224 | perror(path_trace); |
| 225 | open_ret = -1; |
| 226 | close(channel_data->trace); |
| 227 | goto end; |
| 228 | } |
| 229 | } else { |
| 230 | printf("File %s exists, cannot open. Try append mode.\n", path_trace); |
| 231 | open_ret = -1; |
| 232 | goto end; |
| 233 | } |
| 234 | } else { |
| 235 | if(errno == ENOENT) { |
| 236 | channel_data->trace = open(path_trace, O_WRONLY|O_CREAT|O_EXCL, S_IRWXU|S_IRWXG|S_IRWXO); |
| 237 | if(channel_data->trace == -1) { |
| 238 | perror(path_trace); |
| 239 | open_ret = -1; |
| 240 | goto end; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | end: |
| 246 | return open_ret; |
| 247 | |
| 248 | } |
| 249 | |
| 250 | int lttd_on_close_channel(struct liblttd_callbacks *data, struct fd_pair *pair) |
| 251 | { |
| 252 | int ret; |
| 253 | ret = close(((struct lttd_channel_data *)(pair->user_data))->trace); |
| 254 | free(pair->user_data); |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | int lttd_on_new_channels_folder(struct liblttd_callbacks *data, char *relative_folder_path) |
| 259 | { |
| 260 | int ret; |
| 261 | int open_ret = 0; |
| 262 | |
| 263 | strncpy(end_path_trace, relative_folder_path, PATH_MAX - path_trace_len); |
| 264 | printf_verbose("Creating trace subdirectory %s\n", path_trace); |
| 265 | |
| 266 | ret = mkdir(path_trace, S_IRWXU|S_IRWXG|S_IRWXO); |
| 267 | if(ret == -1) { |
| 268 | if(errno != EEXIST) { |
| 269 | perror(path_trace); |
| 270 | open_ret = -1; |
| 271 | goto end; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | end: |
| 276 | return open_ret; |
| 277 | } |
| 278 | |
| 279 | int lttd_on_read_subbuffer(struct liblttd_callbacks *data, struct fd_pair *pair, unsigned int len) |
| 280 | { |
| 281 | long ret; |
| 282 | off_t offset = 0; |
| 283 | |
| 284 | while (len > 0) { |
| 285 | printf_verbose("splice chan to pipe offset %lu\n", |
| 286 | (unsigned long)offset); |
| 287 | ret = splice(pair->channel, &offset, thread_pipe[1], NULL, |
| 288 | len, SPLICE_F_MOVE | SPLICE_F_MORE); |
| 289 | printf_verbose("splice chan to pipe ret %ld\n", ret); |
| 290 | if (ret < 0) { |
| 291 | perror("Error in relay splice"); |
| 292 | goto write_error; |
| 293 | } |
| 294 | ret = splice(thread_pipe[0], NULL, |
| 295 | ((struct lttd_channel_data *)(pair->user_data))->trace, |
| 296 | NULL, ret, SPLICE_F_MOVE | SPLICE_F_MORE); |
| 297 | printf_verbose("splice pipe to file %ld\n", ret); |
| 298 | if (ret < 0) { |
| 299 | perror("Error in file splice"); |
| 300 | goto write_error; |
| 301 | } |
| 302 | len -= ret; |
| 303 | } |
| 304 | |
| 305 | write_error: |
| 306 | return ret; |
| 307 | } |
| 308 | |
| 309 | int on_new_thread(struct liblttd_callbacks *data, unsigned long thread_num) { |
| 310 | int ret; |
| 311 | ret = pipe(thread_pipe); |
| 312 | if (ret < 0) { |
| 313 | perror("Error creating pipe"); |
| 314 | return ret; |
| 315 | } |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | int on_close_thread(struct liblttd_callbacks *data, unsigned long thread_num) { |
| 320 | close(thread_pipe[0]); /* close read end */ |
| 321 | close(thread_pipe[1]); /* close write end */ |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | int main(int argc, char ** argv) |
| 326 | { |
| 327 | int ret = 0; |
| 328 | struct sigaction act; |
| 329 | |
| 330 | struct liblttd_callbacks callbacks = { |
| 331 | lttd_on_open_channel, |
| 332 | lttd_on_close_channel, |
| 333 | lttd_on_new_channels_folder, |
| 334 | lttd_on_read_subbuffer, |
| 335 | NULL, |
| 336 | on_new_thread, |
| 337 | on_close_thread, |
| 338 | NULL |
| 339 | }; |
| 340 | |
| 341 | ret = parse_arguments(argc, argv); |
| 342 | |
| 343 | if(ret != 0) show_arguments(); |
| 344 | if(ret < 0) return EINVAL; |
| 345 | if(ret > 0) return 0; |
| 346 | |
| 347 | show_info(); |
| 348 | |
| 349 | /* Connect the signal handlers */ |
| 350 | act.sa_handler = handler; |
| 351 | act.sa_flags = 0; |
| 352 | sigemptyset(&(act.sa_mask)); |
| 353 | sigaddset(&(act.sa_mask), SIGTERM); |
| 354 | sigaddset(&(act.sa_mask), SIGQUIT); |
| 355 | sigaddset(&(act.sa_mask), SIGINT); |
| 356 | sigaction(SIGTERM, &act, NULL); |
| 357 | sigaction(SIGQUIT, &act, NULL); |
| 358 | sigaction(SIGINT, &act, NULL); |
| 359 | |
| 360 | if(daemon_mode) { |
| 361 | ret = daemon(0, 0); |
| 362 | |
| 363 | if(ret == -1) { |
| 364 | perror("An error occured while daemonizing."); |
| 365 | exit(-1); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | strncpy(path_trace, trace_name, PATH_MAX-1); |
| 370 | path_trace_len = strlen(path_trace); |
| 371 | end_path_trace = path_trace + path_trace_len; |
| 372 | |
| 373 | liblttd_start(channel_name, num_threads, dump_flight_only, dump_normal_only, |
| 374 | verbose_mode, &callbacks); |
| 375 | |
| 376 | return ret; |
| 377 | } |
| 378 | |