Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 1 | //===-- SectionLoadHistory.cpp ----------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Target/SectionLoadHistory.h" |
| 10 | |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 11 | #include "lldb/Target/SectionLoadList.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 12 | #include "lldb/Utility/Stream.h" |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace lldb; |
| 15 | using namespace lldb_private; |
| 16 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 20 | } |
| 21 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | void SectionLoadHistory::Clear() { |
| 23 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 24 | m_stop_id_to_section_load_list.clear(); |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | SectionLoadList * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 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 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 44 | if (stop_id == eStopIDNow) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 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. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(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(); |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 59 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | } |
| 61 | } else { |
| 62 | // You can only use "eStopIDNow" when reading from the section load |
| 63 | // history |
| 64 | assert(stop_id != eStopIDNow); |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 65 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 66 | // We are updating the section load list (not read only), so if the stop |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 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 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | StopIDToSectionLoadList::iterator pos = |
| 70 | m_stop_id_to_section_load_list.lower_bound(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( |
Jonas Devlieghere | 70355ac | 2019-02-12 03:47:39 +0000 | [diff] [blame] | 83 | new SectionLoadList(*rpos->second)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | m_stop_id_to_section_load_list[stop_id] = section_load_list_sp; |
| 85 | return section_load_list_sp.get(); |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 86 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(eStopIDNow, read_only); |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 100 | assert(section_load_list != nullptr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | return *section_load_list; |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | addr_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | bool SectionLoadHistory::ResolveLoadAddress(uint32_t stop_id, addr_t load_addr, |
| 115 | Address &so_addr) { |
| 116 | // First find the top level section that this load address exists in |
| 117 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 118 | const bool read_only = true; |
| 119 | SectionLoadList *section_load_list = |
| 120 | GetSectionLoadListForStopID(stop_id, read_only); |
| 121 | return section_load_list->ResolveLoadAddress(load_addr, so_addr); |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | bool SectionLoadHistory::SetSectionLoadAddress( |
| 125 | uint32_t stop_id, const lldb::SectionSP §ion_sp, addr_t load_addr, |
| 126 | bool warn_multiple) { |
| 127 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 128 | const bool read_only = false; |
| 129 | SectionLoadList *section_load_list = |
| 130 | GetSectionLoadListForStopID(stop_id, read_only); |
| 131 | return section_load_list->SetSectionLoadAddress(section_sp, load_addr, |
| 132 | warn_multiple); |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | SectionLoadHistory::SetSectionUnloaded(uint32_t stop_id, |
| 137 | const lldb::SectionSP §ion_sp) { |
| 138 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 139 | const bool read_only = false; |
| 140 | SectionLoadList *section_load_list = |
| 141 | GetSectionLoadListForStopID(stop_id, read_only); |
| 142 | return section_load_list->SetSectionUnloaded(section_sp); |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | bool SectionLoadHistory::SetSectionUnloaded(uint32_t stop_id, |
| 146 | const lldb::SectionSP §ion_sp, |
| 147 | addr_t load_addr) { |
| 148 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 149 | const bool read_only = false; |
| 150 | SectionLoadList *section_load_list = |
| 151 | GetSectionLoadListForStopID(stop_id, read_only); |
| 152 | return section_load_list->SetSectionUnloaded(section_sp, load_addr); |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 155 | void SectionLoadHistory::Dump(Stream &s, Target *target) { |
| 156 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 157 | StopIDToSectionLoadList::iterator pos, |
| 158 | end = m_stop_id_to_section_load_list.end(); |
| 159 | for (pos = m_stop_id_to_section_load_list.begin(); pos != end; ++pos) { |
| 160 | s.Printf("StopID = %u:\n", pos->first); |
| 161 | pos->second->Dump(s, target); |
| 162 | s.EOL(); |
| 163 | } |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 164 | } |