2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
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.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <urcu/list.h>
26 #include <common/error.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
32 #include "trace-ust.h"
36 * Add kernel context to all channel.
38 static int add_kctx_all_channels(struct ltt_kernel_session
*ksession
,
39 struct ltt_kernel_context
*kctx
)
42 struct ltt_kernel_channel
*kchan
;
47 DBG("Adding kernel context to all channels");
49 /* Go over all channels */
50 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
51 ret
= kernel_add_channel_context(kchan
, kctx
);
53 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
65 * Add kernel context to a specific channel.
67 static int add_kctx_to_channel(struct ltt_kernel_context
*kctx
,
68 struct ltt_kernel_channel
*kchan
)
75 DBG("Add kernel context to channel '%s'", kchan
->channel
->name
);
77 ret
= kernel_add_channel_context(kchan
, kctx
);
79 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
90 * Add UST context to channel.
92 static int add_uctx_to_channel(struct ltt_ust_session
*usess
,
93 enum lttng_domain_type domain
,
94 struct ltt_ust_channel
*uchan
, struct lttng_event_context
*ctx
)
97 struct ltt_ust_context
*uctx
= NULL
;
103 /* Check if context is duplicate */
104 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
105 if (trace_ust_match_context(uctx
, ctx
)) {
113 case LTTNG_DOMAIN_JUL
:
114 case LTTNG_DOMAIN_LOG4J
:
116 struct agent
*agt
= trace_ust_find_agent(usess
, domain
);
119 agt
= agent_create(domain
);
121 ret
= LTTNG_ERR_NOMEM
;
124 agent_add(agt
, usess
->agents
);
126 ret
= agent_add_context(ctx
, agt
);
127 if (ret
!= LTTNG_OK
) {
131 ret
= agent_enable_context(ctx
, domain
);
132 if (ret
!= LTTNG_OK
) {
137 case LTTNG_DOMAIN_UST
:
143 /* Create ltt UST context */
144 uctx
= trace_ust_create_context(ctx
);
150 ret
= ust_app_add_ctx_channel_glb(usess
, uchan
, uctx
);
157 /* Add ltt UST context node to ltt UST channel */
158 lttng_ht_add_ulong(uchan
->ctx
, &uctx
->node
);
160 cds_list_add_tail(&uctx
->list
, &uchan
->ctx_list
);
162 DBG("Context UST %d added to channel %s", uctx
->ctx
.ctx
, uchan
->name
);
173 * Add kernel context to tracer.
175 int context_kernel_add(struct ltt_kernel_session
*ksession
,
176 struct lttng_event_context
*ctx
, char *channel_name
)
179 struct ltt_kernel_channel
*kchan
;
180 struct ltt_kernel_context
*kctx
;
184 assert(channel_name
);
186 kctx
= trace_kernel_create_context(NULL
);
188 ret
= LTTNG_ERR_NOMEM
;
192 /* Setup kernel context structure */
194 case LTTNG_EVENT_CONTEXT_PID
:
195 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PID
;
197 case LTTNG_EVENT_CONTEXT_PROCNAME
:
198 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PROCNAME
;
200 case LTTNG_EVENT_CONTEXT_PRIO
:
201 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PRIO
;
203 case LTTNG_EVENT_CONTEXT_NICE
:
204 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_NICE
;
206 case LTTNG_EVENT_CONTEXT_VPID
:
207 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_VPID
;
209 case LTTNG_EVENT_CONTEXT_TID
:
210 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_TID
;
212 case LTTNG_EVENT_CONTEXT_VTID
:
213 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_VTID
;
215 case LTTNG_EVENT_CONTEXT_PPID
:
216 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PPID
;
218 case LTTNG_EVENT_CONTEXT_VPPID
:
219 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_VPPID
;
221 case LTTNG_EVENT_CONTEXT_HOSTNAME
:
222 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_HOSTNAME
;
224 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
225 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
226 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
;
229 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
233 kctx
->ctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
234 kctx
->ctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
235 strncpy(kctx
->ctx
.u
.perf_counter
.name
, ctx
->u
.perf_counter
.name
,
236 LTTNG_SYMBOL_NAME_LEN
);
237 kctx
->ctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
239 if (*channel_name
== '\0') {
240 ret
= add_kctx_all_channels(ksession
, kctx
);
241 if (ret
!= LTTNG_OK
) {
245 /* Get kernel channel */
246 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
248 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
252 ret
= add_kctx_to_channel(kctx
, kchan
);
253 if (ret
!= LTTNG_OK
) {
262 trace_kernel_destroy_context(kctx
);
268 * Add UST context to tracer.
270 int context_ust_add(struct ltt_ust_session
*usess
,
271 enum lttng_domain_type domain
, struct lttng_event_context
*ctx
,
275 struct lttng_ht_iter iter
;
276 struct lttng_ht
*chan_ht
;
277 struct ltt_ust_channel
*uchan
= NULL
;
281 assert(channel_name
);
285 chan_ht
= usess
->domain_global
.channels
;
287 /* Get UST channel if defined */
288 if (channel_name
[0] != '\0') {
289 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
291 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
297 /* Add ctx to channel */
298 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
301 /* Add ctx all events, all channels */
302 cds_lfht_for_each_entry(chan_ht
->ht
, &iter
.iter
, uchan
, node
.node
) {
303 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
305 ERR("Failed to add context to channel %s",
315 ret
= LTTNG_ERR_UST_CONTEXT_EXIST
;
318 ret
= LTTNG_ERR_FATAL
;
321 ret
= LTTNG_ERR_UST_CONTEXT_INVAL
;
324 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;