How to debug ruby source code using gdb?, can't print array content

I tried something like this in array.c

rb_ary_initialize(int argc, VALUE *argv, VALUE ary)
{
long len;
int ln;
int *ar;
VALUE size, val;
switch (TYPE(ary)) {
case T_FIXNUM:
break;
case T_STRING:
break;
case T_ARRAY:
ar=RARRAY(ary);
ln=RARRAY_LEN(ary);
printf(“ln: %d\n”,ln);
ar=RARRAY_PTR(ary);
break;
default:
rb_raise(rb_eTypeError, “not valid value”);
break;
}

gdb ./ruby
(gdb)run
ctrl+c
(gdb)break array.c:rb_ary_initialize
(gdb)continue
arr=Array.new(3)
arr[0]=5
arr[1]=7
ctrl+d
(gdb)next

But i can’t understand how to print the value using printf(); in
array.c:rb_ary_initialize function doing some casting.

Can anyone post reply with example to do it?

Thanks.

Ruby has GDB helpers. Take a look at
https://github.com/ruby/ruby/blob/trunk/.gdbinit
Fo instance rp prints any ruby value. There are other handy helpers as
well.

Alex N. wrote in post #1065644:

Ruby has GDB helpers. Take a look at
https://github.com/ruby/ruby/blob/trunk/.gdbinit
Fo instance rp prints any ruby value. There are other handy helpers as
well.

How to use that .gdbinit?
https://github.com/ruby/ruby/blob/trunk/.gdbinit

Copy it to the home folder and it’ll be automatically loaded by gdb.

Alex N. wrote in post #1065660:

Copy it to the home folder and it’ll be automatically loaded by gdb.
I did this. But it looks, it doesn’t help or do anything from (gdb).

On Jun 22, 2012, at 00:35 , Sigurd wrote:

Ruby has GDB helpers. Take a look at
https://github.com/ruby/ruby/blob/trunk/.gdbinit
Fo instance rp prints any ruby value. There are other handy helpers as well.

FYI, this is a help vampire that we recently banned on IRC. He has a
long history of this.

Ryan D. wrote in post #1065745:

On Jun 22, 2012, at 00:35 , Sigurd wrote:

Ruby has GDB helpers. Take a look at
https://github.com/ruby/ruby/blob/trunk/.gdbinit
Fo instance rp prints any ruby value. There are other handy helpers as well.

FYI, this is a help vampire that we recently banned on IRC. He has a
long history of this.

No, i have tried this,

arr=Array.new(3)
arr[0]=5
arr[1]=3
arr[2]=2

These lines should call this function,
https://github.com/ruby/ruby/blob/trunk/array.c#L568 according to this,
Class: Array (Ruby 1.9.3)

So I have added couple of lines there to display the values of array.
But i didn’t get the expected result.

else {
memfill(RARRAY_PTR(ary), len, val);
ARY_SET_LEN(ary, len);
int i;
int result;
result = 0;
VALUE *s_arr = RARRAY_PTR(ary);
for(i = 0; i < len; i++) {
result = LONG2NUM(s_arr[i]);
printf(“r: %d\n”,result);
}
}

I got the result like this:

arr=Array.new(3)
arr[0]=5
arr[1]=3
arr[2]=2
r: 9
r: 9
r: 9

Why is this result? Why 9?

I have followed these to solve it:

How to convert ruby array to C array with RubyInline?

https://github.com/ruby/ruby/blob/trunk/README.EXT#L131

https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L708

https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L731

Also tried this, result = NUM2LONG(s_arr[i]); result =
FIX2NUM(s_arr[i]); result = rb_long2int(s_arr[i]); nothing gets the
expected result.

Can anyone please help to display/printf the value of ruby array in C?

Any reply/answer will be highly appreciated.

Thanks in advanced.