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 } else if (number_of_instructions)
161 s->Printf("failed to decode instructions at 0x%" PRIx64 ".", addr);
162 }
163 } else
164 s->Printf("invalid target");
165
166 return offset;
167}
168
169/// Prints the specific escape sequence of the given character to the stream.
170/// If the character doesn't have a known specific escape sequence (e.g., '\a',
171/// '\n' but not generic escape sequences such as'\x12'), this function will
172/// not modify the stream and return false.
173static bool TryDumpSpecialEscapedChar(Stream &s, const char c) {
174 switch (c) {
175 case '\033':
176 // Common non-standard escape code for 'escape'.
177 s.Printf("\\e");
178 return true;
179 case '\a':
180 s.Printf("\\a");
181 return true;
182 case '\b':
183 s.Printf("\\b");
184 return true;
185 case '\f':
186 s.Printf("\\f");
187 return true;
188 case '\n':
189 s.Printf("\\n");
190 return true;
191 case '\r':
192 s.Printf("\\r");
193 return true;
194 case '\t':
195 s.Printf("\\t");
196 return true;
197 case '\v':
198 s.Printf("\\v");
199 return true;
200 case '\0':
201 s.Printf("\\0");
202 return true;
203 default:
204 return false;
205 }
206}
207
208/// Dump the character to a stream. A character that is not printable will be
209/// represented by its escape sequence.
210static void DumpCharacter(Stream &s, const char c) {
212 return;
213 if (llvm::isPrint(c)) {
214 s.PutChar(c);
215 return;
216 }
217 s.Printf("\\x%2.2hhx", c);
218}
219
220/// Dump a floating point type.
221template <typename FloatT>
222void DumpFloatingPoint(std::ostringstream &ss, FloatT f) {
223 static_assert(std::is_floating_point<FloatT>::value,
224 "Only floating point types can be dumped.");
225 // NaN and Inf are potentially implementation defined and on Darwin it
226 // seems NaNs are printed without their sign. Manually implement dumping them
227 // here to avoid having to deal with platform differences.
228 if (std::isnan(f)) {
229 if (std::signbit(f))
230 ss << '-';
231 ss << "nan";
232 return;
233 }
234 if (std::isinf(f)) {
235 if (std::signbit(f))
236 ss << '-';
237 ss << "inf";
238 return;
239 }
240 ss << f;
241}
242
243static std::optional<MemoryTagMap>
244GetMemoryTags(lldb::addr_t addr, size_t length,
245 ExecutionContextScope *exe_scope) {
246 assert(addr != LLDB_INVALID_ADDRESS);
247
248 if (!exe_scope)
249 return std::nullopt;
250
251 TargetSP target_sp = exe_scope->CalculateTarget();
252 if (!target_sp)
253 return std::nullopt;
254
255 ProcessSP process_sp = target_sp->CalculateProcess();
256 if (!process_sp)
257 return std::nullopt;
258
259 llvm::Expected<const MemoryTagManager *> tag_manager_or_err =
260 process_sp->GetMemoryTagManager();
261 if (!tag_manager_or_err) {
262 llvm::consumeError(tag_manager_or_err.takeError());
263 return std::nullopt;
264 }
265
266 MemoryRegionInfos memory_regions;
267 // Don't check return status, list will be just empty if an error happened.
268 process_sp->GetMemoryRegions(memory_regions);
269
270 llvm::Expected<std::vector<MemoryTagManager::TagRange>> tagged_ranges_or_err =
271 (*tag_manager_or_err)
272 ->MakeTaggedRanges(addr, addr + length, memory_regions);
273 // Here we know that our range will not be inverted but we must still check
274 // for an error.
275 if (!tagged_ranges_or_err) {
276 llvm::consumeError(tagged_ranges_or_err.takeError());
277 return std::nullopt;
278 }
279 if (tagged_ranges_or_err->empty())
280 return std::nullopt;
281
282 MemoryTagMap memory_tag_map(*tag_manager_or_err);
283 for (const MemoryTagManager::TagRange &range : *tagged_ranges_or_err) {
284 llvm::Expected<std::vector<lldb::addr_t>> tags_or_err =
285 process_sp->ReadMemoryTags(range.GetRangeBase(), range.GetByteSize());
286
287 if (tags_or_err)
288 memory_tag_map.InsertTags(range.GetRangeBase(), *tags_or_err);
289 else
290 llvm::consumeError(tags_or_err.takeError());
291 }
292
293 if (memory_tag_map.Empty())
294 return std::nullopt;
295
296 return memory_tag_map;
297}
298
299static void printMemoryTags(const DataExtractor &DE, Stream *s,
300 lldb::addr_t addr, size_t len,
301 const std::optional<MemoryTagMap> &memory_tag_map) {
302 std::vector<std::optional<lldb::addr_t>> tags =
303 memory_tag_map->GetTags(addr, len);
304
305 // Only print if there is at least one tag for this line
306 if (tags.empty())
307 return;
308
309 s->Printf(" (tag%s:", tags.size() > 1 ? "s" : "");
310 // Some granules may not be tagged but print something for them
311 // so that the ordering remains intact.
312 for (auto tag : tags) {
313 if (tag)
314 s->Printf(" 0x%" PRIx64, *tag);
315 else
316 s->PutCString(" <no tag>");
317 }
318 s->PutCString(")");
319}
320
321static const llvm::fltSemantics &GetFloatSemantics(const TargetSP &target_sp,
322 size_t byte_size,
323 lldb::Format format) {
324 if (target_sp) {
325 auto type_system_or_err =
326 target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeC);
327 if (!type_system_or_err)
328 llvm::consumeError(type_system_or_err.takeError());
329 else if (auto ts = *type_system_or_err)
330 return ts->GetFloatTypeSemantics(byte_size, format);
331 }
332 // No target, just make a reasonable guess
333 switch(byte_size) {
334 case 2:
335 return llvm::APFloat::IEEEhalf();
336 case 4:
337 return llvm::APFloat::IEEEsingle();
338 case 8:
339 return llvm::APFloat::IEEEdouble();
340 case 16:
341 if (format == eFormatFloat128) {
342 return llvm::APFloat::IEEEquad();
343 }
344 // Otherwise it's ambigious whether a 16-byte float is a float128 or a
345 // target-specific long double.
346 }
347 return llvm::APFloat::Bogus();
348}
349
351 const DataExtractor &DE, Stream *s, offset_t start_offset,
352 lldb::Format item_format, size_t item_byte_size, size_t item_count,
353 size_t num_per_line, uint64_t base_addr,
354 uint32_t item_bit_size, // If zero, this is not a bitfield value, if
355 // non-zero, the value is a bitfield
356 uint32_t item_bit_offset, // If "item_bit_size" is non-zero, this is the
357 // shift amount to apply to a bitfield
358 ExecutionContextScope *exe_scope, bool show_memory_tags) {
359 if (s == nullptr)
360 return start_offset;
361
362 if (item_format == eFormatPointer) {
363 if (item_byte_size != 4 && item_byte_size != 8)
364 item_byte_size = s->GetAddressByteSize();
365 }
366
367 offset_t offset = start_offset;
368
369 std::optional<MemoryTagMap> memory_tag_map;
370 if (show_memory_tags && base_addr != LLDB_INVALID_ADDRESS)
371 memory_tag_map =
372 GetMemoryTags(base_addr, DE.GetByteSize() - offset, exe_scope);
373
374 if (item_format == eFormatInstruction)
375 return DumpInstructions(DE, s, exe_scope, start_offset, base_addr,
376 item_count);
377
378 if ((item_format == eFormatOSType || item_format == eFormatAddressInfo) &&
379 item_byte_size > 8)
380 item_format = eFormatHex;
381
382 lldb::offset_t line_start_offset = start_offset;
383 for (uint32_t count = 0; DE.ValidOffset(offset) && count < item_count;
384 ++count) {
385 // If we are at the beginning or end of a line
386 // Note that the last line is handled outside this for loop.
387 if ((count % num_per_line) == 0) {
388 // If we are at the end of a line
389 if (count > 0) {
390 if (item_format == eFormatBytesWithASCII &&
391 offset > line_start_offset) {
392 s->Printf("%*s",
393 static_cast<int>(
394 (num_per_line - (offset - line_start_offset)) * 3 + 2),
395 "");
396 DumpDataExtractor(DE, s, line_start_offset, eFormatCharPrintable, 1,
397 offset - line_start_offset, SIZE_MAX,
399 }
400
401 if (base_addr != LLDB_INVALID_ADDRESS && memory_tag_map) {
402 size_t line_len = offset - line_start_offset;
403 lldb::addr_t line_base =
404 base_addr + (offset - start_offset - line_len);
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 + (offset - start_offset)));
413
414 line_start_offset = offset;
415 } else if (item_format != eFormatChar &&
416 item_format != eFormatCharPrintable &&
417 item_format != eFormatCharArray && count > 0) {
418 s->PutChar(' ');
419 }
420
421 switch (item_format) {
422 case eFormatBoolean:
423 if (item_byte_size <= 8)
424 s->Printf("%s", DE.GetMaxU64Bitfield(&offset, item_byte_size,
425 item_bit_size, item_bit_offset)
426 ? "true"
427 : "false");
428 else {
429 s->Printf("error: unsupported byte size (%" PRIu64
430 ") for boolean format",
431 (uint64_t)item_byte_size);
432 return offset;
433 }
434 break;
435
436 case eFormatBinary:
437 if (item_byte_size <= 8) {
438 uint64_t uval64 = DE.GetMaxU64Bitfield(&offset, item_byte_size,
439 item_bit_size, item_bit_offset);
440 // Avoid std::bitset<64>::to_string() since it is missing in earlier
441 // C++ libraries
442 std::string binary_value(64, '0');
443 std::bitset<64> bits(uval64);
444 for (uint32_t i = 0; i < 64; ++i)
445 if (bits[i])
446 binary_value[64 - 1 - i] = '1';
447 if (item_bit_size > 0)
448 s->Printf("0b%s", binary_value.c_str() + 64 - item_bit_size);
449 else if (item_byte_size > 0 && item_byte_size <= 8)
450 s->Printf("0b%s", binary_value.c_str() + 64 - item_byte_size * 8);
451 } else {
452 const bool is_signed = false;
453 const unsigned radix = 2;
454 offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
455 }
456 break;
457
458 case eFormatBytes:
460 for (uint32_t i = 0; i < item_byte_size; ++i) {
461 s->Printf("%2.2x", DE.GetU8(&offset));
462 }
463
464 // Put an extra space between the groups of bytes if more than one is
465 // being dumped in a group (item_byte_size is more than 1).
466 if (item_byte_size > 1)
467 s->PutChar(' ');
468 break;
469
470 case eFormatChar:
472 case eFormatCharArray: {
473 // Reject invalid item_byte_size.
474 if (item_byte_size > 8) {
475 s->Printf("error: unsupported byte size (%" PRIu64 ") for char format",
476 (uint64_t)item_byte_size);
477 return offset;
478 }
479
480 // If we are only printing one character surround it with single quotes
481 if (item_count == 1 && item_format == eFormatChar)
482 s->PutChar('\'');
483
484 const uint64_t ch = DE.GetMaxU64Bitfield(&offset, item_byte_size,
485 item_bit_size, item_bit_offset);
486 if (llvm::isPrint(ch))
487 s->Printf("%c", (char)ch);
488 else if (item_format != eFormatCharPrintable) {
489 if (!TryDumpSpecialEscapedChar(*s, ch)) {
490 if (item_byte_size == 1)
491 s->Printf("\\x%2.2x", (uint8_t)ch);
492 else
493 s->Printf("%" PRIu64, ch);
494 }
495 } else {
497 }
498
499 // If we are only printing one character surround it with single quotes
500 if (item_count == 1 && item_format == eFormatChar)
501 s->PutChar('\'');
502 } break;
503
504 case eFormatEnum: // Print enum value as a signed integer when we don't get
505 // the enum type
506 case eFormatDecimal:
507 if (item_byte_size <= 8)
508 s->Printf("%" PRId64,
509 DE.GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size,
510 item_bit_offset));
511 else {
512 const bool is_signed = true;
513 const unsigned radix = 10;
514 offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
515 }
516 break;
517
518 case eFormatUnsigned:
519 if (item_byte_size <= 8)
520 s->Printf("%" PRIu64,
521 DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
522 item_bit_offset));
523 else {
524 const bool is_signed = false;
525 const unsigned radix = 10;
526 offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
527 }
528 break;
529
530 case eFormatOctal:
531 if (item_byte_size <= 8)
532 s->Printf("0%" PRIo64,
533 DE.GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size,
534 item_bit_offset));
535 else {
536 const bool is_signed = false;
537 const unsigned radix = 8;
538 offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
539 }
540 break;
541
542 case eFormatOSType: {
543 uint64_t uval64 = DE.GetMaxU64Bitfield(&offset, item_byte_size,
544 item_bit_size, item_bit_offset);
545 s->PutChar('\'');
546 for (uint32_t i = 0; i < item_byte_size; ++i) {
547 uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8));
548 DumpCharacter(*s, ch);
549 }
550 s->PutChar('\'');
551 } break;
552
553 case eFormatCString: {
554 const char *cstr = DE.GetCStr(&offset);
555
556 if (!cstr) {
557 s->Printf("NULL");
558 offset = LLDB_INVALID_OFFSET;
559 } else {
560 s->PutChar('\"');
561
562 while (const char c = *cstr) {
563 DumpCharacter(*s, c);
564 ++cstr;
565 }
566
567 s->PutChar('\"');
568 }
569 } break;
570
571 case eFormatPointer:
573 DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
574 item_bit_offset),
575 sizeof(addr_t));
576 break;
577
579 size_t complex_int_byte_size = item_byte_size / 2;
580
581 if (complex_int_byte_size > 0 && complex_int_byte_size <= 8) {
582 s->Printf("%" PRIu64,
583 DE.GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
584 s->Printf(" + %" PRIu64 "i",
585 DE.GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
586 } else {
587 s->Printf("error: unsupported byte size (%" PRIu64
588 ") for complex integer format",
589 (uint64_t)item_byte_size);
590 return offset;
591 }
592 } break;
593
594 case eFormatComplex:
595 if (sizeof(float) * 2 == item_byte_size) {
596 float f32_1 = DE.GetFloat(&offset);
597 float f32_2 = DE.GetFloat(&offset);
598
599 s->Printf("%g + %gi", f32_1, f32_2);
600 break;
601 } else if (sizeof(double) * 2 == item_byte_size) {
602 double d64_1 = DE.GetDouble(&offset);
603 double d64_2 = DE.GetDouble(&offset);
604
605 s->Printf("%lg + %lgi", d64_1, d64_2);
606 break;
607 } else if (sizeof(long double) * 2 == item_byte_size) {
608 long double ld64_1 = DE.GetLongDouble(&offset);
609 long double ld64_2 = DE.GetLongDouble(&offset);
610 s->Printf("%Lg + %Lgi", ld64_1, ld64_2);
611 break;
612 } else {
613 s->Printf("error: unsupported byte size (%" PRIu64
614 ") for complex float format",
615 (uint64_t)item_byte_size);
616 return offset;
617 }
618 break;
619
620 default:
621 case eFormatDefault:
622 case eFormatHex:
623 case eFormatHexUppercase: {
624 bool wantsuppercase = (item_format == eFormatHexUppercase);
625 switch (item_byte_size) {
626 case 1:
627 case 2:
628 case 4:
629 case 8:
631 .ShowHexVariableValuesWithLeadingZeroes()) {
632 s->Printf(wantsuppercase ? "0x%*.*" PRIX64 : "0x%*.*" PRIx64,
633 (int)(2 * item_byte_size), (int)(2 * item_byte_size),
634 DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
635 item_bit_offset));
636 } else {
637 s->Printf(wantsuppercase ? "0x%" PRIX64 : "0x%" PRIx64,
638 DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
639 item_bit_offset));
640 }
641 break;
642 default: {
643 assert(item_bit_size == 0 && item_bit_offset == 0);
644 const uint8_t *bytes =
645 (const uint8_t *)DE.GetData(&offset, item_byte_size);
646 if (bytes) {
647 s->PutCString("0x");
648 uint32_t idx;
649 if (DE.GetByteOrder() == eByteOrderBig) {
650 for (idx = 0; idx < item_byte_size; ++idx)
651 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[idx]);
652 } else {
653 for (idx = 0; idx < item_byte_size; ++idx)
654 s->Printf(wantsuppercase ? "%2.2X" : "%2.2x",
655 bytes[item_byte_size - 1 - idx]);
656 }
657 }
658 } break;
659 }
660 } break;
661
662 case eFormatFloat128:
663 case eFormatFloat: {
664 TargetSP target_sp;
665 if (exe_scope)
666 target_sp = exe_scope->CalculateTarget();
667
668 std::optional<unsigned> format_max_padding;
669 if (target_sp)
670 format_max_padding = target_sp->GetMaxZeroPaddingInFloatFormat();
671
672 // Show full precision when printing float values
673 const unsigned format_precision = 0;
674
675 const llvm::fltSemantics &semantics =
676 GetFloatSemantics(target_sp, item_byte_size, item_format);
677
678 // Recalculate the byte size in case of a difference. This is possible
679 // when item_byte_size is 16 (128-bit), because you could get back the
680 // x87DoubleExtended semantics which has a byte size of 10 (80-bit).
681 const size_t semantics_byte_size =
682 (llvm::APFloat::getSizeInBits(semantics) + 7) / 8;
683 std::optional<llvm::APInt> apint =
684 GetAPInt(DE, &offset, semantics_byte_size);
685 if (apint) {
686 llvm::APFloat apfloat(semantics, *apint);
687 llvm::SmallVector<char, 256> sv;
688 if (format_max_padding)
689 apfloat.toString(sv, format_precision, *format_max_padding);
690 else
691 apfloat.toString(sv, format_precision);
692 s->AsRawOstream() << sv;
693 } else {
694 s->Format("error: unsupported byte size ({0}) for float format",
695 item_byte_size);
696 return offset;
697 }
698 } break;
699
700 case eFormatUnicode16:
701 s->Printf("U+%4.4x", DE.GetU16(&offset));
702 break;
703
704 case eFormatUnicode32:
705 s->Printf("U+0x%8.8x", DE.GetU32(&offset));
706 break;
707
708 case eFormatAddressInfo: {
709 addr_t addr = DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
710 item_bit_offset);
711 s->Printf("0x%*.*" PRIx64, (int)(2 * item_byte_size),
712 (int)(2 * item_byte_size), addr);
713 if (exe_scope) {
714 TargetSP target_sp(exe_scope->CalculateTarget());
715 lldb_private::Address so_addr;
716 if (target_sp) {
717 if (target_sp->ResolveLoadAddress(addr, so_addr)) {
718 s->PutChar(' ');
719 so_addr.Dump(s, exe_scope, Address::DumpStyleResolvedDescription,
721 } else {
722 so_addr.SetOffset(addr);
723 so_addr.Dump(s, exe_scope,
725 if (ProcessSP process_sp = exe_scope->CalculateProcess()) {
726 if (ABISP abi_sp = process_sp->GetABI()) {
727 addr_t addr_fixed = abi_sp->FixCodeAddress(addr);
728 if (target_sp->ResolveLoadAddress(addr_fixed, so_addr)) {
729 s->PutChar(' ');
730 s->Printf("(0x%*.*" PRIx64 ")", (int)(2 * item_byte_size),
731 (int)(2 * item_byte_size), addr_fixed);
732 s->PutChar(' ');
733 so_addr.Dump(s, exe_scope,
736 }
737 }
738 }
739 }
740 }
741 }
742 } break;
743
744 case eFormatHexFloat:
745 if (sizeof(float) == item_byte_size) {
746 char float_cstr[256];
747 llvm::APFloat ap_float(DE.GetFloat(&offset));
748 ap_float.convertToHexString(float_cstr, 0, false,
749 llvm::APFloat::rmNearestTiesToEven);
750 s->Printf("%s", float_cstr);
751 break;
752 } else if (sizeof(double) == item_byte_size) {
753 char float_cstr[256];
754 llvm::APFloat ap_float(DE.GetDouble(&offset));
755 ap_float.convertToHexString(float_cstr, 0, false,
756 llvm::APFloat::rmNearestTiesToEven);
757 s->Printf("%s", float_cstr);
758 break;
759 } else {
760 s->Printf("error: unsupported byte size (%" PRIu64
761 ") for hex float format",
762 (uint64_t)item_byte_size);
763 return offset;
764 }
765 break;
766
767 // please keep the single-item formats below in sync with
768 // FormatManager::GetSingleItemFormat if you fail to do so, users will
769 // start getting different outputs depending on internal implementation
770 // details they should not care about ||
771 case eFormatVectorOfChar: // ||
772 s->PutChar('{'); // \/
773 offset =
774 DumpDataExtractor(DE, s, offset, eFormatCharArray, 1, item_byte_size,
775 item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
776 s->PutChar('}');
777 break;
778
780 s->PutChar('{');
781 offset =
782 DumpDataExtractor(DE, s, offset, eFormatDecimal, 1, item_byte_size,
783 item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
784 s->PutChar('}');
785 break;
786
788 s->PutChar('{');
789 offset = DumpDataExtractor(DE, s, offset, eFormatHex, 1, item_byte_size,
790 item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
791 s->PutChar('}');
792 break;
793
795 s->PutChar('{');
796 offset = DumpDataExtractor(
797 DE, s, offset, eFormatDecimal, sizeof(uint16_t),
798 item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t),
800 s->PutChar('}');
801 break;
802
804 s->PutChar('{');
805 offset = DumpDataExtractor(DE, s, offset, eFormatHex, sizeof(uint16_t),
806 item_byte_size / sizeof(uint16_t),
807 item_byte_size / sizeof(uint16_t),
809 s->PutChar('}');
810 break;
811
813 s->PutChar('{');
814 offset = DumpDataExtractor(
815 DE, s, offset, eFormatDecimal, sizeof(uint32_t),
816 item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t),
818 s->PutChar('}');
819 break;
820
822 s->PutChar('{');
823 offset = DumpDataExtractor(DE, s, offset, eFormatHex, sizeof(uint32_t),
824 item_byte_size / sizeof(uint32_t),
825 item_byte_size / sizeof(uint32_t),
827 s->PutChar('}');
828 break;
829
831 s->PutChar('{');
832 offset = DumpDataExtractor(
833 DE, s, offset, eFormatDecimal, sizeof(uint64_t),
834 item_byte_size / sizeof(uint64_t), item_byte_size / sizeof(uint64_t),
836 s->PutChar('}');
837 break;
838
840 s->PutChar('{');
841 offset = DumpDataExtractor(DE, s, offset, eFormatHex, sizeof(uint64_t),
842 item_byte_size / sizeof(uint64_t),
843 item_byte_size / sizeof(uint64_t),
845 s->PutChar('}');
846 break;
847
849 s->PutChar('{');
850 offset =
851 DumpDataExtractor(DE, s, offset, eFormatFloat, 2, item_byte_size / 2,
852 item_byte_size / 2, LLDB_INVALID_ADDRESS, 0, 0);
853 s->PutChar('}');
854 break;
855
857 s->PutChar('{');
858 offset =
859 DumpDataExtractor(DE, s, offset, eFormatFloat, 4, item_byte_size / 4,
860 item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0);
861 s->PutChar('}');
862 break;
863
865 s->PutChar('{');
866 offset =
867 DumpDataExtractor(DE, s, offset, eFormatFloat, 8, item_byte_size / 8,
868 item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0);
869 s->PutChar('}');
870 break;
871
873 s->PutChar('{');
874 offset =
875 DumpDataExtractor(DE, s, offset, eFormatHex, 16, item_byte_size / 16,
876 item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0);
877 s->PutChar('}');
878 break;
879 }
880 }
881
882 // If anything was printed we want to catch the end of the last line.
883 // Since we will exit the for loop above before we get a chance to append to
884 // it normally.
885 if (offset > line_start_offset) {
886 if (item_format == eFormatBytesWithASCII) {
887 s->Printf("%*s",
888 static_cast<int>(
889 (num_per_line - (offset - line_start_offset)) * 3 + 2),
890 "");
891 DumpDataExtractor(DE, s, line_start_offset, eFormatCharPrintable, 1,
892 offset - line_start_offset, SIZE_MAX,
894 }
895
896 if (base_addr != LLDB_INVALID_ADDRESS && memory_tag_map) {
897 size_t line_len = offset - line_start_offset;
898 lldb::addr_t line_base = base_addr + (offset - start_offset - line_len);
899 printMemoryTags(DE, s, line_base, line_len, memory_tag_map);
900 }
901 }
902
903 return offset; // Return the offset at which we ended up
904}
905
906void lldb_private::DumpHexBytes(Stream *s, const void *src, size_t src_len,
907 uint32_t bytes_per_line,
908 lldb::addr_t base_addr) {
909 DataExtractor data(src, src_len, lldb::eByteOrderLittle, 4);
910 DumpDataExtractor(data, s,
911 0, // Offset into "src"
912 lldb::eFormatBytes, // Dump as hex bytes
913 1, // Size of each item is 1 for single bytes
914 src_len, // Number of bytes
915 bytes_per_line, // Num bytes per line
916 base_addr, // Base address
917 0, 0); // Bitfield info
918}
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.
virtual const void * GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const
Extract length bytes 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
virtual uint64_t GetByteSize() const
Get the number of bytes contained in this object.
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
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:364
uint32_t GetAddressByteSize() const
Get the address size in bytes.
Definition Stream.cpp:214
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:406
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:3286
#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.
static uint32_t bits(const uint32_t val, const uint32_t msbit, const uint32_t lsbit)
Definition ARMUtils.h:265
std::string toString(FormatterBytecode::OpCodes op)
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