diff options
Diffstat (limited to 'bindings/java/src/java/com/galois/ppaml/tracer')
8 files changed, 363 insertions, 133 deletions
diff --git a/bindings/java/src/java/com/galois/ppaml/tracer/ConfigurationException.java b/bindings/java/src/java/com/galois/ppaml/tracer/ConfigurationException.java new file mode 100644 index 0000000..57ed28e --- /dev/null +++ b/bindings/java/src/java/com/galois/ppaml/tracer/ConfigurationException.java @@ -0,0 +1,34 @@ +/* ConfigurationException -- signals a configuration failure + * Copyright (C) 2014 Galois, Inc. + * + * This library is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this library. If not, see <http://www.gnu.org/licenses/>. + * + * To contact Galois, complete the Web form at + * <http://corp.galois.com/contact/> or write to Galois, Inc., 421 Southwest + * 6th Avenue, Suite 300, Portland, Oregon, 97204-1622. */ + +package com.galois.ppaml.tracer; + +/** + * Signals a failure related to tracer configuration. + */ +public class ConfigurationException extends java.lang.RuntimeException { + + static final long serialVersionUID = 3219379892261890429L; + + protected ConfigurationException(String s) { + super(s); + } + +} diff --git a/bindings/java/src/java/com/galois/ppaml/tracer/EnvironmentConfiguredTracer.java b/bindings/java/src/java/com/galois/ppaml/tracer/EnvironmentConfiguredTracer.java new file mode 100644 index 0000000..925e3e2 --- /dev/null +++ b/bindings/java/src/java/com/galois/ppaml/tracer/EnvironmentConfiguredTracer.java @@ -0,0 +1,84 @@ +/* EnvironmentConfiguredTracer -- tracer initialized with ppaml_tracer_init_from_env + * Copyright (C) 2014 Galois, Inc. + * + * This library is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this library. If not, see <http://www.gnu.org/licenses/>. + * + * To contact Galois, complete the Web form at + * <http://corp.galois.com/contact/> or write to Galois, Inc., 421 Southwest + * 6th Avenue, Suite 300, Portland, Oregon, 97204-1622. */ + +package com.galois.ppaml.tracer; + +/** + * A tracer initialized automatically from environment variables. + * + * <p>The currently-supported environment variables are: + * + * <dl> + * <dt><code>PPAMLTRACER_TRACE_BASE</code></dt><dd>All trace file paths will + * begin with this path.</dd> + */ +public final class EnvironmentConfiguredTracer extends TracerCWrapper { + + /** + * Creates and initializes an <code>EnvironmentConfiguredTracer</code>. The + * trace report will be stored in Open Trace Format. + */ + public EnvironmentConfiguredTracer() { + super.open(); + } + + ////////////////////////// Low-level life cycle ////////////////////////// + + ///// JNI adapter code ///// + + /** + * Installs data into the structure to make it ready for use. + * + * <p> <b>Precondition:</b> Initialization succeeded. + * + * @throws OTFManagerInitializationException the Open Trace Format file + * manager could not be initialized. + * + * @throws OTFWriterInitializationException the Open Trace Format writer + * could not be initialized. + * + * @throws OTFWriterResolutionException setting the trace resolution failed. + * + * @throws OTFWriterProcessDefinitionException defining the main OTF process + * failed. + */ + @Override + protected void initializeCPointer() { + assert this.cPointer != 0; + final int r = ppaml_tracer_init_from_env(this.cPointer); + switch (r) { + case 0: + break; + case 1: + throw new OTFManagerInitializationException(); + case 2: + throw new OTFWriterInitializationException(); + case 3: + throw new OTFWriterResolutionException(); + case 4: + throw new OTFWriterProcessDefinitionException("main"); + case 5: + throw new UndefinedTracerBasePathException(); + default: + throw new UnexpectedReturnValueError(r); + } + } + +} diff --git a/bindings/java/src/java/com/galois/ppaml/tracer/ExplicitlyConfiguredTracer.java b/bindings/java/src/java/com/galois/ppaml/tracer/ExplicitlyConfiguredTracer.java new file mode 100644 index 0000000..5006e04 --- /dev/null +++ b/bindings/java/src/java/com/galois/ppaml/tracer/ExplicitlyConfiguredTracer.java @@ -0,0 +1,91 @@ +/* ExplicitlyConfiguredTracer -- tracer initialized with ppaml_tracer_init + * Copyright (C) 2014 Galois, Inc. + * + * This library is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this library. If not, see <http://www.gnu.org/licenses/>. + * + * To contact Galois, complete the Web form at + * <http://corp.galois.com/contact/> or write to Galois, Inc., 421 Southwest + * 6th Avenue, Suite 300, Portland, Oregon, 97204-1622. */ + +package com.galois.ppaml.tracer; + +/** + * A tracer initialized with an explicit set of configuration parameters. + */ +public final class ExplicitlyConfiguredTracer extends TracerCWrapper { + + /** + * Base file path for trace report output. + */ + private final String reportNameBase; + + /** + * Creates and initializes an <code>ExplicitlyConfiguredTracer</code>. The + * trace report will be stored in Open Trace Format. + * + * @param reportNameBase all trace file paths will begin with this path. + */ + public ExplicitlyConfiguredTracer(final String reportNameBase) { + this.reportNameBase = reportNameBase; + super.open(); + } + + ////////////////////////// Low-level life cycle ////////////////////////// + + ///// JNI adapter code ///// + + /** + * Installs data into the structure to make it ready for use. + * + * <p> <b>Precondition:</b> Initialization succeeded. + * + * @throws OutOfMemoryError converting <code>reportNameBase</code> from a + * <code>String</code> to a <code>char *</code> failed. + * + * @throws OTFManagerInitializationException the Open Trace Format file + * manager could not be initialized. + * + * @throws OTFWriterInitializationException the Open Trace Format writer + * could not be initialized. + * + * @throws OTFWriterResolutionException setting the trace resolution failed. + * + * @throws OTFWriterProcessDefinitionException defining the main OTF process + * failed. + */ + @Override + protected void initializeCPointer() { + assert this.cPointer != 0; + assert reportNameBase != null; + final int r = ppaml_tracer_init(this.cPointer, reportNameBase); + switch (r) { + case 0: + break; + case -1: + throw new java.lang.OutOfMemoryError( + "failed to convert Java string to C string"); + case 1: + throw new OTFManagerInitializationException(); + case 2: + throw new OTFWriterInitializationException(); + case 3: + throw new OTFWriterResolutionException(); + case 4: + throw new OTFWriterProcessDefinitionException("main"); + default: + throw new UnexpectedReturnValueError(r); + } + } + +} diff --git a/bindings/java/src/java/com/galois/ppaml/tracer/Phase.java b/bindings/java/src/java/com/galois/ppaml/tracer/Phase.java index 23ed2f4..019704f 100644 --- a/bindings/java/src/java/com/galois/ppaml/tracer/Phase.java +++ b/bindings/java/src/java/com/galois/ppaml/tracer/Phase.java @@ -27,9 +27,9 @@ package com.galois.ppaml.tracer; public final class Phase extends CWrapper { /** - * The <code>Tracer</code> this phase is associated with. + * The <code>TracerCWrapper</code> this phase is associated with. */ - private final Tracer tracer; + private final TracerCWrapper tracer; /** * The name of the phase. @@ -38,14 +38,14 @@ public final class Phase extends CWrapper { /** * Creates and initializes a <code>Phase</code>. You should not call this; - * call {@link Tracer.createPhase} instead. + * call <code>Tracer.createPhase</code> instead. * - * @param tracer the {@link Tracer} to associate the <code>Phase</code> - * with. + * @param tracer the {@link TracerCWrapper} to associate the + * <code>Phase</code> with. * * @param name the name of the phase. */ - protected Phase(Tracer tracer, final String name) { + protected Phase(TracerCWrapper tracer, final String name) { this.tracer = tracer; this.name = name; super.open(); diff --git a/bindings/java/src/java/com/galois/ppaml/tracer/Tracer.java b/bindings/java/src/java/com/galois/ppaml/tracer/Tracer.java index fe0a180..24ca8e7 100644 --- a/bindings/java/src/java/com/galois/ppaml/tracer/Tracer.java +++ b/bindings/java/src/java/com/galois/ppaml/tracer/Tracer.java @@ -1,4 +1,4 @@ -/* Tracer -- wrapped ppaml_tracer_t +/* Tracer -- Java analogue to ppaml_tracer_t * Copyright (C) 2014 Galois, Inc. * * This library is free software: you can redistribute it and/or modify it @@ -21,29 +21,13 @@ package com.galois.ppaml.tracer; /** - * State class for ppamltracer. + * State bundle for ppamltracer. * - * <p> ppamltracer is fundamentally a set of stateful operations; a - * <code>Tracer</code> object holds the state ppamltracer needs to operate + * <p> ppamltracer is fundamentally a set of stateful operations; an object + * implementing <code>Tracer</code> holds the state ppamltracer needs to operate * properly. */ -public final class Tracer extends CWrapper { - - /** - * Base file path for trace report output. - */ - private final String reportNameBase; - - /** - * Creates and initializes a <code>Tracer</code>. The trace report will be - * stored in Open Trace Format. - * - * @param reportNameBase all trace file paths will begin with this path. - */ - public Tracer(final String reportNameBase) { - this.reportNameBase = reportNameBase; - super.open(); - } +public interface Tracer { /** * Creates and initializes a {@link Phase} associated with this @@ -51,113 +35,11 @@ public final class Tracer extends CWrapper { * * @param name the name of the phase. */ - public Phase createPhase(final String name) { - return new Phase(this, name); - } - - - ////////////////////////// Low-level life cycle ////////////////////////// - - ///// JNI shared library ///// - - static { - System.loadLibrary("ppamltracer_java"); - } + public Phase createPhase(final String name); /** - * Wraps <code>malloc</code> to allocate a <code>ppaml_tracer_t</code>. - * - * @return Same as <code>malloc</code>. + * Finalizes and frees the underlying structure. */ - private static native long mallocTracer(); - - /** - * Wraps <code>ppaml_tracer_init</code>. - * - * @return -1 if converting <code>reportNameBase</code> from a - * <code>String</code> to a <code>char *</code> failed; all other return - * codes are the same as <code>ppaml_tracer_init</code>. - */ - private static native int ppaml_tracer_init( - long tracer, - final String reportNameBase); - - /** - * Wraps <code>ppaml_tracer_done</code>. - * - * @return Same as <code>ppaml_tracer_done</code>. - */ - private static native int ppaml_tracer_done(long tracer); - - /** - * Wraps <code>free</code>. - */ - private static native void freeTracer(long tracer); - - - ///// JNI adapter code ///// - - @Override - protected void allocateCPointer() { - this.cPointer = mallocTracer(); - } - - /** - * Installs data into the structure to make it ready for use. - * - * <p> <b>Precondition:</b> Initialization succeeded. - * - * @throws OutOfMemoryError converting <code>reportNameBase</code> from a - * <code>String</code> to a <code>char *</code> failed. - * - * @throws OTFManagerInitializationException the Open Trace Format file - * manager could not be initialized. - * - * @throws OTFWriterInitializationException the Open Trace Format writer - * could not be initialized. - * - * @throws OTFWriterResolutionException setting the trace resolution failed. - * - * @throws OTFWriterProcessDefinitionException defining the main OTF process - * failed. - */ - @Override - protected void initializeCPointer() { - assert this.cPointer != 0; - assert reportNameBase != null; - final int r = ppaml_tracer_init(this.cPointer, reportNameBase); - switch (r) { - case 0: - break; - case -1: - throw new java.lang.OutOfMemoryError( - "failed to convert Java string to C string"); - case 1: - throw new OTFManagerInitializationException(); - case 2: - throw new OTFWriterInitializationException(); - case 3: - throw new OTFWriterResolutionException(); - case 4: - throw new OTFWriterProcessDefinitionException("main"); - default: - throw new UnexpectedReturnValueError(r); - } - } - - @Override - protected void finalizeCPointer() { - assert this.cPointer != 0; - final int r = ppaml_tracer_done(this.cPointer); - if (r != 0) { - throw new UnexpectedReturnValueError(r); - } - } - - @Override - protected void freeCPointer() { - assert this.cPointer != 0; - freeTracer(this.cPointer); - } + public void close(); } diff --git a/bindings/java/src/java/com/galois/ppaml/tracer/TracerCWrapper.java b/bindings/java/src/java/com/galois/ppaml/tracer/TracerCWrapper.java new file mode 100644 index 0000000..4f4dc0a --- /dev/null +++ b/bindings/java/src/java/com/galois/ppaml/tracer/TracerCWrapper.java @@ -0,0 +1,105 @@ +/* TracerCWrapper -- wrapped ppaml_tracer_t + * Copyright (C) 2014 Galois, Inc. + * + * This library is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this library. If not, see <http://www.gnu.org/licenses/>. + * + * To contact Galois, complete the Web form at + * <http://corp.galois.com/contact/> or write to Galois, Inc., 421 Southwest + * 6th Avenue, Suite 300, Portland, Oregon, 97204-1622. */ + +package com.galois.ppaml.tracer; + +/** + * Abstract base class for all tracer implementations. + */ +abstract class TracerCWrapper extends CWrapper implements Tracer { + + @Override + public Phase createPhase(final String name) { + return new Phase(this, name); + } + + + ////////////////////////// Low-level life cycle ////////////////////////// + + ///// JNI shared library ///// + + static { + System.loadLibrary("ppamltracer_java"); + } + + /** + * Wraps <code>malloc</code> to allocate a <code>ppaml_tracer_t</code>. + * + * @return Same as <code>malloc</code>. + */ + protected static native long mallocTracer(); + + /** + * Wraps <code>ppaml_tracer_init</code>. + * + * @return -1 if converting <code>reportNameBase</code> from a + * <code>String</code> to a <code>char *</code> failed; all other return + * codes are the same as <code>ppaml_tracer_init</code>. + */ + protected static native int ppaml_tracer_init( + long tracer, + final String reportNameBase); + + /** + * Wraps <code>ppaml_tracer_init_from_env</code>. + * + * @return Same as <code>ppaml_tracer_init_from_env</code>. + */ + protected static native int ppaml_tracer_init_from_env(long tracer); + + /** + * Wraps <code>ppaml_tracer_done</code>. + * + * @return Same as <code>ppaml_tracer_done</code>. + */ + protected static native int ppaml_tracer_done(long tracer); + + /** + * Wraps <code>free</code>. + */ + protected static native void freeTracer(long tracer); + + + ///// JNI adapter code ///// + + @Override + protected void allocateCPointer() { + this.cPointer = mallocTracer(); + } + + @Override + protected abstract void initializeCPointer(); + + @Override + protected void finalizeCPointer() { + assert this.cPointer != 0; + final int r = ppaml_tracer_done(this.cPointer); + if (r != 0) { + throw new UnexpectedReturnValueError(r); + } + } + + @Override + protected void freeCPointer() { + assert this.cPointer != 0; + freeTracer(this.cPointer); + } + +} diff --git a/bindings/java/src/java/com/galois/ppaml/tracer/UndefinedTracerBasePathException.java b/bindings/java/src/java/com/galois/ppaml/tracer/UndefinedTracerBasePathException.java new file mode 100644 index 0000000..1a67826 --- /dev/null +++ b/bindings/java/src/java/com/galois/ppaml/tracer/UndefinedTracerBasePathException.java @@ -0,0 +1,34 @@ +/* UndefinedTracerBasePathException -- no tracer path specified + * Copyright (C) 2014 Galois, Inc. + * + * This library is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this library. If not, see <http://www.gnu.org/licenses/>. + * + * To contact Galois, complete the Web form at + * <http://corp.galois.com/contact/> or write to Galois, Inc., 421 Southwest + * 6th Avenue, Suite 300, Portland, Oregon, 97204-1622. */ + +package com.galois.ppaml.tracer; + +/** + * Indicates that a tracer attempted to initialize with an undefined path. + */ +public class UndefinedTracerBasePathException extends ConfigurationException { + + static final long serialVersionUID = 8041122764340259905L; + + protected UndefinedTracerBasePathException() { + super("attempted to configure tracer with undefined base path"); + } + +} diff --git a/bindings/java/src/java/com/galois/ppaml/tracer/package-info.java b/bindings/java/src/java/com/galois/ppaml/tracer/package-info.java index f922274..7c0d6e5 100644 --- a/bindings/java/src/java/com/galois/ppaml/tracer/package-info.java +++ b/bindings/java/src/java/com/galois/ppaml/tracer/package-info.java @@ -30,7 +30,7 @@ * * <pre> * // Create report. - * Tracer tracer = new Tracer("/tmp/my_report"); + * Tracer tracer = new ExplicitlyConfiguredTracer("/tmp/my_report"); * * // Register phase 1 and do stuff. * Phase phase = tracer.createPhase("phase 1"); |