| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; only version 2 |
| 7 | * of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <popt.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <time.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include "../cmd.h" |
| 30 | #include "../conf.h" |
| 31 | #include "../utils.h" |
| 32 | |
| 33 | static char *opt_output_path; |
| 34 | static char *opt_session_name; |
| 35 | |
| 36 | enum { |
| 37 | OPT_HELP = 1, |
| 38 | }; |
| 39 | |
| 40 | static struct poptOption long_options[] = { |
| 41 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 42 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 43 | {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, 0, 0}, |
| 44 | {0, 0, 0, 0, 0, 0, 0} |
| 45 | }; |
| 46 | |
| 47 | /* |
| 48 | * usage |
| 49 | */ |
| 50 | static void usage(FILE *ofp) |
| 51 | { |
| 52 | fprintf(ofp, "usage: lttng create [options] [NAME]\n"); |
| 53 | fprintf(ofp, "\n"); |
| 54 | fprintf(ofp, " -h, --help Show this help\n"); |
| 55 | fprintf(ofp, " -o, --output PATH Specify output path for traces\n"); |
| 56 | fprintf(ofp, "\n"); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * create_session |
| 61 | * |
| 62 | * Create a tracing session. If no name specified, a default name will be |
| 63 | * generated. |
| 64 | */ |
| 65 | static int create_session() |
| 66 | { |
| 67 | int ret, have_name = 0; |
| 68 | char datetime[16]; |
| 69 | char *session_name, *traces_path = NULL, *alloc_path = NULL; |
| 70 | time_t rawtime; |
| 71 | struct tm *timeinfo; |
| 72 | |
| 73 | /* Get date and time for automatic session name/path */ |
| 74 | time(&rawtime); |
| 75 | timeinfo = localtime(&rawtime); |
| 76 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); |
| 77 | |
| 78 | /* Auto session name creation */ |
| 79 | if (opt_session_name == NULL) { |
| 80 | ret = asprintf(&session_name, "auto-%s", datetime); |
| 81 | if (ret < 0) { |
| 82 | perror("asprintf session name"); |
| 83 | goto error; |
| 84 | } |
| 85 | DBG("Auto session name set to %s", session_name); |
| 86 | } else { |
| 87 | session_name = opt_session_name; |
| 88 | have_name = 1; |
| 89 | } |
| 90 | |
| 91 | /* Auto output path */ |
| 92 | if (opt_output_path == NULL) { |
| 93 | alloc_path = strdup(config_get_default_path()); |
| 94 | if (alloc_path == NULL) { |
| 95 | ERR("Home path not found.\n \ |
| 96 | Please specify an output path using -o, --output PATH"); |
| 97 | ret = CMD_FATAL; |
| 98 | goto error; |
| 99 | } |
| 100 | |
| 101 | if (have_name) { |
| 102 | ret = asprintf(&traces_path, "%s/" LTTNG_DEFAULT_TRACE_DIR_NAME |
| 103 | "/%s-%s", alloc_path, session_name, datetime); |
| 104 | } else { |
| 105 | ret = asprintf(&traces_path, "%s/" LTTNG_DEFAULT_TRACE_DIR_NAME |
| 106 | "/%s", alloc_path, session_name); |
| 107 | } |
| 108 | |
| 109 | if (ret < 0) { |
| 110 | perror("asprintf trace dir name"); |
| 111 | goto error; |
| 112 | } |
| 113 | } else { |
| 114 | traces_path = opt_output_path; |
| 115 | } |
| 116 | |
| 117 | ret = lttng_create_session(session_name, traces_path); |
| 118 | if (ret < 0) { |
| 119 | goto error; |
| 120 | } |
| 121 | |
| 122 | /* Init lttng session config */ |
| 123 | ret = config_init(session_name); |
| 124 | if (ret < 0) { |
| 125 | if (ret == -1) { |
| 126 | ret = CMD_ERROR; |
| 127 | } |
| 128 | goto error; |
| 129 | } |
| 130 | |
| 131 | MSG("Session %s created.", session_name); |
| 132 | MSG("Traces will be written in %s" , traces_path); |
| 133 | |
| 134 | ret = CMD_SUCCESS; |
| 135 | |
| 136 | error: |
| 137 | if (alloc_path) { |
| 138 | free(alloc_path); |
| 139 | } |
| 140 | |
| 141 | if (traces_path) { |
| 142 | free(traces_path); |
| 143 | } |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * cmd_list |
| 149 | * |
| 150 | * The 'list <options>' first level command |
| 151 | */ |
| 152 | int cmd_create(int argc, const char **argv) |
| 153 | { |
| 154 | int opt, ret = CMD_SUCCESS; |
| 155 | static poptContext pc; |
| 156 | |
| 157 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 158 | poptReadDefaultConfig(pc, 0); |
| 159 | |
| 160 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 161 | switch (opt) { |
| 162 | case OPT_HELP: |
| 163 | usage(stderr); |
| 164 | goto end; |
| 165 | default: |
| 166 | usage(stderr); |
| 167 | ret = CMD_UNDEFINED; |
| 168 | goto end; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | opt_session_name = (char*) poptGetArg(pc); |
| 173 | |
| 174 | ret = create_session(); |
| 175 | |
| 176 | end: |
| 177 | return ret; |
| 178 | } |