Commit | Line | Data |
---|---|---|
43e5396b DG |
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 | ||
501f6777 | 19 | package org.lttng.ust.agent; |
43e5396b DG |
20 | |
21 | import java.nio.ByteBuffer; | |
22 | import java.nio.ByteOrder; | |
43e5396b | 23 | import java.util.ArrayList; |
501f6777 | 24 | import java.util.Iterator; |
bc7de6d9 | 25 | import java.util.List; |
43e5396b | 26 | |
501f6777 | 27 | interface LTTngSessiondCmd2_6 { |
08284556 | 28 | |
43e5396b DG |
29 | /** |
30 | * Maximum name length for a logger name to be send to sessiond. | |
31 | */ | |
08284556 | 32 | int NAME_MAX = 255; |
43e5396b | 33 | |
a15440fd DG |
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... | |
08284556 AM |
37 | * |
38 | * | |
a15440fd | 39 | */ |
08284556 | 40 | int INT_SIZE = 4; |
a15440fd | 41 | |
08284556 | 42 | interface SessiondResponse { |
43e5396b DG |
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 | ||
08284556 | 51 | interface SessiondCommand { |
43e5396b DG |
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 | ||
08284556 | 61 | enum lttng_agent_command { |
43e5396b DG |
62 | /** List logger(s). */ |
63 | CMD_LIST(1), | |
64 | /** Enable logger by name. */ | |
65 | CMD_ENABLE(2), | |
66 | /** Disable logger by name. */ | |
f08bb871 DG |
67 | CMD_DISABLE(3), |
68 | /** Registration done */ | |
69 | CMD_REG_DONE(4); | |
70 | ||
43e5396b DG |
71 | private int code; |
72 | ||
501f6777 | 73 | private lttng_agent_command(int c) { |
43e5396b DG |
74 | code = c; |
75 | } | |
76 | ||
77 | public int getCommand() { | |
78 | return code; | |
79 | } | |
80 | } | |
81 | ||
501f6777 | 82 | enum lttng_agent_ret_code { |
43e5396b DG |
83 | CODE_SUCCESS_CMD(1), |
84 | CODE_INVALID_CMD(2), | |
85 | CODE_UNK_LOGGER_NAME(3); | |
86 | private int code; | |
87 | ||
501f6777 | 88 | private lttng_agent_ret_code(int c) { |
43e5396b DG |
89 | code = c; |
90 | } | |
91 | ||
92 | public int getCode() { | |
93 | return code; | |
94 | } | |
95 | } | |
96 | ||
08284556 AM |
97 | class sessiond_hdr implements SessiondCommand { |
98 | ||
43e5396b DG |
99 | /** ABI size of command header. */ |
100 | public final static int SIZE = 16; | |
101 | /** Payload size in bytes following this header. */ | |
08284556 | 102 | public long dataSize; |
43e5396b | 103 | /** Command type. */ |
501f6777 | 104 | public lttng_agent_command cmd; |
43e5396b | 105 | /** Command version. */ |
08284556 | 106 | public int cmdVersion; |
43e5396b | 107 | |
08284556 | 108 | @Override |
43e5396b DG |
109 | public void populate(byte[] data) { |
110 | ByteBuffer buf = ByteBuffer.wrap(data); | |
111 | buf.order(ByteOrder.BIG_ENDIAN); | |
112 | ||
08284556 | 113 | dataSize = buf.getLong(); |
501f6777 | 114 | cmd = lttng_agent_command.values()[buf.getInt() - 1]; |
08284556 | 115 | cmdVersion = buf.getInt(); |
43e5396b DG |
116 | } |
117 | } | |
118 | ||
08284556 AM |
119 | class sessiond_enable_handler implements SessiondResponse, SessiondCommand { |
120 | ||
121 | private static final int SIZE = 4; | |
43e5396b | 122 | public String name; |
a15440fd DG |
123 | public int lttngLogLevel; |
124 | public int lttngLogLevelType; | |
43e5396b DG |
125 | |
126 | /** Return status code to the session daemon. */ | |
501f6777 | 127 | public lttng_agent_ret_code code; |
43e5396b DG |
128 | |
129 | @Override | |
130 | public void populate(byte[] data) { | |
08284556 | 131 | int dataOffset = INT_SIZE * 2; |
a15440fd | 132 | |
43e5396b DG |
133 | ByteBuffer buf = ByteBuffer.wrap(data); |
134 | buf.order(ByteOrder.LITTLE_ENDIAN); | |
a15440fd DG |
135 | lttngLogLevel = buf.getInt(); |
136 | lttngLogLevelType = buf.getInt(); | |
08284556 | 137 | name = new String(data, dataOffset, data.length - dataOffset).trim(); |
43e5396b DG |
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. | |
5bfeaeca AM |
152 | * |
153 | * @param log | |
43e5396b | 154 | */ |
501f6777 CB |
155 | public void execute(LogFramework log) { |
156 | if (log.enableLogger(this.name)) { | |
157 | this.code = lttng_agent_ret_code.CODE_SUCCESS_CMD; | |
158 | } else { | |
159 | this.code = lttng_agent_ret_code.CODE_INVALID_CMD; | |
43e5396b | 160 | } |
43e5396b DG |
161 | } |
162 | } | |
163 | ||
08284556 AM |
164 | class sessiond_disable_handler implements SessiondResponse, SessiondCommand { |
165 | ||
43e5396b DG |
166 | private final static int SIZE = 4; |
167 | public String name; | |
501f6777 | 168 | |
43e5396b DG |
169 | |
170 | /** Return status code to the session daemon. */ | |
501f6777 | 171 | public lttng_agent_ret_code code; |
43e5396b DG |
172 | |
173 | @Override | |
174 | public void populate(byte[] data) { | |
175 | ByteBuffer buf = ByteBuffer.wrap(data); | |
96caa5ed | 176 | buf.order(ByteOrder.LITTLE_ENDIAN); |
9663e532 | 177 | name = new String(data).trim(); |
43e5396b DG |
178 | } |
179 | ||
180 | @Override | |
181 | public byte[] getBytes() { | |
182 | byte data[] = new byte[SIZE]; | |
183 | ByteBuffer buf = ByteBuffer.wrap(data); | |
184 | buf.order(ByteOrder.BIG_ENDIAN); | |
185 | buf.putInt(code.getCode()); | |
186 | return data; | |
187 | } | |
188 | ||
189 | /** | |
190 | * Execute disable handler action which is to disable the given handler | |
191 | * to the received name. | |
5bfeaeca AM |
192 | * |
193 | * @param log | |
43e5396b | 194 | */ |
501f6777 CB |
195 | public void execute(LogFramework log) { |
196 | if (log.disableLogger(this.name)) { | |
197 | this.code = lttng_agent_ret_code.CODE_SUCCESS_CMD; | |
198 | } else { | |
199 | this.code = lttng_agent_ret_code.CODE_INVALID_CMD; | |
43e5396b | 200 | } |
43e5396b DG |
201 | } |
202 | } | |
203 | ||
08284556 AM |
204 | class sessiond_list_logger implements SessiondResponse { |
205 | ||
43e5396b DG |
206 | private final static int SIZE = 12; |
207 | ||
08284556 AM |
208 | private int dataSize = 0; |
209 | private int nbLogger = 0; | |
43e5396b | 210 | |
08284556 | 211 | List<String> loggerList = new ArrayList<String>(); |
43e5396b DG |
212 | |
213 | /** Return status code to the session daemon. */ | |
501f6777 | 214 | public lttng_agent_ret_code code; |
43e5396b DG |
215 | |
216 | @Override | |
217 | public byte[] getBytes() { | |
08284556 | 218 | byte data[] = new byte[SIZE + dataSize]; |
43e5396b DG |
219 | ByteBuffer buf = ByteBuffer.wrap(data); |
220 | buf.order(ByteOrder.BIG_ENDIAN); | |
221 | ||
222 | /* Returned code */ | |
223 | buf.putInt(code.getCode()); | |
08284556 AM |
224 | buf.putInt(dataSize); |
225 | buf.putInt(nbLogger); | |
43e5396b | 226 | |
08284556 | 227 | for (String logger: loggerList) { |
43e5396b DG |
228 | buf.put(logger.getBytes()); |
229 | /* NULL terminated byte after the logger name. */ | |
230 | buf.put((byte) 0x0); | |
231 | } | |
232 | return data; | |
233 | } | |
234 | ||
501f6777 | 235 | public void execute(LogFramework log) { |
43e5396b DG |
236 | String loggerName; |
237 | ||
501f6777 CB |
238 | Iterator<String> loggers = log.listLoggers(); |
239 | while (loggers.hasNext()) { | |
240 | loggerName = loggers.next(); | |
08284556 AM |
241 | this.loggerList.add(loggerName); |
242 | this.nbLogger++; | |
243 | this.dataSize += loggerName.length() + 1; | |
43e5396b DG |
244 | } |
245 | ||
501f6777 | 246 | this.code = lttng_agent_ret_code.CODE_SUCCESS_CMD; |
43e5396b DG |
247 | } |
248 | } | |
249 | } |