gnodet commented on code in PR #24663:
URL: https://github.com/apache/camel/pull/24663#discussion_r3571565310


##########
dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/MemoryTab.java:
##########
@@ -437,6 +441,47 @@ private static Span computeTrendSpan(LinkedList<Long> 
hist, long heapCeiling) {
     public void renderFooter(List<Span> spans) {
         hint(spans, "Esc", "back");
         hint(spans, "g", "gc");
+        hint(spans, "h", "heap dump");
+    }
+
+    private void triggerHeapDump() {
+        IntegrationInfo info = ctx.findSelectedIntegration();
+        if (info == null) {
+            return;
+        }
+        notify("Writing heap dump...", false);
+        String pid = info.pid;
+        Thread t = new Thread(() -> {
+            Path outputFile = ctx.getOutputFile(pid);
+            PathUtils.deleteFile(outputFile);
+            JsonObject root = new JsonObject();
+            root.put("action", "heap-dump");
+            Path actionFile = ctx.getActionFile(pid);
+            PathUtils.writeTextSafely(root.toJson(), actionFile);

Review Comment:
   **[Low]** This method is nearly identical to 
`MemoryLeakTab.triggerHeapDump()`. The only difference is that `MemoryLeakTab` 
uses its `startDaemonThread()` helper while this creates the thread inline. 
Consider extracting the shared logic to avoid the two copies drifting apart 
over time.



##########
core/camel-console/src/main/java/org/apache/camel/impl/console/HeapDumpDevConsole.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.impl.console;
+
+import java.io.File;
+import java.lang.management.ManagementFactory;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Map;
+
+import com.sun.management.HotSpotDiagnosticMXBean;
+import org.apache.camel.spi.Configurer;
+import org.apache.camel.spi.Metadata;
+import org.apache.camel.spi.annotations.DevConsole;
+import org.apache.camel.support.console.AbstractDevConsole;
+import org.apache.camel.util.json.JsonObject;
+
+@DevConsole(name = "heap-dump", displayName = "Heap Dump",
+            description = "Write a heap dump (.hprof) file for deep memory 
analysis")
+@Configurer(extended = true)
+public class HeapDumpDevConsole extends AbstractDevConsole {
+
+    private static final DateTimeFormatter TIMESTAMP = 
DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss");
+
+    @Metadata(label = "action", description = "File name for the heap dump 
(without .hprof extension)",
+              javaType = "java.lang.String")
+    public static final String NAME = "name";
+
+    @Metadata(label = "action", description = "Whether to dump only live 
objects (default true)",
+              defaultValue = "true", javaType = "java.lang.Boolean")
+    public static final String LIVE = "live";
+
+    public HeapDumpDevConsole() {
+        super("jvm", "heap-dump", "Heap Dump", "Write a heap dump (.hprof) 
file for deep memory analysis");
+    }
+
+    @Override
+    protected String doCallText(Map<String, Object> options) {
+        JsonObject json = doCallJson(options);
+        String error = json.getString("error");
+        if (error != null) {
+            return "Heap dump failed: " + error;
+        }
+        return "Heap dump written to: " + json.getString("file") + " (" + 
json.getLong("size") + " bytes)";
+    }
+
+    @Override
+    protected JsonObject doCallJson(Map<String, Object> options) {
+        JsonObject root = new JsonObject();
+
+        String name = optionString(options, NAME);
+        if (name == null || name.isBlank()) {

Review Comment:
   **[Low]** The user-supplied `name` is used as-is in the path passed to 
`dumpHeap()`. A value containing path separators (e.g., `../../tmp/dump`) would 
write the heap dump to an arbitrary location. While Camel's security model 
treats CLI users as trusted, a defensive 
`Path.of(name).getFileName().toString()` would prevent accidental writes 
outside the working directory.



##########
core/camel-console/src/main/java/org/apache/camel/impl/console/HeapDumpDevConsole.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.impl.console;
+
+import java.io.File;
+import java.lang.management.ManagementFactory;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Map;
+
+import com.sun.management.HotSpotDiagnosticMXBean;
+import org.apache.camel.spi.Configurer;

Review Comment:
   **[Medium]** Direct compile-time dependency on 
`com.sun.management.HotSpotDiagnosticMXBean`.
   
   The existing `HeapHistogramDevConsole` avoids this by using string-based 
MBean lookup (`new ObjectName("com.sun.management:type=DiagnosticCommand")`), 
which fails gracefully at runtime on non-HotSpot JVMs. This direct import 
creates a hard compile-time dependency that could cause `NoClassDefFoundError` 
on alternative JVMs or in GraalVM native image builds.
   
   Consider using `ManagementFactory.getPlatformMBeanServer()` with reflection 
or string-based invocation to match the pattern used by 
`HeapHistogramDevConsole`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to