Created
February 28, 2019 10:01
-
-
Save Vaysman/6fb03fd0e8a87b08ee92019a952f0c18 to your computer and use it in GitHub Desktop.
How to get all String objects from current VM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package debug; | |
import com.sun.jdi.Bootstrap; | |
import com.sun.jdi.ObjectReference; | |
import com.sun.jdi.ReferenceType; | |
import com.sun.jdi.VirtualMachineManager; | |
import com.sun.jdi.connect.AttachingConnector; | |
import com.sun.jdi.connect.Connector.Argument; | |
import com.sun.jdi.connect.IllegalConnectorArgumentsException; | |
import java.io.IOException; | |
import java.util.List; | |
import java.util.Map; | |
import javax.management.InstanceNotFoundException; | |
// java -cp <path to tools.jar>:./ -Xdebug -Xrunjdwp:transport=dt_socket,address=8888,server=y,suspend=n debug.Main | |
public class Main { | |
public static void main(String[] args) | |
throws InstanceNotFoundException, IOException, IllegalConnectorArgumentsException { | |
VirtualMachineManager manager = Bootstrap.virtualMachineManager(); | |
AttachingConnector connector = manager.attachingConnectors().stream() | |
.filter(attachingConnector -> attachingConnector.transport().name().contentEquals("dt_socket")) | |
.findFirst() | |
.orElseThrow(InstanceNotFoundException::new); | |
Map<String, Argument> defaults = connector.defaultArguments(); | |
Argument port = defaults.get("port"); | |
port.setValue("8888"); | |
com.sun.jdi.VirtualMachine virtualMachine = connector.attach(defaults); | |
List<ReferenceType> string = virtualMachine.classesByName("java.lang.String"); | |
ReferenceType type = string.get(0); | |
List<ObjectReference> instances = type.instances(10); // put 0 to get all strings | |
for (ObjectReference instance : instances) { | |
System.out.println(instance); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment