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