Skip to content

Commit

Permalink
Modify ISeq#disasm to provide the location as well as the default a…
Browse files Browse the repository at this point in the history
…rgument part.

When you debug a method with default arguments, such as this,

```ruby
def f(a = 1, b = 2)
  3
end
```

you'd like to see where the method body starts at the ISeq.

`ISeq#disasm`, `ISeq#disassemble`, and `ruby --dump=insns` provide this

```
== disasm: #<ISeq:<main>@/tmp/vlDkYWS/294:1 (1,0)-(4,3)> (catch: FALSE)
0000 putspecialobject             1                                   (   2)[Li]
0002 putobject                    :f
0004 putiseq                      f
0006 opt_send_without_block       <callinfo!mid:core#define_method, argc:2, ARGS_SIMPLE>, <callcache>
0009 leave

== disasm: #<ISeq:f@/tmp/vlDkYWS/294:2 (2,0)-(4,3)> (catch: FALSE)
local table (size: 2, argc: 0 [opts: 2, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 2] a@0<Opt=0> [ 1] b@1<Opt=3>
0000 putobject_INT2FIX_1_                                             (   2)
0001 setlocal_WC_0                a@0
0003 putobject                    2
0005 setlocal_WC_0                b@1
0007 putobject                    3                                   (   3)[LiCa]
0009 leave                                                            (   4)[Re]

```

However this does not explain where the method body (in this case `0007 putobject 3`) starts.

(Note that `ISeq#to_a` provides where it starts. See `:label_7` below)

```
["YARVInstructionSequence/SimpleDataFormat", 2, 6, 1,
    {:arg_size=>2, :local_size=>2, :stack_max=>1, :node_id=>8, :code_location=>[1, 0, 3, 3]},
    "f", "<compiled>", "<compiled>", 1, :method, [:a, :b],
    {:opt=>[:label_0, :label_3, :label_7]},
    [],
    [:label_0, 1, [:putobject_INT2FIX_1_], [:setlocal_WC_0, 4],
     :label_3, [:putobject, 2], [:setlocal_WC_0, 3],
     :label_7, 2, :RUBY_EVENT_LINE, :RUBY_EVENT_CALL, [:putobject, 3], 3, :RUBY_EVENT_RETURN, [:leave]]]
```

Modify `ISeq#disasm` to provide the location as well as the default argument part.

* before: ` [ 2] a@0<Opt=0> [ 1] b@1<Opt=3>`
* after: `[ 2] a@0<Opt=0> [ 1] b@1<Opt=3> [ 0] <Opt=7>`

for

```
== disasm: #<ISeq:f@/tmp/vlDkYWS/298:2 (2,0)-(4,3)> (catch: FALSE)
local table (size: 2, argc: 0 [opts: 2, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 2] a@0<Opt=0> [ 1] b@1<Opt=3> [ 0] <Opt=7>
0000 putobject_INT2FIX_1_                                             (   2)
0001 setlocal_WC_0                a@0
0003 putobject                    2
0005 setlocal_WC_0                b@1
0007 putobject                    3                                   (   3)[LiCa]
0009 leave                                                            (   4)[Re]
```
  • Loading branch information
ujihisa committed Apr 29, 2019
1 parent 6033423 commit 258b8e9
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions iseq.c
Expand Up @@ -2210,6 +2210,12 @@ rb_iseq_disasm_recursive(const rb_iseq_t *iseq, VALUE indent)
if (*argi) rb_str_catf(str, "<%s>", argi);
if ((width -= RSTRING_LEN(str)) > 0) rb_str_catf(str, "%*s", (int)width, "");
}
if (body->param.flags.has_opt) {
char opti[0x100] = "";
snprintf(opti, sizeof(opti), "Opt=%"PRIdVALUE,
body->param.opt_table[body->local_table_size]);
rb_str_catf(str, "[ 0] <%s>", opti);
}
rb_str_cat_cstr(right_strip(str), "\n");
}

Expand Down

0 comments on commit 258b8e9

Please sign in to comment.