| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; only version 2 |
| 7 | * of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | #include <urcu/list.h> |
| 25 | |
| 26 | #include "lttngerr.h" |
| 27 | #include "context.h" |
| 28 | |
| 29 | /* |
| 30 | * Add kernel context to an event of a specific channel. |
| 31 | */ |
| 32 | static int add_kctx_to_event(struct lttng_kernel_context *kctx, |
| 33 | struct ltt_kernel_channel *kchan, char *event_name) |
| 34 | { |
| 35 | int ret, found = 0; |
| 36 | struct ltt_kernel_event *kevent; |
| 37 | |
| 38 | DBG("Add kernel context to event %s", event_name); |
| 39 | |
| 40 | kevent = get_kernel_event_by_name(event_name, kchan); |
| 41 | if (kevent != NULL) { |
| 42 | ret = kernel_add_event_context(kevent, kctx); |
| 43 | if (ret < 0) { |
| 44 | goto error; |
| 45 | } |
| 46 | found = 1; |
| 47 | } |
| 48 | |
| 49 | ret = found; |
| 50 | |
| 51 | error: |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Add kernel context to all channel. |
| 57 | * |
| 58 | * If event_name is specified, add context to event instead. |
| 59 | */ |
| 60 | static int add_kctx_all_channels(struct ltt_kernel_session *ksession, |
| 61 | struct lttng_kernel_context *kctx, char *event_name) |
| 62 | { |
| 63 | int ret, no_event = 0, found = 0; |
| 64 | struct ltt_kernel_channel *kchan; |
| 65 | |
| 66 | if (strlen(event_name) == 0) { |
| 67 | no_event = 1; |
| 68 | } |
| 69 | |
| 70 | DBG("Adding kernel context to all channels (event: %s)", event_name); |
| 71 | |
| 72 | /* Go over all channels */ |
| 73 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { |
| 74 | if (no_event) { |
| 75 | ret = kernel_add_channel_context(kchan, kctx); |
| 76 | if (ret < 0) { |
| 77 | ret = LTTCOMM_KERN_CONTEXT_FAIL; |
| 78 | goto error; |
| 79 | } |
| 80 | } else { |
| 81 | ret = add_kctx_to_event(kctx, kchan, event_name); |
| 82 | if (ret < 0) { |
| 83 | ret = LTTCOMM_KERN_CONTEXT_FAIL; |
| 84 | goto error; |
| 85 | } else if (ret == 1) { |
| 86 | /* Event found and context added */ |
| 87 | found = 1; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (!found && !no_event) { |
| 94 | ret = LTTCOMM_NO_EVENT; |
| 95 | goto error; |
| 96 | } |
| 97 | |
| 98 | ret = LTTCOMM_OK; |
| 99 | |
| 100 | error: |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Add kernel context to a specific channel. |
| 106 | * |
| 107 | * If event_name is specified, add context to that event. |
| 108 | */ |
| 109 | static int add_kctx_to_channel(struct lttng_kernel_context *kctx, |
| 110 | struct ltt_kernel_channel *kchan, char *event_name) |
| 111 | { |
| 112 | int ret, no_event = 0, found = 0; |
| 113 | |
| 114 | if (strlen(event_name) == 0) { |
| 115 | no_event = 1; |
| 116 | } |
| 117 | |
| 118 | DBG("Add kernel context to channel '%s', event '%s'", |
| 119 | kchan->channel->name, event_name); |
| 120 | |
| 121 | if (no_event) { |
| 122 | ret = kernel_add_channel_context(kchan, kctx); |
| 123 | if (ret < 0) { |
| 124 | ret = LTTCOMM_KERN_CONTEXT_FAIL; |
| 125 | goto error; |
| 126 | } |
| 127 | } else { |
| 128 | ret = add_kctx_to_event(kctx, kchan, event_name); |
| 129 | if (ret < 0) { |
| 130 | ret = LTTCOMM_KERN_CONTEXT_FAIL; |
| 131 | goto error; |
| 132 | } else if (ret == 1) { |
| 133 | /* Event found and context added */ |
| 134 | found = 1; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (!found && !no_event) { |
| 139 | ret = LTTCOMM_NO_EVENT; |
| 140 | goto error; |
| 141 | } |
| 142 | |
| 143 | ret = LTTCOMM_OK; |
| 144 | |
| 145 | error: |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Add kernel context to tracer. |
| 151 | */ |
| 152 | int add_kernel_context(struct ltt_kernel_session *ksession, |
| 153 | struct lttng_kernel_context *kctx, char *event_name, |
| 154 | char *channel_name) |
| 155 | { |
| 156 | int ret; |
| 157 | struct ltt_kernel_channel *kchan; |
| 158 | |
| 159 | if (strlen(channel_name) == 0) { |
| 160 | ret = add_kctx_all_channels(ksession, kctx, event_name); |
| 161 | if (ret != LTTCOMM_OK) { |
| 162 | goto error; |
| 163 | } |
| 164 | } else { |
| 165 | /* Get kernel channel */ |
| 166 | kchan = get_kernel_channel_by_name(channel_name, ksession); |
| 167 | if (kchan == NULL) { |
| 168 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 169 | goto error; |
| 170 | } |
| 171 | |
| 172 | ret = add_kctx_to_channel(kctx, kchan, event_name); |
| 173 | if (ret != LTTCOMM_OK) { |
| 174 | goto error; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | ret = LTTCOMM_OK; |
| 179 | |
| 180 | error: |
| 181 | return ret; |
| 182 | } |