KSyms.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Demangle.h>
  27. #include <AK/TemporaryChange.h>
  28. #include <Kernel/FileSystem/FileDescription.h>
  29. #include <Kernel/KSyms.h>
  30. #include <Kernel/Process.h>
  31. #include <Kernel/Scheduler.h>
  32. #include <LibELF/Loader.h>
  33. namespace Kernel {
  34. FlatPtr g_lowest_kernel_symbol_address = 0xffffffff;
  35. FlatPtr g_highest_kernel_symbol_address = 0;
  36. bool g_kernel_symbols_available = false;
  37. static KernelSymbol* s_symbols;
  38. static size_t s_symbol_count = 0;
  39. static u8 parse_hex_digit(char nibble)
  40. {
  41. if (nibble >= '0' && nibble <= '9')
  42. return nibble - '0';
  43. ASSERT(nibble >= 'a' && nibble <= 'f');
  44. return 10 + (nibble - 'a');
  45. }
  46. u32 address_for_kernel_symbol(const StringView& name)
  47. {
  48. for (size_t i = 0; i < s_symbol_count; ++i) {
  49. if (!strncmp(name.characters_without_null_termination(), s_symbols[i].name, name.length()))
  50. return s_symbols[i].address;
  51. }
  52. return 0;
  53. }
  54. const KernelSymbol* symbolicate_kernel_address(u32 address)
  55. {
  56. if (address < g_lowest_kernel_symbol_address || address > g_highest_kernel_symbol_address)
  57. return nullptr;
  58. for (unsigned i = 0; i < s_symbol_count; ++i) {
  59. if (address < s_symbols[i + 1].address)
  60. return &s_symbols[i];
  61. }
  62. return nullptr;
  63. }
  64. static void load_kernel_sybols_from_data(const KBuffer& buffer)
  65. {
  66. g_lowest_kernel_symbol_address = 0xffffffff;
  67. g_highest_kernel_symbol_address = 0;
  68. auto* bufptr = (const char*)buffer.data();
  69. auto* start_of_name = bufptr;
  70. FlatPtr address = 0;
  71. for (size_t i = 0; i < 8; ++i)
  72. s_symbol_count = (s_symbol_count << 4) | parse_hex_digit(*(bufptr++));
  73. s_symbols = static_cast<KernelSymbol*>(kmalloc_eternal(sizeof(KernelSymbol) * s_symbol_count));
  74. ++bufptr; // skip newline
  75. klog() << "Loading kernel symbol table...";
  76. size_t current_symbol_index = 0;
  77. while (bufptr < buffer.end_pointer()) {
  78. for (size_t i = 0; i < 8; ++i)
  79. address = (address << 4) | parse_hex_digit(*(bufptr++));
  80. bufptr += 3;
  81. start_of_name = bufptr;
  82. while (*(++bufptr)) {
  83. if (*bufptr == '\n') {
  84. break;
  85. }
  86. }
  87. auto& ksym = s_symbols[current_symbol_index];
  88. ksym.address = address;
  89. char* name = static_cast<char*>(kmalloc_eternal((bufptr - start_of_name) + 1));
  90. memcpy(name, start_of_name, bufptr - start_of_name);
  91. name[bufptr - start_of_name] = '\0';
  92. ksym.name = name;
  93. if (ksym.address < g_lowest_kernel_symbol_address)
  94. g_lowest_kernel_symbol_address = ksym.address;
  95. if (ksym.address > g_highest_kernel_symbol_address)
  96. g_highest_kernel_symbol_address = ksym.address;
  97. ++bufptr;
  98. ++current_symbol_index;
  99. }
  100. g_kernel_symbols_available = true;
  101. }
  102. NEVER_INLINE void dump_backtrace_impl(FlatPtr base_pointer, bool use_ksyms)
  103. {
  104. SmapDisabler disabler;
  105. #if 0
  106. if (!current) {
  107. //hang();
  108. return;
  109. }
  110. #endif
  111. if (use_ksyms && !g_kernel_symbols_available) {
  112. Processor::halt();
  113. return;
  114. }
  115. OwnPtr<Process::ELFBundle> elf_bundle;
  116. auto current_process = Process::current();
  117. if (current_process)
  118. elf_bundle = current_process->elf_bundle();
  119. struct RecognizedSymbol {
  120. FlatPtr address;
  121. const KernelSymbol* symbol { nullptr };
  122. };
  123. size_t max_recognized_symbol_count = 256;
  124. RecognizedSymbol recognized_symbols[max_recognized_symbol_count];
  125. size_t recognized_symbol_count = 0;
  126. if (use_ksyms) {
  127. for (FlatPtr* stack_ptr = (FlatPtr*)base_pointer;
  128. (current_process ? current_process->validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(void*) * 2) : 1) && recognized_symbol_count < max_recognized_symbol_count; stack_ptr = (FlatPtr*)*stack_ptr) {
  129. FlatPtr retaddr = stack_ptr[1];
  130. recognized_symbols[recognized_symbol_count++] = { retaddr, symbolicate_kernel_address(retaddr) };
  131. }
  132. } else {
  133. for (FlatPtr* stack_ptr = (FlatPtr*)base_pointer;
  134. (current_process ? current_process->validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(void*) * 2) : 1); stack_ptr = (FlatPtr*)*stack_ptr) {
  135. FlatPtr retaddr = stack_ptr[1];
  136. dbg() << String::format("%x", retaddr) << " (next: " << String::format("%x", (stack_ptr ? (u32*)*stack_ptr : 0)) << ")";
  137. }
  138. return;
  139. }
  140. ASSERT(recognized_symbol_count <= max_recognized_symbol_count);
  141. for (size_t i = 0; i < recognized_symbol_count; ++i) {
  142. auto& symbol = recognized_symbols[i];
  143. if (!symbol.address)
  144. break;
  145. if (!symbol.symbol) {
  146. if (elf_bundle && elf_bundle->elf_loader->has_symbols()) {
  147. dbg() << String::format("%p", symbol.address) << " " << elf_bundle->elf_loader->symbolicate(symbol.address);
  148. } else {
  149. dbg() << String::format("%p", symbol.address) << " (no ELF symbols for process)";
  150. }
  151. continue;
  152. }
  153. size_t offset = symbol.address - symbol.symbol->address;
  154. if (symbol.symbol->address == g_highest_kernel_symbol_address && offset > 4096)
  155. dbg() << String::format("%p", symbol.address);
  156. else
  157. dbg() << String::format("%p", symbol.address) << " " << demangle(symbol.symbol->name) << " +" << offset;
  158. }
  159. }
  160. void dump_backtrace()
  161. {
  162. static bool in_dump_backtrace = false;
  163. if (in_dump_backtrace)
  164. return;
  165. TemporaryChange change(in_dump_backtrace, true);
  166. TemporaryChange disable_kmalloc_stacks(g_dump_kmalloc_stacks, false);
  167. FlatPtr ebp;
  168. asm volatile("movl %%ebp, %%eax"
  169. : "=a"(ebp));
  170. dump_backtrace_impl(ebp, g_kernel_symbols_available);
  171. }
  172. void load_kernel_symbol_table()
  173. {
  174. auto result = VFS::the().open("/res/kernel.map", O_RDONLY, 0, VFS::the().root_custody());
  175. ASSERT(!result.is_error());
  176. auto description = result.value();
  177. auto buffer = description->read_entire_file();
  178. ASSERT(!buffer.is_error());
  179. load_kernel_sybols_from_data(buffer.value());
  180. }
  181. }