1 //===-- 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 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 
23 bool
24 SectionLoadHistory::IsEmpty() const
25 {
26     Mutex::Locker locker(m_mutex);
27     return m_stop_id_to_section_load_list.empty();
28 }
29 
30 void
31 SectionLoadHistory::Clear ()
32 {
33     Mutex::Locker locker(m_mutex);
34     m_stop_id_to_section_load_list.clear();
35 }
36 
37 uint32_t
38 SectionLoadHistory::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 
47 SectionLoadList *
48 SectionLoadHistory::GetSectionLoadListForStopID (uint32_t stop_id, bool read_only)
49 {
50     if (!m_stop_id_to_section_load_list.empty())
51     {
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
60                 // at the end of our list becuase 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();
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     }
100     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();
105 }
106 
107 SectionLoadList &
108 SectionLoadHistory::GetCurrentSectionLoadList ()
109 {
110     const bool read_only = true;
111     SectionLoadList *section_load_list = GetSectionLoadListForStopID (eStopIDNow, read_only);
112     assert(section_load_list != NULL);
113     return *section_load_list;
114 }
115 
116 addr_t
117 SectionLoadHistory::GetSectionLoadAddress (uint32_t stop_id, const lldb::SectionSP &section_sp)
118 {
119     Mutex::Locker locker(m_mutex);
120     const bool read_only = true;
121     SectionLoadList *section_load_list = GetSectionLoadListForStopID (stop_id, read_only);
122     return section_load_list->GetSectionLoadAddress(section_sp);
123 }
124 
125 bool
126 SectionLoadHistory::ResolveLoadAddress (uint32_t stop_id, addr_t load_addr, Address &so_addr)
127 {
128     // First find the top level section that this load address exists in
129     Mutex::Locker locker(m_mutex);
130     const bool read_only = true;
131     SectionLoadList *section_load_list = GetSectionLoadListForStopID (stop_id, read_only);
132     return section_load_list->ResolveLoadAddress (load_addr, so_addr);
133 }
134 
135 bool
136 SectionLoadHistory::SetSectionLoadAddress (uint32_t stop_id,
137                                            const lldb::SectionSP &section_sp,
138                                            addr_t load_addr,
139                                            bool warn_multiple)
140 {
141     Mutex::Locker locker(m_mutex);
142     const bool read_only = false;
143     SectionLoadList *section_load_list = GetSectionLoadListForStopID (stop_id, read_only);
144     return section_load_list->SetSectionLoadAddress(section_sp, load_addr, warn_multiple);
145 }
146 
147 size_t
148 SectionLoadHistory::SetSectionUnloaded (uint32_t stop_id, const lldb::SectionSP &section_sp)
149 {
150     Mutex::Locker locker(m_mutex);
151     const bool read_only = false;
152     SectionLoadList *section_load_list = GetSectionLoadListForStopID (stop_id, read_only);
153     return section_load_list->SetSectionUnloaded (section_sp);
154 }
155 
156 bool
157 SectionLoadHistory::SetSectionUnloaded (uint32_t stop_id, const lldb::SectionSP &section_sp, addr_t load_addr)
158 {
159     Mutex::Locker locker(m_mutex);
160     const bool read_only = false;
161     SectionLoadList *section_load_list = GetSectionLoadListForStopID (stop_id, read_only);
162     return section_load_list->SetSectionUnloaded (section_sp, load_addr);
163 }
164 
165 void
166 SectionLoadHistory::Dump (Stream &s, Target *target)
167 {
168     Mutex::Locker locker(m_mutex);
169     StopIDToSectionLoadList::iterator pos, end = m_stop_id_to_section_load_list.end();
170     for (pos = m_stop_id_to_section_load_list.begin(); pos != end; ++pos)
171     {
172         s.Printf("StopID = %u:\n", pos->first);
173         pos->second->Dump(s, target);
174         s.EOL();
175     }
176 }
177 
178 
179