zephyr/subsys/debug/symtab/symtab.c
Yong Cong Sin e1ce0aefff debug: implement symtab generation
Use pyelftools to extract the symbol table from the
link stage executable. Then, filter out the function names
and sort them based on their offsets before writing into the
`symtab.c`, this is similar to how the `isr_tables` works.

To access the structure, simply include the new header:
```c
#include <zephyr/debug/symtab.h>
```

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-05-23 11:52:08 -04:00

47 lines
1.0 KiB
C

/*
* Copyright (c) 2024 Meta Platforms
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <zephyr/debug/symtab.h>
const struct symtab_info *const symtab_get(void)
{
extern const struct symtab_info z_symtab;
return &z_symtab;
}
const char *const symtab_find_symbol_name(uintptr_t addr, uint32_t *offset)
{
const struct symtab_info *const symtab = symtab_get();
const uint32_t symbol_offset = addr - symtab->start_addr;
uint32_t left = 0, right = symtab->length - 1;
uint32_t ret_offset = 0;
const char *ret_name = "?";
while (left <= right) {
uint32_t mid = left + (right - left) / 2;
if ((symbol_offset >= symtab->entries[mid].offset) &&
(symbol_offset < symtab->entries[mid + 1].offset)) {
ret_offset = symbol_offset - symtab->entries[mid].offset;
ret_name = symtab->entries[mid].name;
break;
} else if (symbol_offset < symtab->entries[mid].offset) {
right = mid - 1;
} else {
left = mid + 1;
}
}
if (offset != NULL) {
*offset = ret_offset;
}
return ret_name;
}