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;
60 enum rbimpl_typeddata_flags rtypeddata_consts;
71 RUBY_FMODE_NOREVLOOKUP = 0x00000100,
74 RUBY_FMODE_EXTERNAL = 0x00010000,
76 RUBY_FMODE_UNIX = 0x00200000,
77 RUBY_FMODE_INET = 0x00400000,
78 RUBY_FMODE_INET6 = 0x00800000,
80 RUBY_NODE_TYPESHIFT = NODE_TYPESHIFT,
81 RUBY_NODE_TYPEMASK = NODE_TYPEMASK,
82 RUBY_NODE_LSHIFT = NODE_LSHIFT,
83 RUBY_NODE_FL_NEWLINE = NODE_FL_NEWLINE
86 enum imemo_type types;
87 enum {RUBY_IMEMO_MASK = IMEMO_MASK} mask;
91 enum vm_call_flag_bits vm_call_flags;
92} ruby_dummy_gdb_enums;
97ruby_debug_print_indent(
int level,
int debug_level,
int indent_level)
99 if (level < debug_level) {
100 fprintf(stderr,
"%*s", indent_level,
"");
108ruby_debug_printf(
const char *format, ...)
111 va_start(ap, format);
112 vfprintf(stderr, format, ap);
116#include "internal/gc.h"
119ruby_debug_print_value(
int level,
int debug_level,
const char *header,
VALUE obj)
121 if (level < debug_level) {
123 rb_raw_obj_info(buff, 0x100, obj);
125 fprintf(stderr,
"DBG> %s: %s\n", header, buff);
132ruby_debug_print_v(
VALUE v)
134 ruby_debug_print_value(0, 1,
"", v);
138ruby_debug_print_id(
int level,
int debug_level,
const char *header,
ID id)
140 if (level < debug_level) {
141 fprintf(stderr,
"DBG> %s: %s\n", header, rb_id2name(
id));
148ruby_debug_print_node(
int level,
int debug_level,
const char *header,
const NODE *node)
150 if (level < debug_level) {
151 fprintf(stderr,
"DBG> %s: %s (id: %d, line: %d, location: (%d,%d)-(%d,%d))\n",
152 header, ruby_node_name(nd_type(node)), nd_node_id(node), nd_line(node),
153 nd_first_lineno(node), nd_first_column(node),
154 nd_last_lineno(node), nd_last_column(node));
160ruby_debug_print_n(
const NODE *node)
162 ruby_debug_print_node(0, 1,
"", node);
166ruby_debug_breakpoint(
void)
172extern int ruby_w32_rtc_error;
174#if defined _WIN32 || defined __CYGWIN__
176UINT ruby_w32_codepage[2];
178extern int ruby_rgengc_debug;
179extern int ruby_on_ci;
182ruby_env_debug_option(
const char *str,
int len,
void *arg)
187#define NAME_MATCH(name) (len == sizeof(name) - 1 && strncmp(str, (name), len) == 0)
188#define SET_WHEN(name, var, val) do { \
189 if (NAME_MATCH(name)) { \
194#define NAME_MATCH_VALUE(name) \
195 ((size_t)len >= sizeof(name)-1 && \
196 strncmp(str, (name), sizeof(name)-1) == 0 && \
197 ((len == sizeof(name)-1 && !(len = 0)) || \
198 (str[sizeof(name)-1] == '=' && \
199 (str += sizeof(name), len -= sizeof(name), 1))))
200#define SET_UINT(val) do { \
201 n = ruby_scan_digits(str, len, 10, &retlen, &ov); \
202 if (!ov && retlen) { \
203 val = (unsigned int)n; \
208#define SET_UINT_LIST(name, vals, num) do { \
210 for (i = 0; i < (num); ++i) { \
211 SET_UINT((vals)[i]); \
212 if (!len || *str != ':') break; \
217 fprintf(stderr, "ignored "name" option: '%.*s'\n", len, str); \
220#define SET_WHEN_UINT(name, vals, num, req) \
221 if (NAME_MATCH_VALUE(name)) { \
223 else SET_UINT_LIST(name, vals, num); \
227 if (NAME_MATCH(
"gc_stress")) {
228 rb_gc_initial_stress_set(
Qtrue);
231 SET_WHEN(
"core", ruby_enable_coredump, 1);
232 SET_WHEN(
"ci", ruby_on_ci, 1);
233 SET_WHEN_UINT(
"rgengc", &ruby_rgengc_debug, 1, ruby_rgengc_debug = 1);
235 SET_WHEN(
"rtc_error", ruby_w32_rtc_error, 1);
237#if defined _WIN32 || defined __CYGWIN__
238 SET_WHEN_UINT(
"codepage", ruby_w32_codepage, numberof(ruby_w32_codepage),
239 fprintf(stderr,
"missing codepage argument"));
245set_debug_option(
const char *str,
int len,
void *arg)
247 if (!ruby_env_debug_option(str,
len, arg)) {
248 fprintf(stderr,
"unexpected debug option: %.*s\n",
len, str);
252#if USE_RUBY_DEBUG_LOG
253static void setup_debug_log(
void);
255#define setup_debug_log()
259ruby_set_debug_option(
const char *str)
265#if USE_RUBY_DEBUG_LOG
270#define MAX_DEBUG_LOG 0x1000
271#define MAX_DEBUG_LOG_MESSAGE_LEN 0x0200
272#define MAX_DEBUG_LOG_FILTER_LEN 0x0020
273#define MAX_DEBUG_LOG_FILTER_NUM 0x0010
275enum ruby_debug_log_mode ruby_debug_log_mode;
277struct debug_log_filter {
278 enum debug_log_filter_type {
284 char str[MAX_DEBUG_LOG_FILTER_LEN];
287static const char *dlf_type_names[] = {
294#define DEBUG_LOG_MAX_PATH (MAX_PATH-1)
296#define DEBUG_LOG_MAX_PATH 255
302 struct debug_log_filter filters[MAX_DEBUG_LOG_FILTER_NUM];
303 unsigned int filters_num;
305 rb_nativethread_lock_t lock;
306 char output_file[DEBUG_LOG_MAX_PATH+1];
311RUBY_DEBUG_LOG_MEM_ENTRY(
unsigned int index)
313 return &debug_log.mem[MAX_DEBUG_LOG_MESSAGE_LEN * index];
316static enum debug_log_filter_type
317filter_type(
const char *str,
int *skiplen)
319 if (strncmp(str,
"file:", 5) == 0) {
323 else if(strncmp(str,
"func:", 5) == 0) {
334setup_debug_log_filter(
void)
336 const char *filter_config = getenv(
"RUBY_DEBUG_LOG_FILTER");
338 if (filter_config && strlen(filter_config) > 0) {
340 for (i=0; i<MAX_DEBUG_LOG_FILTER_NUM && filter_config; i++) {
342 const char *str = filter_config;
345 if ((p = strchr(str,
',')) == NULL) {
347 filter_config = NULL;
351 filter_config = p + 1;
356 debug_log.filters[i].negative =
true;
359 else if (*str ==
'+') {
366 debug_log.filters[i].type = filter_type(str, &skiplen);
369 if (
len >= MAX_DEBUG_LOG_FILTER_LEN) {
370 fprintf(stderr,
"too long: %s (max:%d)\n", str, MAX_DEBUG_LOG_FILTER_LEN - 1);
375 strncpy(debug_log.filters[i].str, str + skiplen,
len);
376 debug_log.filters[i].str[
len] = 0;
378 debug_log.filters_num = i;
380 for (i=0; i<debug_log.filters_num; i++) {
381 fprintf(stderr,
"RUBY_DEBUG_LOG_FILTER[%d]=%s (%s%s)\n", i,
382 debug_log.filters[i].str,
383 debug_log.filters[i].negative ?
"-" :
"",
384 dlf_type_names[debug_log.filters[i].
type]);
393 const char *log_config = getenv(
"RUBY_DEBUG_LOG");
394 if (log_config && strlen(log_config) > 0) {
395 if (strcmp(log_config,
"mem") == 0) {
396 debug_log.mem = (
char *)malloc(MAX_DEBUG_LOG * MAX_DEBUG_LOG_MESSAGE_LEN);
397 if (debug_log.mem == NULL) {
398 fprintf(stderr,
"setup_debug_log failed (can't allocate memory)\n");
401 ruby_debug_log_mode |= ruby_debug_log_memory;
403 else if (strcmp(log_config,
"stderr") == 0) {
404 ruby_debug_log_mode |= ruby_debug_log_stderr;
407 ruby_debug_log_mode |= ruby_debug_log_file;
410 unsigned long len = strlen(log_config);
412 for (
unsigned long i=0, j=0; i<
len; i++) {
413 const char c = log_config[i];
417 switch (log_config[i]) {
419 debug_log.output_file[j++] =
'%';
422 snprintf(debug_log.output_file + j, DEBUG_LOG_MAX_PATH - j,
"%d", getpid());
423 j = strlen(debug_log.output_file);
426 fprintf(stderr,
"can not parse RUBY_DEBUG_LOG filename: %s\n", log_config);
431 debug_log.output_file[j++] = c;
434 if (j >= DEBUG_LOG_MAX_PATH) {
435 fprintf(stderr,
"RUBY_DEBUG_LOG=%s is too long\n", log_config);
440 if ((debug_log.output = fopen(debug_log.output_file,
"w")) == NULL) {
441 fprintf(stderr,
"can not open %s for RUBY_DEBUG_LOG\n", log_config);
444 setvbuf(debug_log.output, NULL, _IONBF, 0);
447 fprintf(stderr,
"RUBY_DEBUG_LOG=%s %s%s%s\n", log_config,
448 (ruby_debug_log_mode & ruby_debug_log_memory) ?
"[mem]" :
"",
449 (ruby_debug_log_mode & ruby_debug_log_stderr) ?
"[stderr]" :
"",
450 (ruby_debug_log_mode & ruby_debug_log_file) ?
"[file]" :
"");
451 if (debug_log.output_file[0]) {
452 fprintf(stderr,
"RUBY_DEBUG_LOG filename=%s\n", debug_log.output_file);
457 setup_debug_log_filter();
459 if (getenv(
"RUBY_DEBUG_LOG_PID")) {
460 debug_log.show_pid =
true;
466check_filter(
const char *str,
const struct debug_log_filter *filter,
bool *state)
468 if (filter->negative) {
469 if (strstr(str, filter->str) == NULL) {
479 if (strstr(str, filter->str) != NULL) {
510ruby_debug_log_filter(
const char *func_name,
const char *file_name)
512 if (debug_log.filters_num > 0) {
515 for (
unsigned int i = 0; i<debug_log.filters_num; i++) {
516 const struct debug_log_filter *filter = &debug_log.filters[i];
518 switch (filter->type) {
520 if (check_filter(func_name, filter, &state))
return state;
521 if (check_filter(file_name, filter, &state))
return state;
524 if (check_filter(func_name, filter, &state))
return state;
527 if (check_filter(file_name, filter, &state))
return state;
539pretty_filename(
const char *path)
543 while ((s = strchr(path,
'/')) != NULL) {
551ruby_debug_log(
const char *file,
int line,
const char *func_name,
const char *fmt, ...)
553 char buff[MAX_DEBUG_LOG_MESSAGE_LEN] = {0};
557 if (debug_log.show_pid) {
558 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN,
"pid:%d\t", getpid());
559 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
564 if (func_name &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
565 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN,
"%s\t", func_name);
566 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
571 if (fmt &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
574 r = vsnprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len, fmt, args);
576 if (r < 0) rb_bug(
"ruby_debug_log vsnprintf() returns %d", r);
583 if (file &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
584 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN,
"\t%s:%d", pretty_filename(file), line);
585 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
593 const char *ruby_file = ec ? rb_source_location_cstr(&ruby_line) : NULL;
595 if (
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
597 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\t%s:%d", pretty_filename(ruby_file), ruby_line);
600 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\t");
602 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
608 if (
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
609 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\tnt:%d", ruby_nt_serial);
610 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
616 rb_thread_t *th = ec ? rb_ec_thread_ptr(ec) : NULL;
619 if (ruby_single_main_ractor == NULL) {
623 if (r &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
624 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\tr:#%d/%u (%u)",
625 cr ? (
int)rb_ractor_id(cr) : -1, vm->ractor.cnt, vm->ractor.sched.running_cnt);
627 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
633 if (th && r &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
635 const rb_thread_t *rth = rec ? rec->thread_ptr : NULL;
636 const rb_thread_t *sth = th->ractor ? th->ractor->threads.sched.running : NULL;
638 if (rth != th || sth != th) {
639 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\tth:%u (rth:%d,sth:%d)",
640 rb_th_serial(th), rth ? (
int)rb_th_serial(rth) : -1, sth ? (int)rb_th_serial(sth) : -1);
643 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\tth:%u", rb_th_serial(th));
645 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
652 unsigned int cnt = debug_log.cnt++;
654 if (ruby_debug_log_mode & ruby_debug_log_memory) {
655 unsigned int index = cnt % MAX_DEBUG_LOG;
656 char *dst = RUBY_DEBUG_LOG_MEM_ENTRY(index);
657 strncpy(dst, buff, MAX_DEBUG_LOG_MESSAGE_LEN);
659 if (ruby_debug_log_mode & ruby_debug_log_stderr) {
660 fprintf(stderr,
"%4u: %s\n", cnt, buff);
662 if (ruby_debug_log_mode & ruby_debug_log_file) {
663 fprintf(debug_log.output,
"%u\t%s\n", cnt, buff);
671debug_log_dump(
FILE *out,
unsigned int n)
673 if (ruby_debug_log_mode & ruby_debug_log_memory) {
674 unsigned int size = debug_log.cnt > MAX_DEBUG_LOG ? MAX_DEBUG_LOG : debug_log.cnt;
675 unsigned int current_index = debug_log.cnt % MAX_DEBUG_LOG;
676 if (n == 0) n = size;
677 if (n > size) n = size;
679 for (
unsigned int i=0; i<n; i++) {
680 int index = current_index - size + i;
681 if (index < 0) index += MAX_DEBUG_LOG;
682 VM_ASSERT(index <= MAX_DEBUG_LOG);
683 const char *mesg = RUBY_DEBUG_LOG_MEM_ENTRY(index);
684 fprintf(out,
"%4u: %s\n", debug_log.cnt - size + i, mesg);
688 fprintf(stderr,
"RUBY_DEBUG_LOG=mem is not specified.");
695ruby_debug_log_print(
unsigned int n)
697 debug_log_dump(stderr, n);
701ruby_debug_log_dump(
const char *fname,
unsigned int n)
703 FILE *fp = fopen(fname,
"w");
705 fprintf(stderr,
"can't open %s. give up.\n", fname);
708 debug_log_dump(fp, n);
717ruby_debug_log(
const char *file,
int line,
const char *func_name,
const char *fmt, ...)
721 fprintf(stderr,
"[%s:%d] %s: ", file, line, func_name);
724 vfprintf(stderr, fmt, args);
727 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.