1cfca06d7SDimitry Andric //===-- SBLineEntry.cpp ---------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/API/SBLineEntry.h"
105f29bb8aSDimitry Andric #include "Utils.h"
11f034231aSEd Maste #include "lldb/API/SBStream.h"
1274a628f7SDimitry Andric #include "lldb/Host/PosixApi.h"
13f034231aSEd Maste #include "lldb/Symbol/LineEntry.h"
146f8fc217SDimitry Andric #include "lldb/Utility/Instrumentation.h"
1574a628f7SDimitry Andric #include "lldb/Utility/StreamString.h"
16f034231aSEd Maste
17344a3780SDimitry Andric #include <climits>
185f29bb8aSDimitry Andric
19f034231aSEd Maste using namespace lldb;
20f034231aSEd Maste using namespace lldb_private;
21f034231aSEd Maste
SBLineEntry()226f8fc217SDimitry Andric SBLineEntry::SBLineEntry() { LLDB_INSTRUMENT_VA(this); }
23f034231aSEd Maste
SBLineEntry(const SBLineEntry & rhs)246f8fc217SDimitry Andric SBLineEntry::SBLineEntry(const SBLineEntry &rhs) {
256f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
265f29bb8aSDimitry Andric
275f29bb8aSDimitry Andric m_opaque_up = clone(rhs.m_opaque_up);
28f034231aSEd Maste }
29f034231aSEd Maste
SBLineEntry(const lldb_private::LineEntry * lldb_object_ptr)306f8fc217SDimitry Andric SBLineEntry::SBLineEntry(const lldb_private::LineEntry *lldb_object_ptr) {
31f034231aSEd Maste if (lldb_object_ptr)
32ead24645SDimitry Andric m_opaque_up = std::make_unique<LineEntry>(*lldb_object_ptr);
33f034231aSEd Maste }
34f034231aSEd Maste
operator =(const SBLineEntry & rhs)3514f1b3e8SDimitry Andric const SBLineEntry &SBLineEntry::operator=(const SBLineEntry &rhs) {
366f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
375f29bb8aSDimitry Andric
385f29bb8aSDimitry Andric if (this != &rhs)
395f29bb8aSDimitry Andric m_opaque_up = clone(rhs.m_opaque_up);
406f8fc217SDimitry Andric return *this;
41f034231aSEd Maste }
42f034231aSEd Maste
SetLineEntry(const lldb_private::LineEntry & lldb_object_ref)4314f1b3e8SDimitry Andric void SBLineEntry::SetLineEntry(const lldb_private::LineEntry &lldb_object_ref) {
44ead24645SDimitry Andric m_opaque_up = std::make_unique<LineEntry>(lldb_object_ref);
45f034231aSEd Maste }
46f034231aSEd Maste
47cfca06d7SDimitry Andric SBLineEntry::~SBLineEntry() = default;
48f034231aSEd Maste
GetStartAddress() const4914f1b3e8SDimitry Andric SBAddress SBLineEntry::GetStartAddress() const {
506f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
515f29bb8aSDimitry Andric
52f034231aSEd Maste SBAddress sb_address;
535f29bb8aSDimitry Andric if (m_opaque_up)
54b60736ecSDimitry Andric sb_address.SetAddress(m_opaque_up->range.GetBaseAddress());
55f034231aSEd Maste
566f8fc217SDimitry Andric return sb_address;
57f034231aSEd Maste }
58f034231aSEd Maste
GetEndAddress() const5914f1b3e8SDimitry Andric SBAddress SBLineEntry::GetEndAddress() const {
606f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
615f29bb8aSDimitry Andric
62f034231aSEd Maste SBAddress sb_address;
635f29bb8aSDimitry Andric if (m_opaque_up) {
64b60736ecSDimitry Andric sb_address.SetAddress(m_opaque_up->range.GetBaseAddress());
655f29bb8aSDimitry Andric sb_address.OffsetAddress(m_opaque_up->range.GetByteSize());
66f034231aSEd Maste }
676f8fc217SDimitry Andric return sb_address;
68f034231aSEd Maste }
69f034231aSEd Maste
GetSameLineContiguousAddressRangeEnd(bool include_inlined_functions) const70ac9a064cSDimitry Andric SBAddress SBLineEntry::GetSameLineContiguousAddressRangeEnd(
71ac9a064cSDimitry Andric bool include_inlined_functions) const {
72ac9a064cSDimitry Andric LLDB_INSTRUMENT_VA(this);
73ac9a064cSDimitry Andric
74ac9a064cSDimitry Andric SBAddress sb_address;
75ac9a064cSDimitry Andric if (m_opaque_up) {
76ac9a064cSDimitry Andric AddressRange line_range = m_opaque_up->GetSameLineContiguousAddressRange(
77ac9a064cSDimitry Andric include_inlined_functions);
78ac9a064cSDimitry Andric
79ac9a064cSDimitry Andric sb_address.SetAddress(line_range.GetBaseAddress());
80ac9a064cSDimitry Andric sb_address.OffsetAddress(line_range.GetByteSize());
81ac9a064cSDimitry Andric }
82ac9a064cSDimitry Andric return sb_address;
83ac9a064cSDimitry Andric }
84ac9a064cSDimitry Andric
IsValid() const8514f1b3e8SDimitry Andric bool SBLineEntry::IsValid() const {
866f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
875f29bb8aSDimitry Andric return this->operator bool();
885f29bb8aSDimitry Andric }
operator bool() const895f29bb8aSDimitry Andric SBLineEntry::operator bool() const {
906f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
915f29bb8aSDimitry Andric
925f29bb8aSDimitry Andric return m_opaque_up.get() && m_opaque_up->IsValid();
93f034231aSEd Maste }
94f034231aSEd Maste
GetFileSpec() const9514f1b3e8SDimitry Andric SBFileSpec SBLineEntry::GetFileSpec() const {
966f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
97f034231aSEd Maste
98f034231aSEd Maste SBFileSpec sb_file_spec;
99ac9a064cSDimitry Andric if (m_opaque_up.get() && m_opaque_up->GetFile())
100ac9a064cSDimitry Andric sb_file_spec.SetFileSpec(m_opaque_up->GetFile());
101f034231aSEd Maste
1026f8fc217SDimitry Andric return sb_file_spec;
103f034231aSEd Maste }
104f034231aSEd Maste
GetLine() const10514f1b3e8SDimitry Andric uint32_t SBLineEntry::GetLine() const {
1066f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
107f034231aSEd Maste
108f034231aSEd Maste uint32_t line = 0;
1095f29bb8aSDimitry Andric if (m_opaque_up)
1105f29bb8aSDimitry Andric line = m_opaque_up->line;
111f034231aSEd Maste
112f034231aSEd Maste return line;
113f034231aSEd Maste }
114f034231aSEd Maste
GetColumn() const11514f1b3e8SDimitry Andric uint32_t SBLineEntry::GetColumn() const {
1166f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1175f29bb8aSDimitry Andric
1185f29bb8aSDimitry Andric if (m_opaque_up)
1195f29bb8aSDimitry Andric return m_opaque_up->column;
120f034231aSEd Maste return 0;
121f034231aSEd Maste }
122f034231aSEd Maste
SetFileSpec(lldb::SBFileSpec filespec)12314f1b3e8SDimitry Andric void SBLineEntry::SetFileSpec(lldb::SBFileSpec filespec) {
1246f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, filespec);
1255f29bb8aSDimitry Andric
126f034231aSEd Maste if (filespec.IsValid())
127ac9a064cSDimitry Andric ref().file_sp = std::make_shared<SupportFile>(filespec.ref());
128f034231aSEd Maste else
129ac9a064cSDimitry Andric ref().file_sp = std::make_shared<SupportFile>();
130f034231aSEd Maste }
SetLine(uint32_t line)1315f29bb8aSDimitry Andric void SBLineEntry::SetLine(uint32_t line) {
1326f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, line);
133f034231aSEd Maste
1345f29bb8aSDimitry Andric ref().line = line;
1355f29bb8aSDimitry Andric }
1365f29bb8aSDimitry Andric
SetColumn(uint32_t column)1375f29bb8aSDimitry Andric void SBLineEntry::SetColumn(uint32_t column) {
1386f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, column);
1395f29bb8aSDimitry Andric
1405f29bb8aSDimitry Andric ref().line = column;
1415f29bb8aSDimitry Andric }
142f034231aSEd Maste
operator ==(const SBLineEntry & rhs) const14314f1b3e8SDimitry Andric bool SBLineEntry::operator==(const SBLineEntry &rhs) const {
1446f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
1455f29bb8aSDimitry Andric
1465f29bb8aSDimitry Andric lldb_private::LineEntry *lhs_ptr = m_opaque_up.get();
1475f29bb8aSDimitry Andric lldb_private::LineEntry *rhs_ptr = rhs.m_opaque_up.get();
148f034231aSEd Maste
149f034231aSEd Maste if (lhs_ptr && rhs_ptr)
150f034231aSEd Maste return lldb_private::LineEntry::Compare(*lhs_ptr, *rhs_ptr) == 0;
151f034231aSEd Maste
152f034231aSEd Maste return lhs_ptr == rhs_ptr;
153f034231aSEd Maste }
154f034231aSEd Maste
operator !=(const SBLineEntry & rhs) const15514f1b3e8SDimitry Andric bool SBLineEntry::operator!=(const SBLineEntry &rhs) const {
1566f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
1575f29bb8aSDimitry Andric
1585f29bb8aSDimitry Andric lldb_private::LineEntry *lhs_ptr = m_opaque_up.get();
1595f29bb8aSDimitry Andric lldb_private::LineEntry *rhs_ptr = rhs.m_opaque_up.get();
160f034231aSEd Maste
161f034231aSEd Maste if (lhs_ptr && rhs_ptr)
162f034231aSEd Maste return lldb_private::LineEntry::Compare(*lhs_ptr, *rhs_ptr) != 0;
163f034231aSEd Maste
164f034231aSEd Maste return lhs_ptr != rhs_ptr;
165f034231aSEd Maste }
166f034231aSEd Maste
operator ->() const16714f1b3e8SDimitry Andric const lldb_private::LineEntry *SBLineEntry::operator->() const {
1685f29bb8aSDimitry Andric return m_opaque_up.get();
169f034231aSEd Maste }
170f034231aSEd Maste
ref()17114f1b3e8SDimitry Andric lldb_private::LineEntry &SBLineEntry::ref() {
1725f29bb8aSDimitry Andric if (m_opaque_up == nullptr)
173cfca06d7SDimitry Andric m_opaque_up = std::make_unique<lldb_private::LineEntry>();
1745f29bb8aSDimitry Andric return *m_opaque_up;
175f034231aSEd Maste }
176f034231aSEd Maste
ref() const1775f29bb8aSDimitry Andric const lldb_private::LineEntry &SBLineEntry::ref() const { return *m_opaque_up; }
178f034231aSEd Maste
GetDescription(SBStream & description)17914f1b3e8SDimitry Andric bool SBLineEntry::GetDescription(SBStream &description) {
1806f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, description);
1815f29bb8aSDimitry Andric
182f034231aSEd Maste Stream &strm = description.ref();
183f034231aSEd Maste
1845f29bb8aSDimitry Andric if (m_opaque_up) {
185f034231aSEd Maste char file_path[PATH_MAX * 2];
186ac9a064cSDimitry Andric m_opaque_up->GetFile().GetPath(file_path, sizeof(file_path));
187f034231aSEd Maste strm.Printf("%s:%u", file_path, GetLine());
188f034231aSEd Maste if (GetColumn() > 0)
189f034231aSEd Maste strm.Printf(":%u", GetColumn());
19014f1b3e8SDimitry Andric } else
191f034231aSEd Maste strm.PutCString("No value");
192f034231aSEd Maste
193f034231aSEd Maste return true;
194f034231aSEd Maste }
195f034231aSEd Maste
get()1965f29bb8aSDimitry Andric lldb_private::LineEntry *SBLineEntry::get() { return m_opaque_up.get(); }
197