blob: ec16b58b4451cdb7cadb510dfa6275730eae9e99 [file] [log] [blame]
Greg Claytond5944cd2013-12-06 01:12:00 +00001//===-- SectionLoadHistory.cpp ----------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Claytond5944cd2013-12-06 01:12:00 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Target/SectionLoadHistory.h"
10
Greg Claytond5944cd2013-12-06 01:12:00 +000011#include "lldb/Target/SectionLoadList.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000012#include "lldb/Utility/Stream.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000013
14using namespace lldb;
15using namespace lldb_private;
16
Kate Stoneb9c1b512016-09-06 20:57:50 +000017bool SectionLoadHistory::IsEmpty() const {
18 std::lock_guard<std::recursive_mutex> guard(m_mutex);
19 return m_stop_id_to_section_load_list.empty();
Greg Claytond5944cd2013-12-06 01:12:00 +000020}
21
Kate Stoneb9c1b512016-09-06 20:57:50 +000022void SectionLoadHistory::Clear() {
23 std::lock_guard<std::recursive_mutex> guard(m_mutex);
24 m_stop_id_to_section_load_list.clear();
Greg Claytond5944cd2013-12-06 01:12:00 +000025}
26
Kate Stoneb9c1b512016-09-06 20:57:50 +000027uint32_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 Claytond5944cd2013-12-06 01:12:00 +000033}
34
35SectionLoadList *
Kate Stoneb9c1b512016-09-06 20:57:50 +000036SectionLoadHistory::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 Prantl05097242018-04-30 16:49:04 +000041 // 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 Stoneb9c1b512016-09-06 20:57:50 +000044 if (stop_id == eStopIDNow) {
Adrian Prantl05097242018-04-30 16:49:04 +000045 // 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 Stoneb9c1b512016-09-06 20:57:50 +000047 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 Claytond5944cd2013-12-06 01:12:00 +000059 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 }
61 } else {
62 // You can only use "eStopIDNow" when reading from the section load
63 // history
64 assert(stop_id != eStopIDNow);
Greg Claytond5944cd2013-12-06 01:12:00 +000065
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 // We are updating the section load list (not read only), so if the stop
Adrian Prantl05097242018-04-30 16:49:04 +000067 // 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 Stoneb9c1b512016-09-06 20:57:50 +000069 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 Devlieghere70355ac2019-02-12 03:47:39 +000083 new SectionLoadList(*rpos->second));
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 m_stop_id_to_section_load_list[stop_id] = section_load_list_sp;
85 return section_load_list_sp.get();
Greg Claytond5944cd2013-12-06 01:12:00 +000086 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 }
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 Claytond5944cd2013-12-06 01:12:00 +000093}
94
Kate Stoneb9c1b512016-09-06 20:57:50 +000095SectionLoadList &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 Kleine248a1302019-05-23 11:14:47 +0000100 assert(section_load_list != nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 return *section_load_list;
Greg Claytond5944cd2013-12-06 01:12:00 +0000102}
103
104addr_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105SectionLoadHistory::GetSectionLoadAddress(uint32_t stop_id,
106 const lldb::SectionSP &section_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 Claytond5944cd2013-12-06 01:12:00 +0000112}
113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114bool 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 Claytond5944cd2013-12-06 01:12:00 +0000122}
123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124bool SectionLoadHistory::SetSectionLoadAddress(
125 uint32_t stop_id, const lldb::SectionSP &section_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 Claytond5944cd2013-12-06 01:12:00 +0000133}
134
135size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136SectionLoadHistory::SetSectionUnloaded(uint32_t stop_id,
137 const lldb::SectionSP &section_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 Claytond5944cd2013-12-06 01:12:00 +0000143}
144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145bool SectionLoadHistory::SetSectionUnloaded(uint32_t stop_id,
146 const lldb::SectionSP &section_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 Claytond5944cd2013-12-06 01:12:00 +0000153}
154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155void 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 Claytond5944cd2013-12-06 01:12:00 +0000164}