2013-11-08 12:06:55 -05:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
objspace_dump.c - Heap dumping ObjectSpace extender for MRI.
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Sat Oct 11 10:11:00 2013
|
|
|
|
|
|
|
|
NOTE: This extension library is not expected to exist except C Ruby.
|
|
|
|
|
|
|
|
All the files in this distribution are covered under the Ruby's
|
|
|
|
license (see the file COPYING).
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#include "gc.h"
|
2019-12-04 03:16:30 -05:00
|
|
|
#include "internal.h"
|
|
|
|
#include "internal/hash.h"
|
|
|
|
#include "internal/string.h"
|
2020-09-25 17:50:54 -04:00
|
|
|
#include "internal/sanitizers.h"
|
2013-11-08 12:06:55 -05:00
|
|
|
#include "node.h"
|
|
|
|
#include "objspace.h"
|
2019-12-04 03:16:30 -05:00
|
|
|
#include "ruby/debug.h"
|
2020-08-18 03:41:38 -04:00
|
|
|
#include "ruby/util.h"
|
2019-12-04 03:16:30 -05:00
|
|
|
#include "ruby/io.h"
|
|
|
|
#include "vm_core.h"
|
2013-11-08 12:06:55 -05:00
|
|
|
|
2020-08-18 03:41:38 -04:00
|
|
|
RUBY_EXTERN const char ruby_hexdigits[];
|
|
|
|
|
|
|
|
#define BUFFER_CAPACITY 4096
|
|
|
|
|
2013-11-08 12:06:55 -05:00
|
|
|
struct dump_config {
|
|
|
|
VALUE type;
|
2020-08-18 03:41:38 -04:00
|
|
|
VALUE stream;
|
2013-11-08 12:06:55 -05:00
|
|
|
VALUE string;
|
|
|
|
const char *root_category;
|
|
|
|
VALUE cur_obj;
|
|
|
|
VALUE cur_obj_klass;
|
|
|
|
size_t cur_obj_references;
|
2019-07-02 19:29:33 -04:00
|
|
|
unsigned int roots: 1;
|
|
|
|
unsigned int full_heap: 1;
|
2020-07-28 06:58:49 -04:00
|
|
|
unsigned int partial_dump;
|
|
|
|
size_t since;
|
2020-08-18 03:41:38 -04:00
|
|
|
unsigned long buffer_len;
|
|
|
|
char buffer[BUFFER_CAPACITY];
|
2013-11-08 12:06:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_flush(struct dump_config *dc)
|
|
|
|
{
|
|
|
|
if (dc->buffer_len) {
|
|
|
|
if (dc->stream) {
|
|
|
|
size_t written = rb_io_bufwrite(dc->stream, dc->buffer, dc->buffer_len);
|
|
|
|
if (written < dc->buffer_len) {
|
|
|
|
MEMMOVE(dc->buffer, dc->buffer + written, char, dc->buffer_len - written);
|
|
|
|
dc->buffer_len -= written;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (dc->string) {
|
|
|
|
rb_str_cat(dc->string, dc->buffer, dc->buffer_len);
|
|
|
|
}
|
|
|
|
dc->buffer_len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
buffer_ensure_capa(struct dump_config *dc, unsigned long requested)
|
|
|
|
{
|
|
|
|
RUBY_ASSERT(requested <= BUFFER_CAPACITY);
|
|
|
|
if (requested + dc->buffer_len >= BUFFER_CAPACITY) {
|
|
|
|
dump_flush(dc);
|
|
|
|
if (requested + dc->buffer_len >= BUFFER_CAPACITY) {
|
|
|
|
rb_raise(rb_eIOError, "full buffer");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void buffer_append(struct dump_config *dc, const char *cstr, unsigned long len)
|
|
|
|
{
|
|
|
|
if (LIKELY(len > 0)) {
|
|
|
|
buffer_ensure_capa(dc, len);
|
|
|
|
MEMCPY(dc->buffer + dc->buffer_len, cstr, char, len);
|
|
|
|
dc->buffer_len += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# define dump_append(dc, str) buffer_append(dc, (str), (long)strlen(str))
|
|
|
|
|
|
|
|
static void
|
|
|
|
dump_append_ld(struct dump_config *dc, const long number)
|
|
|
|
{
|
2021-04-26 15:14:57 -04:00
|
|
|
const unsigned int width = DECIMAL_SIZE_OF_BITS(sizeof(number) * CHAR_BIT - 1) + 2;
|
2020-08-18 03:41:38 -04:00
|
|
|
buffer_ensure_capa(dc, width);
|
|
|
|
unsigned long required = snprintf(dc->buffer + dc->buffer_len, width, "%ld", number);
|
|
|
|
RUBY_ASSERT(required <= width);
|
|
|
|
dc->buffer_len += required;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dump_append_lu(struct dump_config *dc, const unsigned long number)
|
|
|
|
{
|
2021-04-26 15:14:57 -04:00
|
|
|
const unsigned int width = DECIMAL_SIZE_OF_BITS(sizeof(number) * CHAR_BIT) + 1;
|
2020-08-18 03:41:38 -04:00
|
|
|
buffer_ensure_capa(dc, width);
|
|
|
|
unsigned long required = snprintf(dc->buffer + dc->buffer_len, width, "%lu", number);
|
|
|
|
RUBY_ASSERT(required <= width);
|
|
|
|
dc->buffer_len += required;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dump_append_g(struct dump_config *dc, const double number)
|
2013-11-08 12:06:55 -05:00
|
|
|
{
|
2020-08-18 03:41:38 -04:00
|
|
|
unsigned long capa_left = BUFFER_CAPACITY - dc->buffer_len;
|
|
|
|
unsigned long required = snprintf(dc->buffer + dc->buffer_len, capa_left, "%#g", number);
|
2013-11-08 12:06:55 -05:00
|
|
|
|
2020-08-18 03:41:38 -04:00
|
|
|
if (required >= capa_left) {
|
|
|
|
buffer_ensure_capa(dc, required);
|
|
|
|
capa_left = BUFFER_CAPACITY - dc->buffer_len;
|
|
|
|
snprintf(dc->buffer + dc->buffer_len, capa_left, "%#g", number);
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
2020-08-18 03:41:38 -04:00
|
|
|
dc->buffer_len += required;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dump_append_d(struct dump_config *dc, const int number)
|
|
|
|
{
|
2021-04-26 15:14:57 -04:00
|
|
|
const unsigned int width = DECIMAL_SIZE_OF_BITS(sizeof(number) * CHAR_BIT - 1) + 2;
|
2020-08-18 03:41:38 -04:00
|
|
|
buffer_ensure_capa(dc, width);
|
|
|
|
unsigned long required = snprintf(dc->buffer + dc->buffer_len, width, "%d", number);
|
|
|
|
RUBY_ASSERT(required <= width);
|
|
|
|
dc->buffer_len += required;
|
|
|
|
}
|
2013-11-08 12:06:55 -05:00
|
|
|
|
2020-08-18 03:41:38 -04:00
|
|
|
static void
|
|
|
|
dump_append_sizet(struct dump_config *dc, const size_t number)
|
|
|
|
{
|
2021-04-26 15:14:57 -04:00
|
|
|
const unsigned int width = DECIMAL_SIZE_OF_BITS(sizeof(number) * CHAR_BIT) + 1;
|
2020-08-18 03:41:38 -04:00
|
|
|
buffer_ensure_capa(dc, width);
|
|
|
|
unsigned long required = snprintf(dc->buffer + dc->buffer_len, width, "%"PRIuSIZE, number);
|
|
|
|
RUBY_ASSERT(required <= width);
|
|
|
|
dc->buffer_len += required;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dump_append_c(struct dump_config *dc, char c)
|
|
|
|
{
|
|
|
|
if (c <= 0x1f) {
|
2021-04-26 15:14:57 -04:00
|
|
|
const unsigned int width = (sizeof(c) * CHAR_BIT / 4) + 5;
|
2020-08-18 03:41:38 -04:00
|
|
|
buffer_ensure_capa(dc, width);
|
|
|
|
unsigned long required = snprintf(dc->buffer + dc->buffer_len, width, "\\u00%02x", c);
|
|
|
|
RUBY_ASSERT(required <= width);
|
|
|
|
dc->buffer_len += required;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffer_ensure_capa(dc, 1);
|
|
|
|
dc->buffer[dc->buffer_len] = c;
|
|
|
|
dc->buffer_len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dump_append_ref(struct dump_config *dc, VALUE ref)
|
|
|
|
{
|
|
|
|
RUBY_ASSERT(ref > 0);
|
|
|
|
|
|
|
|
char buffer[((sizeof(VALUE) * CHAR_BIT + 3) / 4) + 4];
|
|
|
|
char *buffer_start, *buffer_end;
|
|
|
|
|
|
|
|
buffer_start = buffer_end = &buffer[sizeof(buffer)];
|
|
|
|
*--buffer_start = '"';
|
|
|
|
while (ref) {
|
|
|
|
*--buffer_start = ruby_hexdigits[ref & 0xF];
|
|
|
|
ref >>= 4;
|
|
|
|
}
|
|
|
|
*--buffer_start = 'x';
|
|
|
|
*--buffer_start = '0';
|
|
|
|
*--buffer_start = '"';
|
|
|
|
buffer_append(dc, buffer_start, buffer_end - buffer_start);
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dump_append_string_value(struct dump_config *dc, VALUE obj)
|
|
|
|
{
|
2015-10-19 02:25:01 -04:00
|
|
|
long i;
|
|
|
|
char c;
|
|
|
|
const char *value;
|
2013-11-08 12:06:55 -05:00
|
|
|
|
|
|
|
dump_append(dc, "\"");
|
|
|
|
for (i = 0, value = RSTRING_PTR(obj); i < RSTRING_LEN(obj); i++) {
|
2020-08-18 03:41:38 -04:00
|
|
|
switch ((c = value[i])) {
|
|
|
|
case '\\':
|
|
|
|
dump_append(dc, "\\\\");
|
2020-09-10 22:01:30 -04:00
|
|
|
break;
|
2020-08-18 03:41:38 -04:00
|
|
|
case '"':
|
|
|
|
dump_append(dc, "\\\"");
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
dump_append(dc, "\\u0000");
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
dump_append(dc, "\\b");
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
dump_append(dc, "\\t");
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
dump_append(dc, "\\f");
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
dump_append(dc, "\\n");
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
dump_append(dc, "\\r");
|
|
|
|
break;
|
|
|
|
case '\177':
|
|
|
|
dump_append(dc, "\\u007f");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dump_append_c(dc, c);
|
|
|
|
}
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
dump_append(dc, "\"");
|
|
|
|
}
|
|
|
|
|
2015-10-19 02:25:01 -04:00
|
|
|
static void
|
|
|
|
dump_append_symbol_value(struct dump_config *dc, VALUE obj)
|
|
|
|
{
|
|
|
|
dump_append(dc, "{\"type\":\"SYMBOL\", \"value\":");
|
|
|
|
dump_append_string_value(dc, rb_sym2str(obj));
|
|
|
|
dump_append(dc, "}");
|
|
|
|
}
|
|
|
|
|
2013-11-08 12:06:55 -05:00
|
|
|
static inline const char *
|
|
|
|
obj_type(VALUE obj)
|
|
|
|
{
|
|
|
|
switch (BUILTIN_TYPE(obj)) {
|
2017-09-27 01:27:06 -04:00
|
|
|
#define CASE_TYPE(type) case T_##type: return #type
|
2013-11-08 12:06:55 -05:00
|
|
|
CASE_TYPE(NONE);
|
|
|
|
CASE_TYPE(NIL);
|
|
|
|
CASE_TYPE(OBJECT);
|
|
|
|
CASE_TYPE(CLASS);
|
|
|
|
CASE_TYPE(ICLASS);
|
|
|
|
CASE_TYPE(MODULE);
|
|
|
|
CASE_TYPE(FLOAT);
|
|
|
|
CASE_TYPE(STRING);
|
|
|
|
CASE_TYPE(REGEXP);
|
|
|
|
CASE_TYPE(ARRAY);
|
|
|
|
CASE_TYPE(HASH);
|
|
|
|
CASE_TYPE(STRUCT);
|
|
|
|
CASE_TYPE(BIGNUM);
|
|
|
|
CASE_TYPE(FILE);
|
|
|
|
CASE_TYPE(FIXNUM);
|
|
|
|
CASE_TYPE(TRUE);
|
|
|
|
CASE_TYPE(FALSE);
|
|
|
|
CASE_TYPE(DATA);
|
|
|
|
CASE_TYPE(MATCH);
|
|
|
|
CASE_TYPE(SYMBOL);
|
|
|
|
CASE_TYPE(RATIONAL);
|
|
|
|
CASE_TYPE(COMPLEX);
|
2015-09-17 18:40:24 -04:00
|
|
|
CASE_TYPE(IMEMO);
|
2013-11-08 12:06:55 -05:00
|
|
|
CASE_TYPE(UNDEF);
|
|
|
|
CASE_TYPE(NODE);
|
|
|
|
CASE_TYPE(ZOMBIE);
|
|
|
|
#undef CASE_TYPE
|
2020-04-08 02:13:37 -04:00
|
|
|
default: break;
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
|
2015-10-19 02:25:01 -04:00
|
|
|
static void
|
|
|
|
dump_append_special_const(struct dump_config *dc, VALUE value)
|
|
|
|
{
|
|
|
|
if (value == Qtrue) {
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, "true");
|
2015-10-19 02:25:01 -04:00
|
|
|
}
|
|
|
|
else if (value == Qfalse) {
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, "false");
|
2015-10-19 02:25:01 -04:00
|
|
|
}
|
|
|
|
else if (value == Qnil) {
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, "null");
|
2015-10-19 02:25:01 -04:00
|
|
|
}
|
|
|
|
else if (FIXNUM_P(value)) {
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append_ld(dc, FIX2LONG(value));
|
2015-10-19 02:25:01 -04:00
|
|
|
}
|
|
|
|
else if (FLONUM_P(value)) {
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append_g(dc, RFLOAT_VALUE(value));
|
2015-10-19 02:25:01 -04:00
|
|
|
}
|
|
|
|
else if (SYMBOL_P(value)) {
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append_symbol_value(dc, value);
|
2015-10-19 02:25:01 -04:00
|
|
|
}
|
|
|
|
else {
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, "{}");
|
2015-10-19 02:25:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-08 12:06:55 -05:00
|
|
|
static void
|
|
|
|
reachable_object_i(VALUE ref, void *data)
|
|
|
|
{
|
|
|
|
struct dump_config *dc = (struct dump_config *)data;
|
|
|
|
|
|
|
|
if (dc->cur_obj_klass == ref)
|
2020-08-18 03:41:38 -04:00
|
|
|
return;
|
2013-11-08 12:06:55 -05:00
|
|
|
|
2020-08-18 03:41:38 -04:00
|
|
|
if (dc->cur_obj_references == 0) {
|
|
|
|
dump_append(dc, ", \"references\":[");
|
|
|
|
dump_append_ref(dc, ref);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dump_append(dc, ", ");
|
|
|
|
dump_append_ref(dc, ref);
|
|
|
|
}
|
2013-11-08 12:06:55 -05:00
|
|
|
|
|
|
|
dc->cur_obj_references++;
|
|
|
|
}
|
|
|
|
|
2015-10-19 02:25:01 -04:00
|
|
|
static void
|
|
|
|
dump_append_string_content(struct dump_config *dc, VALUE obj)
|
|
|
|
{
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, ", \"bytesize\":");
|
|
|
|
dump_append_ld(dc, RSTRING_LEN(obj));
|
|
|
|
if (!STR_EMBED_P(obj) && !STR_SHARED_P(obj) && (long)rb_str_capacity(obj) != RSTRING_LEN(obj)) {
|
|
|
|
dump_append(dc, ", \"capacity\":");
|
|
|
|
dump_append_sizet(dc, rb_str_capacity(obj));
|
|
|
|
}
|
2015-10-19 02:25:01 -04:00
|
|
|
|
|
|
|
if (is_ascii_string(obj)) {
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, ", \"value\":");
|
|
|
|
dump_append_string_value(dc, obj);
|
2015-10-19 02:25:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-08 12:06:55 -05:00
|
|
|
static void
|
2016-09-16 13:44:45 -04:00
|
|
|
dump_object(VALUE obj, struct dump_config *dc)
|
2013-11-08 12:06:55 -05:00
|
|
|
{
|
|
|
|
size_t memsize;
|
2020-07-28 06:58:49 -04:00
|
|
|
struct allocation_info *ainfo = objspace_lookup_allocation_info(obj);
|
2013-11-08 12:06:55 -05:00
|
|
|
rb_io_t *fptr;
|
2013-12-09 21:26:09 -05:00
|
|
|
ID flags[RB_OBJ_GC_FLAGS_MAX];
|
|
|
|
size_t n, i;
|
2013-11-08 12:06:55 -05:00
|
|
|
|
2015-06-20 16:50:20 -04:00
|
|
|
if (SPECIAL_CONST_P(obj)) {
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append_special_const(dc, obj);
|
|
|
|
return;
|
2015-06-20 16:50:20 -04:00
|
|
|
}
|
|
|
|
|
2013-11-08 12:06:55 -05:00
|
|
|
dc->cur_obj = obj;
|
|
|
|
dc->cur_obj_references = 0;
|
|
|
|
dc->cur_obj_klass = BUILTIN_TYPE(obj) == T_NODE ? 0 : RBASIC_CLASS(obj);
|
|
|
|
|
2020-07-28 06:58:49 -04:00
|
|
|
if (dc->partial_dump && (!ainfo || ainfo->generation < dc->since)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-08 12:06:55 -05:00
|
|
|
if (dc->cur_obj == dc->string)
|
2020-08-18 03:41:38 -04:00
|
|
|
return;
|
2013-11-08 12:06:55 -05:00
|
|
|
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, "{\"address\":");
|
|
|
|
dump_append_ref(dc, obj);
|
2013-11-08 12:06:55 -05:00
|
|
|
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, ", \"type\":\"");
|
|
|
|
dump_append(dc, obj_type(obj));
|
|
|
|
dump_append(dc, "\"");
|
|
|
|
|
|
|
|
if (dc->cur_obj_klass) {
|
|
|
|
dump_append(dc, ", \"class\":");
|
|
|
|
dump_append_ref(dc, dc->cur_obj_klass);
|
|
|
|
}
|
2013-11-08 12:06:55 -05:00
|
|
|
if (rb_obj_frozen_p(obj))
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, ", \"frozen\":true");
|
2013-11-08 12:06:55 -05:00
|
|
|
|
|
|
|
switch (BUILTIN_TYPE(obj)) {
|
2017-01-03 17:42:10 -05:00
|
|
|
case T_NONE:
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, "}\n");
|
|
|
|
return;
|
2017-01-03 17:42:10 -05:00
|
|
|
|
2017-01-31 12:46:51 -05:00
|
|
|
case T_IMEMO:
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, ", \"imemo_type\":\"");
|
|
|
|
dump_append(dc, rb_imemo_name(imemo_type(obj)));
|
|
|
|
dump_append(dc, "\"");
|
|
|
|
break;
|
2017-01-31 12:46:51 -05:00
|
|
|
|
2015-10-19 02:25:01 -04:00
|
|
|
case T_SYMBOL:
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append_string_content(dc, rb_sym2str(obj));
|
|
|
|
break;
|
2015-10-19 02:25:01 -04:00
|
|
|
|
2013-11-09 01:16:42 -05:00
|
|
|
case T_STRING:
|
2020-08-18 03:41:38 -04:00
|
|
|
if (STR_EMBED_P(obj))
|
|
|
|
dump_append(dc, ", \"embedded\":true");
|
|
|
|
if (is_broken_string(obj))
|
|
|
|
dump_append(dc, ", \"broken\":true");
|
|
|
|
if (FL_TEST(obj, RSTRING_FSTR))
|
|
|
|
dump_append(dc, ", \"fstring\":true");
|
|
|
|
if (STR_SHARED_P(obj))
|
|
|
|
dump_append(dc, ", \"shared\":true");
|
|
|
|
else
|
|
|
|
dump_append_string_content(dc, obj);
|
|
|
|
|
|
|
|
if (!ENCODING_IS_ASCII8BIT(obj)) {
|
|
|
|
dump_append(dc, ", \"encoding\":\"");
|
|
|
|
dump_append(dc, rb_enc_name(rb_enc_from_index(ENCODING_GET(obj))));
|
|
|
|
dump_append(dc, "\"");
|
|
|
|
}
|
|
|
|
break;
|
2013-11-09 01:16:42 -05:00
|
|
|
|
|
|
|
case T_HASH:
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, ", \"size\":");
|
|
|
|
dump_append_sizet(dc, (size_t)RHASH_SIZE(obj));
|
|
|
|
if (FL_TEST(obj, RHASH_PROC_DEFAULT)) {
|
|
|
|
dump_append(dc, ", \"default\":");
|
|
|
|
dump_append_ref(dc, RHASH_IFNONE(obj));
|
|
|
|
}
|
|
|
|
break;
|
2013-11-09 01:16:42 -05:00
|
|
|
|
|
|
|
case T_ARRAY:
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, ", \"length\":");
|
|
|
|
dump_append_ld(dc, RARRAY_LEN(obj));
|
|
|
|
if (RARRAY_LEN(obj) > 0 && FL_TEST(obj, ELTS_SHARED))
|
|
|
|
dump_append(dc, ", \"shared\":true");
|
|
|
|
if (RARRAY_LEN(obj) > 0 && FL_TEST(obj, RARRAY_EMBED_FLAG))
|
|
|
|
dump_append(dc, ", \"embedded\":true");
|
|
|
|
break;
|
2013-11-09 01:16:42 -05:00
|
|
|
|
2021-01-21 03:45:11 -05:00
|
|
|
case T_ICLASS:
|
|
|
|
if (rb_class_get_superclass(obj)) {
|
|
|
|
dump_append(dc, ", \"superclass\":");
|
|
|
|
dump_append_ref(dc, rb_class_get_superclass(obj));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-11-09 01:16:42 -05:00
|
|
|
case T_CLASS:
|
|
|
|
case T_MODULE:
|
2021-01-21 03:45:11 -05:00
|
|
|
if (rb_class_get_superclass(obj)) {
|
|
|
|
dump_append(dc, ", \"superclass\":");
|
|
|
|
dump_append_ref(dc, rb_class_get_superclass(obj));
|
|
|
|
}
|
|
|
|
|
2020-08-18 03:41:38 -04:00
|
|
|
if (dc->cur_obj_klass) {
|
|
|
|
VALUE mod_name = rb_mod_name(obj);
|
|
|
|
if (!NIL_P(mod_name)) {
|
|
|
|
dump_append(dc, ", \"name\":\"");
|
|
|
|
dump_append(dc, RSTRING_PTR(mod_name));
|
|
|
|
dump_append(dc, "\"");
|
2021-01-21 03:45:11 -05:00
|
|
|
} else {
|
|
|
|
VALUE real_mod_name = rb_mod_name(rb_class_real(obj));
|
|
|
|
if (RTEST(real_mod_name)) {
|
|
|
|
dump_append(dc, ", \"real_class_name\":\"");
|
|
|
|
dump_append(dc, RSTRING_PTR(real_mod_name));
|
|
|
|
dump_append(dc, "\"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FL_TEST(obj, FL_SINGLETON)) {
|
|
|
|
dump_append(dc, ", \"singleton\":true");
|
2020-08-18 03:41:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2013-11-09 01:16:42 -05:00
|
|
|
|
|
|
|
case T_DATA:
|
2020-08-18 03:41:38 -04:00
|
|
|
if (RTYPEDDATA_P(obj)) {
|
|
|
|
dump_append(dc, ", \"struct\":\"");
|
|
|
|
dump_append(dc, RTYPEDDATA_TYPE(obj)->wrap_struct_name);
|
|
|
|
dump_append(dc, "\"");
|
|
|
|
}
|
|
|
|
break;
|
2013-11-09 01:16:42 -05:00
|
|
|
|
|
|
|
case T_FLOAT:
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, ", \"value\":\"");
|
|
|
|
dump_append_g(dc, RFLOAT_VALUE(obj));
|
|
|
|
dump_append(dc, "\"");
|
|
|
|
break;
|
2013-11-09 01:16:42 -05:00
|
|
|
|
|
|
|
case T_OBJECT:
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, ", \"ivars\":");
|
|
|
|
dump_append_lu(dc, ROBJECT_NUMIV(obj));
|
|
|
|
break;
|
2013-11-09 01:16:42 -05:00
|
|
|
|
|
|
|
case T_FILE:
|
2020-08-18 03:41:38 -04:00
|
|
|
fptr = RFILE(obj)->fptr;
|
|
|
|
if (fptr) {
|
|
|
|
dump_append(dc, ", \"fd\":");
|
|
|
|
dump_append_d(dc, fptr->fd);
|
|
|
|
}
|
|
|
|
break;
|
2013-11-09 01:16:42 -05:00
|
|
|
|
|
|
|
case T_ZOMBIE:
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, "}\n");
|
|
|
|
return;
|
2020-04-08 02:13:37 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_objspace_reachable_objects_from(obj, reachable_object_i, dc);
|
|
|
|
if (dc->cur_obj_references > 0)
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, "]");
|
2013-11-08 12:06:55 -05:00
|
|
|
|
2020-07-28 06:58:49 -04:00
|
|
|
if (ainfo) {
|
2021-01-15 12:18:58 -05:00
|
|
|
if (ainfo->path) {
|
|
|
|
dump_append(dc, ", \"file\":\"");
|
|
|
|
dump_append(dc, ainfo->path);
|
|
|
|
dump_append(dc, "\"");
|
|
|
|
}
|
|
|
|
if (ainfo->line) {
|
|
|
|
dump_append(dc, ", \"line\":");
|
|
|
|
dump_append_lu(dc, ainfo->line);
|
|
|
|
}
|
2020-08-18 03:41:38 -04:00
|
|
|
if (RTEST(ainfo->mid)) {
|
|
|
|
VALUE m = rb_sym2str(ainfo->mid);
|
|
|
|
dump_append(dc, ", \"method\":");
|
|
|
|
dump_append_string_value(dc, m);
|
|
|
|
}
|
|
|
|
dump_append(dc, ", \"generation\":");
|
|
|
|
dump_append_sizet(dc, ainfo->generation);
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
|
2020-08-18 03:41:38 -04:00
|
|
|
if ((memsize = rb_obj_memsize_of(obj)) > 0) {
|
|
|
|
dump_append(dc, ", \"memsize\":");
|
|
|
|
dump_append_sizet(dc, memsize);
|
|
|
|
}
|
2013-11-08 12:06:55 -05:00
|
|
|
|
2013-12-09 21:26:09 -05:00
|
|
|
if ((n = rb_obj_gc_flags(obj, flags, sizeof(flags))) > 0) {
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, ", \"flags\":{");
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
dump_append(dc, "\"");
|
|
|
|
dump_append(dc, rb_id2name(flags[i]));
|
|
|
|
dump_append(dc, "\":true");
|
|
|
|
if (i != n-1) dump_append(dc, ", ");
|
|
|
|
}
|
|
|
|
dump_append(dc, "}");
|
2013-12-09 21:26:09 -05:00
|
|
|
}
|
|
|
|
|
2016-09-16 13:44:45 -04:00
|
|
|
dump_append(dc, "}\n");
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
heap_i(void *vstart, void *vend, size_t stride, void *data)
|
|
|
|
{
|
2017-01-03 17:42:10 -05:00
|
|
|
struct dump_config *dc = (struct dump_config *)data;
|
2013-11-08 12:06:55 -05:00
|
|
|
VALUE v = (VALUE)vstart;
|
|
|
|
for (; v != (VALUE)vend; v += stride) {
|
2020-09-25 17:50:54 -04:00
|
|
|
void *ptr = asan_poisoned_object_p(v);
|
|
|
|
asan_unpoison_object(v, false);
|
|
|
|
|
2017-01-03 17:42:10 -05:00
|
|
|
if (dc->full_heap || RBASIC(v)->flags)
|
|
|
|
dump_object(v, dc);
|
2020-09-25 17:50:54 -04:00
|
|
|
|
|
|
|
if (ptr) {
|
|
|
|
asan_poison_object(v);
|
|
|
|
}
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
root_obj_i(const char *category, VALUE obj, void *data)
|
|
|
|
{
|
|
|
|
struct dump_config *dc = (struct dump_config *)data;
|
|
|
|
|
|
|
|
if (dc->root_category != NULL && category != dc->root_category)
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_append(dc, "]}\n");
|
|
|
|
if (dc->root_category == NULL || category != dc->root_category) {
|
|
|
|
dump_append(dc, "{\"type\":\"ROOT\", \"root\":\"");
|
|
|
|
dump_append(dc, category);
|
|
|
|
dump_append(dc, "\", \"references\":[");
|
|
|
|
dump_append_ref(dc, obj);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dump_append(dc, ", ");
|
|
|
|
dump_append_ref(dc, obj);
|
|
|
|
}
|
2013-11-08 12:06:55 -05:00
|
|
|
|
|
|
|
dc->root_category = category;
|
2019-07-02 19:29:33 -04:00
|
|
|
dc->roots = 1;
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
|
2020-09-10 07:17:53 -04:00
|
|
|
static void
|
|
|
|
dump_output(struct dump_config *dc, VALUE output, VALUE full, VALUE since)
|
2013-11-09 01:16:53 -05:00
|
|
|
{
|
2013-11-14 11:06:09 -05:00
|
|
|
|
2017-01-03 17:42:10 -05:00
|
|
|
dc->full_heap = 0;
|
2020-08-18 03:41:38 -04:00
|
|
|
dc->buffer_len = 0;
|
2017-01-03 17:42:10 -05:00
|
|
|
|
2020-09-10 07:17:53 -04:00
|
|
|
if (TYPE(output) == T_STRING) {
|
|
|
|
dc->stream = Qfalse;
|
|
|
|
dc->string = output;
|
|
|
|
} else {
|
|
|
|
dc->stream = output;
|
|
|
|
dc->string = Qfalse;
|
2017-01-03 17:42:10 -05:00
|
|
|
}
|
|
|
|
|
2020-09-10 07:17:53 -04:00
|
|
|
if (full == Qtrue) {
|
|
|
|
dc->full_heap = 1;
|
2013-11-09 01:16:53 -05:00
|
|
|
}
|
2020-08-18 03:41:38 -04:00
|
|
|
|
2020-09-10 07:17:53 -04:00
|
|
|
if (RTEST(since)) {
|
|
|
|
dc->partial_dump = 1;
|
|
|
|
dc->since = NUM2SIZET(since);
|
|
|
|
} else {
|
|
|
|
dc->partial_dump = 0;
|
|
|
|
}
|
2013-11-09 01:16:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2020-09-10 07:17:53 -04:00
|
|
|
dump_result(struct dump_config *dc)
|
2013-11-09 01:16:53 -05:00
|
|
|
{
|
2020-08-18 03:41:38 -04:00
|
|
|
dump_flush(dc);
|
|
|
|
|
2020-09-10 07:17:53 -04:00
|
|
|
if (dc->string) {
|
|
|
|
return dc->string;
|
|
|
|
} else {
|
2020-08-18 03:41:38 -04:00
|
|
|
rb_io_flush(dc->stream);
|
|
|
|
return dc->stream;
|
2013-11-09 01:16:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-08 12:06:55 -05:00
|
|
|
static VALUE
|
2020-09-10 07:17:53 -04:00
|
|
|
objspace_dump(VALUE os, VALUE obj, VALUE output)
|
2013-11-08 12:06:55 -05:00
|
|
|
{
|
|
|
|
struct dump_config dc = {0,};
|
2020-09-10 07:17:53 -04:00
|
|
|
dump_output(&dc, output, Qnil, Qnil);
|
2013-11-08 12:06:55 -05:00
|
|
|
|
2016-09-16 13:44:45 -04:00
|
|
|
dump_object(obj, &dc);
|
2013-11-08 12:06:55 -05:00
|
|
|
|
2020-09-10 07:17:53 -04:00
|
|
|
return dump_result(&dc);
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2020-09-10 07:17:53 -04:00
|
|
|
objspace_dump_all(VALUE os, VALUE output, VALUE full, VALUE since)
|
2013-11-08 12:06:55 -05:00
|
|
|
{
|
|
|
|
struct dump_config dc = {0,};
|
2020-09-10 07:17:53 -04:00
|
|
|
dump_output(&dc, output, full, since);
|
2013-11-08 12:06:55 -05:00
|
|
|
|
2020-07-28 06:58:49 -04:00
|
|
|
if (!dc.partial_dump || dc.since == 0) {
|
|
|
|
/* dump roots */
|
|
|
|
rb_objspace_reachable_objects_from_root(root_obj_i, &dc);
|
|
|
|
if (dc.roots) dump_append(&dc, "]}\n");
|
|
|
|
}
|
2013-11-08 12:06:55 -05:00
|
|
|
|
|
|
|
/* dump all objects */
|
|
|
|
rb_objspace_each_objects(heap_i, &dc);
|
|
|
|
|
2020-09-10 07:17:53 -04:00
|
|
|
return dump_result(&dc);
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_objspace_dump(VALUE rb_mObjSpace)
|
|
|
|
{
|
2018-02-16 03:39:48 -05:00
|
|
|
#undef rb_intern
|
2013-11-08 12:06:55 -05:00
|
|
|
#if 0
|
|
|
|
rb_mObjSpace = rb_define_module("ObjectSpace"); /* let rdoc know */
|
|
|
|
#endif
|
|
|
|
|
2020-09-10 07:17:53 -04:00
|
|
|
rb_define_module_function(rb_mObjSpace, "_dump", objspace_dump, 2);
|
|
|
|
rb_define_module_function(rb_mObjSpace, "_dump_all", objspace_dump_all, 3);
|
2013-12-09 21:26:09 -05:00
|
|
|
|
|
|
|
/* force create static IDs */
|
|
|
|
rb_obj_gc_flags(rb_mObjSpace, 0, 0);
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|