27 import java.io.IOException; |
27 import java.io.IOException; |
28 import java.nio.file.InvalidPathException; |
28 import java.nio.file.InvalidPathException; |
29 import java.nio.file.Paths; |
29 import java.nio.file.Paths; |
30 |
30 |
31 import jdk.jfr.Recording; |
31 import jdk.jfr.Recording; |
|
32 import jdk.jfr.internal.LogLevel; |
|
33 import jdk.jfr.internal.LogTag; |
|
34 import jdk.jfr.internal.Logger; |
32 import jdk.jfr.internal.SecuritySupport.SafePath; |
35 import jdk.jfr.internal.SecuritySupport.SafePath; |
33 |
36 |
34 /** |
37 /** |
35 * JFR.stop |
38 * JFR.stop |
36 * |
39 * |
37 */ |
40 */ |
38 //Instantiated by native |
41 // Instantiated by native |
39 final class DCmdStop extends AbstractDCmd { |
42 final class DCmdStop extends AbstractDCmd { |
40 |
43 |
41 /** |
44 /** |
42 * Execute JFR.stop |
45 * Execute JFR.stop |
43 * |
46 * |
44 * Requires that either <code>name or <code>id</code> is set. |
47 * Requires that either <code>name or <code>id</code> is set. |
45 * |
48 * |
46 * @param recordingText name or id of the recording to stop. |
49 * @param name name or id of the recording to stop. |
47 * |
50 * |
48 * @param textPath file path where data should be written after recording |
51 * @param filename file path where data should be written after recording has |
49 * has been stopped, or <code>null</code> if recording shouldn't be |
52 * been stopped, or <code>null</code> if recording shouldn't be written |
50 * written to disk. |
53 * to disk. |
51 * @return result text |
54 * @return result text |
52 * |
55 * |
53 * @throws DCmdException if recording could not be stopped |
56 * @throws DCmdException if recording could not be stopped |
54 */ |
57 */ |
55 public String execute(String recordingText, String textPath) throws DCmdException { |
58 public String execute(String name, String filename) throws DCmdException { |
|
59 if (LogTag.JFR_DCMD.shouldLog(LogLevel.DEBUG)) { |
|
60 Logger.log(LogTag.JFR_DCMD, LogLevel.DEBUG, "Executing DCmdStart: name=" + name + ", filename=" + filename); |
|
61 } |
|
62 |
56 try { |
63 try { |
57 SafePath path = resolvePath(textPath, "Failed to stop %s"); |
64 SafePath safePath = null; |
58 Recording recording = findRecording(recordingText); |
65 Recording recording = findRecording(name); |
59 if (textPath != null) { |
66 if (filename != null) { |
60 try { |
67 try { |
61 recording.setDestination(Paths.get(textPath)); |
68 // Ensure path is valid. Don't generate safePath if filename == null, as a user may |
62 } catch (IOException e) { |
69 // want to stop recording without a dump |
63 throw new DCmdException("Failed to stop %s. Could not set destination for \"%s\" to file %s", recording.getName(), textPath, e.getMessage()); |
70 safePath = resolvePath(null, filename); |
|
71 recording.setDestination(Paths.get(filename)); |
|
72 } catch (IOException | InvalidPathException e) { |
|
73 throw new DCmdException("Failed to stop %s. Could not set destination for \"%s\" to file %s", recording.getName(), filename, e.getMessage()); |
64 } |
74 } |
65 } |
75 } |
66 recording.stop(); |
76 recording.stop(); |
67 reportOperationComplete("Stopped", recording, path); |
77 reportOperationComplete("Stopped", recording.getName(), safePath); |
68 recording.close(); |
78 recording.close(); |
69 return getResult(); |
79 return getResult(); |
70 } catch (InvalidPathException | DCmdException e) { |
80 } catch (InvalidPathException | DCmdException e) { |
71 if (textPath != null) { |
81 if (filename != null) { |
72 throw new DCmdException("Could not write recording \"%s\" to file. %s", recordingText, e.getMessage()); |
82 throw new DCmdException("Could not write recording \"%s\" to file. %s", name, e.getMessage()); |
73 } |
83 } |
74 throw new DCmdException(e, "Could not stop recording \"%s\".", recordingText, e.getMessage()); |
84 throw new DCmdException(e, "Could not stop recording \"%s\".", name, e.getMessage()); |
75 } |
85 } |
76 } |
86 } |
77 } |
87 } |