Changeset 40eab9f in mainline for kernel/generic/src/debug/sections.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/sections.c

    rdcd8214 r40eab9f  
    2929#include <debug/sections.h>
    3030#include <debug/register.h>
     31#include <debug.h>
    3132
    3233#include <stdio.h>
    3334#include <str.h>
    3435
    35 const void *debug_aranges = NULL;
    36 size_t debug_aranges_size = 0;
     36#define DEBUGF dummy_printf
    3737
    38 const void *debug_info = NULL;
    39 size_t debug_info_size = 0;
    40 
    41 const void *debug_abbrev = NULL;
    42 size_t debug_abbrev_size = 0;
    43 
    44 const void *debug_line = NULL;
    45 size_t debug_line_size = 0;
    46 
    47 const char *debug_str = NULL;
    48 size_t debug_str_size = 0;
    49 
    50 const char *debug_line_str = NULL;
    51 size_t debug_line_str_size = 0;
    52 
    53 const void *debug_rnglists = NULL;
    54 size_t debug_rnglists_size = 0;
    55 
    56 // TODO: get this from the program image
    57 const void *eh_frame_hdr = NULL;
    58 size_t eh_frame_hdr_size = 0;
    59 
    60 const void *eh_frame = NULL;
    61 size_t eh_frame_size = 0;
    62 
    63 const elf_symbol_t *symtab = NULL;
    64 size_t symtab_size = 0;
    65 
    66 const char *strtab = NULL;
    67 size_t strtab_size = 0;
     38debug_sections_t kernel_sections;
    6839
    6940struct section_manifest {
     
    7344};
    7445
    75 #define section(name) { "." #name, (const void **)&name, &name##_size }
     46static void _check_string_section(const char *section, size_t *section_size)
     47{
     48        while (*section_size > 0 && section[*section_size - 1] != 0) {
     49                (*section_size)--;
     50        }
     51}
    7652
    77 static const struct section_manifest manifest[] = {
    78         section(debug_aranges),
    79         section(debug_info),
    80         section(debug_abbrev),
    81         section(debug_line),
    82         section(debug_str),
    83         section(debug_line_str),
    84         section(debug_rnglists),
    85         section(eh_frame_hdr),
    86         section(eh_frame),
    87         section(symtab),
    88         section(strtab),
    89 };
     53debug_sections_t get_debug_sections(const void *elf, size_t elf_size)
     54{
     55        debug_sections_t out = { };
    9056
    91 #undef section
     57#define section(name) { "." #name, (const void **)&out.name, &out.name##_size }
    9258
    93 static const size_t manifest_len = sizeof(manifest) / sizeof(struct section_manifest);
     59        const struct section_manifest manifest[] = {
     60                section(debug_aranges),
     61                section(debug_info),
     62                section(debug_abbrev),
     63                section(debug_line),
     64                section(debug_str),
     65                section(debug_line_str),
     66                section(debug_rnglists),
     67                section(eh_frame_hdr),
     68                section(eh_frame),
     69                section(symtab),
     70                section(strtab),
     71        };
    9472
    95 void register_debug_data(const void *data, size_t data_size)
    96 {
    97         const void *data_end = data + data_size;
     73        const size_t manifest_len = sizeof(manifest) / sizeof(struct section_manifest);
     74
     75        const void *data_end = elf + elf_size;
    9876
    9977        /*
     
    10381         */
    10482
    105         if (data_size < sizeof(elf_header_t)) {
     83        if (elf_size < sizeof(elf_header_t)) {
    10684                printf("bad debug data: too short\n");
    107                 return;
     85                return out;
    10886        }
    10987
    110         if (((uintptr_t) data) % 8 != 0) {
     88        if (((uintptr_t) elf) % 8 != 0) {
    11189                printf("bad debug data: unaligned input\n");
    112                 return;
     90                return out;
    11391        }
    11492
    115         const elf_header_t *hdr = data;
     93        const elf_header_t *hdr = elf;
    11694
    11795        if (hdr->e_ident[0] != ELFMAG0 || hdr->e_ident[1] != ELFMAG1 ||
    11896            hdr->e_ident[2] != ELFMAG2 || hdr->e_ident[3] != ELFMAG3) {
    11997                printf("bad debug data: wrong ELF magic bytes\n");
    120                 return;
     98                return out;
    12199        }
    122100
    123101        if (hdr->e_shentsize != sizeof(elf_section_header_t)) {
    124102                printf("bad debug data: wrong e_shentsize\n");
    125                 return;
     103                return out;
    126104        }
    127105
    128106        /* Get section header table. */
    129         const elf_section_header_t *shdr = data + hdr->e_shoff;
     107        const elf_section_header_t *shdr = elf + hdr->e_shoff;
    130108        size_t shdr_len = hdr->e_shnum;
    131109
    132110        if ((void *) &shdr[shdr_len] > data_end) {
    133111                printf("bad debug data: truncated section header table\n");
    134                 return;
     112                return out;
    135113        }
    136114
    137115        if (hdr->e_shstrndx >= shdr_len) {
    138116                printf("bad debug data: string table index out of range\n");
    139                 return;
     117                return out;
    140118        }
    141119
    142120        /* Get data on section name string table. */
    143121        const elf_section_header_t *shstr = &shdr[hdr->e_shstrndx];
    144         const char *shstrtab = data + shstr->sh_offset;
     122        const char *shstrtab = elf + shstr->sh_offset;
    145123        size_t shstrtab_size = shstr->sh_size;
    146124
    147125        if ((void *) shstrtab + shstrtab_size > data_end) {
    148126                printf("bad debug data: truncated string table\n");
    149                 return;
     127                return out;
    150128        }
    151129
    152130        /* Check NUL-terminator. */
    153         while (shstrtab_size > 0 && shstrtab[shstrtab_size - 1] != 0) {
    154                 shstrtab_size--;
    155         }
     131        _check_string_section(shstrtab, &shstrtab_size);
    156132
    157133        if (shstrtab_size == 0) {
    158134                printf("bad debug data: empty or non-null-terminated string table\n");
    159                 return;
     135                return out;
    160136        }
    161137
     
    167143                if (shdr[i].sh_name >= shstrtab_size) {
    168144                        printf("bad debug data: string table index out of range\n");
    169                         return;
     145                        continue;
    170146                }
    171147
     
    174150                size_t size = shdr[i].sh_size;
    175151
    176                 printf("section '%s': offset %zd (%zd bytes)\n", name, offset, size);
     152                DEBUGF("section '%s': offset %zd (%zd bytes)\n", name, offset, size);
    177153
    178                 if (data + offset + size > data_end) {
     154                if (elf + offset + size > data_end) {
    179155                        printf("bad debug data: truncated section %s\n", name);
    180156                        continue;
     
    183159                for (size_t i = 0; i < manifest_len; i++) {
    184160                        if (str_cmp(manifest[i].name, name) == 0) {
    185                                 *manifest[i].address_field = data + offset;
     161                                *manifest[i].address_field = elf + offset;
    186162                                *manifest[i].size_field = size;
    187163                                break;
     
    191167
    192168        /* Check NUL-terminator in .strtab, .debug_str and .debug_line_str */
    193         while (strtab_size > 0 && strtab[strtab_size - 1] != 0) {
    194                 strtab_size--;
    195         }
     169        _check_string_section(out.strtab, &out.strtab_size);
     170        _check_string_section(out.debug_str, &out.debug_str_size);
     171        _check_string_section(out.debug_line_str, &out.debug_line_str_size);
    196172
    197         while (debug_str_size > 0 && debug_str[debug_str_size - 1] != 0) {
    198                 debug_str_size--;
    199         }
     173        return out;
     174}
    200175
    201         while (debug_line_str_size > 0 && debug_str[debug_line_str_size - 1] != 0) {
    202                 debug_line_str_size--;
    203         }
     176void register_debug_data(const void *elf, size_t elf_size)
     177{
     178        kernel_sections = get_debug_sections(elf, elf_size);
    204179}
Note: See TracChangeset for help on using the changeset viewer.