We can fetch a symbol's name from its address!!

This commit is contained in:
apio 2022-09-19 20:54:05 +02:00
parent d79596f21b
commit 9d3030763b
2 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,4 @@
#pragma once
#include <stdint.h>
void get_symbol_name(uintptr_t address, char* buffer);

View File

@ -0,0 +1,38 @@
#include "trace/Resolve.h"
#include "init/InitRD.h"
#include "std/stdlib.h"
#include "std/string.h"
#include <stddef.h>
extern int kernel_start;
extern int kernel_end;
static InitRD::File symbol_map = {"", 0, 0, 0};
static size_t symbol_strlen(const char* symbol)
{
const char* i = symbol;
for (; *i != '\n'; ++i)
;
return (i - symbol);
}
void get_symbol_name(uintptr_t address, char* buffer)
{
if (!symbol_map.addr) { symbol_map = InitRD::open("sys/moon.sym"); }
while (address >= (uintptr_t)&kernel_start && address <= (uintptr_t)&kernel_end)
{
char addr_as_str[60];
ultoa(address, addr_as_str, 16);
char* symbol = strstr((char*)symbol_map.addr, addr_as_str);
if (symbol)
{
symbol += 19;
size_t symlen = symbol_strlen(symbol);
memcpy(buffer, symbol, symlen);
buffer[symlen] = 0;
return;
}
address--;
}
}