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