use crate::driver::base::kobject::{CommonKobj, DynamicKObjKType, KObject, KObjectManager};
use crate::init::initcall::INITCALL_POSTCORE;
use crate::misc::ksysfs::sys_kernel_kobj;
use alloc::string::ToString;
use alloc::sync::Arc;
use system_error::SystemError;
use unified_init::macros::unified_init;

/// `/sys/kernel/debug`的kobject
static mut SYS_KERNEL_DEBUG_KOBJECT_INSTANCE: Option<Arc<CommonKobj>> = None;

/// 初始化debug模块在sysfs中的目录
#[unified_init(INITCALL_POSTCORE)]
fn debugfs_init() -> Result<(), SystemError> {
    let debug_kobj = CommonKobj::new("debug".to_string());
    debug_kobj.set_parent(Some(Arc::downgrade(
        &(sys_kernel_kobj() as Arc<dyn KObject>),
    )));
    KObjectManager::init_and_add_kobj(debug_kobj.clone(), Some(&DynamicKObjKType))?;
    unsafe {
        SYS_KERNEL_DEBUG_KOBJECT_INSTANCE = Some(debug_kobj);
    }
    super::tracing::init_debugfs_tracing()?;
    super::rcu::init_debugfs_rcu()?;
    super::errseq::init_debugfs_errseq()?;
    super::block::init_debugfs_block()?;
    super::ext4::init_debugfs_ext4()?;
    super::fuse::init_debugfs_fuse()?;
    super::kthread::init_debugfs_kthread()?;
    #[cfg(any(target_arch = "x86_64", target_arch = "riscv64"))]
    super::mm::init_debugfs_mm()?;
    super::page_cache::init_debugfs_page_cache()?;
    super::timekeeping::init_debugfs_timekeeping()?;
    return Ok(());
}

pub fn debugfs_kobj() -> Arc<CommonKobj> {
    unsafe { SYS_KERNEL_DEBUG_KOBJECT_INSTANCE.clone().unwrap() }
}
