Skip to content

Cannot get exception backtrace after its inspect method is called (in C) #1661

@seanchas116

Description

@seanchas116

I tried to inspect and get backtrace of exceptions in C
and I noticed that backtrace returns an empty array after inspect is called.

#include <stdio.h>
#include <mruby.h>
#include <mruby/compile.h>
#include <mruby/string.h>

mrb_value mrb_get_backtrace(mrb_state *mrb, mrb_value self);

int main(void)
{
    mrb_state *mrb = mrb_open();

    mrbc_context *context = mrbc_context_new(mrb);
    mrbc_filename(mrb, context, "file");

    mrb_load_string_cxt(mrb,
                        "def foo\n"
                        "  xxx(0)\n"
                        "end\n"
                        "foo",
                        context);

    mrb_value exc = mrb_obj_value(mrb->exc);

    // this works //
    mrb_value backtrace = mrb_get_backtrace(mrb, exc);
    puts(mrb_str_to_cstr(mrb, mrb_inspect(mrb, backtrace)));
    // => ["file:2:in Object.foo", "file:4"]

    mrb_value inspect = mrb_inspect(mrb, exc);
    puts(mrb_str_to_cstr(mrb, inspect));

    // this does not works //
    /*
    mrb_value inspect = mrb_inspect(mrb, exc);
    puts(mrb_str_to_cstr(mrb, inspect));

    mrb_value backtrace = mrb_get_backtrace(mrb, exc);
    puts(mrb_str_to_cstr(mrb, mrb_inspect(mrb, backtrace)));
    // => []
    */

    mrbc_context_free(mrb, context);
    mrb_close(mrb);

    return 0;
}

The same thing works fine when I loaded this file in mruby command.

begin
  5.times do
    raise "error"
  end
rescue => exc
  p exc.inspect
  p exc.backtrace
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions