]> git.sven.stormbind.net Git - sven/jattach.git/commitdiff
New upstream version 2.2 upstream upstream/2.2
authorSven Hoexter <sven@stormbind.net>
Tue, 16 Jan 2024 12:25:21 +0000 (13:25 +0100)
committerSven Hoexter <sven@stormbind.net>
Tue, 16 Jan 2024 12:25:21 +0000 (13:25 +0100)
Makefile
README.md
jattach.spec
src/posix/jattach.c
src/posix/jattach_hotspot.c
src/posix/jattach_openj9.c
src/posix/psutil.c
src/posix/psutil.h
src/windows/jattach.c

index 7ff43df6cdee496660afb9899950779ba50b7aa7..078bca37c221f23d484ec4d05b86634836ff34e1 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-JATTACH_VERSION=2.1
+JATTACH_VERSION=2.2
 
 ifneq ($(findstring Windows,$(OS)),)
   CL=cl.exe
 
 ifneq ($(findstring Windows,$(OS)),)
   CL=cl.exe
index 369945153f6106091cefebc6889bbf52c0e48dbb..958420452d3341ae33bb7f1d58cddeeb8edbde79 100644 (file)
--- a/README.md
+++ b/README.md
@@ -24,7 +24,7 @@ https://docs.oracle.com/javase/8/docs/jdk/api/attach/spec/
 
 ### Download
 
 
 ### 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.
 
 
 On some platforms, you can also [install](#installation) jattach with a package manager.
 
@@ -46,31 +46,30 @@ which takes .jar path and its arguments as a single options string.
 
 #### List available jcmd commands 
 
 
 #### List available jcmd commands 
 
-    $ jattach <pid> jcmd "help -all"
+    $ jattach <pid> jcmd help -all
 
 ### Installation
 
 ### 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
 
 
 #### 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`:
 
 
 #### 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).
index 452ab91236ac653259428e2f8182ba5186b1ec82..fdac74214ee55151667ddcd400945ea137e98e56 100644 (file)
@@ -1,11 +1,11 @@
 Name:          jattach
 Name:          jattach
-Version:       2.1
+Version:       2.2
 Release:       1
 Summary:       JVM Dynamic Attach utility
 
 Group:         Development/Tools
 License:       ASL 2.0
 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>
 
 Vendor:                Andrei Pangin
 Packager:      Vadim Tsesko <incubos@yandex.com>
 
@@ -35,6 +35,11 @@ install -p -m 555 %{_sourcedir}/bin/jattach ${BIN}
 /usr/bin/jattach
 
 %changelog
 /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
 * 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
index 53d9dfba127d352752eef713affd7710af7db30e..a8a851e11d5d5298239f2c1378bba8275fa4acf8 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * 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.
  *
  * 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 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")))
 
 
 __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;
     uid_t my_uid = geteuid();
     gid_t my_gid = getegid();
     uid_t target_uid = my_uid;
@@ -42,7 +44,7 @@ int jattach(int pid, int argc, char** argv) {
     // Network and IPC namespaces are essential for OpenJ9 connection.
     enter_ns(pid, "net");
     enter_ns(pid, "ipc");
     // 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.
 
     // 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.
@@ -58,9 +60,9 @@ int jattach(int pid, int argc, char** argv) {
     signal(SIGPIPE, SIG_IGN);
 
     if (is_openj9_process(nspid)) {
     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 {
     } else {
-        return jattach_hotspot(pid, nspid, argc, argv);
+        return jattach_hotspot(pid, nspid, argc, argv, print_output);
     }
 }
 
     }
 }
 
@@ -69,7 +71,6 @@ int jattach(int pid, int argc, char** argv) {
 int main(int argc, char** argv) {
     if (argc < 3) {
         printf("jattach " JATTACH_VERSION " built on " __DATE__ "\n"
 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"
                "\n"
                "Usage: jattach <pid> <cmd> [args ...]\n"
                "\n"
@@ -86,7 +87,7 @@ int main(int argc, char** argv) {
         return 1;
     }
 
         return 1;
     }
 
-    return jattach(pid, argc - 2, argv + 2);
+    return jattach(pid, argc - 2, argv + 2, 1);
 }
 
 #endif // JATTACH_VERSION
 }
 
 #endif // JATTACH_VERSION
index e23e4609a0e3dcb31e38ad1e12475f31b1413cea..68d8805c05f9005644e9e9c146fff71f7bae03ee 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * 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.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -27,6 +27,8 @@
 #include "psutil.h"
 
 
 #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];
 // Check if remote JVM has already opened socket for Dynamic Attach
 static int check_socket(int pid) {
     char path[MAX_PATH];
@@ -46,7 +48,7 @@ static uid_t get_file_owner(const char* 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];
 // 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())) {
 
     int fd = creat(path, 0660);
     if (fd == -1 || (close(fd) == 0 && get_file_owner(path) != geteuid())) {
@@ -102,23 +104,37 @@ static int connect_socket(int pid) {
 
 // Send command with arguments to socket
 static int write_command(int fd, int argc, char** argv) {
 
 // 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
     // Protocol version
-    if (write(fd, "1", 2) <= 0) {
-        return -1;
-    }
+    char* p = stpncpy(buf, "1", sizeof(buf)) + 1;
 
     int i;
 
     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;
         }
             return -1;
         }
+        p += (size_t)bytes;
     }
     return 0;
 }
 
 // Mirror response from remote JVM to stdout
     }
     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) {
     char buf[8192];
     ssize_t bytes = read(fd, buf, sizeof(buf) - 1);
     if (bytes == 0) {
@@ -146,18 +162,20 @@ static int read_response(int fd, int argc, char** argv) {
         result = atoi(strncmp(buf + 2, "return code: ", 13) == 0 ? buf + 15 : buf + 2);
     }
 
         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;
 }
 
 
     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;
     if (check_socket(nspid) != 0 && start_attach_mechanism(pid, nspid) != 0) {
         perror("Could not start attach mechanism");
         return 1;
@@ -169,7 +187,9 @@ int jattach_hotspot(int pid, int nspid, int argc, char** argv) {
         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");
 
     if (write_command(fd, argc, argv) != 0) {
         perror("Error writing to socket");
@@ -177,7 +197,7 @@ int jattach_hotspot(int pid, int nspid, int argc, char** argv) {
         return 1;
     }
 
         return 1;
     }
 
-    int result = read_response(fd, argc, argv);
+    int result = read_response(fd, argc, argv, print_output);
     close(fd);
 
     return result;
     close(fd);
 
     return result;
index deab4c68f150f041844e1c25de6aaed4a22b602d..90683c503c1d2b65835cf873bb96d82fb8eee718 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * 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.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -47,7 +47,11 @@ static void translate_command(char* buf, size_t bufsize, int argc, char** argv)
         }
 
     } else if (strcmp(cmd, "jcmd") == 0) {
         }
 
     } 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] : "");
 
     } else if (strcmp(cmd, "threaddump") == 0) {
         snprintf(buf, bufsize, "ATTACH_DIAGNOSTICS:Thread.print,%s", argc > 1 ? argv[1] : "");
@@ -123,7 +127,7 @@ static int write_command(int fd, const char* cmd) {
 }
 
 // Mirror response from remote JVM to stdout
 }
 
 // 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);
 
     size_t size = 8192;
     char* buf = malloc(size);
 
