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.
24 #include <urcu/list.h>
26 #include <common/error.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
32 #include "trace-ust.h"
35 * Add kernel context to all channel.
37 static int add_kctx_all_channels(struct ltt_kernel_session
*ksession
,
38 struct ltt_kernel_context
*kctx
)
41 struct ltt_kernel_channel
*kchan
;
46 DBG("Adding kernel context to all channels");
48 /* Go over all channels */
49 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
50 ret
= kernel_add_channel_context(kchan
, kctx
);
52 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
64 * Add kernel context to a specific channel.
66 static int add_kctx_to_channel(struct ltt_kernel_context
*kctx
,
67 struct ltt_kernel_channel
*kchan
)
74 DBG("Add kernel context to channel '%s'", kchan
->channel
->name
);
76 ret
= kernel_add_channel_context(kchan
, kctx
);
78 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
89 * Add UST context to channel.
91 static int add_uctx_to_channel(struct ltt_ust_session
*usess
, int domain
,
92 struct ltt_ust_channel
*uchan
, struct lttng_event_context
*ctx
)
95 struct ltt_ust_context
*uctx
;
101 /* Check if context is duplicate */
102 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
103 if (trace_ust_match_context(uctx
, ctx
)) {
109 /* Create ltt UST context */
110 uctx
= trace_ust_create_context(ctx
);
117 case LTTNG_DOMAIN_UST
:
118 ret
= ust_app_add_ctx_channel_glb(usess
, uchan
, uctx
);
130 /* Add ltt UST context node to ltt UST channel */
131 lttng_ht_add_ulong(uchan
->ctx
, &uctx
->node
);
133 cds_list_add_tail(&uctx
->list
, &uchan
->ctx_list
);
135 DBG("Context UST %d added to channel %s", uctx
->ctx
.ctx
, uchan
->name
);
146 * Add kernel context to tracer.
148 int context_kernel_add(struct ltt_kernel_session
*ksession
,
149 struct lttng_event_context
*ctx
, char *channel_name
)
152 struct ltt_kernel_channel
*kchan
;
153 struct ltt_kernel_context
*kctx
;
157 assert(channel_name
);
159 kctx
= trace_kernel_create_context(NULL
);
161 ret
= LTTNG_ERR_NOMEM
;
165 /* Setup kernel context structure */
167 case LTTNG_EVENT_CONTEXT_PID
:
168 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PID
;
170 case LTTNG_EVENT_CONTEXT_PROCNAME
:
171 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PROCNAME
;
173 case LTTNG_EVENT_CONTEXT_PRIO
:
174 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PRIO
;
176 case LTTNG_EVENT_CONTEXT_NICE
:
177 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_NICE
;
179 case LTTNG_EVENT_CONTEXT_VPID
:
180 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_VPID
;
182 case LTTNG_EVENT_CONTEXT_TID
:
183 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_TID
;
185 case LTTNG_EVENT_CONTEXT_VTID
:
186 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_VTID
;
188 case LTTNG_EVENT_CONTEXT_PPID
:
189 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PPID
;
191 case LTTNG_EVENT_CONTEXT_VPPID
:
192 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_VPPID
;
194 case LTTNG_EVENT_CONTEXT_HOSTNAME
:
195 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_HOSTNAME
;
197 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
198 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
199 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
;
202 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
206 kctx
->ctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
207 kctx
->ctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
208 strncpy(kctx
->ctx
.u
.perf_counter
.name
, ctx
->u
.perf_counter
.name
,
209 LTTNG_SYMBOL_NAME_LEN
);
210 kctx
->ctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
212 if (*channel_name
== '\0') {
213 ret
= add_kctx_all_channels(ksession
, kctx
);
214 if (ret
!= LTTNG_OK
) {
218 /* Get kernel channel */
219 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
221 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
225 ret
= add_kctx_to_channel(kctx
, kchan
);
226 if (ret
!= LTTNG_OK
) {
235 trace_kernel_destroy_context(kctx
);
241 * Add UST context to tracer.
243 int context_ust_add(struct ltt_ust_session
*usess
, int domain
,
244 struct lttng_event_context
*ctx
, char *channel_name
)
247 struct lttng_ht_iter iter
;
248 struct lttng_ht
*chan_ht
;
249 struct ltt_ust_channel
*uchan
= NULL
;
253 assert(channel_name
);
258 * Define which channel's hashtable to use from the domain or quit if
262 case LTTNG_DOMAIN_UST
:
263 chan_ht
= usess
->domain_global
.channels
;
270 /* Get UST channel if defined */
271 if (channel_name
[0] != '\0') {
272 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
274 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
280 /* Add ctx to channel */
281 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
284 /* Add ctx all events, all channels */
285 cds_lfht_for_each_entry(chan_ht
->ht
, &iter
.iter
, uchan
, node
.node
) {
286 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
288 ERR("Context failed for channel %s", uchan
->name
);
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
;