1 //===-- SBLineEntry.cpp -----------------------------------------*- C++ -*-===//
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 "SBReproducerPrivate.h"
11 #include "Utils.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/Host/PosixApi.h"
14 #include "lldb/Symbol/LineEntry.h"
15 #include "lldb/Utility/Log.h"
16 #include "lldb/Utility/StreamString.h"
17 
18 #include <limits.h>
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 SBLineEntry::SBLineEntry() : m_opaque_up() {
24   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBLineEntry);
25 }
26 
27 SBLineEntry::SBLineEntry(const SBLineEntry &rhs) : m_opaque_up() {
28   LLDB_RECORD_CONSTRUCTOR(SBLineEntry, (const lldb::SBLineEntry &), rhs);
29 
30   m_opaque_up = clone(rhs.m_opaque_up);
31 }
32 
33 SBLineEntry::SBLineEntry(const lldb_private::LineEntry *lldb_object_ptr)
34     : m_opaque_up() {
35   if (lldb_object_ptr)
36     m_opaque_up = llvm::make_unique<LineEntry>(*lldb_object_ptr);
37 }
38 
39 const SBLineEntry &SBLineEntry::operator=(const SBLineEntry &rhs) {
40   LLDB_RECORD_METHOD(const lldb::SBLineEntry &,
41                      SBLineEntry, operator=,(const lldb::SBLineEntry &), rhs);
42 
43   if (this != &rhs)
44     m_opaque_up = clone(rhs.m_opaque_up);
45   return *this;
46 }
47 
48 void SBLineEntry::SetLineEntry(const lldb_private::LineEntry &lldb_object_ref) {
49   m_opaque_up = llvm::make_unique<LineEntry>(lldb_object_ref);
50 }
51 
52 SBLineEntry::~SBLineEntry() {}
53 
54 SBAddress SBLineEntry::GetStartAddress() const {
55   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBAddress, SBLineEntry,
56                                    GetStartAddress);
57 
58   SBAddress sb_address;
59   if (m_opaque_up)
60     sb_address.SetAddress(&m_opaque_up->range.GetBaseAddress());
61 
62   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
63   if (log) {
64     StreamString sstr;
65     const Address *addr = sb_address.get();
66     if (addr)
67       addr->Dump(&sstr, NULL, Address::DumpStyleModuleWithFileAddress,
68                  Address::DumpStyleInvalid, 4);
69     log->Printf("SBLineEntry(%p)::GetStartAddress () => SBAddress (%p): %s",
70                 static_cast<void *>(m_opaque_up.get()),
71                 static_cast<void *>(sb_address.get()), sstr.GetData());
72   }
73 
74   return LLDB_RECORD_RESULT(sb_address);
75 }
76 
77 SBAddress SBLineEntry::GetEndAddress() const {
78   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBAddress, SBLineEntry, GetEndAddress);
79 
80   SBAddress sb_address;
81   if (m_opaque_up) {
82     sb_address.SetAddress(&m_opaque_up->range.GetBaseAddress());
83     sb_address.OffsetAddress(m_opaque_up->range.GetByteSize());
84   }
85   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
86   if (log) {
87     StreamString sstr;
88     const Address *addr = sb_address.get();
89     if (addr)
90       addr->Dump(&sstr, NULL, Address::DumpStyleModuleWithFileAddress,
91                  Address::DumpStyleInvalid, 4);
92     log->Printf("SBLineEntry(%p)::GetEndAddress () => SBAddress (%p): %s",
93                 static_cast<void *>(m_opaque_up.get()),
94                 static_cast<void *>(sb_address.get()), sstr.GetData());
95   }
96   return LLDB_RECORD_RESULT(sb_address);
97 }
98 
99 bool SBLineEntry::IsValid() const {
100   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBLineEntry, IsValid);
101 
102   return m_opaque_up.get() && m_opaque_up->IsValid();
103 }
104 
105 SBFileSpec SBLineEntry::GetFileSpec() const {
106   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBLineEntry, GetFileSpec);
107 
108   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
109 
110   SBFileSpec sb_file_spec;
111   if (m_opaque_up.get() && m_opaque_up->file)
112     sb_file_spec.SetFileSpec(m_opaque_up->file);
113 
114   if (log) {
115     SBStream sstr;
116     sb_file_spec.GetDescription(sstr);
117     log->Printf("SBLineEntry(%p)::GetFileSpec () => SBFileSpec(%p): %s",
118                 static_cast<void *>(m_opaque_up.get()),
119                 static_cast<const void *>(sb_file_spec.get()), sstr.GetData());
120   }
121 
122   return LLDB_RECORD_RESULT(sb_file_spec);
123 }
124 
125 uint32_t SBLineEntry::GetLine() const {
126   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBLineEntry, GetLine);
127 
128   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
129 
130   uint32_t line = 0;
131   if (m_opaque_up)
132     line = m_opaque_up->line;
133 
134   if (log)
135     log->Printf("SBLineEntry(%p)::GetLine () => %u",
136                 static_cast<void *>(m_opaque_up.get()), line);
137 
138   return line;
139 }
140 
141 uint32_t SBLineEntry::GetColumn() const {
142   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBLineEntry, GetColumn);
143 
144   if (m_opaque_up)
145     return m_opaque_up->column;
146   return 0;
147 }
148 
149 void SBLineEntry::SetFileSpec(lldb::SBFileSpec filespec) {
150   LLDB_RECORD_METHOD(void, SBLineEntry, SetFileSpec, (lldb::SBFileSpec),
151                      filespec);
152 
153   if (filespec.IsValid())
154     ref().file = filespec.ref();
155   else
156     ref().file.Clear();
157 }
158 void SBLineEntry::SetLine(uint32_t line) {
159   LLDB_RECORD_METHOD(void, SBLineEntry, SetLine, (uint32_t), line);
160 
161   ref().line = line;
162 }
163 
164 void SBLineEntry::SetColumn(uint32_t column) {
165   LLDB_RECORD_METHOD(void, SBLineEntry, SetColumn, (uint32_t), column);
166 
167   ref().line = column;
168 }
169 
170 bool SBLineEntry::operator==(const SBLineEntry &rhs) const {
171   LLDB_RECORD_METHOD_CONST(
172       bool, SBLineEntry, operator==,(const lldb::SBLineEntry &), rhs);
173 
174   lldb_private::LineEntry *lhs_ptr = m_opaque_up.get();
175   lldb_private::LineEntry *rhs_ptr = rhs.m_opaque_up.get();
176 
177   if (lhs_ptr && rhs_ptr)
178     return lldb_private::LineEntry::Compare(*lhs_ptr, *rhs_ptr) == 0;
179 
180   return lhs_ptr == rhs_ptr;
181 }
182 
183 bool SBLineEntry::operator!=(const SBLineEntry &rhs) const {
184   LLDB_RECORD_METHOD_CONST(
185       bool, SBLineEntry, operator!=,(const lldb::SBLineEntry &), rhs);
186 
187   lldb_private::LineEntry *lhs_ptr = m_opaque_up.get();
188   lldb_private::LineEntry *rhs_ptr = rhs.m_opaque_up.get();
189 
190   if (lhs_ptr && rhs_ptr)
191     return lldb_private::LineEntry::Compare(*lhs_ptr, *rhs_ptr) != 0;
192 
193   return lhs_ptr != rhs_ptr;
194 }
195 
196 const lldb_private::LineEntry *SBLineEntry::operator->() const {
197   return m_opaque_up.get();
198 }
199 
200 lldb_private::LineEntry &SBLineEntry::ref() {
201   if (m_opaque_up == NULL)
202     m_opaque_up.reset(new lldb_private::LineEntry());
203   return *m_opaque_up;
204 }
205 
206 const lldb_private::LineEntry &SBLineEntry::ref() const { return *m_opaque_up; }
207 
208 bool SBLineEntry::GetDescription(SBStream &description) {
209   LLDB_RECORD_METHOD(bool, SBLineEntry, GetDescription, (lldb::SBStream &),
210                      description);
211 
212   Stream &strm = description.ref();
213 
214   if (m_opaque_up) {
215     char file_path[PATH_MAX * 2];
216     m_opaque_up->file.GetPath(file_path, sizeof(file_path));
217     strm.Printf("%s:%u", file_path, GetLine());
218     if (GetColumn() > 0)
219       strm.Printf(":%u", GetColumn());
220   } else
221     strm.PutCString("No value");
222 
223   return true;
224 }
225 
226 lldb_private::LineEntry *SBLineEntry::get() { return m_opaque_up.get(); }
227