@@ -160,7 +164,7 @@ static int read_response(int fd, const char* cmd) {
             // AgentOnLoad error code comes right after AgentInitializationException
             result = strncmp(buf, "ATTACH_ERR AgentInitializationException", 39) == 0 ? atoi(buf + 39) : -1;
         }
             // 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
         char* p = strstr(buf, "openj9_diagnostics.string_result=");
         if (p != NULL) {
             // The result of a diagnostic command is encoded in Java Properties format
@@ -170,8 +174,10 @@ static int read_response(int fd, const char* cmd) {
         }
     }
 
         }
     }
 
-    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;
 
     free(buf);
     return result;
@@ -221,7 +227,8 @@ static int create_attach_socket(int* port) {
     // Try IPv6 socket first, then fall back to IPv4
     int s = socket(AF_INET6, SOCK_STREAM, 0);
     if (s != -1) {
     // 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) {
         socklen_t addrlen = sizeof(addr);
         if (bind(s, (struct sockaddr*)&addr, addrlen) == 0 && listen(s, 0) == 0
                 && getsockname(s, (struct sockaddr*)&addr, &addrlen) == 0) {
@@ -229,7 +236,8 @@ static int create_attach_socket(int* port) {
             return s;
         }
     } else if ((s = socket(AF_INET, SOCK_STREAM, 0)) != -1) {
             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) {
         socklen_t addrlen = sizeof(addr);
         if (bind(s, (struct sockaddr*)&addr, addrlen) == 0 && listen(s, 0) == 0
                 && getsockname(s, (struct sockaddr*)&addr, &addrlen) == 0) {
@@ -373,7 +381,7 @@ int is_openj9_process(int pid) {
     return stat(path, &stats) == 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");
     int attach_lock = acquire_lock("", "_attachlock");
     if (attach_lock < 0) {
         perror("Could not acquire attach lock");
@@ -411,7 +419,9 @@ int jattach_openj9(int pid, int nspid, int argc, char** argv) {
     notify_semaphore(-1, notif_count);
     release_lock(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);
 
     char cmd[8192];
     translate_command(cmd, sizeof(cmd), argc, argv);
@@ -422,7 +432,7 @@ int jattach_openj9(int pid, int nspid, int argc, char** argv) {
         return 1;
     }
 
         return 1;
     }
 
-    int result = read_response(fd, cmd);
+    int result = read_response(fd, cmd, print_output);
     if (result != 1) {
         detach(fd);
     }
     if (result != 1) {
         detach(fd);
     }
index 9beea88f54b095c09db0b33a34985a12d9c4b0d5..d0287f598e2955c4c418be2a1e3cc24b735d5634 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * 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.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -93,15 +93,14 @@ static int alt_lookup_nspid(int pid) {
                 // 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) {
                 // 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);
     }
 
                 }
             }
         }
         closedir(dir);
     }
 
-    // Could not find container pid; return host pid as the last resort
     return pid;
 }
 
     return pid;
 }
 
