KSyms.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "KSyms.h"
  2. #include "Process.h"
  3. #include "Scheduler.h"
  4. #include <AK/ELF/ELFLoader.h>
  5. #include <AK/TemporaryChange.h>
  6. #include <Kernel/FileSystem/FileDescription.h>
  7. static KSym* s_ksyms;
  8. u32 ksym_lowest_address;
  9. u32 ksym_highest_address;
  10. u32 ksym_count;
  11. bool ksyms_ready;
  12. static u8 parse_hex_digit(char nibble)
  13. {
  14. if (nibble >= '0' && nibble <= '9')
  15. return nibble - '0';
  16. ASSERT(nibble >= 'a' && nibble <= 'f');
  17. return 10 + (nibble - 'a');
  18. }
  19. const KSym* ksymbolicate(u32 address)
  20. {
  21. if (address < ksym_lowest_address || address > ksym_highest_address)
  22. return nullptr;
  23. for (unsigned i = 0; i < ksym_count; ++i) {
  24. if (address < s_ksyms[i + 1].address)
  25. return &s_ksyms[i];
  26. }
  27. return nullptr;
  28. }
  29. static void load_ksyms_from_data(const ByteBuffer& buffer)
  30. {
  31. ksym_lowest_address = 0xffffffff;
  32. ksym_highest_address = 0;
  33. auto* bufptr = (const char*)buffer.data();
  34. auto* start_of_name = bufptr;
  35. u32 address = 0;
  36. for (unsigned i = 0; i < 8; ++i)
  37. ksym_count = (ksym_count << 4) | parse_hex_digit(*(bufptr++));
  38. s_ksyms = static_cast<KSym*>(kmalloc_eternal(sizeof(KSym) * ksym_count));
  39. ++bufptr; // skip newline
  40. kprintf("Loading ksyms...");
  41. unsigned current_ksym_index = 0;
  42. while (bufptr < buffer.end_pointer()) {
  43. for (unsigned i = 0; i < 8; ++i)
  44. address = (address << 4) | parse_hex_digit(*(bufptr++));
  45. bufptr += 3;
  46. start_of_name = bufptr;
  47. while (*(++bufptr)) {
  48. if (*bufptr == '\n') {
  49. break;
  50. }
  51. }
  52. auto& ksym = s_ksyms[current_ksym_index];
  53. ksym.address = address;
  54. char* name = static_cast<char*>(kmalloc_eternal((bufptr - start_of_name) + 1));
  55. memcpy(name, start_of_name, bufptr - start_of_name);
  56. name[bufptr - start_of_name] = '\0';
  57. ksym.name = name;
  58. if (ksym.address < ksym_lowest_address)
  59. ksym_lowest_address = ksym.address;
  60. if (ksym.address > ksym_highest_address)
  61. ksym_highest_address = ksym.address;
  62. ++bufptr;
  63. ++current_ksym_index;
  64. }
  65. kprintf("ok\n");
  66. ksyms_ready = true;
  67. }
  68. [[gnu::noinline]] void dump_backtrace_impl(u32 ebp, bool use_ksyms)
  69. {
  70. if (!current) {
  71. //hang();
  72. return;
  73. }
  74. if (use_ksyms && !ksyms_ready) {
  75. hang();
  76. return;
  77. }
  78. struct RecognizedSymbol {
  79. u32 address;
  80. const KSym* ksym;
  81. };
  82. int max_recognized_symbol_count = 256;
  83. RecognizedSymbol recognized_symbols[max_recognized_symbol_count];
  84. int recognized_symbol_count = 0;
  85. if (use_ksyms) {
  86. for (u32* stack_ptr = (u32*)ebp; current->process().validate_read_from_kernel(VirtualAddress((u32)stack_ptr)) && recognized_symbol_count < max_recognized_symbol_count; stack_ptr = (u32*)*stack_ptr) {
  87. u32 retaddr = stack_ptr[1];
  88. recognized_symbols[recognized_symbol_count++] = { retaddr, ksymbolicate(retaddr) };
  89. }
  90. } else {
  91. for (u32* stack_ptr = (u32*)ebp; current->process().validate_read_from_kernel(VirtualAddress((u32)stack_ptr)); stack_ptr = (u32*)*stack_ptr) {
  92. u32 retaddr = stack_ptr[1];
  93. dbgprintf("%x (next: %x)\n", retaddr, stack_ptr ? (u32*)*stack_ptr : 0);
  94. }
  95. return;
  96. }
  97. ASSERT(recognized_symbol_count <= max_recognized_symbol_count);
  98. size_t bytes_needed = 0;
  99. for (int i = 0; i < recognized_symbol_count; ++i) {
  100. auto& symbol = recognized_symbols[i];
  101. bytes_needed += (symbol.ksym ? strlen(symbol.ksym->name) : 0) + 8 + 16;
  102. }
  103. for (int i = 0; i < recognized_symbol_count; ++i) {
  104. auto& symbol = recognized_symbols[i];
  105. if (!symbol.address)
  106. break;
  107. if (!symbol.ksym) {
  108. if (current->process().elf_loader() && current->process().elf_loader()->has_symbols()) {
  109. dbgprintf("%p %s\n", symbol.address, current->process().elf_loader()->symbolicate(symbol.address).characters());
  110. } else {
  111. dbgprintf("%p (no ELF symbols for process)\n", symbol.address);
  112. }
  113. continue;
  114. }
  115. unsigned offset = symbol.address - symbol.ksym->address;
  116. if (symbol.ksym->address == ksym_highest_address && offset > 4096)
  117. dbgprintf("%p\n", symbol.address);
  118. else
  119. dbgprintf("%p %s +%u\n", symbol.address, symbol.ksym->name, offset);
  120. }
  121. }
  122. void dump_backtrace()
  123. {
  124. static bool in_dump_backtrace = false;
  125. if (in_dump_backtrace) {
  126. dbgprintf("dump_backtrace() called from within itself, what the hell is going on!\n");
  127. return;
  128. }
  129. TemporaryChange change(in_dump_backtrace, true);
  130. TemporaryChange disable_kmalloc_stacks(g_dump_kmalloc_stacks, false);
  131. u32 ebp;
  132. asm volatile("movl %%ebp, %%eax"
  133. : "=a"(ebp));
  134. dump_backtrace_impl(ebp, ksyms_ready);
  135. }
  136. void init_ksyms()
  137. {
  138. ksyms_ready = false;
  139. ksym_lowest_address = 0xffffffff;
  140. ksym_highest_address = 0;
  141. ksym_count = 0;
  142. }
  143. void load_ksyms()
  144. {
  145. auto result = VFS::the().open("/kernel.map", 0, 0, VFS::the().root_custody());
  146. ASSERT(!result.is_error());
  147. auto description = result.value();
  148. auto buffer = description->read_entire_file();
  149. ASSERT(buffer);
  150. load_ksyms_from_data(buffer);
  151. }