How to use findSocketFile method of sun.tools.attach.BsdVirtualMachine class

Best Powermock code snippet using sun.tools.attach.BsdVirtualMachine.findSocketFile

Source:BsdVirtualMachine.java Github

copy

Full Screen

...61 }62 // Find the socket file. If not found then we attempt to start the63 // attach mechanism in the target VM by sending it a QUIT signal.64 // Then we attempt to find the socket file again.65 path = findSocketFile(pid);66 if (path == null) {67 File f = new File(tmpdir, ".attach_pid" + pid);68 createAttachFile(f.getPath());69 try {70 sendQuitTo(pid);71 // give the target VM time to start the attach mechanism72 int i = 0;73 long delay = 200;74 int retries = (int)(attachTimeout() / delay);75 do {76 try {77 Thread.sleep(delay);78 } catch (InterruptedException x) { }79 path = findSocketFile(pid);80 i++;81 } while (i <= retries && path == null);82 if (path == null) {83 throw new AttachNotSupportedException(84 "Unable to open socket file: target process not responding " +85 "or HotSpot VM not loaded");86 }87 } finally {88 f.delete();89 }90 }91 // Check that the file owner/permission to avoid attaching to92 // bogus process93 checkPermissions(path);94 // Check that we can connect to the process95 // - this ensures we throw the permission denied error now rather than96 // later when we attempt to enqueue a command.97 int s = socket();98 try {99 connect(s, path);100 } finally {101 close(s);102 }103 }104 /**105 * Detach from the target VM106 */107 public void detach() throws IOException {108 synchronized (this) {109 if (this.path != null) {110 this.path = null;111 }112 }113 }114 // protocol version115 private final static String PROTOCOL_VERSION = "1";116 // known errors117 private final static int ATTACH_ERROR_BADVERSION = 101;118 /**119 * Execute the given command in the target VM.120 */121 InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {122 assert args.length <= 3; // includes null123 // did we detach?124 String p;125 synchronized (this) {126 if (this.path == null) {127 throw new IOException("Detached from target VM");128 }129 p = this.path;130 }131 // create UNIX socket132 int s = socket();133 // connect to target VM134 try {135 connect(s, p);136 } catch (IOException x) {137 close(s);138 throw x;139 }140 IOException ioe = null;141 // connected - write request142 // <ver> <cmd> <args...>143 try {144 writeString(s, PROTOCOL_VERSION);145 writeString(s, cmd);146 for (int i=0; i<3; i++) {147 if (i < args.length && args[i] != null) {148 writeString(s, (String)args[i]);149 } else {150 writeString(s, "");151 }152 }153 } catch (IOException x) {154 ioe = x;155 }156 // Create an input stream to read reply157 SocketInputStream sis = new SocketInputStream(s);158 // Read the command completion status159 int completionStatus;160 try {161 completionStatus = readInt(sis);162 } catch (IOException x) {163 sis.close();164 if (ioe != null) {165 throw ioe;166 } else {167 throw x;168 }169 }170 if (completionStatus != 0) {171 sis.close();172 // In the event of a protocol mismatch then the target VM173 // returns a known error so that we can throw a reasonable174 // error.175 if (completionStatus == ATTACH_ERROR_BADVERSION) {176 throw new IOException("Protocol mismatch with target VM");177 }178 // Special-case the "load" command so that the right exception is179 // thrown.180 if (cmd.equals("load")) {181 throw new AgentLoadException("Failed to load agent library");182 } else {183 throw new IOException("Command failed in target VM");184 }185 }186 // Return the input stream so that the command output can be read187 return sis;188 }189 /*190 * InputStream for the socket connection to get target VM191 */192 private class SocketInputStream extends InputStream {193 int s;194 public SocketInputStream(int s) {195 this.s = s;196 }197 public synchronized int read() throws IOException {198 byte b[] = new byte[1];199 int n = this.read(b, 0, 1);200 if (n == 1) {201 return b[0] & 0xff;202 } else {203 return -1;204 }205 }206 public synchronized int read(byte[] bs, int off, int len) throws IOException {207 if ((off < 0) || (off > bs.length) || (len < 0) ||208 ((off + len) > bs.length) || ((off + len) < 0)) {209 throw new IndexOutOfBoundsException();210 } else if (len == 0)211 return 0;212 return BsdVirtualMachine.read(s, bs, off, len);213 }214 public void close() throws IOException {215 BsdVirtualMachine.close(s);216 }217 }218 // Return the socket file for the given process.219 // Checks temp directory for .java_pid<pid>.220 private String findSocketFile(int pid) {221 String fn = ".java_pid" + pid;222 File f = new File(tmpdir, fn);223 return f.exists() ? f.getPath() : null;224 }225 /*226 * Write/sends the given to the target VM. String is transmitted in227 * UTF-8 encoding.228 */229 private void writeString(int fd, String s) throws IOException {230 if (s.length() > 0) {231 byte b[];232 try {233 b = s.getBytes("UTF-8");234 } catch (java.io.UnsupportedEncodingException x) {...

Full Screen

Full Screen

findSocketFile

Using AI Code Generation

copy

Full Screen

1import sun.tools.attach.BsdVirtualMachine;2import java.io.IOException;3import java.lang.management.ManagementFactory;4import java.util.ArrayList;5import java.util.List;6public class FindSocketFile {7 public static void main(String[] args) throws IOException {8 String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];9 System.out.println(BsdVirtualMachine.findSocketFile(pid));10 }11}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Powermock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful