| 1 | /* |
| 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
| 3 | * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-only |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #define _LGPL_SOURCE |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <unistd.h> |
| 14 | #include <urcu/list.h> |
| 15 | |
| 16 | #include <common/error.h> |
| 17 | #include <common/sessiond-comm/sessiond-comm.h> |
| 18 | |
| 19 | #include "context.h" |
| 20 | #include "kernel.h" |
| 21 | #include "ust-app.h" |
| 22 | #include "trace-ust.h" |
| 23 | #include "agent.h" |
| 24 | |
| 25 | /* |
| 26 | * Add kernel context to all channel. |
| 27 | * |
| 28 | * Assumes the ownership of kctx. |
| 29 | */ |
| 30 | static int add_kctx_all_channels(struct ltt_kernel_session *ksession, |
| 31 | struct ltt_kernel_context *kctx) |
| 32 | { |
| 33 | int ret; |
| 34 | struct ltt_kernel_channel *kchan; |
| 35 | |
| 36 | assert(ksession); |
| 37 | assert(kctx); |
| 38 | |
| 39 | DBG("Adding kernel context to all channels"); |
| 40 | |
| 41 | /* Go over all channels */ |
| 42 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { |
| 43 | struct ltt_kernel_context *kctx_copy; |
| 44 | |
| 45 | kctx_copy = trace_kernel_copy_context(kctx); |
| 46 | if (!kctx_copy) { |
| 47 | PERROR("zmalloc ltt_kernel_context"); |
| 48 | ret = -LTTNG_ERR_NOMEM; |
| 49 | goto error; |
| 50 | } |
| 51 | |
| 52 | /* Ownership of kctx_copy is transferred to the callee. */ |
| 53 | ret = kernel_add_channel_context(kchan, kctx_copy); |
| 54 | kctx_copy = NULL; |
| 55 | if (ret != 0) { |
| 56 | goto error; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | ret = LTTNG_OK; |
| 61 | |
| 62 | error: |
| 63 | trace_kernel_destroy_context(kctx); |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Add kernel context to a specific channel. |
| 69 | * |
| 70 | * Assumes the ownership of kctx. |
| 71 | */ |
| 72 | static int add_kctx_to_channel(struct ltt_kernel_context *kctx, |
| 73 | struct ltt_kernel_channel *kchan) |
| 74 | { |
| 75 | int ret; |
| 76 | |
| 77 | assert(kchan); |
| 78 | assert(kctx); |
| 79 | |
| 80 | DBG("Add kernel context to channel '%s'", kchan->channel->name); |
| 81 | |
| 82 | /* Ownership of kctx is transferred to the callee. */ |
| 83 | ret = kernel_add_channel_context(kchan, kctx); |
| 84 | kctx = NULL; |
| 85 | if (ret != 0) { |
| 86 | goto error; |
| 87 | } |
| 88 | |
| 89 | ret = LTTNG_OK; |
| 90 | |
| 91 | error: |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Add UST context to channel. |
| 97 | */ |
| 98 | static int add_uctx_to_channel(struct ltt_ust_session *usess, |
| 99 | enum lttng_domain_type domain, |
| 100 | struct ltt_ust_channel *uchan, |
| 101 | const struct lttng_event_context *ctx) |
| 102 | { |
| 103 | int ret; |
| 104 | struct ltt_ust_context *uctx = NULL; |
| 105 | |
| 106 | assert(usess); |
| 107 | assert(uchan); |
| 108 | assert(ctx); |
| 109 | |
| 110 | /* Check if context is duplicate */ |
| 111 | cds_list_for_each_entry(uctx, &uchan->ctx_list, list) { |
| 112 | if (trace_ust_match_context(uctx, ctx)) { |
| 113 | ret = LTTNG_ERR_UST_CONTEXT_EXIST; |
| 114 | goto duplicate; |
| 115 | } |
| 116 | } |
| 117 | uctx = NULL; |
| 118 | |
| 119 | switch (domain) { |
| 120 | case LTTNG_DOMAIN_JUL: |
| 121 | case LTTNG_DOMAIN_LOG4J: |
| 122 | { |
| 123 | struct agent *agt; |
| 124 | |
| 125 | if (ctx->ctx != LTTNG_EVENT_CONTEXT_APP_CONTEXT) { |
| 126 | /* Other contexts are not needed by the agent. */ |
| 127 | break; |
| 128 | } |
| 129 | agt = trace_ust_find_agent(usess, domain); |
| 130 | |
| 131 | if (!agt) { |
| 132 | agt = agent_create(domain); |
| 133 | if (!agt) { |
| 134 | ret = -LTTNG_ERR_NOMEM; |
| 135 | goto error; |
| 136 | } |
| 137 | agent_add(agt, usess->agents); |
| 138 | } |
| 139 | ret = agent_add_context(ctx, agt); |
| 140 | if (ret != LTTNG_OK) { |
| 141 | goto error; |
| 142 | } |
| 143 | |
| 144 | ret = agent_enable_context(ctx, domain); |
| 145 | if (ret != LTTNG_OK) { |
| 146 | goto error; |
| 147 | } |
| 148 | break; |
| 149 | } |
| 150 | case LTTNG_DOMAIN_UST: |
| 151 | break; |
| 152 | default: |
| 153 | assert(0); |
| 154 | } |
| 155 | |
| 156 | /* Create ltt UST context */ |
| 157 | uctx = trace_ust_create_context(ctx); |
| 158 | if (uctx == NULL) { |
| 159 | ret = LTTNG_ERR_UST_CONTEXT_INVAL; |
| 160 | goto error; |
| 161 | } |
| 162 | |
| 163 | /* Add ltt UST context node to ltt UST channel */ |
| 164 | lttng_ht_add_ulong(uchan->ctx, &uctx->node); |
| 165 | cds_list_add_tail(&uctx->list, &uchan->ctx_list); |
| 166 | |
| 167 | if (!usess->active) { |
| 168 | goto end; |
| 169 | } |
| 170 | |
| 171 | ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx); |
| 172 | if (ret < 0) { |
| 173 | goto error; |
| 174 | } |
| 175 | end: |
| 176 | DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name); |
| 177 | |
| 178 | return 0; |
| 179 | |
| 180 | error: |
| 181 | free(uctx); |
| 182 | duplicate: |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Add kernel context to tracer. |
| 188 | */ |
| 189 | int context_kernel_add(struct ltt_kernel_session *ksession, |
| 190 | const struct lttng_event_context *ctx, char *channel_name) |
| 191 | { |
| 192 | int ret; |
| 193 | struct ltt_kernel_channel *kchan; |
| 194 | struct ltt_kernel_context *kctx; |
| 195 | |
| 196 | assert(ksession); |
| 197 | assert(ctx); |
| 198 | assert(channel_name); |
| 199 | |
| 200 | kctx = trace_kernel_create_context(NULL); |
| 201 | if (!kctx) { |
| 202 | ret = -LTTNG_ERR_NOMEM; |
| 203 | goto error; |
| 204 | } |
| 205 | |
| 206 | /* Setup kernel context structure */ |
| 207 | switch (ctx->ctx) { |
| 208 | case LTTNG_EVENT_CONTEXT_PID: |
| 209 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PID; |
| 210 | break; |
| 211 | case LTTNG_EVENT_CONTEXT_PROCNAME: |
| 212 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PROCNAME; |
| 213 | break; |
| 214 | case LTTNG_EVENT_CONTEXT_PRIO: |
| 215 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PRIO; |
| 216 | break; |
| 217 | case LTTNG_EVENT_CONTEXT_NICE: |
| 218 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_NICE; |
| 219 | break; |
| 220 | case LTTNG_EVENT_CONTEXT_VPID: |
| 221 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VPID; |
| 222 | break; |
| 223 | case LTTNG_EVENT_CONTEXT_TID: |
| 224 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_TID; |
| 225 | break; |
| 226 | case LTTNG_EVENT_CONTEXT_VTID: |
| 227 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VTID; |
| 228 | break; |
| 229 | case LTTNG_EVENT_CONTEXT_PPID: |
| 230 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PPID; |
| 231 | break; |
| 232 | case LTTNG_EVENT_CONTEXT_VPPID: |
| 233 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VPPID; |
| 234 | break; |
| 235 | case LTTNG_EVENT_CONTEXT_HOSTNAME: |
| 236 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_HOSTNAME; |
| 237 | break; |
| 238 | case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER: |
| 239 | case LTTNG_EVENT_CONTEXT_PERF_COUNTER: |
| 240 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PERF_CPU_COUNTER; |
| 241 | break; |
| 242 | case LTTNG_EVENT_CONTEXT_INTERRUPTIBLE: |
| 243 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_INTERRUPTIBLE; |
| 244 | break; |
| 245 | case LTTNG_EVENT_CONTEXT_PREEMPTIBLE: |
| 246 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PREEMPTIBLE; |
| 247 | break; |
| 248 | case LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE: |
| 249 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_NEED_RESCHEDULE; |
| 250 | break; |
| 251 | case LTTNG_EVENT_CONTEXT_MIGRATABLE: |
| 252 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_MIGRATABLE; |
| 253 | break; |
| 254 | case LTTNG_EVENT_CONTEXT_CALLSTACK_KERNEL: |
| 255 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_KERNEL; |
| 256 | break; |
| 257 | case LTTNG_EVENT_CONTEXT_CALLSTACK_USER: |
| 258 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_USER; |
| 259 | break; |
| 260 | case LTTNG_EVENT_CONTEXT_CGROUP_NS: |
| 261 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_CGROUP_NS; |
| 262 | break; |
| 263 | case LTTNG_EVENT_CONTEXT_IPC_NS: |
| 264 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_IPC_NS; |
| 265 | break; |
| 266 | case LTTNG_EVENT_CONTEXT_MNT_NS: |
| 267 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_MNT_NS; |
| 268 | break; |
| 269 | case LTTNG_EVENT_CONTEXT_NET_NS: |
| 270 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_NET_NS; |
| 271 | break; |
| 272 | case LTTNG_EVENT_CONTEXT_PID_NS: |
| 273 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PID_NS; |
| 274 | break; |
| 275 | case LTTNG_EVENT_CONTEXT_TIME_NS: |
| 276 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_TIME_NS; |
| 277 | break; |
| 278 | case LTTNG_EVENT_CONTEXT_USER_NS: |
| 279 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_USER_NS; |
| 280 | break; |
| 281 | case LTTNG_EVENT_CONTEXT_UTS_NS: |
| 282 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_UTS_NS; |
| 283 | break; |
| 284 | case LTTNG_EVENT_CONTEXT_UID: |
| 285 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_UID; |
| 286 | break; |
| 287 | case LTTNG_EVENT_CONTEXT_EUID: |
| 288 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_EUID; |
| 289 | break; |
| 290 | case LTTNG_EVENT_CONTEXT_SUID: |
| 291 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_SUID; |
| 292 | break; |
| 293 | case LTTNG_EVENT_CONTEXT_GID: |
| 294 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_GID; |
| 295 | break; |
| 296 | case LTTNG_EVENT_CONTEXT_EGID: |
| 297 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_EGID; |
| 298 | break; |
| 299 | case LTTNG_EVENT_CONTEXT_SGID: |
| 300 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_SGID; |
| 301 | break; |
| 302 | case LTTNG_EVENT_CONTEXT_VUID: |
| 303 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VUID; |
| 304 | break; |
| 305 | case LTTNG_EVENT_CONTEXT_VEUID: |
| 306 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VEUID; |
| 307 | break; |
| 308 | case LTTNG_EVENT_CONTEXT_VSUID: |
| 309 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VSUID; |
| 310 | break; |
| 311 | case LTTNG_EVENT_CONTEXT_VGID: |
| 312 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VGID; |
| 313 | break; |
| 314 | case LTTNG_EVENT_CONTEXT_VEGID: |
| 315 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VEGID; |
| 316 | break; |
| 317 | case LTTNG_EVENT_CONTEXT_VSGID: |
| 318 | kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VSGID; |
| 319 | break; |
| 320 | default: |
| 321 | ret = LTTNG_ERR_KERN_CONTEXT_FAIL; |
| 322 | goto error; |
| 323 | } |
| 324 | |
| 325 | kctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type; |
| 326 | kctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config; |
| 327 | strncpy(kctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name, |
| 328 | LTTNG_SYMBOL_NAME_LEN); |
| 329 | kctx->ctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 330 | |
| 331 | if (*channel_name == '\0') { |
| 332 | ret = add_kctx_all_channels(ksession, kctx); |
| 333 | /* Ownership of kctx is transferred to the callee. */ |
| 334 | kctx = NULL; |
| 335 | if (ret != LTTNG_OK) { |
| 336 | goto error; |
| 337 | } |
| 338 | } else { |
| 339 | /* Get kernel channel */ |
| 340 | kchan = trace_kernel_get_channel_by_name(channel_name, ksession); |
| 341 | if (kchan == NULL) { |
| 342 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
| 343 | goto error; |
| 344 | } |
| 345 | |
| 346 | ret = add_kctx_to_channel(kctx, kchan); |
| 347 | /* Ownership of kctx is transferred to the callee. */ |
| 348 | kctx = NULL; |
| 349 | if (ret != LTTNG_OK) { |
| 350 | goto error; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | ret = LTTNG_OK; |
| 355 | |
| 356 | error: |
| 357 | if (kctx) { |
| 358 | trace_kernel_destroy_context(kctx); |
| 359 | } |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Add UST context to tracer. |
| 365 | */ |
| 366 | int context_ust_add(struct ltt_ust_session *usess, |
| 367 | enum lttng_domain_type domain, |
| 368 | const struct lttng_event_context *ctx, |
| 369 | char *channel_name) |
| 370 | { |
| 371 | int ret = LTTNG_OK; |
| 372 | struct lttng_ht_iter iter; |
| 373 | struct lttng_ht *chan_ht; |
| 374 | struct ltt_ust_channel *uchan = NULL; |
| 375 | |
| 376 | assert(usess); |
| 377 | assert(ctx); |
| 378 | assert(channel_name); |
| 379 | |
| 380 | rcu_read_lock(); |
| 381 | |
| 382 | chan_ht = usess->domain_global.channels; |
| 383 | |
| 384 | /* Get UST channel if defined */ |
| 385 | if (channel_name[0] != '\0') { |
| 386 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); |
| 387 | if (uchan == NULL) { |
| 388 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
| 389 | goto error; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | if (uchan) { |
| 394 | /* Add ctx to channel */ |
| 395 | ret = add_uctx_to_channel(usess, domain, uchan, ctx); |
| 396 | } else { |
| 397 | rcu_read_lock(); |
| 398 | /* Add ctx all events, all channels */ |
| 399 | cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) { |
| 400 | ret = add_uctx_to_channel(usess, domain, uchan, ctx); |
| 401 | if (ret) { |
| 402 | ERR("Failed to add context to channel %s", |
| 403 | uchan->name); |
| 404 | continue; |
| 405 | } |
| 406 | } |
| 407 | rcu_read_unlock(); |
| 408 | } |
| 409 | |
| 410 | switch (ret) { |
| 411 | case LTTNG_ERR_UST_CONTEXT_EXIST: |
| 412 | break; |
| 413 | case -ENOMEM: |
| 414 | case -LTTNG_ERR_NOMEM: |
| 415 | ret = LTTNG_ERR_FATAL; |
| 416 | break; |
| 417 | case -EINVAL: |
| 418 | ret = LTTNG_ERR_UST_CONTEXT_INVAL; |
| 419 | break; |
| 420 | case -ENOSYS: |
| 421 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
| 422 | break; |
| 423 | default: |
| 424 | if (ret != 0 && ret != LTTNG_OK) { |
| 425 | ret = ret > 0 ? ret : LTTNG_ERR_UNK; |
| 426 | } else { |
| 427 | ret = LTTNG_OK; |
| 428 | } |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | error: |
| 433 | rcu_read_unlock(); |
| 434 | return ret; |
| 435 | } |