title: How does GDB evaluate C++ expressions at runtime
body: <p>When debugging, I recently noticed that GDB has the ability to evaluate "complex" expressions while debugging a program, and I am wondering how it does this. For example, with the following code:</p>
  
  <pre><code>int main() {
      std::vector&lt;int&gt; v = {1, 2, 3};
      int k = 0;
      std::cin &gt;&gt; k;
      v.push_back(k);
      return v.at(0);
  }
  </code></pre>
  
  <p>I am able to compile the program <code>g++ -g myprogram.cpp</code> and debug it in GDB which allows me to type things like <code>print v.at(4);</code> (which prints the correct value after <code>k</code> is dynamically entered) and <code>print v.at(2) == 3</code> which evaluates to true.</p>
  
  <p>I am wondering how GDB does this. <a href="https://stackoverflow.com/questions/13353437/why-does-gdb-evaluate-floating-point-arithmetic-differently-from-c">This SO question</a> hints that it is something "compiled in memory" but does not elaborate further, so I am wondering does it use some sort of JIT to make this all work or something else? Are they compiling the code inline as I type it and running it? Do they have a framework to evaluate C++ on the fly in the debugging context? In essence, I want to reproduce this in a debugger I am writing to evaluate expressions at breakpoints which is why I am curious how GDB does it. </p>