Delete code for debugging namespace

This commit is contained in:
Satoshi Tagomori 2025-05-08 23:53:20 +09:00
parent 82f645e818
commit 8ecc04dc04
4 changed files with 0 additions and 496 deletions

481
class.c
View File

@ -422,487 +422,6 @@ rb_class_classext_foreach(VALUE klass, rb_class_classext_foreach_callback_func *
func(RCLASS_EXT_PRIME(klass), true, (VALUE)NULL, arg);
}
VALUE
rb_class_debug_duplicate_classext(VALUE klass, VALUE namespace)
{
rb_namespace_t *ns = rb_get_namespace_t(namespace);
RCLASS_EXT_WRITABLE_IN_NS(klass, ns);
return Qnil;
}
static VALUE
debug_dump_inspect_or_return_type_begin(VALUE v)
{
VALUE buf, klass;
const char *typestr;
char cstrbuf[2048];
if (!v) {
return rb_str_new_cstr("NULL");
}
if (!SPECIAL_CONST_P(v)) {
klass = RBASIC_CLASS(v);
if (UNLIKELY(!klass)) {
typestr = rb_type_str(BUILTIN_TYPE(v));
snprintf(cstrbuf, 2048, "[Hidden/Broken: %s]", typestr);
return rb_str_new_cstr(cstrbuf);
}
}
klass = CLASS_OF(v);
if (!klass) {
typestr = rb_type_str(BUILTIN_TYPE(v));
snprintf(cstrbuf, 2048, "[Hidden/Broken: %s]", typestr);
return rb_str_new_cstr(cstrbuf);
}
switch (TYPE(v)) {
case T_ARRAY:
case T_BIGNUM:
case T_COMPLEX:
case T_FALSE:
case T_FILE:
case T_FIXNUM:
case T_FLOAT:
case T_HASH:
case T_MATCH:
case T_NIL:
case T_OBJECT:
case T_RATIONAL:
case T_REGEXP:
case T_STRING:
case T_STRUCT:
case T_SYMBOL:
case T_TRUE:
return rb_inspect(v);
case T_CLASS:
case T_MODULE:
buf = RCLASS_CLASSPATH(v);
if (buf) {
return buf;
}
typestr = rb_type_str(TYPE(v));
snprintf(cstrbuf, 2048, "[%s: ", typestr);
buf = rb_str_new_cstr(cstrbuf);
rb_str_concat(buf, rb_funcall(rb_obj_id(v), rb_intern("to_s"), 0));
rb_str_cat_cstr(buf, "]");
return buf;
case T_DATA:
return rb_str_new_cstr("[VALUE: T_DATA]");
case T_ICLASS:
buf = rb_str_new_cstr("[VALUE: T_ICLASS(");
rb_str_concat(buf, debug_dump_inspect_or_return_type_begin(RBASIC(v)->klass));
rb_str_cat_cstr(buf, ", ");
rb_str_concat(buf, rb_funcall(rb_obj_id(v), rb_intern("to_s"), 0));
rb_str_cat_cstr(buf, ")]");
return buf;
case T_IMEMO:
return rb_str_new_cstr("[VALUE: T_IMEMO ]");
case T_MASK:
return rb_str_new_cstr("[VALUE: T_MASK]");
case T_NODE:
return rb_str_new_cstr("[VALUE: T_NODE]");
case T_NONE:
return rb_str_new_cstr("[VALUE: T_NONE]");
case T_UNDEF:
return rb_str_new_cstr("[VALUE: T_UNDEF]");
case T_ZOMBIE:
return rb_str_new_cstr("[VALUE: T_ZOMBIE]");
}
return rb_str_new_cstr("[VALUE: unknown type]");
}
static VALUE
debug_dump_inspect_or_return_type_rescue(VALUE v, VALUE e)
{
const char *typestr;
char cstrbuf[2048];
if (rb_obj_is_kind_of(e, rb_eNotImpError)) {
typestr = rb_type_str(BUILTIN_TYPE(v));
snprintf(cstrbuf, 2048, "[hidden? object: %s]", typestr);
return rb_str_new_cstr(cstrbuf);
}
VALUE msg_v = rb_funcall(e, rb_intern("message"), 0);
const char *msg = StringValueCStr(msg_v);
rb_raise(rb_class_name(CLASS_OF(e)), "%s", msg);
}
static VALUE
debug_dump_inspect_or_return_type(VALUE v)
{
return rb_rescue(
debug_dump_inspect_or_return_type_begin, v,
debug_dump_inspect_or_return_type_rescue, v
);
}
static VALUE
debug_dump_super_chain(rb_classext_t *ext, const rb_namespace_t *ns)
{
VALUE result = rb_str_new_cstr("Super: ");
VALUE s = RCLASSEXT_SUPER(ext);
bool chaining = true;
int count = 0;
while (chaining && count++ < 100) {
rb_str_concat(result, debug_dump_inspect_or_return_type(s));
if (s) {
rb_str_cat_cstr(result, " -> ");
if (ns) {
s = RCLASSEXT_SUPER(RCLASS_EXT_READABLE_IN_NS(s, ns));
}
else {
s = RCLASSEXT_SUPER(RCLASS_EXT_PRIME(s));
}
}
else {
chaining = false;
}
}
if (count >= 100) {
rb_str_cat_cstr(result, " -> ... (chain size >= 100)\n");
}
else {
rb_str_cat_cstr(result, "\n");
}
return result;
}
static int
debug_dump_ivars_foreach_i(st_data_t k, st_data_t v, st_data_t arg)
{
ID key = (ID)k;
VALUE value = (VALUE)v;
VALUE result = (VALUE)arg;
rb_str_cat_cstr(result, " ");
rb_str_cat_cstr(result, rb_id2name(key));
rb_str_cat_cstr(result, " -> ");
// to suppress the very long Gem ivar values
if (key == rb_intern("@loaded_specs")) {
rb_str_cat_cstr(result, "[ ... ]");
}
else if (key == rb_intern("@path_to_default_spec_map")) {
rb_str_cat_cstr(result, "{ ... }");
}
else {
rb_str_concat(result, debug_dump_inspect_or_return_type(value));
}
rb_str_cat_cstr(result, "\n");
return ST_CONTINUE;
}
static enum rb_id_table_iterator_result
debug_dump_id_table_foreach_i_names(ID key, VALUE value, void *data)
{
VALUE ary = (VALUE)data;
rb_ary_push(ary, rb_id2str(key));
return ID_TABLE_CONTINUE;
}
static VALUE
debug_dump_sorted_list_of_id_table_keys(struct rb_id_table *tbl)
{
VALUE keys = rb_ary_new_capa(rb_id_table_size(tbl));
rb_id_table_foreach(tbl, debug_dump_id_table_foreach_i_names, (void *)keys);
rb_ary_sort_bang(keys);
return rb_ary_join(keys, rb_str_new_cstr(", "));
}
static enum rb_id_table_iterator_result
debug_dump_sorted_methods_i(ID key, VALUE value, void *data)
{
rb_method_entry_t *me = (rb_method_entry_t *)value;
VALUE ary = (VALUE)data;
char buf[2048];
if (METHOD_ENTRY_INVALIDATED(me)) {
// snprintf(buf, 2048, "%s[%p](*)", rb_id2name(key), me);
snprintf(buf, 2048, "%s(*)", rb_id2name(key));
rb_ary_push(ary, rb_str_new_cstr(buf));
}
else {
// snprintf(buf, 2048, "%s[%p]", rb_id2name(key), me);
snprintf(buf, 2048, "%s", rb_id2name(key));
rb_ary_push(ary, rb_str_new_cstr(buf));
}
return ID_TABLE_CONTINUE;
}
static VALUE
debug_dump_sorted_methods(struct rb_id_table *tbl)
{
VALUE results = rb_ary_new_capa(rb_id_table_size(tbl));
rb_id_table_foreach(tbl, debug_dump_sorted_methods_i, (void *)results);
rb_ary_sort_bang(results);
return rb_ary_join(results, rb_str_new_cstr(", "));
}
static enum rb_id_table_iterator_result
debug_dump_constants_i(ID key, VALUE value, void *data)
{
VALUE str = rb_str_dup(rb_id2str(key));
VALUE results = (VALUE)data;
rb_const_entry_t *ce = (rb_const_entry_t *)value;
if (UNDEF_P(ce->value)) { // autoload
rb_str_cat_cstr(str, "(*)");
}
rb_ary_push(results, str);
return ID_TABLE_CONTINUE;
}
static VALUE
debug_dump_sorted_constants(struct rb_id_table *tbl)
{
VALUE results = rb_ary_new_capa(rb_id_table_size(tbl));
rb_id_table_foreach(tbl, debug_dump_constants_i, (void *)results);
rb_ary_sort_bang(results);
return rb_ary_join(results, rb_str_new_cstr(", "));
}
static VALUE
debug_dump_classext(rb_classext_t *ext, VALUE klass, const rb_namespace_t *ns)
{
char buf[2048];
int i, len;
rb_subclass_entry_t *subclass;
VALUE result = rb_str_new_cstr("ext :");
snprintf(buf, 2048, "%p\n", (void *) ext);
rb_str_cat_cstr(result, buf);
rb_str_concat(result, debug_dump_super_chain(ext, ns));
if (klass && !rb_shape_obj_too_complex_p(klass)) {
shape_id_t shape_id = RCLASS_SHAPE_ID(klass);
snprintf(buf, 2048, "Ivars: SHAPE (%d)\n", shape_id);
rb_str_cat_cstr(result, buf);
rb_ivar_foreach(klass, debug_dump_ivars_foreach_i, (st_data_t)result);
} else {
if (!RCLASSEXT_FIELDS(ext)) {
rb_str_cat_cstr(result, "Ivars: NONE\n");
}
else if (st_table_size((st_table *)RCLASSEXT_FIELDS(ext)) == 0) {
snprintf(buf, 2048, "IVars: NONE (%p)\n", (void *)RCLASSEXT_FIELDS(ext));
rb_str_cat_cstr(result, buf);
}
else {
snprintf(buf, 2048, "IVars: %zu (%p)\n", st_table_size((st_table *)RCLASSEXT_FIELDS(ext)), (void *)RCLASSEXT_FIELDS(ext));
rb_str_cat_cstr(result, buf);
st_foreach((st_table *)RCLASSEXT_FIELDS(ext), debug_dump_ivars_foreach_i, (st_data_t)result);
}
}
if (!RCLASSEXT_CONST_TBL(ext) || rb_id_table_size(RCLASSEXT_CONST_TBL(ext)) == 0) {
rb_str_cat_cstr(result, "Constants: NONE\n");
}
else {
snprintf(buf, 2048, "Constants: %zu\n ", rb_id_table_size(RCLASSEXT_CONST_TBL(ext)));
rb_str_cat_cstr(result, buf);
rb_str_concat(result, debug_dump_sorted_constants(RCLASSEXT_CONST_TBL(ext)));
rb_str_cat_cstr(result, ".\n");
}
if (!RCLASSEXT_M_TBL(ext) || rb_id_table_size(RCLASSEXT_M_TBL(ext)) == 0) {
rb_str_cat_cstr(result, "Methods: NONE\n");
}
else {
snprintf(buf, 2048, "Methods: %zu\n ", rb_id_table_size(RCLASSEXT_M_TBL(ext)));
rb_str_cat_cstr(result, buf);
rb_str_concat(result, debug_dump_sorted_methods(RCLASSEXT_M_TBL(ext)));
rb_str_cat_cstr(result, ".\n");
}
if (!RCLASSEXT_CALLABLE_M_TBL(ext) || rb_id_table_size(RCLASSEXT_CALLABLE_M_TBL(ext)) == 0) {
rb_str_cat_cstr(result, "Callable Methods: NONE\n");
}
else {
snprintf(buf, 2048, "Callable Methods: %zu\n ", rb_id_table_size(RCLASSEXT_CALLABLE_M_TBL(ext)));
rb_str_cat_cstr(result, buf);
rb_str_concat(result, debug_dump_sorted_list_of_id_table_keys(RCLASSEXT_CALLABLE_M_TBL(ext)));
rb_str_cat_cstr(result, ".\n");
}
if (!RCLASSEXT_CC_TBL(ext) || rb_id_table_size(RCLASSEXT_CC_TBL(ext)) == 0) {
rb_str_cat_cstr(result, "Call Caches: NONE\n");
}
else {
snprintf(buf, 2048, "Call Caches: %zu\n ", rb_id_table_size(RCLASSEXT_CC_TBL(ext)));
rb_str_cat_cstr(result, buf);
rb_str_concat(result, debug_dump_sorted_list_of_id_table_keys(RCLASSEXT_CC_TBL(ext)));
rb_str_cat_cstr(result, ".\n");
}
if (!RCLASSEXT_CVC_TBL(ext) || rb_id_table_size(RCLASSEXT_CVC_TBL(ext)) == 0) {
rb_str_cat_cstr(result, "CVar Caches: NONE\n");
}
else {
snprintf(buf, 2048, "CVar Caches: %zu\n ", rb_id_table_size(RCLASSEXT_CVC_TBL(ext)));
rb_str_cat_cstr(result, buf);
rb_str_concat(result, debug_dump_sorted_list_of_id_table_keys(RCLASSEXT_CVC_TBL(ext)));
rb_str_cat_cstr(result, ".\n");
}
if (RCLASSEXT_SUPERCLASS_DEPTH(ext) == 0) {
rb_str_cat_cstr(result, "Superclasses: NONE\n");
}
else {
rb_str_cat_cstr(result, "Superclasses: ");
len = (int)RCLASSEXT_SUPERCLASS_DEPTH(ext);
for (i=0; i<len; i++) {
rb_str_concat(result, debug_dump_inspect_or_return_type(RCLASSEXT_SUPERCLASSES(ext)[i]));
rb_str_cat_cstr(result, ", ");
}
rb_str_cat_cstr(result, ".\n");
}
snprintf(buf, 2048, "Superclasses owner: %s, with_self: %s.\n",
RCLASSEXT_SUPERCLASSES_OWNER(ext) ? "true" : "false",
RCLASSEXT_SUPERCLASSES_WITH_SELF(ext) ? "true" : "false");
rb_str_cat_cstr(result, buf);
if (!RCLASSEXT_SUBCLASSES(ext)) {
rb_str_cat_cstr(result, "Subclasses: NONE\n");
}
else {
rb_str_cat_cstr(result, "Subclasses: ");
subclass = RCLASSEXT_SUBCLASSES(ext)->head;
while (subclass->next) {
subclass = subclass->next;
if (subclass->klass) {
rb_str_concat(result, debug_dump_inspect_or_return_type(subclass->klass));
rb_str_cat_cstr(result, ", ");
}
}
rb_str_cat_cstr(result, ".\n");
}
if (!RCLASSEXT_NS_SUPER_SUBCLASSES(ext)) {
rb_str_cat_cstr(result, "Namespace Super Subclass Table: NONE\n");
}
else {
snprintf(buf, 2048, "Namespace Super Subclass Table: %p\n", (void *)RCLASSEXT_NS_SUPER_SUBCLASSES(ext));
rb_str_cat_cstr(result, buf);
}
if (!RCLASSEXT_NS_MODULE_SUBCLASSES(ext)) {
rb_str_cat_cstr(result, "Namespace Module Subclass Table: NONE\n");
}
else {
snprintf(buf, 2048, "Namespace Module Subclass Table: %p\n", (void *)RCLASSEXT_NS_MODULE_SUBCLASSES(ext));
rb_str_cat_cstr(result, buf);
}
if (!RCLASSEXT_ORIGIN(ext)) {
rb_str_cat_cstr(result, "Origin: NONE\n");
}
else {
rb_str_cat_cstr(result, "Origin: ");
rb_str_concat(result, debug_dump_inspect_or_return_type(RCLASSEXT_ORIGIN(ext)));
rb_str_cat_cstr(result, "\n");
}
if (!RCLASSEXT_INCLUDER(ext)) {
rb_str_cat_cstr(result, "Includer: NONE\n");
}
else {
rb_str_cat_cstr(result, "Includer: ");
rb_str_concat(result, debug_dump_inspect_or_return_type(RCLASSEXT_INCLUDER(ext)));
rb_str_cat_cstr(result, "\n");
}
snprintf(buf, 2048,
"Max IV Count: %"PRIu32", Variation Count: %hhu, Permanent Classpath: %s, Cloned: %s, Shared Const TBL:%s\n",
(int32_t)RCLASSEXT_MAX_IV_COUNT(ext),
RCLASSEXT_VARIATION_COUNT(ext),
RCLASSEXT_PERMANENT_CLASSPATH(ext) ? "true" : "false",
RCLASSEXT_CLONED(ext) ? "true" : "false",
RCLASSEXT_SHARED_CONST_TBL(ext) ? "true" : "false");
rb_str_cat_cstr(result, buf);
snprintf(buf, 2048,
"iClass Origin: %s, iClass Origin Shared MTBL: %s, Superclasses Owner: %s, Superclasses With Self: %s\n",
RCLASSEXT_ICLASS_IS_ORIGIN(ext) ? "true" : "false",
RCLASSEXT_ICLASS_ORIGIN_SHARED_MTBL(ext) ? "true" : "false",
RCLASSEXT_SUPERCLASSES_OWNER(ext) ? "true" : "false",
RCLASSEXT_SUPERCLASSES_WITH_SELF(ext) ? "true" : "false");
rb_str_cat_cstr(result, buf);
rb_str_cat_cstr(result, "Classpath: ");
rb_str_concat(result, debug_dump_inspect_or_return_type(RCLASSEXT_CLASSPATH(ext)));
rb_str_cat_cstr(result, "\n");
return result;
}
static int
debug_dump_classext_i(st_data_t namespace, st_data_t classext_ptr, st_data_t arg)
{
VALUE return_value = (VALUE) arg;
rb_str_cat_cstr(return_value, "Namespace: ");
rb_str_concat(return_value, debug_dump_inspect_or_return_type(namespace));
rb_str_cat_cstr(return_value, "\n");
rb_str_concat(return_value, debug_dump_classext((rb_classext_t *)classext_ptr, (VALUE)NULL, rb_get_namespace_t((VALUE)namespace)));
rb_str_cat_cstr(return_value, "----\n");
return ST_CONTINUE;
}
VALUE
rb_class_debug_dump_all_classext(VALUE klass)
{
char buf[2048];
VALUE ns_str = rb_current_namespace_details(Qnil);
snprintf(buf, 2048, "=========== current ns: %s ===========\n", RSTRING_PTR(ns_str));
VALUE r = rb_str_new_cstr(buf);
rb_str_concat(r, debug_dump_inspect_or_return_type(klass));
snprintf(buf, 2048, " (%d,%p)\n", NUM2INT(rb_obj_id(klass)), (void *)klass);
rb_str_cat_cstr(r, buf);
rb_str_cat_cstr(r, "klass: ");
if (METACLASS_OF(klass)) {
rb_str_concat(r, debug_dump_inspect_or_return_type(METACLASS_OF(klass)));
}
else {
rb_str_cat_cstr(r, "NONE\n");
}
rb_str_cat_cstr(r, "\n");
if (RCLASS_SINGLETON_P(klass)) {
rb_str_cat_cstr(r, "singleton class: true\n");
}
else {
rb_str_cat_cstr(r, "singleton class: false\n");
}
snprintf(buf, 2048,
"classext-dump:\n readable:%s\n writable:%s\n table size:%zu\n",
RCLASS_PRIME_CLASSEXT_READABLE_P(klass) ? "true" : "false",
RCLASS_PRIME_CLASSEXT_WRITABLE_P(klass) ? "true" : "false",
RCLASS(klass)->ns_classext_tbl ? st_table_size(RCLASS(klass)->ns_classext_tbl) : 0);
rb_str_cat_cstr(r, buf);
rb_str_cat_cstr(r, "========================\n");
rb_str_cat_cstr(r, "Namespace: ");
rb_str_concat(r, debug_dump_inspect_or_return_type(rb_get_namespace_object((rb_namespace_t *)RCLASSEXT_NS(RCLASS_EXT_PRIME(klass)))));
rb_str_cat_cstr(r, "\n");
rb_str_concat(r, debug_dump_classext(RCLASS_EXT_PRIME(klass), klass, (rb_namespace_t *)NULL));
rb_str_cat_cstr(r, "----\n");
if (RCLASS(klass)->ns_classext_tbl) {
rb_st_foreach(RCLASS(klass)->ns_classext_tbl, debug_dump_classext_i, (st_data_t)r);
}
rb_str_cat_cstr(r, "========== end =========\n");
return r;
}
VALUE
rb_class_debug_dump_all_classext_super(VALUE klass, VALUE upto)
{
int i, count = (int)rb_num2int(upto);
VALUE super;
for (i=0; i<count; i++) {
super = RCLASS_SUPER(klass);
if (!super) {
rb_warn("Super is NULL at %d", i+1);
}
}
return rb_class_debug_dump_all_classext(super);
}
void
rb_class_debug_print_classext(const char *label, const char *opt, VALUE klass)
{
VALUE d = rb_class_debug_dump_all_classext(klass);
if (opt) {
fprintf(stderr, "***** %s (%s)\n%s", label, opt, StringValueCStr(d));
}
else {
fprintf(stderr, "***** %s\n%s", label, StringValueCStr(d));
}
}
VALUE
rb_class_debug_print_module(VALUE self)
{
rb_class_debug_print_classext("class_debug_print", "rb_cModule", rb_cModule);
return Qnil;
}
VALUE
rb_class_super_of(VALUE klass)
{

View File

@ -293,12 +293,6 @@ static inline void RCLASS_WRITE_CLASSPATH(VALUE klass, VALUE classpath, bool per
// 3 is RMODULE_IS_REFINEMENT for RMODULE
// 4-19: SHAPE_FLAG_MASK
VALUE rb_class_debug_duplicate_classext(VALUE klass, VALUE namespace); // TODO: only for development
VALUE rb_class_debug_dump_all_classext(VALUE klass); // TODO: only for development
VALUE rb_class_debug_dump_all_classext_super(VALUE klass, VALUE upto); // TODO: only for development
void rb_class_debug_print_classext(const char *label, const char *opt, VALUE klass);
VALUE rb_class_debug_print_module(VALUE);
/* class.c */
rb_classext_t * rb_class_duplicate_classext(rb_classext_t *orig, VALUE obj, const rb_namespace_t *ns);
void rb_class_ensure_writable(VALUE obj);

View File

@ -75,6 +75,4 @@ VALUE rb_namespace_local_extension(VALUE namespace, VALUE fname, VALUE path);
void rb_initialize_main_namespace(void);
bool rb_namespace_debug_mode_p(void);
#endif /* INTERNAL_NAMESPACE_H */

View File

@ -4603,13 +4603,6 @@ InitVM_Object(void)
rb_define_method(rb_cModule, "deprecate_constant", rb_mod_deprecate_constant, -1); /* in variable.c */
rb_define_method(rb_cModule, "singleton_class?", rb_mod_singleton_p, 0);
// TODO: only for development
rb_define_method(rb_cModule, "debug_duplicate_classext", rb_class_debug_duplicate_classext, 1);
rb_define_method(rb_cModule, "debug_dump_all_classext", rb_class_debug_dump_all_classext, 0);
rb_define_method(rb_cModule, "debug_dump_all_classext_super", rb_class_debug_dump_all_classext_super, 1);
rb_define_method(rb_cBasicObject, "debug_print_module", rb_class_debug_print_module, 0);
rb_define_method(rb_singleton_class(rb_cClass), "allocate", rb_class_alloc_m, 0);
rb_define_method(rb_cClass, "allocate", rb_class_alloc_m, 0);
rb_define_method(rb_cClass, "new", rb_class_new_instance_pass_kw, -1);