| 1 | /* |
| 2 | * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 7 | * as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 12 | * for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public License |
| 15 | * along with this library; if not, write to the Free Software Foundation, |
| 16 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | package org.lttng.ust.agent; |
| 20 | |
| 21 | import java.nio.ByteBuffer; |
| 22 | import java.nio.ByteOrder; |
| 23 | import java.lang.Object; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.List; |
| 26 | import java.util.Enumeration; |
| 27 | import java.util.Iterator; |
| 28 | |
| 29 | interface LTTngSessiondCmd2_6 { |
| 30 | /** |
| 31 | * Maximum name length for a logger name to be send to sessiond. |
| 32 | */ |
| 33 | final static int NAME_MAX = 255; |
| 34 | |
| 35 | /* |
| 36 | * Size of a primitive type int in byte. Because you know, Java can't |
| 37 | * provide that since it does not makes sense... |
| 38 | */ |
| 39 | final static int INT_SIZE = 4; |
| 40 | |
| 41 | public interface SessiondResponse { |
| 42 | /** |
| 43 | * Gets a byte array of the command so that it may be streamed |
| 44 | * |
| 45 | * @return the byte array of the command |
| 46 | */ |
| 47 | public byte[] getBytes(); |
| 48 | } |
| 49 | |
| 50 | public interface SessiondCommand { |
| 51 | /** |
| 52 | * Populate the class from a byte array |
| 53 | * |
| 54 | * @param data |
| 55 | * the byte array containing the streamed command |
| 56 | */ |
| 57 | public void populate(byte[] data); |
| 58 | } |
| 59 | |
| 60 | public enum lttng_agent_command { |
| 61 | /** List logger(s). */ |
| 62 | CMD_LIST(1), |
| 63 | /** Enable logger by name. */ |
| 64 | CMD_ENABLE(2), |
| 65 | /** Disable logger by name. */ |
| 66 | CMD_DISABLE(3), |
| 67 | /** Registration done */ |
| 68 | CMD_REG_DONE(4); |
| 69 | |
| 70 | private int code; |
| 71 | |
| 72 | private lttng_agent_command(int c) { |
| 73 | code = c; |
| 74 | } |
| 75 | |
| 76 | public int getCommand() { |
| 77 | return code; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | enum lttng_agent_ret_code { |
| 82 | CODE_SUCCESS_CMD(1), |
| 83 | CODE_INVALID_CMD(2), |
| 84 | CODE_UNK_LOGGER_NAME(3); |
| 85 | private int code; |
| 86 | |
| 87 | private lttng_agent_ret_code(int c) { |
| 88 | code = c; |
| 89 | } |
| 90 | |
| 91 | public int getCode() { |
| 92 | return code; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public class sessiond_hdr implements SessiondCommand { |
| 97 | /** ABI size of command header. */ |
| 98 | public final static int SIZE = 16; |
| 99 | /** Payload size in bytes following this header. */ |
| 100 | public long data_size; |
| 101 | /** Command type. */ |
| 102 | public lttng_agent_command cmd; |
| 103 | /** Command version. */ |
| 104 | public int cmd_version; |
| 105 | |
| 106 | public void populate(byte[] data) { |
| 107 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 108 | buf.order(ByteOrder.BIG_ENDIAN); |
| 109 | |
| 110 | data_size = buf.getLong(); |
| 111 | cmd = lttng_agent_command.values()[buf.getInt() - 1]; |
| 112 | cmd_version = buf.getInt(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | public class sessiond_enable_handler implements SessiondResponse, SessiondCommand { |
| 117 | private final static int SIZE = 4; |
| 118 | public String name; |
| 119 | public int lttngLogLevel; |
| 120 | public int lttngLogLevelType; |
| 121 | |
| 122 | /** Return status code to the session daemon. */ |
| 123 | public lttng_agent_ret_code code; |
| 124 | |
| 125 | @Override |
| 126 | public void populate(byte[] data) { |
| 127 | int data_offset = INT_SIZE * 2; |
| 128 | |
| 129 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 130 | buf.order(ByteOrder.LITTLE_ENDIAN); |
| 131 | lttngLogLevel = buf.getInt(); |
| 132 | lttngLogLevelType = buf.getInt(); |
| 133 | name = new String(data, data_offset, data.length - data_offset).trim(); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public byte[] getBytes() { |
| 138 | byte data[] = new byte[SIZE]; |
| 139 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 140 | buf.order(ByteOrder.BIG_ENDIAN); |
| 141 | buf.putInt(code.getCode()); |
| 142 | return data; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Execute enable handler action which is to enable the given handler |
| 147 | * to the received name. |
| 148 | */ |
| 149 | public void execute(LogFramework log) { |
| 150 | if (log.enableLogger(this.name)) { |
| 151 | this.code = lttng_agent_ret_code.CODE_SUCCESS_CMD; |
| 152 | } else { |
| 153 | this.code = lttng_agent_ret_code.CODE_INVALID_CMD; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | public class sessiond_disable_handler implements SessiondResponse, SessiondCommand { |
| 159 | private final static int SIZE = 4; |
| 160 | public String name; |
| 161 | |
| 162 | |
| 163 | /** Return status code to the session daemon. */ |
| 164 | public lttng_agent_ret_code code; |
| 165 | |
| 166 | @Override |
| 167 | public void populate(byte[] data) { |
| 168 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 169 | buf.order(ByteOrder.LITTLE_ENDIAN); |
| 170 | name = new String(data).trim(); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public byte[] getBytes() { |
| 175 | byte data[] = new byte[SIZE]; |
| 176 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 177 | buf.order(ByteOrder.BIG_ENDIAN); |
| 178 | buf.putInt(code.getCode()); |
| 179 | return data; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Execute disable handler action which is to disable the given handler |
| 184 | * to the received name. |
| 185 | */ |
| 186 | public void execute(LogFramework log) { |
| 187 | if (log.disableLogger(this.name)) { |
| 188 | this.code = lttng_agent_ret_code.CODE_SUCCESS_CMD; |
| 189 | } else { |
| 190 | this.code = lttng_agent_ret_code.CODE_INVALID_CMD; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | public class sessiond_list_logger implements SessiondResponse { |
| 196 | private final static int SIZE = 12; |
| 197 | |
| 198 | private int data_size = 0; |
| 199 | private int nb_logger = 0; |
| 200 | |
| 201 | List<String> logger_list = new ArrayList<String>(); |
| 202 | |
| 203 | /** Return status code to the session daemon. */ |
| 204 | public lttng_agent_ret_code code; |
| 205 | |
| 206 | @Override |
| 207 | public byte[] getBytes() { |
| 208 | byte data[] = new byte[SIZE + data_size]; |
| 209 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 210 | buf.order(ByteOrder.BIG_ENDIAN); |
| 211 | |
| 212 | /* Returned code */ |
| 213 | buf.putInt(code.getCode()); |
| 214 | buf.putInt(data_size); |
| 215 | buf.putInt(nb_logger); |
| 216 | |
| 217 | for (String logger: logger_list) { |
| 218 | buf.put(logger.getBytes()); |
| 219 | /* NULL terminated byte after the logger name. */ |
| 220 | buf.put((byte) 0x0); |
| 221 | } |
| 222 | return data; |
| 223 | } |
| 224 | |
| 225 | public void execute(LogFramework log) { |
| 226 | String loggerName; |
| 227 | |
| 228 | Iterator<String> loggers = log.listLoggers(); |
| 229 | while (loggers.hasNext()) { |
| 230 | loggerName = loggers.next(); |
| 231 | this.logger_list.add(loggerName); |
| 232 | this.nb_logger++; |
| 233 | this.data_size += loggerName.length() + 1; |
| 234 | } |
| 235 | |
| 236 | this.code = lttng_agent_ret_code.CODE_SUCCESS_CMD; |
| 237 | } |
| 238 | } |
| 239 | } |