1
package org.eclipse.linuxtools.lttng.jni.factory;
2
/*******************************************************************************
3
* Copyright (c) 2009, 2011 Ericsson, MontaVista Software
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
11
* William Bourque (wbourque@gmail.com) - Initial API and implementation
12
* Yufen Kuo (ykuo@mvista.com) - add support to allow user specify trace library path
13
*******************************************************************************/
17
import org.eclipse.linuxtools.internal.lttng.jni.exception.JniTraceVersionException;
20
* This class is responsible of returning the correct version number of a trace at a certain given path.<p>
22
* The class will call the C library to get the correct version number from the trace.<p>
24
* Lttv library loader (liblttvtraceread_loader.so) and the default Lttv library (liblttvtraceread.so) must be installed on system and available to java.
27
* @author William Bourque
29
public class JniTraceVersion {
31
private static final String LTTVTRACEREAD_LOADER_LIBNAME = "lttvtraceread_loader"; //$NON-NLS-1$
33
// Native access functions
34
protected native void ltt_getTraceVersion(String tracepath);
36
// Variables to store version number
37
private int majorNumber = 0;
38
private int minorNumber = 0;
40
// To store the given tracepath
41
private String tracepath = ""; //$NON-NLS-1$
42
// To store the given trace lib path
43
private String traceLibPath = ""; //$NON-NLS-1$
45
// Was the trace read already?
46
private boolean wasTraceRead = false;
49
* Default constructor.<p>
51
* Do nothing, readVersionFromTrace(path) will need to be called by the user
53
public JniTraceVersion() {
58
* Constructor that takes a tracepath parameter.
61
* This constructor read the version number from the trace, so it might
65
* The <b>directory</b> of the trace to read.
67
* The <b>directory</b> of the trace libraries.
69
* @exception JniTraceVersionException
70
* If the library can not be loaded,if the path is wrong or
71
* if something go wrong during the read.
73
public JniTraceVersion(String newTracepath, String traceLibPath) throws JniTraceVersionException {
74
// Read the version number from the trace
75
readVersionFromTrace(newTracepath, traceLibPath);
81
* @param oldVersion A reference to the JniTraceVersion to copy.
83
public JniTraceVersion(JniTraceVersion oldVersion) {
84
majorNumber = oldVersion.majorNumber;
85
minorNumber = oldVersion.minorNumber;
89
* Read the version from the (already set) tracepath.<p>
91
* This version is used internally and will silently dismiss any exceptions.
94
private void readVersionNumberNofail() {
96
readVersionFromTrace(tracepath, traceLibPath);
98
catch(JniTraceVersionException e) {
99
// Yes, we do ignore exception.
104
* Read the version from the (already set) trace path.
107
* @throws JniTraceVersionException
108
* If the library can not be loaded, if the path is wrong or if
109
* something go wrong during the read.
112
public void readVersionNumber() throws JniTraceVersionException {
113
readVersionFromTrace(tracepath, traceLibPath);
117
* Read the version from a given tracepath.
119
* MajorVersion and MinorVersion should be set after a successful execution
120
* of this function.<br>
122
* @param newTracepath
123
* The path of the new trace
124
* @param newTraceLibPath
125
* The path to the trace reading library to use
126
* @throws JniTraceVersionException
127
* If the library can not be loaded,if the path is wrong or if
128
* something go wrong during the read.
131
public void readVersionFromTrace(String newTracepath, String newTraceLibPath) throws JniTraceVersionException {
133
// Verify that the tracepath isn't obliviously wrong (null or empty)
134
if ( (newTracepath == null) || (newTracepath.equals("") ) ) { //$NON-NLS-1$
135
throw new JniTraceVersionException("ERROR : Tracepath is null or empty! (readVersionNumber)"); //$NON-NLS-1$
137
// Otherwise set the path in case it was changed
138
tracepath = newTracepath;
139
traceLibPath = newTraceLibPath;
142
if (newTraceLibPath == null || newTraceLibPath.isEmpty()) {
143
// Load the C library here.
144
// If LD_LIBRARY_PATH is not set correctly this will raise a java.lang.UnsatisfiedLinkError
145
System.loadLibrary(LTTVTRACEREAD_LOADER_LIBNAME);
147
File loaderLib = new File(newTraceLibPath, System.mapLibraryName(LTTVTRACEREAD_LOADER_LIBNAME));
148
System.load(loaderLib.getCanonicalPath());
150
// Assuming the C library loaded correctly, call the JNI here.
151
ltt_getTraceVersion(tracepath);
153
// We can now assume that the trace was read
156
// The library was unable to load -> Lttv not installed or bad version of it?
157
catch (java.lang.UnsatisfiedLinkError e) {
158
throw new JniTraceVersionException("\nERROR : Could not get trace version. Is the library missing?" + //$NON-NLS-1$
159
"\nMake sure your \"LD_LIBRARY_PATH\" is setted correctly (readVersionNumber)\n"); //$NON-NLS-1$
161
// Something else failed -> Possibly a bad tracepath was given
162
catch (Exception e) {
163
throw new JniTraceVersionException("\nERROR : Call to ltt_getTraceVersion failed. (readVersionNumber)\n"); //$NON-NLS-1$
168
* Get major version number of the trace.<p>
169
* Note : readVersionFromTrace() will be called if it wasn't done but exception will be silently ignored.
171
* @return major version
173
public int getMajor() {
174
if ( wasTraceRead == false ) {
175
readVersionNumberNofail();
182
* Get minor version number of the trace.<p>
183
* Note : readVersionFromTrace() will be called if it wasn't done but exception will be silently ignored.
185
* @return minor version
187
public int getMinor() {
188
if ( wasTraceRead == false ) {
189
readVersionNumberNofail();
196
* Get full version number of the trace.<p>
197
* Note : readVersionFromTrace() will be called if it wasn't done but exception will be silently ignored.
199
* @return Full Version as float
201
public float getVersionAsFloat() {
202
if ( wasTraceRead == false ) {
203
readVersionNumberNofail();
206
return (majorNumber + ((float)minorNumber)/10);
210
* Get full version number of the trace.<p>
211
* Note : readVersionFromTrace() will be called if it wasn't done but exception will be silently ignored.
213
* @return Full Version as string
215
public String getVersionAsString() {
216
if ( wasTraceRead == false ) {
217
readVersionNumberNofail();
220
return majorNumber + "." + minorNumber; //$NON-NLS-1$
224
* Get for the current tracepath
226
* @return The tracepath was are currently using.
228
public String getTracepath() {
233
* Set for the tracepath.<p>
234
* NOTE : Changing this will reset the version number currently loaded.
235
* NOTE2 : readVersionFromTrace() will be called but exception will be silently ignored.
237
* @param newtracepath The net tracepath
239
public void setTracepath(String newtracepath) {
242
wasTraceRead = false;
243
tracepath = newtracepath;
245
// Call the read function. This will fill up all the number if it goes well.
246
readVersionNumberNofail();
250
* This function is be called from the C side to assign the version number the Java variable.
252
private void setTraceVersionFromC(int newMajor, int newMinor) {
253
majorNumber = newMajor;
254
minorNumber = newMinor;
259
@SuppressWarnings("nls")
260
public String toString() {
261
return "JniTraceVersion [" + majorNumber + "." + minorNumber + "]";