title: Crystal debugging with GDB
body: <p>I am trying to learn to debug programs written in Crystal with GDB. Here is a sample:</p>
  
  <pre><code>class Demo
    @array = [] of String
  
    def bar(url)
      ret = url.downcase * 2
      if ret == "alsj"
        return false
      else
        return ret
      end
    end
  
    def do(foo)
      @array.push(foo)
      html = bar(foo)
      puts "HI" # GDB breakpoint here
      return html
    end
  end
  
  a = Demo.new
  puts a.do("HI")
  </code></pre>
  
  <p>I compiled the sample above with the <code>--debug</code> flag and loaded it into GDB. Then I let it run and stopped at the marked line (<code>GDB breakpoint here</code>). Now I have <s>three</s> four questions:</p>
  
  <ol>
  <li><strong>Printing string values (e.g. <code>foo</code>):</strong>
  When I inspect a string variable, I often see something like <code>$1 = (struct String *) 0x4b9f18</code>. When I say <code>printf "%s", foo</code>, I get nothing back. How can I display the current value of a string variable?</li>
  <li><strong>optimized out.</strong>
  Other times I just see <code>$1 = &lt;optimized out&gt;</code> when inspecting a variable. What does that mean and how can I see the value in that case?</li>
  <li><strong>Accessing object variables</strong>
  How can I see the value of <code>@array</code> in the given situation? <code>p array</code> says no symbol in current context and <code>p @array</code> returns unknown address space modifier.
  <strong>Edit:</strong> I found a way: use <code>p self.array</code></li>
  <li><strong>Vanished variables</strong>
  In the given situation (breaking at the line <code>puts "HI"</code>) I can't see the variable html at all: <code>p html</code> returns no symbol in current context. Why is that and how do I solve it?</li>
  </ol>