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