Add lttng-share header and change data type
[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;
a55dd136 140 goto error_create;
471d1693
DG
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 }
a55dd136 148
5e16da05 149 return 0;
471d1693 150
5e16da05
MD
151error_create:
152 free(trace);
471d1693
DG
153error:
154 return ret;
155}
156
157/*
158 * ust_start_trace
159 *
160 * Start a trace. This trace, identified by the pid, must be
161 * in the current session ust_traces list.
162 */
163int ust_start_trace(int sock, pid_t pid)
164{
165 int ret;
166 struct ltt_ust_trace *trace;
167
168 DBG("Starting trace for pid %d", pid);
169
170 trace = find_session_ust_trace_by_pid(current_session, pid);
171 if (trace == NULL) {
172 ret = LTTCOMM_NO_TRACE;
173 goto error;
174 }
175
176 ret = ustctl_start_trace(sock, "auto");
177 if (ret < 0) {
178 ret = LTTCOMM_START_FAIL;
179 goto error;
180 }
181
182error:
183 return ret;
184}
185
186/*
187 * ust_stop_trace
188 *
189 * Stop a trace. This trace, identified by the pid, must be
190 * in the current session ust_traces list.
191 */
192int ust_stop_trace(int sock, pid_t pid)
193{
194 int ret;
195 struct ltt_ust_trace *trace;
196
197 DBG("Stopping trace for pid %d", pid);
198
199 trace = find_session_ust_trace_by_pid(current_session, pid);
200 if (trace == NULL) {
201 ret = LTTCOMM_NO_TRACE;
202 goto error;
203 }
204
205 ret = ustctl_stop_trace(sock, trace->name);
206 if (ret < 0) {
207 ret = LTTCOMM_STOP_FAIL;
208 goto error;
209 }
210
211error:
212 return ret;
213}
214
This page took 0.045401 seconds and 4 git commands to generate.