LLDB mainline
DumpDataExtractor.cpp
Go to the documentation of this file.
1//===-- DumpDataExtractor.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
10
11#include "lldb/lldb-defines.h"
12#include "lldb/lldb-forward.h"
13
14#include "lldb/Core/Address.h"
17#include "lldb/Target/ABI.h"
23#include "lldb/Target/Process.h"
25#include "lldb/Target/Target.h"
27#include "lldb/Utility/Log.h"
28#include "lldb/Utility/Stream.h"
29
30#include "llvm/ADT/APFloat.h"
31#include "llvm/ADT/APInt.h"
32#include "llvm/ADT/ArrayRef.h"
33#include "llvm/ADT/SmallVector.h"
34
35#include <limits>
36#include <memory>
37#include <string>
38
39#include <cassert>
40#include <cctype>
41#include <cinttypes>
42#include <cmath>
43
44#include <bitset>
45#include <optional>
46#include <sstream>
47
48using namespace lldb_private;
49using namespace lldb;
50
51#define NON_PRINTABLE_CHAR '.'
52
53static std::optional<llvm::APInt> GetAPInt(const DataExtractor &data,
54 lldb::offset_t *offset_ptr,
55 lldb::offset_t byte_size) {
56 if (byte_size == 0)
57 return std::nullopt;
58
59 llvm::SmallVector<uint64_t, 2> uint64_array;
60 lldb::offset_t bytes_left = byte_size;
61 uint64_t u64;
62 const lldb::ByteOrder byte_order = data.GetByteOrder();
63 if (byte_order == lldb::eByteOrderLittle) {
64 while (bytes_left > 0) {
65 if (bytes_left >= 8) {
66 u64 = data.GetU64(offset_ptr);
67 bytes_left -= 8;
68 } else {
69 u64 = data.GetMaxU64(offset_ptr, (uint32_t)bytes_left);
70 bytes_left = 0;
71 }
72 uint64_array.push_back(u64);
73 }
74 return llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
75 } else if (byte_order == lldb::eByteOrderBig) {
76 lldb::offset_t be_offset = *offset_ptr + byte_size;
77 lldb::offset_t temp_offset;
78 while (bytes_left > 0) {
79 if (bytes_left >= 8) {
80 be_offset -= 8;
81 temp_offset = be_offset;
82 u64 = data.GetU64(&temp_offset);
83 bytes_left -= 8;
84 } else {
85 be_offset -= bytes_left;
86 temp_offset = be_offset;
87 u64 = data.GetMaxU64(&temp_offset, (uint32_t)bytes_left);
88 bytes_left = 0;
89 }
90 uint64_array.push_back(u64);
91 }
92 *offset_ptr += byte_size;
93 return llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
94 }
95 return std::nullopt;
96}
97
99 lldb::offset_t offset, lldb::offset_t byte_size,
100 bool is_signed, unsigned radix) {
101 std::optional<llvm::APInt> apint = GetAPInt(data, &offset, byte_size);
102 if (apint) {
103 std::string apint_str = toString(*apint, radix, is_signed);
104 switch (radix) {
105 case 2:
106 s->Write("0b", 2);
107 break;
108 case 8:
109 s->Write("0", 1);
110 break;
111 case 10:
112 break;
113 }
114 s->Write(apint_str.c_str(), apint_str.size());
115 }
116 return offset;
117}
118
119/// Dumps decoded instructions to a stream.
121 ExecutionContextScope *exe_scope,
122 offset_t start_offset,
123 uint64_t base_addr,
124 size_t number_of_instructions) {
125 offset_t offset = start_offset;
126
127 TargetSP target_sp;
128 if (exe_scope)
129 target_sp = exe_scope->CalculateTarget();
130 if (target_sp) {
132 target_sp->GetArchitecture(), target_sp->GetDisassemblyFlavor(),
133 target_sp->GetDisassemblyCPU(), target_sp->GetDisassemblyFeatures(),
134 nullptr));
135 if (disassembler_sp) {
136 lldb::addr_t addr = base_addr + start_offset;
137 lldb_private::Address so_addr;
138 bool data_from_file = true;
139 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) {
140 data_from_file = false;
141 } else {
142 if (target_sp->GetSectionLoadList().IsEmpty() ||
143 !target_sp->GetImages().ResolveFileAddress(addr, so_addr))
144 so_addr.SetRawAddress(addr);
145 }
146
147 size_t bytes_consumed = disassembler_sp->DecodeInstructions(
148 so_addr, DE, start_offset, number_of_instructions, false,
149 data_from_file);
150
151 if (bytes_consumed) {
152 offset += bytes_consumed;
153 const bool show_address = base_addr != LLDB_INVALID_ADDRESS;
154 const bool show_bytes = false;
155 const bool show_control_flow_kind = false;
156 ExecutionContext exe_ctx;
157 exe_scope->CalculateExecutionContext(exe_ctx);
158 disassembler_sp->GetInstructionList().Dump(
159 s, show_address, show_bytes, show_control_flow_kind, &exe_ctx);
160 }
161 }
162 } else
163 s->Printf("invalid target");
164
165 return offset;
166}
167
168/// Prints the specific escape sequence of the given character to the stream.
169/// If the character doesn't have a known specific escape sequence (e.g., '\a',
170/// '\n' but not generic escape sequences such as'\x12'), this function will
171/// not modify the stream and return false.
172static bool TryDumpSpecialEscapedChar(Stream &s, const char c) {
173 switch (c) {
174 case '\033':
175 // Common non-standard escape code for 'escape'.
176 s.Printf("\\e");
177 return true;
178 case '\a':
179 s.Printf("\\a");
180 return true;
181 case '\b':
182 s.Printf("\\b");
183 return true;
184 case '\f':
185 s.Printf("\\f");
186 return true;
187 case '\n':
188 s.Printf("\\n");
189 return true;
190 case '\r':
191 s.Printf("\\r");
192 return true;
193 case '\t':
194 s.Printf("\\t");
195 return true;
196 case '\v':
197 s.Printf("\\v");
198 return true;
199 case '\0':
200 s.Printf("\\0");
201 return true;
202 default:
203 return false;
204 }
205}
206
207/// Dump the character to a stream. A character that is not printable will be
208/// represented by its escape sequence.
209static void DumpCharacter(Stream &s, const char c) {
211 return;
212 if (llvm::isPrint(c)) {
213 s.PutChar(c);
214 return;
215 }
216 s.Printf("\\x%2.2hhx", c);
217}
218
219/// Dump a floating point type.
220template <typename FloatT>
221void DumpFloatingPoint(std::ostringstream &ss, FloatT f) {
222 static_assert(std::is_floating_point<FloatT>::value,
223 "Only floating point types can be dumped.");
224 // NaN and Inf are potentially implementation defined and on Darwin it
225 // seems NaNs are printed without their sign. Manually implement dumping them
226 // here to avoid having to deal with platform differences.
227 if (std::isnan(f)) {
228 if (std::signbit(f))
229 ss << '-';
230 ss << "nan";
231 return;
232 }
233 if (std::isinf(f)) {
234 if (std::signbit(f))
235 ss << '-';
236 ss << "inf";
237 return;
238 }
239 ss << f;
240}
241
242static std::optional<MemoryTagMap>
243GetMemoryTags(lldb::addr_t addr, size_t length,
244 ExecutionContextScope *exe_scope) {
245 assert(addr != LLDB_INVALID_ADDRESS);
246
247 if (!exe_scope)
248 return std::nullopt;
249
250 TargetSP target_sp = exe_scope->CalculateTarget();
251 if (!target_sp)
252 return std::nullopt;
253
254 ProcessSP process_sp = target_sp->CalculateProcess();
255 if (!process_sp)
256 return std::nullopt;
257
258 llvm::Expected<const MemoryTagManager *> tag_manager_or_err =
259 process_sp->GetMemoryTagManager();
260 if (!tag_manager_or_err) {
261 llvm::consumeError(tag_manager_or_err.takeError());
262 return std::nullopt;
263 }
264
265 MemoryRegionInfos memory_regions;
266 // Don't check return status, list will be just empty if an error happened.
267 process_sp->GetMemoryRegions(memory_regions);
268
269 llvm::Expected<std::vector<MemoryTagManager::TagRange>> tagged_ranges_or_err =
270 (*tag_manager_or_err)
271 ->MakeTaggedRanges(addr, addr + length, memory_regions);
272 // Here we know that our range will not be inverted but we must still check
273 // for an error.
274 if (!tagged_ranges_or_err) {
275 llvm::consumeError(tagged_ranges_or_err.takeError());
276 return std::nullopt;
277 }
278 if (tagged_ranges_or_err->empty())
279 return std::nullopt;
280
281 MemoryTagMap memory_tag_map(*tag_manager_or_err);
282 for (const MemoryTagManager::TagRange &range : *tagged_ranges_or_err) {
283 llvm::Expected<std::vector<lldb::addr_t>> tags_or_err =
284 process_sp->ReadMemoryTags(range.GetRangeBase(), range.GetByteSize());
285
286 if (tags_or_err)
287 memory_tag_map.InsertTags(range.GetRangeBase(), *tags_or_err);
288 else
289 llvm::consumeError(tags_or_err.takeError());
290 }
291
292 if (memory_tag_map.Empty())
293 return std::nullopt;
294
295 return memory_tag_map;
296}
297
298static void printMemoryTags(const DataExtractor &DE, Stream *s,
299 lldb::addr_t addr, size_t len,
300 const std::optional<MemoryTagMap> &memory_tag_map) {
301 std::vector<std::optional<lldb::addr_t>> tags =
302 memory_tag_map->GetTags(addr, len);
303
304 // Only print if there is at least one tag for this line
305 if (tags.empty())
306 return;
307
308 s->Printf(" (tag%s:", tags.size() > 1 ? "s" : "");
309 // Some granules may not be tagged but print something for them
310 // so that the ordering remains intact.
311 for (auto tag : tags) {
312 if (tag)
313 s->Printf(" 0x%" PRIx64, *tag);
314 else
315 s->PutCString(" <no tag>");
316 }
317 s->PutCString(")");
318}
319
320static const llvm::fltSemantics &GetFloatSemantics(const TargetSP &target_sp,
321 size_t byte_size) {
322 if (target_sp) {
323 auto type_system_or_err =
324 target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeC);
325 if (!type_system_or_err)
326 llvm::consumeError(type_system_or_err.takeError());
327 else if (auto ts = *type_system_or_err)
328 return ts->GetFloatTypeSemantics(byte_size);
329 }
330 // No target, just make a reasonable guess
331 switch(byte_size) {
332 case 2:
333 return llvm::APFloat::IEEEhalf();
334 case 4:
335 return llvm::APFloat::IEEEsingle();
336 case 8:
337 return llvm::APFloat::IEEEdouble();
338 }
339 return llvm::APFloat::Bogus();
340}
341
343 const DataExtractor &DE, Stream *s, offset_t start_offset,
344 lldb::Format item_format, size_t item_byte_size, size_t item_count,
345 size_t num_per_line, uint64_t base_addr,
346 uint32_t item_bit_size, // If zero, this is not a bitfield value, if
347 // non-zero, the value is a bitfield
348 uint32_t item_bit_offset, // If "item_bit_size" is non-zero, this is the
349 // shift amount to apply to a bitfield
350 ExecutionContextScope *exe_scope, bool show_memory_tags) {
351 if (s == nullptr)
352 return start_offset;
353
354 if (item_format == eFormatPointer) {
355 if (item_byte_size != 4 && item_byte_size != 8)
356 item_byte_size = s->GetAddressByteSize();
357 }
358
359 offset_t offset = start_offset;
360
361 std::optional<MemoryTagMap> memory_tag_map;
362 if (show_memory_tags && base_addr != LLDB_INVALID_ADDRESS)
363 memory_tag_map =
364 GetMemoryTags(base_addr, DE.GetByteSize() - offset, exe_scope);
365
366 if (item_format == eFormatInstruction)
367 return DumpInstructions(DE, s, exe_scope, start_offset, base_addr,
368 item_count);
369
370 if ((item_format == eFormatOSType || item_format == eFormatAddressInfo) &&
371 item_byte_size > 8)
372 item_format = eFormatHex;
373
374 lldb::offset_t line_start_offset = start_offset;
375 for (uint32_t count = 0; DE.ValidOffset(offset) && count < item_count;
376 ++count) {
377 // If we are at the beginning or end of a line
378 // Note that the last line is handled outside this for loop.
379 if ((count % num_per_line) == 0) {
380 // If we are at the end of a line
381 if (count > 0) {
382 if (item_format == eFormatBytesWithASCII &&
383 offset > line_start_offset) {
384 s->Printf("%*s",
385 static_cast<int>(
386 (num_per_line - (offset - line_start_offset)) * 3 + 2),
387 "");
388 DumpDataExtractor(DE, s, line_start_offset, eFormatCharPrintable, 1,
389 offset - line_start_offset, SIZE_MAX,
391 }
392
393 if (base_addr != LLDB_INVALID_ADDRESS && memory_tag_map) {
394 size_t line_len = offset - line_start_offset;
395 lldb::addr_t line_base =
396 base_addr +
397 (offset - start_offset - line_len) / DE.getTargetByteSize();
398 printMemoryTags(DE, s, line_base, line_len, memory_tag_map);
399 }
400
401 s->EOL();
402 }
403 if (base_addr != LLDB_INVALID_ADDRESS)
404 s->Printf("0x%8.8" PRIx64 ": ",
405 (uint64_t)(base_addr +
406 (offset - start_offset) / DE.getTargetByteSize()));
407
408 line_start_offset = offset;
409 } else if (item_format != eFormatChar &&
410 item_format != eFormatCharPrintable &&
411 item_format != eFormatCharArray && count > 0) {
412 s->PutChar(' ');
413 }
414
415 switch (item_format) {
416 case eFormatBoolean:
417 if (item_byte_size <= 8)
418 s->Printf("%s", DE.GetMaxU64Bitfield(&offset, item_byte_size,
419 item_bit_size, item_bit_offset)
420 ? "true"
421 : "false");
422 else {
423 s->Printf("error: unsupported byte size (%" PRIu64
424 ") for boolean format",
425 (uint64_t)item_byte_size);
426 return offset;
427 }
428 break;
429
430 case eFormatBinary:
431 if (item_byte_size <= 8) {
432 uint64_t uval64 = DE.GetMaxU64Bitfield(&offset, item_byte_size,
433 item_bit_size, item_bit_offset);
434 // Avoid std::bitset<64>::to_string() since it is missing in earlier
435 // C++ libraries
436 std::string binary_value(64, '0');
437 std::bitset<64> bits(uval64);
438 for (uint32_t i = 0; i < 64; ++i)
439 if (bits[i])
440 binary_value[64 - 1 - i] = '1';
441 if (item_bit_size > 0)
442 s->Printf("0b%s", binary_value.c_str() + 64 - item_bit_size);
443 else if (item_byte_size > 0 && item_byte_size <= 8)
444 s->Printf("0b%s", binary_value.c_str() + 64 - item_byte_size * 8);
445 } else {
446 const bool is_signed = false;
447 const unsigned radix = 2;
448 offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
449 }
450 break;
451
452 case eFormatBytes:
454 for (uint32_t i = 0; i < item_byte_size; ++i) {
455 s->Printf("%2.2x", DE.GetU8(&offset));
456 }
457
458 // Put an extra space between the groups of bytes if more than one is
459 // being dumped in a group (item_byte_size is more than 1).
460 if (item_byte_size > 1)
461 s->PutChar(' ');
462 break;
463
464 case eFormatChar:
466 case eFormatCharArray: {
467 // Reject invalid item_byte_size.
468 if (item_byte_size > 8) {
469 s->Printf("error: unsupported byte size (%" PRIu64 ") for char format",
470 (uint64_t)item_byte_size);
471 return offset;
472 }
473
474 // If we are only printing one character surround it with single quotes
475 if (item_count == 1 && item_format == eFormatChar)
476 s->PutChar('\'');
477
478 const uint64_t ch = DE.GetMaxU64Bitfield(&offset, item_byte_size,
479 item_bit_size, item_bit_offset);
480 if (llvm::isPrint(ch))
481 s->Printf("%c", (char)ch);
482 else if (item_format != eFormatCharPrintable) {
483 if (!TryDumpSpecialEscapedChar(*s, ch)) {
484 if (item_byte_size == 1)
485 s->Printf("\\x%2.2x", (uint8_t)ch);
486 else
487 s->Printf("%" PRIu64, ch);
488 }
489 } else {
491 }
492
493 // If we are only printing one character surround it with single quotes
494 if (item_count == 1 && item_format == eFormatChar)
495 s->PutChar('\'');
496 } break;
497
498 case eFormatEnum: // Print enum value as a signed integer when we don't get
499 // the enum type
500 case eFormatDecimal:
501 if (item_byte_size <= 8)
502 s->Printf("%" PRId64,
503 DE.GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size,
504 item_bit_offset));
505 else {
506 const bool is_signed = true;
507 const unsigned radix = 10;
508 offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
509 }
510 break;
511
512 case eFormatUnsigned:
513 if (item_byte_size <= 8)
514 s->Printf("%" PRIu64,
515 DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
516 item_bit_offset));
517 else {
518 const bool is_signed = false;
519 const unsigned radix = 10;
520 offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
521 }
522 break;
523
524 case eFormatOctal:
525 if (item_byte_size <= 8)
526 s->Printf("0%" PRIo64,
527 DE.GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size,
528 item_bit_offset));
529 else {
530 const bool is_signed = false;
531 const unsigned radix = 8;
532 offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
533 }
534 break;
535
536 case eFormatOSType: {
537 uint64_t uval64 = DE.GetMaxU64Bitfield(&offset, item_byte_size,
538 item_bit_size, item_bit_offset);
539 s->PutChar('\'');
540 for (uint32_t i = 0; i < item_byte_size; ++i) {
541 uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8));
542 DumpCharacter(*s, ch);
543 }
544 s->PutChar('\'');
545 } break;
546
547 case eFormatCString: {
548 const char *cstr = DE.GetCStr(&offset);
549
550 if (!cstr) {
551 s->Printf("NULL");
552 offset = LLDB_INVALID_OFFSET;
553 } else {
554 s->PutChar('\"');
555
556 while (const char c = *cstr) {
557 DumpCharacter(*s, c);
558 ++cstr;
559 }
560
561 s->PutChar('\"');
562 }
563 } break;
564
565 case eFormatPointer:
567 DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
568 item_bit_offset),
569 sizeof(addr_t));
570 break;
571
573 size_t complex_int_byte_size = item_byte_size / 2;
574
575 if (complex_int_byte_size > 0 && complex_int_byte_size <= 8) {
576 s->Printf("%" PRIu64,
577 DE.GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
578 s->Printf(" + %" PRIu64 "i",
579 DE.GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
580 } else {
581 s->Printf("error: unsupported byte size (%" PRIu64
582 ") for complex integer format",
583 (uint64_t)item_byte_size);
584 return offset;
585 }
586 } break;
587
588 case eFormatComplex:
589 if (sizeof(float) * 2 == item_byte_size) {
590 float f32_1 = DE.GetFloat(&offset);
591 float f32_2 = DE.GetFloat(&offset);
592
593 s->Printf("%g + %gi", f32_1, f32_2);
594 break;
595 } else if (sizeof(double) * 2 == item_byte_size) {
596 double d64_1 = DE.GetDouble(&offset);
597 double d64_2 = DE.GetDouble(&offset);
598
599 s->Printf("%lg + %lgi", d64_1, d64_2);
600 break;
601 } else if (sizeof(long double) * 2 == item_byte_size) {
602 long double ld64_1 = DE.GetLongDouble(&offset);
603 long double ld64_2 = DE.GetLongDouble(&offset);
604 s->Printf("%Lg + %Lgi", ld64_1, ld64_2);
605 break;
606 } else {
607 s->Printf("error: unsupported byte size (%" PRIu64
608 ") for complex float format",
609 (uint64_t)item_byte_size);
610 return offset;
611 }
612 break;
613
614 default:
615 case eFormatDefault:
616 case eFormatHex:
617 case eFormatHexUppercase: {
618 bool wantsuppercase = (item_format == eFormatHexUppercase);
619 switch (item_byte_size) {
620 case 1:
621 case 2:
622 case 4:
623 case 8:
626 s->Printf(wantsuppercase ? "0x%*.*" PRIX64 : "0x%*.*" PRIx64,
627 (int)(2 * item_byte_size), (int)(2 * item_byte_size),
628 DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
629 item_bit_offset));
630 } else {
631 s->Printf(wantsuppercase ? "0x%" PRIX64 : "0x%" PRIx64,
632 DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
633 item_bit_offset));
634 }
635 break;
636 default: {
637 assert(item_bit_size == 0 && item_bit_offset == 0);
638 const uint8_t *bytes =
639 (const uint8_t *)DE.GetData(&offset, item_byte_size);
640 if (bytes) {
641 s->PutCString("0x");
642 uint32_t idx;
643 if (DE.GetByteOrder() == eByteOrderBig) {
644 for (idx = 0; idx < item_byte_size; ++idx)
645 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[idx]);
646 } else {
647 for (idx = 0; idx < item_byte_size; ++idx)
648 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x",
649 bytes[item_byte_size - 1 - idx]);
650 }
651 }
652 } break;
653 }
654 } break;
655
656 case eFormatFloat: {
657 TargetSP target_sp;
658 if (exe_scope)
659 target_sp = exe_scope->CalculateTarget();
660
661 std::optional<unsigned> format_max_padding;
662 if (target_sp)
663 format_max_padding = target_sp->GetMaxZeroPaddingInFloatFormat();
664
665 // Show full precision when printing float values
666 const unsigned format_precision = 0;
667
668 const llvm::fltSemantics &semantics =
669 GetFloatSemantics(target_sp, item_byte_size);
670
671 // Recalculate the byte size in case of a difference. This is possible
672 // when item_byte_size is 16 (128-bit), because you could get back the
673 // x87DoubleExtended semantics which has a byte size of 10 (80-bit).
674 const size_t semantics_byte_size =
675 (llvm::APFloat::getSizeInBits(semantics) + 7) / 8;
676 std::optional<llvm::APInt> apint =
677 GetAPInt(DE, &offset, semantics_byte_size);
678 if (apint) {
679 llvm::APFloat apfloat(semantics, *apint);
680 llvm::SmallVector<char, 256> sv;
681 if (format_max_padding)
682 apfloat.toString(sv, format_precision, *format_max_padding);
683 else
684 apfloat.toString(sv, format_precision);
685 s->AsRawOstream() << sv;
686 } else {
687 s->Format("error: unsupported byte size ({0}) for float format",
688 item_byte_size);
689 return offset;
690 }
691 } break;
692
693 case eFormatUnicode16:
694 s->Printf("U+%4.4x", DE.GetU16(&offset));
695 break;
696
697 case eFormatUnicode32:
698 s->Printf("U+0x%8.8x", DE.GetU32(&offset));
699 break;
700
701 case eFormatAddressInfo: {
702 addr_t addr = DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
703 item_bit_offset);
704 s->Printf("0x%*.*" PRIx64, (int)(2 * item_byte_size),
705 (int)(2 * item_byte_size), addr);
706 if (exe_scope) {
707 TargetSP target_sp(exe_scope->CalculateTarget());
708 lldb_private::Address so_addr;
709 if (target_sp) {
710 if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr,
711 so_addr)) {
712 s->PutChar(' ');
713 so_addr.Dump(s, exe_scope, Address::DumpStyleResolvedDescription,
715 } else {
716 so_addr.SetOffset(addr);
717 so_addr.Dump(s, exe_scope,
719 if (ProcessSP process_sp = exe_scope->CalculateProcess()) {
720 if (ABISP abi_sp = process_sp->GetABI()) {
721 addr_t addr_fixed = abi_sp->FixCodeAddress(addr);
722 if (target_sp->GetSectionLoadList().ResolveLoadAddress(
723 addr_fixed, so_addr)) {
724 s->PutChar(' ');
725 s->Printf("(0x%*.*" PRIx64 ")", (int)(2 * item_byte_size),
726 (int)(2 * item_byte_size), addr_fixed);
727 s->PutChar(' ');
728 so_addr.Dump(s, exe_scope,
731 }
732 }
733 }
734 }
735 }
736 }
737 } break;
738
739 case eFormatHexFloat:
740 if (sizeof(float) == item_byte_size) {
741 char float_cstr[256];
742 llvm::APFloat ap_float(DE.GetFloat(&offset));
743 ap_float.convertToHexString(float_cstr, 0, false,
744 llvm::APFloat::rmNearestTiesToEven);
745 s->Printf("%s", float_cstr);
746 break;
747 } else if (sizeof(double) == item_byte_size) {
748 char float_cstr[256];
749 llvm::APFloat ap_float(DE.GetDouble(&offset));
750 ap_float.convertToHexString(float_cstr, 0, false,
751 llvm::APFloat::rmNearestTiesToEven);
752 s->Printf("%s", float_cstr);
753 break;
754 } else {
755 s->Printf("error: unsupported byte size (%" PRIu64
756 ") for hex float format",
757 (uint64_t)item_byte_size);
758 return offset;
759 }
760 break;
761
762 // please keep the single-item formats below in sync with
763 // FormatManager::GetSingleItemFormat if you fail to do so, users will
764 // start getting different outputs depending on internal implementation
765 // details they should not care about ||
766 case eFormatVectorOfChar: // ||
767 s->PutChar('{'); // \/
768 offset =
769 DumpDataExtractor(DE, s, offset, eFormatCharArray, 1, item_byte_size,
770 item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
771 s->PutChar('}');
772 break;
773
775 s->PutChar('{');
776 offset =
777 DumpDataExtractor(DE, s, offset, eFormatDecimal, 1, item_byte_size,
778 item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
779 s->PutChar('}');
780 break;
781
783 s->PutChar('{');
784 offset = DumpDataExtractor(DE, s, offset, eFormatHex, 1, item_byte_size,
785 item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
786 s->PutChar('}');
787 break;
788
790 s->PutChar('{');
791 offset = DumpDataExtractor(
792 DE, s, offset, eFormatDecimal, sizeof(uint16_t),
793 item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t),
795 s->PutChar('}');
796 break;
797
799 s->PutChar('{');
800 offset = DumpDataExtractor(DE, s, offset, eFormatHex, sizeof(uint16_t),
801 item_byte_size / sizeof(uint16_t),
802 item_byte_size / sizeof(uint16_t),
804 s->PutChar('}');
805 break;
806
808 s->PutChar('{');
809 offset = DumpDataExtractor(
810 DE, s, offset, eFormatDecimal, sizeof(uint32_t),
811 item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t),
813 s->PutChar('}');
814 break;
815
817 s->PutChar('{');
818 offset = DumpDataExtractor(DE, s, offset, eFormatHex, sizeof(uint32_t),
819 item_byte_size / sizeof(uint32_t),
820 item_byte_size / sizeof(uint32_t),
822 s->PutChar('}');
823 break;
824
826 s->PutChar('{');
827 offset = DumpDataExtractor(
828 DE, s, offset, eFormatDecimal, sizeof(uint64_t),
829 item_byte_size / sizeof(uint64_t), item_byte_size / sizeof(uint64_t),
831 s->PutChar('}');
832 break;
833
835 s->PutChar('{');
836 offset = DumpDataExtractor(DE, s, offset, eFormatHex, sizeof(uint64_t),
837 item_byte_size / sizeof(uint64_t),
838 item_byte_size / sizeof(uint64_t),
840 s->PutChar('}');
841 break;
842
844 s->PutChar('{');
845 offset =
846 DumpDataExtractor(DE, s, offset, eFormatFloat, 2, item_byte_size / 2,
847 item_byte_size / 2, LLDB_INVALID_ADDRESS, 0, 0);
848 s->PutChar('}');
849 break;
850
852 s->PutChar('{');
853 offset =
854 DumpDataExtractor(DE, s, offset, eFormatFloat, 4, item_byte_size / 4,
855 item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0);
856 s->PutChar('}');
857 break;
858
860 s->PutChar('{');
861 offset =
862 DumpDataExtractor(DE, s, offset, eFormatFloat, 8, item_byte_size / 8,
863 item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0);
864 s->PutChar('}');
865 break;
866
868 s->PutChar('{');
869 offset =
870 DumpDataExtractor(DE, s, offset, eFormatHex, 16, item_byte_size / 16,
871 item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0);
872 s->PutChar('}');
873 break;
874 }
875 }
876
877 // If anything was printed we want to catch the end of the last line.
878 // Since we will exit the for loop above before we get a chance to append to
879 // it normally.
880 if (offset > line_start_offset) {
881 if (item_format == eFormatBytesWithASCII) {
882 s->Printf("%*s",
883 static_cast<int>(
884 (num_per_line - (offset - line_start_offset)) * 3 + 2),
885 "");
886 DumpDataExtractor(DE, s, line_start_offset, eFormatCharPrintable, 1,
887 offset - line_start_offset, SIZE_MAX,
889 }
890
891 if (base_addr != LLDB_INVALID_ADDRESS && memory_tag_map) {
892 size_t line_len = offset - line_start_offset;
893 lldb::addr_t line_base = base_addr + (offset - start_offset - line_len) /
895 printMemoryTags(DE, s, line_base, line_len, memory_tag_map);
896 }
897 }
898
899 return offset; // Return the offset at which we ended up
900}
901
902void lldb_private::DumpHexBytes(Stream *s, const void *src, size_t src_len,
903 uint32_t bytes_per_line,
904 lldb::addr_t base_addr) {
905 DataExtractor data(src, src_len, lldb::eByteOrderLittle, 4);
906 DumpDataExtractor(data, s,
907 0, // Offset into "src"
908 lldb::eFormatBytes, // Dump as hex bytes
909 1, // Size of each item is 1 for single bytes
910 src_len, // Number of bytes
911 bytes_per_line, // Num bytes per line
912 base_addr, // Base address
913 0, 0); // Bitfield info
914}
static lldb::offset_t DumpAPInt(Stream *s, const DataExtractor &data, lldb::offset_t offset, lldb::offset_t byte_size, bool is_signed, unsigned radix)
static void DumpCharacter(Stream &s, const char c)
Dump the character to a stream.
void DumpFloatingPoint(std::ostringstream &ss, FloatT f)
Dump a floating point type.
static std::optional< MemoryTagMap > GetMemoryTags(lldb::addr_t addr, size_t length, ExecutionContextScope *exe_scope)
static std::optional< llvm::APInt > GetAPInt(const DataExtractor &data, lldb::offset_t *offset_ptr, lldb::offset_t byte_size)
static void printMemoryTags(const DataExtractor &DE, Stream *s, lldb::addr_t addr, size_t len, const std::optional< MemoryTagMap > &memory_tag_map)
static lldb::offset_t DumpInstructions(const DataExtractor &DE, Stream *s, ExecutionContextScope *exe_scope, offset_t start_offset, uint64_t base_addr, size_t number_of_instructions)
Dumps decoded instructions to a stream.
static const llvm::fltSemantics & GetFloatSemantics(const TargetSP &target_sp, size_t byte_size)
#define NON_PRINTABLE_CHAR
static bool TryDumpSpecialEscapedChar(Stream &s, const char c)
Prints the specific escape sequence of the given character to the stream.
A section + offset based address class.
Definition: Address.h:62
void SetRawAddress(lldb::addr_t addr)
Definition: Address.h:454
@ DumpStyleModuleWithFileAddress
Display as the file address with the module name prepended (if any).
Definition: Address.h:93
@ DumpStyleResolvedDescription
Display the details about what an address resolves to.
Definition: Address.h:104
@ DumpStyleResolvedPointerDescription
Dereference a pointer at the current address and then lookup the dereferenced address using DumpStyle...
Definition: Address.h:115
bool Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style=DumpStyleInvalid, uint32_t addr_byte_size=UINT32_MAX, bool all_ranges=false, std::optional< Stream::HighlightSettings > settings=std::nullopt) const
Dump a description of this object to a Stream.
Definition: Address.cpp:408
bool SetOffset(lldb::addr_t offset)
Set accessor for the offset.
Definition: Address.h:448
An data extractor class.
Definition: DataExtractor.h:48
float GetFloat(lldb::offset_t *offset_ptr) const
Extract a float from *offset_ptr.
const char * GetCStr(lldb::offset_t *offset_ptr) const
Extract a C string from *offset_ptr.
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
long double GetLongDouble(lldb::offset_t *offset_ptr) const
const void * GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const
Extract length bytes from *offset_ptr.
uint32_t getTargetByteSize() const
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
uint16_t GetU16(lldb::offset_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
uint64_t GetMaxU64Bitfield(lldb::offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
Extract an unsigned integer of size byte_size from *offset_ptr, then extract the bitfield from this v...
bool ValidOffset(lldb::offset_t offset) const
Test the validity of offset.
uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
int64_t GetMaxS64Bitfield(lldb::offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
Extract an signed integer of size size from *offset_ptr, then extract and sign-extend the bitfield fr...
lldb::ByteOrder GetByteOrder() const
Get the current byte order value.
double GetDouble(lldb::offset_t *offset_ptr) const
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
static lldb::DisassemblerSP FindPlugin(const ArchSpec &arch, const char *flavor, const char *cpu, const char *features, const char *plugin_name)
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
virtual void CalculateExecutionContext(ExecutionContext &exe_ctx)=0
Reconstruct the object's execution context into sc.
virtual lldb::ProcessSP CalculateProcess()=0
virtual lldb::TargetSP CalculateTarget()=0
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
MemoryTagMap provides a way to give a sparse read result when reading memory tags for a range.
Definition: MemoryTagMap.h:23
void InsertTags(lldb::addr_t addr, const std::vector< lldb::addr_t > tags)
Insert tags into the map starting from addr.
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
void Format(const char *format, Args &&... args)
Definition: Stream.h:353
uint32_t GetAddressByteSize() const
Get the address size in bytes.
Definition: Stream.cpp:206
size_t Write(const void *src, size_t src_len)
Output character bytes to the stream.
Definition: Stream.h:112
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:401
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
size_t PutChar(char ch)
Definition: Stream.cpp:131
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:155
bool ShowHexVariableValuesWithLeadingZeroes() const
Definition: Target.cpp:4771
static TargetProperties & GetGlobalProperties()
Definition: Target.cpp:3183
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
#define LLDB_INVALID_OFFSET
Definition: lldb-defines.h:93
A class that represents a running process on the host machine.
void DumpHexBytes(Stream *s, const void *src, size_t src_len, uint32_t bytes_per_line, lldb::addr_t base_addr)
void DumpAddress(llvm::raw_ostream &s, uint64_t addr, uint32_t addr_size, const char *prefix=nullptr, const char *suffix=nullptr)
Output an address value to this stream.
Definition: Stream.cpp:108
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.
const char * toString(AppleArm64ExceptionClass EC)
static uint32_t bits(const uint32_t val, const uint32_t msbit, const uint32_t lsbit)
Definition: ARMUtils.h:265
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::ABI > ABISP
Definition: lldb-forward.h:317
Format
Display format definitions.
@ eFormatCString
NULL terminated C strings.
@ eFormatCharArray
Print characters with no single quotes, used for character arrays that can contain non printable char...
@ eFormatInstruction
Disassemble an opcode.
@ eFormatVectorOfChar
@ eFormatVectorOfUInt64
@ eFormatVectorOfFloat16
@ eFormatVectorOfSInt64
@ eFormatComplex
Floating point complex type.
@ eFormatHexFloat
ISO C99 hex float string.
@ eFormatBytesWithASCII
@ eFormatOSType
OS character codes encoded into an integer 'PICT' 'text' etc...
@ eFormatUnicode16
@ eFormatAddressInfo
Describe what an address points to (func + offset.
@ eFormatVectorOfUInt128
@ eFormatVectorOfUInt8
@ eFormatVectorOfFloat32
@ eFormatVectorOfSInt32
@ eFormatUnicode32
@ eFormatVectorOfSInt8
@ eFormatVectorOfUInt16
@ eFormatHexUppercase
@ eFormatVectorOfFloat64
@ eFormatCharPrintable
Only printable characters, '.' if not printable.
@ eFormatComplexInteger
Integer complex type.
@ eFormatVectorOfSInt16
@ eFormatVectorOfUInt32
uint64_t offset_t
Definition: lldb-types.h:85
@ eLanguageTypeC
Non-standardized C, such as K&R.
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:389
std::shared_ptr< lldb_private::Disassembler > DisassemblerSP
Definition: lldb-forward.h:341
ByteOrder
Byte ordering definitions.
@ eByteOrderLittle
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:448