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