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

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

Source:LinuxVirtualMachine.java Github

copy

Full Screen

...50 int pid = Integer.parseInt(vmid);51 // Find the socket file. If not found then we attempt to start the52 // attach mechanism in the target VM by sending it a QUIT signal.53 // Then we attempt to find the socket file again.54 path = findSocketFile(pid);55 if (path == null) {56 File f = createAttachFile(pid);57 try {58 // On LinuxThreads each thread is a process and we don't have the59 // pid of the VMThread which has SIGQUIT unblocked. To workaround60 // this we get the pid of the "manager thread" that is created61 // by the first call to pthread_create. This is parent of all62 // threads (except the initial thread).63 if (isLinuxThreads) {64 int mpid = getLinuxThreadsManager(pid);65 assert mpid >= 1;66 sendQuitToChildrenOf(mpid);67 }68 else {69 sendQuitTo(pid);70 }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 }79 catch (InterruptedException ignore) { }80 path = findSocketFile(pid);81 i++;82 }83 while (i <= retries && path == null);84 if (path == null) {85 throw new AttachNotSupportedException(86 "Unable to open socket file: target process not responding " +87 "or HotSpot VM not loaded");88 }89 }90 finally {91 f.delete();92 }93 }94 // Check that the file owner/permission to avoid attaching to95 // bogus process96 checkPermissions(path);97 // Check that we can connect to the process98 // - this ensures we throw the permission denied error now rather than99 // later when we attempt to enqueue a command.100 int s = socket();101 try {102 connect(s, path);103 }104 finally {105 close(s);106 }107 }108 /**109 * Detach from the target VM.110 */111 @Override112 public void detach()113 {114 synchronized (this) {115 if (path != null) {116 path = null;117 }118 }119 }120 // protocol version121 private static final String PROTOCOL_VERSION = "1";122 // known errors123 private static final int ATTACH_ERROR_BADVERSION = 101;124 /**125 * Execute the given command in the target VM.126 */127 @Override128 InputStream execute(String cmd, Object... args) throws AgentLoadException, IOException129 {130 assert args.length <= 3; // includes null131 // did we detach?132 String p;133 synchronized (this) {134 if (path == null) {135 throw new IOException("Detached from target VM");136 }137 p = path;138 }139 // create UNIX socket140 int s = socket();141 // connect to target VM142 try {143 connect(s, p);144 }145 catch (IOException x) {146 close(s);147 throw x;148 }149 IOException ioe = null;150 // connected - write request151 // <ver> <cmd> <args...>152 try {153 writeString(s, PROTOCOL_VERSION);154 writeString(s, cmd);155 for (int i = 0; i < 3; i++) {156 if (i < args.length && args[i] != null) {157 writeString(s, (String) args[i]);158 }159 else {160 writeString(s, "");161 }162 }163 }164 catch (IOException x) {165 ioe = x;166 }167 // Create an input stream to read reply.168 SocketInputStream sis = new SocketInputStream(s);169 // Read the command completion status.170 int completionStatus;171 try {172 completionStatus = readInt(sis);173 }174 catch (IOException x) {175 sis.close();176 if (ioe != null) {177 throw ioe;178 }179 else {180 throw x;181 }182 }183 if (completionStatus != 0) {184 sis.close();185 // In the event of a protocol mismatch then the target VM186 // returns a known error so that we can throw a reasonable error.187 if (completionStatus == ATTACH_ERROR_BADVERSION) {188 throw new IOException("Protocol mismatch with target VM");189 }190 // Special-case the "load" command so that the right exception is thrown.191 if ("load".equals(cmd)) {192 throw new AgentLoadException("Failed to load agent library");193 }194 else {195 throw new IOException("Command failed in target VM");196 }197 }198 // Return the input stream so that the command output can be read.199 return sis;200 }201 /**202 * InputStream for the socket connection to get target VM.203 */204 private final class SocketInputStream extends InputStream205 {206 int s;207 SocketInputStream(int s)208 {209 this.s = s;210 }211 @Override212 public synchronized int read() throws IOException213 {214 byte[] b = new byte[1];215 int n = read(b, 0, 1);216 if (n == 1) {217 return b[0] & 0xff;218 }219 else {220 return -1;221 }222 }223 @Override224 public synchronized int read(byte[] bs, int off, int len) throws IOException225 {226 if (off < 0 || off > bs.length || len < 0 || off + len > bs.length || off + len < 0) {227 throw new IndexOutOfBoundsException();228 }229 else if (len == 0) {230 return 0;231 }232 return LinuxVirtualMachine.read(s, bs, off, len);233 }234 @Override235 public void close() throws IOException236 {237 LinuxVirtualMachine.close(s);238 }239 }240 // Return the socket file for the given process.241 // Checks working directory of process for .java_pid<pid>. If not242 // found it looks in /tmp.243 private String findSocketFile(int pid)244 {245 // First check for a .java_pid<pid> file in the working directory246 // of the target process247 String fn = ".java_pid" + pid;248 String path = "/proc/" + pid + "/cwd/" + fn;249 File f = new File(path);250 if (!f.exists()) {251 // Not found, so try /tmp252 path = "/tmp/" + fn;253 f = new File(path);254 if (!f.exists()) {255 return null; // not found256 }257 }...

