Initial import
[lttng-tools.git] / lttng / lttng.c
1 /* Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
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 *
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.
16 *
17 */
18
19 #define _GNU_SOURCE
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <grp.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
32
33 #include <lttng/liblttngctl.h>
34
35 #include "lttng.h"
36 #include "lttngerr.h"
37
38 /* Variables */
39 static char *progname;
40
41 /* Prototypes */
42 static int process_client_opt(void);
43 static int process_opt_list_apps(void);
44
45 /*
46 * start_client
47 *
48 * Process client request from the command line
49 * options. Every tracing action is done by the
50 * liblttngctl API.
51 */
52 static int process_client_opt(void)
53 {
54 int ret;
55
56 /* Connect to the session daemon */
57 ret = lttng_connect_sessiond();
58 if (ret < 0) {
59 ERR("%s", lttng_get_readable_code(ret));
60 goto end;
61 }
62
63 if (opt_list_apps) {
64 ret = process_opt_list_apps();
65 if (ret < 0) {
66 ERR("%s", lttng_get_readable_code(ret));
67 goto end;
68 }
69 }
70
71 return 0;
72
73 end:
74 return ret;
75 }
76
77 /*
78 * process_opt_list_apps
79 *
80 * Get the UST traceable pid list and print
81 * them to the user.
82 */
83 static int process_opt_list_apps(void)
84 {
85 int i, ret;
86 pid_t *pids;
87 FILE *fp;
88 char path[24]; /* Can't go bigger than /proc/65535/cmdline */
89 char cmdline[PATH_MAX];
90
91 ret = lttng_ust_list_apps(&pids);
92 if (ret < 0) {
93 goto error;
94 }
95
96 MSG("LTTng UST traceable application [name (pid)]:");
97 for (i=0; i < ret; i++) {
98 snprintf(path, sizeof(path), "/proc/%d/cmdline", pids[i]);
99 fp = fopen(path, "r");
100 if (fp == NULL) {
101 continue;
102 }
103 ret = fread(cmdline, 1, sizeof(cmdline), fp);
104 MSG("\t%s (%d)", cmdline, pids[i]);
105 fclose(fp);
106 }
107
108 return 0;
109
110 error:
111 return ret;
112 }
113
114 /*
115 * check_ltt_sessiond
116 *
117 * Check if the session daemon is available using
118 * the liblttngctl API for the check.
119 */
120 static int check_ltt_sessiond(void)
121 {
122 int ret;
123
124 ret = lttng_check_session_daemon();
125 if (ret < 0) {
126 ERR("No session daemon found. Aborting.");
127 }
128
129 return ret;
130 }
131
132
133 /*
134 * clean_exit
135 */
136 void clean_exit(int code)
137 {
138 DBG("Clean exit");
139 exit(code);
140 }
141
142 /*
143 * main
144 */
145 int main(int argc, char *argv[])
146 {
147 int ret;
148
149 progname = argv[0] ? argv[0] : "lttng";
150
151 /* For Mathieu Desnoyers aka Dr Tracing */
152 if (strncmp(progname, "drtrace", 7) == 0) {
153 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
154 }
155
156 ret = parse_args(argc, (const char **) argv);
157 if (ret < 0) {
158 return EXIT_FAILURE;
159 }
160
161 if (opt_tracing_group != NULL) {
162 DBG("Set tracing group to '%s'", opt_tracing_group);
163 lttng_set_tracing_group(opt_tracing_group);
164 }
165
166 /* If ask for kernel tracing, need root perms */
167 if (opt_trace_kernel) {
168 DBG("Kernel tracing activated");
169 if (getuid() != 0) {
170 ERR("%s must be setuid root", progname);
171 return -EPERM;
172 }
173 }
174
175 /* Check if the lttng session daemon is running.
176 * If no, a daemon will be spawned.
177 */
178 if (check_ltt_sessiond() < 0) {
179 return EXIT_FAILURE;
180 }
181
182 ret = process_client_opt();
183 if (ret < 0) {
184 return ret;
185 }
186
187 return 0;
188 }
This page took 0.046081 seconds and 4 git commands to generate.