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