]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Target/SectionLoadHistory.cpp
Vendor import of lldb release_39 branch r276489:
[FreeBSD/FreeBSD.git] / source / Target / SectionLoadHistory.cpp
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     std::lock_guard<std::recursive_mutex> guard(m_mutex);
27     return m_stop_id_to_section_load_list.empty();
28 }
29
30 void
31 SectionLoadHistory::Clear ()
32 {
33     std::lock_guard<std::recursive_mutex> guard(m_mutex);
34     m_stop_id_to_section_load_list.clear();
35 }
36
37 uint32_t
38 SectionLoadHistory::GetLastStopID() const
39 {
40     std::lock_guard<std::recursive_mutex> guard(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 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();
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     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;
115 }
116
117 addr_t
118 SectionLoadHistory::GetSectionLoadAddress (uint32_t stop_id, const lldb::SectionSP &section_sp)
119 {
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);
124 }
125
126 bool
127 SectionLoadHistory::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     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);
134 }
135
136 bool
137 SectionLoadHistory::SetSectionLoadAddress (uint32_t stop_id,
138                                            const lldb::SectionSP &section_sp,
139                                            addr_t load_addr,
140                                            bool warn_multiple)
141 {
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);
146 }
147
148 size_t
149 SectionLoadHistory::SetSectionUnloaded (uint32_t stop_id, const lldb::SectionSP &section_sp)
150 {
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);
155 }
156
157 bool
158 SectionLoadHistory::SetSectionUnloaded (uint32_t stop_id, const lldb::SectionSP &section_sp, addr_t load_addr)
159 {
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);
164 }
165
166 void
167 SectionLoadHistory::Dump (Stream &s, Target *target)
168 {
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)
172     {
173         s.Printf("StopID = %u:\n", pos->first);
174         pos->second->Dump(s, target);
175         s.EOL();
176     }
177 }
178
179