title: Change Variable Value in JVM with GDB
body: <p>Currently I have a simple Java program:</p>
  
  <pre><code>public class Test {
    public static void main(String[] args) {
      boolean test = true;
      while (test) {
        System.out.println("Hello World");
        try { Thread.sleep(1000); } catch (Exception e) {}
      }
      System.out.println("Bye-bye");
    }
  }
  </code></pre>
  
  <p>It prints "Hello World" every second. I would like to use gdb to attach to the process and make a memory patch to stop it with "Bye-bye" printed.</p>
  
  <p>I know GDB can get created VMs (JNI_GetCreatedVMs) from its console, the env object is also available via the API of GetEnv. <strong>How can I</strong> find the <code>test</code> variable address in JVM and set it to false (this is optional) to <strong>make the program exit normally?</strong> Not sure if API like AttachCurrentThread, class like HotSpotVirtualMachine, tools like jmap or jstack can help?</p>
  
  <p>And there is no debug option, assume the simple program running in production with <code>java -cp . Test</code>.</p>
  
  <p>Thanks in advance for any guidance. :)</p>
  
  <hr>
  
  <h3>additional info (track state)</h3>
  
  <ul>
  <li><code>jmap -dump:file=hex &lt;pid&gt; &amp;&amp; jhat hex</code> and browse at <a href="http://localhost:7000" rel="nofollow">http://localhost:7000</a>; cannot find any reference to <code>test</code> (it is not an object and just an instance of <code>class Z</code>)</li>
  <li><code>jstack &lt;pid&gt;</code> can get the tid of main thread (0x7fa412002000) and <code>jhat hex</code> has the object of the java.lang.Thread of main (0x76ab05c40)</li>
  <li><code>java.lang.Thread</code> has a native method <code>start0</code> which invokes hotspot method of <code>JVM_StartThread</code> (hotspot/src/share/vm/prims/jvm.cpp), there is a class <code>JavaThread</code> may contain the memory structure for local variables in thread stack.</li>
  <li>if <code>private static boolean test = true;</code>; then <code>JNI_GetCreatedJavaVMs ==&gt; jvm</code>, <code>jvm-&gt;jvm_api-&gt;AttachCurrentThread ==&gt; env</code>, <code>env-&gt;env_api-&gt;(FindClass, GetStaticFieldID, SetStaticBooleanField) ==&gt; test[true ==&gt; false]</code></li>
  </ul>