Best Powermock code snippet using sun.tools.attach.SolarisVirtualMachine.read
Source:SolarisVirtualMachine.java
...73 long delay = 200;74 int retries = (int)(attachTimeout() / delay);75 do {76 try {77 Thread.sleep(delay);78 } catch (InterruptedException x) { }79 try {80 fd = openDoor(pid);81 } catch (FileNotFoundException fnf2) { }82 i++;83 } while (i <= retries && fd == -1);84 if (fd == -1) {85 throw new AttachNotSupportedException(86 "Unable to open door: target process not responding or " +87 "HotSpot VM not loaded");88 }89 } finally {90 f.delete();91 }92 }93 assert fd >= 0;94 }95 /**96 * Detach from the target VM97 */98 public void detach() throws IOException {99 synchronized (this) {100 if (fd != -1) {101 close(fd);102 fd = -1;103 }104 }105 }106 /**107 * Execute the given command in the target VM.108 */109 InputStream execute(String cmd, Object ... args) throws AgentLoadException, IOException {110 assert args.length <= 3; // includes null111 // first check that we are still attached112 int door;113 synchronized (this) {114 if (fd == -1) {115 throw new IOException("Detached from target VM");116 }117 door = fd;118 }119 // enqueue the command via a door call120 int s = enqueue(door, cmd, args);121 assert s >= 0; // valid file descriptor122 // The door call returns a file descriptor (one end of a socket pair).123 // Create an input stream around it.124 SocketInputStream sis = new SocketInputStream(s);125 // Read the command completion status126 int completionStatus;127 try {128 completionStatus = readInt(sis);129 } catch (IOException ioe) {130 sis.close();131 throw ioe;132 }133 // If non-0 it means an error but we need to special-case the134 // "load" command to ensure that the right exception is thrown.135 if (completionStatus != 0) {136 // read from the stream and use that as the error message137 String message = readErrorMessage(sis);138 sis.close();139 if (cmd.equals("load")) {140 throw new AgentLoadException("Failed to load agent library");141 } else {142 if (message == null) {143 throw new AttachOperationFailedException("Command failed in target VM");144 } else {145 throw new AttachOperationFailedException(message);146 }147 }148 }149 // Return the input stream so that the command output can be read150 return sis;151 }152 // InputStream over a socket153 private class SocketInputStream extends InputStream {154 int s;155 public SocketInputStream(int s) {156 this.s = s;157 }158 public synchronized int read() throws IOException {159 byte b[] = new byte[1];160 int n = this.read(b, 0, 1);161 if (n == 1) {162 return b[0] & 0xff;163 } else {164 return -1;165 }166 }167 public synchronized int read(byte[] bs, int off, int len) throws IOException {168 if ((off < 0) || (off > bs.length) || (len < 0) ||169 ((off + len) > bs.length) || ((off + len) < 0)) {170 throw new IndexOutOfBoundsException();171 } else if (len == 0)172 return 0;173 return SolarisVirtualMachine.read(s, bs, off, len);174 }175 public void close() throws IOException {176 SolarisVirtualMachine.close(s);177 }178 }179 // The door is attached to .java_pid<pid> in the temporary directory.180 private int openDoor(int pid) throws IOException {181 String path = tmpdir + "/.java_pid" + pid;;182 fd = open(path);183 // Check that the file owner/permission to avoid attaching to184 // bogus process185 try {186 checkPermissions(path);187 } catch (IOException ioe) {188 close(fd);189 throw ioe;190 }191 return fd;192 }193 // On Solaris/Linux a simple handshake is used to start the attach mechanism194 // if not already started. The client creates a .attach_pid<pid> file in the195 // target VM's working directory (or temporary directory), and the SIGQUIT196 // handler checks for the file.197 private File createAttachFile(int pid) throws IOException {198 String fn = ".attach_pid" + pid;199 String path = "/proc/" + pid + "/cwd/" + fn;200 File f = new File(path);201 try {202 f.createNewFile();203 } catch (IOException x) {204 f = new File(tmpdir, fn);205 f.createNewFile();206 }207 return f;208 }209 //-- native methods210 static native int open(String path) throws IOException;211 static native void close(int fd) throws IOException;212 static native int read(int fd, byte buf[], int off, int buflen) throws IOException;213 static native void checkPermissions(String path) throws IOException;214 static native void sigquit(int pid) throws IOException;215 // enqueue a command (and arguments) to the given door216 static native int enqueue(int fd, String cmd, Object ... args)217 throws IOException;218 static {219 System.loadLibrary("attach");220 }221}...
read
Using AI Code Generation
1import java.io.*;2public class ReadMethod {3 public static void main(String[] args) {4 try {5 Class<?> clazz = Class.forName("sun.tools.attach.SolarisVirtualMachine");6 Object vm = clazz.getConstructor(String.class).newInstance("1234");7 InputStream is = (InputStream) clazz.getMethod("read").invoke(vm);8 BufferedReader reader = new BufferedReader(new InputStreamReader(is));9 String line;10 while ((line = reader.readLine()) != null) {11 System.out.println(line);12 }13 } catch (Exception e) {14 e.printStackTrace();15 }16 }17}
read
Using AI Code Generation
1import java.io.*;2import sun.tools.attach.*;3public class ReadMemory {4 public static void main(String args[]) {5 if (args.length < 3) {6 System.out.println("Usage: java -cp .:./tools.jar ReadMemory pid address length");7 System.exit(1);8 }9 long address = Long.parseLong(args[1], 16);10 int length = Integer.parseInt(args[2]);11 byte[] memory = new byte[length];12 try {13 SolarisVirtualMachine vm = SolarisVirtualMachine.attach(args[0]);14 vm.read(address, memory);15 vm.detach();16 } catch (Exception e) {17 e.printStackTrace();18 System.exit(1);19 }20 for (int i = 0; i < memory.length; i++) {21 System.out.printf("%02x", memory[i]);22 }23 System.out.println();24 }25}
read
Using AI Code Generation
1import sun.tools.attach.*;2import java.io.IOException;3import java.util.List;4public class AttachTest {5 public static void main(String[] args) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {6 List<SolarisVirtualMachineDescriptor> list = SolarisVirtualMachine.list();7 for(SolarisVirtualMachineDescriptor s : list){8 String jvmId = s.id();9 System.out.println("jvm id: " + jvmId);10 SolarisVirtualMachine s1 = SolarisVirtualMachine.attach(jvmId);11 s1.loadAgent("/home/rajesh/agent.jar");12 s1.detach();13 }14 }15}
read
Using AI Code Generation
1import sun.tools.attach.SolarisVirtualMachine;2import sun.tools.attach.SolarisVirtualMachineDescriptor;3import java.io.IOException;4import java.util.List;5public class Main {6 public static void main(String[] args) throws IOException {7 String pid = args[0];8 String address = args[1];9 String length = args[2];10 byte[] buffer = new byte[Integer.parseInt(length)];11 List<SolarisVirtualMachineDescriptor> descriptors = SolarisVirtualMachine.list();12 for (SolarisVirtualMachineDescriptor descriptor : descriptors) {13 if (descriptor.id().equals(pid)) {14 SolarisVirtualMachine machine = (SolarisVirtualMachine) descriptor.attach();15 machine.read(Long.parseLong(address), buffer, 0, buffer.length);16 System.out.println(new String(buffer));17 }18 }19 }20}
read
Using AI Code Generation
1import java.io.*;2import java.lang.reflect.*;3import java.util.*;4public class ReadProcessMemory {5 public static void main(String[] args) throws Exception {6 if (args.length != 1) {7 System.out.println("Usage: java -cp . ReadProcessMemory <pid>");8 System.exit(1);9 }10 int pid = Integer.parseInt(args[0]);11 System.out.format("Reading memory of process with pid %d12", pid);13 Class<?> clazz = Class.forName("sun.tools.attach.SolarisVirtualMachine");14 Constructor<?> constructor = clazz.getDeclaredConstructor(Integer.TYPE);15 constructor.setAccessible(true);16 Object vm = constructor.newInstance(pid);17 Method readMethod = clazz.getDeclaredMethod("read", Long.TYPE,
read
Using AI Code Generation
1import java.lang.reflect.Field;2import java.lang.reflect.Method;3import java.lang.reflect.Modifier;4import java.util.Arrays;5import java.util.List;6import java.util.ArrayList;7import java.util.Map;8import java.util.HashMap;9import java.util.Scanner;10public class JavaAgent {11 public static void main(String[] args) throws Exception {12 if (args.length != 1) {13 System.out.println("Usage: java JavaAgent <pid>");14 return;15 }16 int pid = Integer.parseInt(args[0]);17 String[] targets = new String[] {18 };19 for (String target : targets) {20 try {21 Class<?> clazz = Class.forName(target);22 Method m = clazz.getDeclaredMethod("read", long.class, int.class);23 m.setAccessible(true);24 long addr = findJVM(m, pid);25 System.out.println("Found jvm.dll at: " + Long.toHexString(addr));26 if (addr != 0) {27 m = clazz.getDeclaredMethod("write", long.class, byte[].class);28 m.setAccessible(true);29 writeShellcode(m, pid, addr);30 }31 break;32 } catch (ClassNotFoundException e) {33 continue;34 }35 }36 }37 private static long findJVM(Method m, int pid) throws Exception {38 String jvm = "jvm.dll";39 byte[] buf = (byte[]) m.invoke(null, 0L, 0x1000);40 int offset = 0;41 while (true) {42 int i = indexOf(buf, jvm.getBytes());43 if (i != -1) {44 return offset + i;45 }46 offset += buf.length;47 buf = (byte[]) m.invoke(null, (long) offset, 0x1000);48 }49 }50 private static void writeShellcode(Method m, int pid, long addr) throws Exception {51 String cmd = "cmd.exe";
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!