mod.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. mod events;
  2. pub mod trace_pipe;
  3. use crate::debug::sysfs::debugfs_kset;
  4. use crate::driver::base::kobject::KObject;
  5. use crate::filesystem::kernfs::callback::{KernCallbackData, KernFSCallback, KernInodePrivateData};
  6. use crate::filesystem::kernfs::KernFSInode;
  7. use crate::filesystem::vfs::syscall::ModeType;
  8. use crate::filesystem::vfs::PollStatus;
  9. use crate::libs::spinlock::SpinLock;
  10. use crate::tracepoint::TracePointInfo;
  11. use alloc::string::ToString;
  12. use alloc::sync::Arc;
  13. use system_error::SystemError;
  14. static mut TRACING_ROOT_INODE: Option<Arc<KernFSInode>> = None;
  15. static TRACE_RAW_PIPE: SpinLock<crate::tracepoint::TracePipeRaw> =
  16. SpinLock::new(crate::tracepoint::TracePipeRaw::new(4096));
  17. static TRACE_CMDLINE_CACHE: SpinLock<crate::tracepoint::TraceCmdLineCache> =
  18. SpinLock::new(crate::tracepoint::TraceCmdLineCache::new(128));
  19. pub fn trace_pipe_push_raw_record(record: &[u8]) {
  20. TRACE_RAW_PIPE.lock().push_event(record.to_vec());
  21. }
  22. pub fn trace_cmdline_push(pid: u32) {
  23. let process = crate::process::ProcessManager::current_pcb();
  24. let binding = process.basic();
  25. let pname = binding
  26. .name()
  27. .split(' ')
  28. .next()
  29. .unwrap_or("unknown")
  30. .split('/')
  31. .last()
  32. .unwrap_or("unknown");
  33. TRACE_CMDLINE_CACHE.lock().insert(pid, pname.to_string());
  34. }
  35. #[allow(unused)]
  36. fn tracing_root_inode() -> Arc<KernFSInode> {
  37. unsafe { TRACING_ROOT_INODE.clone().unwrap() }
  38. }
  39. #[derive(Debug)]
  40. pub struct TracingDirCallBack;
  41. impl KernFSCallback for TracingDirCallBack {
  42. fn open(&self, _data: KernCallbackData) -> Result<(), SystemError> {
  43. Ok(())
  44. }
  45. fn read(
  46. &self,
  47. _data: KernCallbackData,
  48. _buf: &mut [u8],
  49. _offset: usize,
  50. ) -> Result<usize, SystemError> {
  51. Err(SystemError::EISDIR)
  52. }
  53. fn write(
  54. &self,
  55. _data: KernCallbackData,
  56. _buf: &[u8],
  57. _offset: usize,
  58. ) -> Result<usize, SystemError> {
  59. Err(SystemError::EISDIR)
  60. }
  61. fn poll(&self, _data: KernCallbackData) -> Result<PollStatus, SystemError> {
  62. Err(SystemError::EISDIR)
  63. }
  64. }
  65. impl KernInodePrivateData {
  66. pub fn debugfs_tracepoint(&self) -> Option<&Arc<TracePointInfo>> {
  67. return match self {
  68. KernInodePrivateData::DebugFS(tracepoint) => Some(tracepoint),
  69. _ => None,
  70. };
  71. }
  72. pub fn tracepipe(&mut self) -> Option<&mut crate::tracepoint::TracePipeSnapshot> {
  73. return match self {
  74. KernInodePrivateData::TracePipe(snapshot) => Some(snapshot),
  75. _ => None,
  76. };
  77. }
  78. }
  79. /// Initialize the debugfs tracing directory
  80. pub fn init_debugfs_tracing() -> Result<(), SystemError> {
  81. let debugfs = debugfs_kset();
  82. let root_dir = debugfs.inode().ok_or(SystemError::ENOENT)?;
  83. let tracing_root = root_dir.add_dir(
  84. "tracing".to_string(),
  85. ModeType::from_bits_truncate(0o555),
  86. None,
  87. Some(&TracingDirCallBack),
  88. )?;
  89. let events_root = tracing_root.add_dir(
  90. "events".to_string(),
  91. ModeType::from_bits_truncate(0o755),
  92. None,
  93. Some(&TracingDirCallBack),
  94. )?;
  95. // tracing_root.add_file(
  96. // "trace".to_string(),
  97. // ModeType::from_bits_truncate(0o444),
  98. // Some(4096),
  99. // None,
  100. // Some(&trace_pipe::TraceCallBack),
  101. // )?;
  102. tracing_root.add_file_lazy("trace".to_string(), trace_pipe::kernel_inode_provider)?;
  103. tracing_root.add_file(
  104. "trace_pipe".to_string(),
  105. ModeType::from_bits_truncate(0o444),
  106. Some(4096),
  107. None,
  108. Some(&trace_pipe::TracePipeCallBack),
  109. )?;
  110. events::init_events(events_root)?;
  111. unsafe {
  112. TRACING_ROOT_INODE = Some(tracing_root);
  113. }
  114. Ok(())
  115. }