index fa1c4169ad8925ec334da8dc08214eab18e9420c..5dc3040dc7722c8921e5c83aaba75a8bad0edaab 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * 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.
  *
  * 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>
 
 
 #include <sys/types.h>
 
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #define MAX_PATH 1024
 extern char tmp_path[];
 
 #define MAX_PATH 1024
 extern char tmp_path[];
 
@@ -43,4 +47,8 @@ int get_process_info(int pid, uid_t* uid, gid_t* gid, int* nspid);
 //        -1, if the attempt failed.
 int enter_ns(int pid, const char* type);
 
 //        -1, if the attempt failed.
 int enter_ns(int pid, const char* type);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif // _PSUTIL_H
 #endif // _PSUTIL_H
index 1318a94541a2ef6706bde4d361cc81c4d28385f3..f25f1d6f049f1f2acac970e02850736e0104d82b 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * 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.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -85,10 +85,19 @@ static LPVOID allocate_data(HANDLE hProcess, char* pipeName, int argc, char** ar
     strcpy(data.strJvm, "jvm");
     strcpy(data.strEnqueue, "_JVM_EnqueueOperation");
     strcpy(data.pipeName, pipeName);
     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;
     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);
     }
 
     LPVOID remoteData = VirtualAllocEx(hProcess, NULL, sizeof(CallData), MEM_COMMIT, PAGE_READWRITE);
@@ -203,7 +212,7 @@ static int inject_thread(int pid, char* pipeName, int argc, char** argv) {
 }
 
 // JVM response is read from the pipe and mirrored to stdout
 }
 
 // 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];
     ConnectNamedPipe(hPipe, NULL);
 
     char buf[8192];
@@ -217,14 +226,19 @@ static int read_response(HANDLE hPipe) {
     buf[bytesRead] = 0;
     int result = atoi(buf);
 
     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;
 }
 
 
     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
     // 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
@@ -236,7 +250,7 @@ int jattach(int pid, int argc, char** argv) {
     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);
     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;
         print_error("Could not create pipe", GetLastError());
         LocalFree(sec.lpSecurityDescriptor);
         return 1;
@@ -249,11 +263,7 @@ int jattach(int pid, int argc, char** argv) {
         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;
     CloseHandle(hPipe);
 
     return result;
@@ -264,7 +274,6 @@ int jattach(int pid, int argc, char** argv) {
 int main(int argc, char** argv) {
     if (argc < 3) {
         printf("jattach " JATTACH_VERSION " built on " __DATE__ "\n"
 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"
                "\n"
                "Usage: jattach <pid> <cmd> [args ...]\n"
                "\n"
@@ -281,7 +290,7 @@ int main(int argc, char** argv) {
         return 1;
     }
 
         return 1;
     }
 
-    return jattach(pid, argc - 2, argv + 2);
+    return jattach(pid, argc - 2, argv + 2, 1);
 }
 
 #endif // JATTACH_VERSION
 }
 
 #endif // JATTACH_VERSION