Ruby  2.5.0dev(2017-10-22revision60238)
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/util.h"
15 #include "vm_debug.h"
16 #include "eval_intern.h"
17 #include "vm_core.h"
18 #include "id.h"
19 
20 /* for gdb */
21 const union {
36  enum {
37  RUBY_FMODE_READABLE = FMODE_READABLE,
38  RUBY_FMODE_WRITABLE = FMODE_WRITABLE,
39  RUBY_FMODE_READWRITE = FMODE_READWRITE,
40  RUBY_FMODE_BINMODE = FMODE_BINMODE,
41  RUBY_FMODE_SYNC = FMODE_SYNC,
42  RUBY_FMODE_TTY = FMODE_TTY,
43  RUBY_FMODE_DUPLEX = FMODE_DUPLEX,
44  RUBY_FMODE_APPEND = FMODE_APPEND,
45  RUBY_FMODE_CREATE = FMODE_CREATE,
46  RUBY_FMODE_NOREVLOOKUP = 0x00000100,
47  RUBY_FMODE_TRUNC = FMODE_TRUNC,
48  RUBY_FMODE_TEXTMODE = FMODE_TEXTMODE,
49  RUBY_FMODE_PREP = 0x00010000,
50  RUBY_FMODE_SETENC_BY_BOM = FMODE_SETENC_BY_BOM,
51  RUBY_FMODE_UNIX = 0x00200000,
52  RUBY_FMODE_INET = 0x00400000,
53  RUBY_FMODE_INET6 = 0x00800000,
54 
55  RUBY_NODE_TYPESHIFT = NODE_TYPESHIFT,
56  RUBY_NODE_TYPEMASK = NODE_TYPEMASK,
57  RUBY_NODE_LSHIFT = NODE_LSHIFT,
58  RUBY_NODE_FL_NEWLINE = NODE_FL_NEWLINE
59  } various;
61 
63 
64 int
65 ruby_debug_print_indent(int level, int debug_level, int indent_level)
66 {
67  if (level < debug_level) {
68  fprintf(stderr, "%*s", indent_level, "");
69  fflush(stderr);
70  return TRUE;
71  }
72  return FALSE;
73 }
74 
75 void
76 ruby_debug_printf(const char *format, ...)
77 {
78  va_list ap;
79  va_start(ap, format);
80  vfprintf(stderr, format, ap);
81  va_end(ap);
82 }
83 
84 #include "gc.h"
85 
86 VALUE
87 ruby_debug_print_value(int level, int debug_level, const char *header, VALUE obj)
88 {
89  if (level < debug_level) {
90  char buff[0x100];
91  rb_raw_obj_info(buff, 0x100, obj);
92 
93  fprintf(stderr, "DBG> %s: %s\n", header, buff);
94  fflush(stderr);
95  }
96  return obj;
97 }
98 
99 void
101 {
102  ruby_debug_print_value(0, 1, "", v);
103 }
104 
105 ID
106 ruby_debug_print_id(int level, int debug_level, const char *header, ID id)
107 {
108  if (level < debug_level) {
109  fprintf(stderr, "DBG> %s: %s\n", header, rb_id2name(id));
110  fflush(stderr);
111  }
112  return id;
113 }
114 
115 NODE *
116 ruby_debug_print_node(int level, int debug_level, const char *header, const NODE *node)
117 {
118  if (level < debug_level) {
119  fprintf(stderr, "DBG> %s: %s (%u)\n", header,
120  ruby_node_name(nd_type(node)), nd_line(node));
121  }
122  return (NODE *)node;
123 }
124 
125 void
127 {
128  /* */
129 }
130 
131 #if defined _WIN32
132 # if RUBY_MSVCRT_VERSION >= 80
133 extern int ruby_w32_rtc_error;
134 # endif
135 #endif
136 #if defined _WIN32 || defined __CYGWIN__
137 #include <windows.h>
138 UINT ruby_w32_codepage[2];
139 #endif
140 extern int ruby_rgengc_debug;
141 
142 int
143 ruby_env_debug_option(const char *str, int len, void *arg)
144 {
145  int ov;
146  size_t retlen;
147  unsigned long n;
148 #define SET_WHEN(name, var, val) do { \
149  if (len == sizeof(name) - 1 && \
150  strncmp(str, (name), len) == 0) { \
151  (var) = (val); \
152  return 1; \
153  } \
154  } while (0)
155 #define NAME_MATCH_VALUE(name) \
156  ((size_t)len >= sizeof(name)-1 && \
157  strncmp(str, (name), sizeof(name)-1) == 0 && \
158  ((len == sizeof(name)-1 && !(len = 0)) || \
159  (str[sizeof(name)-1] == '=' && \
160  (str += sizeof(name), len -= sizeof(name), 1))))
161 #define SET_UINT(val) do { \
162  n = ruby_scan_digits(str, len, 10, &retlen, &ov); \
163  if (!ov && retlen) { \
164  val = (unsigned int)n; \
165  } \
166  str += retlen; \
167  len -= retlen; \
168  } while (0)
169 #define SET_UINT_LIST(name, vals, num) do { \
170  int i; \
171  for (i = 0; i < (num); ++i) { \
172  SET_UINT((vals)[i]); \
173  if (!len || *str != ':') break; \
174  ++str; \
175  --len; \
176  } \
177  if (len > 0) { \
178  fprintf(stderr, "ignored "name" option: `%.*s'\n", len, str); \
179  } \
180  } while (0)
181 #define SET_WHEN_UINT(name, vals, num, req) \
182  if (NAME_MATCH_VALUE(name)) SET_UINT_LIST(name, vals, num);
183 
184  SET_WHEN("gc_stress", *ruby_initial_gc_stress_ptr, Qtrue);
185  SET_WHEN("core", ruby_enable_coredump, 1);
186  if (NAME_MATCH_VALUE("rgengc")) {
187  if (!len) ruby_rgengc_debug = 1;
188  else SET_UINT_LIST("rgengc", &ruby_rgengc_debug, 1);
189  return 1;
190  }
191 #if defined _WIN32
192 # if RUBY_MSVCRT_VERSION >= 80
193  SET_WHEN("rtc_error", ruby_w32_rtc_error, 1);
194 # endif
195 #endif
196 #if defined _WIN32 || defined __CYGWIN__
197  if (NAME_MATCH_VALUE("codepage")) {
198  if (!len) fprintf(stderr, "missing codepage argument");
199  else SET_UINT_LIST("codepage", ruby_w32_codepage, numberof(ruby_w32_codepage));
200  return 1;
201  }
202 #endif
203  return 0;
204 }
205 
206 static void
207 set_debug_option(const char *str, int len, void *arg)
208 {
209  if (!ruby_env_debug_option(str, len, arg)) {
210  fprintf(stderr, "unexpected debug option: %.*s\n", len, str);
211  }
212 }
213 
214 void
215 ruby_set_debug_option(const char *str)
216 {
217  ruby_each_words(str, set_debug_option, 0);
218 }
VALUE ruby_debug_print_value(int level, int debug_level, const char *header, VALUE obj)
Definition: debug.c:87
enum ruby_robject_flags robject_flags
Definition: debug.c:32
#define FALSE
Definition: nkf.h:174
ruby_tag_type
Definition: vm_core.h:151
#define FMODE_READWRITE
Definition: io.h:104
#define NODE_LSHIFT
Definition: node.h:276
#define NAME_MATCH_VALUE(name)
enum ruby_method_ids method_ids
Definition: debug.c:26
enum ruby_econv_flag_type econv_flag_types
Definition: debug.c:31
ruby_econv_flag_type
Definition: encoding.h:355
#define nd_line(n)
Definition: node.h:278
void ruby_each_words(const char *, void(*)(const char *, int, void *), void *)
Definition: util.c:3822
#define Qtrue
Definition: ruby.h:437
#define SET_WHEN(name, var, val)
const int id
Definition: nkf.c:209
#define FMODE_WRITABLE
Definition: io.h:103
node_type
Definition: node.h:22
ruby_fl_type
Definition: ruby.h:810
#define FMODE_READABLE
Definition: io.h:102
enum ruby_tag_type tag_type
Definition: debug.c:24
void ruby_debug_printf(const char *format,...)
Definition: debug.c:76
int ruby_debug_print_indent(int level, int debug_level, int indent_level)
Definition: debug.c:65
#define FMODE_CREATE
Definition: io.h:110
#define FMODE_DUPLEX
Definition: io.h:108
#define FMODE_BINMODE
Definition: io.h:105
#define FMODE_APPEND
Definition: io.h:109
ruby_rmodule_flags
Definition: ruby.h:921
#define nd_type(n)
Definition: node.h:272
enum ruby_id_types id_types
Definition: debug.c:27
ruby_coderange_type
Definition: encoding.h:62
Definition: node.h:233
enum node_type node_type
Definition: debug.c:25
#define level
#define FMODE_SETENC_BY_BOM
Definition: io.h:115
const SIGNED_VALUE RUBY_NODE_LMASK
Definition: debug.c:62
#define NODE_LMASK
Definition: node.h:277
ruby_id_types
Definition: id.h:16
enum ruby_rstring_flags rstring_flags
Definition: debug.c:34
enum ruby_rarray_flags rarray_flags
Definition: debug.c:35
#define FMODE_TRUNC
Definition: io.h:112
ruby_special_consts
Definition: ruby.h:405
int ruby_rgengc_debug
Definition: gc.c:235
#define numberof(array)
Definition: etc.c:618
const union @4 ruby_dummy_gdb_enums
#define SET_UINT_LIST(name, vals, num)
#define TRUE
Definition: nkf.h:175
VALUE * ruby_initial_gc_stress_ptr
Definition: gc.c:738
unsigned long ID
Definition: ruby.h:86
#define FMODE_TEXTMODE
Definition: io.h:113
#define NODE_FL_NEWLINE
Definition: node.h:267
unsigned long VALUE
Definition: ruby.h:85
enum @4::@5 various
ruby_rstring_flags
Definition: ruby.h:944
const char * rb_id2name(ID)
Definition: symbol.c:751
enum ruby_encoding_consts encoding_consts
Definition: debug.c:29
ruby_value_type
Definition: ruby.h:455
void ruby_set_debug_option(const char *str)
Definition: debug.c:215
register unsigned int len
Definition: zonetab.h:51
int ruby_enable_coredump
Definition: signal.c:1449
#define NODE_TYPESHIFT
Definition: node.h:269
enum ruby_coderange_type enc_coderange_types
Definition: debug.c:30
ruby_encoding_consts
Definition: encoding.h:28
NODE * ruby_debug_print_node(int level, int debug_level, const char *header, const NODE *node)
Definition: debug.c:116
const char * ruby_node_name(int node)
Definition: iseq.c:1767
enum ruby_special_consts special_consts
Definition: debug.c:22
enum ruby_value_type value_type
Definition: debug.c:23
ruby_robject_flags
Definition: ruby.h:882
enum ruby_rmodule_flags rmodule_flags
Definition: debug.c:33
int ruby_env_debug_option(const char *str, int len, void *arg)
Definition: debug.c:143
void ruby_debug_print_v(VALUE v)
Definition: debug.c:100
ruby_method_ids
Definition: id.h:76
#define FMODE_SYNC
Definition: io.h:106
#define FMODE_TTY
Definition: io.h:107
const char * rb_raw_obj_info(char *buff, const int buff_size, VALUE obj)
Definition: gc.c:9290
void ruby_debug_breakpoint(void)
Definition: debug.c:126
#define NODE_TYPEMASK
Definition: node.h:270
ruby_rarray_flags
Definition: ruby.h:989
enum ruby_fl_type fl_types
Definition: debug.c:28
ID ruby_debug_print_id(int level, int debug_level, const char *header, ID id)
Definition: debug.c:106
#define SIGNED_VALUE
Definition: ruby.h:87