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