diff --git a/BAN/include/BAN/Debug.h b/BAN/include/BAN/Debug.h new file mode 100644 index 0000000000..2dc87f33a9 --- /dev/null +++ b/BAN/include/BAN/Debug.h @@ -0,0 +1,31 @@ +#pragma once + +#if __is_kernel +#error "This is userspace only file" +#endif + +#include +#include + +#define __debug_putchar [](int c) { putc(c, stddbg); } + +#define dprintln(...) \ + do { \ + BAN::Formatter::print(__debug_putchar, __VA_ARGS__); \ + BAN::Formatter::print(__debug_putchar,"\r\n"); \ + fflush(stddbg); \ + } while (false) + +#define dwarnln(...) \ + do { \ + BAN::Formatter::print(__debug_putchar, "\e[33m"); \ + dprintln(__VA_ARGS__); \ + BAN::Formatter::print(__debug_putchar, "\e[m"); \ + } while(false) + +#define derrorln(...) \ + do { \ + BAN::Formatter::print(__debug_putchar, "\e[31m"); \ + dprintln(__VA_ARGS__); \ + BAN::Formatter::print(__debug_putchar, "\e[m"); \ + } while(false) diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 1b73337328..a0189ad3a1 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -16,6 +16,7 @@ set(KERNEL_SOURCES kernel/CPUID.cpp kernel/Credentials.cpp kernel/Debug.cpp + kernel/Device/DebugDevice.cpp kernel/Device/Device.cpp kernel/Device/FramebufferDevice.cpp kernel/Device/NullDevice.cpp diff --git a/kernel/include/kernel/Debug.h b/kernel/include/kernel/Debug.h index 4d8c5a9a2a..1e4ae98763 100644 --- a/kernel/include/kernel/Debug.h +++ b/kernel/include/kernel/Debug.h @@ -5,7 +5,7 @@ #define dprintln(...) \ do { \ Debug::DebugLock::lock(); \ - Debug::print_prefix(__FILE__, __LINE__); \ + Debug::print_prefix(__FILE__, __LINE__); \ BAN::Formatter::print(Debug::putchar, __VA_ARGS__); \ BAN::Formatter::print(Debug::putchar, "\r\n"); \ Debug::DebugLock::unlock(); \ diff --git a/kernel/include/kernel/Device/DebugDevice.h b/kernel/include/kernel/Device/DebugDevice.h new file mode 100644 index 0000000000..2487e1eaba --- /dev/null +++ b/kernel/include/kernel/Device/DebugDevice.h @@ -0,0 +1,28 @@ +#include + +namespace Kernel +{ + + class DebugDevice : public CharacterDevice + { + public: + static BAN::ErrorOr> create(mode_t, uid_t, gid_t); + + virtual dev_t rdev() const override { return m_rdev; } + + virtual BAN::StringView name() const override { return "debug"sv; } + + protected: + DebugDevice(mode_t mode, uid_t uid, gid_t gid, dev_t rdev) + : CharacterDevice(mode, uid, gid) + , m_rdev(rdev) + { } + + virtual BAN::ErrorOr read_impl(off_t, BAN::ByteSpan) override { return 0; } + virtual BAN::ErrorOr write_impl(off_t, BAN::ConstByteSpan buffer) override; + + private: + const dev_t m_rdev; + }; + +} diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index 70eb7946b5..19d371da78 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -62,6 +62,8 @@ namespace Kernel bool is_session_leader() const { return pid() == sid(); } + const char* name() const { return m_cmdline.empty() ? "" : m_cmdline.front().data(); } + const Credentials& credentials() const { return m_credentials; } BAN::ErrorOr sys_exit(int status); diff --git a/kernel/kernel/Device/DebugDevice.cpp b/kernel/kernel/Device/DebugDevice.cpp new file mode 100644 index 0000000000..380823c2f7 --- /dev/null +++ b/kernel/kernel/Device/DebugDevice.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +namespace Kernel +{ + + BAN::ErrorOr> DebugDevice::create(mode_t mode, uid_t uid, gid_t gid) + { + auto* result = new DebugDevice(mode, uid, gid, DevFileSystem::get().get_next_dev()); + if (result == nullptr) + return BAN::Error::from_errno(ENOMEM); + return BAN::RefPtr::adopt(result); + } + + BAN::ErrorOr DebugDevice::write_impl(off_t, BAN::ConstByteSpan buffer) + { + auto ms_since_boot = SystemTimer::get().ms_since_boot(); + Debug::DebugLock::lock(); + BAN::Formatter::print(Debug::putchar, "[{5}.{3}] {}: ", + ms_since_boot / 1000, + ms_since_boot % 1000, + Kernel::Process::current().name() + ); + for (size_t i = 0; i < buffer.size(); i++) + Debug::putchar(buffer[i]); + Debug::DebugLock::unlock(); + return buffer.size(); + } + +} diff --git a/kernel/kernel/FS/DevFS/FileSystem.cpp b/kernel/kernel/FS/DevFS/FileSystem.cpp index 177118ef8f..d82d917ea9 100644 --- a/kernel/kernel/FS/DevFS/FileSystem.cpp +++ b/kernel/kernel/FS/DevFS/FileSystem.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -22,6 +23,7 @@ namespace Kernel ASSERT(s_instance); MUST(s_instance->TmpFileSystem::initialize(0755, 0, 0)); + s_instance->add_device(MUST(DebugDevice::create(0666, 0, 0))); s_instance->add_device(MUST(NullDevice::create(0666, 0, 0))); s_instance->add_device(MUST(ZeroDevice::create(0666, 0, 0))); } diff --git a/libc/include/stdio.h b/libc/include/stdio.h index bf3a37db04..4fc04d2089 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -53,6 +53,8 @@ extern FILE* __stdout; #define stdout __stdout extern FILE* __stderr; #define stderr __stderr +extern FILE* __stddbg; +#define stddbg __stddbg void clearerr(FILE* stream); char* ctermid(char* s); diff --git a/libc/include/unistd.h b/libc/include/unistd.h index 0961cabb3e..76c46ba9a8 100644 --- a/libc/include/unistd.h +++ b/libc/include/unistd.h @@ -120,6 +120,7 @@ __BEGIN_DECLS #define STDIN_FILENO 0 #define STDOUT_FILENO 1 #define STDERR_FILENO 2 +#define STDDBG_FILENO 3 #define _POSIX_VDISABLE 0 diff --git a/libc/stdio.cpp b/libc/stdio.cpp index d2fdf2e2d3..6e33d2baa7 100644 --- a/libc/stdio.cpp +++ b/libc/stdio.cpp @@ -22,11 +22,13 @@ static FILE s_files[FOPEN_MAX] { { .fd = STDIN_FILENO }, { .fd = STDOUT_FILENO }, { .fd = STDERR_FILENO }, + { .fd = STDDBG_FILENO }, }; FILE* stdin = &s_files[0]; FILE* stdout = &s_files[1]; FILE* stderr = &s_files[2]; +FILE* stddbg = &s_files[3]; void clearerr(FILE* file) { diff --git a/userspace/init/main.cpp b/userspace/init/main.cpp index 4fdf9d4127..6e9a2aebc1 100644 --- a/userspace/init/main.cpp +++ b/userspace/init/main.cpp @@ -17,6 +17,7 @@ void initialize_stdio() if (open(tty, O_RDONLY | O_TTY_INIT) != 0) _exit(1); if (open(tty, O_WRONLY) != 1) _exit(1); if (open(tty, O_WRONLY) != 2) _exit(1); + if (open("/dev/debug", O_WRONLY) != 3) _exit(1); } int main()