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. | |
43e5396b | 152 | */ |
501f6777 CB |
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; | |
43e5396b | 158 | } |
43e5396b DG |
159 | } |
160 | } | |
161 | ||
08284556 AM |
162 | class sessiond_disable_handler implements SessiondResponse, SessiondCommand { |
163 | ||
43e5396b DG |
164 | private final static int SIZE = 4; |
165 | public String name; | |
501f6777 | 166 | |
43e5396b DG |
167 | |
168 | /** Return status code to the session daemon. */ | |
501f6777 | 169 | public lttng_agent_ret_code code; |
43e5396b DG |
170 | |
171 | @Override | |
172 | public void populate(byte[] data) { | |
173 | ByteBuffer buf = ByteBuffer.wrap(data); | |
96caa5ed | 174 | buf.order(ByteOrder.LITTLE_ENDIAN); |
9663e532 | 175 | name = new String(data).trim(); |
43e5396b DG |
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 | */ | |
501f6777 CB |
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; | |
43e5396b | 196 | } |
43e5396b DG |
197 | } |
198 | } | |
199 | ||
08284556 AM |
200 | class sessiond_list_logger implements SessiondResponse { |
201 | ||
43e5396b DG |
202 | private final static int SIZE = 12; |
203 | ||
08284556 AM |
204 | private int dataSize = 0; |
205 | private int nbLogger = 0; | |
43e5396b | 206 | |
08284556 | 207 | List<String> loggerList = new ArrayList<String>(); |
43e5396b DG |
208 | |
209 | /** Return status code to the session daemon. */ | |
501f6777 | 210 | public lttng_agent_ret_code code; |
43e5396b DG |
211 | |
212 | @Override | |
213 | public byte[] getBytes() { | |
08284556 | 214 | byte data[] = new byte[SIZE + dataSize]; |
43e5396b DG |
215 | ByteBuffer buf = ByteBuffer.wrap(data); |
216 | buf.order(ByteOrder.BIG_ENDIAN); | |
217 | ||
218 | /* Returned code */ | |
219 | buf.putInt(code.getCode()); | |
08284556 AM |
220 | buf.putInt(dataSize); |
221 | buf.putInt(nbLogger); | |
43e5396b | 222 | |
08284556 | 223 | for (String logger: loggerList) { |
43e5396b DG |
224 | buf.put(logger.getBytes()); |
225 | /* NULL terminated byte after the logger name. */ | |
226 | buf.put((byte) 0x0); | |
227 | } | |
228 | return data; | |
229 | } | |
230 | ||
501f6777 | 231 | public void execute(LogFramework log) { |
43e5396b DG |
232 | String loggerName; |
233 | ||
501f6777 CB |
234 | Iterator<String> loggers = log.listLoggers(); |
235 | while (loggers.hasNext()) { | |
236 | loggerName = loggers.next(); | |
08284556 AM |
237 | this.loggerList.add(loggerName); |
238 | this.nbLogger++; | |
239 | this.dataSize += loggerName.length() + 1; | |
43e5396b DG |
240 | } |
241 | ||
501f6777 | 242 | this.code = lttng_agent_ret_code.CODE_SUCCESS_CMD; |
43e5396b DG |
243 | } |
244 | } | |
245 | } |