KSyms.cpp 7.0 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. namespace Kernel {
  33. FlatPtr g_lowest_kernel_symbol_address = 0xffffffff;
  34. FlatPtr g_highest_kernel_symbol_address = 0;
  35. bool g_kernel_symbols_available = false;
  36. static KernelSymbol* s_symbols;
  37. static size_t s_symbol_count = 0;
  38. static u8 parse_hex_digit(char nibble)
  39. {
  40. if (nibble >= '0' && nibble <= '9')
  41. return nibble - '0';
  42. ASSERT(nibble >= 'a' && nibble <= 'f');
  43. return 10 + (nibble - 'a');
  44. }
  45. u32 address_for_kernel_symbol(const StringView& name)
  46. {
  47. for (size_t i = 0; i < s_symbol_count; ++i) {
  48. if (!strncmp(name.characters_without_null_termination(), s_symbols[i].name, name.length()))
  49. return s_symbols[i].address;
  50. }
  51. return 0;
  52. }
  53. const KernelSymbol* symbolicate_kernel_address(u32 address)
  54. {
  55. if (address < g_lowest_kernel_symbol_address || address > g_highest_kernel_symbol_address)
  56. return nullptr;
  57. for (unsigned i = 0; i < s_symbol_count; ++i) {
  58. if (address < s_symbols[i + 1].address)
  59. return &s_symbols[i];
  60. }
  61. return nullptr;
  62. }
  63. static void load_kernel_sybols_from_data(const KBuffer& buffer)
  64. {
  65. g_lowest_kernel_symbol_address = 0xffffffff;
  66. g_highest_kernel_symbol_address = 0;
  67. auto* bufptr = (const char*)buffer.data();
  68. auto* start_of_name = bufptr;
  69. FlatPtr address = 0;
  70. for (size_t i = 0; i < 8; ++i)
  71. s_symbol_count = (s_symbol_count << 4) | parse_hex_digit(*(bufptr++));
  72. s_symbols = static_cast<KernelSymbol*>(kmalloc_eternal(sizeof(KernelSymbol) * s_symbol_count));
  73. ++bufptr; // skip newline
  74. klog() << "Loading kernel symbol table...";
  75. size_t current_symbol_index = 0;
  76. while (bufptr < buffer.end_pointer()) {
  77. for (size_t i = 0; i < 8; ++i)
  78. address = (address << 4) | parse_hex_digit(*(bufptr++));
  79. bufptr += 3;
  80. start_of_name = bufptr;
  81. while (*(++bufptr)) {
  82. if (*bufptr == '\n') {
  83. break;
  84. }
  85. }
  86. auto& ksym = s_symbols[current_symbol_index];
  87. ksym.address = address;
  88. char* name = static_cast<char*>(kmalloc_eternal((bufptr - start_of_name) + 1));
  89. memcpy(name, start_of_name, bufptr - start_of_name);
  90. name[bufptr - start_of_name] = '\0';
  91. ksym.name = name;
  92. if (ksym.address < g_lowest_kernel_symbol_address)
  93. g_lowest_kernel_symbol_address = ksym.address;
  94. if (ksym.address > g_highest_kernel_symbol_address)
  95. g_highest_kernel_symbol_address = ksym.address;
  96. ++bufptr;
  97. ++current_symbol_index;
  98. }
  99. g_kernel_symbols_available = true;
  100. }
  101. NEVER_INLINE static void dump_backtrace_impl(FlatPtr base_pointer, bool use_ksyms)
  102. {
  103. SmapDisabler disabler;
  104. #if 0
  105. if (!current) {
  106. //hang();
  107. return;
  108. }
  109. #endif
  110. if (use_ksyms && !g_kernel_symbols_available) {
  111. Processor::halt();
  112. return;
  113. }
  114. struct RecognizedSymbol {
  115. FlatPtr address;
  116. const KernelSymbol* symbol { nullptr };
  117. };
  118. size_t max_recognized_symbol_count = 256;
  119. RecognizedSymbol recognized_symbols[max_recognized_symbol_count];
  120. size_t recognized_symbol_count = 0;
  121. if (use_ksyms) {
  122. FlatPtr copied_stack_ptr[2];
  123. for (FlatPtr* stack_ptr = (FlatPtr*)base_pointer; stack_ptr && recognized_symbol_count < max_recognized_symbol_count; stack_ptr = (FlatPtr*)copied_stack_ptr[0]) {
  124. if ((FlatPtr)stack_ptr < 0xc0000000)
  125. break;
  126. void* fault_at;
  127. if (!safe_memcpy(copied_stack_ptr, stack_ptr, sizeof(copied_stack_ptr), fault_at))
  128. break;
  129. FlatPtr retaddr = copied_stack_ptr[1];
  130. recognized_symbols[recognized_symbol_count++] = { retaddr, symbolicate_kernel_address(retaddr) };
  131. }
  132. } else {
  133. void* fault_at;
  134. FlatPtr copied_stack_ptr[2];
  135. FlatPtr* stack_ptr = (FlatPtr*)base_pointer;
  136. while (stack_ptr && safe_memcpy(copied_stack_ptr, stack_ptr, sizeof(copied_stack_ptr), fault_at)) {
  137. FlatPtr retaddr = copied_stack_ptr[1];
  138. dbgln("{:p} (next: {:p})", retaddr, stack_ptr ? (u32*)copied_stack_ptr[0] : 0);
  139. stack_ptr = (FlatPtr*)copied_stack_ptr[0];
  140. }
  141. return;
  142. }
  143. ASSERT(recognized_symbol_count <= max_recognized_symbol_count);
  144. for (size_t i = 0; i < recognized_symbol_count; ++i) {
  145. auto& symbol = recognized_symbols[i];
  146. if (!symbol.address)
  147. break;
  148. if (!symbol.symbol) {
  149. dbgln("{:p}", symbol.address);
  150. continue;
  151. }
  152. size_t offset = symbol.address - symbol.symbol->address;
  153. if (symbol.symbol->address == g_highest_kernel_symbol_address && offset > 4096)
  154. dbgln("{:p}", symbol.address);
  155. else
  156. dbgln("{:p} {} +0x{:x}", symbol.address, demangle(symbol.symbol->name), offset);
  157. }
  158. }
  159. void dump_backtrace()
  160. {
  161. static bool in_dump_backtrace = false;
  162. if (in_dump_backtrace)
  163. return;
  164. TemporaryChange change(in_dump_backtrace, true);
  165. TemporaryChange disable_kmalloc_stacks(g_dump_kmalloc_stacks, false);
  166. FlatPtr ebp;
  167. asm volatile("movl %%ebp, %%eax"
  168. : "=a"(ebp));
  169. dump_backtrace_impl(ebp, g_kernel_symbols_available);
  170. }
  171. void load_kernel_symbol_table()
  172. {
  173. auto result = VFS::the().open("/res/kernel.map", O_RDONLY, 0, VFS::the().root_custody());
  174. if (!result.is_error()) {
  175. auto description = result.value();
  176. auto buffer = description->read_entire_file();
  177. if (!buffer.is_error())
  178. load_kernel_sybols_from_data(*buffer.value());
  179. }
  180. }
  181. }