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;
59 enum ruby_rmodule_flags rmodule_flags;
60 enum ruby_rstring_flags rstring_flags;
64 enum ruby_rarray_flags rarray_flags;
76 RUBY_FMODE_NOREVLOOKUP = 0x00000100,
79 RUBY_FMODE_PREP = 0x00010000,
81 RUBY_FMODE_UNIX = 0x00200000,
82 RUBY_FMODE_INET = 0x00400000,
83 RUBY_FMODE_INET6 = 0x00800000,
85 RUBY_NODE_TYPESHIFT = NODE_TYPESHIFT,
86 RUBY_NODE_TYPEMASK = NODE_TYPEMASK,
87 RUBY_NODE_LSHIFT = NODE_LSHIFT,
88 RUBY_NODE_FL_NEWLINE = NODE_FL_NEWLINE
91 enum imemo_type types;
92 enum {RUBY_IMEMO_MASK = IMEMO_MASK} mask;
96 enum vm_call_flag_bits vm_call_flags;
97} ruby_dummy_gdb_enums;
102ruby_debug_print_indent(
int level,
int debug_level,
int indent_level)
104 if (level < debug_level) {
105 fprintf(stderr,
"%*s", indent_level,
"");
113ruby_debug_printf(
const char *format, ...)
116 va_start(ap, format);
117 vfprintf(stderr, format, ap);
124ruby_debug_print_value(
int level,
int debug_level,
const char *header,
VALUE obj)
126 if (level < debug_level) {
128 rb_raw_obj_info(buff, 0x100, obj);
130 fprintf(stderr,
"DBG> %s: %s\n", header, buff);
137ruby_debug_print_v(
VALUE v)
139 ruby_debug_print_value(0, 1,
"", v);
143ruby_debug_print_id(
int level,
int debug_level,
const char *header,
ID id)
145 if (level < debug_level) {
146 fprintf(stderr,
"DBG> %s: %s\n", header,
rb_id2name(
id));
153ruby_debug_print_node(
int level,
int debug_level,
const char *header,
const NODE *node)
155 if (level < debug_level) {
156 fprintf(stderr,
"DBG> %s: %s (%u)\n", header,
157 ruby_node_name(nd_type(node)), nd_line(node));
163ruby_debug_breakpoint(
void)
169# if RUBY_MSVCRT_VERSION >= 80
170extern 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 SET_WHEN(name, var, val) do { \
187 if (len == sizeof(name) - 1 && \
188 strncmp(str, (name), len) == 0) { \
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)) SET_UINT_LIST(name, vals, num);
222 SET_WHEN(
"gc_stress", *ruby_initial_gc_stress_ptr,
Qtrue);
223 SET_WHEN(
"core", ruby_enable_coredump, 1);
224 SET_WHEN(
"ci", ruby_on_ci, 1);
225 if (NAME_MATCH_VALUE(
"rgengc")) {
226 if (!len) ruby_rgengc_debug = 1;
227 else SET_UINT_LIST(
"rgengc", &ruby_rgengc_debug, 1);
231# if RUBY_MSVCRT_VERSION >= 80
232 SET_WHEN(
"rtc_error", ruby_w32_rtc_error, 1);
235#if defined _WIN32 || defined __CYGWIN__
236 if (NAME_MATCH_VALUE(
"codepage")) {
237 if (!len) fprintf(stderr,
"missing codepage argument");
238 else SET_UINT_LIST(
"codepage", ruby_w32_codepage, numberof(ruby_w32_codepage));
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;
279 enum debug_log_filter_type {
285 char str[MAX_DEBUG_LOG_FILTER_LEN];
288static const char *dlf_type_names[] = {
298 unsigned int filters_num;
304RUBY_DEBUG_LOG_MEM_ENTRY(
unsigned int index)
306 return &debug_log.mem[MAX_DEBUG_LOG_MESSAGE_LEN * index];
309static enum debug_log_filter_type
310filter_type(
const char *str,
int *skiplen)
312 if (strncmp(str,
"file:", 5) == 0) {
316 else if(strncmp(str,
"func:", 5) == 0) {
327setup_debug_log_filter(
void)
329 const char *filter_config = getenv(
"RUBY_DEBUG_LOG_FILTER");
331 if (filter_config && strlen(filter_config) > 0) {
333 for (i=0; i<MAX_DEBUG_LOG_FILTER_NUM && filter_config; i++) {
335 const char *str = filter_config;
338 if ((p = strchr(str,
',')) == NULL) {
340 filter_config = NULL;
344 filter_config = p + 1;
349 debug_log.filters[i].negative =
true;
352 else if (*str ==
'+') {
359 debug_log.filters[i].type = filter_type(str, &skiplen);
362 if (len >= MAX_DEBUG_LOG_FILTER_LEN) {
363 fprintf(stderr,
"too long: %s (max:%d)\n", str, MAX_DEBUG_LOG_FILTER_LEN - 1);
368 strncpy(debug_log.filters[i].str, str + skiplen, len);
369 debug_log.filters[i].str[len] = 0;
371 debug_log.filters_num = i;
373 for (i=0; i<debug_log.filters_num; i++) {
374 fprintf(stderr,
"RUBY_DEBUG_LOG_FILTER[%d]=%s (%s%s)\n", i,
375 debug_log.filters[i].str,
376 debug_log.filters[i].negative ?
"-" :
"",
377 dlf_type_names[debug_log.filters[i].
type]);
386 const char *log_config = getenv(
"RUBY_DEBUG_LOG");
387 if (log_config && strlen(log_config) > 0) {
388 if (strcmp(log_config,
"mem") == 0) {
389 debug_log.mem = (
char *)malloc(MAX_DEBUG_LOG * MAX_DEBUG_LOG_MESSAGE_LEN);
390 if (debug_log.mem == NULL) {
391 fprintf(stderr,
"setup_debug_log failed (can't allocate memory)\n");
394 ruby_debug_log_mode |= ruby_debug_log_memory;
396 else if (strcmp(log_config,
"stderr") == 0) {
397 ruby_debug_log_mode |= ruby_debug_log_stderr;
400 ruby_debug_log_mode |= ruby_debug_log_file;
401 if ((debug_log.output = fopen(log_config,
"w")) == NULL) {
402 fprintf(stderr,
"can not open %s for RUBY_DEBUG_LOG\n", log_config);
405 setvbuf(debug_log.output, NULL, _IONBF, 0);
408 fprintf(stderr,
"RUBY_DEBUG_LOG=%s %s%s%s\n", log_config,
409 (ruby_debug_log_mode & ruby_debug_log_memory) ?
"[mem]" :
"",
410 (ruby_debug_log_mode & ruby_debug_log_stderr) ?
"[stderr]" :
"",
411 (ruby_debug_log_mode & ruby_debug_log_file) ?
"[file]" :
"");
414 setup_debug_log_filter();
419check_filter(
const char *str,
const struct debug_log_filter *filter,
bool *state)
421 if (filter->negative) {
422 if (strstr(str, filter->str) == NULL) {
432 if (strstr(str, filter->str) != NULL) {
463ruby_debug_log_filter(
const char *func_name,
const char *file_name)
465 if (debug_log.filters_num > 0) {
468 for (
unsigned int i = 0; i<debug_log.filters_num; i++) {
471 switch (filter->type) {
473 if (check_filter(func_name, filter, &state))
return state;
474 if (check_filter(file_name, filter, &state))
return state;
477 if (check_filter(func_name, filter, &state))
return state;
480 if (check_filter(file_name, filter, &state))
return state;
492pretty_filename(
const char *path)
496 while ((s = strchr(path,
'/')) != NULL) {
504ruby_debug_log(
const char *file,
int line,
const char *func_name,
const char *fmt, ...)
506 char buff[MAX_DEBUG_LOG_MESSAGE_LEN] = {0};
511 if (func_name && len < MAX_DEBUG_LOG_MESSAGE_LEN) {
512 r = snprintf(buff + len, MAX_DEBUG_LOG_MESSAGE_LEN,
"%s\t", func_name);
513 if (r < 0)
rb_bug(
"ruby_debug_log returns %d\n", r);
518 if (fmt && len < MAX_DEBUG_LOG_MESSAGE_LEN) {
521 r = vsnprintf(buff + len, MAX_DEBUG_LOG_MESSAGE_LEN - len, fmt, args);
523 if (r < 0)
rb_bug(
"ruby_debug_log vsnprintf() returns %d", r);
530 if (file && len < MAX_DEBUG_LOG_MESSAGE_LEN) {
531 r = snprintf(buff + len, MAX_DEBUG_LOG_MESSAGE_LEN,
"\t%s:%d", pretty_filename(file), line);
532 if (r < 0)
rb_bug(
"ruby_debug_log returns %d\n", r);
536 if (rb_current_execution_context(
false)) {
539 const char *ruby_file = rb_source_location_cstr(&ruby_line);
540 if (len < MAX_DEBUG_LOG_MESSAGE_LEN) {
542 r = snprintf(buff + len, MAX_DEBUG_LOG_MESSAGE_LEN - len,
"\t%s:%d", pretty_filename(ruby_file), ruby_line);
545 r = snprintf(buff + len, MAX_DEBUG_LOG_MESSAGE_LEN - len,
"\t");
547 if (r < 0)
rb_bug(
"ruby_debug_log returns %d\n", r);
552 if (ruby_single_main_ractor == NULL) {
554 if (r && len < MAX_DEBUG_LOG_MESSAGE_LEN) {
555 r = snprintf(buff + len, MAX_DEBUG_LOG_MESSAGE_LEN - len,
"\tr:#%u/%u",
556 (
unsigned int)rb_ractor_id(cr), GET_VM()->ractor.cnt);
557 if (r < 0)
rb_bug(
"ruby_debug_log returns %d\n", r);
564 if (r && len < MAX_DEBUG_LOG_MESSAGE_LEN) {
565 r = snprintf(buff + len, MAX_DEBUG_LOG_MESSAGE_LEN - len,
"\tth:%u", rb_th_serial(th));
566 if (r < 0)
rb_bug(
"ruby_debug_log returns %d\n", r);
573 unsigned int cnt = debug_log.cnt++;
575 if (ruby_debug_log_mode & ruby_debug_log_memory) {
576 unsigned int index = cnt % MAX_DEBUG_LOG;
577 char *dst = RUBY_DEBUG_LOG_MEM_ENTRY(index);
578 strncpy(dst, buff, MAX_DEBUG_LOG_MESSAGE_LEN);
580 if (ruby_debug_log_mode & ruby_debug_log_stderr) {
581 fprintf(stderr,
"%4u: %s\n", cnt, buff);
583 if (ruby_debug_log_mode & ruby_debug_log_file) {
584 fprintf(debug_log.output,
"%u\t%s\n", cnt, buff);
592debug_log_dump(
FILE *out,
unsigned int n)
594 if (ruby_debug_log_mode & ruby_debug_log_memory) {
595 unsigned int size = debug_log.cnt > MAX_DEBUG_LOG ? MAX_DEBUG_LOG : debug_log.cnt;
596 unsigned int current_index = debug_log.cnt % MAX_DEBUG_LOG;
597 if (n == 0) n = size;
598 if (n > size) n = size;
600 for (
unsigned int i=0; i<n; i++) {
601 int index = current_index - size + i;
602 if (index < 0) index += MAX_DEBUG_LOG;
603 VM_ASSERT(index <= MAX_DEBUG_LOG);
604 const char *mesg = RUBY_DEBUG_LOG_MEM_ENTRY(index);;
605 fprintf(out,
"%4u: %s\n", debug_log.cnt - size + i, mesg);
609 fprintf(stderr,
"RUBY_DEBUG_LOG=mem is not specified.");
616ruby_debug_log_print(
unsigned int n)
618 debug_log_dump(stderr, n);
622ruby_debug_log_dump(
const char *fname,
unsigned int n)
624 FILE *fp = fopen(fname,
"w");
626 fprintf(stderr,
"can't open %s. give up.\n", fname);
629 debug_log_dump(fp, 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.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
rb_econv_result_t
return value of rb_econv_convert()
ruby_econv_flag_type
This enum is kind of omnibus.
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
#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 ...
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_robject_consts
This is an enum because GDB wants it (rather than a macro).
ruby_rstring_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 VALUE
Type that represents a Ruby object.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
ruby_value_type
C-level type of an object.