| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by the Free |
| 6 | * Software Foundation; only version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 15 | * Place - Suite 330, Boston, MA 02111-1307, USA. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <unistd.h> |
| 23 | #include <urcu/list.h> |
| 24 | |
| 25 | #include <lttng-sessiond-comm.h> |
| 26 | #include <lttngerr.h> |
| 27 | |
| 28 | #include "context.h" |
| 29 | #include "hashtable.h" |
| 30 | #include "kernel.h" |
| 31 | #include "ust-app.h" |
| 32 | #include "trace-ust.h" |
| 33 | |
| 34 | /* |
| 35 | * Add kernel context to an event of a specific channel. |
| 36 | */ |
| 37 | static int add_kctx_to_event(struct lttng_kernel_context *kctx, |
| 38 | struct ltt_kernel_channel *kchan, char *event_name) |
| 39 | { |
| 40 | int ret, found = 0; |
| 41 | struct ltt_kernel_event *kevent; |
| 42 | |
| 43 | DBG("Add kernel context to event %s", event_name); |
| 44 | |
| 45 | kevent = trace_kernel_get_event_by_name(event_name, kchan); |
| 46 | if (kevent != NULL) { |
| 47 | ret = kernel_add_event_context(kevent, kctx); |
| 48 | if (ret < 0) { |
| 49 | goto error; |
| 50 | } |
| 51 | found = 1; |
| 52 | } |
| 53 | |
| 54 | ret = found; |
| 55 | |
| 56 | error: |
| 57 | return ret; |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Add kernel context to all channel. |
| 62 | * |
| 63 | * If event_name is specified, add context to event instead. |
| 64 | */ |
| 65 | static int add_kctx_all_channels(struct ltt_kernel_session *ksession, |
| 66 | struct lttng_kernel_context *kctx, char *event_name) |
| 67 | { |
| 68 | int ret, no_event = 0, found = 0; |
| 69 | struct ltt_kernel_channel *kchan; |
| 70 | |
| 71 | if (strlen(event_name) == 0) { |
| 72 | no_event = 1; |
| 73 | } |
| 74 | |
| 75 | DBG("Adding kernel context to all channels (event: %s)", event_name); |
| 76 | |
| 77 | /* Go over all channels */ |
| 78 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { |
| 79 | if (no_event) { |
| 80 | ret = kernel_add_channel_context(kchan, kctx); |
| 81 | if (ret < 0) { |
| 82 | ret = LTTCOMM_KERN_CONTEXT_FAIL; |
| 83 | goto error; |
| 84 | } |
| 85 | } else { |
| 86 | ret = add_kctx_to_event(kctx, kchan, event_name); |
| 87 | if (ret < 0) { |
| 88 | ret = LTTCOMM_KERN_CONTEXT_FAIL; |
| 89 | goto error; |
| 90 | } else if (ret == 1) { |
| 91 | /* Event found and context added */ |
| 92 | found = 1; |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if (!found && !no_event) { |
| 99 | ret = LTTCOMM_NO_EVENT; |
| 100 | goto error; |
| 101 | } |
| 102 | |
| 103 | ret = LTTCOMM_OK; |
| 104 | |
| 105 | error: |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Add kernel context to a specific channel. |
| 111 | * |
| 112 | * If event_name is specified, add context to that event. |
| 113 | */ |
| 114 | static int add_kctx_to_channel(struct lttng_kernel_context *kctx, |
| 115 | struct ltt_kernel_channel *kchan, char *event_name) |
| 116 | { |
| 117 | int ret, no_event = 0, found = 0; |
| 118 | |
| 119 | if (strlen(event_name) == 0) { |
| 120 | no_event = 1; |
| 121 | } |
| 122 | |
| 123 | DBG("Add kernel context to channel '%s', event '%s'", |
| 124 | kchan->channel->name, event_name); |
| 125 | |
| 126 | if (no_event) { |
| 127 | ret = kernel_add_channel_context(kchan, kctx); |
| 128 | if (ret < 0) { |
| 129 | ret = LTTCOMM_KERN_CONTEXT_FAIL; |
| 130 | goto error; |
| 131 | } |
| 132 | } else { |
| 133 | ret = add_kctx_to_event(kctx, kchan, event_name); |
| 134 | if (ret < 0) { |
| 135 | ret = LTTCOMM_KERN_CONTEXT_FAIL; |
| 136 | goto error; |
| 137 | } else if (ret == 1) { |
| 138 | /* Event found and context added */ |
| 139 | found = 1; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (!found && !no_event) { |
| 144 | ret = LTTCOMM_NO_EVENT; |
| 145 | goto error; |
| 146 | } |
| 147 | |
| 148 | ret = LTTCOMM_OK; |
| 149 | |
| 150 | error: |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Add kernel context to tracer. |
| 156 | */ |
| 157 | int context_kernel_add(struct ltt_kernel_session *ksession, |
| 158 | struct lttng_event_context *ctx, char *event_name, |
| 159 | char *channel_name) |
| 160 | { |
| 161 | int ret; |
| 162 | struct ltt_kernel_channel *kchan; |
| 163 | struct lttng_kernel_context kctx; |
| 164 | |
| 165 | /* Setup kernel context structure */ |
| 166 | kctx.ctx = ctx->ctx; |
| 167 | kctx.u.perf_counter.type = ctx->u.perf_counter.type; |
| 168 | kctx.u.perf_counter.config = ctx->u.perf_counter.config; |
| 169 | strncpy(kctx.u.perf_counter.name, ctx->u.perf_counter.name, |
| 170 | LTTNG_SYMBOL_NAME_LEN); |
| 171 | kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 172 | |
| 173 | if (strlen(channel_name) == 0) { |
| 174 | ret = add_kctx_all_channels(ksession, &kctx, event_name); |
| 175 | if (ret != LTTCOMM_OK) { |
| 176 | goto error; |
| 177 | } |
| 178 | } else { |
| 179 | /* Get kernel channel */ |
| 180 | kchan = trace_kernel_get_channel_by_name(channel_name, ksession); |
| 181 | if (kchan == NULL) { |
| 182 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 183 | goto error; |
| 184 | } |
| 185 | |
| 186 | ret = add_kctx_to_channel(&kctx, kchan, event_name); |
| 187 | if (ret != LTTCOMM_OK) { |
| 188 | goto error; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | ret = LTTCOMM_OK; |
| 193 | |
| 194 | error: |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Add UST context to tracer. |
| 200 | */ |
| 201 | int context_ust_add(struct ltt_ust_session *usess, int domain, |
| 202 | struct lttng_event_context *ctx, char *event_name, |
| 203 | char *channel_name) |
| 204 | { |
| 205 | int ret = LTTCOMM_OK, have_event = 0; |
| 206 | struct cds_lfht_iter iter, uiter; |
| 207 | struct cds_lfht *chan_ht; |
| 208 | struct ltt_ust_channel *uchan = NULL; |
| 209 | struct ltt_ust_event *uevent = NULL; |
| 210 | struct ltt_ust_context *uctx; |
| 211 | |
| 212 | switch (domain) { |
| 213 | case LTTNG_DOMAIN_UST: |
| 214 | chan_ht = usess->domain_global.channels; |
| 215 | break; |
| 216 | case LTTNG_DOMAIN_UST_EXEC_NAME: |
| 217 | case LTTNG_DOMAIN_UST_PID: |
| 218 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: |
| 219 | default: |
| 220 | ret = LTTCOMM_NOT_IMPLEMENTED; |
| 221 | goto error; |
| 222 | } |
| 223 | |
| 224 | if (strlen(event_name) != 0) { |
| 225 | have_event = 1; |
| 226 | } |
| 227 | |
| 228 | /* Get UST channel if defined */ |
| 229 | if (strlen(channel_name) != 0) { |
| 230 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); |
| 231 | if (uchan == NULL) { |
| 232 | ret = LTTCOMM_UST_CHAN_NOT_FOUND; |
| 233 | goto error; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /* If UST channel specified and event name, get UST event ref */ |
| 238 | if (uchan && have_event) { |
| 239 | uevent = trace_ust_find_event_by_name(uchan->events, event_name); |
| 240 | if (uevent == NULL) { |
| 241 | ret = LTTCOMM_UST_EVENT_NOT_FOUND; |
| 242 | goto error; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /* Create ltt UST context */ |
| 247 | uctx = trace_ust_create_context(ctx); |
| 248 | if (uctx == NULL) { |
| 249 | ret = LTTCOMM_FATAL; |
| 250 | goto error; |
| 251 | } |
| 252 | |
| 253 | /* At this point, we have 4 possibilities */ |
| 254 | |
| 255 | if (uchan && uevent) { /* Add ctx to event in channel */ |
| 256 | ret = ust_app_add_ctx_event_glb(usess, uchan, uevent, uctx); |
| 257 | } else if (uchan && !have_event) { /* Add ctx to channel */ |
| 258 | ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx); |
| 259 | } else if (!uchan && have_event) { /* Add ctx to event */ |
| 260 | cds_lfht_for_each_entry(chan_ht, &iter, uchan, node) { |
| 261 | uevent = trace_ust_find_event_by_name(uchan->events, event_name); |
| 262 | if (uevent != NULL) { |
| 263 | ret = ust_app_add_ctx_event_glb(usess, uchan, uevent, uctx); |
| 264 | /* |
| 265 | * LTTng UST does not allowed the same event to be registered |
| 266 | * multiple time in different or the same channel. So, if we |
| 267 | * found our event in the first place, no need to continue. |
| 268 | */ |
| 269 | goto end; |
| 270 | } |
| 271 | } |
| 272 | ret = LTTCOMM_UST_EVENT_NOT_FOUND; |
| 273 | goto error_free_ctx; |
| 274 | } else if (!uchan && !have_event) { /* Add ctx to all events, all channels */ |
| 275 | cds_lfht_for_each_entry(chan_ht, &iter, uchan, node) { |
| 276 | ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx); |
| 277 | if (ret < 0) { |
| 278 | /* Add failed so uctx was not added. We can keep it. */ |
| 279 | continue; |
| 280 | } |
| 281 | cds_lfht_for_each_entry(uchan->events, &uiter, uevent, node) { |
| 282 | ret = ust_app_add_ctx_event_glb(usess, uchan, uevent, uctx); |
| 283 | if (ret < 0) { |
| 284 | /* Add failed so uctx was not added. We can keep it. */ |
| 285 | continue; |
| 286 | } |
| 287 | /* Create ltt UST context */ |
| 288 | uctx = trace_ust_create_context(ctx); |
| 289 | if (uctx == NULL) { |
| 290 | ret = LTTCOMM_FATAL; |
| 291 | goto error; |
| 292 | } |
| 293 | } |
| 294 | /* Create ltt UST context */ |
| 295 | uctx = trace_ust_create_context(ctx); |
| 296 | if (uctx == NULL) { |
| 297 | ret = LTTCOMM_FATAL; |
| 298 | goto error; |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | end: |
| 304 | switch (ret) { |
| 305 | case -EEXIST: |
| 306 | ret = LTTCOMM_UST_CONTEXT_EXIST; |
| 307 | goto error_free_ctx; |
| 308 | case -ENOMEM: |
| 309 | ret = LTTCOMM_FATAL; |
| 310 | goto error_free_ctx; |
| 311 | } |
| 312 | |
| 313 | return LTTCOMM_OK; |
| 314 | |
| 315 | error_free_ctx: |
| 316 | free(uctx); |
| 317 | error: |
| 318 | return ret; |
| 319 | } |