Commit | Line | Data |
---|---|---|
f3ed775e DG |
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 | |
82a3637f DG |
6 | * as published by the Free Software Foundation; only version 2 |
7 | * of the License. | |
f3ed775e DG |
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 <unistd.h> | |
27 | ||
6e2d116c DG |
28 | #include "../cmd.h" |
29 | #include "../conf.h" | |
30 | #include "../utils.h" | |
f3ed775e DG |
31 | |
32 | static char *opt_session_name; | |
33 | ||
34 | enum { | |
35 | OPT_HELP = 1, | |
36 | }; | |
37 | ||
cd80958d DG |
38 | static struct lttng_handle *handle; |
39 | ||
f3ed775e DG |
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 | {0, 0, 0, 0, 0, 0, 0} | |
44 | }; | |
45 | ||
46 | /* | |
47 | * usage | |
48 | */ | |
49 | static void usage(FILE *ofp) | |
50 | { | |
51 | fprintf(ofp, "usage: lttng start [options] [NAME]\n"); | |
52 | fprintf(ofp, "\n"); | |
53 | fprintf(ofp, "Where NAME is an optional session name. If not specified, lttng will\n"); | |
54 | fprintf(ofp, "get it from the configuration directory (.lttng).\n"); | |
55 | fprintf(ofp, "\n"); | |
56 | fprintf(ofp, " -h, --help Show this help\n"); | |
57 | fprintf(ofp, "\n"); | |
58 | } | |
59 | ||
60 | /* | |
61 | * start_tracing | |
62 | * | |
63 | * Start tracing for all trace of the session. | |
64 | */ | |
65 | static int start_tracing(void) | |
66 | { | |
67 | int ret = CMD_SUCCESS; | |
68 | char *session_name; | |
69 | ||
70 | if (opt_session_name == NULL) { | |
71 | session_name = get_session_name(); | |
72 | if (session_name == NULL) { | |
73 | ret = CMD_ERROR; | |
74 | goto error; | |
75 | } | |
76 | } else { | |
77 | session_name = opt_session_name; | |
78 | } | |
79 | ||
cd80958d DG |
80 | handle = lttng_create_handle(session_name, NULL); |
81 | if (handle == NULL) { | |
82 | ret = -1; | |
83 | goto error; | |
84 | } | |
85 | ||
f3ed775e DG |
86 | DBG("Starting tracing for session %s", session_name); |
87 | ||
cd80958d | 88 | ret = lttng_start_tracing(handle); |
f3ed775e DG |
89 | if (ret < 0) { |
90 | goto free_name; | |
91 | } | |
92 | ||
93 | MSG("Tracing started for session %s", session_name); | |
94 | ||
95 | free_name: | |
b73d0b29 MD |
96 | if (opt_session_name == NULL) { |
97 | free(session_name); | |
98 | } | |
f3ed775e | 99 | error: |
cd80958d DG |
100 | lttng_destroy_handle(handle); |
101 | ||
f3ed775e DG |
102 | return ret; |
103 | } | |
104 | ||
105 | /* | |
106 | * cmd_start | |
107 | * | |
108 | * The 'start <options>' first level command | |
109 | */ | |
110 | int cmd_start(int argc, const char **argv) | |
111 | { | |
112 | int opt, ret; | |
113 | static poptContext pc; | |
114 | ||
115 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
116 | poptReadDefaultConfig(pc, 0); | |
117 | ||
118 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
119 | switch (opt) { | |
120 | case OPT_HELP: | |
121 | usage(stderr); | |
122 | ret = CMD_SUCCESS; | |
123 | goto end; | |
124 | default: | |
125 | usage(stderr); | |
126 | ret = CMD_UNDEFINED; | |
127 | goto end; | |
128 | } | |
129 | } | |
130 | ||
131 | opt_session_name = (char*) poptGetArg(pc); | |
132 | ||
133 | ret = start_tracing(); | |
134 | ||
135 | end: | |
136 | return ret; | |
137 | } |