blob: f873526623663ef546f0f5fddd3eac312c6c1bf6 [file] [log] [blame]
Enrico Granata91544802011-09-06 19:20:51 +00001//===-- SBData.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/API/SBData.h"
11#include "lldb/API/SBError.h"
12#include "lldb/API/SBStream.h"
13
14#include "lldb/Core/DataExtractor.h"
15#include "lldb/Core/Log.h"
Greg Clayton96154be2011-11-13 06:57:31 +000016#include "lldb/Core/Stream.h"
17
Enrico Granata91544802011-09-06 19:20:51 +000018
19using namespace lldb;
20using namespace lldb_private;
21
22SBData::SBData ()
23{
24}
25
26SBData::SBData (const lldb::DataExtractorSP& data_sp) :
27 m_opaque_sp (data_sp)
28{
29}
30
31SBData::SBData(const SBData &rhs) :
32 m_opaque_sp (rhs.m_opaque_sp)
33{
34}
35
36const SBData &
37SBData::operator = (const SBData &rhs)
38{
39 if (this != &rhs)
40 m_opaque_sp = rhs.m_opaque_sp;
41 return *this;
42}
43
44SBData::~SBData ()
45{
46}
47
48void
49SBData::SetOpaque (const lldb::DataExtractorSP &data_sp)
50{
51 m_opaque_sp = data_sp;
52}
53
54lldb_private::DataExtractor *
55SBData::get() const
56{
57 return m_opaque_sp.get();
58}
59
60lldb_private::DataExtractor *
61SBData::operator->() const
62{
63 return m_opaque_sp.operator->();
64}
65
66lldb::DataExtractorSP &
67SBData::operator*()
68{
69 return m_opaque_sp;
70}
71
72const lldb::DataExtractorSP &
73SBData::operator*() const
74{
75 return m_opaque_sp;
76}
77
78bool
79SBData::IsValid()
80{
81 return m_opaque_sp.get() != NULL;
82}
83
84uint8_t
85SBData::GetAddressByteSize ()
86{
87 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
88 uint8_t value = 0;
89 if (m_opaque_sp.get())
90 value = m_opaque_sp->GetAddressByteSize();
91 if (log)
92 log->Printf ("SBData::GetAddressByteSize () => "
93 "(%i)", value);
94 return value;
95}
96
97void
98SBData::Clear ()
99{
100 if (m_opaque_sp.get())
101 m_opaque_sp->Clear();
102}
103
104size_t
105SBData::GetByteSize ()
106{
107 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
108 size_t value = 0;
109 if (m_opaque_sp.get())
110 value = m_opaque_sp->GetByteSize();
111 if (log)
112 log->Printf ("SBData::GetByteSize () => "
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000113 "(%lu)", value);
Enrico Granata91544802011-09-06 19:20:51 +0000114 return value;
115}
116
117lldb::ByteOrder
118SBData::GetByteOrder ()
119{
120 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
121 lldb::ByteOrder value = eByteOrderInvalid;
122 if (m_opaque_sp.get())
123 value = m_opaque_sp->GetByteOrder();
124 if (log)
125 log->Printf ("SBData::GetByteOrder () => "
126 "(%i)", value);
127 return value;
128}
129
130float
131SBData::GetFloat (lldb::SBError& error, uint32_t offset)
132{
133 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
134 float value = 0;
135 if (!m_opaque_sp.get())
136 {
137 error.SetErrorString("no value to read from");
138 }
139 else
140 {
141 uint32_t old_offset = offset;
142 value = m_opaque_sp->GetFloat(&offset);
143 if (offset == old_offset)
144 error.SetErrorString("unable to read data");
145 }
146 if (log)
147 log->Printf ("SBData::GetFloat (error=%p,offset=%d) => "
148 "(%f)", error.get(), offset, value);
149 return value;
150}
151
152double
153SBData::GetDouble (lldb::SBError& error, uint32_t offset)
154{
155 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
156 double value = 0;
157 if (!m_opaque_sp.get())
158 {
159 error.SetErrorString("no value to read from");
160 }
161 else
162 {
163 uint32_t old_offset = offset;
164 value = m_opaque_sp->GetDouble(&offset);
165 if (offset == old_offset)
166 error.SetErrorString("unable to read data");
167 }
168 if (log)
169 log->Printf ("SBData::GetDouble (error=%p,offset=%d) => "
170 "(%f)", error.get(), offset, value);
171 return value;
172}
173
174long double
175SBData::GetLongDouble (lldb::SBError& error, uint32_t offset)
176{
177 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
178 long double value = 0;
179 if (!m_opaque_sp.get())
180 {
181 error.SetErrorString("no value to read from");
182 }
183 else
184 {
185 uint32_t old_offset = offset;
186 value = m_opaque_sp->GetLongDouble(&offset);
187 if (offset == old_offset)
188 error.SetErrorString("unable to read data");
189 }
190 if (log)
191 log->Printf ("SBData::GetLongDouble (error=%p,offset=%d) => "
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000192 "(%Lf)", error.get(), offset, value);
Enrico Granata91544802011-09-06 19:20:51 +0000193 return value;
194}
195
196lldb::addr_t
197SBData::GetAddress (lldb::SBError& error, uint32_t offset)
198{
199 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
200 lldb::addr_t value = 0;
201 if (!m_opaque_sp.get())
202 {
203 error.SetErrorString("no value to read from");
204 }
205 else
206 {
207 uint32_t old_offset = offset;
208 value = m_opaque_sp->GetAddress(&offset);
209 if (offset == old_offset)
210 error.SetErrorString("unable to read data");
211 }
212 if (log)
213 log->Printf ("SBData::GetAddress (error=%p,offset=%d) => "
214 "(%p)", error.get(), offset, (void*)value);
215 return value;
216}
217
218uint8_t
219SBData::GetUnsignedInt8 (lldb::SBError& error, uint32_t offset)
220{
221 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
222 uint8_t value = 0;
223 if (!m_opaque_sp.get())
224 {
225 error.SetErrorString("no value to read from");
226 }
227 else
228 {
229 uint32_t old_offset = offset;
230 value = m_opaque_sp->GetU8(&offset);
231 if (offset == old_offset)
232 error.SetErrorString("unable to read data");
233 }
234 if (log)
235 log->Printf ("SBData::GetUnsignedInt8 (error=%p,offset=%d) => "
236 "(%c)", error.get(), offset, value);
237 return value;
238}
239
240uint16_t
241SBData::GetUnsignedInt16 (lldb::SBError& error, uint32_t offset)
242{
243 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
244 uint16_t value = 0;
245 if (!m_opaque_sp.get())
246 {
247 error.SetErrorString("no value to read from");
248 }
249 else
250 {
251 uint32_t old_offset = offset;
252 value = m_opaque_sp->GetU16(&offset);
253 if (offset == old_offset)
254 error.SetErrorString("unable to read data");
255 }
256 if (log)
257 log->Printf ("SBData::GetUnsignedInt16 (error=%p,offset=%d) => "
258 "(%hd)", error.get(), offset, value);
259 return value;
260}
261
262uint32_t
263SBData::GetUnsignedInt32 (lldb::SBError& error, uint32_t offset)
264{
265 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
266 uint32_t value = 0;
267 if (!m_opaque_sp.get())
268 {
269 error.SetErrorString("no value to read from");
270 }
271 else
272 {
273 uint32_t old_offset = offset;
274 value = m_opaque_sp->GetU32(&offset);
275 if (offset == old_offset)
276 error.SetErrorString("unable to read data");
277 }
278 if (log)
279 log->Printf ("SBData::GetUnsignedInt32 (error=%p,offset=%d) => "
280 "(%d)", error.get(), offset, value);
281 return value;
282}
283
284uint64_t
285SBData::GetUnsignedInt64 (lldb::SBError& error, uint32_t offset)
286{
287 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
288 uint64_t value = 0;
289 if (!m_opaque_sp.get())
290 {
291 error.SetErrorString("no value to read from");
292 }
293 else
294 {
295 uint32_t old_offset = offset;
296 value = m_opaque_sp->GetU64(&offset);
297 if (offset == old_offset)
298 error.SetErrorString("unable to read data");
299 }
300 if (log)
301 log->Printf ("SBData::GetUnsignedInt64 (error=%p,offset=%d) => "
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000302 "(%lld)", error.get(), offset, value);
Enrico Granata91544802011-09-06 19:20:51 +0000303 return value;
304}
305
306int8_t
307SBData::GetSignedInt8 (lldb::SBError& error, uint32_t offset)
308{
309 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
310 int8_t value = 0;
311 if (!m_opaque_sp.get())
312 {
313 error.SetErrorString("no value to read from");
314 }
315 else
316 {
317 uint32_t old_offset = offset;
318 value = (int8_t)m_opaque_sp->GetMaxS64(&offset, 1);
319 if (offset == old_offset)
320 error.SetErrorString("unable to read data");
321 }
322 if (log)
323 log->Printf ("SBData::GetSignedInt8 (error=%p,offset=%d) => "
324 "(%c)", error.get(), offset, value);
325 return value;
326}
327
328int16_t
329SBData::GetSignedInt16 (lldb::SBError& error, uint32_t offset)
330{
331 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
332 int16_t value = 0;
333 if (!m_opaque_sp.get())
334 {
335 error.SetErrorString("no value to read from");
336 }
337 else
338 {
339 uint32_t old_offset = offset;
340 value = (int16_t)m_opaque_sp->GetMaxS64(&offset, 2);
341 if (offset == old_offset)
342 error.SetErrorString("unable to read data");
343 }
344 if (log)
345 log->Printf ("SBData::GetSignedInt16 (error=%p,offset=%d) => "
346 "(%hd)", error.get(), offset, value);
347 return value;
348}
349
350int32_t
351SBData::GetSignedInt32 (lldb::SBError& error, uint32_t offset)
352{
353 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
354 int32_t value = 0;
355 if (!m_opaque_sp.get())
356 {
357 error.SetErrorString("no value to read from");
358 }
359 else
360 {
361 uint32_t old_offset = offset;
362 value = (int32_t)m_opaque_sp->GetMaxS64(&offset, 4);
363 if (offset == old_offset)
364 error.SetErrorString("unable to read data");
365 }
366 if (log)
367 log->Printf ("SBData::GetSignedInt32 (error=%p,offset=%d) => "
368 "(%d)", error.get(), offset, value);
369 return value;
370}
371
372int64_t
373SBData::GetSignedInt64 (lldb::SBError& error, uint32_t offset)
374{
375 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
376 int64_t value = 0;
377 if (!m_opaque_sp.get())
378 {
379 error.SetErrorString("no value to read from");
380 }
381 else
382 {
383 uint32_t old_offset = offset;
384 value = (int64_t)m_opaque_sp->GetMaxS64(&offset, 8);
385 if (offset == old_offset)
386 error.SetErrorString("unable to read data");
387 }
388 if (log)
389 log->Printf ("SBData::GetSignedInt64 (error=%p,offset=%d) => "
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000390 "(%lld)", error.get(), offset, value);
Enrico Granata91544802011-09-06 19:20:51 +0000391 return value;
392}
393
394const char*
395SBData::GetString (lldb::SBError& error, uint32_t offset)
396{
397 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
398 const char* value = 0;
399 if (!m_opaque_sp.get())
400 {
401 error.SetErrorString("no value to read from");
402 }
403 else
404 {
405 uint32_t old_offset = offset;
406 value = m_opaque_sp->GetCStr(&offset);
407 if (offset == old_offset || (value == NULL))
408 error.SetErrorString("unable to read data");
409 }
410 if (log)
411 log->Printf ("SBData::GetString (error=%p,offset=%d) => "
412 "(%p)", error.get(), offset, value);
413 return value;
414}
415
416bool
Greg Clayton15ef51e2011-09-24 05:04:40 +0000417SBData::GetDescription (lldb::SBStream &description, lldb::addr_t base_addr)
Enrico Granata91544802011-09-06 19:20:51 +0000418{
Greg Clayton96154be2011-11-13 06:57:31 +0000419 Stream &strm = description.ref();
420
Enrico Granata91544802011-09-06 19:20:51 +0000421 if (m_opaque_sp)
422 {
Greg Clayton96154be2011-11-13 06:57:31 +0000423 m_opaque_sp->Dump (&strm,
424 0,
425 lldb::eFormatBytesWithASCII,
426 1,
427 m_opaque_sp->GetByteSize(),
428 16,
429 base_addr,
430 0,
431 0);
Enrico Granata91544802011-09-06 19:20:51 +0000432 }
433 else
Greg Clayton96154be2011-11-13 06:57:31 +0000434 strm.PutCString ("No value");
Enrico Granata91544802011-09-06 19:20:51 +0000435
436 return true;
437}
438
439size_t
440SBData::ReadRawData (lldb::SBError& error,
441 uint32_t offset,
442 void *buf,
443 size_t size)
444{
445 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
446 void* ok = NULL;
447 if (!m_opaque_sp.get())
448 {
449 error.SetErrorString("no value to read from");
450 }
451 else
452 {
453 uint32_t old_offset = offset;
454 ok = m_opaque_sp->GetU8(&offset, buf, size);
455 if ((offset == old_offset) || (ok == NULL))
456 error.SetErrorString("unable to read data");
457 }
458 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000459 log->Printf ("SBData::ReadRawData (error=%p,offset=%d,buf=%p,size=%lu) => "
Enrico Granata91544802011-09-06 19:20:51 +0000460 "(%p)", error.get(), offset, buf, size, ok);
461 return ok ? size : 0;
462}
463
464void
465SBData::SetData(lldb::SBError& error,
466 const void *buf,
467 size_t size,
468 lldb::ByteOrder endian,
469 uint8_t addr_size)
470{
471 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
472 if (!m_opaque_sp.get())
473 m_opaque_sp.reset(new DataExtractor(buf, size, endian, addr_size));
474 else
475 m_opaque_sp->SetData(buf, size, endian);
476 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000477 log->Printf ("SBData::SetData (error=%p,buf=%p,size=%lu,endian=%d,addr_size=%c) => "
Enrico Granata91544802011-09-06 19:20:51 +0000478 "(%p)", error.get(), buf, size, endian, addr_size, m_opaque_sp.get());
479}
480
481bool
482SBData::Append(const SBData& rhs)
483{
484 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
485 bool value = false;
486 if (m_opaque_sp.get() && rhs.m_opaque_sp.get())
487 value = m_opaque_sp.get()->Append(*rhs.m_opaque_sp);
488 if (log)
489 log->Printf ("SBData::Append (rhs=%p) => "
490 "(%s)", rhs.get(), value ? "true" : "false");
491 return value;
Greg Clayton141f8d92011-10-12 00:53:29 +0000492}