Internalize lttng.jni
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.jni / src / org / eclipse / linuxtools / lttng / jni / JniMarker.java
CommitLineData
0152140d
ASL
1package org.eclipse.linuxtools.lttng.jni;
2/*******************************************************************************
3 * Copyright (c) 2009 Ericsson
4 *
5 * All rights reserved. This program and the accompanying materials are
6 * made available under the terms of the Eclipse Public License v1.0 which
7 * accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
9 *
10 * Contributors:
11 * William Bourque (wbourque@gmail.com) - Initial API and implementation
12 *******************************************************************************/
13
14import java.util.ArrayList;
15import java.util.HashMap;
16
ce38c104
FC
17import org.eclipse.linuxtools.internal.lttng.jni.common.Jni_C_Pointer_And_Library_Id;
18import org.eclipse.linuxtools.internal.lttng.jni.exception.JniException;
19import org.eclipse.linuxtools.internal.lttng.jni.exception.JniMarkerException;
0152140d
ASL
20
21/**
22 * <b><u>JniMarker</u></b><p>
23 *
24 * A JniMarker contain information how to interpret the unparsed content (payload) of an event.<br>
25 * Each JniMarker contains several MarkerFields for each fields in the event's payload.
26 *
27 * Provides access to the marker_info C structure (from LTT) in java.
28 *
29 * Most important fields in the JniMarker are :
30 * <ul>
31 * <li> the name of the marker in String
32 * <li> an overview of the marker format (in C style printf format)
33 * <li> a reference to an ArrayList that contains MarkerFields object of this JniMarker
34 * </ul>
3b7509b0
WB
35 *
36 * <b>NOTE</b><p>
37 * This class is ABSTRACT, you need to extends it to support your specific LTTng version.<br>
38 * Please look at the abstract functions to override at the bottom of this file.<p>
39 *
0152140d
ASL
40 */
41public abstract class JniMarker extends Jni_C_Common
42{
43 // Internal C pointer of the JniEvent used in LTT
c85e8cb2 44 private Jni_C_Pointer_And_Library_Id thisMarkerPtr = new Jni_C_Pointer_And_Library_Id();
0152140d 45
9c4eb5f7
FC
46 private String name = ""; //$NON-NLS-1$
47 private String formatOverview = ""; //$NON-NLS-1$
0152140d
ASL
48
49 // These two contains hold references to the same MarkerField object
50 // The ArrayList can be used to efficiently find a field by its position
51 // The HashMap can be used to find a field by its name
52 private HashMap<String, JniMarkerField> markerFieldsHashMap = null;
53 private ArrayList<JniMarkerField> markerFieldsArrayList = null;
54
55 // Native access method
c85e8cb2
WB
56 protected native String ltt_getName(int libId, long markerPtr);
57 protected native String ltt_getFormatOverview(int libId, long markerPtr);
58 protected native long ltt_getSize(int libId, long markerPtr);
59 protected native short ltt_getLargestAlign(int libId, long markerPtr);
60 protected native short ltt_getIntSize(int libId, long markerPtr);
61 protected native short ltt_getLongSize(int libId, long markerPtr);
62 protected native short ltt_getPointerSize(int libId, long markerPtr);
63 protected native short ltt_getSize_tSize(int libId, long markerPtr);
64 protected native void ltt_getAllMarkerFields(int libId, long tracePtr);
65 protected native short ltt_getAlignement(int libId, long markerPtr);
66 protected native long ltt_getNextMarkerPtr(int libId, long markerPtr);
0152140d
ASL
67
68 // Debug native function, ask LTT to print marker structure
c85e8cb2 69 protected native void ltt_printMarker(int libId, long markerPtr);
a9fcdd8d 70
0152140d
ASL
71 /*
72 * Default constructor is forbidden
73 */
74 protected JniMarker() {
75 }
76
77 /**
78 * Copy constructor.<p>
79 *
80 * @param oldMarker Reference to the JniMarker you want to copy.
81 */
82 public JniMarker(JniMarker oldMarker) {
83 thisMarkerPtr = oldMarker.thisMarkerPtr;
84 name = oldMarker.name;
85 formatOverview = oldMarker.formatOverview;
86 markerFieldsHashMap = oldMarker.markerFieldsHashMap;
87 markerFieldsArrayList = oldMarker.markerFieldsArrayList;
88
89 }
90
91 /**
92 * Constructor, using pointer.<p>
93 *
94 * @param newMarkerPtr Pointer to a C marker_info structure
95 *
96 * @exception JniException
97 */
c85e8cb2 98 public JniMarker(Jni_C_Pointer_And_Library_Id newMarkerPtr) throws JniException {
0152140d
ASL
99 thisMarkerPtr = newMarkerPtr;
100 markerFieldsArrayList = new ArrayList<JniMarkerField>();
101 markerFieldsHashMap = new HashMap<String, JniMarkerField>();
102
103 // Populate the marker
104 populateMarkerInformation();
105 }
106
107
108 /*
109 * This function populates the marker data with data from LTT
110 *
111 */
112 private void populateMarkerInformation() throws JniException {
113 if (thisMarkerPtr.getPointer() == NULL) {
9c4eb5f7 114 throw new JniMarkerException("Pointer is NULL, trace closed? (populateMarkerInformatOverviewion)"); //$NON-NLS-1$
0152140d 115 } else {
c85e8cb2
WB
116 name = ltt_getName(thisMarkerPtr.getLibraryId(), thisMarkerPtr.getPointer());
117 formatOverview = ltt_getFormatOverview(thisMarkerPtr.getLibraryId(), thisMarkerPtr.getPointer());
0152140d 118 // To fill the markerFieldArray is a bit different
c85e8cb2 119 ltt_getAllMarkerFields(thisMarkerPtr.getLibraryId(), thisMarkerPtr.getPointer());
0152140d
ASL
120 }
121 }
122
123 /*
124 * Fills a map of all the JniMarkerField associated with this JniMarker.
125 *
126 * Note: This function is called from C and there is no way to propagate
127 * exception back to the caller without crashing JNI. Therefore, it MUST
128 * catch all exceptions.
129 *
130 * @param markerName Name of the parent marker
131 * @param markerFieldPtr C Pointer (converted in long) to marker_field C Structure
132 */
687cc7e7 133 private void addMarkerFieldFromC(String markerFieldName, long markerFieldPtr) {
34eff969 134 // Create a new Jni_markerField object and insert it in the map
0152140d
ASL
135 // the maker field fill itself with LTT data while being constructed
136 try {
c85e8cb2 137 JniMarkerField newMarkerField = allocateNewJniMarkerField( new Jni_C_Pointer_And_Library_Id(thisMarkerPtr.getLibraryId(), markerFieldPtr));
0152140d
ASL
138 markerFieldsArrayList.add(newMarkerField);
139 markerFieldsHashMap.put(markerFieldName, newMarkerField);
140
141 } catch (JniException e) {
9c4eb5f7 142 printlnC(thisMarkerPtr.getLibraryId(), "Failed to add marker field " + markerFieldName + " to marker fields list!(addMarkerFieldFromC)\n\tException raised : " + e.toString() ); //$NON-NLS-1$ //$NON-NLS-2$
0152140d
ASL
143 }
144 }
145
146 // Access to class variable. Most of them doesn't have setter
147 public String getName() {
148 return name;
149 }
150
151 public String getFormatOverview() {
152 return formatOverview;
153 }
154
155 public HashMap<String,JniMarkerField> getMarkerFieldsHashMap() {
156 return markerFieldsHashMap;
157 }
158
159 public ArrayList<JniMarkerField> getMarkerFieldsArrayList() {
160 return markerFieldsArrayList;
161 }
162
163 /**
164 * Pointer to the marker_info C structure.<p>
165 *
166 * The pointer should only be used <u>INTERNALY</u>, do not use unless you
167 * know what you are doing.<p>
168 *
169 * @return The actual (long converted) pointer or NULL
170 *
ce38c104 171 * @see org.eclipse.linuxtools.internal.lttng.jni.common.Jni_C_Pointer_And_Library_Id
0152140d 172 */
c85e8cb2 173 public Jni_C_Pointer_And_Library_Id getMarkerPtr() {
0152140d
ASL
174 return thisMarkerPtr;
175 }
176
177
178 /**
179 * Print information for this JniMarker.
180 * <u>Intended to debug</u><br>
181 *
182 * This function will call Ltt to print, so information printed will be the one from
183 * the C structure, not the one populated in java.<p>
184 *
185 * This function will not throw but will complain loudly if pointer is NULL
186 */
187 public void printMarkerInformation() {
c85e8cb2 188 ltt_printMarker(thisMarkerPtr.getLibraryId(), thisMarkerPtr.getPointer());
0152140d
ASL
189 }
190
191 /**
192 * Print information for ALL marker fields for this marker.
193 * <u>Intended to debug</u><br>
194 *
195 * This function will call Ltt to print, so information printed will be the one from
196 * the C structure, not the one populated in java.
197 */
198 public void printAllMarkerFieldsInformation() {
199 Object[] allMarkersField = markerFieldsArrayList.toArray();
200
201 for (int pos = 0; pos < allMarkersField.length; pos++) {
c85e8cb2 202 printlnC(thisMarkerPtr.getLibraryId(), allMarkersField[pos].toString());
0152140d
ASL
203 }
204 }
205
206 /**
207 * toString() method.
208 * <u>Intended to debug</u><br>
209 *
210 * @return Attributes of the object concatenated in String
211 */
212 @Override
3b38ea61 213 @SuppressWarnings("nls")
0152140d
ASL
214 public String toString() {
215 String returnData = "";
216
217 returnData += "name : " + name + "\n";
218 returnData += "formatOverview : " + formatOverview + "\n";
e0ea56dd
WB
219 returnData += "markerFieldArrayList : " + markerFieldsArrayList.hashCode() + " (size : " + markerFieldsArrayList.size() + " )" + "\n";
220
0152140d
ASL
221 return returnData;
222 }
223
224
c357e4b6
WB
225 // ****************************
226 // **** ABSTRACT FUNCTIONS ****
227 // You MUST override those in your version specific implementation
228
229
230 /**
231 * Function place holder to allocate a new JniMarkerField.<p>
232 * <br>
233 * JniMarkerField constructor is non overridable so we need another overridable function to return the correct version of JniMarkerField.<br>
234 * Effect of this function should be the same (allocate a fresh new JniMarkerField).<br>
235 * <br>
236 * <b>!! Override this with you version specific implementation.</b><br>
237 *
c85e8cb2 238 * @param newMarkerFieldPtr The pointer and library id of an already opened marker_field C Structure
c357e4b6
WB
239 *
240 * @return The newly allocated JniMarkerField of the correct version
241 *
242 * @throws JniException The construction (allocation) failed.
243 *
ce38c104 244 * @see org.eclipse.linuxtools.internal.lttng.jni.common.Jni_C_Pointer_And_Library_Id
c357e4b6
WB
245 * @see org.eclipse.linuxtools.lttng.jni.JniMarkerField
246 */
c85e8cb2 247 public abstract JniMarkerField allocateNewJniMarkerField(Jni_C_Pointer_And_Library_Id newMarkerFieldPtr) throws JniException;
0152140d
ASL
248
249}
This page took 0.040655 seconds and 5 git commands to generate.