Skip to content

Print errors by calling mrb_print_backtrace() without backtrace#5858

Merged
matz merged 1 commit into
mruby:masterfrom
dearblue:mrb_print_backtrace
Nov 20, 2022
Merged

Print errors by calling mrb_print_backtrace() without backtrace#5858
matz merged 1 commit into
mruby:masterfrom
dearblue:mrb_print_backtrace

Conversation

@dearblue

Copy link
Copy Markdown
Contributor

I don't remember for sure, but I think I used mrb_p() in #4250 because it sometimes didn't output and that was also the case before the change in commit da7d7f8.

- In case of `NoMemoryError` exceptions, the error message is now printed directly.
- Replaced `mrb_p()` used by mruby#4250 with `mrb_print_error()`.

  ref. squashed commit f1523d2
  ref. subcommit da7d7f8
  ref. subcommit d9c7b6b
@dearblue dearblue requested a review from matz as a code owner November 20, 2022 12:17
@dearblue

Copy link
Copy Markdown
Contributor Author
  • example c file

    #include <mruby.h>
    #include <mruby/compile.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define LOAD_LIT(MRB, ...) mrb_load_string(MRB, #__VA_ARGS__)
    
    int
    main(int argc, char *argv[])
    {
      mrb_state *mrb = mrb_open();
      mrb_value fiber = LOAD_LIT(mrb,
        Fiber.new do \n
          5.times { |i| Fiber.yield(i) } \n
          0 / 0 \n
        end
      );
    
      for (int i = 0; i < 9; i++) {
        mrb_value args[] = { mrb_fixnum_value(i) };
        mrb_value ret = mrb_fiber_resume(mrb, fiber, 1, args);
        if (mrb->exc) {
          fprintf(stderr, "[%d:ERR] ", i);
          mrb_print_error(mrb);
        } else {
          fprintf(stderr, "[%d:RET] ", i);
          mrb_p(mrb, ret);
        }
      }
    
      mrb_close(mrb);
      fprintf(stderr, "[SHUTDOWN]\n");
    
      return 0;
    }
  • currently master (3a0e225)

    [0:RET] 0
    [1:RET] 1
    [2:RET] 2
    [3:RET] 3
    [4:RET] 4
    [5:ERR] (unknown):0: divided by 0 (ZeroDivisionError)
    [6:ERR] [7:ERR] [8:ERR] [SHUTDOWN]
    
  • this PR

    [0:RET] 0
    [1:RET] 1
    [2:RET] 2
    [3:RET] 3
    [4:RET] 4
    [5:ERR] (unknown):0: divided by 0 (ZeroDivisionError)
    [6:ERR] (unknown):0: resuming dead fiber (FiberError)
    [7:ERR] (unknown):0: resuming dead fiber (FiberError)
    [8:ERR] (unknown):0: resuming dead fiber (FiberError)
    [SHUTDOWN]
    

Comment thread src/backtrace.c
fwrite(RSTRING_PTR(mesg), RSTRING_LEN(mesg), 1, stderr);
fputc('\n', stderr);
else {
fprintf(stderr, "%s:0: ", "(unknown)");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need fprintf() here? It's a static string.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind. I will work on removing fprintf() later.

Comment thread src/backtrace.c

if (exc == mrb->nomem_err) {
static const char nomem[] = "Out of memory (NoMemoryError)\n";
fwrite(nomem, sizeof(nomem) - 1, 1, stderr);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer no spaces around -. Just my preference.

@matz matz merged commit ffb6b22 into mruby:master Nov 20, 2022
@dearblue dearblue deleted the mrb_print_backtrace branch December 25, 2022 12:29
@dearblue dearblue mentioned this pull request Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants