| 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 | #include <string.h> |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #include <lttng/lttng.h> |
| 22 | #include <lttng-sessiond-comm.h> |
| 23 | #include <lttngerr.h> |
| 24 | |
| 25 | #include "channel.h" |
| 26 | #include "kernel-ctl.h" |
| 27 | #include "ust-ctl.h" |
| 28 | #include "utils.h" |
| 29 | |
| 30 | /* |
| 31 | * Return allocated channel attributes. |
| 32 | */ |
| 33 | static struct lttng_channel *init_default_attr(int dom) |
| 34 | { |
| 35 | struct lttng_channel *chan; |
| 36 | |
| 37 | chan = zmalloc(sizeof(struct lttng_channel)); |
| 38 | if (chan == NULL) { |
| 39 | perror("malloc channel init"); |
| 40 | goto error_alloc; |
| 41 | } |
| 42 | |
| 43 | if (snprintf(chan->name, sizeof(chan->name), "%s", |
| 44 | DEFAULT_CHANNEL_NAME) < 0) { |
| 45 | perror("snprintf default channel name"); |
| 46 | goto error; |
| 47 | } |
| 48 | |
| 49 | chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; |
| 50 | chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; |
| 51 | chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; |
| 52 | |
| 53 | switch (dom) { |
| 54 | case LTTNG_DOMAIN_KERNEL: |
| 55 | chan->attr.subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE; |
| 56 | chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; |
| 57 | chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; |
| 58 | break; |
| 59 | case LTTNG_DOMAIN_UST_PID: |
| 60 | chan->attr.subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE; |
| 61 | chan->attr.num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM; |
| 62 | chan->attr.output = DEFAULT_UST_CHANNEL_OUTPUT; |
| 63 | break; |
| 64 | default: |
| 65 | goto error; /* Not implemented */ |
| 66 | } |
| 67 | |
| 68 | return chan; |
| 69 | |
| 70 | error: |
| 71 | free(chan); |
| 72 | error_alloc: |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Copy two ltt ust channel. Dst and src must be already allocated. |
| 78 | */ |
| 79 | int channel_ust_copy(struct ltt_ust_channel *dst, |
| 80 | struct ltt_ust_channel *src) |
| 81 | { |
| 82 | struct ltt_ust_event *uevent, *new_uevent; |
| 83 | |
| 84 | memcpy(dst, src, sizeof(struct ltt_ust_channel)); |
| 85 | CDS_INIT_LIST_HEAD(&dst->events.head); |
| 86 | |
| 87 | cds_list_for_each_entry(uevent, &src->events.head, list) { |
| 88 | new_uevent = malloc(sizeof(struct ltt_ust_event)); |
| 89 | if (new_uevent == NULL) { |
| 90 | perror("malloc ltt_ust_event"); |
| 91 | goto error; |
| 92 | } |
| 93 | |
| 94 | memcpy(new_uevent, uevent, sizeof(struct ltt_ust_event)); |
| 95 | cds_list_add(&new_uevent->list, &dst->events.head); |
| 96 | dst->events.count++; |
| 97 | } |
| 98 | |
| 99 | return 0; |
| 100 | |
| 101 | error: |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Disable kernel channel of the kernel session. |
| 107 | */ |
| 108 | int channel_kernel_disable(struct ltt_kernel_session *ksession, |
| 109 | char *channel_name) |
| 110 | { |
| 111 | int ret; |
| 112 | struct ltt_kernel_channel *kchan; |
| 113 | |
| 114 | kchan = trace_kernel_get_channel_by_name(channel_name, ksession); |
| 115 | if (kchan == NULL) { |
| 116 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; |
| 117 | goto error; |
| 118 | } else if (kchan->enabled == 1) { |
| 119 | ret = kernel_disable_channel(kchan); |
| 120 | if (ret < 0) { |
| 121 | if (ret != EEXIST) { |
| 122 | ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL; |
| 123 | } |
| 124 | goto error; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | ret = LTTCOMM_OK; |
| 129 | |
| 130 | error: |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Enable kernel channel of the kernel session. |
| 136 | */ |
| 137 | int channel_kernel_enable(struct ltt_kernel_session *ksession, |
| 138 | struct ltt_kernel_channel *kchan) |
| 139 | { |
| 140 | int ret; |
| 141 | |
| 142 | if (kchan->enabled == 0) { |
| 143 | ret = kernel_enable_channel(kchan); |
| 144 | if (ret < 0) { |
| 145 | ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL; |
| 146 | goto error; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | ret = LTTCOMM_OK; |
| 151 | |
| 152 | error: |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Create kernel channel of the kernel session and notify kernel thread. |
| 158 | */ |
| 159 | int channel_kernel_create(struct ltt_kernel_session *ksession, |
| 160 | struct lttng_channel *chan, int kernel_pipe) |
| 161 | { |
| 162 | int ret; |
| 163 | struct lttng_channel *attr = chan; |
| 164 | |
| 165 | /* Creating channel attributes if needed */ |
| 166 | if (attr == NULL) { |
| 167 | attr = init_default_attr(LTTNG_DOMAIN_KERNEL); |
| 168 | if (attr == NULL) { |
| 169 | ret = LTTCOMM_FATAL; |
| 170 | goto error; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /* Channel not found, creating it */ |
| 175 | ret = kernel_create_channel(ksession, attr, ksession->trace_path); |
| 176 | if (ret < 0) { |
| 177 | ret = LTTCOMM_KERN_CHAN_FAIL; |
| 178 | goto error; |
| 179 | } |
| 180 | |
| 181 | /* Notify kernel thread that there is a new channel */ |
| 182 | ret = notify_thread_pipe(kernel_pipe); |
| 183 | if (ret < 0) { |
| 184 | ret = LTTCOMM_FATAL; |
| 185 | goto error; |
| 186 | } |
| 187 | |
| 188 | ret = LTTCOMM_OK; |
| 189 | |
| 190 | error: |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Create UST channel and enable it on the tracer. |
| 196 | */ |
| 197 | int channel_ust_create(struct ltt_ust_session *usession, |
| 198 | struct lttng_channel *chan, int sock) |
| 199 | { |
| 200 | int ret; |
| 201 | struct lttng_channel *attr = chan; |
| 202 | |
| 203 | /* Creating channel attributes if needed */ |
| 204 | if (attr == NULL) { |
| 205 | attr = init_default_attr(LTTNG_DOMAIN_UST_PID); |
| 206 | if (attr == NULL) { |
| 207 | ret = LTTCOMM_FATAL; |
| 208 | goto error; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | ret = ustctl_create_channel(sock, usession, attr); |
| 213 | if (ret < 0) { |
| 214 | ret = LTTCOMM_UST_CHAN_FAIL; |
| 215 | goto error; |
| 216 | } |
| 217 | |
| 218 | DBG2("Channel %s UST create successfully for sock:%d", attr->name, sock); |
| 219 | |
| 220 | ret = LTTCOMM_OK; |
| 221 | |
| 222 | error: |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Enable UST channel on the tracer. |
| 228 | */ |
| 229 | int channel_ust_enable(struct ltt_ust_session *usession, |
| 230 | struct ltt_ust_channel *uchan, int sock) |
| 231 | { |
| 232 | int ret; |
| 233 | ret = LTTCOMM_OK; |
| 234 | |
| 235 | ret = ustctl_enable_channel(sock, usession, uchan); |
| 236 | if (ret < 0) { |
| 237 | ret = LTTCOMM_UST_CHAN_FAIL; |
| 238 | goto error; |
| 239 | } |
| 240 | |
| 241 | ret = LTTCOMM_OK; |
| 242 | |
| 243 | error: |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Disable UST channel on the tracer. |
| 249 | */ |
| 250 | int channel_ust_disable(struct ltt_ust_session *usession, |
| 251 | struct ltt_ust_channel *uchan, int sock) |
| 252 | { |
| 253 | int ret; |
| 254 | ret = LTTCOMM_OK; |
| 255 | |
| 256 | ret = ustctl_disable_channel(sock, usession, uchan); |
| 257 | if (ret < 0) { |
| 258 | ret = LTTCOMM_UST_CHAN_FAIL; |
| 259 | goto error; |
| 260 | } |
| 261 | |
| 262 | ret = LTTCOMM_OK; |
| 263 | |
| 264 | error: |
| 265 | return ret; |
| 266 | } |