1 //===-- SectionLoadHistory.cpp ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/SectionLoadHistory.h"
14 // Other libraries and framework includes
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Target/SectionLoadList.h"
20 using namespace lldb_private;
24 SectionLoadHistory::IsEmpty() const
26 std::lock_guard<std::recursive_mutex> guard(m_mutex);
27 return m_stop_id_to_section_load_list.empty();
31 SectionLoadHistory::Clear ()
33 std::lock_guard<std::recursive_mutex> guard(m_mutex);
34 m_stop_id_to_section_load_list.clear();
38 SectionLoadHistory::GetLastStopID() const
40 std::lock_guard<std::recursive_mutex> guard(m_mutex);
41 if (m_stop_id_to_section_load_list.empty())
44 return m_stop_id_to_section_load_list.rbegin()->first;
48 SectionLoadHistory::GetSectionLoadListForStopID (uint32_t stop_id, bool read_only)
50 if (!m_stop_id_to_section_load_list.empty())
54 // The section load list is for reading data only so we don't need to create
55 // a new SectionLoadList for the current stop ID, just return the section
56 // load list for the stop ID that is equal to or less than the current stop ID
57 if (stop_id == eStopIDNow)
59 // If we are asking for the latest and greatest value, it is always
60 // at the end of our list because that will be the highest stop ID.
61 StopIDToSectionLoadList::reverse_iterator rpos = m_stop_id_to_section_load_list.rbegin();
62 return rpos->second.get();
66 StopIDToSectionLoadList::iterator pos = m_stop_id_to_section_load_list.lower_bound(stop_id);
67 if (pos != m_stop_id_to_section_load_list.end() && pos->first == stop_id)
68 return pos->second.get();
69 else if (pos != m_stop_id_to_section_load_list.begin())
72 return pos->second.get();
78 // You can only use "eStopIDNow" when reading from the section load history
79 assert(stop_id != eStopIDNow);
81 // We are updating the section load list (not read only), so if the stop ID
82 // passed in isn't the same as the last stop ID in our collection, then create
83 // a new node using the current stop ID
84 StopIDToSectionLoadList::iterator pos = m_stop_id_to_section_load_list.lower_bound(stop_id);
85 if (pos != m_stop_id_to_section_load_list.end() && pos->first == stop_id)
87 // We already have an entry for this value
88 return pos->second.get();
91 // We must make a new section load list that is based on the last valid
92 // section load list, so here we copy the last section load list and add
93 // a new node for the current stop ID.
94 StopIDToSectionLoadList::reverse_iterator rpos = m_stop_id_to_section_load_list.rbegin();
95 SectionLoadListSP section_load_list_sp(new SectionLoadList(*rpos->second.get()));
96 m_stop_id_to_section_load_list[stop_id] = section_load_list_sp;
97 return section_load_list_sp.get();
100 SectionLoadListSP section_load_list_sp(new SectionLoadList());
101 if (stop_id == eStopIDNow)
103 m_stop_id_to_section_load_list[stop_id] = section_load_list_sp;
104 return section_load_list_sp.get();
108 SectionLoadHistory::GetCurrentSectionLoadList ()
110 const bool read_only = true;
111 std::lock_guard<std::recursive_mutex> guard(m_mutex);
112 SectionLoadList *section_load_list = GetSectionLoadListForStopID (eStopIDNow, read_only);
113 assert(section_load_list != NULL);
114 return *section_load_list;
118 SectionLoadHistory::GetSectionLoadAddress (uint32_t stop_id, const lldb::SectionSP §ion_sp)
120 std::lock_guard<std::recursive_mutex> guard(m_mutex);
121 const bool read_only = true;
122 SectionLoadList *section_load_list = GetSectionLoadListForStopID (stop_id, read_only);
123 return section_load_list->GetSectionLoadAddress(section_sp);
127 SectionLoadHistory::ResolveLoadAddress (uint32_t stop_id, addr_t load_addr, Address &so_addr)
129 // First find the top level section that this load address exists in
130 std::lock_guard<std::recursive_mutex> guard(m_mutex);
131 const bool read_only = true;
132 SectionLoadList *section_load_list = GetSectionLoadListForStopID (stop_id, read_only);
133 return section_load_list->ResolveLoadAddress (load_addr, so_addr);
137 SectionLoadHistory::SetSectionLoadAddress (uint32_t stop_id,
138 const lldb::SectionSP §ion_sp,
142 std::lock_guard<std::recursive_mutex> guard(m_mutex);
143 const bool read_only = false;
144 SectionLoadList *section_load_list = GetSectionLoadListForStopID (stop_id, read_only);
145 return section_load_list->SetSectionLoadAddress(section_sp, load_addr, warn_multiple);
149 SectionLoadHistory::SetSectionUnloaded (uint32_t stop_id, const lldb::SectionSP §ion_sp)
151 std::lock_guard<std::recursive_mutex> guard(m_mutex);
152 const bool read_only = false;
153 SectionLoadList *section_load_list = GetSectionLoadListForStopID (stop_id, read_only);
154 return section_load_list->SetSectionUnloaded (section_sp);
158 SectionLoadHistory::SetSectionUnloaded (uint32_t stop_id, const lldb::SectionSP §ion_sp, addr_t load_addr)
160 std::lock_guard<std::recursive_mutex> guard(m_mutex);
161 const bool read_only = false;
162 SectionLoadList *section_load_list = GetSectionLoadListForStopID (stop_id, read_only);
163 return section_load_list->SetSectionUnloaded (section_sp, load_addr);
167 SectionLoadHistory::Dump (Stream &s, Target *target)
169 std::lock_guard<std::recursive_mutex> guard(m_mutex);
170 StopIDToSectionLoadList::iterator pos, end = m_stop_id_to_section_load_list.end();
171 for (pos = m_stop_id_to_section_load_list.begin(); pos != end; ++pos)
173 s.Printf("StopID = %u:\n", pos->first);
174 pos->second->Dump(s, target);