Added debug_printf()

The debug_printf() function works just as any printf-like function. It prints to the serial port using ANSI coloring. Also, debug_vprintf() was implemented, which takes arguments as a va_list instead of as variadic arguments.
This commit is contained in:
Arnau Camprubí 2022-08-07 21:58:36 +02:00
pare 0e0d79811e
commit 109b5b17a1
S'han modificat 8 arxius amb 39 adicions i 23 eliminacions

Veure arxiu

@ -6,6 +6,7 @@
#include "../arch/i686/io.h"
#include "../lib/stdio.h"
#include "../shell.h"
#include "../lib/debug.h"
static bool g_caps_lock = false;
static bool g_shift = false;
@ -58,5 +59,6 @@ static void keyboard_callback(){
}
void keyboard_initialize(){
debug_printf("[KEYBOARD] Initializing keyboard\n");
i686_isr_register_handler(IRQ(1), keyboard_callback);
}

Veure arxiu

@ -1,11 +1,13 @@
#include "timer.h"
#include "../arch/i686/isr.h"
#include "../lib/stdio.h"
#include "../lib/debug.h"
static void timer_handler(registers *regs){
//printf("TICK\n");
}
void timer_initialize(){
debug_printf("[TIMER] Initializing PIT\n");
i686_isr_register_handler(IRQ(0), timer_handler);
}

14
src/kernel/lib/debug.c Normal file
Veure arxiu

@ -0,0 +1,14 @@
#include "debug.h"
#include "stdio.h"
void debug_printf(char *fmt, ...){
va_list args;
va_start(args, fmt);
debug_vprintf(fmt, args);
}
void debug_vprintf(char *fmt, va_list args){
serial_printf("\x1b[34m");
serial_vprintf(fmt, args);
serial_printf("\x1b[0m");
}

6
src/kernel/lib/debug.h Normal file
Veure arxiu

@ -0,0 +1,6 @@
#pragma once
#include <stdarg.h>
void debug_printf(char *fmt, ...);
void debug_vprintf(char *fmt, va_list args);

Veure arxiu

@ -119,7 +119,7 @@ void printf(char *fmt, ...){
}
void serial_vprintf(char *fmt, va_list args){
_vprintf(putc, fmt, args);
_vprintf(serial_putc, fmt, args);
}
void serial_printf(char *fmt, ...){

Veure arxiu

@ -1,6 +1,12 @@
#pragma once
#include <stdarg.h>
void puts(char *str);
void putc(char c);
void serial_putc(char c);
void vprintf(char *fmt, va_list args);
void printf(char *fmt, ...);
void serial_vprintf(char *fmt, va_list args);
void serial_printf(char *fmt, ...);
void clear_screen();

Veure arxiu

@ -8,6 +8,7 @@
#include "arch/i686/mm/pmm.h"
#include "arch/i686/mm/vmm.h"
#include "multiboot.h"
#include "lib/debug.h"
extern uint8_t end; // Kernel end
@ -21,6 +22,10 @@ void __attribute__((cdecl)) kmain(multiboot_info_t *multiboot_info){
// Initialize serial
uart_initialize(COM1, 2);
// Initialize drivers
timer_initialize();
keyboard_initialize();
// Initialize physical memory manager
uint32_t mem_size = 1024 + multiboot_info->mem_lower + multiboot_info->mem_upper*64;
@ -28,7 +33,7 @@ void __attribute__((cdecl)) kmain(multiboot_info_t *multiboot_info){
for(int i = 0; i < multiboot_info->mmap_length; i += sizeof(multiboot_memory_map_t)){
multiboot_memory_map_t *memory_map = (multiboot_memory_map_t *)(multiboot_info->mmap_addr + i);
serial_printf("Start Addr: 0x%x%x | Length: 0x%x%x | Size: 0x%x | Type: %i\n", memory_map->addr_h, memory_map->addr_l, memory_map->len_h, memory_map->len_l, memory_map->size, memory_map->type);
debug_printf("Start Addr: 0x%x%x | Length: 0x%x%x | Size: 0x%x | Type: %i\n", memory_map->addr_h, memory_map->addr_l, memory_map->len_h, memory_map->len_l, memory_map->size, memory_map->type);
if(memory_map->type == 1){
i686_pmm_init_region(memory_map->addr_l, memory_map->len_l);
@ -40,10 +45,6 @@ void __attribute__((cdecl)) kmain(multiboot_info_t *multiboot_info){
// Initialize virtual memory
i686_vmm_initialize();
// Initialize drivers
timer_initialize();
keyboard_initialize();
// Initialize FS
vfs_initialize();

Veure arxiu

@ -2,7 +2,6 @@
#include "lib/stdio.h"
#include "lib/string.h"
#include "lib/memory.h"
#include "drivers/uart.h"
int shell_run(char *cmd){
int ret = 0;
@ -17,22 +16,8 @@ int shell_run(char *cmd){
else if(!strcmp(cmd, "clear")){
clear_screen();
}
else if(!strcmp(cmd, "uart")){
uart_write(COM1, '\x1b');
uart_write(COM1, '[');
uart_write(COM1, '3');
uart_write(COM1, '2');
uart_write(COM1, 'm');
uart_write(COM1, 'H');
uart_write(COM1, 'E');
uart_write(COM1, 'L');
uart_write(COM1, 'L');
uart_write(COM1, 'O');
uart_write(COM1, '\n');
uart_write(COM1, '\x1b');
uart_write(COM1, '[');
uart_write(COM1, '0');
uart_write(COM1, 'm');
else if(!strcmp(cmd, "serial")){
serial_printf("Hello world!\n");
}
else{
printf("SHELL: Unknown command \"%s\"\n", cmd);