Changeset 40eab9f in mainline for kernel/generic/src/debug/symtab.c


Ignore:
Timestamp:
2023-11-03T18:47:41Z (15 months ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master, topic/msim-upgrade, topic/simplify-dev-export
Children:
b1397ab
Parents:
dcd8214
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2023-11-03 18:46:22)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2023-11-03 18:47:41)
Message:

Print symbol names and line numbers in stacktraces for init tasks

Only useful in select few situations, but useful nonetheless.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/debug/symtab.c

    rdcd8214 r40eab9f  
    3636 */
    3737
     38#include <abi/elf.h>
     39#include <byteorder.h>
     40#include <console/prompt.h>
     41#include <debug/sections.h>
     42#include <errno.h>
     43#include <proc/task.h>
     44#include <stdio.h>
     45#include <str.h>
    3846#include <symtab.h>
    39 #include <byteorder.h>
    40 #include <str.h>
    41 #include <stdio.h>
    4247#include <typedefs.h>
    43 #include <errno.h>
    44 #include <console/prompt.h>
    45 
    46 #include <abi/elf.h>
    47 #include <debug/sections.h>
    48 
    49 static inline size_t symtab_len()
    50 {
    51         return symtab_size / sizeof(elf_symbol_t);
    52 }
    53 
    54 static inline const char *symtab_entry_name(int entry)
    55 {
    56         size_t index = symtab[entry].st_name;
    57 
    58         if (index >= strtab_size)
    59                 return NULL;
    60 
    61         return strtab + index;
    62 }
    63 
    64 static inline size_t symtab_next(size_t i)
    65 {
    66         for (; i < symtab_len(); i++) {
    67                 const char *name = symtab_entry_name(i);
    68                 int st_bind = elf_st_bind(symtab[i].st_info);
    69                 int st_type = elf_st_type(symtab[i].st_info);
     48
     49static inline const char *symtab_entry_name(debug_sections_t *scs, int entry)
     50{
     51        size_t index = scs->symtab[entry].st_name;
     52
     53        if (index >= scs->strtab_size)
     54                return NULL;
     55
     56        return scs->strtab + index;
     57}
     58
     59static inline size_t symtab_next(debug_sections_t *scs, size_t i)
     60{
     61        size_t symtab_len = scs->symtab_size / sizeof(elf_symbol_t);
     62
     63        for (; i < symtab_len; i++) {
     64                const char *name = symtab_entry_name(scs, i);
     65                int st_bind = elf_st_bind(scs->symtab[i].st_info);
     66                int st_type = elf_st_type(scs->symtab[i].st_info);
    7067
    7168                if (st_bind == STB_LOCAL)
     
    8279}
    8380
    84 const char *symtab_name_lookup(uintptr_t addr, uintptr_t *symbol_addr)
    85 {
     81const char *symtab_name_lookup(uintptr_t addr, uintptr_t *symbol_addr, debug_sections_t *scs)
     82{
     83        const elf_symbol_t *symtab = scs->symtab;
     84        size_t symtab_len = scs->symtab_size / sizeof(elf_symbol_t);
     85        const char *strtab = scs->strtab;
     86        size_t strtab_size = scs->strtab_size;
     87
    8688        if (symtab == NULL || strtab == NULL)
    8789                return NULL;
     
    9092        uintptr_t closest_symbol_name = 0;
    9193
    92         for (size_t i = symtab_next(0); i < symtab_len(); i = symtab_next(i + 1)) {
     94        for (size_t i = symtab_next(scs, 0); i < symtab_len; i = symtab_next(scs, i + 1)) {
    9395                if (symtab[i].st_value > addr)
    9496                        continue;
     
    132134const char *symtab_fmt_name_lookup(uintptr_t addr)
    133135{
    134         const char *name = symtab_name_lookup(addr, NULL);
     136        const char *name = symtab_name_lookup(addr, NULL, &kernel_sections);
    135137        if (name == NULL)
    136138                name = "<unknown>";
     
    150152errno_t symtab_addr_lookup(const char *name, uintptr_t *addr)
    151153{
    152         for (size_t i = symtab_next(0); i < symtab_len(); i = symtab_next(i + 1)) {
    153                 if (str_cmp(name, symtab_entry_name(i)) == 0) {
    154                         *addr = symtab[i].st_value;
     154        debug_sections_t *scs = &kernel_sections;
     155        size_t symtab_len = scs->symtab_size / sizeof(elf_symbol_t);
     156
     157        for (size_t i = symtab_next(scs, 0); i < symtab_len; i = symtab_next(scs, i + 1)) {
     158                if (str_cmp(name, symtab_entry_name(scs, i)) == 0) {
     159                        *addr = scs->symtab[i].st_value;
    155160                        return EOK;
    156161                }
     
    163168void symtab_print_search(const char *name)
    164169{
    165         if (symtab == NULL || strtab == NULL) {
     170        debug_sections_t *scs = &kernel_sections;
     171        size_t symtab_len = scs->symtab_size / sizeof(elf_symbol_t);
     172
     173        if (scs->symtab == NULL || scs->strtab == NULL) {
    166174                printf("No symbol information available.\n");
    167175                return;
     
    170178        size_t namelen = str_length(name);
    171179
    172         for (size_t i = symtab_next(0); i < symtab_len(); i = symtab_next(i + 1)) {
    173                 const char *n = symtab_entry_name(i);
     180        for (size_t i = symtab_next(scs, 0); i < symtab_len; i = symtab_next(scs, i + 1)) {
     181                const char *n = symtab_entry_name(scs, i);
    174182
    175183                if (str_lcmp(name, n, namelen) == 0) {
    176                         printf("%p: %s\n", (void *) symtab[i].st_value, n);
     184                        printf("%p: %s\n", (void *) scs->symtab[i].st_value, n);
    177185                }
    178186        }
     
    182190const char *symtab_hints_enum(const char *input, const char **help, void **ctx)
    183191{
    184         if (symtab == NULL || strtab == NULL)
     192        debug_sections_t *scs = &kernel_sections;
     193        size_t symtab_len = scs->symtab_size / sizeof(elf_symbol_t);
     194
     195        if (scs->symtab == NULL || scs->strtab == NULL)
    185196                return NULL;
    186197
     
    189200
    190201        size_t len = str_length(input);
    191         for (size_t i = symtab_next((size_t) *ctx); i < symtab_len(); i = symtab_next(i + 1)) {
    192                 const char *curname = symtab_entry_name(i);
     202        for (size_t i = symtab_next(scs, (size_t) *ctx); i < symtab_len; i = symtab_next(scs, i + 1)) {
     203                const char *curname = symtab_entry_name(scs, i);
    193204
    194205                if (str_lcmp(input, curname, len) == 0) {
Note: See TracChangeset for help on using the changeset viewer.