-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathruby_heap_obj.cc
245 lines (229 loc) · 7 KB
/
ruby_heap_obj.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <inttypes.h>
#include "ruby_heap_obj.h"
#include "graph.h"
namespace harb {
RubyHeapObj::RubyHeapObj(Graph *graph, RubyValueType t, int32_t idx)
: flags(t), idx(idx), graph(graph) {
refs_to.addr = NULL;
as.obj.clazz.addr = 0;
if (t == RUBY_T_ROOT) {
as.root.children = new RubyHeapObjList();
}
}
RubyValueType RubyHeapObj::get_value_type(const char *type) {
assert(type);
if (strcmp(type, "OBJECT") == 0) {
return RUBY_T_OBJECT;
} else if (strcmp(type, "STRING") == 0) {
return RUBY_T_STRING;
} else if (strcmp(type, "HASH") == 0) {
return RUBY_T_HASH;
} else if (strcmp(type, "ARRAY") == 0) {
return RUBY_T_ARRAY;
} else if (strcmp(type, "CLASS") == 0) {
return RUBY_T_CLASS;
} else if (strcmp(type, "ICLASS") == 0) {
return RUBY_T_ICLASS;
} else if (strcmp(type, "DATA") == 0) {
return RUBY_T_DATA;
} else if (strcmp(type, "MODULE") == 0) {
return RUBY_T_MODULE;
} else if (strcmp(type, "STRUCT") == 0) {
return RUBY_T_STRUCT;
} else if (strcmp(type, "NODE") == 0) {
return RUBY_T_NODE;
} else if (strcmp(type, "REGEXP") == 0) {
return RUBY_T_REGEXP;
} else if (strcmp(type, "BIGNUM") == 0) {
return RUBY_T_BIGNUM;
} else if (strcmp(type, "FLOAT") == 0) {
return RUBY_T_FLOAT;
} else if (strcmp(type, "FILE") == 0) {
return RUBY_T_FILE;
} else if (strcmp(type, "MATCH") == 0) {
return RUBY_T_MATCH;
} else if (strcmp(type, "COMPLEX") == 0) {
return RUBY_T_COMPLEX;
} else if (strcmp(type, "RATIONAL") == 0) {
return RUBY_T_RATIONAL;
} else if (strcmp(type, "NIL") == 0) {
return RUBY_T_NIL;
} else if (strcmp(type, "TRUE") == 0) {
return RUBY_T_TRUE;
} else if (strcmp(type, "FALSE") == 0) {
return RUBY_T_FALSE;
} else if (strcmp(type, "SYMBOL") == 0) {
return RUBY_T_SYMBOL;
} else if (strcmp(type, "FIXNUM") == 0) {
return RUBY_T_FIXNUM;
} else if (strcmp(type, "UNDEF") == 0) {
return RUBY_T_UNDEF;
} else if (strcmp(type, "ZOMBIE") == 0) {
return RUBY_T_ZOMBIE;
} else if (strcmp(type, "MOVED") == 0) {
return RUBY_T_MOVED;
} else if (strcmp(type, "ROOT") == 0) {
return RUBY_T_ROOT;
} else if (strcmp(type, "IMEMO") == 0) {
return RUBY_T_IMEMO;
}
return RUBY_T_NONE;
}
const char * RubyHeapObj::get_value_type_string(uint32_t type) {
uint32_t t = type & RUBY_T_MASK;
if (t == RUBY_T_OBJECT) {
return "OBJECT";
} else if (t == RUBY_T_STRING) {
return "STRING";
} else if (t == RUBY_T_HASH) {
return "HASH";
} else if (t == RUBY_T_ARRAY) {
return "ARRAY";
} else if (t == RUBY_T_CLASS) {
return "CLASS";
} else if (t == RUBY_T_ICLASS) {
return "ICLASS";
} else if (t == RUBY_T_DATA) {
return "DATA";
} else if (t == RUBY_T_MODULE) {
return "MODULE";
} else if (t == RUBY_T_STRUCT) {
return "STRUCT";
} else if (t == RUBY_T_NODE) {
return "NODE";
} else if (t == RUBY_T_REGEXP) {
return "REGEXP";
} else if (t == RUBY_T_BIGNUM) {
return "BIGNUM";
} else if (t == RUBY_T_FLOAT) {
return "FLOAT";
} else if (t == RUBY_T_FILE) {
return "FILE";
} else if (t == RUBY_T_MATCH) {
return "MATCH";
} else if (t == RUBY_T_COMPLEX) {
return "COMPLEX";
} else if (t == RUBY_T_RATIONAL) {
return "RATIONAL";
} else if (t == RUBY_T_NIL) {
return "NIL";
} else if (t == RUBY_T_TRUE) {
return "TRUE";
} else if (t == RUBY_T_FALSE) {
return "FALSE";
} else if (t == RUBY_T_SYMBOL) {
return "SYMBOL";
} else if (t == RUBY_T_FIXNUM) {
return "FIXNUM";
} else if (t == RUBY_T_UNDEF) {
return "UNDEF";
} else if (t == RUBY_T_ZOMBIE) {
return "ZOMBIE";
} else if (t == RUBY_T_MOVED) {
return "MOVED";
} else if (t == RUBY_T_ROOT) {
return "ROOT";
} else if (t == RUBY_T_IMEMO) {
return "IMEMO";
}
return "NONE";
}
const char * RubyHeapObj::get_object_summary(char *buf, size_t buf_sz) {
uint32_t type = flags & RUBY_T_MASK;
if (type == RUBY_T_ROOT) {
return "ROOT";
}
char value_buf[128];
const char *value_bufp = value_buf;
if (type == RUBY_T_ARRAY || type == RUBY_T_HASH) {
sprintf(value_buf, "size %d", get_size());
} else if (type == RUBY_T_OBJECT || type == RUBY_T_ICLASS) {
value_bufp = get_class_obj()->get_value();
} else if (type == RUBY_T_STRING && flags & RUBY_FL_SHARED) {
value_bufp = get_refs_to(0)->get_value();
} else {
value_bufp = get_value();
}
int ret = snprintf(buf, buf_sz, "%s: %s%s%s", get_value_type_string(flags),
type == RUBY_T_STRING ? "\"" : "",
value_bufp,
type == RUBY_T_STRING ? "\"" : "");
if (ret < 0) {
return NULL;
}
if ((unsigned) ret >= buf_sz && buf_sz > 3) {
buf[buf_sz - 2] = '.';
buf[buf_sz - 3] = '.';
buf[buf_sz - 4] = '.';
}
return buf;
}
void RubyHeapObj::print_ref_object(FILE *out) {
char buf[64];
if (is_root_object()) {
fprintf(out, "%20s ROOT (%s)\n", "", get_root_name());
} else {
fprintf(out, "%20s 0x%" PRIx64 " (%s)\n", "", get_addr(), get_object_summary(buf, sizeof(buf)));
}
}
void RubyHeapObj::print_object(FILE *out) {
uint32_t type = flags & RUBY_T_MASK;
if (type == RUBY_T_ROOT) {
fprintf(out, "ROOT (%s)\n", get_root_name());
} else {
char buf[64] = { 0 };
const char *p = buf;
const char *name_title = NULL;
sprintf(buf, "0x%" PRIx64, get_addr());
fprintf(out, "%18s: \"%s\"\n", buf, get_value_type_string(flags));
if (type == RUBY_T_DATA) {
name_title = "struct";
p = get_value();
} else if (type == RUBY_T_OBJECT || type == RUBY_T_ICLASS) {
name_title = type == RUBY_T_OBJECT ? "class" : "name";
p = get_class_obj()->get_value();
} else if (type == RUBY_T_STRING || type == RUBY_T_SYMBOL) {
name_title = "value";
p = get_value();
} else if (type == RUBY_T_CLASS || type == RUBY_T_MODULE) {
name_title = "name";
p = get_value();
} else if (type == RUBY_T_HASH || type == RUBY_T_ARRAY) {
name_title = "length";
sprintf(buf, "%'d", get_size());
p = buf;
} else if (type == RUBY_T_IMEMO) {
name_title = "imemo_type";
p = get_value();
}
if (name_title) {
fprintf(out, "%18s: %s%s%s\n", name_title,
type == RUBY_T_STRING ? "\"" : "",
p,
type == RUBY_T_STRING ? "\"" : "");
}
fprintf(out, "%18s: %'zu\n", "memsize", get_memsize());
fprintf(out, "%18s: %'zu\n", "retained memsize", graph->get_retained_size(this));
if (flags & RUBY_FL_SHARED) {
fprintf(out, "%18s: %s\n", "shared", "true");
}
if (flags & RUBY_FL_FROZEN) {
fprintf(out, "%18s: %s\n", "frozen", "true");
}
if (has_refs_to()) {
fprintf(out, "%18s: [\n", "references to");
for (uint32_t i = 0; refs_to.obj[i]; ++i) {
refs_to.obj[i]->print_ref_object(out);
}
fprintf(out, "%18s ]\n", "");
}
if (refs_from.size() > 0) {
fprintf(out, "%18s: [\n", "referenced from");
for (auto it = refs_from.begin(); it != refs_from.end(); ++it) {
(*it)->print_ref_object(out);
}
fprintf(out, "%18s ]\n", "");
}
}
}
}