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_rmodule_flags rmodule_flags;
56 enum ruby_rstring_flags rstring_flags;
57 enum ruby_rarray_flags rarray_flags;
59 enum rbimpl_typeddata_flags rtypeddata_consts;
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;
172extern int ruby_w32_wer;
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);
236 SET_WHEN(
"wer", ruby_w32_wer, 1);
238#if defined _WIN32 || defined __CYGWIN__
239 SET_WHEN_UINT(
"codepage", ruby_w32_codepage, numberof(ruby_w32_codepage),
240 fprintf(stderr,
"missing codepage argument"));
246set_debug_option(
const char *str,
int len,
void *arg)
248 if (!ruby_env_debug_option(str,
len, arg)) {
249 fprintf(stderr,
"unexpected debug option: %.*s\n",
len, str);
253#if USE_RUBY_DEBUG_LOG
254static void setup_debug_log(
void);
256#define setup_debug_log()
260ruby_set_debug_option(
const char *str)
266#if USE_RUBY_DEBUG_LOG
271#define MAX_DEBUG_LOG 0x1000
272#define MAX_DEBUG_LOG_MESSAGE_LEN 0x0200
273#define MAX_DEBUG_LOG_FILTER_LEN 0x0020
274#define MAX_DEBUG_LOG_FILTER_NUM 0x0010
276enum ruby_debug_log_mode ruby_debug_log_mode;
278struct debug_log_filter {
279 enum debug_log_filter_type {
285 char str[MAX_DEBUG_LOG_FILTER_LEN];
288static const char *dlf_type_names[] = {
295#define DEBUG_LOG_MAX_PATH (MAX_PATH-1)
297#define DEBUG_LOG_MAX_PATH 255
303 struct debug_log_filter filters[MAX_DEBUG_LOG_FILTER_NUM];
304 unsigned int filters_num;
306 rb_nativethread_lock_t lock;
307 char output_file[DEBUG_LOG_MAX_PATH+1];
312RUBY_DEBUG_LOG_MEM_ENTRY(
unsigned int index)
314 return &debug_log.mem[MAX_DEBUG_LOG_MESSAGE_LEN * index];
317static enum debug_log_filter_type
318filter_type(
const char *str,
int *skiplen)
320 if (strncmp(str,
"file:", 5) == 0) {
324 else if(strncmp(str,
"func:", 5) == 0) {
335setup_debug_log_filter(
void)
337 const char *filter_config = getenv(
"RUBY_DEBUG_LOG_FILTER");
339 if (filter_config && strlen(filter_config) > 0) {
341 for (i=0; i<MAX_DEBUG_LOG_FILTER_NUM && filter_config; i++) {
343 const char *str = filter_config;
346 if ((p = strchr(str,
',')) == NULL) {
348 filter_config = NULL;
352 filter_config = p + 1;
357 debug_log.filters[i].negative =
true;
360 else if (*str ==
'+') {
367 debug_log.filters[i].type = filter_type(str, &skiplen);
370 if (
len >= MAX_DEBUG_LOG_FILTER_LEN) {
371 fprintf(stderr,
"too long: %s (max:%d)\n", str, MAX_DEBUG_LOG_FILTER_LEN - 1);
376 strncpy(debug_log.filters[i].str, str + skiplen,
len);
377 debug_log.filters[i].str[
len] = 0;
379 debug_log.filters_num = i;
381 for (i=0; i<debug_log.filters_num; i++) {
382 fprintf(stderr,
"RUBY_DEBUG_LOG_FILTER[%d]=%s (%s%s)\n", i,
383 debug_log.filters[i].str,
384 debug_log.filters[i].negative ?
"-" :
"",
385 dlf_type_names[debug_log.filters[i].
type]);
394 const char *log_config = getenv(
"RUBY_DEBUG_LOG");
395 if (log_config && strlen(log_config) > 0) {
396 if (strcmp(log_config,
"mem") == 0) {
397 debug_log.mem = (
char *)malloc(MAX_DEBUG_LOG * MAX_DEBUG_LOG_MESSAGE_LEN);
398 if (debug_log.mem == NULL) {
399 fprintf(stderr,
"setup_debug_log failed (can't allocate memory)\n");
402 ruby_debug_log_mode |= ruby_debug_log_memory;
404 else if (strcmp(log_config,
"stderr") == 0) {
405 ruby_debug_log_mode |= ruby_debug_log_stderr;
408 ruby_debug_log_mode |= ruby_debug_log_file;
411 unsigned long len = strlen(log_config);
413 for (
unsigned long i=0, j=0; i<
len; i++) {
414 const char c = log_config[i];
418 switch (log_config[i]) {
420 debug_log.output_file[j++] =
'%';
423 snprintf(debug_log.output_file + j, DEBUG_LOG_MAX_PATH - j,
"%d", getpid());
424 j = strlen(debug_log.output_file);
427 fprintf(stderr,
"can not parse RUBY_DEBUG_LOG filename: %s\n", log_config);
432 debug_log.output_file[j++] = c;
435 if (j >= DEBUG_LOG_MAX_PATH) {
436 fprintf(stderr,
"RUBY_DEBUG_LOG=%s is too long\n", log_config);
441 if ((debug_log.output = fopen(debug_log.output_file,
"w")) == NULL) {
442 fprintf(stderr,
"can not open %s for RUBY_DEBUG_LOG\n", log_config);
445 setvbuf(debug_log.output, NULL, _IONBF, 0);
448 fprintf(stderr,
"RUBY_DEBUG_LOG=%s %s%s%s\n", log_config,
449 (ruby_debug_log_mode & ruby_debug_log_memory) ?
"[mem]" :
"",
450 (ruby_debug_log_mode & ruby_debug_log_stderr) ?
"[stderr]" :
"",
451 (ruby_debug_log_mode & ruby_debug_log_file) ?
"[file]" :
"");
452 if (debug_log.output_file[0]) {
453 fprintf(stderr,
"RUBY_DEBUG_LOG filename=%s\n", debug_log.output_file);
458 setup_debug_log_filter();
460 if (getenv(
"RUBY_DEBUG_LOG_PID")) {
461 debug_log.show_pid =
true;
467check_filter(
const char *str,
const struct debug_log_filter *filter,
bool *state)
469 if (filter->negative) {
470 if (strstr(str, filter->str) == NULL) {
480 if (strstr(str, filter->str) != NULL) {
511ruby_debug_log_filter(
const char *func_name,
const char *file_name)
513 if (debug_log.filters_num > 0) {
516 for (
unsigned int i = 0; i<debug_log.filters_num; i++) {
517 const struct debug_log_filter *filter = &debug_log.filters[i];
519 switch (filter->type) {
521 if (check_filter(func_name, filter, &state))
return state;
522 if (check_filter(file_name, filter, &state))
return state;
525 if (check_filter(func_name, filter, &state))
return state;
528 if (check_filter(file_name, filter, &state))
return state;
540pretty_filename(
const char *path)
544 while ((s = strchr(path,
'/')) != NULL) {
552ruby_debug_log(
const char *file,
int line,
const char *func_name,
const char *fmt, ...)
554 char buff[MAX_DEBUG_LOG_MESSAGE_LEN] = {0};
558 if (debug_log.show_pid) {
559 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN,
"pid:%d\t", getpid());
560 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
565 if (func_name &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
566 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN,
"%s\t", func_name);
567 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
572 if (fmt &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
575 r = vsnprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len, fmt, args);
577 if (r < 0) rb_bug(
"ruby_debug_log vsnprintf() returns %d", r);
584 if (file &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
585 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN,
"\t%s:%d", pretty_filename(file), line);
586 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
594 const char *ruby_file = ec ? rb_source_location_cstr(&ruby_line) : NULL;
596 if (
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
598 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\t%s:%d", pretty_filename(ruby_file), ruby_line);
601 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\t");
603 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
609 if (
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
610 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\tnt:%d", ruby_nt_serial);
611 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
617 rb_thread_t *th = ec ? rb_ec_thread_ptr(ec) : NULL;
620 if (ruby_single_main_ractor == NULL) {
624 if (r &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
625 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\tr:#%d/%u",
626 cr ? (
int)rb_ractor_id(cr) : -1, vm->ractor.cnt);
628 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
634 if (th && r &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
636 const rb_thread_t *rth = rec ? rec->thread_ptr : NULL;
637 const rb_thread_t *sth = th->ractor ? th->ractor->threads.sched.running : NULL;
639 if (rth != th || sth != th) {
640 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\tth:%u (rth:%d,sth:%d)",
641 rb_th_serial(th), rth ? (
int)rb_th_serial(rth) : -1, sth ? (int)rb_th_serial(sth) : -1);
644 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\tth:%u", rb_th_serial(th));
646 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
653 unsigned int cnt = debug_log.cnt++;
655 if (ruby_debug_log_mode & ruby_debug_log_memory) {
656 unsigned int index = cnt % MAX_DEBUG_LOG;
657 char *dst = RUBY_DEBUG_LOG_MEM_ENTRY(index);
658 strncpy(dst, buff, MAX_DEBUG_LOG_MESSAGE_LEN);
660 if (ruby_debug_log_mode & ruby_debug_log_stderr) {
661 fprintf(stderr,
"%4u: %s\n", cnt, buff);
663 if (ruby_debug_log_mode & ruby_debug_log_file) {
664 fprintf(debug_log.output,
"%u\t%s\n", cnt, buff);
672debug_log_dump(
FILE *out,
unsigned int n)
674 if (ruby_debug_log_mode & ruby_debug_log_memory) {
675 unsigned int size = debug_log.cnt > MAX_DEBUG_LOG ? MAX_DEBUG_LOG : debug_log.cnt;
676 unsigned int current_index = debug_log.cnt % MAX_DEBUG_LOG;
677 if (n == 0) n = size;
678 if (n > size) n = size;
680 for (
unsigned int i=0; i<n; i++) {
681 int index = current_index - size + i;
682 if (index < 0) index += MAX_DEBUG_LOG;
683 VM_ASSERT(index <= MAX_DEBUG_LOG);
684 const char *mesg = RUBY_DEBUG_LOG_MEM_ENTRY(index);
685 fprintf(out,
"%4u: %s\n", debug_log.cnt - size + i, mesg);
689 fprintf(stderr,
"RUBY_DEBUG_LOG=mem is not specified.");
696ruby_debug_log_print(
unsigned int n)
698 debug_log_dump(stderr, n);
702ruby_debug_log_dump(
const char *fname,
unsigned int n)
704 FILE *fp = fopen(fname,
"w");
706 fprintf(stderr,
"can't open %s. give up.\n", fname);
709 debug_log_dump(fp, n);
718ruby_debug_log(
const char *file,
int line,
const char *func_name,
const char *fmt, ...)
722 fprintf(stderr,
"[%s:%d] %s: ", file, line, func_name);
725 vfprintf(stderr, fmt, args);
728 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.