Debugging Crystal with GDB

I am trying to learn how to debug programs written in Crystal with GDB. Here is an example:

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")

I compiled the sample above with the flag --debugand uploaded it to GDB. Then I let it work and settled on the marked line ( GDB breakpoint here). Now I have three four questions:

  • Printing string values ​​(e.g. foo): When I check a string variable, I often see something like $1 = (struct String *) 0x4b9f18. When I say printf "%s", foo, I get nothing. How can I display the current value of a string variable?
  • optimized. In other cases, I see $1 = <optimized out>when checking a variable. What does this mean and how can I see the meaning in this case?
  • @array ? p array , p @array . : : p self.array
  • ( puts "HI") html: p html . ?
+4
1

- , , LLVM. , , --debug. :

inlined , .

3.times do |i|
  pp i
end

pp name => value.

pp i
i => 1
i => 2
i => 3

debugger {% debug() %} $ crystal tool expand.

, , GDB.

i = 0
while i < 3
  debugger
  i += 1 # i variable is listed by GDB
end

, , .

  • : try @[NoInline] p &foo.c, args,

@[NoInline]:

@[NoInline]
def do(foo)
  debugger
end

GDB:

(gdb) p &foo.c
$1 = (UInt8 *) 0x10008e6c4 "HI"
  1. : , LLVM . <optimized out>, @[NoInline] vars vars array = @array

  2. : self.var, , vars.

p array.buffer[0]@size .

(gdb) p &array.buffer[0].c
$19 = (UInt8 *) 0x10008e7f4 "HI"
  1. : , .

:

@[NoInline]
def do(foo)
  html = bar(foo).as(String)
  html = bar(foo).to_s
  debugger
end

html var GDB .as .to_s

(gdb) p &html.c
$1 = (UInt8 *) 0x1002fcfec "hihi"
+3

Source: https://habr.com/ru/post/1686236/


All Articles