| 1 | //===-- SectionLoadHistory.cpp --------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Target/SectionLoadHistory.h" |
| 10 | |
| 11 | #include "lldb/Target/SectionLoadList.h" |
| 12 | #include "lldb/Utility/Stream.h" |
| 13 | |
| 14 | using namespace lldb; |
| 15 | using namespace lldb_private; |
| 16 | |
| 17 | bool SectionLoadHistory::IsEmpty() const { |
| 18 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 19 | return m_stop_id_to_section_load_list.empty(); |
| 20 | } |
| 21 | |
| 22 | void SectionLoadHistory::Clear() { |
| 23 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 24 | m_stop_id_to_section_load_list.clear(); |
| 25 | } |
| 26 | |
| 27 | uint32_t SectionLoadHistory::GetLastStopID() const { |
| 28 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 29 | if (m_stop_id_to_section_load_list.empty()) |
| 30 | return 0; |
| 31 | else |
| 32 | return m_stop_id_to_section_load_list.rbegin()->first; |
| 33 | } |
| 34 | |
| 35 | SectionLoadList * |
| 36 | SectionLoadHistory::GetSectionLoadListForStopID(uint32_t stop_id, |
| 37 | bool read_only) { |
| 38 | if (!m_stop_id_to_section_load_list.empty()) { |
| 39 | if (read_only) { |
| 40 | // The section load list is for reading data only so we don't need to |
| 41 | // create a new SectionLoadList for the current stop ID, just return the |
| 42 | // section load list for the stop ID that is equal to or less than the |
| 43 | // current stop ID |
| 44 | if (stop_id == eStopIDNow) { |
| 45 | // If we are asking for the latest and greatest value, it is always at |
| 46 | // the end of our list because that will be the highest stop ID. |
| 47 | StopIDToSectionLoadList::reverse_iterator rpos = |
| 48 | m_stop_id_to_section_load_list.rbegin(); |
| 49 | return rpos->second.get(); |
| 50 | } else { |
| 51 | StopIDToSectionLoadList::iterator pos = |
| 52 | m_stop_id_to_section_load_list.lower_bound(x: stop_id); |
| 53 | if (pos != m_stop_id_to_section_load_list.end() && |
| 54 | pos->first == stop_id) |
| 55 | return pos->second.get(); |
| 56 | else if (pos != m_stop_id_to_section_load_list.begin()) { |
| 57 | --pos; |
| 58 | return pos->second.get(); |
| 59 | } |
| 60 | } |
| 61 | } else { |
| 62 | // You can only use "eStopIDNow" when reading from the section load |
| 63 | // history |
| 64 | assert(stop_id != eStopIDNow); |
| 65 | |
| 66 | // We are updating the section load list (not read only), so if the stop |
| 67 | // ID passed in isn't the same as the last stop ID in our collection, |
| 68 | // then create a new node using the current stop ID |
| 69 | StopIDToSectionLoadList::iterator pos = |
| 70 | m_stop_id_to_section_load_list.lower_bound(x: stop_id); |
| 71 | if (pos != m_stop_id_to_section_load_list.end() && |
| 72 | pos->first == stop_id) { |
| 73 | // We already have an entry for this value |
| 74 | return pos->second.get(); |
| 75 | } |
| 76 | |
| 77 | // We must make a new section load list that is based on the last valid |
| 78 | // section load list, so here we copy the last section load list and add |
| 79 | // a new node for the current stop ID. |
| 80 | StopIDToSectionLoadList::reverse_iterator rpos = |
| 81 | m_stop_id_to_section_load_list.rbegin(); |
| 82 | SectionLoadListSP section_load_list_sp( |
| 83 | new SectionLoadList(*rpos->second)); |
| 84 | m_stop_id_to_section_load_list[stop_id] = section_load_list_sp; |
| 85 | return section_load_list_sp.get(); |
| 86 | } |
| 87 | } |
| 88 | SectionLoadListSP section_load_list_sp(new SectionLoadList()); |
| 89 | if (stop_id == eStopIDNow) |
| 90 | stop_id = 0; |
| 91 | m_stop_id_to_section_load_list[stop_id] = section_load_list_sp; |
| 92 | return section_load_list_sp.get(); |
| 93 | } |
| 94 | |
| 95 | SectionLoadList &SectionLoadHistory::GetCurrentSectionLoadList() { |
| 96 | const bool read_only = true; |
| 97 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 98 | SectionLoadList *section_load_list = |
| 99 | GetSectionLoadListForStopID(stop_id: eStopIDNow, read_only); |
| 100 | assert(section_load_list != nullptr); |
| 101 | return *section_load_list; |
| 102 | } |
| 103 | |
| 104 | addr_t |
| 105 | SectionLoadHistory::GetSectionLoadAddress(uint32_t stop_id, |
| 106 | const lldb::SectionSP §ion_sp) { |
| 107 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 108 | const bool read_only = true; |
| 109 | SectionLoadList *section_load_list = |
| 110 | GetSectionLoadListForStopID(stop_id, read_only); |
| 111 | return section_load_list->GetSectionLoadAddress(section_sp); |
| 112 | } |
| 113 | |
| 114 | bool SectionLoadHistory::ResolveLoadAddress(uint32_t stop_id, addr_t load_addr, |
| 115 | Address &so_addr, |
| 116 | bool allow_section_end) { |
| 117 | // First find the top level section that this load address exists in |
| 118 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 119 | const bool read_only = true; |
| 120 | SectionLoadList *section_load_list = |
| 121 | GetSectionLoadListForStopID(stop_id, read_only); |
| 122 | return section_load_list->ResolveLoadAddress(load_addr, so_addr, |
| 123 | allow_section_end); |
| 124 | } |
| 125 | |
| 126 | bool SectionLoadHistory::SetSectionLoadAddress( |
| 127 | uint32_t stop_id, const lldb::SectionSP §ion_sp, addr_t load_addr, |
| 128 | bool warn_multiple) { |
| 129 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 130 | const bool read_only = false; |
| 131 | SectionLoadList *section_load_list = |
| 132 | GetSectionLoadListForStopID(stop_id, read_only); |
| 133 | return section_load_list->SetSectionLoadAddress(section_sp, load_addr, |
| 134 | warn_multiple); |
| 135 | } |
| 136 | |
| 137 | size_t |
| 138 | SectionLoadHistory::SetSectionUnloaded(uint32_t stop_id, |
| 139 | const lldb::SectionSP §ion_sp) { |
| 140 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 141 | const bool read_only = false; |
| 142 | SectionLoadList *section_load_list = |
| 143 | GetSectionLoadListForStopID(stop_id, read_only); |
| 144 | return section_load_list->SetSectionUnloaded(section_sp); |
| 145 | } |
| 146 | |
| 147 | bool SectionLoadHistory::SetSectionUnloaded(uint32_t stop_id, |
| 148 | const lldb::SectionSP §ion_sp, |
| 149 | addr_t load_addr) { |
| 150 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 151 | const bool read_only = false; |
| 152 | SectionLoadList *section_load_list = |
| 153 | GetSectionLoadListForStopID(stop_id, read_only); |
| 154 | return section_load_list->SetSectionUnloaded(section_sp, load_addr); |
| 155 | } |
| 156 | |
| 157 | void SectionLoadHistory::Dump(Stream &s, Target *target) { |
| 158 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 159 | StopIDToSectionLoadList::iterator pos, |
| 160 | end = m_stop_id_to_section_load_list.end(); |
| 161 | for (pos = m_stop_id_to_section_load_list.begin(); pos != end; ++pos) { |
| 162 | s.Printf(format: "StopID = %u:\n" , pos->first); |
| 163 | pos->second->Dump(s, target); |
| 164 | s.EOL(); |
| 165 | } |
| 166 | } |
| 167 | |