112b93ac6SEd Maste //===-- SectionLoadHistory.cpp ----------------------------------*- C++ -*-===//
212b93ac6SEd Maste //
312b93ac6SEd Maste // The LLVM Compiler Infrastructure
412b93ac6SEd Maste //
512b93ac6SEd Maste // This file is distributed under the University of Illinois Open Source
612b93ac6SEd Maste // License. See LICENSE.TXT for details.
712b93ac6SEd Maste //
812b93ac6SEd Maste //===----------------------------------------------------------------------===//
912b93ac6SEd Maste
1012b93ac6SEd Maste #include "lldb/Target/SectionLoadHistory.h"
1112b93ac6SEd Maste
1212b93ac6SEd Maste #include "lldb/Target/SectionLoadList.h"
13f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
1412b93ac6SEd Maste
1512b93ac6SEd Maste using namespace lldb;
1612b93ac6SEd Maste using namespace lldb_private;
1712b93ac6SEd Maste
IsEmpty() const18435933ddSDimitry Andric bool SectionLoadHistory::IsEmpty() const {
194bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(m_mutex);
2012b93ac6SEd Maste return m_stop_id_to_section_load_list.empty();
2112b93ac6SEd Maste }
2212b93ac6SEd Maste
Clear()23435933ddSDimitry Andric void SectionLoadHistory::Clear() {
244bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(m_mutex);
2512b93ac6SEd Maste m_stop_id_to_section_load_list.clear();
2612b93ac6SEd Maste }
2712b93ac6SEd Maste
GetLastStopID() const28435933ddSDimitry Andric uint32_t SectionLoadHistory::GetLastStopID() const {
294bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(m_mutex);
3012b93ac6SEd Maste if (m_stop_id_to_section_load_list.empty())
3112b93ac6SEd Maste return 0;
3212b93ac6SEd Maste else
3312b93ac6SEd Maste return m_stop_id_to_section_load_list.rbegin()->first;
3412b93ac6SEd Maste }
3512b93ac6SEd Maste
3612b93ac6SEd Maste SectionLoadList *
GetSectionLoadListForStopID(uint32_t stop_id,bool read_only)37435933ddSDimitry Andric SectionLoadHistory::GetSectionLoadListForStopID(uint32_t stop_id,
38435933ddSDimitry Andric bool read_only) {
39435933ddSDimitry Andric if (!m_stop_id_to_section_load_list.empty()) {
40435933ddSDimitry Andric if (read_only) {
41435933ddSDimitry Andric // The section load list is for reading data only so we don't need to
42*4ba319b5SDimitry Andric // create a new SectionLoadList for the current stop ID, just return the
43*4ba319b5SDimitry Andric // section load list for the stop ID that is equal to or less than the
44*4ba319b5SDimitry Andric // current stop ID
45435933ddSDimitry Andric if (stop_id == eStopIDNow) {
46*4ba319b5SDimitry Andric // If we are asking for the latest and greatest value, it is always at
47*4ba319b5SDimitry Andric // the end of our list because that will be the highest stop ID.
48435933ddSDimitry Andric StopIDToSectionLoadList::reverse_iterator rpos =
49435933ddSDimitry Andric m_stop_id_to_section_load_list.rbegin();
5012b93ac6SEd Maste return rpos->second.get();
51435933ddSDimitry Andric } else {
52435933ddSDimitry Andric StopIDToSectionLoadList::iterator pos =
53435933ddSDimitry Andric m_stop_id_to_section_load_list.lower_bound(stop_id);
54435933ddSDimitry Andric if (pos != m_stop_id_to_section_load_list.end() &&
55435933ddSDimitry Andric pos->first == stop_id)
5612b93ac6SEd Maste return pos->second.get();
57435933ddSDimitry Andric else if (pos != m_stop_id_to_section_load_list.begin()) {
5812b93ac6SEd Maste --pos;
5912b93ac6SEd Maste return pos->second.get();
6012b93ac6SEd Maste }
6112b93ac6SEd Maste }
62435933ddSDimitry Andric } else {
63435933ddSDimitry Andric // You can only use "eStopIDNow" when reading from the section load
64435933ddSDimitry Andric // history
6512b93ac6SEd Maste assert(stop_id != eStopIDNow);
6612b93ac6SEd Maste
67435933ddSDimitry Andric // We are updating the section load list (not read only), so if the stop
68*4ba319b5SDimitry Andric // ID passed in isn't the same as the last stop ID in our collection,
69*4ba319b5SDimitry Andric // then create a new node using the current stop ID
70435933ddSDimitry Andric StopIDToSectionLoadList::iterator pos =
71435933ddSDimitry Andric m_stop_id_to_section_load_list.lower_bound(stop_id);
72435933ddSDimitry Andric if (pos != m_stop_id_to_section_load_list.end() &&
73435933ddSDimitry Andric pos->first == stop_id) {
7412b93ac6SEd Maste // We already have an entry for this value
7512b93ac6SEd Maste return pos->second.get();
7612b93ac6SEd Maste }
7712b93ac6SEd Maste
7812b93ac6SEd Maste // We must make a new section load list that is based on the last valid
7912b93ac6SEd Maste // section load list, so here we copy the last section load list and add
8012b93ac6SEd Maste // a new node for the current stop ID.
81435933ddSDimitry Andric StopIDToSectionLoadList::reverse_iterator rpos =
82435933ddSDimitry Andric m_stop_id_to_section_load_list.rbegin();
83435933ddSDimitry Andric SectionLoadListSP section_load_list_sp(
84435933ddSDimitry Andric new SectionLoadList(*rpos->second.get()));
8512b93ac6SEd Maste m_stop_id_to_section_load_list[stop_id] = section_load_list_sp;
8612b93ac6SEd Maste return section_load_list_sp.get();
8712b93ac6SEd Maste }
8812b93ac6SEd Maste }
890127ef0fSEd Maste SectionLoadListSP section_load_list_sp(new SectionLoadList());
900127ef0fSEd Maste if (stop_id == eStopIDNow)
910127ef0fSEd Maste stop_id = 0;
920127ef0fSEd Maste m_stop_id_to_section_load_list[stop_id] = section_load_list_sp;
930127ef0fSEd Maste return section_load_list_sp.get();
9412b93ac6SEd Maste }
9512b93ac6SEd Maste
GetCurrentSectionLoadList()96435933ddSDimitry Andric SectionLoadList &SectionLoadHistory::GetCurrentSectionLoadList() {
9712b93ac6SEd Maste const bool read_only = true;
984bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(m_mutex);
99435933ddSDimitry Andric SectionLoadList *section_load_list =
100435933ddSDimitry Andric GetSectionLoadListForStopID(eStopIDNow, read_only);
10112b93ac6SEd Maste assert(section_load_list != NULL);
10212b93ac6SEd Maste return *section_load_list;
10312b93ac6SEd Maste }
10412b93ac6SEd Maste
10512b93ac6SEd Maste addr_t
GetSectionLoadAddress(uint32_t stop_id,const lldb::SectionSP & section_sp)106435933ddSDimitry Andric SectionLoadHistory::GetSectionLoadAddress(uint32_t stop_id,
107435933ddSDimitry Andric const lldb::SectionSP §ion_sp) {
1084bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(m_mutex);
10912b93ac6SEd Maste const bool read_only = true;
110435933ddSDimitry Andric SectionLoadList *section_load_list =
111435933ddSDimitry Andric GetSectionLoadListForStopID(stop_id, read_only);
11212b93ac6SEd Maste return section_load_list->GetSectionLoadAddress(section_sp);
11312b93ac6SEd Maste }
11412b93ac6SEd Maste
ResolveLoadAddress(uint32_t stop_id,addr_t load_addr,Address & so_addr)115435933ddSDimitry Andric bool SectionLoadHistory::ResolveLoadAddress(uint32_t stop_id, addr_t load_addr,
116435933ddSDimitry Andric Address &so_addr) {
11712b93ac6SEd Maste // First find the top level section that this load address exists in
1184bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(m_mutex);
11912b93ac6SEd Maste const bool read_only = true;
120435933ddSDimitry Andric SectionLoadList *section_load_list =
121435933ddSDimitry Andric GetSectionLoadListForStopID(stop_id, read_only);
12212b93ac6SEd Maste return section_load_list->ResolveLoadAddress(load_addr, so_addr);
12312b93ac6SEd Maste }
12412b93ac6SEd Maste
SetSectionLoadAddress(uint32_t stop_id,const lldb::SectionSP & section_sp,addr_t load_addr,bool warn_multiple)125435933ddSDimitry Andric bool SectionLoadHistory::SetSectionLoadAddress(
126435933ddSDimitry Andric uint32_t stop_id, const lldb::SectionSP §ion_sp, addr_t load_addr,
127435933ddSDimitry Andric bool warn_multiple) {
1284bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(m_mutex);
12912b93ac6SEd Maste const bool read_only = false;
130435933ddSDimitry Andric SectionLoadList *section_load_list =
131435933ddSDimitry Andric GetSectionLoadListForStopID(stop_id, read_only);
132435933ddSDimitry Andric return section_load_list->SetSectionLoadAddress(section_sp, load_addr,
133435933ddSDimitry Andric warn_multiple);
13412b93ac6SEd Maste }
13512b93ac6SEd Maste
13612b93ac6SEd Maste size_t
SetSectionUnloaded(uint32_t stop_id,const lldb::SectionSP & section_sp)137435933ddSDimitry Andric SectionLoadHistory::SetSectionUnloaded(uint32_t stop_id,
138435933ddSDimitry Andric const lldb::SectionSP §ion_sp) {
1394bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(m_mutex);
14012b93ac6SEd Maste const bool read_only = false;
141435933ddSDimitry Andric SectionLoadList *section_load_list =
142435933ddSDimitry Andric GetSectionLoadListForStopID(stop_id, read_only);
14312b93ac6SEd Maste return section_load_list->SetSectionUnloaded(section_sp);
14412b93ac6SEd Maste }
14512b93ac6SEd Maste
SetSectionUnloaded(uint32_t stop_id,const lldb::SectionSP & section_sp,addr_t load_addr)146435933ddSDimitry Andric bool SectionLoadHistory::SetSectionUnloaded(uint32_t stop_id,
147435933ddSDimitry Andric const lldb::SectionSP §ion_sp,
148435933ddSDimitry Andric addr_t load_addr) {
1494bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(m_mutex);
15012b93ac6SEd Maste const bool read_only = false;
151435933ddSDimitry Andric SectionLoadList *section_load_list =
152435933ddSDimitry Andric GetSectionLoadListForStopID(stop_id, read_only);
15312b93ac6SEd Maste return section_load_list->SetSectionUnloaded(section_sp, load_addr);
15412b93ac6SEd Maste }
15512b93ac6SEd Maste
Dump(Stream & s,Target * target)156435933ddSDimitry Andric void SectionLoadHistory::Dump(Stream &s, Target *target) {
1574bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(m_mutex);
158435933ddSDimitry Andric StopIDToSectionLoadList::iterator pos,
159435933ddSDimitry Andric end = m_stop_id_to_section_load_list.end();
160435933ddSDimitry Andric for (pos = m_stop_id_to_section_load_list.begin(); pos != end; ++pos) {
16112b93ac6SEd Maste s.Printf("StopID = %u:\n", pos->first);
16212b93ac6SEd Maste pos->second->Dump(s, target);
16312b93ac6SEd Maste s.EOL();
16412b93ac6SEd Maste }
16512b93ac6SEd Maste }
166