You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JdiTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import com.sun.jdi.*;
  2. import com.sun.jdi.connect.*;
  3. import com.sun.jdi.request.ClassPrepareRequest;
  4. import com.sun.jdi.request.EventRequestManager;
  5. import java.util.*;
  6. import java.io.*;
  7. /**
  8. * @version 1.0
  9. * @author
  10. */
  11. public class JdiTest {
  12. static Thread errThread, outThread;
  13. public static void main(String[] xxx) throws Exception {
  14. LaunchingConnector lc = findLaunchingConnector();
  15. String mainArgs = "Hello";
  16. Map args = connectorArguments(lc, mainArgs);
  17. System.out.println(args);
  18. VirtualMachine vm = lc.launch(args);
  19. System.out.println(vm);
  20. redirectOutput(vm);
  21. vm.resume();
  22. while (vm.classesByName("Hello").size() == 0) {}
  23. ReferenceType rt = (ReferenceType)vm.classesByName("Hello").get(0);
  24. System.out.println(rt);
  25. System.out.println(rt.availableStrata());
  26. try {
  27. System.out.println(rt.sourceDebugExtension());
  28. System.out.println("Names: " + rt.sourceNames("AspectJ"));
  29. } catch (AbsentInformationException aie) {
  30. System.out.println("NO source debug extension");
  31. }
  32. System.out.println("Name: " + rt.sourceName());
  33. for (Iterator i = rt.allLineLocations().iterator(); i.hasNext(); ) {
  34. Location loc = (Location)i.next();
  35. System.out.println(" " + loc.sourceName() + ":" + loc.lineNumber());
  36. }
  37. try {
  38. //eventThread.join();
  39. errThread.join(); // Make sure output is forwarded
  40. outThread.join(); // before we exit
  41. } catch (InterruptedException exc) {
  42. // we don't interrupt
  43. }
  44. }
  45. /**
  46. * Find a com.sun.jdi.CommandLineLaunch connector
  47. */
  48. static LaunchingConnector findLaunchingConnector() {
  49. List connectors = Bootstrap.virtualMachineManager().allConnectors();
  50. Iterator iter = connectors.iterator();
  51. while (iter.hasNext()) {
  52. Connector connector = (Connector) iter.next();
  53. if (connector.name().equals("com.sun.jdi.CommandLineLaunch")) {
  54. return (LaunchingConnector) connector;
  55. }
  56. }
  57. throw new Error("No launching connector");
  58. }
  59. /**
  60. * Return the launching connector's arguments.
  61. */
  62. static Map connectorArguments(LaunchingConnector connector, String mainArgs) {
  63. Map arguments = connector.defaultArguments();
  64. Connector.Argument mainArg = (Connector.Argument) arguments.get("main");
  65. if (mainArg == null) {
  66. throw new Error("Bad launching connector");
  67. }
  68. mainArg.setValue(mainArgs);
  69. // if (watchFields) {
  70. // // We need a VM that supports watchpoints
  71. // Connector.Argument optionArg = (Connector.Argument) arguments.get("options");
  72. // if (optionArg == null) {
  73. // throw new Error("Bad launching connector");
  74. // }
  75. // optionArg.setValue("-classic");
  76. // }
  77. return arguments;
  78. }
  79. static void redirectOutput(VirtualMachine vm) {
  80. Process process = vm.process();
  81. // Copy target's output and error to our output and error.
  82. errThread = new StreamRedirectThread("error reader",
  83. process.getErrorStream(),
  84. System.err);
  85. outThread = new StreamRedirectThread("output reader",
  86. process.getInputStream(),
  87. System.out);
  88. errThread.start();
  89. outThread.start();
  90. }
  91. }
  92. class StreamRedirectThread extends Thread {
  93. private final Reader in;
  94. private final Writer out;
  95. private static final int BUFFER_SIZE = 2048;
  96. /**
  97. * Set up for copy.
  98. * @param name Name of the thread
  99. * @param in Stream to copy from
  100. * @param out Stream to copy to
  101. */
  102. StreamRedirectThread(String name, InputStream in, OutputStream out) {
  103. super(name);
  104. this.in = new InputStreamReader(in);
  105. this.out = new OutputStreamWriter(out);
  106. setPriority(Thread.MAX_PRIORITY-1);
  107. }
  108. /**
  109. * Copy.
  110. */
  111. public void run() {
  112. try {
  113. char[] cbuf = new char[BUFFER_SIZE];
  114. int count;
  115. while ((count = in.read(cbuf, 0, BUFFER_SIZE)) >= 0) {
  116. out.write(cbuf, 0, count);
  117. }
  118. out.flush();
  119. } catch(IOException exc) {
  120. System.err.println("Child I/O Transfer - " + exc);
  121. }
  122. }
  123. }