How to use LinuxVirtualMachine class of sun.tools.attach package

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

Source:LinuxVirtualMachine.java Github

copy

Full Screen

...32import java.io.File;33/*34 * Linux implementation of HotSpotVirtualMachine35 */36public class LinuxVirtualMachine extends HotSpotVirtualMachine {37 // "/tmp" is used as a global well-known location for the files38 // .java_pid<pid>. and .attach_pid<pid>. It is important that this39 // location is the same for all processes, otherwise the tools40 // will not be able to find all Hotspot processes.41 // Any changes to this needs to be synchronized with HotSpot.42 private static final String tmpdir = "/tmp";43 // Indicates if this machine uses the old LinuxThreads44 static boolean isLinuxThreads;45 // The patch to the socket file created by the target VM46 String path;47 /**48 * Attaches to the target VM49 */50 LinuxVirtualMachine(AttachProvider provider, String vmid)51 throws AttachNotSupportedException, IOException52 {53 super(provider, vmid);54 // This provider only understands pids55 int pid;56 try {57 pid = Integer.parseInt(vmid);58 } catch (NumberFormatException x) {59 throw new AttachNotSupportedException("Invalid process identifier");60 }61 // Find the socket file. If not found then we attempt to start the62 // attach mechanism in the target VM by sending it a QUIT signal.63 // Then we attempt to find the socket file again.64 path = findSocketFile(pid);65 if (path == null) {66 File f = createAttachFile(pid);67 try {68 // On LinuxThreads each thread is a process and we don't have the69 // pid of the VMThread which has SIGQUIT unblocked. To workaround70 // this we get the pid of the "manager thread" that is created71 // by the first call to pthread_create. This is parent of all72 // threads (except the initial thread).73 if (isLinuxThreads) {74 int mpid;75 try {76 mpid = getLinuxThreadsManager(pid);77 } catch (IOException x) {78 throw new AttachNotSupportedException(x.getMessage());79 }80 assert(mpid >= 1);81 sendQuitToChildrenOf(mpid);82 } else {83 sendQuitTo(pid);84 }85 // give the target VM time to start the attach mechanism86 int i = 0;87 long delay = 200;88 int retries = (int)(attachTimeout() / delay);89 do {90 try {91 Thread.sleep(delay);92 } catch (InterruptedException x) { }93 path = findSocketFile(pid);94 i++;95 } while (i <= retries && path == null);96 if (path == null) {97 throw new AttachNotSupportedException(98 "Unable to open socket file: target process not responding " +99 "or HotSpot VM not loaded");100 }101 } finally {102 f.delete();103 }104 }105 // Check that the file owner/permission to avoid attaching to106 // bogus process107 checkPermissions(path);108 // Check that we can connect to the process109 // - this ensures we throw the permission denied error now rather than110 // later when we attempt to enqueue a command.111 int s = socket();112 try {113 connect(s, path);114 } finally {115 close(s);116 }117 }118 /**119 * Detach from the target VM120 */121 public void detach() throws IOException {122 synchronized (this) {123 if (this.path != null) {124 this.path = null;125 }126 }127 }128 // protocol version129 private final static String PROTOCOL_VERSION = "1";130 // known errors131 private final static int ATTACH_ERROR_BADVERSION = 101;132 /**133 * Execute the given command in the target VM.134 */135 InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {136 assert args.length <= 3; // includes null137 // did we detach?138 String p;139 synchronized (this) {140 if (this.path == null) {141 throw new IOException("Detached from target VM");142 }143 p = this.path;144 }145 // create UNIX socket146 int s = socket();147 // connect to target VM148 try {149 connect(s, p);150 } catch (IOException x) {151 close(s);152 throw x;153 }154 IOException ioe = null;155 // connected - write request156 // <ver> <cmd> <args...>157 try {158 writeString(s, PROTOCOL_VERSION);159 writeString(s, cmd);160 for (int i=0; i<3; i++) {161 if (i < args.length && args[i] != null) {162 writeString(s, (String)args[i]);163 } else {164 writeString(s, "");165 }166 }167 } catch (IOException x) {168 ioe = x;169 }170 // Create an input stream to read reply171 SocketInputStream sis = new SocketInputStream(s);172 // Read the command completion status173 int completionStatus;174 try {175 completionStatus = readInt(sis);176 } catch (IOException x) {177 sis.close();178 if (ioe != null) {179 throw ioe;180 } else {181 throw x;182 }183 }184 if (completionStatus != 0) {185 // read from the stream and use that as the error message186 String message = readErrorMessage(sis);187 sis.close();188 // In the event of a protocol mismatch then the target VM189 // returns a known error so that we can throw a reasonable190 // error.191 if (completionStatus == ATTACH_ERROR_BADVERSION) {192 throw new IOException("Protocol mismatch with target VM");193 }194 // Special-case the "load" command so that the right exception is195 // thrown.196 if (cmd.equals("load")) {197 throw new AgentLoadException("Failed to load agent library");198 } else {199 if (message == null) {200 throw new AttachOperationFailedException("Command failed in target VM");201 } else {202 throw new AttachOperationFailedException(message);203 }204 }205 }206 // Return the input stream so that the command output can be read207 return sis;208 }209 /*210 * InputStream for the socket connection to get target VM211 */212 private class SocketInputStream extends InputStream {213 int s;214 public SocketInputStream(int s) {215 this.s = s;216 }217 public synchronized int read() throws IOException {218 byte b[] = new byte[1];219 int n = this.read(b, 0, 1);220 if (n == 1) {221 return b[0] & 0xff;222 } else {223 return -1;224 }225 }226 public synchronized int read(byte[] bs, int off, int len) throws IOException {227 if ((off < 0) || (off > bs.length) || (len < 0) ||228 ((off + len) > bs.length) || ((off + len) < 0)) {229 throw new IndexOutOfBoundsException();230 } else if (len == 0)231 return 0;232 return LinuxVirtualMachine.read(s, bs, off, len);233 }234 public void close() throws IOException {235 LinuxVirtualMachine.close(s);236 }237 }238 // Return the socket file for the given process.239 private String findSocketFile(int pid) {240 File f = new File(tmpdir, ".java_pid" + pid);241 if (!f.exists()) {242 return null;243 }244 return f.getPath();245 }246 // On Solaris/Linux a simple handshake is used to start the attach mechanism247 // if not already started. The client creates a .attach_pid<pid> file in the248 // target VM's working directory (or temp directory), and the SIGQUIT handler249 // checks for the file.250 private File createAttachFile(int pid) throws IOException {251 String fn = ".attach_pid" + pid;252 String path = "/proc/" + pid + "/cwd/" + fn;253 File f = new File(path);254 try {255 f.createNewFile();256 } catch (IOException x) {257 f = new File(tmpdir, fn);258 f.createNewFile();259 }260 return f;261 }262 /*263 * Write/sends the given to the target VM. String is transmitted in264 * UTF-8 encoding.265 */266 private void writeString(int fd, String s) throws IOException {267 if (s.length() > 0) {268 byte b[];269 try {270 b = s.getBytes("UTF-8");271 } catch (java.io.UnsupportedEncodingException x) {272 throw new InternalError(x);273 }274 LinuxVirtualMachine.write(fd, b, 0, b.length);275 }276 byte b[] = new byte[1];277 b[0] = 0;278 write(fd, b, 0, 1);279 }280 //-- native methods281 static native boolean isLinuxThreads();282 static native int getLinuxThreadsManager(int pid) throws IOException;283 static native void sendQuitToChildrenOf(int pid) throws IOException;284 static native void sendQuitTo(int pid) throws IOException;285 static native void checkPermissions(String path) throws IOException;286 static native int socket() throws IOException;287 static native void connect(int fd, String path) throws IOException;288 static native void close(int fd) throws IOException;...

Full Screen

Full Screen

Source:LinuxAttachProvider.java Github

copy

Full Screen

...25 checkAttachPermission();26 // AttachNotSupportedException will be thrown if the target VM can be determined27 // to be not attachable.28 testAttachable(vmid);29 return new LinuxVirtualMachine(this, vmid);30 }31 public VirtualMachine attachVirtualMachine(VirtualMachineDescriptor vmd)32 throws AttachNotSupportedException, IOException33 {34 if (vmd.provider() != this) {35 throw new AttachNotSupportedException("provider mismatch");36 }37 // To avoid re-checking if the VM if attachable, we check if the descriptor38 // is for a hotspot VM - these descriptors are created by the listVirtualMachines39 // implementation which only returns a list of attachable VMs.40 if (vmd instanceof HotSpotVirtualMachineDescriptor) {41 assert ((HotSpotVirtualMachineDescriptor)vmd).isAttachable();42 checkAttachPermission();43 return new LinuxVirtualMachine(this, vmd.id());44 } else {45 return attachVirtualMachine(vmd.id());46 }47 }48}...

Full Screen

Full Screen

LinuxVirtualMachine

Using AI Code Generation

copy

Full Screen

1import java.io.IOException;2import java.io.InputStream;3import java.io.OutputStream;4import java.io.PrintStream;5import java.lang.management.ManagementFactory;6import java.lang.management.RuntimeMXBean;7import java.lang.reflect.Method;8import java.util.Properties;9import sun.tools.attach.HotSpotVirtualMachine;10import sun.tools.attach.LinuxVirtualMachine;11public class AttachTest {12 public static void main(String[] args) throws Exception {13 Properties props = System.getProperties();14 RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();15 String pid = bean.getName().split("@")[0];16 System.out.println("pid: "+pid);17 String pathToJar = "/home/ashish/Downloads/4.jar";18 String className = "4";19 String[] arguments = new String[]{"hello", "world"};20 String[] classPath = new String[]{pathToJar};21 String[] jvmArgs = new String[]{"-Djava.class.path="+pathToJar};22 String[] jvmEnv = new String[]{"JAVA_HOME=/usr/lib/jvm/java-6-openjdk"};23 String[] jvmCmd = new String[]{"java"};24 LinuxVirtualMachine vm = LinuxVirtualMachine.attach(pid);25 System.out.println("vm: "+vm);26 vm.loadAgent(pathToJar, "hello");27 vm.detach();28 }29}30import java.io.IOException;31import java.io.InputStream;32import java.io.OutputStream;33import java.io.PrintStream;34import java.lang.management.ManagementFactory;35import java.lang.management.RuntimeMXBean;36import java.lang.reflect.Method;37import java.util.Properties;38import sun.tools.attach.HotSpotVirtualMachine;39import sun.tools.attach.LinuxVirtualMachine;40import com.sun.tools.attach.AttachNotSupportedException;41import com.sun.tools.attach.VirtualMachine;42import com.sun.tools.attach.VirtualMachineDescriptor;43public class AttachTest {44 public static void main(String[] args) throws Exception {45 Properties props = System.getProperties();46 RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();47 String pid = bean.getName().split("@")[0];48 System.out.println("pid: "+pid);49 String pathToJar = "/home/ashish/Downloads/4.jar";50 String className = "4";51 String[] arguments = new String[]{"hello", "world"};

Full Screen

Full Screen

LinuxVirtualMachine

Using AI Code Generation

copy

Full Screen

1import sun.tools.attach.*;2import com.sun.tools.attach.*;3import java.util.*;4import java.io.*;5import java.lang.management.*;6import javax.management.*;7import javax.management.remote.*;8public class 4{9 public static void main(String args[]) throws Exception{10 LinuxVirtualMachine vm = LinuxVirtualMachine.attach("pid");11 System.out.println(vm.getSystemProperties());12 }13}14import com.sun.tools.attach.*;15import java.util.*;16import java.io.*;17import java.lang.management.*;18import javax.management.*;19import javax.management.remote.*;20public class 5{21 public static void main(String args[]) throws Exception{22 VirtualMachine vm = VirtualMachine.attach("pid");23 System.out.println(vm.getSystemProperties());24 }25}26import com.sun.tools.attach.*;27import java.util.*;28import java.io.*;29import java.lang.management.*;30import javax.management.*;31import javax.management.remote.*;32public class 6{33 public static void main(String args[]) throws Exception{34 VirtualMachine vm = VirtualMachine.attach("pid");35 System.out.println(vm.getSystemProperties());36 }37}38import com.sun.tools.attach.*;39import java.util.*;40import java.io.*;41import java.lang.management.*;42import javax.management.*;43import javax.management.remote.*;44public class 7{45 public static void main(String args[]) throws Exception{46 VirtualMachine vm = VirtualMachine.attach("pid");47 System.out.println(vm.getSystemProperties());48 }49}50import com.sun.tools.attach.*;51import java.util.*;52import java.io.*;53import java.lang.management.*;54import javax.management.*;55import javax.management.remote.*;56public class 8{57 public static void main(String args[]) throws Exception{58 VirtualMachine vm = VirtualMachine.attach("pid");59 System.out.println(vm.getSystemProperties());60 }61}62import com.sun.tools.attach.*;63import java.util.*;64import java.io.*;65import java.lang.management.*;66import javax.management.*;67import javax.management.remote.*;68public class 9{69 public static void main(String args[]) throws Exception{

Full Screen

Full Screen

LinuxVirtualMachine

Using AI Code Generation

copy

Full Screen

1import java.io.IOException;2import java.lang.management.ManagementFactory;3import java.lang.management.MemoryMXBean;4import java.lang.management.MemoryUsage;5import java.util.Properties;6import java.util.Scanner;7import java.lang.reflect.Field;8import java.lang.reflect.Method;9import sun.tools.attach.*;10import java.lang.management.*;11import java.util.*;12import java.util.List;13import java.lang.reflect.*;14import java.io.*;15import com.sun.tools.attach.*;16import java.lang.management.*;17import java.util.*;18import java.util.List;19import java.lang.reflect.*;20import java.io.*;21import com.sun.tools.attach.*;22import java.lang.management.*;23import java.util.*;24import java.util.List;25import java.lang.reflect.*;26import java.io.*;27import com.sun.tools.attach.*;28import java.lang.management.*;29import java.util.*;30import java.util.List;31import java.lang.reflect.*;32import java.io.*;33import com.sun.tools.attach.*;34import java.lang.management.*;35import java.util.*;36import java.util.List;37import java.lang.reflect.*;38import java.io.*;39import com.sun.tools.attach.*;40import java.lang.management.*;41import java.util.*;42import java.util.List;43import java.lang.reflect.*;44import java.io.*;45import com.sun.tools.attach.*;46import java.lang.management.*;47import java.util.*;48import java.util.List;49import java.lang.reflect.*;50import java.io.*;51import com.sun.tools.attach.*;52import java.lang.management.*;53import java.util.*;54import java.util.List;55import java.lang.reflect.*;56import java.io.*;57import com.sun.tools.attach.*;58import java.lang.management.*;59import java.util.*;60import java.util.List;61import java.lang.reflect.*;62import java.io.*;63import com.sun.tools.attach.*;64import java.lang.management.*;65import java.util.*;66import java.util.List;67import java.lang.reflect.*;68import java.io.*;69import com.sun.tools.attach.*;70import java.lang.management.*;71import java.util.*;72import java.util.List;73import java.lang.reflect.*;74import java.io.*;75import com.sun.tools.attach.*;76import java.lang.management.*;77import java.util.*;78import java.util.List;79import java.lang.reflect.*;80import java.io.*;81import com.sun.tools.attach.*;82import java.lang.management.*;83import java.util.*;84import java.util.List;85import java.lang.reflect.*;86import java.io.*;87import com.sun.tools.attach.*;88import java.lang.management.*;89import java.util.*;90import java.util.List;91import java.lang.reflect.*;92import java.io.*;93import com.sun.tools.attach.*;94import java.lang.management.*;95import java.util.*;96import java.util.List;97import java.lang.reflect.*;98import java.io.*;99import com.sun.tools.attach.*;100import java.lang.management.*;101import java.util.*;

Full Screen

Full Screen

LinuxVirtualMachine

Using AI Code Generation

copy

Full Screen

1import java.io.IOException;2import java.util.List;3import java.util.ArrayList;4import com.sun.tools.attach.VirtualMachine;5import com.sun.tools.attach.VirtualMachineDescriptor;6public class 4 {7 public static void main(String[] args) throws IOException {8 List<VirtualMachineDescriptor> vmds = VirtualMachine.list();9 List<VirtualMachine> vms = new ArrayList<VirtualMachine>();10 for (VirtualMachineDescriptor vmd : vmds) {11 if (vmd.displayName().equals("MyJavaApp")) {12 vms.add(VirtualMachine.attach(vmd));13 }14 }15 for (VirtualMachine vm : vms) {16 System.out.println(vm.id());17 vm.detach();18 }19 }20}21import java.io.IOException;22import java.util.List;23import java.util.ArrayList;24import com.sun.tools.attach.VirtualMachine;25import com.sun.tools.attach.VirtualMachineDescriptor;26public class 5 {27 public static void main(String[] args) throws IOException {28 List<VirtualMachineDescriptor> vmds = VirtualMachine.list();29 List<VirtualMachine> vms = new ArrayList<VirtualMachine>();30 for (VirtualMachineDescriptor vmd : vmds) {31 if (vmd.displayName().equals("MyJavaApp")) {32 vms.add(VirtualMachine.attach(vmd));33 }34 }35 for (VirtualMachine vm : vms) {36 System.out.println(vm.id());37 vm.detach();38 }39 }40}41import java.io.IOException;42import java.util.List;43import java.util.ArrayList;44import com.sun.tools.attach.VirtualMachine;45import com.sun.tools.attach.VirtualMachineDescriptor;46public class 6 {47 public static void main(String[] args) throws IOException {48 List<VirtualMachineDescriptor> vmds = VirtualMachine.list();49 List<VirtualMachine> vms = new ArrayList<VirtualMachine>();50 for (VirtualMachineDescriptor vmd : vmds) {51 if (vmd.displayName().equals("MyJavaApp")) {52 vms.add(VirtualMachine.attach(vmd));53 }54 }55 for (Virtual

Full Screen

Full Screen

LinuxVirtualMachine

Using AI Code Generation

copy

Full Screen

1import com.sun.tools.attach.*;2import java.util.*;3{4public static void main(String args[]) throws Exception5{6List l = VirtualMachine.list();7Iterator it = l.iterator();8while(it.hasNext())9{10VirtualMachineDescriptor vmd = (VirtualMachineDescriptor)it.next();11if(vmd.displayName().equals("HelloWorld"))12{13VirtualMachine vm = VirtualMachine.attach(vmd);14String pid = vm.id();15System.out.println("pid = "+pid);16vm.detach();17break;18}19}20}21}

Full Screen

Full Screen

LinuxVirtualMachine

Using AI Code Generation

copy

Full Screen

1import java.io.*;2import java.util.*;3import sun.tools.attach.*;4public class 4 {5 public static void main(String[] args) throws Exception {6 String pid = args[0];7 LinuxVirtualMachine lvm = LinuxVirtualMachine.attach(pid);8 InputStream in = lvm.remoteDataDump();9 BufferedReader reader = new BufferedReader(new InputStreamReader(in));10 String line;11 while ((line = reader.readLine()) != null) {12 System.out.println(line);13 }14 reader.close();15 lvm.detach();16 }17}18import sun.tools.attach.*;19 at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:198)20 at sun.tools.attach.LinuxVirtualMachine.attach(LinuxVirtualMachine.java:102)

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.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful