Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

debug.c
Go to the documentation of this file.
1 /**********************************************************************
2
3 debug.c -
4
5 $Author$
6 created at: 04/08/25 02:31:54 JST
7
8 Copyright (C) 2004-2007 Koichi Sasada
9
10 **********************************************************************/
11
12 #include "ruby/ruby.h"
13 #include "ruby/encoding.h"
14 #include "ruby/io.h"
15 #include "ruby/util.h"
16 #include "vm_debug.h"
17 #include "eval_intern.h"
18 #include "vm_core.h"
19 #include "symbol.h"
20 #include "id.h"
21
22 /* for gdb */
23 const union {
24 enum ruby_special_consts special_consts;
25 enum ruby_value_type value_type;
26 enum ruby_tag_type tag_type;
27 enum node_type node_type;
28 enum ruby_method_ids method_ids;
29 enum ruby_id_types id_types;
30 enum ruby_fl_type fl_types;
31 enum ruby_encoding_consts encoding_consts;
32 enum ruby_coderange_type enc_coderange_types;
33 enum ruby_econv_flag_type econv_flag_types;
34 enum ruby_robject_flags robject_flags;
35 enum ruby_rmodule_flags rmodule_flags;
36 enum ruby_rstring_flags rstring_flags;
37 enum ruby_rarray_flags rarray_flags;
38 enum {
39 RUBY_FMODE_READABLE = FMODE_READABLE,
40 RUBY_FMODE_WRITABLE = FMODE_WRITABLE,
41 RUBY_FMODE_READWRITE = FMODE_READWRITE,
42 RUBY_FMODE_BINMODE = FMODE_BINMODE,
43 RUBY_FMODE_SYNC = FMODE_SYNC,
44 RUBY_FMODE_TTY = FMODE_TTY,
45 RUBY_FMODE_DUPLEX = FMODE_DUPLEX,
46 RUBY_FMODE_APPEND = FMODE_APPEND,
47 RUBY_FMODE_CREATE = FMODE_CREATE,
48 RUBY_FMODE_NOREVLOOKUP = 0x00000100,
49 RUBY_FMODE_TRUNC = FMODE_TRUNC,
50 RUBY_FMODE_TEXTMODE = FMODE_TEXTMODE,
51 RUBY_FMODE_PREP = 0x00010000,
52 RUBY_FMODE_SETENC_BY_BOM = FMODE_SETENC_BY_BOM,
53 RUBY_FMODE_UNIX = 0x00200000,
54 RUBY_FMODE_INET = 0x00400000,
55 RUBY_FMODE_INET6 = 0x00800000,
56
57 RUBY_NODE_TYPESHIFT = NODE_TYPESHIFT,
58 RUBY_NODE_TYPEMASK = NODE_TYPEMASK,
59 RUBY_NODE_LSHIFT = NODE_LSHIFT,
60 RUBY_NODE_FL_NEWLINE = NODE_FL_NEWLINE
61 } various;
62 union {
63 enum imemo_type types;
64 enum {RUBY_IMEMO_MASK = IMEMO_MASK} mask;
65 struct RIMemo *ptr;
66 } imemo;
67 struct RSymbol *symbol_ptr;
68 enum vm_call_flag_bits vm_call_flags;
69 } ruby_dummy_gdb_enums;
70
71 const SIGNED_VALUE RUBY_NODE_LMASK = NODE_LMASK;
72
73 int
74 ruby_debug_print_indent(int level, int debug_level, int indent_level)
75 {
76 if (level < debug_level) {
77 fprintf(stderr, "%*s", indent_level, "");
78 fflush(stderr);
79 return TRUE;
80 }
81 return FALSE;
82 }
83
84 void
85 ruby_debug_printf(const char *format, ...)
86 {
87 va_list ap;
88 va_start(ap, format);
89 vfprintf(stderr, format, ap);
90 va_end(ap);
91 }
92
93 #include "gc.h"
94
95 VALUE
96 ruby_debug_print_value(int level, int debug_level, const char *header, VALUE obj)
97 {
98 if (level < debug_level) {
99 char buff[0x100];
100 rb_raw_obj_info(buff, 0x100, obj);
101
102 fprintf(stderr, "DBG> %s: %s\n", header, buff);
103 fflush(stderr);
104 }
105 return obj;
106 }
107
108 void
109 ruby_debug_print_v(VALUE v)
110 {
111 ruby_debug_print_value(0, 1, "", v);
112 }
113
114 ID
115 ruby_debug_print_id(int level, int debug_level, const char *header, ID id)
116 {
117 if (level < debug_level) {
118 fprintf(stderr, "DBG> %s: %s\n", header, rb_id2name(id));
119 fflush(stderr);
120 }
121 return id;
122 }
123
124 NODE *
125 ruby_debug_print_node(int level, int debug_level, const char *header, const NODE *node)
126 {
127 if (level < debug_level) {
128 fprintf(stderr, "DBG> %s: %s (%u)\n", header,
129 ruby_node_name(nd_type(node)), nd_line(node));
130 }
131 return (NODE *)node;
132 }
133
134 void
135 ruby_debug_breakpoint(void)
136 {
137 /* */
138 }
139
140 #if defined _WIN32
141 # if RUBY_MSVCRT_VERSION >= 80
142 extern int ruby_w32_rtc_error;
143 # endif
144 #endif
145 #if defined _WIN32 || defined __CYGWIN__
146 #include <windows.h>
147 UINT ruby_w32_codepage[2];
148 #endif
149 extern int ruby_rgengc_debug;
150 extern int ruby_on_ci;
151
152 int
153 ruby_env_debug_option(const char *str, int len, void *arg)
154 {
155 int ov;
156 size_t retlen;
157 unsigned long n;
158 #define SET_WHEN(name, var, val) do { \
159 if (len == sizeof(name) - 1 && \
160 strncmp(str, (name), len) == 0) { \
161 (var) = (val); \
162 return 1; \
163 } \
164 } while (0)
165 #define NAME_MATCH_VALUE(name) \
166 ((size_t)len >= sizeof(name)-1 && \
167 strncmp(str, (name), sizeof(name)-1) == 0 && \
168 ((len == sizeof(name)-1 && !(len = 0)) || \
169 (str[sizeof(name)-1] == '=' && \
170 (str += sizeof(name), len -= sizeof(name), 1))))
171 #define SET_UINT(val) do { \
172 n = ruby_scan_digits(str, len, 10, &retlen, &ov); \
173 if (!ov && retlen) { \
174 val = (unsigned int)n; \
175 } \
176 str += retlen; \
177 len -= retlen; \
178 } while (0)
179 #define SET_UINT_LIST(name, vals, num) do { \
180 int i; \
181 for (i = 0; i < (num); ++i) { \
182 SET_UINT((vals)[i]); \
183 if (!len || *str != ':') break; \
184 ++str; \
185 --len; \
186 } \
187 if (len > 0) { \
188 fprintf(stderr, "ignored "name" option: `%.*s'\n", len, str); \
189 } \
190 } while (0)
191 #define SET_WHEN_UINT(name, vals, num, req) \
192 if (NAME_MATCH_VALUE(name)) SET_UINT_LIST(name, vals, num);
193
194 SET_WHEN("gc_stress", *ruby_initial_gc_stress_ptr, Qtrue);
195 SET_WHEN("core", ruby_enable_coredump, 1);
196 SET_WHEN("ci", ruby_on_ci, 1);
197 if (NAME_MATCH_VALUE("rgengc")) {
198 if (!len) ruby_rgengc_debug = 1;
199 else SET_UINT_LIST("rgengc", &ruby_rgengc_debug, 1);
200 return 1;
201 }
202 #if defined _WIN32
203 # if RUBY_MSVCRT_VERSION >= 80
204 SET_WHEN("rtc_error", ruby_w32_rtc_error, 1);
205 # endif
206 #endif
207 #if defined _WIN32 || defined __CYGWIN__
208 if (NAME_MATCH_VALUE("codepage")) {
209 if (!len) fprintf(stderr, "missing codepage argument");
210 else SET_UINT_LIST("codepage", ruby_w32_codepage, numberof(ruby_w32_codepage));
211 return 1;
212 }
213 #endif
214 return 0;
215 }
216
217 static void
218 set_debug_option(const char *str, int len, void *arg)
219 {
220 if (!ruby_env_debug_option(str, len, arg)) {
221 fprintf(stderr, "unexpected debug option: %.*s\n", len, str);
222 }
223 }
224
225 void
226 ruby_set_debug_option(const char *str)
227 {
228 ruby_each_words(str, set_debug_option, 0);
229 }
NODE_FL_NEWLINE
#define NODE_FL_NEWLINE
Definition: node.h:183
ID
unsigned long ID
Definition: ruby.h:103
ruby_node_name
const char * ruby_node_name(int node)
Definition: iseq.c:2534
rb_raw_obj_info
const char * rb_raw_obj_info(char *buff, const int buff_size, VALUE obj)
Definition: gc.c:11419
rb_id2name
const char * rb_id2name(ID)
Definition: symbol.c:801
TRUE
#define TRUE
Definition: nkf.h:175
ruby_rmodule_flags
ruby_rmodule_flags
Definition: ruby.h:954
ruby_method_ids
ruby_method_ids
Definition: id.h:78
vm_call_flag_bits
vm_call_flag_bits
Definition: vm_core.h:1085
id
const int id
Definition: nkf.c:209
ruby_value_type
ruby_value_type
Definition: ruby.h:486
value_type
enum ruby_value_type value_type
Definition: debug.c:34
IMEMO_MASK
#define IMEMO_MASK
Definition: internal.h:1145
gc.h
rmodule_flags
enum ruby_rmodule_flags rmodule_flags
Definition: debug.c:44
FMODE_TTY
#define FMODE_TTY
Definition: io.h:113
ruby_rstring_flags
ruby_rstring_flags
Definition: ruby.h:977
special_consts
enum ruby_special_consts special_consts
Definition: debug.c:33
VALUE
unsigned long VALUE
Definition: ruby.h:102
encoding.h
FMODE_TRUNC
#define FMODE_TRUNC
Definition: io.h:119
id.h
SIGNED_VALUE
#define SIGNED_VALUE
Definition: ruby.h:104
RUBY_NODE_LMASK
const SIGNED_VALUE RUBY_NODE_LMASK
Definition: debug.c:71
id_types
enum ruby_id_types id_types
Definition: debug.c:38
imemo
union @11::@13 imemo
ruby_id_types
ruby_id_types
Definition: id.h:16
ruby_debug_breakpoint
void ruby_debug_breakpoint(void)
Definition: debug.c:135
ptr
struct RIMemo * ptr
Definition: debug.c:74
ruby_debug_print_node
NODE * ruby_debug_print_node(int level, int debug_level, const char *header, const NODE *node)
Definition: debug.c:125
FMODE_DUPLEX
#define FMODE_DUPLEX
Definition: io.h:114
RIMemo
Definition: internal.h:1124
vm_debug.h
vfprintf
int int int int int int vfprintf(FILE *__restrict, const char *__restrict, __gnuc_va_list) __attribute__((__format__(__printf__
ruby.h
FMODE_BINMODE
#define FMODE_BINMODE
Definition: io.h:111
ruby_on_ci
int ruby_on_ci
Definition: vm_dump.c:44
ruby_initial_gc_stress_ptr
VALUE * ruby_initial_gc_stress_ptr
Definition: gc.c:905
v
int VALUE v
Definition: rb_mjit_min_header-2.7.0.h:12332
FMODE_SYNC
#define FMODE_SYNC
Definition: io.h:112
obj
const VALUE VALUE obj
Definition: rb_mjit_min_header-2.7.0.h:5742
imemo_type
imemo_type
Definition: internal.h:1132
FMODE_READABLE
#define FMODE_READABLE
Definition: io.h:108
SET_WHEN
#define SET_WHEN(name, var, val)
fprintf
int fprintf(FILE *__restrict, const char *__restrict,...) __attribute__((__format__(__printf__
various
enum @11::@12 various
ruby_debug_print_value
VALUE ruby_debug_print_value(int level, int debug_level, const char *header, VALUE obj)
Definition: debug.c:96
encoding_consts
enum ruby_encoding_consts encoding_consts
Definition: debug.c:40
node_type
enum node_type node_type
Definition: debug.c:36
FMODE_TEXTMODE
#define FMODE_TEXTMODE
Definition: io.h:120
NODE_TYPESHIFT
#define NODE_TYPESHIFT
Definition: node.h:185
mask
enum @11::@13::@14 mask
ruby_robject_flags
ruby_robject_flags
Definition: ruby.h:915
NODE_TYPEMASK
#define NODE_TYPEMASK
Definition: node.h:186
FMODE_SETENC_BY_BOM
#define FMODE_SETENC_BY_BOM
Definition: io.h:122
va_end
#define va_end(v)
Definition: rb_mjit_min_header-2.7.0.h:3979
vm_core.h
stderr
#define stderr
Definition: rb_mjit_min_header-2.7.0.h:1485
fflush
int fflush(FILE *)
ruby_rarray_flags
ruby_rarray_flags
Definition: ruby.h:1027
node_type
node_type
Definition: node.h:22
symbol.h
FMODE_READWRITE
#define FMODE_READWRITE
Definition: io.h:110
FALSE
#define FALSE
Definition: nkf.h:174
method_ids
enum ruby_method_ids method_ids
Definition: debug.c:37
ruby_econv_flag_type
ruby_econv_flag_type
Definition: encoding.h:361
NODE_LMASK
#define NODE_LMASK
Definition: node.h:193
ruby_debug_printf
void ruby_debug_printf(const char *format,...)
Definition: debug.c:85
ruby_debug_print_indent
int ruby_debug_print_indent(int level, int debug_level, int indent_level)
Definition: debug.c:74
NODE_LSHIFT
#define NODE_LSHIFT
Definition: node.h:192
n
const char size_t n
Definition: rb_mjit_min_header-2.7.0.h:5456
arg
VALUE arg
Definition: rb_mjit_min_header-2.7.0.h:5601
FMODE_APPEND
#define FMODE_APPEND
Definition: io.h:115
RSymbol
Definition: symbol.h:26
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
fl_types
enum ruby_fl_type fl_types
Definition: debug.c:39
types
enum imemo_type types
Definition: debug.c:72
FMODE_CREATE
#define FMODE_CREATE
Definition: io.h:116
ruby_env_debug_option
int ruby_env_debug_option(const char *str, int len, void *arg)
Definition: debug.c:153
rarray_flags
enum ruby_rarray_flags rarray_flags
Definition: debug.c:46
io.h
ruby_enable_coredump
int ruby_enable_coredump
Definition: signal.c:1481
tag_type
enum ruby_tag_type tag_type
Definition: debug.c:35
enc_coderange_types
enum ruby_coderange_type enc_coderange_types
Definition: debug.c:41
ruby_coderange_type
ruby_coderange_type
Definition: encoding.h:66
ruby_debug_print_id
ID ruby_debug_print_id(int level, int debug_level, const char *header, ID id)
Definition: debug.c:115
Qtrue
#define Qtrue
Definition: ruby.h:468
FMODE_WRITABLE
#define FMODE_WRITABLE
Definition: io.h:109
len
uint8_t len
Definition: escape.c:17
SET_UINT_LIST
#define SET_UINT_LIST(name, vals, num)
symbol_ptr
struct RSymbol * symbol_ptr
Definition: debug.c:76
NAME_MATCH_VALUE
#define NAME_MATCH_VALUE(name)
va_start
#define va_start(v, l)
Definition: rb_mjit_min_header-2.7.0.h:3978
nd_line
#define nd_line(n)
Definition: node.h:194
vm_call_flags
enum vm_call_flag_bits vm_call_flags
Definition: debug.c:77
ruby_rgengc_debug
int ruby_rgengc_debug
Definition: gc.c:388
va_list
__gnuc_va_list va_list
Definition: rb_mjit_min_header-2.7.0.h:836
ruby_encoding_consts
ruby_encoding_consts
Definition: encoding.h:32
eval_intern.h
nd_type
#define nd_type(n)
Definition: node.h:188
econv_flag_types
enum ruby_econv_flag_type econv_flag_types
Definition: debug.c:42
util.h
ruby_dummy_gdb_enums
const union @11 ruby_dummy_gdb_enums
numberof
#define numberof(array)
Definition: etc.c:618
ruby_debug_print_v
void ruby_debug_print_v(VALUE v)
Definition: debug.c:109
rstring_flags
enum ruby_rstring_flags rstring_flags
Definition: debug.c:45
ruby_set_debug_option
void ruby_set_debug_option(const char *str)
Definition: debug.c:226
ruby_special_consts
ruby_special_consts
Definition: ruby.h:436
ruby_tag_type
ruby_tag_type
Definition: vm_core.h:184
ruby_each_words
void ruby_each_words(const char *, void(*)(const char *, int, void *), void *)
Definition: util.c:585
RNode
Definition: node.h:149
ruby_fl_type
ruby_fl_type
Definition: ruby.h:841
robject_flags
enum ruby_robject_flags robject_flags
Definition: debug.c:43