2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <urcu/list.h>
25 #include <common/error.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
31 #include "trace-ust.h"
34 * Add kernel context to all channel.
36 static int add_kctx_all_channels(struct ltt_kernel_session
*ksession
,
37 struct ltt_kernel_context
*kctx
)
40 struct ltt_kernel_channel
*kchan
;
45 DBG("Adding kernel context to all channels");
47 /* Go over all channels */
48 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
49 ret
= kernel_add_channel_context(kchan
, kctx
);
51 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
63 * Add kernel context to a specific channel.
65 static int add_kctx_to_channel(struct ltt_kernel_context
*kctx
,
66 struct ltt_kernel_channel
*kchan
)
73 DBG("Add kernel context to channel '%s'", kchan
->channel
->name
);
75 ret
= kernel_add_channel_context(kchan
, kctx
);
77 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
88 * Add UST context to channel.
90 static int add_uctx_to_channel(struct ltt_ust_session
*usess
, int domain
,
91 struct ltt_ust_channel
*uchan
, struct lttng_event_context
*ctx
)
94 struct ltt_ust_context
*uctx
;
100 /* Check if context is duplicate */
101 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
102 if (trace_ust_match_context(uctx
, ctx
)) {
108 /* Create ltt UST context */
109 uctx
= trace_ust_create_context(ctx
);
116 case LTTNG_DOMAIN_UST
:
117 ret
= ust_app_add_ctx_channel_glb(usess
, uchan
, uctx
);
129 /* Add ltt UST context node to ltt UST channel */
130 lttng_ht_add_ulong(uchan
->ctx
, &uctx
->node
);
132 cds_list_add_tail(&uctx
->list
, &uchan
->ctx_list
);
134 DBG("Context UST %d added to channel %s", uctx
->ctx
.ctx
, uchan
->name
);
145 * Add kernel context to tracer.
147 int context_kernel_add(struct ltt_kernel_session
*ksession
,
148 struct lttng_event_context
*ctx
, char *channel_name
)
151 struct ltt_kernel_channel
*kchan
;
152 struct ltt_kernel_context
*kctx
;
156 assert(channel_name
);
158 kctx
= trace_kernel_create_context(NULL
);
160 ret
= LTTNG_ERR_NOMEM
;
164 /* Setup kernel context structure */
166 case LTTNG_EVENT_CONTEXT_PID
:
167 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PID
;
169 case LTTNG_EVENT_CONTEXT_PROCNAME
:
170 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PROCNAME
;
172 case LTTNG_EVENT_CONTEXT_PRIO
:
173 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PRIO
;
175 case LTTNG_EVENT_CONTEXT_NICE
:
176 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_NICE
;
178 case LTTNG_EVENT_CONTEXT_VPID
:
179 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_VPID
;
181 case LTTNG_EVENT_CONTEXT_TID
:
182 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_TID
;
184 case LTTNG_EVENT_CONTEXT_VTID
:
185 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_VTID
;
187 case LTTNG_EVENT_CONTEXT_PPID
:
188 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PPID
;
190 case LTTNG_EVENT_CONTEXT_VPPID
:
191 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_VPPID
;
193 case LTTNG_EVENT_CONTEXT_HOSTNAME
:
194 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_HOSTNAME
;
196 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
197 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
198 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
;
201 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
205 kctx
->ctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
206 kctx
->ctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
207 strncpy(kctx
->ctx
.u
.perf_counter
.name
, ctx
->u
.perf_counter
.name
,
208 LTTNG_SYMBOL_NAME_LEN
);
209 kctx
->ctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
211 if (*channel_name
== '\0') {
212 ret
= add_kctx_all_channels(ksession
, kctx
);
213 if (ret
!= LTTNG_OK
) {
217 /* Get kernel channel */
218 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
220 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
224 ret
= add_kctx_to_channel(kctx
, kchan
);
225 if (ret
!= LTTNG_OK
) {
234 trace_kernel_destroy_context(kctx
);
240 * Add UST context to tracer.
242 int context_ust_add(struct ltt_ust_session
*usess
, int domain
,
243 struct lttng_event_context
*ctx
, char *channel_name
)
246 struct lttng_ht_iter iter
;
247 struct lttng_ht
*chan_ht
;
248 struct ltt_ust_channel
*uchan
= NULL
;
252 assert(channel_name
);
257 * Define which channel's hashtable to use from the domain or quit if
261 case LTTNG_DOMAIN_UST
:
262 chan_ht
= usess
->domain_global
.channels
;
269 /* Get UST channel if defined */
270 if (channel_name
[0] != '\0') {
271 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
273 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
279 /* Add ctx to channel */
280 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
283 /* Add ctx all events, all channels */
284 cds_lfht_for_each_entry(chan_ht
->ht
, &iter
.iter
, uchan
, node
.node
) {
285 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
287 ERR("Failed to add context to channel %s",
297 ret
= LTTNG_ERR_UST_CONTEXT_EXIST
;
300 ret
= LTTNG_ERR_FATAL
;
303 ret
= LTTNG_ERR_UST_CONTEXT_INVAL
;
306 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;