| 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 "kernel-ctl.h" |
| 30 | |
| 31 | /* |
| 32 | * Add kernel context to an event of a specific channel. |
| 33 | */ |
| 34 | static int add_kctx_to_event(struct lttng_kernel_context *kctx, |
| 35 | struct ltt_kernel_channel *kchan, char *event_name) |
| 36 | { |
| 37 | int ret, found = 0; |
| 38 | struct ltt_kernel_event *kevent; |
| 39 | |
| 40 | DBG("Add kernel context to event %s", event_name); |
| 41 | |
| 42 | kevent = trace_kernel_get_event_by_name(event_name, kchan); |
| 43 | if (kevent != NULL) { |
| 44 | ret = kernel_add_event_context(kevent, kctx); |
| 45 | if (ret < 0) { |
| 46 | goto error; |
| 47 | } |
| 48 | found = 1; |
| 49 | } |
| 50 | |
| 51 | ret = found; |
| 52 | |
| 53 | error: |
| 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Add kernel context to all channel. |
| 59 | * |
| 60 | * If event_name is specified, add context to event instead. |
| 61 | */ |
| 62 | static int add_kctx_all_channels(struct ltt_kernel_session *ksession, |
| 63 | struct lttng_kernel_context *kctx, char *event_name) |
| 64 | { |
| 65 | int ret, no_event = 0, found = 0; |
| 66 | struct ltt_kernel_channel *kchan; |
| 67 | |
| 68 | if (strlen(event_name) == 0) { |
| 69 | no_event = 1; |
| 70 | } |
| 71 | |
| 72 | DBG("Adding kernel context to all channels (event: %s)", event_name); |
| 73 | |
| 74 | /* Go over all channels */ |
| 75 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { |
| 76 | if (no_event) { |
| 77 | ret = kernel_add_channel_context(kchan, kctx); |
| 78 | if (ret < 0) { |
| 79 | ret = LTTCOMM_KERN_CONTEXT_FAIL; |
| 80 | goto error; |
| 81 | } |
| 82 | } else { |
| 83 | ret = add_kctx_to_event(kctx, kchan, event_name); |
| 84 | if (ret < 0) { |
| 85 | ret = LTTCOMM_KERN_CONTEXT_FAIL; |
| 86 | goto error; |
| 87 | } else if (ret == 1) { |
| 88 | /* Event found and context added */ |
| 89 | found = 1; |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (!found && !no_event) { |
| 96 | ret = LTTCOMM_NO_EVENT; |
| 97 | goto error; |
| 98 | } |
| 99 | |
| 100 | ret = LTTCOMM_OK; |
| 101 | |
| 102 | error: |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Add kernel context to a specific channel. |
| 108 | * |
| 109 | * If event_name is specified, add context to that event. |
| 110 | */ |
| 111 | static int add_kctx_to_channel(struct lttng_kernel_context *kctx, |
| 112 | struct ltt_kernel_channel *kchan, char *event_name) |
| 113 | { |
| 114 | int ret, no_event = 0, found = 0; |
| 115 | |
| 116 | if (strlen(event_name) == 0) { |
| 117 | no_event = 1; |
| 118 | } |
| 119 | |
| 120 | DBG("Add kernel context to channel '%s', event '%s'", |
| 121 | kchan->channel->name, event_name); |
| 122 | |
| 123 | if (no_event) { |
| 124 | ret = kernel_add_channel_context(kchan, kctx); |
| 125 | if (ret < 0) { |
| 126 | ret = LTTCOMM_KERN_CONTEXT_FAIL; |
| 127 | goto error; |
| 128 | } |
| 129 | } else { |
| 130 | ret = add_kctx_to_event(kctx, kchan, event_name); |
| 131 | if (ret < 0) { |
| 132 | ret = LTTCOMM_KERN_CONTEXT_FAIL; |
| 133 | goto error; |
| 134 | } else if (ret == 1) { |
| 135 | /* Event found and context added */ |
| 136 | found = 1; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (!found && !no_event) { |
| 141 | ret = LTTCOMM_NO_EVENT; |
| 142 | goto error; |
| 143 | } |
| 144 | |
| 145 | ret = LTTCOMM_OK; |
| 146 | |
| 147 | error: |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Add kernel context to tracer. |
| 153 | */ |
| 154 | int context_kernel_add(struct ltt_kernel_session *ksession, |
| 155 | struct lttng_event_context *ctx, char *event_name, |
| 156 | char *channel_name) |
| 157 | { |
| 158 | int ret; |
| 159 | struct ltt_kernel_channel *kchan; |
| 160 | struct lttng_kernel_context kctx; |
| 161 | |
| 162 | /* Setup kernel context structure */ |
| 163 | kctx.ctx = ctx->ctx; |
| 164 | kctx.u.perf_counter.type = ctx->u.perf_counter.type; |
| 165 | kctx.u.perf_counter.config = ctx->u.perf_counter.config; |
| 166 | strncpy(kctx.u.perf_counter.name, ctx->u.perf_counter.name, |
| 167 | LTTNG_SYMBOL_NAME_LEN); |
| 168 | kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
| 169 | |
| 170 | if (strlen(channel_name) == 0) { |
| 171 | ret = add_kctx_all_channels(ksession, &kctx, event_name); |
| 172 | if (ret != LTTCOMM_OK) { |
| 173 | goto error; |
| 174 | } |
| 175 | } else { |
| 176 | /* Get kernel channel */ |
| 177 | kchan = trace_kernel_get_channel_by_name(channel_name, ksession); |
| 178 | if (kchan == NULL) { |
| 179 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 180 | goto error; |
| 181 | } |
| 182 | |
| 183 | ret = add_kctx_to_channel(&kctx, kchan, event_name); |
| 184 | if (ret != LTTCOMM_OK) { |
| 185 | goto error; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | ret = LTTCOMM_OK; |
| 190 | |
| 191 | error: |
| 192 | return ret; |
| 193 | } |