Full Screen

Full Screen

findSocketFile

Using AI Code Generation

copy

Full Screen

1import java.io.*;2import java.lang.reflect.*;3import com.sun.tools.attach.*;4public class FindSocketFile {5 public static void main(String[] args) throws Exception {6 if (args.length != 1) {7 System.err.println("Usage: java FindSocketFile <pid>");8 System.exit(1);9 }10 int pid = Integer.parseInt(args[0]);11 String socketFile = findSocketFile(pid);12 System.out.println("Socket file for pid " + pid + " is " + socketFile);13 }14 public static String findSocketFile(int pid) throws Exception {15 String socketFile = null;16 VirtualMachine vm = null;17 try {18 vm = VirtualMachine.attach(String.valueOf(pid));19 Method findSocketFile = vm.getClass().getDeclaredMethod("findSocketFile");20 findSocketFile.setAccessible(true);21 socketFile = (String)findSocketFile.invoke(vm);22 } finally {23 if (vm != null) {24 vm.detach();25 }26 }27 return socketFile;28 }29}

Full Screen

Full Screen

findSocketFile

Using AI Code Generation

copy

Full Screen

1import sun.tools.attach.LinuxVirtualMachine;2import sun.tools.attach.*;3import java.io.*;4import java.lang.management.*;5import java.util.*;6public class jcmd {7 public static void main(String[] args) throws Exception {8 if (args.length != 2) {9 System.err.println("Usage: jcmd <pid> <command>");10 System.exit(1);11 }12 String pid = args[0];13 String command = args[1];14 String socketFile = LinuxVirtualMachine.findSocketFile(pid);15 if (socketFile == null) {16 System.err.println("Can't find socket file for pid " + pid);17 System.exit(1);18 }19 try (VirtualMachine vm = VirtualMachine.attach(pid)) {20 InputStream in = vm.executeCommand("jcmd", command);21 BufferedReader reader = new BufferedReader(new InputStreamReader(in));22 String line;23 while ((line = reader.readLine()) != null) {24 System.out.println(line);25 }26 }27 }28}29import sun.tools.attach.LinuxVirtualMachine;30import sun.tools.attach.*;31import java.io.*;32import java.lang.management.*;33import java.util.*;34public class jcmd {35 public static void main(String[] args) throws Exception {36 if (args.length != 2) {37 System.err.println("Usage: jcmd <pid> <command>");38 System.exit(1);39 }40 String pid = args[0];41 String command = args[1];42 String socketFile = LinuxVirtualMachine.findSocketFile(pid);43 if (socketFile == null) {44 System.err.println("Can't find socket file for pid " + pid);45 System.exit(1);46 }47 try (VirtualMachine vm = VirtualMachine.attach(pid)) {48 InputStream in = vm.executeCommand("jcmd", command);49 BufferedReader reader = new BufferedReader(new InputStreamReader(in));50 String line;51 while ((line = reader.readLine()) != null) {52 System.out.println(line);53 }54 }55 }56}

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