12#include "ruby/internal/config.h"
16#include "eval_intern.h"
19#include "internal/signal.h"
27#include "vm_callinfo.h"
29#include "ractor_core.h"
44 enum ruby_tag_type tag_type;
45 enum node_type node_type;
46 enum ruby_method_ids method_ids;
47 enum ruby_id_types id_types;
50 enum ruby_encoding_consts encoding_consts;
54 enum ruby_preserved_encindex encoding_index;
55 enum ruby_robject_flags robject_flags;
56 enum ruby_rmodule_flags rmodule_flags;
57 enum ruby_rstring_flags rstring_flags;
58 enum ruby_rarray_flags rarray_flags;
70 RUBY_FMODE_NOREVLOOKUP = 0x00000100,
73 RUBY_FMODE_EXTERNAL = 0x00010000,
75 RUBY_FMODE_UNIX = 0x00200000,
76 RUBY_FMODE_INET = 0x00400000,
77 RUBY_FMODE_INET6 = 0x00800000,
79 RUBY_NODE_TYPESHIFT = NODE_TYPESHIFT,
80 RUBY_NODE_TYPEMASK = NODE_TYPEMASK,
81 RUBY_NODE_LSHIFT = NODE_LSHIFT,
82 RUBY_NODE_FL_NEWLINE = NODE_FL_NEWLINE
85 enum imemo_type types;
86 enum {RUBY_IMEMO_MASK = IMEMO_MASK} mask;
90 enum vm_call_flag_bits vm_call_flags;
91} ruby_dummy_gdb_enums;
96ruby_debug_print_indent(
int level,
int debug_level,
int indent_level)
98 if (level < debug_level) {
99 fprintf(stderr,
"%*s", indent_level,
"");
107ruby_debug_printf(
const char *format, ...)
110 va_start(ap, format);
111 vfprintf(stderr, format, ap);
115#include "internal/gc.h"
118ruby_debug_print_value(
int level,
int debug_level,
const char *header,
VALUE obj)
120 if (level < debug_level) {
122 rb_raw_obj_info(buff, 0x100, obj);
124 fprintf(stderr,
"DBG> %s: %s\n", header, buff);
131ruby_debug_print_v(
VALUE v)
133 ruby_debug_print_value(0, 1,
"", v);
137ruby_debug_print_id(
int level,
int debug_level,
const char *header,
ID id)
139 if (level < debug_level) {
140 fprintf(stderr,
"DBG> %s: %s\n", header, rb_id2name(
id));
147ruby_debug_print_node(
int level,
int debug_level,
const char *header,
const NODE *node)
149 if (level < debug_level) {
150 fprintf(stderr,
"DBG> %s: %s (id: %d, line: %d, location: (%d,%d)-(%d,%d))\n",
151 header, ruby_node_name(nd_type(node)), nd_node_id(node), nd_line(node),
152 nd_first_lineno(node), nd_first_column(node),
153 nd_last_lineno(node), nd_last_column(node));
159ruby_debug_print_n(
const NODE *node)
161 ruby_debug_print_node(0, 1,
"", node);
165ruby_debug_breakpoint(
void)
171extern int ruby_w32_rtc_error;
173#if defined _WIN32 || defined __CYGWIN__
175UINT ruby_w32_codepage[2];
177extern int ruby_rgengc_debug;
178extern int ruby_on_ci;
181ruby_env_debug_option(
const char *str,
int len,
void *arg)
186#define NAME_MATCH(name) (len == sizeof(name) - 1 && strncmp(str, (name), len) == 0)
187#define SET_WHEN(name, var, val) do { \
188 if (NAME_MATCH(name)) { \
193#define NAME_MATCH_VALUE(name) \
194 ((size_t)len >= sizeof(name)-1 && \
195 strncmp(str, (name), sizeof(name)-1) == 0 && \
196 ((len == sizeof(name)-1 && !(len = 0)) || \
197 (str[sizeof(name)-1] == '=' && \
198 (str += sizeof(name), len -= sizeof(name), 1))))
199#define SET_UINT(val) do { \
200 n = ruby_scan_digits(str, len, 10, &retlen, &ov); \
201 if (!ov && retlen) { \
202 val = (unsigned int)n; \
207#define SET_UINT_LIST(name, vals, num) do { \
209 for (i = 0; i < (num); ++i) { \
210 SET_UINT((vals)[i]); \
211 if (!len || *str != ':') break; \
216 fprintf(stderr, "ignored "name" option: '%.*s'\n", len, str); \
219#define SET_WHEN_UINT(name, vals, num, req) \
220 if (NAME_MATCH_VALUE(name)) { \
222 else SET_UINT_LIST(name, vals, num); \
226 if (NAME_MATCH(
"gc_stress")) {
227 rb_gc_initial_stress_set(
Qtrue);
230 SET_WHEN(
"core", ruby_enable_coredump, 1);
231 SET_WHEN(
"ci", ruby_on_ci, 1);
232 SET_WHEN_UINT(
"rgengc", &ruby_rgengc_debug, 1, ruby_rgengc_debug = 1);
234 SET_WHEN(
"rtc_error", ruby_w32_rtc_error, 1);
236#if defined _WIN32 || defined __CYGWIN__
237 SET_WHEN_UINT(
"codepage", ruby_w32_codepage, numberof(ruby_w32_codepage),
238 fprintf(stderr,
"missing codepage argument"));
244set_debug_option(
const char *str,
int len,
void *arg)
246 if (!ruby_env_debug_option(str,
len, arg)) {
247 fprintf(stderr,
"unexpected debug option: %.*s\n",
len, str);
251#if USE_RUBY_DEBUG_LOG
252static void setup_debug_log(
void);
254#define setup_debug_log()
258ruby_set_debug_option(
const char *str)
264#if USE_RUBY_DEBUG_LOG
269#define MAX_DEBUG_LOG 0x1000
270#define MAX_DEBUG_LOG_MESSAGE_LEN 0x0200
271#define MAX_DEBUG_LOG_FILTER_LEN 0x0020
272#define MAX_DEBUG_LOG_FILTER_NUM 0x0010
274enum ruby_debug_log_mode ruby_debug_log_mode;
276struct debug_log_filter {
277 enum debug_log_filter_type {
283 char str[MAX_DEBUG_LOG_FILTER_LEN];
286static const char *dlf_type_names[] = {
293#define DEBUG_LOG_MAX_PATH (MAX_PATH-1)
295#define DEBUG_LOG_MAX_PATH 255
301 struct debug_log_filter filters[MAX_DEBUG_LOG_FILTER_NUM];
302 unsigned int filters_num;
304 rb_nativethread_lock_t lock;
305 char output_file[DEBUG_LOG_MAX_PATH+1];
310RUBY_DEBUG_LOG_MEM_ENTRY(
unsigned int index)
312 return &debug_log.mem[MAX_DEBUG_LOG_MESSAGE_LEN * index];
315static enum debug_log_filter_type
316filter_type(
const char *str,
int *skiplen)
318 if (strncmp(str,
"file:", 5) == 0) {
322 else if(strncmp(str,
"func:", 5) == 0) {
333setup_debug_log_filter(
void)
335 const char *filter_config = getenv(
"RUBY_DEBUG_LOG_FILTER");
337 if (filter_config && strlen(filter_config) > 0) {
339 for (i=0; i<MAX_DEBUG_LOG_FILTER_NUM && filter_config; i++) {
341 const char *str = filter_config;
344 if ((p = strchr(str,
',')) == NULL) {
346 filter_config = NULL;
350 filter_config = p + 1;
355 debug_log.filters[i].negative =
true;
358 else if (*str ==
'+') {
365 debug_log.filters[i].type = filter_type(str, &skiplen);
368 if (
len >= MAX_DEBUG_LOG_FILTER_LEN) {
369 fprintf(stderr,
"too long: %s (max:%d)\n", str, MAX_DEBUG_LOG_FILTER_LEN - 1);
374 strncpy(debug_log.filters[i].str, str + skiplen,
len);
375 debug_log.filters[i].str[
len] = 0;
377 debug_log.filters_num = i;
379 for (i=0; i<debug_log.filters_num; i++) {
380 fprintf(stderr,
"RUBY_DEBUG_LOG_FILTER[%d]=%s (%s%s)\n", i,
381 debug_log.filters[i].str,
382 debug_log.filters[i].negative ?
"-" :
"",
383 dlf_type_names[debug_log.filters[i].
type]);
392 const char *log_config = getenv(
"RUBY_DEBUG_LOG");
393 if (log_config && strlen(log_config) > 0) {
394 if (strcmp(log_config,
"mem") == 0) {
395 debug_log.mem = (
char *)malloc(MAX_DEBUG_LOG * MAX_DEBUG_LOG_MESSAGE_LEN);
396 if (debug_log.mem == NULL) {
397 fprintf(stderr,
"setup_debug_log failed (can't allocate memory)\n");
400 ruby_debug_log_mode |= ruby_debug_log_memory;
402 else if (strcmp(log_config,
"stderr") == 0) {
403 ruby_debug_log_mode |= ruby_debug_log_stderr;
406 ruby_debug_log_mode |= ruby_debug_log_file;
409 unsigned long len = strlen(log_config);
411 for (
unsigned long i=0, j=0; i<
len; i++) {
412 const char c = log_config[i];
416 switch (log_config[i]) {
418 debug_log.output_file[j++] =
'%';
421 snprintf(debug_log.output_file + j, DEBUG_LOG_MAX_PATH - j,
"%d", getpid());
422 j = strlen(debug_log.output_file);
425 fprintf(stderr,
"can not parse RUBY_DEBUG_LOG filename: %s\n", log_config);
430 debug_log.output_file[j++] = c;
433 if (j >= DEBUG_LOG_MAX_PATH) {
434 fprintf(stderr,
"RUBY_DEBUG_LOG=%s is too long\n", log_config);
439 if ((debug_log.output = fopen(debug_log.output_file,
"w")) == NULL) {
440 fprintf(stderr,
"can not open %s for RUBY_DEBUG_LOG\n", log_config);
443 setvbuf(debug_log.output, NULL, _IONBF, 0);
446 fprintf(stderr,
"RUBY_DEBUG_LOG=%s %s%s%s\n", log_config,
447 (ruby_debug_log_mode & ruby_debug_log_memory) ?
"[mem]" :
"",
448 (ruby_debug_log_mode & ruby_debug_log_stderr) ?
"[stderr]" :
"",
449 (ruby_debug_log_mode & ruby_debug_log_file) ?
"[file]" :
"");
450 if (debug_log.output_file[0]) {
451 fprintf(stderr,
"RUBY_DEBUG_LOG filename=%s\n", debug_log.output_file);
456 setup_debug_log_filter();
458 if (getenv(
"RUBY_DEBUG_LOG_PID")) {
459 debug_log.show_pid =
true;
465check_filter(
const char *str,
const struct debug_log_filter *filter,
bool *state)
467 if (filter->negative) {
468 if (strstr(str, filter->str) == NULL) {
478 if (strstr(str, filter->str) != NULL) {
509ruby_debug_log_filter(
const char *func_name,
const char *file_name)
511 if (debug_log.filters_num > 0) {
514 for (
unsigned int i = 0; i<debug_log.filters_num; i++) {
515 const struct debug_log_filter *filter = &debug_log.filters[i];
517 switch (filter->type) {
519 if (check_filter(func_name, filter, &state))
return state;
520 if (check_filter(file_name, filter, &state))
return state;
523 if (check_filter(func_name, filter, &state))
return state;
526 if (check_filter(file_name, filter, &state))
return state;
538pretty_filename(
const char *path)
542 while ((s = strchr(path,
'/')) != NULL) {
550ruby_debug_log(
const char *file,
int line,
const char *func_name,
const char *fmt, ...)
552 char buff[MAX_DEBUG_LOG_MESSAGE_LEN] = {0};
556 if (debug_log.show_pid) {
557 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN,
"pid:%d\t", getpid());
558 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
563 if (func_name &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
564 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN,
"%s\t", func_name);
565 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
570 if (fmt &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
573 r = vsnprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len, fmt, args);
575 if (r < 0) rb_bug(
"ruby_debug_log vsnprintf() returns %d", r);
582 if (file &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
583 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN,
"\t%s:%d", pretty_filename(file), line);
584 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
592 const char *ruby_file = ec ? rb_source_location_cstr(&ruby_line) : NULL;
594 if (
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
596 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\t%s:%d", pretty_filename(ruby_file), ruby_line);
599 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\t");
601 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
607 if (
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
608 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\tnt:%d", ruby_nt_serial);
609 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
615 rb_thread_t *th = ec ? rb_ec_thread_ptr(ec) : NULL;
618 if (ruby_single_main_ractor == NULL) {
622 if (r &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
623 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\tr:#%d/%u (%u)",
624 cr ? (
int)rb_ractor_id(cr) : -1, vm->ractor.cnt, vm->ractor.sched.running_cnt);
626 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
632 if (th && r &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
634 const rb_thread_t *rth = rec ? rec->thread_ptr : NULL;
635 const rb_thread_t *sth = th->ractor ? th->ractor->threads.sched.running : NULL;
637 if (rth != th || sth != th) {
638 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\tth:%u (rth:%d,sth:%d)",
639 rb_th_serial(th), rth ? (
int)rb_th_serial(rth) : -1, sth ? (int)rb_th_serial(sth) : -1);
642 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\tth:%u", rb_th_serial(th));
644 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
651 unsigned int cnt = debug_log.cnt++;
653 if (ruby_debug_log_mode & ruby_debug_log_memory) {
654 unsigned int index = cnt % MAX_DEBUG_LOG;
655 char *dst = RUBY_DEBUG_LOG_MEM_ENTRY(index);
656 strncpy(dst, buff, MAX_DEBUG_LOG_MESSAGE_LEN);
658 if (ruby_debug_log_mode & ruby_debug_log_stderr) {
659 fprintf(stderr,
"%4u: %s\n", cnt, buff);
661 if (ruby_debug_log_mode & ruby_debug_log_file) {
662 fprintf(debug_log.output,
"%u\t%s\n", cnt, buff);
670debug_log_dump(
FILE *out,
unsigned int n)
672 if (ruby_debug_log_mode & ruby_debug_log_memory) {
673 unsigned int size = debug_log.cnt > MAX_DEBUG_LOG ? MAX_DEBUG_LOG : debug_log.cnt;
674 unsigned int current_index = debug_log.cnt % MAX_DEBUG_LOG;
675 if (n == 0) n = size;
676 if (n > size) n = size;
678 for (
unsigned int i=0; i<n; i++) {
679 int index = current_index - size + i;
680 if (index < 0) index += MAX_DEBUG_LOG;
681 VM_ASSERT(index <= MAX_DEBUG_LOG);
682 const char *mesg = RUBY_DEBUG_LOG_MEM_ENTRY(index);
683 fprintf(out,
"%4u: %s\n", debug_log.cnt - size + i, mesg);
687 fprintf(stderr,
"RUBY_DEBUG_LOG=mem is not specified.");
694ruby_debug_log_print(
unsigned int n)
696 debug_log_dump(stderr, n);
700ruby_debug_log_dump(
const char *fname,
unsigned int n)
702 FILE *fp = fopen(fname,
"w");
704 fprintf(stderr,
"can't open %s. give up.\n", fname);
707 debug_log_dump(fp, n);
716ruby_debug_log(
const char *file,
int line,
const char *func_name,
const char *fmt, ...)
720 fprintf(stderr,
"[%s:%d] %s: ", file, line, func_name);
723 vfprintf(stderr, fmt, args);
726 fprintf(stderr,
"\n");
ruby_coderange_type
What rb_enc_str_coderange() returns.
ruby_fl_ushift
This is an enum because GDB wants it (rather than a macro).
#define Qtrue
Old name of RUBY_Qtrue.
rb_econv_result_t
return value of rb_econv_convert()
ruby_econv_flag_type
This enum is kind of omnibus.
#define FMODE_READABLE
The IO is opened for reading.
#define FMODE_SETENC_BY_BOM
This flag amends the encoding of the IO so that the BOM of the contents of the IO takes effect.
#define FMODE_READWRITE
The IO is opened for both read/write.
#define FMODE_TTY
The IO is a TTY.
#define FMODE_CREATE
The IO is opened for creating.
#define FMODE_WRITABLE
The IO is opened for writing.
#define FMODE_APPEND
The IO is opened for appending.
#define FMODE_DUPLEX
Ruby eventually detects that the IO is bidirectional.
#define FMODE_BINMODE
The IO is in "binary mode".
#define FMODE_SYNC
The IO is in "sync mode".
#define FMODE_TEXTMODE
The IO is in "text mode".
#define FMODE_TRUNC
This flag amends the effect of FMODE_CREATE, so that if there already is a file at the given path it ...
int len
Length of the buffer.
void ruby_each_words(const char *str, void(*func)(const char *word, int len, void *argv), void *argv)
Scans the passed string, with calling the callback function every time it encounters a "word".
VALUE type(ANYARGS)
ANYARGS-ed function type.
ruby_rarray_consts
This is an enum because GDB wants it (rather than a macro).
ruby_special_consts
special constants - i.e.
void rb_nativethread_lock_lock(rb_nativethread_lock_t *lock)
Blocks until the current thread obtains a lock.
void rb_nativethread_lock_unlock(rb_nativethread_lock_t *lock)
Releases a lock.
void rb_nativethread_lock_initialize(rb_nativethread_lock_t *lock)
Fills the passed lock with an initial value.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
ruby_value_type
C-level type of an object.