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

source code of lldb/source/Core/DumpDataExtractor.cpp