Skip to content

Commit

Permalink
Add --bugreport-path option
Browse files Browse the repository at this point in the history
It has precedence over the environment variable `RUBY_BUGREPORT_PATH`.
  • Loading branch information
nobu committed Sep 25, 2023
1 parent 696f082 commit 70e8a08
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
11 changes: 9 additions & 2 deletions error.c
Expand Up @@ -839,13 +839,17 @@ open_report_path(const char *template, char *buf, size_t size, rb_pid_t *pid)
return NULL;
}

static const char *bugreport_path;

/* SIGSEGV handler might have a very small stack. Thus we need to use it carefully. */
#define REPORT_BUG_BUFSIZ 256
static FILE *
bug_report_file(const char *file, int line, rb_pid_t *pid)
{
char buf[REPORT_BUG_BUFSIZ];
FILE *out = open_report_path(getenv("RUBY_BUGREPORT_PATH"), buf, sizeof(buf), pid);
const char *report = bugreport_path;
if (!report) report = getenv("RUBY_BUGREPORT_PATH");
FILE *out = open_report_path(report, buf, sizeof(buf), pid);
int len = err_position_0(buf, sizeof(buf), file, line);

if (out) {
Expand Down Expand Up @@ -998,8 +1002,10 @@ bug_report_end(FILE *out, rb_pid_t pid)
} while (0) \

void
ruby_test_bug_report(const char *template)
ruby_set_bug_report(const char *template)
{
bugreport_path = template;
#if RUBY_DEBUG
rb_pid_t pid = -1;
char buf[REPORT_BUG_BUFSIZ];
FILE *out = open_report_path(template, buf, sizeof(buf), &pid);
Expand All @@ -1008,6 +1014,7 @@ ruby_test_bug_report(const char *template)
fprintf(out, "ruby_test_bug_report: %s", ctime(&t));
finish_report(out, pid);
}
#endif
}

NORETURN(static void die(void));
Expand Down
2 changes: 2 additions & 0 deletions internal/cmdlineopt.h
Expand Up @@ -28,6 +28,8 @@ typedef struct ruby_cmdline_options {
struct rb_rjit_options rjit;
#endif

const char *bugreport_path;

signed int sflag: 2;
unsigned int xflag: 1;
unsigned int warning: 1;
Expand Down
7 changes: 7 additions & 0 deletions man/ruby.1
Expand Up @@ -25,6 +25,7 @@
.Op Fl - Ns Bro Cm enable Ns | Ns Cm disable Brc Ns - Ns Ar FEATURE
.Op Fl -dump Ns = Ns Ar target
.Op Fl -verbose
.Op Fl -bugreport-path Ns = Ns Ar template
.Op Fl -
.Op Ar program_file
.Op Ar argument ...
Expand Down Expand Up @@ -469,6 +470,12 @@ variable to true.
If this switch is given, and no script arguments (script file or
.Fl e
options) are present, Ruby quits immediately.
.Pp
.It Fl -bugreport-path Ns = Ns Ar template
Sets the template of path name to save bug report.
See
.Ev RUBY_BUGREPORT_PATH
environment variable for details.
.El
.Pp
.Sh ENVIRONMENT
Expand Down
15 changes: 8 additions & 7 deletions ruby.c
Expand Up @@ -337,6 +337,7 @@ usage(const char *name, int help, int highlight, int columns)
M("--backtrace-limit=num", "", "limit the maximum length of backtrace"),
M("--verbose", "", "turn on verbose mode and disable script from stdin"),
M("--version", "", "print the version number, then exit"),
M("--bugreport-path=TEMPLATE", "", "template of bug report files"),
M("-y", ", --yydebug", "print log of parser. Backward compatibility is not guaranteed"),
M("--help", "", "show this message, -h for short message"),
};
Expand Down Expand Up @@ -1352,8 +1353,6 @@ proc_encoding_option(ruby_cmdline_options_t *opt, const char *s, const char *opt
UNREACHABLE;
}

static const char *test_bug_report_template;

static long
proc_long_options(ruby_cmdline_options_t *opt, const char *s, long argc, char **argv, int envopt)
{
Expand Down Expand Up @@ -1464,8 +1463,10 @@ proc_long_options(ruby_cmdline_options_t *opt, const char *s, long argc, char **
opt->backtrace_length_limit = n;
}
}
else if (is_option_with_arg("test-bugreport", true, false)) {
test_bug_report_template = s;
else if (is_option_with_arg("bugreport-path", true, true)) {
if (!opt->bugreport_path) {
opt->bugreport_path = s;
}
}
else {
rb_raise(rb_eRuntimeError,
Expand Down Expand Up @@ -2916,9 +2917,9 @@ ruby_process_options(int argc, char **argv)

iseq = process_options(argc, argv, cmdline_options_init(&opt));

if (test_bug_report_template) {
void ruby_test_bug_report(const char *template);
ruby_test_bug_report(test_bug_report_template);
if (opt.bugreport_path && *opt.bugreport_path) {
void ruby_set_bug_report(const char *template);
ruby_set_bug_report(opt.bugreport_path);
}
return (void*)(struct RData*)iseq;
}
Expand Down

0 comments on commit 70e8a08

Please sign in to comment.