From cc9249c85c50116813665d5753d81c3d5f11c9e7 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Mon, 3 Feb 2014 17:19:53 -0800 Subject: Java: Add 'EnvironmentConfiguredTracer' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adding 'ppaml_tracer_init_from_env' (commit b9dfb8d0) makes the Java code substantially more complicated, as it introduces a second mechanism to construct 'Tracer's. While I could simply provide two constructors in the 'Tracer' class, this would make for an ugly and dangerous implementation; after all, the life cycle of a tracer configured explicitly is noticeably different from the life cycle of a tracer configured by sourcing environment variables. Instead, I’ve changed 'Tracer' to be an interface implemented by 'ExplicitlyConfiguredTracer' and 'EnvironmentConfiguredTracer'. This is an API-breaking change. --- bindings/java/build.xml | 2 +- .../com/galois/ppaml/tracer/examples/Simple.java | 3 +- bindings/java/src/c/Makefile.am | 4 +- .../java/src/c/com_galois_ppaml_tracer_Tracer.c | 76 ------------ .../src/c/com_galois_ppaml_tracer_TracerCWrapper.c | 86 +++++++++++++ .../ppaml/tracer/ConfigurationException.java | 34 ++++++ .../ppaml/tracer/EnvironmentConfiguredTracer.java | 84 +++++++++++++ .../ppaml/tracer/ExplicitlyConfiguredTracer.java | 91 ++++++++++++++ .../src/java/com/galois/ppaml/tracer/Phase.java | 12 +- .../src/java/com/galois/ppaml/tracer/Tracer.java | 134 ++------------------- .../com/galois/ppaml/tracer/TracerCWrapper.java | 105 ++++++++++++++++ .../tracer/UndefinedTracerBasePathException.java | 34 ++++++ .../java/com/galois/ppaml/tracer/package-info.java | 2 +- 13 files changed, 454 insertions(+), 213 deletions(-) delete mode 100644 bindings/java/src/c/com_galois_ppaml_tracer_Tracer.c create mode 100644 bindings/java/src/c/com_galois_ppaml_tracer_TracerCWrapper.c create mode 100644 bindings/java/src/java/com/galois/ppaml/tracer/ConfigurationException.java create mode 100644 bindings/java/src/java/com/galois/ppaml/tracer/EnvironmentConfiguredTracer.java create mode 100644 bindings/java/src/java/com/galois/ppaml/tracer/ExplicitlyConfiguredTracer.java create mode 100644 bindings/java/src/java/com/galois/ppaml/tracer/TracerCWrapper.java create mode 100644 bindings/java/src/java/com/galois/ppaml/tracer/UndefinedTracerBasePathException.java diff --git a/bindings/java/build.xml b/bindings/java/build.xml index d4e57f6..59fd854 100644 --- a/bindings/java/build.xml +++ b/bindings/java/build.xml @@ -55,7 +55,7 @@ Oregon, 97204-1622. --> diff --git a/bindings/java/examples/src/com/galois/ppaml/tracer/examples/Simple.java b/bindings/java/examples/src/com/galois/ppaml/tracer/examples/Simple.java index a698573..f4a6b99 100644 --- a/bindings/java/examples/src/com/galois/ppaml/tracer/examples/Simple.java +++ b/bindings/java/examples/src/com/galois/ppaml/tracer/examples/Simple.java @@ -3,6 +3,7 @@ package com.galois.ppaml.tracer.examples; +import com.galois.ppaml.tracer.ExplicitlyConfiguredTracer; import com.galois.ppaml.tracer.Phase; import com.galois.ppaml.tracer.Tracer; @@ -72,7 +73,7 @@ public class Simple { public static void main(String[] args) { // Start ppamltracer. - Tracer tracer = new Tracer("/tmp/simple_report"); + Tracer tracer = new ExplicitlyConfiguredTracer("/tmp/simple_report"); // Run the app. (new Simple()).go(tracer); diff --git a/bindings/java/src/c/Makefile.am b/bindings/java/src/c/Makefile.am index c30a4cf..275d482 100644 --- a/bindings/java/src/c/Makefile.am +++ b/bindings/java/src/c/Makefile.am @@ -24,8 +24,8 @@ lib_LTLIBRARIES = libppamltracer_java.la libppamltracer_java_la_SOURCES = \ $(srcdir)/com_galois_ppaml_tracer_Phase.c \ $(srcdir)/com_galois_ppaml_tracer_Phase.h \ - $(srcdir)/com_galois_ppaml_tracer_Tracer.c \ - $(srcdir)/com_galois_ppaml_tracer_Tracer.h \ + $(srcdir)/com_galois_ppaml_tracer_TracerCWrapper.c \ + $(srcdir)/com_galois_ppaml_tracer_TracerCWrapper.h \ $(srcdir)/pointer.h libppamltracer_java_la_CFLAGS = \ -std=c99 \ diff --git a/bindings/java/src/c/com_galois_ppaml_tracer_Tracer.c b/bindings/java/src/c/com_galois_ppaml_tracer_Tracer.c deleted file mode 100644 index 2c23966..0000000 --- a/bindings/java/src/c/com_galois_ppaml_tracer_Tracer.c +++ /dev/null @@ -1,76 +0,0 @@ -/* com_galois_ppaml_tracer_Tracer.c -- JNI wrapper for com.galois.ppaml.tracer.Tracer - * 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 . - * - * To contact Galois, complete the Web form at - * or write to Galois, Inc., 421 Southwest - * 6th Avenue, Suite 300, Portland, Oregon, 97204-1622. */ - -#include - -#include - -#include -#include - -#include "pointer.h" - -JNIEXPORT -jlong JNICALL Java_com_galois_ppaml_tracer_Tracer_mallocTracer( - JNIEnv *const env, - jclass Tracer) -{ - return jlong_of_pointer(malloc(sizeof(ppaml_tracer_t))); -} - -JNIEXPORT -jint JNICALL Java_com_galois_ppaml_tracer_Tracer_ppaml_1tracer_1init( - JNIEnv *const env, - jclass Tracer, - jlong tracer, - jstring reportNameBase) -{ - jint r; - // Allocate memory. - const char *const cReportNameBase = - (*env)->GetStringUTFChars(env, reportNameBase, NULL); - if (cReportNameBase == NULL) { - r = -1; - goto done; - } - // Perform the operation. - r = ppaml_tracer_init(pointer_of_jlong(tracer), cReportNameBase); - // Clean up. -done: (*env)->ReleaseStringUTFChars(env, reportNameBase, cReportNameBase); - return r; -} - -JNIEXPORT -jint JNICALL Java_com_galois_ppaml_tracer_Tracer_ppaml_1tracer_1done( - JNIEnv *const env, - jclass Tracer, - jlong tracer) -{ - return ppaml_tracer_done(pointer_of_jlong(tracer)); -} - -JNIEXPORT -void JNICALL Java_com_galois_ppaml_tracer_Tracer_freeTracer( - JNIEnv *const env, - jclass Tracer, - jlong tracer) -{ - free(pointer_of_jlong(tracer)); -} diff --git a/bindings/java/src/c/com_galois_ppaml_tracer_TracerCWrapper.c b/bindings/java/src/c/com_galois_ppaml_tracer_TracerCWrapper.c new file mode 100644 index 0000000..19a313c --- /dev/null +++ b/bindings/java/src/c/com_galois_ppaml_tracer_TracerCWrapper.c @@ -0,0 +1,86 @@ +/* com_galois_ppaml_tracer_TracerCWrapper.c -- JNI wrapper for + * com.galois.ppaml.tracer.TracerCWrapper + * 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 . + * + * To contact Galois, complete the Web form at + * or write to Galois, Inc., 421 Southwest + * 6th Avenue, Suite 300, Portland, Oregon, 97204-1622. */ + +#include + +#include + +#include +#include + +#include "pointer.h" + +JNIEXPORT +jlong JNICALL Java_com_galois_ppaml_tracer_TracerCWrapper_mallocTracer( + JNIEnv *const env, + jclass Tracer) +{ + return jlong_of_pointer(malloc(sizeof(ppaml_tracer_t))); +} + +JNIEXPORT +jint JNICALL Java_com_galois_ppaml_tracer_TracerCWrapper_ppaml_1tracer_1init( + JNIEnv *const env, + jclass Tracer, + jlong tracer, + jstring reportNameBase) +{ + jint r; + // Allocate memory. + const char *const cReportNameBase = + (*env)->GetStringUTFChars(env, reportNameBase, NULL); + if (cReportNameBase == NULL) { + r = -1; + goto done; + } + // Perform the operation. + r = ppaml_tracer_init(pointer_of_jlong(tracer), cReportNameBase); + // Clean up. +done: (*env)->ReleaseStringUTFChars(env, reportNameBase, cReportNameBase); + return r; +} + +JNIEXPORT +jint JNICALL Java_com_galois_ppaml_tracer_TracerCWrapper_ppaml_1tracer_1init_1from_1env( + JNIEnv *const env, + jclass Tracer, + jlong tracer) +{ + return ppaml_tracer_init_from_env(pointer_of_jlong(tracer)); +} + +JNIEXPORT +jint JNICALL Java_com_galois_ppaml_tracer_TracerCWrapper_ppaml_1tracer_1done( + JNIEnv *const env, + jclass Tracer, + jlong tracer) +{ + return ppaml_tracer_done(pointer_of_jlong(tracer)); +} + +JNIEXPORT +void JNICALL Java_com_galois_ppaml_tracer_TracerCWrapper_freeTracer( + JNIEnv *const env, + jclass Tracer, + jlong tracer) +{ + free(pointer_of_jlong(tracer)); +} 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 . + * + * To contact Galois, complete the Web form at + * 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 . + * + * To contact Galois, complete the Web form at + * 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. + * + *

The currently-supported environment variables are: + * + *

+ *
PPAMLTRACER_TRACE_BASE
All trace file paths will + * begin with this path.
+ */ +public final class EnvironmentConfiguredTracer extends TracerCWrapper { + + /** + * Creates and initializes an EnvironmentConfiguredTracer. 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. + * + *

Precondition: 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 . + * + * To contact Galois, complete the Web form at + * 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 ExplicitlyConfiguredTracer. 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. + * + *

Precondition: Initialization succeeded. + * + * @throws OutOfMemoryError converting reportNameBase from a + * String to a char * 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 Tracer this phase is associated with. + * The TracerCWrapper 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 Phase. You should not call this; - * call {@link Tracer.createPhase} instead. + * call Tracer.createPhase instead. * - * @param tracer the {@link Tracer} to associate the Phase - * with. + * @param tracer the {@link TracerCWrapper} to associate the + * Phase 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. * - *

ppamltracer is fundamentally a set of stateful operations; a - * Tracer object holds the state ppamltracer needs to operate + *

ppamltracer is fundamentally a set of stateful operations; an object + * implementing Tracer 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 Tracer. 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 malloc to allocate a ppaml_tracer_t. - * - * @return Same as malloc. + * Finalizes and frees the underlying structure. */ - private static native long mallocTracer(); - - /** - * Wraps ppaml_tracer_init. - * - * @return -1 if converting reportNameBase from a - * String to a char * failed; all other return - * codes are the same as ppaml_tracer_init. - */ - private static native int ppaml_tracer_init( - long tracer, - final String reportNameBase); - - /** - * Wraps ppaml_tracer_done. - * - * @return Same as ppaml_tracer_done. - */ - private static native int ppaml_tracer_done(long tracer); - - /** - * Wraps free. - */ - 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. - * - *

Precondition: Initialization succeeded. - * - * @throws OutOfMemoryError converting reportNameBase from a - * String to a char * 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 . + * + * To contact Galois, complete the Web form at + * 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 malloc to allocate a ppaml_tracer_t. + * + * @return Same as malloc. + */ + protected static native long mallocTracer(); + + /** + * Wraps ppaml_tracer_init. + * + * @return -1 if converting reportNameBase from a + * String to a char * failed; all other return + * codes are the same as ppaml_tracer_init. + */ + protected static native int ppaml_tracer_init( + long tracer, + final String reportNameBase); + + /** + * Wraps ppaml_tracer_init_from_env. + * + * @return Same as ppaml_tracer_init_from_env. + */ + protected static native int ppaml_tracer_init_from_env(long tracer); + + /** + * Wraps ppaml_tracer_done. + * + * @return Same as ppaml_tracer_done. + */ + protected static native int ppaml_tracer_done(long tracer); + + /** + * Wraps free. + */ + 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 . + * + * To contact Galois, complete the Web form at + * 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 @@ * *

  *   // 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");
-- 
cgit v1.2.3