From ef5d7213507f8148ae0b3fd7f82ea4afa5695d72 Mon Sep 17 00:00:00 2001
From: hubodao <hubodao@huawei.com>
Date: Sat, 11 Sep 2021 09:56:54 +0800
Subject: [PATCH 13/23] create jfr dump file with pid or timestamp if specified

Summary: <JFR> : create jfr dump file with pid or timestamp if specified
LLT: NA
Patch Type: huawei
Bug url: NA
---
 hotspot/src/share/vm/jfr/dcmd/jfrDcmds.cpp       | 16 ++++++++++++++--
 .../jdk/jfr/jcmd/TestJcmdDumpWithFileName.java   | 16 ++++++++++++++++
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/hotspot/src/share/vm/jfr/dcmd/jfrDcmds.cpp b/hotspot/src/share/vm/jfr/dcmd/jfrDcmds.cpp
index 167405e39..9585de28c 100644
--- a/hotspot/src/share/vm/jfr/dcmd/jfrDcmds.cpp
+++ b/hotspot/src/share/vm/jfr/dcmd/jfrDcmds.cpp
@@ -226,7 +226,13 @@ void JfrDumpFlightRecordingDCmd::execute(DCmdSource source, TRAPS) {
 
   jstring filepath = NULL;
   if (_filename.is_set() && _filename.value() != NULL) {
-    filepath = JfrJavaSupport::new_string(_filename.value(), CHECK);
+    const char* extended_path = make_log_name(_filename.value(), NULL);
+    if (extended_path != NULL) {
+      filepath = JfrJavaSupport::new_string(extended_path, CHECK);
+      FREE_C_HEAP_ARRAY(char, extended_path, mtInternal);
+    } else {
+      filepath = JfrJavaSupport::new_string(_filename.value(), CHECK);
+    }
   }
 
   jobject maxage = NULL;
@@ -394,7 +400,13 @@ void JfrStartFlightRecordingDCmd::execute(DCmdSource source, TRAPS) {
 
   jstring filename = NULL;
   if (_filename.is_set() && _filename.value() != NULL) {
-    filename = JfrJavaSupport::new_string(_filename.value(), CHECK);
+    const char* dumpPath = make_log_name(_filename.value(), NULL);
+    if (dumpPath != NULL) {
+      filename = JfrJavaSupport::new_string(dumpPath, CHECK);
+      FREE_C_HEAP_ARRAY(char, dumpPath, mtInternal);
+    } else {
+      filename = JfrJavaSupport::new_string(_filename.value(), CHECK);
+    }
   }
 
   jobject maxage = NULL;
diff --git a/jdk/test/jdk/jfr/jcmd/TestJcmdDumpWithFileName.java b/jdk/test/jdk/jfr/jcmd/TestJcmdDumpWithFileName.java
index 40ff35b4d..f0c39564f 100644
--- a/jdk/test/jdk/jfr/jcmd/TestJcmdDumpWithFileName.java
+++ b/jdk/test/jdk/jfr/jcmd/TestJcmdDumpWithFileName.java
@@ -48,6 +48,7 @@ public class TestJcmdDumpWithFileName {
         testDumpAll();
         testDumpNamed();
         testDumpNamedWithFilename();
+        testDumpNamedWithFilenameExpansion();
     }
 
     private static void testDumpAll() throws Exception {
@@ -96,6 +97,21 @@ public class TestJcmdDumpWithFileName {
         }
         cleanup();
     }
+    
+    private static void testDumpNamedWithFilenameExpansion() throws Exception {
+        long pid = ProcessTools.getProcessId();
+        Path dumpPath = Paths.get("dumpPath-%p-%t.jfr").toAbsolutePath();
+        try (Recording r = new Recording()) {
+            r.setName("testDumpNamedWithFilenameExpansion");
+            r.setDestination(dumpPath);
+            r.start();
+            JcmdHelper.jcmd("JFR.dump", "name=testDumpNamedWithFilenameExpansion", "filename=" + dumpPath.toString());
+            Stream<Path> stream = Files.find(Paths.get("."), 1, (s, a) -> s.toString()
+                .matches("^.*dumpPath-pid" + pid + ".\\d{4}.\\d{2}.\\d{2}.\\d{2}.\\d{2}.\\d{2}" + ".jfr") && (a.size() > 0L));
+            Asserts.assertTrue(stream.findAny().isPresent());
+        }
+        cleanup();
+    }
 
     private static boolean namedFile(Path dumpFile) throws IOException {
         return Files.exists(dumpFile) && (Files.size(dumpFile) > 0);
-- 
2.22.0

