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->ResolveLoadAddress(addr, so_addr)) {
140 data_from_file = false;
141 } else {
142 if (!target_sp->HasLoadedSections() ||
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 lldb::Format format) {
323 if (target_sp) {
324 auto type_system_or_err =
325 target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeC);
326 if (!type_system_or_err)
327 llvm::consumeError(type_system_or_err.takeError());
328 else if (auto ts = *type_system_or_err)
329 return ts->GetFloatTypeSemantics(byte_size, format);
330 }
331 // No target, just make a reasonable guess
332 switch(byte_size) {
333 case 2:
334 return llvm::APFloat::IEEEhalf();
335 case 4:
336 return llvm::APFloat::IEEEsingle();
337 case 8:
338 return llvm::APFloat::IEEEdouble();
339 case 16:
340 if (format == eFormatFloat128) {
341 return llvm::APFloat::IEEEquad();
342 }
343 // Otherwise it's ambigious whether a 16-byte float is a float128 or a
344 // target-specific long double.
345 }
346 return llvm::APFloat::Bogus();
347}
348
350 const DataExtractor &DE, Stream *s, offset_t start_offset,
351 lldb::Format item_format, size_t item_byte_size, size_t item_count,
352 size_t num_per_line, uint64_t base_addr,
353 uint32_t item_bit_size, // If zero, this is not a bitfield value, if
354 // non-zero, the value is a bitfield
355 uint32_t item_bit_offset, // If "item_bit_size" is non-zero, this is the
356 // shift amount to apply to a bitfield
357 ExecutionContextScope *exe_scope, bool show_memory_tags) {
358 if (s == nullptr)
359 return start_offset;
360
361 if (item_format == eFormatPointer) {
362 if (item_byte_size != 4 && item_byte_size != 8)
363 item_byte_size = s->GetAddressByteSize();
364 }
365
366 offset_t offset = start_offset;
367
368 std::optional<MemoryTagMap> memory_tag_map;
369 if (show_memory_tags && base_addr != LLDB_INVALID_ADDRESS)
370 memory_tag_map =
371 GetMemoryTags(base_addr, DE.GetByteSize() - offset, exe_scope);
372
373 if (item_format == eFormatInstruction)
374 return DumpInstructions(DE, s, exe_scope, start_offset, base_addr,
375 item_count);
376
377 if ((item_format == eFormatOSType || item_format == eFormatAddressInfo) &&
378 item_byte_size > 8)
379 item_format = eFormatHex;
380
381 lldb::offset_t line_start_offset = start_offset;
382 for (uint32_t count = 0; DE.ValidOffset(offset) && count < item_count;
383 ++count) {
384 // If we are at the beginning or end of a line
385 // Note that the last line is handled outside this for loop.
386 if ((count % num_per_line) == 0) {
387 // If we are at the end of a line
388 if (count > 0) {
389 if (item_format == eFormatBytesWithASCII &&
390 offset > line_start_offset) {
391 s->Printf("%*s",
392 static_cast<int>(
393 (num_per_line - (offset - line_start_offset)) * 3 + 2),
394 "");
395 DumpDataExtractor(DE, s, line_start_offset, eFormatCharPrintable, 1,
396 offset - line_start_offset, SIZE_MAX,
398 }
399
400 if (base_addr != LLDB_INVALID_ADDRESS && memory_tag_map) {
401 size_t line_len = offset - line_start_offset;
402 lldb::addr_t line_base =
403 base_addr +
404 (offset - start_offset - line_len) / DE.getTargetByteSize();
405 printMemoryTags(DE, s, line_base, line_len, memory_tag_map);
406 }
407
408 s->EOL();
409 }
410 if (base_addr != LLDB_INVALID_ADDRESS)
411 s->Printf("0x%8.8" PRIx64 ": ",
412 (uint64_t)(base_addr +
413 (offset - start_offset) / DE.getTargetByteSize()));
414
415 line_start_offset = offset;
416 } else if (item_format != eFormatChar &&
417 item_format != eFormatCharPrintable &&
418 item_format != eFormatCharArray && count > 0) {
419 s->PutChar(' ');
420 }
421
422 switch (item_format) {
423 case eFormatBoolean:
424 if (item_byte_size <= 8)
425 s->Printf("%s", DE.GetMaxU64Bitfield(&offset, item_byte_size,
426 item_bit_size, item_bit_offset)
427 ? "true"
428 : "false");
429 else {
430 s->Printf("error: unsupported byte size (%" PRIu64
431 ") for boolean format",
432 (uint64_t)item_byte_size);
433 return offset;
434 }
435 break;
436
437 case eFormatBinary:
438 if (item_byte_size <= 8) {
439 uint64_t uval64 = DE.GetMaxU64Bitfield(&offset, item_byte_size,
440 item_bit_size, item_bit_offset);
441 // Avoid std::bitset<64>::to_string() since it is missing in earlier
442 // C++ libraries
443 std::string binary_value(64, '0');
444 std::bitset<64> bits(uval64);
445 for (uint32_t i = 0; i < 64; ++i)
446 if (bits[i])
447 binary_value[64 - 1 - i] = '1';
448 if (item_bit_size > 0)
449 s->Printf("0b%s", binary_value.c_str() + 64 - item_bit_size);
450 else if (item_byte_size > 0 && item_byte_size <= 8)
451 s->Printf("0b%s", binary_value.c_str() + 64 - item_byte_size * 8);
452 } else {
453 const bool is_signed = false;
454 const unsigned radix = 2;
455 offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
456 }
457 break;
458
459 case eFormatBytes:
461 for (uint32_t i = 0; i < item_byte_size; ++i) {
462 s->Printf("%2.2x", DE.GetU8(&offset));
463 }
464
465 // Put an extra space between the groups of bytes if more than one is
466 // being dumped in a group (item_byte_size is more than 1).
467 if (item_byte_size > 1)
468 s->PutChar(' ');
469 break;
470
471 case eFormatChar:
473 case eFormatCharArray: {
474 // Reject invalid item_byte_size.
475 if (item_byte_size > 8) {
476 s->Printf("error: unsupported byte size (%" PRIu64 ") for char format",
477 (uint64_t)item_byte_size);
478 return offset;
479 }
480
481 // If we are only printing one character surround it with single quotes
482 if (item_count == 1 && item_format == eFormatChar)
483 s->PutChar('\'');
484
485 const uint64_t ch = DE.GetMaxU64Bitfield(&offset, item_byte_size,
486 item_bit_size, item_bit_offset);
487 if (llvm::isPrint(ch))
488 s->Printf("%c", (char)ch);
489 else if (item_format != eFormatCharPrintable) {
490 if (!TryDumpSpecialEscapedChar(*s, ch)) {
491 if (item_byte_size == 1)
492 s->Printf("\\x%2.2x", (uint8_t)ch);
493 else
494 s->Printf("%" PRIu64, ch);
495 }
496 } else {
498 }
499
500 // If we are only printing one character surround it with single quotes
501 if (item_count == 1 && item_format == eFormatChar)
502 s->PutChar('\'');
503 } break;
504
505 case eFormatEnum: // Print enum value as a signed integer when we don't get
506 // the enum type
507 case eFormatDecimal:
508 if (item_byte_size <= 8)
509 s->Printf("%" PRId64,
510 DE.GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size,
511 item_bit_offset));
512 else {
513 const bool is_signed = true;
514 const unsigned radix = 10;
515 offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
516 }
517 break;
518
519 case eFormatUnsigned:
520 if (item_byte_size <= 8)
521 s->Printf("%" PRIu64,
522 DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
523 item_bit_offset));
524 else {
525 const bool is_signed = false;
526 const unsigned radix = 10;
527 offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
528 }
529 break;
530
531 case eFormatOctal:
532 if (item_byte_size <= 8)
533 s->Printf("0%" PRIo64,
534 DE.GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size,
535 item_bit_offset));
536 else {
537 const bool is_signed = false;
538 const unsigned radix = 8;
539 offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
540 }
541 break;
542
543 case eFormatOSType: {
544 uint64_t uval64 = DE.GetMaxU64Bitfield(&offset, item_byte_size,
545 item_bit_size, item_bit_offset);
546 s->PutChar('\'');
547 for (uint32_t i = 0; i < item_byte_size; ++i) {
548 uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8));
549 DumpCharacter(*s, ch);
550 }
551 s->PutChar('\'');
552 } break;
553
554 case eFormatCString: {
555 const char *cstr = DE.GetCStr(&offset);
556
557 if (!cstr) {
558 s->Printf("NULL");
559 offset = LLDB_INVALID_OFFSET;
560 } else {
561 s->PutChar('\"');
562
563 while (const char c = *cstr) {
564 DumpCharacter(*s, c);
565 ++cstr;
566 }
567
568 s->PutChar('\"');
569 }
570 } break;
571
572 case eFormatPointer:
574 DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
575 item_bit_offset),
576 sizeof(addr_t));
577 break;
578
580 size_t complex_int_byte_size = item_byte_size / 2;
581
582 if (complex_int_byte_size > 0 && complex_int_byte_size <= 8) {
583 s->Printf("%" PRIu64,
584 DE.GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
585 s->Printf(" + %" PRIu64 "i",
586 DE.GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
587 } else {
588 s->Printf("error: unsupported byte size (%" PRIu64
589 ") for complex integer format",
590 (uint64_t)item_byte_size);
591 return offset;
592 }
593 } break;
594
595 case eFormatComplex:
596 if (sizeof(float) * 2 == item_byte_size) {
597 float f32_1 = DE.GetFloat(&offset);
598 float f32_2 = DE.GetFloat(&offset);
599
600 s->Printf("%g + %gi", f32_1, f32_2);
601 break;
602 } else if (sizeof(double) * 2 == item_byte_size) {
603 double d64_1 = DE.GetDouble(&offset);
604 double d64_2 = DE.GetDouble(&offset);
605
606 s->Printf("%lg + %lgi", d64_1, d64_2);
607 break;
608 } else if (sizeof(long double) * 2 == item_byte_size) {
609 long double ld64_1 = DE.GetLongDouble(&offset);
610 long double ld64_2 = DE.GetLongDouble(&offset);
611 s->Printf("%Lg + %Lgi", ld64_1, ld64_2);
612 break;
613 } else {
614 s->Printf("error: unsupported byte size (%" PRIu64
615 ") for complex float format",
616 (uint64_t)item_byte_size);
617 return offset;
618 }
619 break;
620
621 default:
622 case eFormatDefault:
623 case eFormatHex:
624 case eFormatHexUppercase: {
625 bool wantsuppercase = (item_format == eFormatHexUppercase);
626 switch (item_byte_size) {
627 case 1:
628 case 2:
629 case 4:
630 case 8:
632 .ShowHexVariableValuesWithLeadingZeroes()) {
633 s->Printf(wantsuppercase ? "0x%*.*" PRIX64 : "0x%*.*" PRIx64,
634 (int)(2 * item_byte_size), (int)(2 * item_byte_size),
635 DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
636 item_bit_offset));
637 } else {
638 s->Printf(wantsuppercase ? "0x%" PRIX64 : "0x%" PRIx64,
639 DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
640 item_bit_offset));
641 }
642 break;
643 default: {
644 assert(item_bit_size == 0 && item_bit_offset == 0);
645 const uint8_t *bytes =
646 (const uint8_t *)DE.GetData(&offset, item_byte_size);
647 if (bytes) {
648 s->PutCString("0x");
649 uint32_t idx;
650 if (DE.GetByteOrder() == eByteOrderBig) {
651 for (idx = 0; idx < item_byte_size; ++idx)
652 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[idx]);
653 } else {
654 for (idx = 0; idx < item_byte_size; ++idx)
655 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x",
656 bytes[item_byte_size - 1 - idx]);
657 }
658 }
659 } break;
660 }
661 } break;
662
663 case eFormatFloat128:
664 case eFormatFloat: {
665 TargetSP target_sp;
666 if (exe_scope)
667 target_sp = exe_scope->CalculateTarget();
668
669 std::optional<unsigned> format_max_padding;
670 if (target_sp)
671 format_max_padding = target_sp->GetMaxZeroPaddingInFloatFormat();
672
673 // Show full precision when printing float values
674 const unsigned format_precision = 0;
675
676 const llvm::fltSemantics &semantics =
677 GetFloatSemantics(target_sp, item_byte_size, item_format);
678
679 // Recalculate the byte size in case of a difference. This is possible
680 // when item_byte_size is 16 (128-bit), because you could get back the
681 // x87DoubleExtended semantics which has a byte size of 10 (80-bit).
682 const size_t semantics_byte_size =
683 (llvm::APFloat::getSizeInBits(semantics) + 7) / 8;
684 std::optional<llvm::APInt> apint =
685 GetAPInt(DE, &offset, semantics_byte_size);
686 if (apint) {
687 llvm::APFloat apfloat(semantics, *apint);
688 llvm::SmallVector<char, 256> sv;
689 if (format_max_padding)
690 apfloat.toString(sv, format_precision, *format_max_padding);
691 else
692 apfloat.toString(sv, format_precision);
693 s->AsRawOstream() << sv;
694 } else {
695 s->Format("error: unsupported byte size ({0}) for float format",
696 item_byte_size);
697 return offset;
698 }
699 } break;
700
701 case eFormatUnicode16:
702 s->Printf("U+%4.4x", DE.GetU16(&offset));
703 break;
704
705 case eFormatUnicode32:
706 s->Printf("U+0x%8.8x", DE.GetU32(&offset));
707 break;
708
709 case eFormatAddressInfo: {
710 addr_t addr = DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
711 item_bit_offset);
712 s->Printf("0x%*.*" PRIx64, (int)(2 * item_byte_size),
713 (int)(2 * item_byte_size), addr);
714 if (exe_scope) {
715 TargetSP target_sp(exe_scope->CalculateTarget());
716 lldb_private::Address so_addr;
717 if (target_sp) {
718 if (target_sp->ResolveLoadAddress(addr, so_addr)) {
719 s->PutChar(' ');
720 so_addr.Dump(s, exe_scope, Address::DumpStyleResolvedDescription,
722 } else {
723 so_addr.SetOffset(addr);
724 so_addr.Dump(s, exe_scope,
726 if (ProcessSP process_sp = exe_scope->CalculateProcess()) {
727 if (ABISP abi_sp = process_sp->GetABI()) {
728 addr_t addr_fixed = abi_sp->FixCodeAddress(addr);
729 if (target_sp->ResolveLoadAddress(addr_fixed, so_addr)) {
730 s->PutChar(' ');
731 s->Printf("(0x%*.*" PRIx64 ")", (int)(2 * item_byte_size),
732 (int)(2 * item_byte_size), addr_fixed);
733 s->PutChar(' ');
734 so_addr.Dump(s, exe_scope,
737 }
738 }
739 }
740 }
741 }
742 }
743 } break;
744
745 case eFormatHexFloat:
746 if (sizeof(float) == item_byte_size) {
747 char float_cstr[256];
748 llvm::APFloat ap_float(DE.GetFloat(&offset));
749 ap_float.convertToHexString(float_cstr, 0, false,
750 llvm::APFloat::rmNearestTiesToEven);
751 s->Printf("%s", float_cstr);
752 break;
753 } else if (sizeof(double) == item_byte_size) {
754 char float_cstr[256];
755 llvm::APFloat ap_float(DE.GetDouble(&offset));
756 ap_float.convertToHexString(float_cstr, 0, false,
757 llvm::APFloat::rmNearestTiesToEven);
758 s->Printf("%s", float_cstr);
759 break;
760 } else {
761 s->Printf("error: unsupported byte size (%" PRIu64
762 ") for hex float format",
763 (uint64_t)item_byte_size);
764 return offset;
765 }
766 break;
767
768 // please keep the single-item formats below in sync with
769 // FormatManager::GetSingleItemFormat if you fail to do so, users will
770 // start getting different outputs depending on internal implementation
771 // details they should not care about ||
772 case eFormatVectorOfChar: // ||
773 s->PutChar('{'); // \/
774 offset =
775 DumpDataExtractor(DE, s, offset, eFormatCharArray, 1, item_byte_size,
776 item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
777 s->PutChar('}');
778 break;
779
781 s->PutChar('{');
782 offset =
783 DumpDataExtractor(DE, s, offset, eFormatDecimal, 1, item_byte_size,
784 item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
785 s->PutChar('}');
786 break;
787
789 s->PutChar('{');
790 offset = DumpDataExtractor(DE, s, offset, eFormatHex, 1, item_byte_size,
791 item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
792 s->PutChar('}');
793 break;
794
796 s->PutChar('{');
797 offset = DumpDataExtractor(
798 DE, s, offset, eFormatDecimal, sizeof(uint16_t),
799 item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t),
801 s->PutChar('}');
802 break;
803
805 s->PutChar('{');
806 offset = DumpDataExtractor(DE, s, offset, eFormatHex, sizeof(uint16_t),
807 item_byte_size / sizeof(uint16_t),
808 item_byte_size / sizeof(uint16_t),
810 s->PutChar('}');
811 break;
812
814 s->PutChar('{');
815 offset = DumpDataExtractor(
816 DE, s, offset, eFormatDecimal, sizeof(uint32_t),
817 item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t),
819 s->PutChar('}');
820 break;
821
823 s->PutChar('{');
824 offset = DumpDataExtractor(DE, s, offset, eFormatHex, sizeof(uint32_t),
825 item_byte_size / sizeof(uint32_t),
826 item_byte_size / sizeof(uint32_t),
828 s->PutChar('}');
829 break;
830
832 s->PutChar('{');
833 offset = DumpDataExtractor(
834 DE, s, offset, eFormatDecimal, sizeof(uint64_t),
835 item_byte_size / sizeof(uint64_t), item_byte_size / sizeof(uint64_t),
837 s->PutChar('}');
838 break;
839
841 s->PutChar('{');
842 offset = DumpDataExtractor(DE, s, offset, eFormatHex, sizeof(uint64_t),
843 item_byte_size / sizeof(uint64_t),
844 item_byte_size / sizeof(uint64_t),
846 s->PutChar('}');
847 break;
848
850 s->PutChar('{');
851 offset =
852 DumpDataExtractor(DE, s, offset, eFormatFloat, 2, item_byte_size / 2,
853 item_byte_size / 2, LLDB_INVALID_ADDRESS, 0, 0);
854 s->PutChar('}');
855 break;
856
858 s->PutChar('{');
859 offset =
860 DumpDataExtractor(DE, s, offset, eFormatFloat, 4, item_byte_size / 4,
861 item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0);
862 s->PutChar('}');
863 break;
864
866 s->PutChar('{');
867 offset =
868 DumpDataExtractor(DE, s, offset, eFormatFloat, 8, item_byte_size / 8,
869 item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0);
870 s->PutChar('}');
871 break;
872
874 s->PutChar('{');
875 offset =
876 DumpDataExtractor(DE, s, offset, eFormatHex, 16, item_byte_size / 16,
877 item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0);
878 s->PutChar('}');
879 break;
880 }
881 }
882
883 // If anything was printed we want to catch the end of the last line.
884 // Since we will exit the for loop above before we get a chance to append to
885 // it normally.
886 if (offset > line_start_offset) {
887 if (item_format == eFormatBytesWithASCII) {
888 s->Printf("%*s",
889 static_cast<int>(
890 (num_per_line - (offset - line_start_offset)) * 3 + 2),
891 "");
892 DumpDataExtractor(DE, s, line_start_offset, eFormatCharPrintable, 1,
893 offset - line_start_offset, SIZE_MAX,
895 }
896
897 if (base_addr != LLDB_INVALID_ADDRESS && memory_tag_map) {
898 size_t line_len = offset - line_start_offset;
899 lldb::addr_t line_base = base_addr + (offset - start_offset - line_len) /
901 printMemoryTags(DE, s, line_base, line_len, memory_tag_map);
902 }
903 }
904
905 return offset; // Return the offset at which we ended up
906}
907
908void lldb_private::DumpHexBytes(Stream *s, const void *src, size_t src_len,
909 uint32_t bytes_per_line,
910 lldb::addr_t base_addr) {
911 DataExtractor data(src, src_len, lldb::eByteOrderLittle, 4);
912 DumpDataExtractor(data, s,
913 0, // Offset into "src"
914 lldb::eFormatBytes, // Dump as hex bytes
915 1, // Size of each item is 1 for single bytes
916 src_len, // Number of bytes
917 bytes_per_line, // Num bytes per line
918 base_addr, // Base address
919 0, 0); // Bitfield info
920}
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 const llvm::fltSemantics & GetFloatSemantics(const TargetSP &target_sp, size_t byte_size, lldb::Format format)
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.
#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:447
@ 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:396
bool SetOffset(lldb::addr_t offset)
Set accessor for the offset.
Definition Address.h:441
An data extractor class.
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.
Range< lldb::addr_t, lldb::addr_t > TagRange
MemoryTagMap provides a way to give a sparse read result when reading memory tags for a range.
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:352
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:400
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
static TargetProperties & GetGlobalProperties()
Definition Target.cpp:3249
#define LLDB_INVALID_ADDRESS
#define LLDB_INVALID_OFFSET
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
std::shared_ptr< lldb_private::ABI > ABISP
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...
@ eFormatAddressInfo
Describe what an address points to (func + offset with file/line, symbol + offset,...
@ eFormatVectorOfUInt128
@ eFormatVectorOfUInt8
@ eFormatVectorOfFloat32
@ eFormatVectorOfSInt32
@ eFormatVectorOfSInt8
@ eFormatVectorOfUInt16
@ eFormatHexUppercase
@ eFormatVectorOfFloat64
@ eFormatCharPrintable
Only printable characters, '.' if not printable.
@ eFormatComplexInteger
Integer complex type.
@ eFormatVectorOfSInt16
@ eFormatFloat128
Disambiguate between 128-bit long double (which uses eFormatFloat) and __float128 (which uses eFormat...
@ 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
std::shared_ptr< lldb_private::Disassembler > DisassemblerSP
ByteOrder
Byte ordering definitions.
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP