Update version to 2.0-pre1
[lttng-tools.git] / lttng / commands / list.c
CommitLineData
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
6e2d116c 25#include "../cmd.h"
f3ed775e
DG
26
27static int opt_pid;
28static int opt_channels;
29
30enum {
31 OPT_HELP = 1,
32 OPT_EVENTS,
33 OPT_KERNEL,
34 OPT_APPS,
35 OPT_SESSIONS,
36 OPT_CHANNEL,
37};
38
39static struct poptOption long_options[] = {
40 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
41 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
42 {"events", 'e', POPT_ARG_NONE, 0, OPT_EVENTS, 0, 0},
43 {"kernel", 'k', POPT_ARG_NONE, 0, OPT_KERNEL, 0, 0},
44 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
45 {"apps", 'a', POPT_ARG_NONE, 0, OPT_APPS, 0, 0},
46 {"session", 's', POPT_ARG_NONE, 0, OPT_SESSIONS, 0, 0},
47 {"channel", 'c', POPT_ARG_VAL, &opt_channels, 1, 0, 0},
48 {0, 0, 0, 0, 0, 0, 0}
49};
50
51/*
52 * usage
53 */
54static void usage(FILE *ofp)
55{
56 fprintf(ofp, "usage: lttng list [options] [<executable>]\n");
57 fprintf(ofp, "\n");
58 fprintf(ofp, " -h, --help Show this help\n");
59 fprintf(ofp, " -e, --events List all available instrumentation\n");
60 fprintf(ofp, " -k, --kernel List kernel instrumentation\n");
61 fprintf(ofp, " -p, --pid PID List user-space instrumentation by PID\n");
62 fprintf(ofp, " -a, --apps List traceable user-space applications/pids\n");
63 fprintf(ofp, " -s, --sessions List tracing session\n");
64 fprintf(ofp, "\n");
65}
66
67/*
68 * get_cmdline_by_pid
69 *
70 * Get command line from /proc for a specific pid.
71 *
72 * On success, return an allocated string pointer pointing to the proc
73 * cmdline.
74 * On error, return NULL.
75 */
76static char *get_cmdline_by_pid(pid_t pid)
77{
78 int ret;
79 FILE *fp;
80 char *cmdline = NULL;
81 char path[24]; /* Can't go bigger than /proc/65535/cmdline */
82
83 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
84 fp = fopen(path, "r");
85 if (fp == NULL) {
86 goto end;
87 }
88
89 /* Caller must free() *cmdline */
90 cmdline = malloc(PATH_MAX);
91 ret = fread(cmdline, 1, PATH_MAX, fp);
92 fclose(fp);
93
94end:
95 return cmdline;
96}
97
98/*
99 * list_kernel
100 *
101 * Ask for all trace events in the kernel and pretty print them.
102 */
103static int list_kernel(void)
104{
105 int ret, pos, size;
106 char *event_list, *event, *ptr;
7d29a247 107 struct lttng_domain dom;
f3ed775e
DG
108
109 DBG("Getting all tracing events");
110
7d29a247
DG
111 dom.type = LTTNG_DOMAIN_KERNEL;
112
113 ret = lttng_list_events(&dom, &event_list);
f3ed775e
DG
114 if (ret < 0) {
115 ERR("Unable to list kernel instrumentation");
116 return ret;
117 }
118
119 MSG("Kernel tracepoints:\n-------------");
120
121 ptr = event_list;
122 while ((size = sscanf(ptr, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
123 MSG(" - %s", event);
124 /* Move pointer to the next line */
125 ptr += pos + 1;
126 free(event);
127 }
128
129 free(event_list);
130
131 return CMD_SUCCESS;
132}
133
134/*
135 * list_sessions
136 *
137 * Get the list of available sessions from the session daemon and print it to
138 * user.
139 */
140static int list_sessions(void)
141{
142 int ret, count, i;
143 struct lttng_session *sessions;
144
145 count = lttng_list_sessions(&sessions);
146 DBG("Session count %d", count);
147 if (count < 0) {
148 ret = count;
149 goto error;
150 }
151
152 MSG("Available sessions:");
153 for (i = 0; i < count; i++) {
154 MSG(" %d) %s (%s)", i+1, sessions[i].name, sessions[i].path);
155 }
156
157 free(sessions);
158
159 return CMD_SUCCESS;
160
161error:
162 return ret;
163}
164
165/*
166 * list_apps
167 *
168 * Get the UST traceable pid list and print them to the user.
169 */
170static int list_apps(void)
171{
172 int i, ret, count;
173 pid_t *pids;
174 char *cmdline;
175
7d29a247 176 count = -1;
f3ed775e
DG
177 //count = lttng_ust_list_traceable_apps(&pids);
178 if (count < 0) {
179 ret = count;
180 goto error;
181 }
182
183 MSG("LTTng UST traceable application [name (pid)]:");
184 for (i=0; i < count; i++) {
185 cmdline = get_cmdline_by_pid(pids[i]);
186 if (cmdline == NULL) {
187 MSG("\t(not running) (%d)", pids[i]);
188 continue;
189 }
190 MSG("\t%s (%d)", cmdline, pids[i]);
191 free(cmdline);
192 }
193
194 /* Allocated by lttng_ust_list_apps() */
195 free(pids);
196
197 return CMD_SUCCESS;
198
199error:
200 return ret;
201}
202
203/*
204 * list_pid
205 *
206 * List all instrumentation for a specific pid
207 */
208/*
209static int list_pid(int pid)
210{
211 int ret;
212
213 return CMD_SUCCESS;
214
215error:
216 return ret;
217}
218*/
219
220/*
221 * list_executable
222 *
223 * List all instrumentation for an executable on the system
224 */
225/*
226static int list_executable(char *name)
227{
228}
229*/
230
231/*
232 * cmd_list
233 *
234 * The 'list <options>' first level command
235 */
236int cmd_list(int argc, const char **argv)
237{
238 int opt, ret = CMD_SUCCESS;
239 const char *command_name;
240 static poptContext pc;
241
242 if (argc < 2) {
243 usage(stderr);
244 goto end;
245 }
246
247 pc = poptGetContext(NULL, argc, argv, long_options, 0);
248 poptReadDefaultConfig(pc, 0);
249
250 while ((opt = poptGetNextOpt(pc)) != -1) {
251 switch (opt) {
252 case OPT_HELP:
253 usage(stderr);
254 goto end;
255 case OPT_EVENTS:
256 ret = CMD_NOT_IMPLEMENTED;
257 goto end;
258 case OPT_APPS:
259 ret = list_apps();
260 break;
261 case OPT_KERNEL:
262 ret = list_kernel();
263 break;
264 case OPT_SESSIONS:
265 ret = list_sessions();
266 break;
267 default:
268 usage(stderr);
269 ret = CMD_UNDEFINED;
270 goto end;
271 }
272 }
273
274 if (opt_pid != 0) {
275 //ret = list_pid(pid);
276 ret = CMD_NOT_IMPLEMENTED;
277 }
278
279 command_name = poptGetArg(pc);
280 if (command_name != NULL) {
281 // ret = list_executable(command_name);
282 ret = CMD_NOT_IMPLEMENTED;
283 }
284
285end:
286 return ret;
287}
This page took 0.032724 seconds and 4 git commands to generate.