Kernel/Userspace/LibC: Implement basic dprintln for userspace

This commit is contained in:
Bananymous 2024-02-05 01:24:09 +02:00
parent 79897e77dc
commit 692cec8458
11 changed files with 103 additions and 1 deletions

31
BAN/include/BAN/Debug.h Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#if __is_kernel
#error "This is userspace only file"
#endif
#include <BAN/Formatter.h>
#include <stdio.h>
#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)

View File

@ -16,6 +16,7 @@ set(KERNEL_SOURCES
kernel/CPUID.cpp kernel/CPUID.cpp
kernel/Credentials.cpp kernel/Credentials.cpp
kernel/Debug.cpp kernel/Debug.cpp
kernel/Device/DebugDevice.cpp
kernel/Device/Device.cpp kernel/Device/Device.cpp
kernel/Device/FramebufferDevice.cpp kernel/Device/FramebufferDevice.cpp
kernel/Device/NullDevice.cpp kernel/Device/NullDevice.cpp

View File

@ -5,7 +5,7 @@
#define dprintln(...) \ #define dprintln(...) \
do { \ do { \
Debug::DebugLock::lock(); \ 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, __VA_ARGS__); \
BAN::Formatter::print(Debug::putchar, "\r\n"); \ BAN::Formatter::print(Debug::putchar, "\r\n"); \
Debug::DebugLock::unlock(); \ Debug::DebugLock::unlock(); \

View File

@ -0,0 +1,28 @@
#include <kernel/Device/Device.h>
namespace Kernel
{
class DebugDevice : public CharacterDevice
{
public:
static BAN::ErrorOr<BAN::RefPtr<DebugDevice>> 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<size_t> read_impl(off_t, BAN::ByteSpan) override { return 0; }
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan buffer) override;
private:
const dev_t m_rdev;
};
}

View File

@ -62,6 +62,8 @@ namespace Kernel
bool is_session_leader() const { return pid() == sid(); } 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; } const Credentials& credentials() const { return m_credentials; }
BAN::ErrorOr<long> sys_exit(int status); BAN::ErrorOr<long> sys_exit(int status);

View File

@ -0,0 +1,32 @@
#include <kernel/Device/DebugDevice.h>
#include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/Process.h>
#include <kernel/Timer/Timer.h>
namespace Kernel
{
BAN::ErrorOr<BAN::RefPtr<DebugDevice>> 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<DebugDevice>::adopt(result);
}
BAN::ErrorOr<size_t> 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();
}
}

View File

@ -1,4 +1,5 @@
#include <BAN/ScopeGuard.h> #include <BAN/ScopeGuard.h>
#include <kernel/Device/DebugDevice.h>
#include <kernel/Device/FramebufferDevice.h> #include <kernel/Device/FramebufferDevice.h>
#include <kernel/Device/NullDevice.h> #include <kernel/Device/NullDevice.h>
#include <kernel/Device/ZeroDevice.h> #include <kernel/Device/ZeroDevice.h>
@ -22,6 +23,7 @@ namespace Kernel
ASSERT(s_instance); ASSERT(s_instance);
MUST(s_instance->TmpFileSystem::initialize(0755, 0, 0)); 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(NullDevice::create(0666, 0, 0)));
s_instance->add_device(MUST(ZeroDevice::create(0666, 0, 0))); s_instance->add_device(MUST(ZeroDevice::create(0666, 0, 0)));
} }

View File

@ -53,6 +53,8 @@ extern FILE* __stdout;
#define stdout __stdout #define stdout __stdout
extern FILE* __stderr; extern FILE* __stderr;
#define stderr __stderr #define stderr __stderr
extern FILE* __stddbg;
#define stddbg __stddbg
void clearerr(FILE* stream); void clearerr(FILE* stream);
char* ctermid(char* s); char* ctermid(char* s);

View File

@ -120,6 +120,7 @@ __BEGIN_DECLS
#define STDIN_FILENO 0 #define STDIN_FILENO 0
#define STDOUT_FILENO 1 #define STDOUT_FILENO 1
#define STDERR_FILENO 2 #define STDERR_FILENO 2
#define STDDBG_FILENO 3
#define _POSIX_VDISABLE 0 #define _POSIX_VDISABLE 0

View File

@ -22,11 +22,13 @@ static FILE s_files[FOPEN_MAX] {
{ .fd = STDIN_FILENO }, { .fd = STDIN_FILENO },
{ .fd = STDOUT_FILENO }, { .fd = STDOUT_FILENO },
{ .fd = STDERR_FILENO }, { .fd = STDERR_FILENO },
{ .fd = STDDBG_FILENO },
}; };
FILE* stdin = &s_files[0]; FILE* stdin = &s_files[0];
FILE* stdout = &s_files[1]; FILE* stdout = &s_files[1];
FILE* stderr = &s_files[2]; FILE* stderr = &s_files[2];
FILE* stddbg = &s_files[3];
void clearerr(FILE* file) void clearerr(FILE* file)
{ {

View File

@ -17,6 +17,7 @@ void initialize_stdio()
if (open(tty, O_RDONLY | O_TTY_INIT) != 0) _exit(1); if (open(tty, O_RDONLY | O_TTY_INIT) != 0) _exit(1);
if (open(tty, O_WRONLY) != 1) _exit(1); if (open(tty, O_WRONLY) != 1) _exit(1);
if (open(tty, O_WRONLY) != 2) _exit(1); if (open(tty, O_WRONLY) != 2) _exit(1);
if (open("/dev/debug", O_WRONLY) != 3) _exit(1);
} }
int main() int main()