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"
|
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"
|
|
|
|
#include "ruby/io.h"
|
|
|
|
#include "vm_core.h"
|
2013-11-08 12:06:55 -05:00
|
|
|
|
|
|
|
static VALUE sym_output, sym_stdout, sym_string, sym_file;
|
2020-07-28 06:58:49 -04:00
|
|
|
static VALUE sym_full, sym_since;
|
2013-11-08 12:06:55 -05:00
|
|
|
|
|
|
|
struct dump_config {
|
|
|
|
VALUE type;
|
|
|
|
FILE *stream;
|
|
|
|
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;
|
2013-11-08 12:06:55 -05:00
|
|
|
};
|
|
|
|
|
2016-05-07 20:55:29 -04:00
|
|
|
PRINTF_ARGS(static void dump_append(struct dump_config *, const char *, ...), 2, 3);
|
2013-11-08 12:06:55 -05:00
|
|
|
static void
|
|
|
|
dump_append(struct dump_config *dc, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list vl;
|
|
|
|
va_start(vl, format);
|
|
|
|
|
|
|
|
if (dc->stream) {
|
|
|
|
vfprintf(dc->stream, format, vl);
|
|
|
|
}
|
|
|
|
else if (dc->string)
|
|
|
|
rb_str_vcatf(dc->string, format, vl);
|
|
|
|
|
|
|
|
va_end(vl);
|
|
|
|
}
|
|
|
|
|
|
|
|
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++) {
|
|
|
|
switch ((c = value[i])) {
|
2013-11-09 01:16:42 -05:00
|
|
|
case '\\':
|
|
|
|
case '"':
|
|
|
|
dump_append(dc, "\\%c", c);
|
|
|
|
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;
|
2020-08-17 09:36:48 -04:00
|
|
|
case '\177':
|
|
|
|
dump_append(dc, "\\u007f");
|
|
|
|
break;
|
2013-11-09 01:16:42 -05:00
|
|
|
default:
|
2013-11-26 00:27:22 -05:00
|
|
|
if (c <= 0x1f)
|
2020-08-17 09:29:51 -04:00
|
|
|
dump_append(dc, "\\u%04x", c);
|
2013-11-26 00:27:22 -05:00
|
|
|
else
|
|
|
|
dump_append(dc, "%c", 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) {
|
|
|
|
dump_append(dc, "true");
|
|
|
|
}
|
|
|
|
else if (value == Qfalse) {
|
|
|
|
dump_append(dc, "false");
|
|
|
|
}
|
|
|
|
else if (value == Qnil) {
|
|
|
|
dump_append(dc, "null");
|
|
|
|
}
|
|
|
|
else if (FIXNUM_P(value)) {
|
|
|
|
dump_append(dc, "%ld", FIX2LONG(value));
|
|
|
|
}
|
|
|
|
else if (FLONUM_P(value)) {
|
|
|
|
dump_append(dc, "%#g", RFLOAT_VALUE(value));
|
|
|
|
}
|
|
|
|
else if (SYMBOL_P(value)) {
|
|
|
|
dump_append_symbol_value(dc, value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dump_append(dc, "{}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (dc->cur_obj_references == 0)
|
2018-10-08 23:21:57 -04:00
|
|
|
dump_append(dc, ", \"references\":[\"%#"PRIxVALUE"\"", ref);
|
2013-11-08 12:06:55 -05:00
|
|
|
else
|
2018-10-08 23:21:57 -04:00
|
|
|
dump_append(dc, ", \"%#"PRIxVALUE"\"", 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)
|
|
|
|
{
|
|
|
|
dump_append(dc, ", \"bytesize\":%ld", RSTRING_LEN(obj));
|
|
|
|
if (!STR_EMBED_P(obj) && !STR_SHARED_P(obj) && (long)rb_str_capacity(obj) != RSTRING_LEN(obj))
|
2016-09-13 08:33:13 -04:00
|
|
|
dump_append(dc, ", \"capacity\":%"PRIuSIZE, rb_str_capacity(obj));
|
2015-10-19 02:25:01 -04:00
|
|
|
|
|
|
|
if (is_ascii_string(obj)) {
|
|
|
|
dump_append(dc, ", \"value\":");
|
|
|
|
dump_append_string_value(dc, obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)) {
|
2015-10-19 02:25:01 -04:00
|
|
|
dump_append_special_const(dc, obj);
|
2015-06-20 23:21:34 -04:00
|
|
|
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)
|
|
|
|
return;
|
|
|
|
|
2018-10-08 23:21:57 -04:00
|
|
|
dump_append(dc, "{\"address\":\"%#"PRIxVALUE"\", \"type\":\"%s\"", obj, obj_type(obj));
|
2013-11-08 12:06:55 -05:00
|
|
|
|
|
|
|
if (dc->cur_obj_klass)
|
2018-10-08 23:21:57 -04:00
|
|
|
dump_append(dc, ", \"class\":\"%#"PRIxVALUE"\"", dc->cur_obj_klass);
|
2013-11-08 12:06:55 -05:00
|
|
|
if (rb_obj_frozen_p(obj))
|
|
|
|
dump_append(dc, ", \"frozen\":true");
|
|
|
|
|
|
|
|
switch (BUILTIN_TYPE(obj)) {
|
2017-01-03 17:42:10 -05:00
|
|
|
case T_NONE:
|
2017-09-27 01:27:06 -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-07-09 18:37:03 -04:00
|
|
|
dump_append(dc, ", \"imemo_type\":\"%s\"", rb_imemo_name(imemo_type(obj)));
|
2017-01-31 12:46:51 -05:00
|
|
|
break;
|
|
|
|
|
2015-10-19 02:25:01 -04:00
|
|
|
case T_SYMBOL:
|
|
|
|
dump_append_string_content(dc, rb_sym2str(obj));
|
|
|
|
break;
|
|
|
|
|
2013-11-09 01:16:42 -05:00
|
|
|
case T_STRING:
|
|
|
|
if (STR_EMBED_P(obj))
|
|
|
|
dump_append(dc, ", \"embedded\":true");
|
|
|
|
if (is_broken_string(obj))
|
|
|
|
dump_append(dc, ", \"broken\":true");
|
2013-12-09 21:26:09 -05:00
|
|
|
if (FL_TEST(obj, RSTRING_FSTR))
|
|
|
|
dump_append(dc, ", \"fstring\":true");
|
2013-11-09 01:16:42 -05:00
|
|
|
if (STR_SHARED_P(obj))
|
|
|
|
dump_append(dc, ", \"shared\":true");
|
2015-10-19 02:25:01 -04:00
|
|
|
else
|
|
|
|
dump_append_string_content(dc, obj);
|
2013-11-08 12:06:55 -05:00
|
|
|
|
2013-11-09 01:16:42 -05:00
|
|
|
if (!ENCODING_IS_ASCII8BIT(obj))
|
|
|
|
dump_append(dc, ", \"encoding\":\"%s\"", rb_enc_name(rb_enc_from_index(ENCODING_GET(obj))));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_HASH:
|
2016-09-13 08:33:13 -04:00
|
|
|
dump_append(dc, ", \"size\":%"PRIuSIZE, (size_t)RHASH_SIZE(obj));
|
2019-07-30 21:22:47 -04:00
|
|
|
if (FL_TEST(obj, RHASH_PROC_DEFAULT))
|
2018-10-08 23:21:57 -04:00
|
|
|
dump_append(dc, ", \"default\":\"%#"PRIxVALUE"\"", RHASH_IFNONE(obj));
|
2013-11-09 01:16:42 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_ARRAY:
|
|
|
|
dump_append(dc, ", \"length\":%ld", 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;
|
|
|
|
|
|
|
|
case T_CLASS:
|
|
|
|
case T_MODULE:
|
2020-07-22 06:47:14 -04:00
|
|
|
if (dc->cur_obj_klass) {
|
|
|
|
VALUE mod_name = rb_mod_name(obj);
|
|
|
|
if (!NIL_P(mod_name))
|
|
|
|
dump_append(dc, ", \"name\":\"%s\"", RSTRING_PTR(mod_name));
|
|
|
|
}
|
2013-11-09 01:16:42 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_DATA:
|
|
|
|
if (RTYPEDDATA_P(obj))
|
|
|
|
dump_append(dc, ", \"struct\":\"%s\"", RTYPEDDATA_TYPE(obj)->wrap_struct_name);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
|
|
|
dump_append(dc, ", \"value\":\"%g\"", RFLOAT_VALUE(obj));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_OBJECT:
|
2016-04-22 05:47:34 -04:00
|
|
|
dump_append(dc, ", \"ivars\":%u", ROBJECT_NUMIV(obj));
|
2013-11-09 01:16:42 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FILE:
|
|
|
|
fptr = RFILE(obj)->fptr;
|
2014-03-10 17:55:51 -04:00
|
|
|
if (fptr)
|
|
|
|
dump_append(dc, ", \"fd\":%d", fptr->fd);
|
2013-11-09 01:16:42 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_ZOMBIE:
|
2016-09-16 13:44:45 -04:00
|
|
|
dump_append(dc, "}\n");
|
2013-11-09 01:16:42 -05:00
|
|
|
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)
|
|
|
|
dump_append(dc, "]");
|
|
|
|
|
2020-07-28 06:58:49 -04:00
|
|
|
if (ainfo) {
|
2013-11-08 12:06:55 -05:00
|
|
|
dump_append(dc, ", \"file\":\"%s\", \"line\":%lu", ainfo->path, ainfo->line);
|
2014-08-02 21:56:16 -04:00
|
|
|
if (RTEST(ainfo->mid)) {
|
|
|
|
VALUE m = rb_sym2str(ainfo->mid);
|
2020-07-09 17:59:59 -04:00
|
|
|
dump_append(dc, ", \"method\":");
|
|
|
|
dump_append_string_value(dc, m);
|
2014-08-02 21:56:16 -04:00
|
|
|
}
|
2013-11-23 19:22:19 -05:00
|
|
|
dump_append(dc, ", \"generation\":%"PRIuSIZE, ainfo->generation);
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((memsize = rb_obj_memsize_of(obj)) > 0)
|
2013-11-23 19:22:19 -05:00
|
|
|
dump_append(dc, ", \"memsize\":%"PRIuSIZE, 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) {
|
|
|
|
dump_append(dc, ", \"flags\":{");
|
|
|
|
for (i=0; i<n; i++) {
|
|
|
|
dump_append(dc, "\"%s\":true", rb_id2name(flags[i]));
|
|
|
|
if (i != n-1) dump_append(dc, ", ");
|
|
|
|
}
|
|
|
|
dump_append(dc, "}");
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-01-03 17:42:10 -05:00
|
|
|
if (dc->full_heap || RBASIC(v)->flags)
|
|
|
|
dump_object(v, dc);
|
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)
|
2016-09-16 13:44:45 -04:00
|
|
|
dump_append(dc, "]}\n");
|
|
|
|
if (dc->root_category == NULL || category != dc->root_category)
|
2018-10-08 23:21:57 -04:00
|
|
|
dump_append(dc, "{\"type\":\"ROOT\", \"root\":\"%s\", \"references\":[\"%#"PRIxVALUE"\"", category, obj);
|
2013-11-08 12:06:55 -05:00
|
|
|
else
|
2018-10-08 23:21:57 -04:00
|
|
|
dump_append(dc, ", \"%#"PRIxVALUE"\"", 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
|
|
|
}
|
|
|
|
|
2013-11-09 01:16:53 -05:00
|
|
|
static VALUE
|
2013-11-14 11:06:09 -05:00
|
|
|
dump_output(struct dump_config *dc, VALUE opts, VALUE output, const char *filename)
|
2013-11-09 01:16:53 -05:00
|
|
|
{
|
2013-11-14 11:06:09 -05:00
|
|
|
VALUE tmp;
|
|
|
|
|
2017-01-03 17:42:10 -05:00
|
|
|
dc->full_heap = 0;
|
|
|
|
|
|
|
|
if (RTEST(opts)) {
|
2013-11-09 01:16:53 -05:00
|
|
|
output = rb_hash_aref(opts, sym_output);
|
|
|
|
|
2017-01-03 17:42:10 -05:00
|
|
|
if (Qtrue == rb_hash_lookup2(opts, sym_full, Qfalse))
|
|
|
|
dc->full_heap = 1;
|
2020-07-28 06:58:49 -04:00
|
|
|
|
|
|
|
VALUE since = rb_hash_aref(opts, sym_since);
|
|
|
|
if (RTEST(since)) {
|
|
|
|
dc->partial_dump = 1;
|
|
|
|
dc->since = NUM2SIZET(since);
|
|
|
|
} else {
|
|
|
|
dc->partial_dump = 0;
|
|
|
|
}
|
2017-01-03 17:42:10 -05:00
|
|
|
}
|
|
|
|
|
2013-11-09 01:16:53 -05:00
|
|
|
if (output == sym_stdout) {
|
|
|
|
dc->stream = stdout;
|
|
|
|
dc->string = Qnil;
|
|
|
|
}
|
|
|
|
else if (output == sym_file) {
|
2013-11-14 11:06:09 -05:00
|
|
|
rb_io_t *fptr;
|
|
|
|
rb_require("tempfile");
|
|
|
|
tmp = rb_assoc_new(rb_str_new_cstr(filename), rb_str_new_cstr(".json"));
|
|
|
|
tmp = rb_funcallv(rb_path2class("Tempfile"), rb_intern("create"), 1, &tmp);
|
|
|
|
io:
|
|
|
|
dc->string = rb_io_get_write_io(tmp);
|
|
|
|
rb_io_flush(dc->string);
|
|
|
|
GetOpenFile(dc->string, fptr);
|
|
|
|
dc->stream = rb_io_stdio_file(fptr);
|
2013-11-09 01:16:53 -05:00
|
|
|
}
|
|
|
|
else if (output == sym_string) {
|
|
|
|
dc->string = rb_str_new_cstr("");
|
|
|
|
}
|
2013-11-14 11:06:09 -05:00
|
|
|
else if (!NIL_P(tmp = rb_io_check_io(output))) {
|
|
|
|
output = sym_file;
|
|
|
|
goto io;
|
|
|
|
}
|
2013-11-09 01:16:53 -05:00
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "wrong output option: %"PRIsVALUE, output);
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dump_result(struct dump_config *dc, VALUE output)
|
|
|
|
{
|
|
|
|
if (output == sym_string) {
|
2016-06-11 01:04:22 -04:00
|
|
|
return rb_str_resurrect(dc->string);
|
2013-11-09 01:16:53 -05:00
|
|
|
}
|
|
|
|
else if (output == sym_file) {
|
2013-11-14 11:06:09 -05:00
|
|
|
rb_io_flush(dc->string);
|
2013-11-09 01:16:53 -05:00
|
|
|
return dc->string;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-08 12:06:55 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ObjectSpace.dump(obj[, output: :string]) # => "{ ... }"
|
2013-11-25 16:46:00 -05:00
|
|
|
* ObjectSpace.dump(obj, output: :file) # => #<File:/tmp/rubyobj20131125-88733-1xkfmpv.json>
|
|
|
|
* ObjectSpace.dump(obj, output: :stdout) # => nil
|
2013-11-08 12:06:55 -05:00
|
|
|
*
|
|
|
|
* Dump the contents of a ruby object as JSON.
|
|
|
|
*
|
|
|
|
* This method is only expected to work with C Ruby.
|
|
|
|
* This is an experimental method and is subject to change.
|
|
|
|
* In particular, the function signature and output format are
|
|
|
|
* not guaranteed to be compatible in future versions of ruby.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
objspace_dump(int argc, VALUE *argv, VALUE os)
|
|
|
|
{
|
2013-11-14 11:06:09 -05:00
|
|
|
static const char filename[] = "rubyobj";
|
2013-11-08 12:06:55 -05:00
|
|
|
VALUE obj = Qnil, opts = Qnil, output;
|
|
|
|
struct dump_config dc = {0,};
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "1:", &obj, &opts);
|
|
|
|
|
2013-11-09 01:16:53 -05:00
|
|
|
output = dump_output(&dc, opts, sym_string, filename);
|
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
|
|
|
|
2013-11-09 01:16:53 -05:00
|
|
|
return dump_result(&dc, output);
|
2013-11-08 12:06:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2013-11-25 16:46:00 -05:00
|
|
|
* ObjectSpace.dump_all([output: :file]) # => #<File:/tmp/rubyheap20131125-88469-laoj3v.json>
|
2013-11-08 12:06:55 -05:00
|
|
|
* ObjectSpace.dump_all(output: :stdout) # => nil
|
|
|
|
* ObjectSpace.dump_all(output: :string) # => "{...}\n{...}\n..."
|
2013-11-26 00:27:22 -05:00
|
|
|
* ObjectSpace.dump_all(output:
|
|
|
|
* File.open('heap.json','w')) # => #<File:heap.json>
|
2020-07-28 06:58:49 -04:00
|
|
|
* ObjectSpace.dump_all(output: :string,
|
|
|
|
* since: 42) # => "{...}\n{...}\n..."
|
2013-11-08 12:06:55 -05:00
|
|
|
*
|
|
|
|
* Dump the contents of the ruby heap as JSON.
|
|
|
|
*
|
2020-07-28 06:58:49 -04:00
|
|
|
* _since_ must be a non-negative integer or +nil+.
|
|
|
|
*
|
|
|
|
* If _since_ is a positive integer, only objects of that generation and
|
|
|
|
* newer generations are dumped. The current generation can be accessed using
|
|
|
|
* GC::count.
|
|
|
|
*
|
|
|
|
* Objects that were allocated without object allocation tracing enabled
|
|
|
|
* are ignored. See ::trace_object_allocations for more information and
|
|
|
|
* examples.
|
|
|
|
*
|
|
|
|
* If _since_ is omitted or is +nil+, all objects are dumped.
|
|
|
|
*
|
2013-11-08 12:06:55 -05:00
|
|
|
* This method is only expected to work with C Ruby.
|
|
|
|
* This is an experimental method and is subject to change.
|
|
|
|
* In particular, the function signature and output format are
|
|
|
|
* not guaranteed to be compatible in future versions of ruby.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
objspace_dump_all(int argc, VALUE *argv, VALUE os)
|
|
|
|
{
|
2013-11-14 11:06:09 -05:00
|
|
|
static const char filename[] = "rubyheap";
|
2013-11-08 12:06:55 -05:00
|
|
|
VALUE opts = Qnil, output;
|
|
|
|
struct dump_config dc = {0,};
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "0:", &opts);
|
|
|
|
|
2013-11-09 01:16:53 -05:00
|
|
|
output = dump_output(&dc, opts, sym_file, filename);
|
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);
|
|
|
|
|
2013-11-09 01:16:53 -05:00
|
|
|
return dump_result(&dc, output);
|
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
|
|
|
|
|
|
|
|
rb_define_module_function(rb_mObjSpace, "dump", objspace_dump, -1);
|
|
|
|
rb_define_module_function(rb_mObjSpace, "dump_all", objspace_dump_all, -1);
|
|
|
|
|
|
|
|
sym_output = ID2SYM(rb_intern("output"));
|
|
|
|
sym_stdout = ID2SYM(rb_intern("stdout"));
|
|
|
|
sym_string = ID2SYM(rb_intern("string"));
|
2020-07-28 06:58:49 -04:00
|
|
|
sym_since = ID2SYM(rb_intern("since"));
|
2013-11-08 12:06:55 -05:00
|
|
|
sym_file = ID2SYM(rb_intern("file"));
|
2017-01-03 17:42:10 -05:00
|
|
|
sym_full = ID2SYM(rb_intern("full"));
|
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
|
|
|
}
|