Create ust-ctl.c/.h files for user-space control
[lttng-tools.git] / ltt-sessiond / ust-ctl.c
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; either version 2
7 * of the License, or (at your option) any later version.
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 <errno.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <ust/ustctl.h>
27
28 #include "liblttsessiondcomm.h"
29 #include "lttngerr.h"
30 #include "ltt-sessiond.h"
31 #include "session.h"
32 #include "ust-ctl.h"
33 #include "trace.h"
34
35 /*
36 * find_session_ust_trace_by_pid
37 *
38 * Iterate over the session ust_traces and
39 * return a pointer or NULL if not found.
40 */
41 static struct ltt_ust_trace *find_session_ust_trace_by_pid(
42 struct ltt_session *session, pid_t pid)
43 {
44 struct ltt_ust_trace *iter;
45
46 cds_list_for_each_entry(iter, &session->ust_traces, list) {
47 if (iter->pid == pid) {
48 /* Found */
49 return iter;
50 }
51 }
52
53 return NULL;
54 }
55
56 /*
57 * get_trace_count_per_session
58 *
59 * Return the total count of traces (ust and kernel)
60 * for the specified session.
61 */
62 int get_trace_count_per_session(struct ltt_session *session)
63 {
64 return session->ust_trace_count + session->kern_session_count;
65 }
66
67 /*
68 * get_traces_per_session
69 *
70 * Fill the lttng_trace array of all the
71 * available trace of the session.
72 */
73 void get_traces_per_session(struct ltt_session *session, struct lttng_trace *traces)
74 {
75 int i = 0;
76 struct ltt_ust_trace *ust_iter;
77 struct lttng_trace trace;
78
79 DBG("Getting userspace traces for session %s", session->name);
80
81 /* Getting userspace traces */
82 cds_list_for_each_entry(ust_iter, &session->ust_traces, list) {
83 trace.type = USERSPACE;
84 trace.pid = ust_iter->pid;
85 strncpy(trace.name, ust_iter->name, sizeof(trace.name));
86 trace.name[sizeof(trace.name) - 1] = '\0';
87 memcpy(&traces[i], &trace, sizeof(trace));
88 memset(&trace, 0, sizeof(trace));
89 i++;
90 }
91
92 DBG("Getting kernel traces for session %s", session->name);
93
94 /* Getting kernel traces */
95 if (session->kern_session_count > 0) {
96 trace.type = KERNEL;
97 strncpy(trace.name, "kernel", 6);
98 memcpy(&traces[i], &trace, sizeof(trace));
99 }
100 }
101
102 /*
103 * ust_create_trace
104 *
105 * Create an userspace trace using pid.
106 * This trace is then appended to the current session
107 * ust trace list.
108 */
109 int ust_create_trace(struct command_ctx *cmd_ctx)
110 {
111 int ret;
112 struct ltt_ust_trace *trace;
113
114 DBG("Creating trace for pid %d", cmd_ctx->lsm->pid);
115
116 trace = malloc(sizeof(struct ltt_ust_trace));
117 if (trace == NULL) {
118 perror("malloc");
119 ret = -1;
120 goto error;
121 }
122
123 /* Init */
124 trace->pid = cmd_ctx->lsm->pid;
125 trace->shmid = 0;
126 /* NOTE: to be removed. Trace name will no longer be
127 * required for LTTng userspace tracer. For now, we set it
128 * to 'auto' for API compliance.
129 */
130 snprintf(trace->name, 5, "auto");
131
132 ret = ustctl_create_trace(cmd_ctx->ust_sock, trace->name);
133 if (ret < 0) {
134 ret = LTTCOMM_CREATE_FAIL;
135 goto error_create;
136 }
137
138 /* Check if current session is valid */
139 if (cmd_ctx->session) {
140 cds_list_add(&trace->list, &cmd_ctx->session->ust_traces);
141 cmd_ctx->session->ust_trace_count++;
142 }
143
144 return LTTCOMM_OK;
145
146 error_create:
147 free(trace);
148 error:
149 return ret;
150 }
151
152 /*
153 * ust_start_trace
154 *
155 * Start a trace. This trace, identified by the pid, must be
156 * in the current session ust_traces list.
157 */
158 int ust_start_trace(struct command_ctx *cmd_ctx)
159 {
160 int ret;
161 struct ltt_ust_trace *trace;
162
163 DBG("Starting trace for pid %d", cmd_ctx->lsm->pid);
164
165 trace = find_session_ust_trace_by_pid(cmd_ctx->session, cmd_ctx->lsm->pid);
166 if (trace == NULL) {
167 ret = LTTCOMM_NO_TRACE;
168 goto error;
169 }
170
171 ret = ustctl_start_trace(cmd_ctx->ust_sock, "auto");
172 if (ret < 0) {
173 ret = LTTCOMM_START_FAIL;
174 goto error;
175 }
176
177 ret = LTTCOMM_OK;
178
179 error:
180 return ret;
181 }
182
183 /*
184 * ust_stop_trace
185 *
186 * Stop a trace. This trace, identified by the pid, must be
187 * in the current session ust_traces list.
188 */
189 int ust_stop_trace(struct command_ctx *cmd_ctx)
190 {
191 int ret;
192 struct ltt_ust_trace *trace;
193
194 DBG("Stopping trace for pid %d", cmd_ctx->lsm->pid);
195
196 trace = find_session_ust_trace_by_pid(cmd_ctx->session, cmd_ctx->lsm->pid);
197 if (trace == NULL) {
198 ret = LTTCOMM_NO_TRACE;
199 goto error;
200 }
201
202 ret = ustctl_stop_trace(cmd_ctx->ust_sock, trace->name);
203 if (ret < 0) {
204 ret = LTTCOMM_STOP_FAIL;
205 goto error;
206 }
207
208 ret = LTTCOMM_OK;
209
210 error:
211 return ret;
212 }
213
This page took 0.032781 seconds and 4 git commands to generate.