diff --git a/src/java/one/profiler/AsyncProfiler.java b/src/java/one/profiler/AsyncProfiler.java index f7a688c71..47ea4e085 100644 --- a/src/java/one/profiler/AsyncProfiler.java +++ b/src/java/one/profiler/AsyncProfiler.java @@ -48,12 +48,12 @@ public static synchronized AsyncProfiler getInstance(String libPath) { } @Override - public void start(String event, long interval) { + public void start(String event, long interval) throws IllegalStateException { start0(event, interval); } @Override - public void stop() { + public void stop() throws IllegalStateException { stop0(); } @@ -61,7 +61,7 @@ public void stop() { public native long getSamples(); @Override - public String execute(String command) { + public String execute(String command) throws IllegalArgumentException, java.io.IOException { return execute0(command); } @@ -80,9 +80,9 @@ public String dumpFlat(int maxMethods) { return dumpFlat0(maxMethods); } - private native void start0(String event, long interval); - private native void stop0(); - private native String execute0(String command); + private native void start0(String event, long interval) throws IllegalStateException; + private native void stop0() throws IllegalStateException; + private native String execute0(String command) throws IllegalArgumentException, java.io.IOException; private native String dumpCollapsed0(int counter); private native String dumpTraces0(int maxTraces); private native String dumpFlat0(int maxMethods); diff --git a/src/java/one/profiler/AsyncProfilerMXBean.java b/src/java/one/profiler/AsyncProfilerMXBean.java index c8cfce7d5..71a9dcb34 100644 --- a/src/java/one/profiler/AsyncProfilerMXBean.java +++ b/src/java/one/profiler/AsyncProfilerMXBean.java @@ -33,7 +33,7 @@ public interface AsyncProfilerMXBean { long getSamples(); - String execute(String command) throws IllegalArgumentException; + String execute(String command) throws IllegalArgumentException, java.io.IOException; String dumpCollapsed(Counter counter); String dumpTraces(int maxTraces); diff --git a/src/javaApi.cpp b/src/javaApi.cpp index 34d97594b..0d663a7bd 100755 --- a/src/javaApi.cpp +++ b/src/javaApi.cpp @@ -14,7 +14,10 @@ * limitations under the License. */ +#include #include +#include +#include #include "arguments.h" #include "profiler.h" @@ -64,9 +67,21 @@ Java_one_profiler_AsyncProfiler_execute0(JNIEnv* env, jobject unused, jstring co return NULL; } - std::ostringstream out; - Profiler::_instance.runInternal(args, out); - return env->NewStringUTF(out.str().c_str()); + if (args._file == NULL) { + std::ostringstream out; + Profiler::_instance.runInternal(args, out); + return env->NewStringUTF(out.str().c_str()); + } else { + std::ofstream out(args._file, std::ios::out | std::ios::trunc); + if (out.is_open()) { + Profiler::_instance.runInternal(args, out); + out.close(); + return env->NewStringUTF("OK"); + } else { + throw_new(env, "java/io/IOException", strerror(errno)); + return NULL; + } + } } extern "C" JNIEXPORT jstring JNICALL