]> git.sven.stormbind.net Git - sven/jattach.git/blobdiff - src/posix/jattach_openj9.c
Update upstream source from tag 'upstream/2.2'
[sven/jattach.git] / src / posix / jattach_openj9.c
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.
@@ -47,7 +47,11 @@ static void translate_command(char* buf, size_t bufsize, int argc, char** argv)
         }
 
     } 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] : "");
@@ -123,7 +127,7 @@ static int write_command(int fd, const char* cmd) {
 }
 
 // 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);
 
@@ -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;
         }
-    } 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
@@ -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;
@@ -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) {
-        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) {
@@ -229,7 +236,8 @@ static int create_attach_socket(int* port) {
             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) {
@@ -373,7 +381,7 @@ int is_openj9_process(int pid) {
     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");
@@ -411,7 +419,9 @@ int jattach_openj9(int pid, int nspid, int argc, char** argv) {
     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);
@@ -422,7 +432,7 @@ int jattach_openj9(int pid, int nspid, int argc, char** argv) {
         return 1;
     }
 
-    int result = read_response(fd, cmd);
+    int result = read_response(fd, cmd, print_output);
     if (result != 1) {
         detach(fd);
     }