- Minor modification of the FW API (better trace/parser integration)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng / src / org / eclipse / linuxtools / lttng / jni / JniParser.java
1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * William Bourque (wbourque@gmail.com) - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.lttng.jni;
14
15 import java.util.HashMap;
16
17 /**
18 * <b><u>JniParser</u></b>
19 * <p>
20 * JniParser class.
21 * All methods are static, the parser shouldn't be instantiated.
22 */
23 public class JniParser extends Jni_C_Common
24 {
25 private static native void ltt_getParsedData(ParsedObjectContent parseddata, long eventPtr, long markerFieldPtr);
26
27 static {
28 System.loadLibrary("lttvtraceread");
29 }
30
31 /**
32 * Default constructor is forbidden
33 */
34 private JniParser() {
35 }
36
37
38
39 /**
40 * Method to parse a single field identified by its id<br>
41 * All parsing will be done on C side as we need Ltt function
42 *
43 * @param eventToParse The jni event we want to parse.
44 * @param fieldPosition The position (or id) of the field we want to parse
45 *
46 * @return An Object that contain the JniEvent payload parsed by the C, or null, if if was impossible to parse (i.e., wrong position)
47 *
48 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
49 */
50 static public Object parseField(JniEvent eventToParse, int fieldPosition) {
51
52 // Sanity check
53 if ( (fieldPosition < 0) || ( fieldPosition >= eventToParse.requestEventMarker().getMarkerFieldsArrayList().size() ) ){
54 return null;
55 }
56
57 // *** HACK ***
58 // We cannot use "Object" directly as java does not support swapping primitive value
59 // We either need to create a new object type or to use a "non-primitive" type that have "Setter()" functions
60 // ***
61 ParsedObjectContent parsedData = new ParsedObjectContent();
62
63 // Call the parsing function in C. The result will be put in parsedData object
64 ltt_getParsedData(parsedData, eventToParse.getEventPtr().getPointer(), eventToParse.requestEventMarker().getMarkerFieldsArrayList().get(fieldPosition).getMarkerFieldPtr().getPointer() );
65
66 return parsedData.getData();
67 }
68
69
70 /**
71 * Method to parse a single field identified by its name<br>
72 * All parsing will be done on C side as we need Ltt function
73 *
74 * @param eventToParse The jni event we want to parse.
75 * @param fieldPosition The position (or id) of the field we want to parse
76 *
77 * @return An Object that contain the JniEvent payload parsed by the C, or null, if if was impossible to parse (i.e., wrong position)
78 *
79 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
80 */
81 static public Object parseField(JniEvent eventToParse, String fieldName) {
82
83 JniMarkerField tmpField = eventToParse.requestEventMarker().getMarkerFieldsHashMap().get(fieldName);
84
85 // return immediately if there is no field by that name
86 if ( tmpField == null ) {
87 return null;
88 }
89
90 // *** HACK ***
91 // We cannot use "Object" directly as java does not support swapping on primitive value
92 // We either need to create a new object type or to use a "non-primitive" type that have "Setter()" functions
93 // ***
94 ParsedObjectContent parsedData = new ParsedObjectContent();
95
96 ltt_getParsedData(parsedData, eventToParse.getEventPtr().getPointer(), tmpField.getMarkerFieldPtr().getPointer() );
97
98 return parsedData.getData();
99 }
100
101
102
103 /**
104 * Method to parse all field at once<br>
105 * All parsing will be done on C side as we need Ltt function
106 *
107 * @param eventToParse The jni event we want to parse.
108 * @return An HashMap of Object that contain the is the JniEvent's payload parsed by the C
109 *
110 * @see org.eclipse.linuxtools.lttng.jni.JniEvent
111 */
112 static public HashMap<String, Object> parseAllFields(JniEvent eventToParse) {
113 JniMarker markerData = eventToParse.requestEventMarker();
114 int nbMarkerField = markerData.getMarkerFieldsArrayList().size();
115
116 // This hashmap will contain the parsed content.
117 // ParsedContent is defined at the end of this file
118 HashMap<String, Object> parsedDataArray = new HashMap<String, Object>(nbMarkerField);
119
120 // *** HACK ***
121 // We cannot use "Object" directly as java does not support swapping on primitive value
122 // We either need to create a new object type or to use a "non-primitive" type that have "Setter()" functions
123 // ***
124 ParsedObjectContent parsedData = new ParsedObjectContent();
125
126 // Loop on markerfield, as we need to parse each field in the event data
127 for (int pos = 0; pos < nbMarkerField; pos++) {
128 // Call the C to parse the data
129 ltt_getParsedData(parsedData, eventToParse.getEventPtr().getPointer(), markerData.getMarkerFieldsArrayList().get(pos).getMarkerFieldPtr().getPointer() );
130 // Save the result into the HashMap
131 parsedDataArray.put(markerData.getMarkerFieldsArrayList().get(pos).getField(), parsedData.getData() );
132 }
133
134 return parsedDataArray;
135 }
136
137 /*
138 * Add a parsed String value to the Array<br>
139 * <br>
140 * Note : this function will be called from the C side.
141 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
142 * its goal is to give a generic interface to people that would like to use the JNI library
143 *
144 * @param parsedArray Array where to store the value
145 * @param fieldName The name of the parsed field
146 * @param stringToAdd The parsed data to add
147 * @param formatToAdd The format of the raw data
148 */
149 @SuppressWarnings("unused")
150 static private void addStringToParsingFromC(Object contentHolder, String fieldName, String stringToAdd) {
151 ((ParsedObjectContent)contentHolder).setData( stringToAdd);
152 }
153
154 /*
155 * Add a parsed 64 bits Pointer value to the Array<br>
156 * <br>
157 * Note : this function will be called from the C side.
158 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
159 * its goal is to give a generic interface to people that would like to use the JNI library
160 *
161 * @param contentHolder Object where to store the parsed value
162 * @param fieldName The name of the parsed field
163 * @param pointerToAdd The parsed data to add (in 64 bits long!)
164 * @param formatToAdd The format of the raw data
165 */
166 @SuppressWarnings("unused")
167 static private void addLongPointerToParsingFromC(Object contentHolder, String fieldName, long pointerToAdd) {
168 ((ParsedObjectContent)contentHolder).setData( new C_Pointer((long) pointerToAdd));
169 }
170
171 /*
172 * Add a parsed 32 bits Pointer value to the Array<br>
173 * <br>
174 * Note : this function will be called from the C side.
175 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
176 * its goal is to give a generic interface to people that would like to use the JNI library
177 *
178 * @param contentHolder Object where to store the parsed value
179 * @param fieldName The name of the parsed field
180 * @param pointerToAdd The parsed data to add (converted in 64 bits long!)
181 * @param formatToAdd The format of the raw data
182 */
183 @SuppressWarnings("unused")
184 static private void addIntPointerToParsingFromC(Object contentHolder, String fieldName, long pointerToAdd) {
185 ((ParsedObjectContent)contentHolder).setData( new C_Pointer((int) pointerToAdd));
186 }
187
188 /*
189 * Add a parsed short value to the Array<br>
190 * <br>
191 * Note : this function will be called from the C side.
192 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
193 * its goal is to give a generic interface to people that would like to use the JNI library
194 *
195 * @param contentHolder Object where to store the parsed value
196 * @param fieldName The name of the parsed field
197 * @param shortToAdd The parsed data to add
198 * @param formatToAdd The format of the raw data
199 */
200 @SuppressWarnings("unused")
201 static private void addShortToParsingFromC(Object contentHolder, String fieldName, short shortToAdd) {
202 ((ParsedObjectContent)contentHolder).setData( new Short(shortToAdd));
203 }
204
205 /*
206 * Add a parsed integer value to the Array<br>
207 * <br>
208 * Note : this function will be called from the C side.
209 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
210 * its goal is to give a generic interface to people that would like to use the JNI library
211 *
212 * @param contentHolder Object where to store the parsed value
213 * @param fieldName The name of the parsed field
214 * @param intToAdd The parsed data to add
215 * @param formatToAdd The format of the raw data
216 */
217 @SuppressWarnings("unused")
218 static private void addIntegerToParsingFromC(Object contentHolder, String fieldName, int intToAdd) {
219 ((ParsedObjectContent)contentHolder).setData( new Integer(intToAdd));
220 }
221
222 /*
223 * Add a parsed long value to the Array<br>
224 * <br>
225 * Note : this function will be called from the C side.
226 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
227 * its goal is to give a generic interface to people that would like to use the JNI library
228 *
229 * @param contentHolder Object where to store the parsed value
230 * @param fieldName The name of the parsed field
231 * @param longToAdd The parsed data to add
232 * @param formatToAdd The format of the raw data
233 */
234 @SuppressWarnings("unused")
235 static private void addLongToParsingFromC(Object contentHolder, String fieldName, long longToAdd) {
236 ((ParsedObjectContent)contentHolder).setData( new Long(longToAdd));
237 }
238
239 /*
240 * Add a parsed float value to the Array<br>
241 * <br>
242 * Note : this function will be called from the C side.
243 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
244 * its goal is to give a generic interface to people that would like to use the JNI library
245 *
246 * @param contentHolder Object where to store the parsed value
247 * @param fieldName The name of the parsed field
248 * @param floatToAdd The parsed data to add
249 * @param formatToAdd The format of the raw data
250 */
251 @SuppressWarnings("unused")
252 static private void addFloatToParsingFromC(Object contentHolder, String fieldName, float floatToAdd) {
253 ((ParsedObjectContent)contentHolder).setData( new Float(floatToAdd));
254 }
255
256 /*
257 * Add a parsed double value to the Array<br>
258 * <br>
259 * Note : this function will be called from the C side.
260 * Note2: contentHolder is of type "Object" instead of "ParsedObjectContent" to be able to use the most generic function signature on the C side.
261 * its goal is to give a generic interface to people that would like to use the JNI library
262 *
263 *
264 * @param contentHolder Object where to store the parsed value
265 * @param fieldName The name of the parsed field
266 * @param doubleToAdd The parsed data to add
267 * @param formatToAdd The format of the raw data
268 */
269 @SuppressWarnings("unused")
270 static private void addDoubleToParsingFromC(Object contentHolder, String fieldName, double doubleToAdd) {
271 ((ParsedObjectContent)contentHolder).setData( new Double(doubleToAdd));
272 }
273
274 }
275
276 /**
277 * <b><u>ParsedObjectContent</u></b>
278 * <p>
279 * ParsedObjectContent class.
280 * This class will only be used locally in this object to parse event data more efficiently in the C
281 */
282 class ParsedObjectContent {
283 private Object parsedData = null;
284
285 public Object getData() {
286 return parsedData;
287 }
288
289 public void setData(Object newData) {
290 parsedData = newData;
291 }
292 }
This page took 0.036592 seconds and 5 git commands to generate.