-JATTACH_VERSION=2.1
+JATTACH_VERSION=2.2
ifneq ($(findstring Windows,$(OS)),)
CL=cl.exe
### Download
-Binaries are available on the [Releases](https://github.com/apangin/jattach/releases) page.
+Binaries are available on the [Releases](https://github.com/jattach/jattach/releases) page.
On some platforms, you can also [install](#installation) jattach with a package manager.
#### List available jcmd commands
- $ jattach <pid> jcmd "help -all"
+ $ jattach <pid> jcmd help -all
### Installation
-#### FreeBSD
+#### Debian, Ubuntu
-On FreeBSD, you can use the following command to install `jattach` package:
+On Debian and Ubuntu, you can install `jattach` from the official repository:
- $ pkg install jattach
+ # apt install jattach
#### Alpine Linux
-On Alpine Linux, you can use the following command to install `jattach` package from the edge/community repository:
+On Alpine Linux, you can install `jattach` package from the edge/community repository:
- $ apk add --no-cache jattach --repository http://dl-cdn.alpinelinux.org/alpine/edge/community/
+ # apk add --no-cache jattach --repository http://dl-cdn.alpinelinux.org/alpine/edge/community/
#### Archlinux
[jattach](https://aur.archlinux.org/packages/jattach/) package can be installed from [AUR](https://wiki.archlinux.org/index.php/Arch_User_Repository) using one of [AUR helpers](https://wiki.archlinux.org/index.php/AUR_helpers), e.g., `yay`:
- $ yay -S jattach
+ # yay -S jattach
-#### Debian Linux
+#### FreeBSD
-On Debian Linux, you can use the following command to install `jattach` from the [official repository](https://packages.debian.org/search?keywords=jattach):
+On FreeBSD, you can use the following command to install `jattach`:
- $ apt install jattach
+ # pkg install jattach
-Packages are provided for **bullseye** (stable), **bookworm** (testing) and **sid** (unstable).
Name: jattach
-Version: 2.1
+Version: 2.2
Release: 1
Summary: JVM Dynamic Attach utility
Group: Development/Tools
License: ASL 2.0
-URL: https://github.com/apangin/jattach
+URL: https://github.com/jattach/jattach
Vendor: Andrei Pangin
Packager: Vadim Tsesko <incubos@yandex.com>
/usr/bin/jattach
%changelog
+* Wed Jan 10 2024 Andrei Pangin <noreply@pangin.pro> - 2.2-1
+- Automatically concatenate jcmd arguments
+- Fixed attach to OpenJ9 on macOS
+- Fixed container support on Linux 3.x
+
* Mon Jul 25 2022 Vadim Tsesko <incubos@yandex.com> - 2.1-1
- Handle both tabs and spaces when parsing /proc/pid/status
- Socket timeout while reading response from OpenJ9 VM
/*
- * Copyright 2021 Andrei Pangin
+ * Copyright jattach authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
extern int is_openj9_process(int pid);
-extern int jattach_openj9(int pid, int nspid, int argc, char** argv);
-extern int jattach_hotspot(int pid, int nspid, int argc, char** argv);
+extern int jattach_openj9(int pid, int nspid, int argc, char** argv, int print_output);
+extern int jattach_hotspot(int pid, int nspid, int argc, char** argv, int print_output);
+
+int mnt_changed = 0;
__attribute__((visibility("default")))
-int jattach(int pid, int argc, char** argv) {
+int jattach(int pid, int argc, char** argv, int print_output) {
uid_t my_uid = geteuid();
gid_t my_gid = getegid();
uid_t target_uid = my_uid;
// Network and IPC namespaces are essential for OpenJ9 connection.
enter_ns(pid, "net");
enter_ns(pid, "ipc");
- int mnt_changed = enter_ns(pid, "mnt");
+ mnt_changed = enter_ns(pid, "mnt");
// In HotSpot, dynamic attach is allowed only for the clients with the same euid/egid.
// If we are running under root, switch to the required euid/egid automatically.
signal(SIGPIPE, SIG_IGN);
if (is_openj9_process(nspid)) {
- return jattach_openj9(pid, nspid, argc, argv);
+ return jattach_openj9(pid, nspid, argc, argv, print_output);
} else {
- return jattach_hotspot(pid, nspid, argc, argv);
+ return jattach_hotspot(pid, nspid, argc, argv, print_output);
}
}
int main(int argc, char** argv) {
if (argc < 3) {
printf("jattach " JATTACH_VERSION " built on " __DATE__ "\n"
- "Copyright 2021 Andrei Pangin\n"
"\n"
"Usage: jattach <pid> <cmd> [args ...]\n"
"\n"
return 1;
}
- return jattach(pid, argc - 2, argv + 2);
+ return jattach(pid, argc - 2, argv + 2, 1);
}
#endif // JATTACH_VERSION
/*
- * Copyright 2021 Andrei Pangin
+ * Copyright jattach authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include "psutil.h"
+extern int mnt_changed;
+
// Check if remote JVM has already opened socket for Dynamic Attach
static int check_socket(int pid) {
char path[MAX_PATH];
// HotSpot will start Attach listener in response to SIGQUIT if it sees .attach_pid file
static int start_attach_mechanism(int pid, int nspid) {
char path[MAX_PATH];
- snprintf(path, sizeof(path), "/proc/%d/cwd/.attach_pid%d", nspid, nspid);
+ snprintf(path, sizeof(path), "/proc/%d/cwd/.attach_pid%d", mnt_changed > 0 ? nspid : pid, nspid);
int fd = creat(path, 0660);
if (fd == -1 || (close(fd) == 0 && get_file_owner(path) != geteuid())) {
// Send command with arguments to socket
static int write_command(int fd, int argc, char** argv) {
+ char buf[8192];
+ const char* const limit = buf + sizeof(buf);
+
+ // jcmd has 2 arguments maximum; merge excessive arguments into one
+ int cmd_args = argc >= 2 && strcmp(argv[0], "jcmd") == 0 ? 2 : argc >= 4 ? 4 : argc;
+
// Protocol version
- if (write(fd, "1", 2) <= 0) {
- return -1;
- }
+ char* p = stpncpy(buf, "1", sizeof(buf)) + 1;
int i;
- for (i = 0; i < 4; i++) {
- const char* arg = i < argc ? argv[i] : "";
- if (write(fd, arg, strlen(arg) + 1) <= 0) {
+ for (i = 0; i < argc && p < limit; i++) {
+ if (i >= cmd_args) p[-1] = ' ';
+ p = stpncpy(p, argv[i], limit - p) + 1;
+ }
+ for (i = cmd_args; i < 4 && p < limit; i++) {
+ *p++ = 0;
+ }
+
+ const char* q = p < limit ? p : limit;
+ for (p = buf; p < q; ) {
+ ssize_t bytes = write(fd, p, q - p);
+ if (bytes <= 0) {
return -1;
}
+ p += (size_t)bytes;
}
return 0;
}
// Mirror response from remote JVM to stdout
-static int read_response(int fd, int argc, char** argv) {
+static int read_response(int fd, int argc, char** argv, int print_output) {
char buf[8192];
ssize_t bytes = read(fd, buf, sizeof(buf) - 1);
if (bytes == 0) {
result = atoi(strncmp(buf + 2, "return code: ", 13) == 0 ? buf + 15 : buf + 2);
}
- // Mirror JVM response to stdout
- printf("JVM response code = ");
- do {
- fwrite(buf, 1, bytes, stdout);
- bytes = read(fd, buf, sizeof(buf));
- } while (bytes > 0);
- printf("\n");
+ if (print_output) {
+ // Mirror JVM response to stdout
+ printf("JVM response code = ");
+ do {
+ fwrite(buf, 1, bytes, stdout);
+ bytes = read(fd, buf, sizeof(buf));
+ } while (bytes > 0);
+ printf("\n");
+ }
return result;
}
-int jattach_hotspot(int pid, int nspid, int argc, char** argv) {
+int jattach_hotspot(int pid, int nspid, int argc, char** argv, int print_output) {
if (check_socket(nspid) != 0 && start_attach_mechanism(pid, nspid) != 0) {
perror("Could not start attach mechanism");
return 1;
return 1;
}
- printf("Connected to remote JVM\n");
+ if (print_output) {
+ printf("Connected to remote JVM\n");
+ }
if (write_command(fd, argc, argv) != 0) {
perror("Error writing to socket");
return 1;
}
- int result = read_response(fd, argc, argv);
+ int result = read_response(fd, argc, argv, print_output);
close(fd);
return result;
/*
- * Copyright 2021 Andrei Pangin
+ * Copyright jattach authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
}
} else if (strcmp(cmd, "jcmd") == 0) {
- snprintf(buf, bufsize, "ATTACH_DIAGNOSTICS:%s,%s", argc > 1 ? argv[1] : "help", argc > 2 ? argv[2] : "");
+ size_t n = snprintf(buf, bufsize, "ATTACH_DIAGNOSTICS:%s", argc > 1 ? argv[1] : "help");
+ int i;
+ for (i = 2; i < argc && n < bufsize; i++) {
+ n += snprintf(buf + n, bufsize - n, ",%s", argv[i]);
+ }
} else if (strcmp(cmd, "threaddump") == 0) {
snprintf(buf, bufsize, "ATTACH_DIAGNOSTICS:Thread.print,%s", argc > 1 ? argv[1] : "");
}
// Mirror response from remote JVM to stdout
-static int read_response(int fd, const char* cmd) {
+static int read_response(int fd, const char* cmd, int print_output) {
size_t size = 8192;
char* buf = malloc(size);
// AgentOnLoad error code comes right after AgentInitializationException
result = strncmp(buf, "ATTACH_ERR AgentInitializationException", 39) == 0 ? atoi(buf + 39) : -1;
}
- } else if (strncmp(cmd, "ATTACH_DIAGNOSTICS:", 19) == 0) {
+ } else if (strncmp(cmd, "ATTACH_DIAGNOSTICS:", 19) == 0 && print_output) {
char* p = strstr(buf, "openj9_diagnostics.string_result=");
if (p != NULL) {
// The result of a diagnostic command is encoded in Java Properties format
}
}
- buf[off - 1] = '\n';
- fwrite(buf, 1, off, stdout);
+ if (print_output) {
+ buf[off - 1] = '\n';
+ fwrite(buf, 1, off, stdout);
+ }
free(buf);
return result;
// Try IPv6 socket first, then fall back to IPv4
int s = socket(AF_INET6, SOCK_STREAM, 0);
if (s != -1) {
- struct sockaddr_in6 addr = {AF_INET6, 0};
+ struct sockaddr_in6 addr = {0};
+ addr.sin6_family = AF_INET6;
socklen_t addrlen = sizeof(addr);
if (bind(s, (struct sockaddr*)&addr, addrlen) == 0 && listen(s, 0) == 0
&& getsockname(s, (struct sockaddr*)&addr, &addrlen) == 0) {
return s;
}
} else if ((s = socket(AF_INET, SOCK_STREAM, 0)) != -1) {
- struct sockaddr_in addr = {AF_INET, 0};
+ struct sockaddr_in addr = {0};
+ addr.sin_family = AF_INET;
socklen_t addrlen = sizeof(addr);
if (bind(s, (struct sockaddr*)&addr, addrlen) == 0 && listen(s, 0) == 0
&& getsockname(s, (struct sockaddr*)&addr, &addrlen) == 0) {
return stat(path, &stats) == 0;
}
-int jattach_openj9(int pid, int nspid, int argc, char** argv) {
+int jattach_openj9(int pid, int nspid, int argc, char** argv, int print_output) {
int attach_lock = acquire_lock("", "_attachlock");
if (attach_lock < 0) {
perror("Could not acquire attach lock");
notify_semaphore(-1, notif_count);
release_lock(attach_lock);
- printf("Connected to remote JVM\n");
+ if (print_output) {
+ printf("Connected to remote JVM\n");
+ }
char cmd[8192];
translate_command(cmd, sizeof(cmd), argc, argv);
return 1;
}
- int result = read_response(fd, cmd);
+ int result = read_response(fd, cmd, print_output);
if (result != 1) {
detach(fd);
}
/*
- * Copyright 2021 Andrei Pangin
+ * Copyright jattach authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
// Check if /proc/<container-pid>/sched points back to <host-pid>
snprintf(path, sizeof(path), "/proc/%d/root/proc/%s/sched", pid, entry->d_name);
if (sched_get_host_pid(path) == pid) {
- closedir(dir);
- return atoi(entry->d_name);
+ pid = atoi(entry->d_name);
+ break;
}
}
}
closedir(dir);
}
- // Could not find container pid; return host pid as the last resort
return pid;
}
/*
- * Copyright 2021 Andrei Pangin
+ * Copyright jattach authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <sys/types.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+
#define MAX_PATH 1024
extern char tmp_path[];
// -1, if the attempt failed.
int enter_ns(int pid, const char* type);
+#ifdef __cplusplus
+}
+#endif
+
#endif // _PSUTIL_H
/*
- * Copyright 2016 Andrei Pangin
+ * Copyright jattach authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
strcpy(data.strJvm, "jvm");
strcpy(data.strEnqueue, "_JVM_EnqueueOperation");
strcpy(data.pipeName, pipeName);
+ data.args[0][0] = data.args[1][0] = data.args[2][0] = data.args[3][0] = 0;
+ // jcmd has 2 arguments maximum; merge excessive arguments into one
+ int cmd_args = argc >= 2 && strcmp(argv[0], "jcmd") == 0 ? 2 : argc >= 4 ? 4 : argc;
+
+ size_t n = 0;
int i;
- for (i = 0; i < 4; i++) {
- strcpy(data.args[i], i < argc ? argv[i] : "");
+ for (i = 0; i < argc; i++) {
+ if (i < cmd_args) {
+ n = snprintf(data.args[i], sizeof(data.args[i]), "%s", argv[i]);
+ } else if (n < sizeof(data.args[cmd_args - 1])) {
+ n += snprintf(data.args[cmd_args - 1] + n, sizeof(data.args[cmd_args - 1]) - n, " %s", argv[i]);
+ }
}
LPVOID remoteData = VirtualAllocEx(hProcess, NULL, sizeof(CallData), MEM_COMMIT, PAGE_READWRITE);
}
// JVM response is read from the pipe and mirrored to stdout
-static int read_response(HANDLE hPipe) {
+static int read_response(HANDLE hPipe, int print_output) {
ConnectNamedPipe(hPipe, NULL);
char buf[8192];
buf[bytesRead] = 0;
int result = atoi(buf);
- do {
- fwrite(buf, 1, bytesRead, stdout);
- } while (ReadFile(hPipe, buf, sizeof(buf), &bytesRead, NULL));
+ if (print_output) {
+ // Mirror JVM response to stdout
+ printf("JVM response code = ");
+ do {
+ fwrite(buf, 1, bytesRead, stdout);
+ } while (ReadFile(hPipe, buf, sizeof(buf), &bytesRead, NULL));
+ printf("\n");
+ }
return result;
}
-int jattach(int pid, int argc, char** argv) {
+int jattach(int pid, int argc, char** argv, int print_output) {
// When attaching as an Administrator, make sure the target process can connect to our pipe,
// i.e. allow read-write access to everyone. For the complete format description, see
// https://docs.microsoft.com/en-us/windows/win32/secauthz/security-descriptor-string-format
sprintf(pipeName, "\\\\.\\pipe\\javatool%d", GetTickCount());
HANDLE hPipe = CreateNamedPipe(pipeName, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
1, 4096, 8192, NMPWAIT_USE_DEFAULT_WAIT, &sec);
- if (hPipe == NULL) {
+ if (hPipe == INVALID_HANDLE_VALUE) {
print_error("Could not create pipe", GetLastError());
LocalFree(sec.lpSecurityDescriptor);
return 1;
return 1;
}
- printf("Response code = ");
- fflush(stdout);
-
- int result = read_response(hPipe);
- printf("\n");
+ int result = read_response(hPipe, print_output);
CloseHandle(hPipe);
return result;
int main(int argc, char** argv) {
if (argc < 3) {
printf("jattach " JATTACH_VERSION " built on " __DATE__ "\n"
- "Copyright 2021 Andrei Pangin\n"
"\n"
"Usage: jattach <pid> <cmd> [args ...]\n"
"\n"
return 1;
}
- return jattach(pid, argc - 2, argv + 2);
+ return jattach(pid, argc - 2, argv + 2, 1);
}
#endif // JATTACH_VERSION