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 lttng_kernel_context
*kctx
)
40 struct ltt_kernel_channel
*kchan
;
42 DBG("Adding kernel context to all channels");
44 /* Go over all channels */
45 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
46 ret
= kernel_add_channel_context(kchan
, kctx
);
48 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
60 * Add kernel context to a specific channel.
62 static int add_kctx_to_channel(struct lttng_kernel_context
*kctx
,
63 struct ltt_kernel_channel
*kchan
)
67 DBG("Add kernel context to channel '%s'", kchan
->channel
->name
);
69 ret
= kernel_add_channel_context(kchan
, kctx
);
71 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
82 * Add UST context to channel.
84 static int add_uctx_to_channel(struct ltt_ust_session
*usess
, int domain
,
85 struct ltt_ust_channel
*uchan
, struct lttng_event_context
*ctx
)
88 struct ltt_ust_context
*uctx
;
89 struct lttng_ht_iter iter
;
90 struct lttng_ht_node_ulong
*uctx_node
;
92 /* Create ltt UST context */
93 uctx
= trace_ust_create_context(ctx
);
100 case LTTNG_DOMAIN_UST
:
101 ret
= ust_app_add_ctx_channel_glb(usess
, uchan
, uctx
);
113 /* Lookup context before adding it */
114 lttng_ht_lookup(uchan
->ctx
, (void *)((unsigned long)uctx
->ctx
.ctx
), &iter
);
115 uctx_node
= lttng_ht_iter_get_node_ulong(&iter
);
116 if (uctx_node
!= NULL
) {
122 /* Add ltt UST context node to ltt UST channel */
123 lttng_ht_add_unique_ulong(uchan
->ctx
, &uctx
->node
);
126 DBG("Context UST %d added to channel %s", uctx
->ctx
.ctx
, uchan
->name
);
136 * Add kernel context to tracer.
138 int context_kernel_add(struct ltt_kernel_session
*ksession
,
139 struct lttng_event_context
*ctx
, char *channel_name
)
142 struct ltt_kernel_channel
*kchan
;
143 struct lttng_kernel_context kctx
;
145 /* Setup kernel context structure */
147 case LTTNG_EVENT_CONTEXT_PID
:
148 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_PID
;
150 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
151 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_PERF_COUNTER
;
153 case LTTNG_EVENT_CONTEXT_PROCNAME
:
154 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_PROCNAME
;
156 case LTTNG_EVENT_CONTEXT_PRIO
:
157 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_PRIO
;
159 case LTTNG_EVENT_CONTEXT_NICE
:
160 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_NICE
;
162 case LTTNG_EVENT_CONTEXT_VPID
:
163 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_VPID
;
165 case LTTNG_EVENT_CONTEXT_TID
:
166 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_TID
;
168 case LTTNG_EVENT_CONTEXT_VTID
:
169 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_VTID
;
171 case LTTNG_EVENT_CONTEXT_PPID
:
172 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_PPID
;
174 case LTTNG_EVENT_CONTEXT_VPPID
:
175 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_VPPID
;
177 case LTTNG_EVENT_CONTEXT_HOSTNAME
:
178 kctx
.ctx
= LTTNG_KERNEL_CONTEXT_HOSTNAME
;
181 return LTTNG_ERR_KERN_CONTEXT_FAIL
;
184 kctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
185 kctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
186 strncpy(kctx
.u
.perf_counter
.name
, ctx
->u
.perf_counter
.name
,
187 LTTNG_SYMBOL_NAME_LEN
);
188 kctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
190 if (strlen(channel_name
) == 0) {
191 ret
= add_kctx_all_channels(ksession
, &kctx
);
192 if (ret
!= LTTNG_OK
) {
196 /* Get kernel channel */
197 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
199 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
203 ret
= add_kctx_to_channel(&kctx
, kchan
);
204 if (ret
!= LTTNG_OK
) {
216 * Add UST context to tracer.
218 int context_ust_add(struct ltt_ust_session
*usess
, int domain
,
219 struct lttng_event_context
*ctx
, char *channel_name
)
222 struct lttng_ht_iter iter
;
223 struct lttng_ht
*chan_ht
;
224 struct ltt_ust_channel
*uchan
= NULL
;
227 * Define which channel's hashtable to use from the domain or quit if
231 case LTTNG_DOMAIN_UST
:
232 chan_ht
= usess
->domain_global
.channels
;
235 case LTTNG_DOMAIN_UST_EXEC_NAME
:
236 case LTTNG_DOMAIN_UST_PID
:
237 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
244 /* Get UST channel if defined */
245 if (strlen(channel_name
) != 0) {
246 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
248 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
254 /* Add ctx to channel */
255 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
258 /* Add ctx all events, all channels */
259 cds_lfht_for_each_entry(chan_ht
->ht
, &iter
.iter
, uchan
, node
.node
) {
260 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
262 ERR("Context failed for channel %s", uchan
->name
);
271 ret
= LTTNG_ERR_UST_CONTEXT_EXIST
;
274 ret
= LTTNG_ERR_FATAL
;
277 ret
= LTTNG_ERR_UST_CONTEXT_INVAL
;
280 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;