[Go to site: main page, start]

LLDB mainline
SBData.cpp
Go to the documentation of this file.
1//===-- SBData.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/SBData.h"
10#include "lldb/API/SBError.h"
11#include "lldb/API/SBStream.h"
13
18#include "lldb/Utility/Stream.h"
19
20#include <cinttypes>
21#include <memory>
22
23using namespace lldb;
24using namespace lldb_private;
25
29
31
33 LLDB_INSTRUMENT_VA(this, rhs);
34}
35
36const SBData &SBData::operator=(const SBData &rhs) {
37 LLDB_INSTRUMENT_VA(this, rhs);
38
39 if (this != &rhs)
41 return *this;
42}
43
44SBData::~SBData() = default;
45
47 m_opaque_sp = data_sp;
48}
49
51
53 return m_opaque_sp.operator->();
54}
55
57
59
62 return this->operator bool();
63}
64SBData::operator bool() const {
66
67 return m_opaque_sp.get() != nullptr;
68}
69
72
73 uint8_t value = 0;
74 if (m_opaque_sp.get())
75 value = m_opaque_sp->GetAddressByteSize();
76 return value;
77}
78
79void SBData::SetAddressByteSize(uint8_t addr_byte_size) {
80 LLDB_INSTRUMENT_VA(this, addr_byte_size);
81
82 if (m_opaque_sp.get())
83 m_opaque_sp->SetAddressByteSize(addr_byte_size);
84}
85
88
89 if (m_opaque_sp.get())
90 m_opaque_sp->Clear();
91}
92
95
96 size_t value = 0;
97 if (m_opaque_sp.get())
98 value = m_opaque_sp->GetByteSize();
99 return value;
100}
101
103 LLDB_INSTRUMENT_VA(this);
104
106 if (m_opaque_sp.get())
107 value = m_opaque_sp->GetByteOrder();
108 return value;
109}
110
113
114 if (m_opaque_sp.get())
115 m_opaque_sp->SetByteOrder(endian);
116}
117
119 LLDB_INSTRUMENT_VA(this, error, offset);
120
121 float value = 0;
122 if (!m_opaque_sp.get()) {
123 error.SetErrorString("no value to read from");
124 } else {
125 uint32_t old_offset = offset;
126 value = m_opaque_sp->GetFloat(&offset);
127 if (offset == old_offset)
128 error.SetErrorString("unable to read data");
129 }
130 return value;
131}
132
134 LLDB_INSTRUMENT_VA(this, error, offset);
135
136 double value = 0;
137 if (!m_opaque_sp.get()) {
138 error.SetErrorString("no value to read from");
139 } else {
140 uint32_t old_offset = offset;
141 value = m_opaque_sp->GetDouble(&offset);
142 if (offset == old_offset)
143 error.SetErrorString("unable to read data");
144 }
145 return value;
146}
147
149 LLDB_INSTRUMENT_VA(this, error, offset);
150
151 long double value = 0;
152 if (!m_opaque_sp.get()) {
153 error.SetErrorString("no value to read from");
154 } else {
155 uint32_t old_offset = offset;
156 value = m_opaque_sp->GetLongDouble(&offset);
157 if (offset == old_offset)
158 error.SetErrorString("unable to read data");
159 }
160 return value;
161}
162
164 LLDB_INSTRUMENT_VA(this, error, offset);
165
166 lldb::addr_t value = 0;
167 if (!m_opaque_sp.get()) {
168 error.SetErrorString("no value to read from");
169 } else {
170 uint32_t old_offset = offset;
171 value = m_opaque_sp->GetAddress(&offset);
172 if (offset == old_offset)
173 error.SetErrorString("unable to read data");
174 }
175 return value;
176}
177
179 LLDB_INSTRUMENT_VA(this, error, offset);
180
181 uint8_t value = 0;
182 if (!m_opaque_sp.get()) {
183 error.SetErrorString("no value to read from");
184 } else {
185 uint32_t old_offset = offset;
186 value = m_opaque_sp->GetU8(&offset);
187 if (offset == old_offset)
188 error.SetErrorString("unable to read data");
189 }
190 return value;
191}
192
194 LLDB_INSTRUMENT_VA(this, error, offset);
195
196 uint16_t value = 0;
197 if (!m_opaque_sp.get()) {
198 error.SetErrorString("no value to read from");
199 } else {
200 uint32_t old_offset = offset;
201 value = m_opaque_sp->GetU16(&offset);
202 if (offset == old_offset)
203 error.SetErrorString("unable to read data");
204 }
205 return value;
206}
207
209 LLDB_INSTRUMENT_VA(this, error, offset);
210
211 uint32_t value = 0;
212 if (!m_opaque_sp.get()) {
213 error.SetErrorString("no value to read from");
214 } else {
215 uint32_t old_offset = offset;
216 value = m_opaque_sp->GetU32(&offset);
217 if (offset == old_offset)
218 error.SetErrorString("unable to read data");
219 }
220 return value;
221}
222
224 LLDB_INSTRUMENT_VA(this, error, offset);
225
226 uint64_t value = 0;
227 if (!m_opaque_sp.get()) {
228 error.SetErrorString("no value to read from");
229 } else {
230 uint32_t old_offset = offset;
231 value = m_opaque_sp->GetU64(&offset);
232 if (offset == old_offset)
233 error.SetErrorString("unable to read data");
234 }
235 return value;
236}
237
239 LLDB_INSTRUMENT_VA(this, error, offset);
240
241 int8_t value = 0;
242 if (!m_opaque_sp.get()) {
243 error.SetErrorString("no value to read from");
244 } else {
245 uint32_t old_offset = offset;
246 value = (int8_t)m_opaque_sp->GetMaxS64(&offset, 1);
247 if (offset == old_offset)
248 error.SetErrorString("unable to read data");
249 }
250 return value;
251}
252
254 LLDB_INSTRUMENT_VA(this, error, offset);
255
256 int16_t value = 0;
257 if (!m_opaque_sp.get()) {
258 error.SetErrorString("no value to read from");
259 } else {
260 uint32_t old_offset = offset;
261 value = (int16_t)m_opaque_sp->GetMaxS64(&offset, 2);
262 if (offset == old_offset)
263 error.SetErrorString("unable to read data");
264 }
265 return value;
266}
267
269 LLDB_INSTRUMENT_VA(this, error, offset);
270
271 int32_t value = 0;
272 if (!m_opaque_sp.get()) {
273 error.SetErrorString("no value to read from");
274 } else {
275 uint32_t old_offset = offset;
276 value = (int32_t)m_opaque_sp->GetMaxS64(&offset, 4);
277 if (offset == old_offset)
278 error.SetErrorString("unable to read data");
279 }
280 return value;
281}
282
284 LLDB_INSTRUMENT_VA(this, error, offset);
285
286 int64_t value = 0;
287 if (!m_opaque_sp.get()) {
288 error.SetErrorString("no value to read from");
289 } else {
290 uint32_t old_offset = offset;
291 value = (int64_t)m_opaque_sp->GetMaxS64(&offset, 8);
292 if (offset == old_offset)
293 error.SetErrorString("unable to read data");
294 }
295 return value;
296}
297
299 LLDB_INSTRUMENT_VA(this, error, offset);
300
301 if (!m_opaque_sp) {
302 error.SetErrorString("no value to read from");
303 return nullptr;
304 }
305
306 lldb::offset_t old_offset = offset;
307 const char *value = m_opaque_sp->GetCStr(&offset);
308 if (offset == old_offset || value == nullptr) {
309 error.SetErrorString("unable to read data");
310 return nullptr;
311 }
312
313 return ConstString(value).GetCString();
314}
315
317 lldb::addr_t base_addr) {
318 LLDB_INSTRUMENT_VA(this, description, base_addr);
319
320 Stream &strm = description.ref();
321
322 if (m_opaque_sp) {
324 m_opaque_sp->GetByteSize(), 16, base_addr, 0, 0);
325 } else
326 strm.PutCString("No value");
327
328 return true;
329}
330
332 void *buf, size_t size) {
333 LLDB_INSTRUMENT_VA(this, error, offset, buf, size);
334
335 void *ok = nullptr;
336 if (!m_opaque_sp.get()) {
337 error.SetErrorString("no value to read from");
338 } else {
339 uint32_t old_offset = offset;
340 ok = m_opaque_sp->GetU8(&offset, buf, size);
341 if ((offset == old_offset) || (ok == nullptr))
342 error.SetErrorString("unable to read data");
343 }
344 return ok ? size : 0;
345}
346
347void SBData::SetData(lldb::SBError &error, const void *buf, size_t size,
348 lldb::ByteOrder endian, uint8_t addr_size) {
349 LLDB_INSTRUMENT_VA(this, error, buf, size, endian, addr_size);
350
351 if (!m_opaque_sp.get())
352 m_opaque_sp = std::make_shared<DataExtractor>(buf, size, endian, addr_size);
353 else
354 {
355 m_opaque_sp->SetData(buf, size, endian);
356 m_opaque_sp->SetAddressByteSize(addr_size);
357 }
358}
359
361 size_t size, lldb::ByteOrder endian,
362 uint8_t addr_size) {
363 LLDB_INSTRUMENT_VA(this, error, buf, size, endian, addr_size);
364
365 lldb::DataBufferSP buffer_sp = std::make_shared<DataBufferHeap>(buf, size);
366
367 if (!m_opaque_sp.get())
368 m_opaque_sp = std::make_shared<DataExtractor>(buf, size, endian, addr_size);
369 else {
370 m_opaque_sp->SetData(buffer_sp);
371 m_opaque_sp->SetByteOrder(endian);
372 m_opaque_sp->SetAddressByteSize(addr_size);
373 }
374}
375
376bool SBData::Append(const SBData &rhs) {
377 LLDB_INSTRUMENT_VA(this, rhs);
378
379 bool value = false;
380 if (m_opaque_sp.get() && rhs.m_opaque_sp.get())
381 value = m_opaque_sp.get()->Append(*rhs.m_opaque_sp);
382 return value;
383}
384
386 uint32_t addr_byte_size,
387 const char *data) {
388 LLDB_INSTRUMENT_VA(endian, addr_byte_size, data);
389
390 if (!data || !data[0])
391 return SBData();
392
393 uint32_t data_len = strlen(data);
394
395 lldb::DataBufferSP buffer_sp(new DataBufferHeap(data, data_len));
396 lldb::DataExtractorSP data_sp(
397 new DataExtractor(buffer_sp, endian, addr_byte_size));
398
399 SBData ret(data_sp);
400
401 return ret;
402}
403
405 uint32_t addr_byte_size,
406 uint64_t *array,
407 size_t array_len) {
408 LLDB_INSTRUMENT_VA(endian, addr_byte_size, array, array_len);
409
410 if (!array || array_len == 0)
411 return SBData();
412
413 size_t data_len = array_len * sizeof(uint64_t);
414
415 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
416 lldb::DataExtractorSP data_sp(
417 new DataExtractor(buffer_sp, endian, addr_byte_size));
418
419 SBData ret(data_sp);
420
421 return ret;
422}
423
425 uint32_t addr_byte_size,
426 uint32_t *array,
427 size_t array_len) {
428 LLDB_INSTRUMENT_VA(endian, addr_byte_size, array, array_len);
429
430 if (!array || array_len == 0)
431 return SBData();
432
433 size_t data_len = array_len * sizeof(uint32_t);
434
435 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
436 lldb::DataExtractorSP data_sp(
437 new DataExtractor(buffer_sp, endian, addr_byte_size));
438
439 SBData ret(data_sp);
440
441 return ret;
442}
443
445 uint32_t addr_byte_size,
446 int64_t *array,
447 size_t array_len) {
448 LLDB_INSTRUMENT_VA(endian, addr_byte_size, array, array_len);
449
450 if (!array || array_len == 0)
451 return SBData();
452
453 size_t data_len = array_len * sizeof(int64_t);
454
455 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
456 lldb::DataExtractorSP data_sp(
457 new DataExtractor(buffer_sp, endian, addr_byte_size));
458
459 SBData ret(data_sp);
460
461 return ret;
462}
463
465 uint32_t addr_byte_size,
466 int32_t *array,
467 size_t array_len) {
468 LLDB_INSTRUMENT_VA(endian, addr_byte_size, array, array_len);
469
470 if (!array || array_len == 0)
471 return SBData();
472
473 size_t data_len = array_len * sizeof(int32_t);
474
475 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
476 lldb::DataExtractorSP data_sp(
477 new DataExtractor(buffer_sp, endian, addr_byte_size));
478
479 SBData ret(data_sp);
480
481 return ret;
482}
483
485 uint32_t addr_byte_size,
486 double *array,
487 size_t array_len) {
488 LLDB_INSTRUMENT_VA(endian, addr_byte_size, array, array_len);
489
490 if (!array || array_len == 0)
491 return SBData();
492
493 size_t data_len = array_len * sizeof(double);
494
495 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
496 lldb::DataExtractorSP data_sp(
497 new DataExtractor(buffer_sp, endian, addr_byte_size));
498
499 SBData ret(data_sp);
500
501 return ret;
502}
503
504bool SBData::SetDataFromCString(const char *data) {
505 LLDB_INSTRUMENT_VA(this, data);
506
507 if (!data) {
508 return false;
509 }
510
511 size_t data_len = strlen(data);
512
513 lldb::DataBufferSP buffer_sp(new DataBufferHeap(data, data_len));
514
515 if (!m_opaque_sp.get())
516 m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),
518 else
519 m_opaque_sp->SetData(buffer_sp);
520
521
522 return true;
523}
524
525bool SBData::SetDataFromUInt64Array(uint64_t *array, size_t array_len) {
526 LLDB_INSTRUMENT_VA(this, array, array_len);
527
528 if (!array || array_len == 0) {
529 return false;
530 }
531
532 size_t data_len = array_len * sizeof(uint64_t);
533
534 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
535
536 if (!m_opaque_sp.get())
537 m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),
539 else
540 m_opaque_sp->SetData(buffer_sp);
541
542
543 return true;
544}
545
546bool SBData::SetDataFromUInt32Array(uint32_t *array, size_t array_len) {
547 LLDB_INSTRUMENT_VA(this, array, array_len);
548
549 if (!array || array_len == 0) {
550 return false;
551 }
552
553 size_t data_len = array_len * sizeof(uint32_t);
554
555 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
556
557 if (!m_opaque_sp.get())
558 m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),
560 else
561 m_opaque_sp->SetData(buffer_sp);
562
563 return true;
564}
565
566bool SBData::SetDataFromSInt64Array(int64_t *array, size_t array_len) {
567 LLDB_INSTRUMENT_VA(this, array, array_len);
568
569 if (!array || array_len == 0) {
570 return false;
571 }
572
573 size_t data_len = array_len * sizeof(int64_t);
574
575 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
576
577 if (!m_opaque_sp.get())
578 m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),
580 else
581 m_opaque_sp->SetData(buffer_sp);
582
583 return true;
584}
585
586bool SBData::SetDataFromSInt32Array(int32_t *array, size_t array_len) {
587 LLDB_INSTRUMENT_VA(this, array, array_len);
588
589 if (!array || array_len == 0) {
590 return false;
591 }
592
593 size_t data_len = array_len * sizeof(int32_t);
594
595 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
596
597 if (!m_opaque_sp.get())
598 m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),
600 else
601 m_opaque_sp->SetData(buffer_sp);
602
603 return true;
604}
605
606bool SBData::SetDataFromDoubleArray(double *array, size_t array_len) {
607 LLDB_INSTRUMENT_VA(this, array, array_len);
608
609 if (!array || array_len == 0) {
610 return false;
611 }
612
613 size_t data_len = array_len * sizeof(double);
614
615 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
616
617 if (!m_opaque_sp.get())
618 m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),
620 else
621 m_opaque_sp->SetData(buffer_sp);
622
623 return true;
624}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_INSTRUMENT_VA(...)
void SetDataWithOwnership(lldb::SBError &error, const void *buf, size_t size, lldb::ByteOrder endian, uint8_t addr_size)
Definition SBData.cpp:360
lldb_private::DataExtractor * get() const
Definition SBData.cpp:50
lldb::ByteOrder GetByteOrder()
Definition SBData.cpp:102
void SetByteOrder(lldb::ByteOrder endian)
Definition SBData.cpp:111
void Clear()
Definition SBData.cpp:86
uint8_t GetUnsignedInt8(lldb::SBError &error, lldb::offset_t offset)
Definition SBData.cpp:178
uint64_t GetUnsignedInt64(lldb::SBError &error, lldb::offset_t offset)
Definition SBData.cpp:223
uint8_t GetAddressByteSize()
Definition SBData.cpp:70
double GetDouble(lldb::SBError &error, lldb::offset_t offset)
Definition SBData.cpp:133
bool IsValid()
Definition SBData.cpp:60
bool SetDataFromSInt64Array(int64_t *array, size_t array_len)
Definition SBData.cpp:566
size_t ReadRawData(lldb::SBError &error, lldb::offset_t offset, void *buf, size_t size)
Definition SBData.cpp:331
static lldb::SBData CreateDataFromSInt64Array(lldb::ByteOrder endian, uint32_t addr_byte_size, int64_t *array, size_t array_len)
Definition SBData.cpp:444
int8_t GetSignedInt8(lldb::SBError &error, lldb::offset_t offset)
Definition SBData.cpp:238
bool Append(const SBData &rhs)
Definition SBData.cpp:376
bool GetDescription(lldb::SBStream &description, lldb::addr_t base_addr=LLDB_INVALID_ADDRESS)
Definition SBData.cpp:316
int16_t GetSignedInt16(lldb::SBError &error, lldb::offset_t offset)
Definition SBData.cpp:253
lldb_private::DataExtractor * operator->() const
Definition SBData.cpp:52
lldb::addr_t GetAddress(lldb::SBError &error, lldb::offset_t offset)
Definition SBData.cpp:163
bool SetDataFromUInt32Array(uint32_t *array, size_t array_len)
Definition SBData.cpp:546
void SetAddressByteSize(uint8_t addr_byte_size)
Definition SBData.cpp:79
const char * GetString(lldb::SBError &error, lldb::offset_t offset)
Definition SBData.cpp:298
size_t GetByteSize()
Definition SBData.cpp:93
uint32_t GetUnsignedInt32(lldb::SBError &error, lldb::offset_t offset)
Definition SBData.cpp:208
static lldb::SBData CreateDataFromCString(lldb::ByteOrder endian, uint32_t addr_byte_size, const char *data)
Definition SBData.cpp:385
static lldb::SBData CreateDataFromUInt32Array(lldb::ByteOrder endian, uint32_t addr_byte_size, uint32_t *array, size_t array_len)
Definition SBData.cpp:424
bool SetDataFromCString(const char *data)
Definition SBData.cpp:504
int32_t GetSignedInt32(lldb::SBError &error, lldb::offset_t offset)
Definition SBData.cpp:268
void SetOpaque(const lldb::DataExtractorSP &data_sp)
Definition SBData.cpp:46
static lldb::SBData CreateDataFromDoubleArray(lldb::ByteOrder endian, uint32_t addr_byte_size, double *array, size_t array_len)
Definition SBData.cpp:484
bool SetDataFromUInt64Array(uint64_t *array, size_t array_len)
Definition SBData.cpp:525
lldb::DataExtractorSP & operator*()
Definition SBData.cpp:56
static lldb::SBData CreateDataFromUInt64Array(lldb::ByteOrder endian, uint32_t addr_byte_size, uint64_t *array, size_t array_len)
Definition SBData.cpp:404
bool SetDataFromDoubleArray(double *array, size_t array_len)
Definition SBData.cpp:606
uint16_t GetUnsignedInt16(lldb::SBError &error, lldb::offset_t offset)
Definition SBData.cpp:193
long double GetLongDouble(lldb::SBError &error, lldb::offset_t offset)
Definition SBData.cpp:148
float GetFloat(lldb::SBError &error, lldb::offset_t offset)
Definition SBData.cpp:118
const SBData & operator=(const SBData &rhs)
Definition SBData.cpp:36
static lldb::SBData CreateDataFromSInt32Array(lldb::ByteOrder endian, uint32_t addr_byte_size, int32_t *array, size_t array_len)
Definition SBData.cpp:464
int64_t GetSignedInt64(lldb::SBError &error, lldb::offset_t offset)
Definition SBData.cpp:283
bool SetDataFromSInt32Array(int32_t *array, size_t array_len)
Definition SBData.cpp:586
void SetData(lldb::SBError &error, const void *buf, size_t size, lldb::ByteOrder endian, uint8_t addr_size)
Definition SBData.cpp:347
lldb::DataExtractorSP m_opaque_sp
Definition SBData.h:159
lldb_private::Stream & ref()
Definition SBStream.cpp:179
A uniqued constant string class.
Definition ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
A subclass of DataBuffer that stores a data buffer on the heap.
An data extractor class.
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
A class that represents a running process on the host machine.
lldb::offset_t DumpDataExtractor(const DataExtractor &DE, Stream *s, lldb::offset_t offset, lldb::Format item_format, size_t item_byte_size, size_t item_count, size_t num_per_line, uint64_t base_addr, uint32_t item_bit_size, uint32_t item_bit_offset, ExecutionContextScope *exe_scope=nullptr, bool show_memory_tags=false)
Dumps item_count objects into the stream s.
@ eFormatBytesWithASCII
uint64_t offset_t
Definition lldb-types.h:86
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP