Refactor to use the print function, at byte formatter, at memdump cmd

This commit is contained in:
Maxime Bloch 2020-01-28 23:10:35 +01:00
parent 831b49d8dd
commit 5c8f8defe0
No known key found for this signature in database
GPG key ID: CE32A7D95B7D6418
3 changed files with 81 additions and 60 deletions

View file

@ -68,7 +68,8 @@ void free(void *data) {
} }
void print_memory() { void print_memory() {
terminal_write("==== MEM DUMP ====\n", 19); print("____________________\n");
print(" === MEM DUMP === \n");
page_tag *curr_page = start; page_tag *curr_page = start;
int i = 0; int i = 0;
while (curr_page != NULL) { while (curr_page != NULL) {
@ -79,7 +80,7 @@ void print_memory() {
void *empty_start = (void *) curr_page + sizeof(page_tag) + curr_page->size; void *empty_start = (void *) curr_page + sizeof(page_tag) + curr_page->size;
if (empty_start + sizeof(page_tag) < (void *) curr_page->next) { if (empty_start + sizeof(page_tag) < (void *) curr_page->next) {
print("_: empty_page (%d, %d)\n", print("_: empty page (%d, %d)\n",
sizeof(page_tag), sizeof(page_tag),
(void *) curr_page->next - (void *) curr_page->next -
(empty_start + sizeof(page_tag))); (empty_start + sizeof(page_tag)));
@ -90,8 +91,13 @@ void print_memory() {
curr_page = curr_page->next; curr_page = curr_page->next;
i += 1; i += 1;
} }
terminal_write("------------------\n\n", 20); print("____________________\n");
} }
int command_mem_dump(char*string){
print_memory();
}
// //
//void test_allocs() { //void test_allocs() {
// void *ptr0 = __alloc(64); // void *ptr0 = __alloc(64);

View file

@ -8,6 +8,7 @@
#include "terminal.c" #include "terminal.c"
#include "inline_asm.c" #include "inline_asm.c"
#include "drivers/keyboard/keyboard.c" #include "drivers/keyboard/keyboard.c"
#include "memory.c"
char buffer[SHELL_CMD_BUFFER_SIZE]; char buffer[SHELL_CMD_BUFFER_SIZE];
int buffer_idx = 0; int buffer_idx = 0;
@ -48,63 +49,61 @@ int get_gdt(char* unused) {
//terminal_writestring("\nEntry "); //terminal_writestring("\nEntry ");
//terminal_writeint(entry_num, 10); //terminal_writeint(entry_num, 10);
terminal_writestring("base = 0x"); print("base = %x\n", base);
terminal_writeint(base, 16); print("limit = %x\n", limit);
terminal_writestring("\nlimit = 0x"); print("flags = %b\n", (entry.flags_limit_higher >> 4));
terminal_writeint(limit, 16);
terminal_writestring("\nflags = 0b");
terminal_writeint((entry.flags_limit_higher >> 4), 2);
if ((flags & 0b1000) == 0) { if ((flags & 0b1000) == 0) {
terminal_writestring(" (byte granularity"); print(" (byte granularity");
} else { } else {
terminal_writestring(" (page granularity"); print(" (page granularity");
} }
if ((flags & 0b0100) == 0) { if ((flags & 0b0100) == 0) {
terminal_writestring(", 16 bit)"); print(", 16 bit)");
} else { } else {
terminal_writestring(", 32 bit)"); print(", 32 bit)");
} }
print("\n");
terminal_writestring("\naccess = 0b"); print("access = %b (ring %d",
terminal_writeint(entry.access_byte, 2); entry.access_byte,
terminal_writestring(" (ring "); (entry.access_byte & 0b01100000) >> 5
terminal_writeint((entry.access_byte & 0b01100000) >> 5, 10); );
if ((entry.access_byte & 0b00010000) == 0) { if ((entry.access_byte & 0b00010000) == 0) {
terminal_writestring(", System"); print(", System");
} }
if (is_data) { if (is_data) {
terminal_writestring(", Data"); print(", Data");
if ((entry.access_byte & 0b00000100) == 0) { if ((entry.access_byte & 0b00000100) == 0) {
terminal_writestring(" (growing up, "); print(" (growing up, ");
} else { } else {
terminal_writestring(" (growing down, "); print(" (growing down, ");
} }
if ((entry.access_byte & 0b00000010) == 0) { if ((entry.access_byte & 0b00000010) == 0) {
terminal_writestring("r--)"); print("r--)");
} else { } else {
terminal_writestring("rw-)"); print("rw-)");
} }
} else { } else {
terminal_writestring(", Code"); print(", Code");
if ((entry.access_byte & 0b00000100) == 0) { if ((entry.access_byte & 0b00000100) == 0) {
terminal_writestring(" (non-conforming, "); print(" (non-conforming, ");
} else { } else {
terminal_writestring(" (conforming, "); print(" (conforming, ");
} }
if ((entry.access_byte & 0b00000010) == 0) { if ((entry.access_byte & 0b00000010) == 0) {
terminal_writestring("--x)"); print("--x)");
} else { } else {
terminal_writestring("r-x)"); print("r-x)");
} }
} }
terminal_writestring(")\n"); print(")\n");
} }
return 0; return 0;
} }
@ -118,6 +117,10 @@ int ree(char* unused) {
return 0; return 0;
} }
int save_text(char *text) {
}
// TODO This is ugly, fix this // TODO This is ugly, fix this
const char *shell_commands_strings[] = { const char *shell_commands_strings[] = {
"echo", "echo",
@ -125,6 +128,7 @@ const char* shell_commands_strings[] = {
"cls", "cls",
"ree", "ree",
"getgdt", "getgdt",
"memdump",
NULL NULL
}; };
@ -133,7 +137,8 @@ int (*shell_commands_functions[]) (char*) = {
hello, hello,
cls, cls,
ree, ree,
get_gdt get_gdt,
command_mem_dump,
}; };
int run_command(char *buffer) { int run_command(char *buffer) {
@ -176,7 +181,7 @@ void shell_step() {
buffer_idx = 0; buffer_idx = 0;
if (result == -1) { if (result == -1) {
terminal_writestring("No such command\n"); print("No such command\n");
} }
} else if (curr_char == 0x08) { } else if (curr_char == 0x08) {
if (buffer_idx != 0) { if (buffer_idx != 0) {

View file

@ -14,6 +14,7 @@
* - %c: character * - %c: character
* - %d: digit (integer) * - %d: digit (integer)
* - %x: hexadecimal value. Can be used to print pointers to * - %x: hexadecimal value. Can be used to print pointers to
* - %b: binary value. Digits with base 2
* @param fmt Formatter string * @param fmt Formatter string
* @param ... Variable amount of arguments to be inserted * @param ... Variable amount of arguments to be inserted
*/ */
@ -46,10 +47,19 @@ void print(const char *fmt, ...) {
terminal_writestring(s); terminal_writestring(s);
break; break;
case 'x': case 'x':
terminal_writestring("0x");
i = va_arg(argp, int); i = va_arg(argp, int);
s = itoa(i, fmtbuf, 16); s = itoa(i, fmtbuf, 16);
terminal_writestring(s); terminal_writestring(s);
break; break;
case 'b':
terminal_writestring("0b");
i = va_arg(argp, int);
s = itoa(i, fmtbuf, 2);
terminal_writestring(s);
break;
case '%': case '%':
terminal_putchar('%'); terminal_putchar('%');
break; break;