Documentation
¶
Overview ¶
Package jattach provides Go bindings for the jattach utility, which allows sending commands to running JVM processes via the Dynamic Attach mechanism.
This package wraps the native C implementation of jattach using CGo, providing a type-safe, idiomatic Go API for interacting with Java Virtual Machines.
Supported platforms: Linux, macOS, and Windows on amd64, arm64, and 386 architectures.
Example usage:
// Get thread dump
output, err := jattach.ThreadDump(12345)
if err != nil {
log.Fatal(err)
}
fmt.Println(output)
// Load Java agent
err = jattach.LoadAgent(12345, "/path/to/agent.jar", "options", false)
if err != nil {
log.Fatal(err)
}
For more information about jattach commands, see: https://github.com/jattach/jattach
Index ¶
- func Attach(pid int, cmd Command, args ...string) (int, error)
- func AttachWithOutput(pid int, cmd Command, args ...string) (string, int, error)
- func ExecuteJcmd(pid int, jcmdArgs ...string) (string, error)
- func GetAgentProperties(pid int) (string, error)
- func GetHeapHistogram(pid int) (string, error)
- func GetSystemProperties(pid int) (string, error)
- func GetThreadDump(pid int) (string, error)
- func HeapDump(pid int, filepath string) error
- func LoadAgent(pid int, agentPath string, options string, absolute bool) error
- func PrintVMFlag(pid int, flagName string) (string, error)
- func SetVMFlag(pid int, flagName string, value string) error
- type Command
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Attach ¶
Attach sends a command to a JVM process. The command output is printed to stdout, errors to stderr. Returns the exit code from the JVM command.
Example ¶
ExampleAttach demonstrates low-level attach with custom command.
package main
import (
"fmt"
"log"
jattach "github.com/vlsi/jattach/v2"
)
func main() {
pid := 12345 // Replace with actual JVM PID
// Execute a command and print output to stdout
exitCode, err := jattach.Attach(pid, jattach.Properties)
if err != nil {
log.Fatal(err)
}
if exitCode != 0 {
fmt.Printf("Command failed with exit code %d\n", exitCode)
}
}
Output:
func AttachWithOutput ¶
AttachWithOutput sends a command to a JVM process and captures the output. Unlike Attach, this function captures both stdout and stderr from the C code via a pipe passed directly as file descriptors. Returns the captured output, exit code, and any error.
Example ¶
ExampleAttachWithOutput demonstrates capturing command output.
package main
import (
"fmt"
"log"
jattach "github.com/vlsi/jattach/v2"
)
func main() {
pid := 12345 // Replace with actual JVM PID
// Execute command and capture output
output, exitCode, err := jattach.AttachWithOutput(pid, jattach.Jcmd, "VM.flags")
if err != nil {
log.Fatal(err)
}
if exitCode != 0 {
fmt.Printf("Command failed with exit code %d\n", exitCode)
return
}
fmt.Println(output)
}
Output:
func ExecuteJcmd ¶
ExecuteJcmd executes an arbitrary jcmd command on the target JVM. The jcmdArgs are passed directly to jcmd.
Example:
output, err := jattach.ExecuteJcmd(12345, "VM.flags", "-all")
Example ¶
ExampleExecuteJcmd demonstrates how to execute a jcmd command.
package main
import (
"fmt"
"log"
jattach "github.com/vlsi/jattach/v2"
)
func main() {
pid := 12345 // Replace with actual JVM PID
// Get VM version
output, err := jattach.ExecuteJcmd(pid, "VM.version")
if err != nil {
log.Fatal(err)
}
fmt.Println(output)
}
Output:
func GetAgentProperties ¶
GetAgentProperties retrieves agent properties from the target JVM.
func GetHeapHistogram ¶
GetHeapHistogram retrieves a heap histogram from the target JVM. This is equivalent to running jmap -histo.
Example ¶
ExampleGetHeapHistogram demonstrates how to get a heap histogram.
package main
import (
"fmt"
"log"
jattach "github.com/vlsi/jattach/v2"
)
func main() {
pid := 12345 // Replace with actual JVM PID
histogram, err := jattach.GetHeapHistogram(pid)
if err != nil {
log.Fatal(err)
}
fmt.Println(histogram)
}
Output:
func GetSystemProperties ¶
GetSystemProperties retrieves all system properties from the target JVM.
Example ¶
ExampleGetSystemProperties demonstrates how to retrieve system properties.
package main
import (
"fmt"
"log"
jattach "github.com/vlsi/jattach/v2"
)
func main() {
pid := 12345 // Replace with actual JVM PID
props, err := jattach.GetSystemProperties(pid)
if err != nil {
log.Fatal(err)
}
fmt.Println(props)
}
Output:
func GetThreadDump ¶
GetThreadDump retrieves a thread dump from the target JVM. This is equivalent to running jstack.
Example ¶
ExampleGetThreadDump demonstrates how to get a thread dump from a JVM process.
package main
import (
"fmt"
"log"
jattach "github.com/vlsi/jattach/v2"
)
func main() {
pid := 12345 // Replace with actual JVM PID
output, err := jattach.GetThreadDump(pid)
if err != nil {
log.Fatal(err)
}
fmt.Println(output)
}
Output:
func HeapDump ¶
HeapDump creates a heap dump file at the specified path. The filepath should be accessible by the target JVM process.
Example ¶
ExampleHeapDump demonstrates how to create a heap dump.
package main
import (
"fmt"
"log"
jattach "github.com/vlsi/jattach/v2"
)
func main() {
pid := 12345 // Replace with actual JVM PID
err := jattach.HeapDump(pid, "/tmp/heapdump.hprof")
if err != nil {
log.Fatal(err)
}
fmt.Println("Heap dump created at /tmp/heapdump.hprof")
}
Output:
func LoadAgent ¶
LoadAgent loads a native agent (.so) or Java agent (.jar) into the target JVM.
Parameters:
- pid: Process ID of the target JVM
- agentPath: Path to the agent file (must be accessible by the target JVM)
- options: Agent options string (can be empty)
- absolute: If true, agentPath is treated as absolute; if false, relative to JVM working directory
Example ¶
ExampleLoadAgent demonstrates how to load a Java agent.
package main
import (
"fmt"
"log"
jattach "github.com/vlsi/jattach/v2"
)
func main() {
pid := 12345 // Replace with actual JVM PID
// Load agent with absolute path
err := jattach.LoadAgent(pid, "/path/to/agent.jar", "option1,option2", true)
if err != nil {
log.Fatal(err)
}
fmt.Println("Agent loaded successfully")
}
Output:
func PrintVMFlag ¶
PrintVMFlag prints the value of a VM flag.
Types ¶
type Command ¶
type Command string
Command represents a JVM attach command type.
const ( // Load loads a native or Java agent into the target JVM Load Command = "load" // ThreadDump prints all stack traces (equivalent to jstack) ThreadDump Command = "threaddump" // DumpHeap dumps the heap (equivalent to jmap) DumpHeap Command = "dumpheap" // InspectHeap prints heap histogram (equivalent to jmap -histo) InspectHeap Command = "inspectheap" // DataDump shows heap and thread summary DataDump Command = "datadump" // Properties prints system properties Properties Command = "properties" // AgentProperties prints agent properties AgentProperties Command = "agentProperties" // SetFlag modifies a manageable VM flag SetFlag Command = "setflag" // PrintFlag prints a VM flag value PrintFlag Command = "printflag" // Jcmd executes an arbitrary jcmd command Jcmd Command = "jcmd" )
Supported JVM attach commands