Create ust-ctl.c/.h files for user-space control
[lttng-tools.git] / ltt-sessiond / ust-ctl.c
CommitLineData
d4a2a84a
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
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>
d4a2a84a 21#include <stdlib.h>
baa0e4a9 22#include <stdio.h>
1657e9bb 23#include <string.h>
baa0e4a9
DG
24#include <unistd.h>
25
471d1693 26#include <ust/ustctl.h>
d4a2a84a 27
471d1693 28#include "liblttsessiondcomm.h"
d4a2a84a 29#include "lttngerr.h"
5461b305 30#include "ltt-sessiond.h"
baa0e4a9
DG
31#include "session.h"
32#include "ust-ctl.h"
33#include "trace.h"
471d1693 34
d4a2a84a
DG
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 */
471d1693
DG
41static struct ltt_ust_trace *find_session_ust_trace_by_pid(
42 struct ltt_session *session, pid_t pid)
d4a2a84a
DG
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
1657e9bb
DG
56/*
57 * get_trace_count_per_session
58 *
59 * Return the total count of traces (ust and kernel)
60 * for the specified session.
61 */
62int get_trace_count_per_session(struct ltt_session *session)
63{
20fe2104 64 return session->ust_trace_count + session->kern_session_count;
1657e9bb
DG
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 */
73void get_traces_per_session(struct ltt_session *session, struct lttng_trace *traces)
74{
75 int i = 0;
76 struct ltt_ust_trace *ust_iter;
1657e9bb
DG
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 */
20fe2104 95 if (session->kern_session_count > 0) {
1657e9bb 96 trace.type = KERNEL;
20fe2104 97 strncpy(trace.name, "kernel", 6);
1657e9bb 98 memcpy(&traces[i], &trace, sizeof(trace));
1657e9bb
DG
99 }
100}
471d1693
DG
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 */
5461b305 109int ust_create_trace(struct command_ctx *cmd_ctx)
471d1693
DG
110{
111 int ret;
112 struct ltt_ust_trace *trace;
113
5461b305 114 DBG("Creating trace for pid %d", cmd_ctx->lsm->pid);
471d1693
DG
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 */
5461b305 124 trace->pid = cmd_ctx->lsm->pid;
471d1693
DG
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
5461b305 132 ret = ustctl_create_trace(cmd_ctx->ust_sock, trace->name);
471d1693
DG
133 if (ret < 0) {
134 ret = LTTCOMM_CREATE_FAIL;
a55dd136 135 goto error_create;
471d1693
DG
136 }
137
138 /* Check if current session is valid */
5461b305
DG
139 if (cmd_ctx->session) {
140 cds_list_add(&trace->list, &cmd_ctx->session->ust_traces);
141 cmd_ctx->session->ust_trace_count++;
471d1693 142 }
a55dd136 143
5461b305 144 return LTTCOMM_OK;
471d1693 145
5e16da05
MD
146error_create:
147 free(trace);
471d1693
DG
148error:
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 */
5461b305 158int ust_start_trace(struct command_ctx *cmd_ctx)
471d1693
DG
159{
160 int ret;
161 struct ltt_ust_trace *trace;
162
5461b305 163 DBG("Starting trace for pid %d", cmd_ctx->lsm->pid);
471d1693 164
5461b305 165 trace = find_session_ust_trace_by_pid(cmd_ctx->session, cmd_ctx->lsm->pid);
471d1693
DG
166 if (trace == NULL) {
167 ret = LTTCOMM_NO_TRACE;
168 goto error;
169 }
170
5461b305 171 ret = ustctl_start_trace(cmd_ctx->ust_sock, "auto");
471d1693
DG
172 if (ret < 0) {
173 ret = LTTCOMM_START_FAIL;
174 goto error;
175 }
176
5461b305
DG
177 ret = LTTCOMM_OK;
178
471d1693
DG
179error:
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 */
5461b305 189int ust_stop_trace(struct command_ctx *cmd_ctx)
471d1693
DG
190{
191 int ret;
192 struct ltt_ust_trace *trace;
193
5461b305 194 DBG("Stopping trace for pid %d", cmd_ctx->lsm->pid);
471d1693 195
5461b305 196 trace = find_session_ust_trace_by_pid(cmd_ctx->session, cmd_ctx->lsm->pid);
471d1693
DG
197 if (trace == NULL) {
198 ret = LTTCOMM_NO_TRACE;
199 goto error;
200 }
201
5461b305 202 ret = ustctl_stop_trace(cmd_ctx->ust_sock, trace->name);
471d1693
DG
203 if (ret < 0) {
204 ret = LTTCOMM_STOP_FAIL;
205 goto error;
206 }
207
5461b305
DG
208 ret = LTTCOMM_OK;
209
471d1693
DG
210error:
211 return ret;
212}
213
This page took 0.030814 seconds and 4 git commands to generate.