| 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.util.ArrayList; |
| 24 | import java.util.Iterator; |
| 25 | import java.util.List; |
| 26 | |
| 27 | interface LTTngSessiondCmd2_6 { |
| 28 | |
| 29 | /** |
| 30 | * Maximum name length for a logger name to be send to sessiond. |
| 31 | */ |
| 32 | int NAME_MAX = 255; |
| 33 | |
| 34 | /* |
| 35 | * Size of a primitive type int in byte. Because you know, Java can't |
| 36 | * provide that since it does not makes sense... |
| 37 | * |
| 38 | * |
| 39 | */ |
| 40 | int INT_SIZE = 4; |
| 41 | |
| 42 | interface SessiondResponse { |
| 43 | /** |
| 44 | * Gets a byte array of the command so that it may be streamed |
| 45 | * |
| 46 | * @return the byte array of the command |
| 47 | */ |
| 48 | public byte[] getBytes(); |
| 49 | } |
| 50 | |
| 51 | interface SessiondCommand { |
| 52 | /** |
| 53 | * Populate the class from a byte array |
| 54 | * |
| 55 | * @param data |
| 56 | * the byte array containing the streamed command |
| 57 | */ |
| 58 | public void populate(byte[] data); |
| 59 | } |
| 60 | |
| 61 | enum lttng_agent_command { |
| 62 | /** List logger(s). */ |
| 63 | CMD_LIST(1), |
| 64 | /** Enable logger by name. */ |
| 65 | CMD_ENABLE(2), |
| 66 | /** Disable logger by name. */ |
| 67 | CMD_DISABLE(3), |
| 68 | /** Registration done */ |
| 69 | CMD_REG_DONE(4); |
| 70 | |
| 71 | private int code; |
| 72 | |
| 73 | private lttng_agent_command(int c) { |
| 74 | code = c; |
| 75 | } |
| 76 | |
| 77 | public int getCommand() { |
| 78 | return code; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | enum lttng_agent_ret_code { |
| 83 | CODE_SUCCESS_CMD(1), |
| 84 | CODE_INVALID_CMD(2), |
| 85 | CODE_UNK_LOGGER_NAME(3); |
| 86 | private int code; |
| 87 | |
| 88 | private lttng_agent_ret_code(int c) { |
| 89 | code = c; |
| 90 | } |
| 91 | |
| 92 | public int getCode() { |
| 93 | return code; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | class sessiond_hdr implements SessiondCommand { |
| 98 | |
| 99 | /** ABI size of command header. */ |
| 100 | public final static int SIZE = 16; |
| 101 | /** Payload size in bytes following this header. */ |
| 102 | public long dataSize; |
| 103 | /** Command type. */ |
| 104 | public lttng_agent_command cmd; |
| 105 | /** Command version. */ |
| 106 | public int cmdVersion; |
| 107 | |
| 108 | @Override |
| 109 | public void populate(byte[] data) { |
| 110 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 111 | buf.order(ByteOrder.BIG_ENDIAN); |
| 112 | |
| 113 | dataSize = buf.getLong(); |
| 114 | cmd = lttng_agent_command.values()[buf.getInt() - 1]; |
| 115 | cmdVersion = buf.getInt(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | class sessiond_enable_handler implements SessiondResponse, SessiondCommand { |
| 120 | |
| 121 | private static final int SIZE = 4; |
| 122 | public String name; |
| 123 | public int lttngLogLevel; |
| 124 | public int lttngLogLevelType; |
| 125 | |
| 126 | /** Return status code to the session daemon. */ |
| 127 | public lttng_agent_ret_code code; |
| 128 | |
| 129 | @Override |
| 130 | public void populate(byte[] data) { |
| 131 | int dataOffset = INT_SIZE * 2; |
| 132 | |
| 133 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 134 | buf.order(ByteOrder.LITTLE_ENDIAN); |
| 135 | lttngLogLevel = buf.getInt(); |
| 136 | lttngLogLevelType = buf.getInt(); |
| 137 | name = new String(data, dataOffset, data.length - dataOffset).trim(); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public byte[] getBytes() { |
| 142 | byte data[] = new byte[SIZE]; |
| 143 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 144 | buf.order(ByteOrder.BIG_ENDIAN); |
| 145 | buf.putInt(code.getCode()); |
| 146 | return data; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Execute enable handler action which is to enable the given handler |
| 151 | * to the received name. |
| 152 | */ |
| 153 | public void execute(LogFramework log) { |
| 154 | if (log.enableLogger(this.name)) { |
| 155 | this.code = lttng_agent_ret_code.CODE_SUCCESS_CMD; |
| 156 | } else { |
| 157 | this.code = lttng_agent_ret_code.CODE_INVALID_CMD; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | class sessiond_disable_handler implements SessiondResponse, SessiondCommand { |
| 163 | |
| 164 | private final static int SIZE = 4; |
| 165 | public String name; |
| 166 | |
| 167 | |
| 168 | /** Return status code to the session daemon. */ |
| 169 | public lttng_agent_ret_code code; |
| 170 | |
| 171 | @Override |
| 172 | public void populate(byte[] data) { |
| 173 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 174 | buf.order(ByteOrder.LITTLE_ENDIAN); |
| 175 | name = new String(data).trim(); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public byte[] getBytes() { |
| 180 | byte data[] = new byte[SIZE]; |
| 181 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 182 | buf.order(ByteOrder.BIG_ENDIAN); |
| 183 | buf.putInt(code.getCode()); |
| 184 | return data; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Execute disable handler action which is to disable the given handler |
| 189 | * to the received name. |
| 190 | */ |
| 191 | public void execute(LogFramework log) { |
| 192 | if (log.disableLogger(this.name)) { |
| 193 | this.code = lttng_agent_ret_code.CODE_SUCCESS_CMD; |
| 194 | } else { |
| 195 | this.code = lttng_agent_ret_code.CODE_INVALID_CMD; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | class sessiond_list_logger implements SessiondResponse { |
| 201 | |
| 202 | private final static int SIZE = 12; |
| 203 | |
| 204 | private int dataSize = 0; |
| 205 | private int nbLogger = 0; |
| 206 | |
| 207 | List<String> loggerList = new ArrayList<String>(); |
| 208 | |
| 209 | /** Return status code to the session daemon. */ |
| 210 | public lttng_agent_ret_code code; |
| 211 | |
| 212 | @Override |
| 213 | public byte[] getBytes() { |
| 214 | byte data[] = new byte[SIZE + dataSize]; |
| 215 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 216 | buf.order(ByteOrder.BIG_ENDIAN); |
| 217 | |
| 218 | /* Returned code */ |
| 219 | buf.putInt(code.getCode()); |
| 220 | buf.putInt(dataSize); |
| 221 | buf.putInt(nbLogger); |
| 222 | |
| 223 | for (String logger: loggerList) { |
| 224 | buf.put(logger.getBytes()); |
| 225 | /* NULL terminated byte after the logger name. */ |
| 226 | buf.put((byte) 0x0); |
| 227 | } |
| 228 | return data; |
| 229 | } |
| 230 | |
| 231 | public void execute(LogFramework log) { |
| 232 | String loggerName; |
| 233 | |
| 234 | Iterator<String> loggers = log.listLoggers(); |
| 235 | while (loggers.hasNext()) { |
| 236 | loggerName = loggers.next(); |
| 237 | this.loggerList.add(loggerName); |
| 238 | this.nbLogger++; |
| 239 | this.dataSize += loggerName.length() + 1; |
| 240 | } |
| 241 | |
| 242 | this.code = lttng_agent_ret_code.CODE_SUCCESS_CMD; |
| 243 | } |
| 244 | } |
| 245 | } |