SectionLoadHistory.cpp revision 269024
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
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{
50    if (m_stop_id_to_section_load_list.empty())
51    {
52        SectionLoadListSP section_load_list_sp(new SectionLoadList());
53        if (stop_id == eStopIDNow)
54            stop_id = 0;
55        m_stop_id_to_section_load_list[stop_id] = section_load_list_sp;
56        return section_load_list_sp.get();
57    }
58    else
59    {
60        if (read_only)
61        {
62            // The section load list is for reading data only so we don't need to create
63            // a new SectionLoadList for the current stop ID, just return the section
64            // load list for the stop ID that is equal to or less than the current stop ID
65            if (stop_id == eStopIDNow)
66            {
67                // If we are asking for the latest and greatest value, it is always
68                // at the end of our list becuase that will be the highest stop ID.
69                StopIDToSectionLoadList::reverse_iterator rpos = m_stop_id_to_section_load_list.rbegin();
70                return rpos->second.get();
71            }
72            else
73            {
74                StopIDToSectionLoadList::iterator pos = m_stop_id_to_section_load_list.lower_bound(stop_id);
75                if (pos != m_stop_id_to_section_load_list.end() && pos->first == stop_id)
76                    return pos->second.get();
77                else if (pos != m_stop_id_to_section_load_list.begin())
78                {
79                    --pos;
80                    return pos->second.get();
81                }
82            }
83        }
84        else
85        {
86            // You can only use "eStopIDNow" when reading from the section load history
87            assert(stop_id != eStopIDNow);
88
89            // We are updating the section load list (not read only), so if the stop ID
90            // passed in isn't the same as the last stop ID in our collection, then create
91            // a new node using the current stop ID
92            StopIDToSectionLoadList::iterator pos = m_stop_id_to_section_load_list.lower_bound(stop_id);
93            if (pos != m_stop_id_to_section_load_list.end() && pos->first == stop_id)
94            {
95                // We already have an entry for this value
96                return pos->second.get();
97            }
98
99            // We must make a new section load list that is based on the last valid
100            // section load list, so here we copy the last section load list and add
101            // a new node for the current stop ID.
102            StopIDToSectionLoadList::reverse_iterator rpos = m_stop_id_to_section_load_list.rbegin();
103            SectionLoadListSP section_load_list_sp(new SectionLoadList(*rpos->second.get()));
104            m_stop_id_to_section_load_list[stop_id] = section_load_list_sp;
105            return section_load_list_sp.get();
106        }
107    }
108    return NULL;
109}
110
111SectionLoadList &
112SectionLoadHistory::GetCurrentSectionLoadList ()
113{
114    const bool read_only = true;
115    SectionLoadList *section_load_list = GetSectionLoadListForStopID (eStopIDNow, read_only);
116    assert(section_load_list != NULL);
117    return *section_load_list;
118}
119
120addr_t
121SectionLoadHistory::GetSectionLoadAddress (uint32_t stop_id, const lldb::SectionSP &section_sp)
122{
123    Mutex::Locker locker(m_mutex);
124    const bool read_only = true;
125    SectionLoadList *section_load_list = GetSectionLoadListForStopID (stop_id, read_only);
126    return section_load_list->GetSectionLoadAddress(section_sp);
127}
128
129bool
130SectionLoadHistory::ResolveLoadAddress (uint32_t stop_id, addr_t load_addr, Address &so_addr)
131{
132    // First find the top level section that this load address exists in
133    Mutex::Locker locker(m_mutex);
134    const bool read_only = true;
135    SectionLoadList *section_load_list = GetSectionLoadListForStopID (stop_id, read_only);
136    return section_load_list->ResolveLoadAddress (load_addr, so_addr);
137}
138
139bool
140SectionLoadHistory::SetSectionLoadAddress (uint32_t stop_id,
141                                           const lldb::SectionSP &section_sp,
142                                           addr_t load_addr,
143                                           bool warn_multiple)
144{
145    Mutex::Locker locker(m_mutex);
146    const bool read_only = false;
147    SectionLoadList *section_load_list = GetSectionLoadListForStopID (stop_id, read_only);
148    return section_load_list->SetSectionLoadAddress(section_sp, load_addr, warn_multiple);
149}
150
151size_t
152SectionLoadHistory::SetSectionUnloaded (uint32_t stop_id, const lldb::SectionSP &section_sp)
153{
154    Mutex::Locker locker(m_mutex);
155    const bool read_only = false;
156    SectionLoadList *section_load_list = GetSectionLoadListForStopID (stop_id, read_only);
157    return section_load_list->SetSectionUnloaded (section_sp);
158}
159
160bool
161SectionLoadHistory::SetSectionUnloaded (uint32_t stop_id, const lldb::SectionSP &section_sp, addr_t load_addr)
162{
163    Mutex::Locker locker(m_mutex);
164    const bool read_only = false;
165    SectionLoadList *section_load_list = GetSectionLoadListForStopID (stop_id, read_only);
166    return section_load_list->SetSectionUnloaded (section_sp, load_addr);
167}
168
169void
170SectionLoadHistory::Dump (Stream &s, Target *target)
171{
172    Mutex::Locker locker(m_mutex);
173    StopIDToSectionLoadList::iterator pos, end = m_stop_id_to_section_load_list.end();
174    for (pos = m_stop_id_to_section_load_list.begin(); pos != end; ++pos)
175    {
176        s.Printf("StopID = %u:\n", pos->first);
177        pos->second->Dump(s, target);
178        s.EOL();
179    }
180}
181
182
183