| 1 | //===-- SBLineEntry.cpp ---------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/API/SBLineEntry.h" |
| 10 | #include "Utils.h" |
| 11 | #include "lldb/API/SBStream.h" |
| 12 | #include "lldb/Host/PosixApi.h" |
| 13 | #include "lldb/Symbol/LineEntry.h" |
| 14 | #include "lldb/Utility/Instrumentation.h" |
| 15 | #include "lldb/Utility/StreamString.h" |
| 16 | |
| 17 | #include <climits> |
| 18 | |
| 19 | using namespace lldb; |
| 20 | using namespace lldb_private; |
| 21 | |
| 22 | SBLineEntry::SBLineEntry() { LLDB_INSTRUMENT_VA(this); } |
| 23 | |
| 24 | SBLineEntry::SBLineEntry(const SBLineEntry &rhs) { |
| 25 | LLDB_INSTRUMENT_VA(this, rhs); |
| 26 | |
| 27 | m_opaque_up = clone(src: rhs.m_opaque_up); |
| 28 | } |
| 29 | |
| 30 | SBLineEntry::SBLineEntry(const lldb_private::LineEntry *lldb_object_ptr) { |
| 31 | if (lldb_object_ptr) |
| 32 | m_opaque_up = std::make_unique<LineEntry>(args: *lldb_object_ptr); |
| 33 | } |
| 34 | |
| 35 | const SBLineEntry &SBLineEntry::operator=(const SBLineEntry &rhs) { |
| 36 | LLDB_INSTRUMENT_VA(this, rhs); |
| 37 | |
| 38 | if (this != &rhs) |
| 39 | m_opaque_up = clone(src: rhs.m_opaque_up); |
| 40 | return *this; |
| 41 | } |
| 42 | |
| 43 | void SBLineEntry::SetLineEntry(const lldb_private::LineEntry &lldb_object_ref) { |
| 44 | m_opaque_up = std::make_unique<LineEntry>(args: lldb_object_ref); |
| 45 | } |
| 46 | |
| 47 | SBLineEntry::~SBLineEntry() = default; |
| 48 | |
| 49 | SBAddress SBLineEntry::GetStartAddress() const { |
| 50 | LLDB_INSTRUMENT_VA(this); |
| 51 | |
| 52 | SBAddress sb_address; |
| 53 | if (m_opaque_up) |
| 54 | sb_address.SetAddress(m_opaque_up->range.GetBaseAddress()); |
| 55 | |
| 56 | return sb_address; |
| 57 | } |
| 58 | |
| 59 | SBAddress SBLineEntry::GetEndAddress() const { |
| 60 | LLDB_INSTRUMENT_VA(this); |
| 61 | |
| 62 | SBAddress sb_address; |
| 63 | if (m_opaque_up) { |
| 64 | sb_address.SetAddress(m_opaque_up->range.GetBaseAddress()); |
| 65 | sb_address.OffsetAddress(offset: m_opaque_up->range.GetByteSize()); |
| 66 | } |
| 67 | return sb_address; |
| 68 | } |
| 69 | |
| 70 | SBAddress SBLineEntry::GetSameLineContiguousAddressRangeEnd( |
| 71 | bool include_inlined_functions) const { |
| 72 | LLDB_INSTRUMENT_VA(this); |
| 73 | |
| 74 | SBAddress sb_address; |
| 75 | if (m_opaque_up) { |
| 76 | AddressRange line_range = m_opaque_up->GetSameLineContiguousAddressRange( |
| 77 | include_inlined_functions); |
| 78 | |
| 79 | sb_address.SetAddress(line_range.GetBaseAddress()); |
| 80 | sb_address.OffsetAddress(offset: line_range.GetByteSize()); |
| 81 | } |
| 82 | return sb_address; |
| 83 | } |
| 84 | |
| 85 | bool SBLineEntry::IsValid() const { |
| 86 | LLDB_INSTRUMENT_VA(this); |
| 87 | return this->operator bool(); |
| 88 | } |
| 89 | SBLineEntry::operator bool() const { |
| 90 | LLDB_INSTRUMENT_VA(this); |
| 91 | |
| 92 | return m_opaque_up.get() && m_opaque_up->IsValid(); |
| 93 | } |
| 94 | |
| 95 | SBFileSpec SBLineEntry::GetFileSpec() const { |
| 96 | LLDB_INSTRUMENT_VA(this); |
| 97 | |
| 98 | SBFileSpec sb_file_spec; |
| 99 | if (m_opaque_up.get() && m_opaque_up->GetFile()) |
| 100 | sb_file_spec.SetFileSpec(m_opaque_up->GetFile()); |
| 101 | |
| 102 | return sb_file_spec; |
| 103 | } |
| 104 | |
| 105 | uint32_t SBLineEntry::GetLine() const { |
| 106 | LLDB_INSTRUMENT_VA(this); |
| 107 | |
| 108 | uint32_t line = 0; |
| 109 | if (m_opaque_up) |
| 110 | line = m_opaque_up->line; |
| 111 | |
| 112 | return line; |
| 113 | } |
| 114 | |
| 115 | uint32_t SBLineEntry::GetColumn() const { |
| 116 | LLDB_INSTRUMENT_VA(this); |
| 117 | |
| 118 | if (m_opaque_up) |
| 119 | return m_opaque_up->column; |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | void SBLineEntry::SetFileSpec(lldb::SBFileSpec filespec) { |
| 124 | LLDB_INSTRUMENT_VA(this, filespec); |
| 125 | |
| 126 | if (filespec.IsValid()) |
| 127 | ref().file_sp = std::make_shared<SupportFile>(args: filespec.ref()); |
| 128 | else |
| 129 | ref().file_sp = std::make_shared<SupportFile>(); |
| 130 | } |
| 131 | void SBLineEntry::SetLine(uint32_t line) { |
| 132 | LLDB_INSTRUMENT_VA(this, line); |
| 133 | |
| 134 | ref().line = line; |
| 135 | } |
| 136 | |
| 137 | void SBLineEntry::SetColumn(uint32_t column) { |
| 138 | LLDB_INSTRUMENT_VA(this, column); |
| 139 | |
| 140 | ref().column = column; |
| 141 | } |
| 142 | |
| 143 | bool SBLineEntry::operator==(const SBLineEntry &rhs) const { |
| 144 | LLDB_INSTRUMENT_VA(this, rhs); |
| 145 | |
| 146 | lldb_private::LineEntry *lhs_ptr = m_opaque_up.get(); |
| 147 | lldb_private::LineEntry *rhs_ptr = rhs.m_opaque_up.get(); |
| 148 | |
| 149 | if (lhs_ptr && rhs_ptr) |
| 150 | return lldb_private::LineEntry::Compare(lhs: *lhs_ptr, rhs: *rhs_ptr) == 0; |
| 151 | |
| 152 | return lhs_ptr == rhs_ptr; |
| 153 | } |
| 154 | |
| 155 | bool SBLineEntry::operator!=(const SBLineEntry &rhs) const { |
| 156 | LLDB_INSTRUMENT_VA(this, rhs); |
| 157 | |
| 158 | lldb_private::LineEntry *lhs_ptr = m_opaque_up.get(); |
| 159 | lldb_private::LineEntry *rhs_ptr = rhs.m_opaque_up.get(); |
| 160 | |
| 161 | if (lhs_ptr && rhs_ptr) |
| 162 | return lldb_private::LineEntry::Compare(lhs: *lhs_ptr, rhs: *rhs_ptr) != 0; |
| 163 | |
| 164 | return lhs_ptr != rhs_ptr; |
| 165 | } |
| 166 | |
| 167 | const lldb_private::LineEntry *SBLineEntry::operator->() const { |
| 168 | return m_opaque_up.get(); |
| 169 | } |
| 170 | |
| 171 | lldb_private::LineEntry &SBLineEntry::ref() { |
| 172 | if (m_opaque_up == nullptr) |
| 173 | m_opaque_up = std::make_unique<lldb_private::LineEntry>(); |
| 174 | return *m_opaque_up; |
| 175 | } |
| 176 | |
| 177 | const lldb_private::LineEntry &SBLineEntry::ref() const { return *m_opaque_up; } |
| 178 | |
| 179 | bool SBLineEntry::GetDescription(SBStream &description) { |
| 180 | LLDB_INSTRUMENT_VA(this, description); |
| 181 | |
| 182 | Stream &strm = description.ref(); |
| 183 | |
| 184 | if (m_opaque_up) { |
| 185 | char file_path[PATH_MAX * 2]; |
| 186 | m_opaque_up->GetFile().GetPath(path: file_path, max_path_length: sizeof(file_path)); |
| 187 | strm.Printf(format: "%s:%u" , file_path, GetLine()); |
| 188 | if (GetColumn() > 0) |
| 189 | strm.Printf(format: ":%u" , GetColumn()); |
| 190 | } else |
| 191 | strm.PutCString(cstr: "No value" ); |
| 192 | |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | lldb_private::LineEntry *SBLineEntry::get() { return m_opaque_up.get(); } |
| 197 | |