KSyms.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/ELFLoader.h>
  33. static KSym* s_ksyms;
  34. u32 ksym_lowest_address = 0xffffffff;
  35. u32 ksym_highest_address = 0;
  36. u32 ksym_count = 0;
  37. bool ksyms_ready = false;
  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 (unsigned i = 0; i < ksym_count; ++i) {
  48. if (!strncmp(name.characters_without_null_termination(), s_ksyms[i].name, name.length()))
  49. return s_ksyms[i].address;
  50. }
  51. return 0;
  52. }
  53. const KSym* ksymbolicate(u32 address)
  54. {
  55. if (address < ksym_lowest_address || address > ksym_highest_address)
  56. return nullptr;
  57. for (unsigned i = 0; i < ksym_count; ++i) {
  58. if (address < s_ksyms[i + 1].address)
  59. return &s_ksyms[i];
  60. }
  61. return nullptr;
  62. }
  63. static void load_ksyms_from_data(const ByteBuffer& buffer)
  64. {
  65. ksym_lowest_address = 0xffffffff;
  66. ksym_highest_address = 0;
  67. auto* bufptr = (const char*)buffer.data();
  68. auto* start_of_name = bufptr;
  69. u32 address = 0;
  70. for (unsigned i = 0; i < 8; ++i)
  71. ksym_count = (ksym_count << 4) | parse_hex_digit(*(bufptr++));
  72. s_ksyms = static_cast<KSym*>(kmalloc_eternal(sizeof(KSym) * ksym_count));
  73. ++bufptr; // skip newline
  74. kprintf("Loading ksyms...");
  75. unsigned current_ksym_index = 0;
  76. while (bufptr < buffer.end_pointer()) {
  77. for (unsigned 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_ksyms[current_ksym_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 < ksym_lowest_address)
  93. ksym_lowest_address = ksym.address;
  94. if (ksym.address > ksym_highest_address)
  95. ksym_highest_address = ksym.address;
  96. ++bufptr;
  97. ++current_ksym_index;
  98. }
  99. kprintf("ok\n");
  100. ksyms_ready = true;
  101. }
  102. [[gnu::noinline]] void dump_backtrace_impl(u32 ebp, bool use_ksyms)
  103. {
  104. SmapDisabler disabler;
  105. #if 0
  106. if (!current) {
  107. //hang();
  108. return;
  109. }
  110. #endif
  111. if (use_ksyms && !ksyms_ready) {
  112. hang();
  113. return;
  114. }
  115. struct RecognizedSymbol {
  116. u32 address;
  117. const KSym* ksym;
  118. };
  119. int max_recognized_symbol_count = 256;
  120. RecognizedSymbol recognized_symbols[max_recognized_symbol_count];
  121. int recognized_symbol_count = 0;
  122. if (use_ksyms) {
  123. for (u32* stack_ptr = (u32*)ebp;
  124. (current ? current->process().validate_read_from_kernel(VirtualAddress((uintptr_t)stack_ptr), sizeof(void*) * 2) : 1) && recognized_symbol_count < max_recognized_symbol_count; stack_ptr = (u32*)*stack_ptr) {
  125. u32 retaddr = stack_ptr[1];
  126. recognized_symbols[recognized_symbol_count++] = { retaddr, ksymbolicate(retaddr) };
  127. }
  128. } else {
  129. for (u32* stack_ptr = (u32*)ebp;
  130. (current ? current->process().validate_read_from_kernel(VirtualAddress((uintptr_t)stack_ptr), sizeof(void*) * 2) : 1); stack_ptr = (u32*)*stack_ptr) {
  131. u32 retaddr = stack_ptr[1];
  132. dbgprintf("%x (next: %x)\n", retaddr, stack_ptr ? (u32*)*stack_ptr : 0);
  133. }
  134. return;
  135. }
  136. ASSERT(recognized_symbol_count <= max_recognized_symbol_count);
  137. for (int i = 0; i < recognized_symbol_count; ++i) {
  138. auto& symbol = recognized_symbols[i];
  139. if (!symbol.address)
  140. break;
  141. if (!symbol.ksym) {
  142. if (current && current->process().elf_loader() && current->process().elf_loader()->has_symbols()) {
  143. dbgprintf("%p %s\n", symbol.address, current->process().elf_loader()->symbolicate(symbol.address).characters());
  144. } else {
  145. dbgprintf("%p (no ELF symbols for process)\n", symbol.address);
  146. }
  147. continue;
  148. }
  149. unsigned offset = symbol.address - symbol.ksym->address;
  150. if (symbol.ksym->address == ksym_highest_address && offset > 4096)
  151. dbgprintf("%p\n", symbol.address);
  152. else
  153. dbgprintf("%p %s +%u\n", symbol.address, demangle(symbol.ksym->name).characters(), offset);
  154. }
  155. }
  156. void dump_backtrace()
  157. {
  158. static bool in_dump_backtrace = false;
  159. if (in_dump_backtrace)
  160. return;
  161. TemporaryChange change(in_dump_backtrace, true);
  162. TemporaryChange disable_kmalloc_stacks(g_dump_kmalloc_stacks, false);
  163. u32 ebp;
  164. asm volatile("movl %%ebp, %%eax"
  165. : "=a"(ebp));
  166. dump_backtrace_impl(ebp, ksyms_ready);
  167. }
  168. void load_ksyms()
  169. {
  170. auto result = VFS::the().open("/res/kernel.map", O_RDONLY, 0, VFS::the().root_custody());
  171. ASSERT(!result.is_error());
  172. auto description = result.value();
  173. auto buffer = description->read_entire_file();
  174. ASSERT(buffer);
  175. load_ksyms_from_data(buffer);
  176. }