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