Process.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Process.h"
  7. #include <LibCore/File.h>
  8. namespace Profiler {
  9. Thread* Process::find_thread(pid_t tid, EventSerialNumber serial)
  10. {
  11. auto it = threads.find(tid);
  12. if (it == threads.end())
  13. return nullptr;
  14. for (auto& thread : it->value) {
  15. if (thread.start_valid < serial && (thread.end_valid == EventSerialNumber {} || thread.end_valid > serial))
  16. return &thread;
  17. }
  18. return nullptr;
  19. }
  20. void Process::handle_thread_create(pid_t tid, EventSerialNumber serial)
  21. {
  22. auto it = threads.find(tid);
  23. if (it == threads.end()) {
  24. threads.set(tid, {});
  25. it = threads.find(tid);
  26. }
  27. auto thread = Thread { tid, serial, {} };
  28. it->value.append(move(thread));
  29. }
  30. void Process::handle_thread_exit(pid_t tid, EventSerialNumber serial)
  31. {
  32. auto* thread = find_thread(tid, serial);
  33. if (!thread)
  34. return;
  35. thread->end_valid = serial;
  36. }
  37. HashMap<String, OwnPtr<MappedObject>> g_mapped_object_cache;
  38. static MappedObject* get_or_create_mapped_object(const String& path)
  39. {
  40. if (auto it = g_mapped_object_cache.find(path); it != g_mapped_object_cache.end())
  41. return it->value.ptr();
  42. auto file_or_error = MappedFile::map(path);
  43. if (file_or_error.is_error()) {
  44. g_mapped_object_cache.set(path, {});
  45. return nullptr;
  46. }
  47. auto elf = ELF::Image(file_or_error.value()->bytes());
  48. if (!elf.is_valid()) {
  49. g_mapped_object_cache.set(path, {});
  50. return nullptr;
  51. }
  52. auto new_mapped_object = adopt_own(*new MappedObject {
  53. .file = file_or_error.release_value(),
  54. .elf = elf,
  55. });
  56. auto* ptr = new_mapped_object.ptr();
  57. g_mapped_object_cache.set(path, move(new_mapped_object));
  58. return ptr;
  59. }
  60. void LibraryMetadata::handle_mmap(FlatPtr base, size_t size, const String& name)
  61. {
  62. StringView path;
  63. if (name.contains("Loader.so"sv))
  64. path = "Loader.so"sv;
  65. else if (!name.contains(':'))
  66. return;
  67. else
  68. path = name.substring_view(0, name.view().find(':').value());
  69. // Each loaded object has at least 4 segments associated with it: .rodata, .text, .relro, .data.
  70. // We only want to create a single LibraryMetadata object for each library, so we need to update the
  71. // associated base address and size as new regions are discovered.
  72. // We don't allocate a temporary String object if an entry already exists.
  73. // This assumes that String::hash and StringView::hash return the same result.
  74. auto string_view_compare = [&path](auto& entry) { return path == entry.key.view(); };
  75. if (auto existing_it = m_libraries.find(path.hash(), string_view_compare); existing_it != m_libraries.end()) {
  76. auto& entry = *existing_it->value;
  77. entry.base = min(entry.base, base);
  78. entry.size = max(entry.size + size, base - entry.base + size);
  79. } else {
  80. String path_string = path.to_string();
  81. String full_path;
  82. if (Core::File::looks_like_shared_library(path_string))
  83. full_path = String::formatted("/usr/lib/{}", path);
  84. else
  85. full_path = path_string;
  86. auto* mapped_object = get_or_create_mapped_object(full_path);
  87. if (!mapped_object) {
  88. full_path = String::formatted("/usr/local/lib/{}", path);
  89. mapped_object = get_or_create_mapped_object(full_path);
  90. if (!mapped_object)
  91. return;
  92. }
  93. m_libraries.set(path_string, adopt_own(*new Library { base, size, path_string, mapped_object, {} }));
  94. }
  95. }
  96. const Debug::DebugInfo& LibraryMetadata::Library::load_debug_info(FlatPtr base_address) const
  97. {
  98. if (debug_info == nullptr)
  99. debug_info = make<Debug::DebugInfo>(object->elf, String::empty(), base_address);
  100. return *debug_info.ptr();
  101. }
  102. String LibraryMetadata::Library::symbolicate(FlatPtr ptr, u32* offset) const
  103. {
  104. if (!object)
  105. return String::formatted("?? <{:p}>", ptr);
  106. return object->elf.symbolicate(ptr - base, offset);
  107. }
  108. const LibraryMetadata::Library* LibraryMetadata::library_containing(FlatPtr ptr) const
  109. {
  110. for (auto& it : m_libraries) {
  111. auto& library = *it.value;
  112. if (ptr >= library.base && ptr < (library.base + library.size))
  113. return &library;
  114. }
  115. return nullptr;
  116. }
  117. }