blob: 1e5c4175a2bf4305c776ea9c72371070a1c3ef9c [file] [log] [blame]
Greg Claytond5944cd2013-12-06 01:12:00 +00001//===-- SectionLoadHistory.cpp ----------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/SectionLoadHistory.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Core/Stream.h"
17#include "lldb/Target/SectionLoadList.h"
18
19using namespace lldb;
20using namespace lldb_private;
21
22
23bool
24SectionLoadHistory::IsEmpty() const
25{
26 Mutex::Locker locker(m_mutex);
27 return m_stop_id_to_section_load_list.empty();
28}
29
30void
31SectionLoadHistory::Clear ()
32{
33 Mutex::Locker locker(m_mutex);
34 m_stop_id_to_section_load_list.clear();
35}
36
37uint32_t
38SectionLoadHistory::GetLastStopID() const
39{
40 Mutex::Locker locker(m_mutex);
41 if (m_stop_id_to_section_load_list.empty())
42 return 0;
43 else
44 return m_stop_id_to_section_load_list.rbegin()->first;
45}
46
47SectionLoadList *
48SectionLoadHistory::GetSectionLoadListForStopID (uint32_t stop_id, bool read_only)
49{
Greg Clayton24feaf72014-05-14 17:25:00 +000050 if (!m_stop_id_to_section_load_list.empty())
Greg Claytond5944cd2013-12-06 01:12:00 +000051 {
52 if (read_only)
53 {
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)
58 {
59 // If we are asking for the latest and greatest value, it is always
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000060 // at the end of our list because that will be the highest stop ID.
Greg Claytond5944cd2013-12-06 01:12:00 +000061 StopIDToSectionLoadList::reverse_iterator rpos = m_stop_id_to_section_load_list.rbegin();
62 return rpos->second.get();
63 }
64 else
65 {
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())
70 {
71 --pos;
72 return pos->second.get();
73 }
74 }
75 }
76 else
77 {
78 // You can only use "eStopIDNow" when reading from the section load history
79 assert(stop_id != eStopIDNow);
80
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)
86 {
87 // We already have an entry for this value
88 return pos->second.get();
89 }
90
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();
98 }
99 }
Greg Clayton24feaf72014-05-14 17:25:00 +0000100 SectionLoadListSP section_load_list_sp(new SectionLoadList());
101 if (stop_id == eStopIDNow)
102 stop_id = 0;
103 m_stop_id_to_section_load_list[stop_id] = section_load_list_sp;
104 return section_load_list_sp.get();
Greg Claytond5944cd2013-12-06 01:12:00 +0000105}
106
107SectionLoadList &
108SectionLoadHistory::GetCurrentSectionLoadList ()
109{
110 const bool read_only = true;
Greg Claytone756aa32014-05-22 23:54:17 +0000111 Mutex::Locker locker(m_mutex);
Greg Claytond5944cd2013-12-06 01:12:00 +0000112 SectionLoadList *section_load_list = GetSectionLoadListForStopID (eStopIDNow, read_only);
113 assert(section_load_list != NULL);
114 return *section_load_list;
115}
116
117addr_t
118SectionLoadHistory::GetSectionLoadAddress (uint32_t stop_id, const lldb::SectionSP &section_sp)
119{
120 Mutex::Locker locker(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);
124}
125
126bool
127SectionLoadHistory::ResolveLoadAddress (uint32_t stop_id, addr_t load_addr, Address &so_addr)
128{
129 // First find the top level section that this load address exists in
130 Mutex::Locker locker(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);
134}
135
136bool
137SectionLoadHistory::SetSectionLoadAddress (uint32_t stop_id,
138 const lldb::SectionSP &section_sp,
139 addr_t load_addr,
140 bool warn_multiple)
141{
142 Mutex::Locker locker(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);
146}
147
148size_t
149SectionLoadHistory::SetSectionUnloaded (uint32_t stop_id, const lldb::SectionSP &section_sp)
150{
151 Mutex::Locker locker(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);
155}
156
157bool
158SectionLoadHistory::SetSectionUnloaded (uint32_t stop_id, const lldb::SectionSP &section_sp, addr_t load_addr)
159{
160 Mutex::Locker locker(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);
164}
165
166void
167SectionLoadHistory::Dump (Stream &s, Target *target)
168{
169 Mutex::Locker locker(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)
172 {
173 s.Printf("StopID = %u:\n", pos->first);
174 pos->second->Dump(s, target);
175 s.EOL();
176 }
177}
178
179