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 (%u)\n", header,
151 ruby_node_name(nd_type(node)), nd_line(node));
157ruby_debug_breakpoint(
void)
163# if RUBY_MSVCRT_VERSION >= 80
164extern int ruby_w32_rtc_error;
167#if defined _WIN32 || defined __CYGWIN__
169UINT ruby_w32_codepage[2];
171extern int ruby_rgengc_debug;
172extern int ruby_on_ci;
175ruby_env_debug_option(
const char *str,
int len,
void *arg)
180#define SET_WHEN(name, var, val) do { \
181 if (len == sizeof(name) - 1 && \
182 strncmp(str, (name), len) == 0) { \
187#define NAME_MATCH_VALUE(name) \
188 ((size_t)len >= sizeof(name)-1 && \
189 strncmp(str, (name), sizeof(name)-1) == 0 && \
190 ((len == sizeof(name)-1 && !(len = 0)) || \
191 (str[sizeof(name)-1] == '=' && \
192 (str += sizeof(name), len -= sizeof(name), 1))))
193#define SET_UINT(val) do { \
194 n = ruby_scan_digits(str, len, 10, &retlen, &ov); \
195 if (!ov && retlen) { \
196 val = (unsigned int)n; \
201#define SET_UINT_LIST(name, vals, num) do { \
203 for (i = 0; i < (num); ++i) { \
204 SET_UINT((vals)[i]); \
205 if (!len || *str != ':') break; \
210 fprintf(stderr, "ignored "name" option: `%.*s'\n", len, str); \
213#define SET_WHEN_UINT(name, vals, num, req) \
214 if (NAME_MATCH_VALUE(name)) SET_UINT_LIST(name, vals, num);
216 SET_WHEN(
"gc_stress", *ruby_initial_gc_stress_ptr,
Qtrue);
217 SET_WHEN(
"core", ruby_enable_coredump, 1);
218 SET_WHEN(
"ci", ruby_on_ci, 1);
219 if (NAME_MATCH_VALUE(
"rgengc")) {
220 if (!
len) ruby_rgengc_debug = 1;
221 else SET_UINT_LIST(
"rgengc", &ruby_rgengc_debug, 1);
225# if RUBY_MSVCRT_VERSION >= 80
226 SET_WHEN(
"rtc_error", ruby_w32_rtc_error, 1);
229#if defined _WIN32 || defined __CYGWIN__
230 if (NAME_MATCH_VALUE(
"codepage")) {
231 if (!
len) fprintf(stderr,
"missing codepage argument");
232 else SET_UINT_LIST(
"codepage", ruby_w32_codepage, numberof(ruby_w32_codepage));
240set_debug_option(
const char *str,
int len,
void *arg)
242 if (!ruby_env_debug_option(str,
len, arg)) {
243 fprintf(stderr,
"unexpected debug option: %.*s\n",
len, str);
247#if USE_RUBY_DEBUG_LOG
248static void setup_debug_log(
void);
250#define setup_debug_log()
254ruby_set_debug_option(
const char *str)
260#if USE_RUBY_DEBUG_LOG
265#define MAX_DEBUG_LOG 0x1000
266#define MAX_DEBUG_LOG_MESSAGE_LEN 0x0200
267#define MAX_DEBUG_LOG_FILTER_LEN 0x0020
268#define MAX_DEBUG_LOG_FILTER_NUM 0x0010
270enum ruby_debug_log_mode ruby_debug_log_mode;
272struct debug_log_filter {
273 enum debug_log_filter_type {
279 char str[MAX_DEBUG_LOG_FILTER_LEN];
282static const char *dlf_type_names[] = {
289#define DEBUG_LOG_MAX_PATH (MAX_PATH-1)
291#define DEBUG_LOG_MAX_PATH 255
297 struct debug_log_filter filters[MAX_DEBUG_LOG_FILTER_NUM];
298 unsigned int filters_num;
300 rb_nativethread_lock_t lock;
301 char output_file[DEBUG_LOG_MAX_PATH+1];
306RUBY_DEBUG_LOG_MEM_ENTRY(
unsigned int index)
308 return &debug_log.mem[MAX_DEBUG_LOG_MESSAGE_LEN * index];
311static enum debug_log_filter_type
312filter_type(
const char *str,
int *skiplen)
314 if (strncmp(str,
"file:", 5) == 0) {
318 else if(strncmp(str,
"func:", 5) == 0) {
329setup_debug_log_filter(
void)
331 const char *filter_config = getenv(
"RUBY_DEBUG_LOG_FILTER");
333 if (filter_config && strlen(filter_config) > 0) {
335 for (i=0; i<MAX_DEBUG_LOG_FILTER_NUM && filter_config; i++) {
337 const char *str = filter_config;
340 if ((p = strchr(str,
',')) == NULL) {
342 filter_config = NULL;
346 filter_config = p + 1;
351 debug_log.filters[i].negative =
true;
354 else if (*str ==
'+') {
361 debug_log.filters[i].type = filter_type(str, &skiplen);
364 if (
len >= MAX_DEBUG_LOG_FILTER_LEN) {
365 fprintf(stderr,
"too long: %s (max:%d)\n", str, MAX_DEBUG_LOG_FILTER_LEN - 1);
370 strncpy(debug_log.filters[i].str, str + skiplen,
len);
371 debug_log.filters[i].str[
len] = 0;
373 debug_log.filters_num = i;
375 for (i=0; i<debug_log.filters_num; i++) {
376 fprintf(stderr,
"RUBY_DEBUG_LOG_FILTER[%d]=%s (%s%s)\n", i,
377 debug_log.filters[i].str,
378 debug_log.filters[i].negative ?
"-" :
"",
379 dlf_type_names[debug_log.filters[i].
type]);
388 const char *log_config = getenv(
"RUBY_DEBUG_LOG");
389 if (log_config && strlen(log_config) > 0) {
390 if (strcmp(log_config,
"mem") == 0) {
391 debug_log.mem = (
char *)malloc(MAX_DEBUG_LOG * MAX_DEBUG_LOG_MESSAGE_LEN);
392 if (debug_log.mem == NULL) {
393 fprintf(stderr,
"setup_debug_log failed (can't allocate memory)\n");
396 ruby_debug_log_mode |= ruby_debug_log_memory;
398 else if (strcmp(log_config,
"stderr") == 0) {
399 ruby_debug_log_mode |= ruby_debug_log_stderr;
402 ruby_debug_log_mode |= ruby_debug_log_file;
405 unsigned long len = strlen(log_config);
407 for (
unsigned long i=0, j=0; i<
len; i++) {
408 const char c = log_config[i];
412 switch (log_config[i]) {
414 debug_log.output_file[j++] =
'%';
417 snprintf(debug_log.output_file + j, DEBUG_LOG_MAX_PATH - j,
"%d", getpid());
418 j = strlen(debug_log.output_file);
421 fprintf(stderr,
"can not parse RUBY_DEBUG_LOG filename: %s\n", log_config);
426 debug_log.output_file[j++] = c;
429 if (j >= DEBUG_LOG_MAX_PATH) {
430 fprintf(stderr,
"RUBY_DEBUG_LOG=%s is too long\n", log_config);
435 if ((debug_log.output = fopen(debug_log.output_file,
"w")) == NULL) {
436 fprintf(stderr,
"can not open %s for RUBY_DEBUG_LOG\n", log_config);
439 setvbuf(debug_log.output, NULL, _IONBF, 0);
442 fprintf(stderr,
"RUBY_DEBUG_LOG=%s %s%s%s\n", log_config,
443 (ruby_debug_log_mode & ruby_debug_log_memory) ?
"[mem]" :
"",
444 (ruby_debug_log_mode & ruby_debug_log_stderr) ?
"[stderr]" :
"",
445 (ruby_debug_log_mode & ruby_debug_log_file) ?
"[file]" :
"");
446 if (debug_log.output_file[0]) {
447 fprintf(stderr,
"RUBY_DEBUG_LOG filename=%s\n", debug_log.output_file);
452 setup_debug_log_filter();
454 if (getenv(
"RUBY_DEBUG_LOG_PID")) {
455 debug_log.show_pid =
true;
461check_filter(
const char *str,
const struct debug_log_filter *filter,
bool *state)
463 if (filter->negative) {
464 if (strstr(str, filter->str) == NULL) {
474 if (strstr(str, filter->str) != NULL) {
505ruby_debug_log_filter(
const char *func_name,
const char *file_name)
507 if (debug_log.filters_num > 0) {
510 for (
unsigned int i = 0; i<debug_log.filters_num; i++) {
511 const struct debug_log_filter *filter = &debug_log.filters[i];
513 switch (filter->type) {
515 if (check_filter(func_name, filter, &state))
return state;
516 if (check_filter(file_name, filter, &state))
return state;
519 if (check_filter(func_name, filter, &state))
return state;
522 if (check_filter(file_name, filter, &state))
return state;
534pretty_filename(
const char *path)
538 while ((s = strchr(path,
'/')) != NULL) {
546ruby_debug_log(
const char *file,
int line,
const char *func_name,
const char *fmt, ...)
548 char buff[MAX_DEBUG_LOG_MESSAGE_LEN] = {0};
552 if (debug_log.show_pid) {
553 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN,
"pid:%d\t", getpid());
554 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
559 if (func_name &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
560 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN,
"%s\t", func_name);
561 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
566 if (fmt &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
569 r = vsnprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len, fmt, args);
571 if (r < 0) rb_bug(
"ruby_debug_log vsnprintf() returns %d", r);
578 if (file &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
579 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN,
"\t%s:%d", pretty_filename(file), line);
580 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
588 const char *ruby_file = ec ? rb_source_location_cstr(&ruby_line) : NULL;
590 if (
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
592 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\t%s:%d", pretty_filename(ruby_file), ruby_line);
595 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\t");
597 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
603 if (
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
604 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\tnt:%d", ruby_nt_serial);
605 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
611 rb_thread_t *th = ec ? rb_ec_thread_ptr(ec) : NULL;
614 if (ruby_single_main_ractor == NULL) {
618 if (r &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
619 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\tr:#%d/%u (%u)",
620 cr ? (
int)rb_ractor_id(cr) : -1, vm->ractor.cnt, vm->ractor.sched.running_cnt);
622 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
628 if (th && r &&
len < MAX_DEBUG_LOG_MESSAGE_LEN) {
630 const rb_thread_t *rth = rec ? rec->thread_ptr : NULL;
631 const rb_thread_t *sth = th->ractor ? th->ractor->threads.sched.running : NULL;
633 if (rth != th || sth != th) {
634 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\tth:%u (rth:%d,sth:%d)",
635 rb_th_serial(th), rth ? (
int)rb_th_serial(rth) : -1, sth ? (int)rb_th_serial(sth) : -1);
638 r = snprintf(buff +
len, MAX_DEBUG_LOG_MESSAGE_LEN -
len,
"\tth:%u", rb_th_serial(th));
640 if (r < 0) rb_bug(
"ruby_debug_log returns %d", r);
647 unsigned int cnt = debug_log.cnt++;
649 if (ruby_debug_log_mode & ruby_debug_log_memory) {
650 unsigned int index = cnt % MAX_DEBUG_LOG;
651 char *dst = RUBY_DEBUG_LOG_MEM_ENTRY(index);
652 strncpy(dst, buff, MAX_DEBUG_LOG_MESSAGE_LEN);
654 if (ruby_debug_log_mode & ruby_debug_log_stderr) {
655 fprintf(stderr,
"%4u: %s\n", cnt, buff);
657 if (ruby_debug_log_mode & ruby_debug_log_file) {
658 fprintf(debug_log.output,
"%u\t%s\n", cnt, buff);
666debug_log_dump(
FILE *out,
unsigned int n)
668 if (ruby_debug_log_mode & ruby_debug_log_memory) {
669 unsigned int size = debug_log.cnt > MAX_DEBUG_LOG ? MAX_DEBUG_LOG : debug_log.cnt;
670 unsigned int current_index = debug_log.cnt % MAX_DEBUG_LOG;
671 if (n == 0) n = size;
672 if (n > size) n = size;
674 for (
unsigned int i=0; i<n; i++) {
675 int index = current_index - size + i;
676 if (index < 0) index += MAX_DEBUG_LOG;
677 VM_ASSERT(index <= MAX_DEBUG_LOG);
678 const char *mesg = RUBY_DEBUG_LOG_MEM_ENTRY(index);;
679 fprintf(out,
"%4u: %s\n", debug_log.cnt - size + i, mesg);
683 fprintf(stderr,
"RUBY_DEBUG_LOG=mem is not specified.");
690ruby_debug_log_print(
unsigned int n)
692 debug_log_dump(stderr, n);
696ruby_debug_log_dump(
const char *fname,
unsigned int n)
698 FILE *fp = fopen(fname,
"w");
700 fprintf(stderr,
"can't open %s. give up.\n", fname);
703 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.
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.