180814287SRaphael Isemann //===-- DumpDataExtractor.cpp ---------------------------------------------===//
229cb868aSZachary Turner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
629cb868aSZachary Turner //
729cb868aSZachary Turner //===----------------------------------------------------------------------===//
829cb868aSZachary Turner 
929cb868aSZachary Turner #include "lldb/Core/DumpDataExtractor.h"
1029cb868aSZachary Turner 
11672d2c12SJonas Devlieghere #include "lldb/lldb-defines.h"
12672d2c12SJonas Devlieghere #include "lldb/lldb-forward.h"
132f3df613SZachary Turner 
14672d2c12SJonas Devlieghere #include "lldb/Core/Address.h"
1529cb868aSZachary Turner #include "lldb/Core/Disassembler.h"
16672d2c12SJonas Devlieghere #include "lldb/Core/ModuleList.h"
17f7414759SJonas Devlieghere #include "lldb/Target/ABI.h"
1829cb868aSZachary Turner #include "lldb/Target/ExecutionContext.h"
1929cb868aSZachary Turner #include "lldb/Target/ExecutionContextScope.h"
20070090d0SDavid Spickett #include "lldb/Target/MemoryRegionInfo.h"
21070090d0SDavid Spickett #include "lldb/Target/MemoryTagManager.h"
22070090d0SDavid Spickett #include "lldb/Target/MemoryTagMap.h"
23f7414759SJonas Devlieghere #include "lldb/Target/Process.h"
2429cb868aSZachary Turner #include "lldb/Target/SectionLoadList.h"
2529cb868aSZachary Turner #include "lldb/Target/Target.h"
26666cc0b2SZachary Turner #include "lldb/Utility/DataExtractor.h"
275e777e1eSAlex Langford #include "lldb/Utility/Log.h"
2829cb868aSZachary Turner #include "lldb/Utility/Stream.h"
2929cb868aSZachary Turner 
30672d2c12SJonas Devlieghere #include "llvm/ADT/APFloat.h"
31672d2c12SJonas Devlieghere #include "llvm/ADT/APInt.h"
32672d2c12SJonas Devlieghere #include "llvm/ADT/ArrayRef.h"
335e777e1eSAlex Langford #include "llvm/ADT/Optional.h"
34672d2c12SJonas Devlieghere #include "llvm/ADT/SmallVector.h"
352f3df613SZachary Turner 
36672d2c12SJonas Devlieghere #include <limits>
37672d2c12SJonas Devlieghere #include <memory>
38672d2c12SJonas Devlieghere #include <string>
392f3df613SZachary Turner 
4076e47d48SRaphael Isemann #include <cassert>
4176e47d48SRaphael Isemann #include <cctype>
4276e47d48SRaphael Isemann #include <cinttypes>
43ae58cf5fSRaphael Isemann #include <cmath>
442f3df613SZachary Turner 
4529cb868aSZachary Turner #include <bitset>
4629cb868aSZachary Turner #include <sstream>
4729cb868aSZachary Turner 
4829cb868aSZachary Turner using namespace lldb_private;
4929cb868aSZachary Turner using namespace lldb;
5029cb868aSZachary Turner 
5129cb868aSZachary Turner #define NON_PRINTABLE_CHAR '.'
5229cb868aSZachary Turner 
half2float(uint16_t half)5329cb868aSZachary Turner static float half2float(uint16_t half) {
5429cb868aSZachary Turner   union {
5529cb868aSZachary Turner     float f;
5629cb868aSZachary Turner     uint32_t u;
5729cb868aSZachary Turner   } u;
5842a9c0c8SRaphael Isemann   // Sign extend to 4 byte.
5942a9c0c8SRaphael Isemann   int32_t sign_extended = static_cast<int16_t>(half);
6042a9c0c8SRaphael Isemann   uint32_t v = static_cast<uint32_t>(sign_extended);
6129cb868aSZachary Turner 
6229cb868aSZachary Turner   if (0 == (v & 0x7c00)) {
6329cb868aSZachary Turner     u.u = v & 0x80007FFFU;
6429cb868aSZachary Turner     return u.f * ldexpf(1, 125);
6529cb868aSZachary Turner   }
6629cb868aSZachary Turner 
6729cb868aSZachary Turner   v <<= 13;
6829cb868aSZachary Turner   u.u = v | 0x70000000U;
6929cb868aSZachary Turner   return u.f * ldexpf(1, -112);
7029cb868aSZachary Turner }
7129cb868aSZachary Turner 
GetAPInt(const DataExtractor & data,lldb::offset_t * offset_ptr,lldb::offset_t byte_size)725e777e1eSAlex Langford static llvm::Optional<llvm::APInt> GetAPInt(const DataExtractor &data,
735e777e1eSAlex Langford                                             lldb::offset_t *offset_ptr,
745e777e1eSAlex Langford                                             lldb::offset_t byte_size) {
75b482db6dSAlex Langford   if (byte_size == 0)
76b482db6dSAlex Langford     return llvm::None;
77b482db6dSAlex Langford 
7829cb868aSZachary Turner   llvm::SmallVector<uint64_t, 2> uint64_array;
7929cb868aSZachary Turner   lldb::offset_t bytes_left = byte_size;
8029cb868aSZachary Turner   uint64_t u64;
8129cb868aSZachary Turner   const lldb::ByteOrder byte_order = data.GetByteOrder();
8229cb868aSZachary Turner   if (byte_order == lldb::eByteOrderLittle) {
8329cb868aSZachary Turner     while (bytes_left > 0) {
8429cb868aSZachary Turner       if (bytes_left >= 8) {
8529cb868aSZachary Turner         u64 = data.GetU64(offset_ptr);
8629cb868aSZachary Turner         bytes_left -= 8;
8729cb868aSZachary Turner       } else {
8829cb868aSZachary Turner         u64 = data.GetMaxU64(offset_ptr, (uint32_t)bytes_left);
8929cb868aSZachary Turner         bytes_left = 0;
9029cb868aSZachary Turner       }
9129cb868aSZachary Turner       uint64_array.push_back(u64);
9229cb868aSZachary Turner     }
935e777e1eSAlex Langford     return llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
9429cb868aSZachary Turner   } else if (byte_order == lldb::eByteOrderBig) {
9529cb868aSZachary Turner     lldb::offset_t be_offset = *offset_ptr + byte_size;
9629cb868aSZachary Turner     lldb::offset_t temp_offset;
9729cb868aSZachary Turner     while (bytes_left > 0) {
9829cb868aSZachary Turner       if (bytes_left >= 8) {
9929cb868aSZachary Turner         be_offset -= 8;
10029cb868aSZachary Turner         temp_offset = be_offset;
10129cb868aSZachary Turner         u64 = data.GetU64(&temp_offset);
10229cb868aSZachary Turner         bytes_left -= 8;
10329cb868aSZachary Turner       } else {
10429cb868aSZachary Turner         be_offset -= bytes_left;
10529cb868aSZachary Turner         temp_offset = be_offset;
10629cb868aSZachary Turner         u64 = data.GetMaxU64(&temp_offset, (uint32_t)bytes_left);
10729cb868aSZachary Turner         bytes_left = 0;
10829cb868aSZachary Turner       }
10929cb868aSZachary Turner       uint64_array.push_back(u64);
11029cb868aSZachary Turner     }
11129cb868aSZachary Turner     *offset_ptr += byte_size;
1125e777e1eSAlex Langford     return llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
11329cb868aSZachary Turner   }
1145e777e1eSAlex Langford   return llvm::None;
11529cb868aSZachary Turner }
11629cb868aSZachary Turner 
DumpAPInt(Stream * s,const DataExtractor & data,lldb::offset_t offset,lldb::offset_t byte_size,bool is_signed,unsigned radix)11729cb868aSZachary Turner static lldb::offset_t DumpAPInt(Stream *s, const DataExtractor &data,
11829cb868aSZachary Turner                                 lldb::offset_t offset, lldb::offset_t byte_size,
11929cb868aSZachary Turner                                 bool is_signed, unsigned radix) {
1205e777e1eSAlex Langford   llvm::Optional<llvm::APInt> apint = GetAPInt(data, &offset, byte_size);
12196d1b4ddSKazu Hirata   if (apint) {
122*5cff5142SKazu Hirata     std::string apint_str = toString(apint.value(), radix, is_signed);
12329cb868aSZachary Turner     switch (radix) {
12429cb868aSZachary Turner     case 2:
12529cb868aSZachary Turner       s->Write("0b", 2);
12629cb868aSZachary Turner       break;
12729cb868aSZachary Turner     case 8:
12829cb868aSZachary Turner       s->Write("0", 1);
12929cb868aSZachary Turner       break;
13029cb868aSZachary Turner     case 10:
13129cb868aSZachary Turner       break;
13229cb868aSZachary Turner     }
13329cb868aSZachary Turner     s->Write(apint_str.c_str(), apint_str.size());
13429cb868aSZachary Turner   }
13529cb868aSZachary Turner   return offset;
13629cb868aSZachary Turner }
13729cb868aSZachary Turner 
138f3b3689cSRaphael Isemann /// Dumps decoded instructions to a stream.
DumpInstructions(const DataExtractor & DE,Stream * s,ExecutionContextScope * exe_scope,offset_t start_offset,uint64_t base_addr,size_t number_of_instructions)139f3b3689cSRaphael Isemann static lldb::offset_t DumpInstructions(const DataExtractor &DE, Stream *s,
140f3b3689cSRaphael Isemann                                        ExecutionContextScope *exe_scope,
141f3b3689cSRaphael Isemann                                        offset_t start_offset,
142f3b3689cSRaphael Isemann                                        uint64_t base_addr,
143f3b3689cSRaphael Isemann                                        size_t number_of_instructions) {
144f3b3689cSRaphael Isemann   offset_t offset = start_offset;
145f3b3689cSRaphael Isemann 
146f3b3689cSRaphael Isemann   TargetSP target_sp;
147f3b3689cSRaphael Isemann   if (exe_scope)
148f3b3689cSRaphael Isemann     target_sp = exe_scope->CalculateTarget();
149f3b3689cSRaphael Isemann   if (target_sp) {
150f3b3689cSRaphael Isemann     DisassemblerSP disassembler_sp(
151f3b3689cSRaphael Isemann         Disassembler::FindPlugin(target_sp->GetArchitecture(),
152f3b3689cSRaphael Isemann                                  target_sp->GetDisassemblyFlavor(), nullptr));
153f3b3689cSRaphael Isemann     if (disassembler_sp) {
154f3b3689cSRaphael Isemann       lldb::addr_t addr = base_addr + start_offset;
155f3b3689cSRaphael Isemann       lldb_private::Address so_addr;
156f3b3689cSRaphael Isemann       bool data_from_file = true;
157f3b3689cSRaphael Isemann       if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) {
158f3b3689cSRaphael Isemann         data_from_file = false;
159f3b3689cSRaphael Isemann       } else {
160f3b3689cSRaphael Isemann         if (target_sp->GetSectionLoadList().IsEmpty() ||
161f3b3689cSRaphael Isemann             !target_sp->GetImages().ResolveFileAddress(addr, so_addr))
162f3b3689cSRaphael Isemann           so_addr.SetRawAddress(addr);
163f3b3689cSRaphael Isemann       }
164f3b3689cSRaphael Isemann 
165f3b3689cSRaphael Isemann       size_t bytes_consumed = disassembler_sp->DecodeInstructions(
166f3b3689cSRaphael Isemann           so_addr, DE, start_offset, number_of_instructions, false,
167f3b3689cSRaphael Isemann           data_from_file);
168f3b3689cSRaphael Isemann 
169f3b3689cSRaphael Isemann       if (bytes_consumed) {
170f3b3689cSRaphael Isemann         offset += bytes_consumed;
171f3b3689cSRaphael Isemann         const bool show_address = base_addr != LLDB_INVALID_ADDRESS;
172f3b3689cSRaphael Isemann         const bool show_bytes = true;
173ad7bcda9SWalter Erquinigo         const bool show_control_flow_kind = true;
174f3b3689cSRaphael Isemann         ExecutionContext exe_ctx;
175f3b3689cSRaphael Isemann         exe_scope->CalculateExecutionContext(exe_ctx);
176ad7bcda9SWalter Erquinigo         disassembler_sp->GetInstructionList().Dump(
177ad7bcda9SWalter Erquinigo             s, show_address, show_bytes, show_control_flow_kind, &exe_ctx);
178f3b3689cSRaphael Isemann       }
179f3b3689cSRaphael Isemann     }
180f3b3689cSRaphael Isemann   } else
181f3b3689cSRaphael Isemann     s->Printf("invalid target");
182f3b3689cSRaphael Isemann 
183f3b3689cSRaphael Isemann   return offset;
184f3b3689cSRaphael Isemann }
185f3b3689cSRaphael Isemann 
1866f51ceeaSRaphael Isemann /// Prints the specific escape sequence of the given character to the stream.
1876f51ceeaSRaphael Isemann /// If the character doesn't have a known specific escape sequence (e.g., '\a',
1886f51ceeaSRaphael Isemann /// '\n' but not generic escape sequences such as'\x12'), this function will
1896f51ceeaSRaphael Isemann /// not modify the stream and return false.
TryDumpSpecialEscapedChar(Stream & s,const char c)1906f51ceeaSRaphael Isemann static bool TryDumpSpecialEscapedChar(Stream &s, const char c) {
1916f51ceeaSRaphael Isemann   switch (c) {
1926f51ceeaSRaphael Isemann   case '\033':
1936f51ceeaSRaphael Isemann     // Common non-standard escape code for 'escape'.
1946f51ceeaSRaphael Isemann     s.Printf("\\e");
1956f51ceeaSRaphael Isemann     return true;
1966f51ceeaSRaphael Isemann   case '\a':
1976f51ceeaSRaphael Isemann     s.Printf("\\a");
1986f51ceeaSRaphael Isemann     return true;
1996f51ceeaSRaphael Isemann   case '\b':
2006f51ceeaSRaphael Isemann     s.Printf("\\b");
2016f51ceeaSRaphael Isemann     return true;
2026f51ceeaSRaphael Isemann   case '\f':
2036f51ceeaSRaphael Isemann     s.Printf("\\f");
2046f51ceeaSRaphael Isemann     return true;
2056f51ceeaSRaphael Isemann   case '\n':
2066f51ceeaSRaphael Isemann     s.Printf("\\n");
2076f51ceeaSRaphael Isemann     return true;
2086f51ceeaSRaphael Isemann   case '\r':
2096f51ceeaSRaphael Isemann     s.Printf("\\r");
2106f51ceeaSRaphael Isemann     return true;
2116f51ceeaSRaphael Isemann   case '\t':
2126f51ceeaSRaphael Isemann     s.Printf("\\t");
2136f51ceeaSRaphael Isemann     return true;
2146f51ceeaSRaphael Isemann   case '\v':
2156f51ceeaSRaphael Isemann     s.Printf("\\v");
2166f51ceeaSRaphael Isemann     return true;
2176f51ceeaSRaphael Isemann   case '\0':
2186f51ceeaSRaphael Isemann     s.Printf("\\0");
2196f51ceeaSRaphael Isemann     return true;
2206f51ceeaSRaphael Isemann   default:
2216f51ceeaSRaphael Isemann     return false;
2226f51ceeaSRaphael Isemann   }
2236f51ceeaSRaphael Isemann }
2246f51ceeaSRaphael Isemann 
2256f51ceeaSRaphael Isemann /// Dump the character to a stream. A character that is not printable will be
2266f51ceeaSRaphael Isemann /// represented by its escape sequence.
DumpCharacter(Stream & s,const char c)2276f51ceeaSRaphael Isemann static void DumpCharacter(Stream &s, const char c) {
2286f51ceeaSRaphael Isemann   if (TryDumpSpecialEscapedChar(s, c))
2296f51ceeaSRaphael Isemann     return;
2306f51ceeaSRaphael Isemann   if (llvm::isPrint(c)) {
2316f51ceeaSRaphael Isemann     s.PutChar(c);
2326f51ceeaSRaphael Isemann     return;
2336f51ceeaSRaphael Isemann   }
2346f51ceeaSRaphael Isemann   s.Printf("\\x%2.2x", c);
2356f51ceeaSRaphael Isemann }
2366f51ceeaSRaphael Isemann 
237ae58cf5fSRaphael Isemann /// Dump a floating point type.
238ae58cf5fSRaphael Isemann template <typename FloatT>
DumpFloatingPoint(std::ostringstream & ss,FloatT f)239ae58cf5fSRaphael Isemann void DumpFloatingPoint(std::ostringstream &ss, FloatT f) {
240ae58cf5fSRaphael Isemann   static_assert(std::is_floating_point<FloatT>::value,
241ae58cf5fSRaphael Isemann                 "Only floating point types can be dumped.");
242ae58cf5fSRaphael Isemann   // NaN and Inf are potentially implementation defined and on Darwin it
243ae58cf5fSRaphael Isemann   // seems NaNs are printed without their sign. Manually implement dumping them
244ae58cf5fSRaphael Isemann   // here to avoid having to deal with platform differences.
245ae58cf5fSRaphael Isemann   if (std::isnan(f)) {
246ae58cf5fSRaphael Isemann     if (std::signbit(f))
247ae58cf5fSRaphael Isemann       ss << '-';
248ae58cf5fSRaphael Isemann     ss << "nan";
249ae58cf5fSRaphael Isemann     return;
250ae58cf5fSRaphael Isemann   }
251ae58cf5fSRaphael Isemann   if (std::isinf(f)) {
252ae58cf5fSRaphael Isemann     if (std::signbit(f))
253ae58cf5fSRaphael Isemann       ss << '-';
254ae58cf5fSRaphael Isemann     ss << "inf";
255ae58cf5fSRaphael Isemann     return;
256ae58cf5fSRaphael Isemann   }
257ae58cf5fSRaphael Isemann   ss << f;
258ae58cf5fSRaphael Isemann }
259ae58cf5fSRaphael Isemann 
260070090d0SDavid Spickett static llvm::Optional<MemoryTagMap>
GetMemoryTags(lldb::addr_t addr,size_t length,ExecutionContextScope * exe_scope)261070090d0SDavid Spickett GetMemoryTags(lldb::addr_t addr, size_t length,
262070090d0SDavid Spickett               ExecutionContextScope *exe_scope) {
263070090d0SDavid Spickett   assert(addr != LLDB_INVALID_ADDRESS);
264070090d0SDavid Spickett 
265070090d0SDavid Spickett   if (!exe_scope)
266070090d0SDavid Spickett     return llvm::None;
267070090d0SDavid Spickett 
268070090d0SDavid Spickett   TargetSP target_sp = exe_scope->CalculateTarget();
269070090d0SDavid Spickett   if (!target_sp)
270070090d0SDavid Spickett     return llvm::None;
271070090d0SDavid Spickett 
272070090d0SDavid Spickett   ProcessSP process_sp = target_sp->CalculateProcess();
273070090d0SDavid Spickett   if (!process_sp)
274070090d0SDavid Spickett     return llvm::None;
275070090d0SDavid Spickett 
276070090d0SDavid Spickett   llvm::Expected<const MemoryTagManager *> tag_manager_or_err =
277070090d0SDavid Spickett       process_sp->GetMemoryTagManager();
278070090d0SDavid Spickett   if (!tag_manager_or_err) {
279070090d0SDavid Spickett     llvm::consumeError(tag_manager_or_err.takeError());
280070090d0SDavid Spickett     return llvm::None;
281070090d0SDavid Spickett   }
282070090d0SDavid Spickett 
283070090d0SDavid Spickett   MemoryRegionInfos memory_regions;
284070090d0SDavid Spickett   // Don't check return status, list will be just empty if an error happened.
285070090d0SDavid Spickett   process_sp->GetMemoryRegions(memory_regions);
286070090d0SDavid Spickett 
287070090d0SDavid Spickett   llvm::Expected<std::vector<MemoryTagManager::TagRange>> tagged_ranges_or_err =
288070090d0SDavid Spickett       (*tag_manager_or_err)
289070090d0SDavid Spickett           ->MakeTaggedRanges(addr, addr + length, memory_regions);
290070090d0SDavid Spickett   // Here we know that our range will not be inverted but we must still check
291070090d0SDavid Spickett   // for an error.
292070090d0SDavid Spickett   if (!tagged_ranges_or_err) {
293070090d0SDavid Spickett     llvm::consumeError(tagged_ranges_or_err.takeError());
294070090d0SDavid Spickett     return llvm::None;
295070090d0SDavid Spickett   }
296070090d0SDavid Spickett   if (tagged_ranges_or_err->empty())
297070090d0SDavid Spickett     return llvm::None;
298070090d0SDavid Spickett 
299070090d0SDavid Spickett   MemoryTagMap memory_tag_map(*tag_manager_or_err);
300070090d0SDavid Spickett   for (const MemoryTagManager::TagRange &range : *tagged_ranges_or_err) {
301070090d0SDavid Spickett     llvm::Expected<std::vector<lldb::addr_t>> tags_or_err =
302070090d0SDavid Spickett         process_sp->ReadMemoryTags(range.GetRangeBase(), range.GetByteSize());
303070090d0SDavid Spickett 
304070090d0SDavid Spickett     if (tags_or_err)
305070090d0SDavid Spickett       memory_tag_map.InsertTags(range.GetRangeBase(), *tags_or_err);
306070090d0SDavid Spickett     else
307070090d0SDavid Spickett       llvm::consumeError(tags_or_err.takeError());
308070090d0SDavid Spickett   }
309070090d0SDavid Spickett 
310070090d0SDavid Spickett   if (memory_tag_map.Empty())
311070090d0SDavid Spickett     return llvm::None;
312070090d0SDavid Spickett 
313070090d0SDavid Spickett   return memory_tag_map;
314070090d0SDavid Spickett }
315070090d0SDavid Spickett 
316070090d0SDavid Spickett static void
printMemoryTags(const DataExtractor & DE,Stream * s,lldb::addr_t addr,size_t len,const llvm::Optional<MemoryTagMap> & memory_tag_map)317070090d0SDavid Spickett printMemoryTags(const DataExtractor &DE, Stream *s, lldb::addr_t addr,
318070090d0SDavid Spickett                 size_t len,
319070090d0SDavid Spickett                 const llvm::Optional<MemoryTagMap> &memory_tag_map) {
320070090d0SDavid Spickett   std::vector<llvm::Optional<lldb::addr_t>> tags =
321070090d0SDavid Spickett       memory_tag_map->GetTags(addr, len);
322070090d0SDavid Spickett 
323070090d0SDavid Spickett   // Only print if there is at least one tag for this line
324070090d0SDavid Spickett   if (tags.empty())
325070090d0SDavid Spickett     return;
326070090d0SDavid Spickett 
327070090d0SDavid Spickett   s->Printf(" (tag%s:", tags.size() > 1 ? "s" : "");
328070090d0SDavid Spickett   // Some granules may not be tagged but print something for them
329070090d0SDavid Spickett   // so that the ordering remains intact.
330070090d0SDavid Spickett   for (auto tag : tags) {
331070090d0SDavid Spickett     if (tag)
332070090d0SDavid Spickett       s->Printf(" 0x%" PRIx64, *tag);
333070090d0SDavid Spickett     else
334070090d0SDavid Spickett       s->PutCString(" <no tag>");
335070090d0SDavid Spickett   }
336070090d0SDavid Spickett   s->PutCString(")");
337070090d0SDavid Spickett }
338070090d0SDavid Spickett 
DumpDataExtractor(const DataExtractor & DE,Stream * s,offset_t start_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,bool show_memory_tags)33929cb868aSZachary Turner lldb::offset_t lldb_private::DumpDataExtractor(
34029cb868aSZachary Turner     const DataExtractor &DE, Stream *s, offset_t start_offset,
34129cb868aSZachary Turner     lldb::Format item_format, size_t item_byte_size, size_t item_count,
34229cb868aSZachary Turner     size_t num_per_line, uint64_t base_addr,
34329cb868aSZachary Turner     uint32_t item_bit_size,   // If zero, this is not a bitfield value, if
34429cb868aSZachary Turner                               // non-zero, the value is a bitfield
34529cb868aSZachary Turner     uint32_t item_bit_offset, // If "item_bit_size" is non-zero, this is the
34629cb868aSZachary Turner                               // shift amount to apply to a bitfield
347070090d0SDavid Spickett     ExecutionContextScope *exe_scope, bool show_memory_tags) {
34829cb868aSZachary Turner   if (s == nullptr)
34929cb868aSZachary Turner     return start_offset;
35029cb868aSZachary Turner 
35129cb868aSZachary Turner   if (item_format == eFormatPointer) {
35229cb868aSZachary Turner     if (item_byte_size != 4 && item_byte_size != 8)
35329cb868aSZachary Turner       item_byte_size = s->GetAddressByteSize();
35429cb868aSZachary Turner   }
35529cb868aSZachary Turner 
35629cb868aSZachary Turner   offset_t offset = start_offset;
35729cb868aSZachary Turner 
358070090d0SDavid Spickett   llvm::Optional<MemoryTagMap> memory_tag_map = llvm::None;
359070090d0SDavid Spickett   if (show_memory_tags && base_addr != LLDB_INVALID_ADDRESS)
360070090d0SDavid Spickett     memory_tag_map =
361070090d0SDavid Spickett         GetMemoryTags(base_addr, DE.GetByteSize() - offset, exe_scope);
362070090d0SDavid Spickett 
363f3b3689cSRaphael Isemann   if (item_format == eFormatInstruction)
364f3b3689cSRaphael Isemann     return DumpInstructions(DE, s, exe_scope, start_offset, base_addr,
365f3b3689cSRaphael Isemann                             item_count);
36629cb868aSZachary Turner 
36729cb868aSZachary Turner   if ((item_format == eFormatOSType || item_format == eFormatAddressInfo) &&
36829cb868aSZachary Turner       item_byte_size > 8)
36929cb868aSZachary Turner     item_format = eFormatHex;
37029cb868aSZachary Turner 
37129cb868aSZachary Turner   lldb::offset_t line_start_offset = start_offset;
37229cb868aSZachary Turner   for (uint32_t count = 0; DE.ValidOffset(offset) && count < item_count;
37329cb868aSZachary Turner        ++count) {
374070090d0SDavid Spickett     // If we are at the beginning or end of a line
375070090d0SDavid Spickett     // Note that the last line is handled outside this for loop.
37629cb868aSZachary Turner     if ((count % num_per_line) == 0) {
377070090d0SDavid Spickett       // If we are at the end of a line
37829cb868aSZachary Turner       if (count > 0) {
37929cb868aSZachary Turner         if (item_format == eFormatBytesWithASCII &&
38029cb868aSZachary Turner             offset > line_start_offset) {
38129cb868aSZachary Turner           s->Printf("%*s",
38229cb868aSZachary Turner                     static_cast<int>(
38329cb868aSZachary Turner                         (num_per_line - (offset - line_start_offset)) * 3 + 2),
38429cb868aSZachary Turner                     "");
38529cb868aSZachary Turner           DumpDataExtractor(DE, s, line_start_offset, eFormatCharPrintable, 1,
38629cb868aSZachary Turner                             offset - line_start_offset, SIZE_MAX,
38729cb868aSZachary Turner                             LLDB_INVALID_ADDRESS, 0, 0);
38829cb868aSZachary Turner         }
389070090d0SDavid Spickett 
390070090d0SDavid Spickett         if (base_addr != LLDB_INVALID_ADDRESS && memory_tag_map) {
391070090d0SDavid Spickett           size_t line_len = offset - line_start_offset;
392070090d0SDavid Spickett           lldb::addr_t line_base =
393070090d0SDavid Spickett               base_addr +
394070090d0SDavid Spickett               (offset - start_offset - line_len) / DE.getTargetByteSize();
395070090d0SDavid Spickett           printMemoryTags(DE, s, line_base, line_len, memory_tag_map);
396070090d0SDavid Spickett         }
397070090d0SDavid Spickett 
39829cb868aSZachary Turner         s->EOL();
39929cb868aSZachary Turner       }
40029cb868aSZachary Turner       if (base_addr != LLDB_INVALID_ADDRESS)
40129cb868aSZachary Turner         s->Printf("0x%8.8" PRIx64 ": ",
40229cb868aSZachary Turner                   (uint64_t)(base_addr +
40329cb868aSZachary Turner                              (offset - start_offset) / DE.getTargetByteSize()));
40429cb868aSZachary Turner 
40529cb868aSZachary Turner       line_start_offset = offset;
40629cb868aSZachary Turner     } else if (item_format != eFormatChar &&
40729cb868aSZachary Turner                item_format != eFormatCharPrintable &&
40829cb868aSZachary Turner                item_format != eFormatCharArray && count > 0) {
40929cb868aSZachary Turner       s->PutChar(' ');
41029cb868aSZachary Turner     }
41129cb868aSZachary Turner 
41229cb868aSZachary Turner     switch (item_format) {
41329cb868aSZachary Turner     case eFormatBoolean:
41429cb868aSZachary Turner       if (item_byte_size <= 8)
41529cb868aSZachary Turner         s->Printf("%s", DE.GetMaxU64Bitfield(&offset, item_byte_size,
41629cb868aSZachary Turner                                              item_bit_size, item_bit_offset)
41729cb868aSZachary Turner                             ? "true"
41829cb868aSZachary Turner                             : "false");
41929cb868aSZachary Turner       else {
42029cb868aSZachary Turner         s->Printf("error: unsupported byte size (%" PRIu64
42129cb868aSZachary Turner                   ") for boolean format",
42229cb868aSZachary Turner                   (uint64_t)item_byte_size);
42329cb868aSZachary Turner         return offset;
42429cb868aSZachary Turner       }
42529cb868aSZachary Turner       break;
42629cb868aSZachary Turner 
42729cb868aSZachary Turner     case eFormatBinary:
42829cb868aSZachary Turner       if (item_byte_size <= 8) {
42929cb868aSZachary Turner         uint64_t uval64 = DE.GetMaxU64Bitfield(&offset, item_byte_size,
43029cb868aSZachary Turner                                                item_bit_size, item_bit_offset);
43105097246SAdrian Prantl         // Avoid std::bitset<64>::to_string() since it is missing in earlier
43205097246SAdrian Prantl         // C++ libraries
43329cb868aSZachary Turner         std::string binary_value(64, '0');
43429cb868aSZachary Turner         std::bitset<64> bits(uval64);
43529cb868aSZachary Turner         for (uint32_t i = 0; i < 64; ++i)
43629cb868aSZachary Turner           if (bits[i])
43729cb868aSZachary Turner             binary_value[64 - 1 - i] = '1';
43829cb868aSZachary Turner         if (item_bit_size > 0)
43929cb868aSZachary Turner           s->Printf("0b%s", binary_value.c_str() + 64 - item_bit_size);
44029cb868aSZachary Turner         else if (item_byte_size > 0 && item_byte_size <= 8)
44129cb868aSZachary Turner           s->Printf("0b%s", binary_value.c_str() + 64 - item_byte_size * 8);
44229cb868aSZachary Turner       } else {
44329cb868aSZachary Turner         const bool is_signed = false;
44429cb868aSZachary Turner         const unsigned radix = 2;
44529cb868aSZachary Turner         offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
44629cb868aSZachary Turner       }
44729cb868aSZachary Turner       break;
44829cb868aSZachary Turner 
44929cb868aSZachary Turner     case eFormatBytes:
45029cb868aSZachary Turner     case eFormatBytesWithASCII:
45129cb868aSZachary Turner       for (uint32_t i = 0; i < item_byte_size; ++i) {
45229cb868aSZachary Turner         s->Printf("%2.2x", DE.GetU8(&offset));
45329cb868aSZachary Turner       }
45429cb868aSZachary Turner 
45505097246SAdrian Prantl       // Put an extra space between the groups of bytes if more than one is
45605097246SAdrian Prantl       // being dumped in a group (item_byte_size is more than 1).
45729cb868aSZachary Turner       if (item_byte_size > 1)
45829cb868aSZachary Turner         s->PutChar(' ');
45929cb868aSZachary Turner       break;
46029cb868aSZachary Turner 
46129cb868aSZachary Turner     case eFormatChar:
46229cb868aSZachary Turner     case eFormatCharPrintable:
46329cb868aSZachary Turner     case eFormatCharArray: {
464dbd7c338SPetr Pavlu       // Reject invalid item_byte_size.
465dbd7c338SPetr Pavlu       if (item_byte_size > 8) {
466dbd7c338SPetr Pavlu         s->Printf("error: unsupported byte size (%" PRIu64 ") for char format",
467dbd7c338SPetr Pavlu                   (uint64_t)item_byte_size);
468dbd7c338SPetr Pavlu         return offset;
469dbd7c338SPetr Pavlu       }
470dbd7c338SPetr Pavlu 
47105097246SAdrian Prantl       // If we are only printing one character surround it with single quotes
47229cb868aSZachary Turner       if (item_count == 1 && item_format == eFormatChar)
47329cb868aSZachary Turner         s->PutChar('\'');
47429cb868aSZachary Turner 
47529cb868aSZachary Turner       const uint64_t ch = DE.GetMaxU64Bitfield(&offset, item_byte_size,
47629cb868aSZachary Turner                                                item_bit_size, item_bit_offset);
477f5eaa2afSRaphael Isemann       if (llvm::isPrint(ch))
47829cb868aSZachary Turner         s->Printf("%c", (char)ch);
47929cb868aSZachary Turner       else if (item_format != eFormatCharPrintable) {
4806f51ceeaSRaphael Isemann         if (!TryDumpSpecialEscapedChar(*s, ch)) {
48129cb868aSZachary Turner           if (item_byte_size == 1)
48229cb868aSZachary Turner             s->Printf("\\x%2.2x", (uint8_t)ch);
48329cb868aSZachary Turner           else
48429cb868aSZachary Turner             s->Printf("%" PRIu64, ch);
48529cb868aSZachary Turner         }
48629cb868aSZachary Turner       } else {
48729cb868aSZachary Turner         s->PutChar(NON_PRINTABLE_CHAR);
48829cb868aSZachary Turner       }
48929cb868aSZachary Turner 
49029cb868aSZachary Turner       // If we are only printing one character surround it with single quotes
49129cb868aSZachary Turner       if (item_count == 1 && item_format == eFormatChar)
49229cb868aSZachary Turner         s->PutChar('\'');
49329cb868aSZachary Turner     } break;
49429cb868aSZachary Turner 
49529cb868aSZachary Turner     case eFormatEnum: // Print enum value as a signed integer when we don't get
49629cb868aSZachary Turner                       // the enum type
49729cb868aSZachary Turner     case eFormatDecimal:
49829cb868aSZachary Turner       if (item_byte_size <= 8)
49929cb868aSZachary Turner         s->Printf("%" PRId64,
50029cb868aSZachary Turner                   DE.GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size,
50129cb868aSZachary Turner                                        item_bit_offset));
50229cb868aSZachary Turner       else {
50329cb868aSZachary Turner         const bool is_signed = true;
50429cb868aSZachary Turner         const unsigned radix = 10;
50529cb868aSZachary Turner         offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
50629cb868aSZachary Turner       }
50729cb868aSZachary Turner       break;
50829cb868aSZachary Turner 
50929cb868aSZachary Turner     case eFormatUnsigned:
51029cb868aSZachary Turner       if (item_byte_size <= 8)
51129cb868aSZachary Turner         s->Printf("%" PRIu64,
51229cb868aSZachary Turner                   DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
51329cb868aSZachary Turner                                        item_bit_offset));
51429cb868aSZachary Turner       else {
51529cb868aSZachary Turner         const bool is_signed = false;
51629cb868aSZachary Turner         const unsigned radix = 10;
51729cb868aSZachary Turner         offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
51829cb868aSZachary Turner       }
51929cb868aSZachary Turner       break;
52029cb868aSZachary Turner 
52129cb868aSZachary Turner     case eFormatOctal:
52229cb868aSZachary Turner       if (item_byte_size <= 8)
52329cb868aSZachary Turner         s->Printf("0%" PRIo64,
52429cb868aSZachary Turner                   DE.GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size,
52529cb868aSZachary Turner                                        item_bit_offset));
52629cb868aSZachary Turner       else {
52729cb868aSZachary Turner         const bool is_signed = false;
52829cb868aSZachary Turner         const unsigned radix = 8;
52929cb868aSZachary Turner         offset = DumpAPInt(s, DE, offset, item_byte_size, is_signed, radix);
53029cb868aSZachary Turner       }
53129cb868aSZachary Turner       break;
53229cb868aSZachary Turner 
53329cb868aSZachary Turner     case eFormatOSType: {
53429cb868aSZachary Turner       uint64_t uval64 = DE.GetMaxU64Bitfield(&offset, item_byte_size,
53529cb868aSZachary Turner                                              item_bit_size, item_bit_offset);
53629cb868aSZachary Turner       s->PutChar('\'');
53729cb868aSZachary Turner       for (uint32_t i = 0; i < item_byte_size; ++i) {
53829cb868aSZachary Turner         uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8));
5396f51ceeaSRaphael Isemann         DumpCharacter(*s, ch);
54029cb868aSZachary Turner       }
54129cb868aSZachary Turner       s->PutChar('\'');
54229cb868aSZachary Turner     } break;
54329cb868aSZachary Turner 
54429cb868aSZachary Turner     case eFormatCString: {
54529cb868aSZachary Turner       const char *cstr = DE.GetCStr(&offset);
54629cb868aSZachary Turner 
54729cb868aSZachary Turner       if (!cstr) {
54829cb868aSZachary Turner         s->Printf("NULL");
54929cb868aSZachary Turner         offset = LLDB_INVALID_OFFSET;
55029cb868aSZachary Turner       } else {
55129cb868aSZachary Turner         s->PutChar('\"');
55229cb868aSZachary Turner 
55329cb868aSZachary Turner         while (const char c = *cstr) {
5546f51ceeaSRaphael Isemann           DumpCharacter(*s, c);
55529cb868aSZachary Turner           ++cstr;
55629cb868aSZachary Turner         }
55729cb868aSZachary Turner 
55829cb868aSZachary Turner         s->PutChar('\"');
55929cb868aSZachary Turner       }
56029cb868aSZachary Turner     } break;
56129cb868aSZachary Turner 
56229cb868aSZachary Turner     case eFormatPointer:
5631462f5a4SRaphael Isemann       DumpAddress(s->AsRawOstream(),
5641462f5a4SRaphael Isemann                   DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
56529cb868aSZachary Turner                                        item_bit_offset),
56629cb868aSZachary Turner                   sizeof(addr_t));
56729cb868aSZachary Turner       break;
56829cb868aSZachary Turner 
56929cb868aSZachary Turner     case eFormatComplexInteger: {
57029cb868aSZachary Turner       size_t complex_int_byte_size = item_byte_size / 2;
57129cb868aSZachary Turner 
57229cb868aSZachary Turner       if (complex_int_byte_size > 0 && complex_int_byte_size <= 8) {
57329cb868aSZachary Turner         s->Printf("%" PRIu64,
57429cb868aSZachary Turner                   DE.GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
57529cb868aSZachary Turner         s->Printf(" + %" PRIu64 "i",
57629cb868aSZachary Turner                   DE.GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
57729cb868aSZachary Turner       } else {
57829cb868aSZachary Turner         s->Printf("error: unsupported byte size (%" PRIu64
57929cb868aSZachary Turner                   ") for complex integer format",
58029cb868aSZachary Turner                   (uint64_t)item_byte_size);
58129cb868aSZachary Turner         return offset;
58229cb868aSZachary Turner       }
58329cb868aSZachary Turner     } break;
58429cb868aSZachary Turner 
58529cb868aSZachary Turner     case eFormatComplex:
58629cb868aSZachary Turner       if (sizeof(float) * 2 == item_byte_size) {
58729cb868aSZachary Turner         float f32_1 = DE.GetFloat(&offset);
58829cb868aSZachary Turner         float f32_2 = DE.GetFloat(&offset);
58929cb868aSZachary Turner 
59029cb868aSZachary Turner         s->Printf("%g + %gi", f32_1, f32_2);
59129cb868aSZachary Turner         break;
59229cb868aSZachary Turner       } else if (sizeof(double) * 2 == item_byte_size) {
59329cb868aSZachary Turner         double d64_1 = DE.GetDouble(&offset);
59429cb868aSZachary Turner         double d64_2 = DE.GetDouble(&offset);
59529cb868aSZachary Turner 
59629cb868aSZachary Turner         s->Printf("%lg + %lgi", d64_1, d64_2);
59729cb868aSZachary Turner         break;
59829cb868aSZachary Turner       } else if (sizeof(long double) * 2 == item_byte_size) {
59929cb868aSZachary Turner         long double ld64_1 = DE.GetLongDouble(&offset);
60029cb868aSZachary Turner         long double ld64_2 = DE.GetLongDouble(&offset);
60129cb868aSZachary Turner         s->Printf("%Lg + %Lgi", ld64_1, ld64_2);
60229cb868aSZachary Turner         break;
60329cb868aSZachary Turner       } else {
60429cb868aSZachary Turner         s->Printf("error: unsupported byte size (%" PRIu64
60529cb868aSZachary Turner                   ") for complex float format",
60629cb868aSZachary Turner                   (uint64_t)item_byte_size);
60729cb868aSZachary Turner         return offset;
60829cb868aSZachary Turner       }
60929cb868aSZachary Turner       break;
61029cb868aSZachary Turner 
61129cb868aSZachary Turner     default:
61229cb868aSZachary Turner     case eFormatDefault:
61329cb868aSZachary Turner     case eFormatHex:
61429cb868aSZachary Turner     case eFormatHexUppercase: {
61529cb868aSZachary Turner       bool wantsuppercase = (item_format == eFormatHexUppercase);
61629cb868aSZachary Turner       switch (item_byte_size) {
61729cb868aSZachary Turner       case 1:
61829cb868aSZachary Turner       case 2:
61929cb868aSZachary Turner       case 4:
62029cb868aSZachary Turner       case 8:
62129cb868aSZachary Turner         s->Printf(wantsuppercase ? "0x%*.*" PRIX64 : "0x%*.*" PRIx64,
62229cb868aSZachary Turner                   (int)(2 * item_byte_size), (int)(2 * item_byte_size),
62329cb868aSZachary Turner                   DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
62429cb868aSZachary Turner                                        item_bit_offset));
62529cb868aSZachary Turner         break;
62629cb868aSZachary Turner       default: {
62729cb868aSZachary Turner         assert(item_bit_size == 0 && item_bit_offset == 0);
62829cb868aSZachary Turner         const uint8_t *bytes =
62929cb868aSZachary Turner             (const uint8_t *)DE.GetData(&offset, item_byte_size);
63029cb868aSZachary Turner         if (bytes) {
63129cb868aSZachary Turner           s->PutCString("0x");
63229cb868aSZachary Turner           uint32_t idx;
63329cb868aSZachary Turner           if (DE.GetByteOrder() == eByteOrderBig) {
63429cb868aSZachary Turner             for (idx = 0; idx < item_byte_size; ++idx)
63529cb868aSZachary Turner               s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[idx]);
63629cb868aSZachary Turner           } else {
63729cb868aSZachary Turner             for (idx = 0; idx < item_byte_size; ++idx)
63829cb868aSZachary Turner               s->Printf(wantsuppercase ? "%2.2X" : "%2.2x",
63929cb868aSZachary Turner                         bytes[item_byte_size - 1 - idx]);
64029cb868aSZachary Turner           }
64129cb868aSZachary Turner         }
64229cb868aSZachary Turner       } break;
64329cb868aSZachary Turner       }
64429cb868aSZachary Turner     } break;
64529cb868aSZachary Turner 
64629cb868aSZachary Turner     case eFormatFloat: {
64729cb868aSZachary Turner       TargetSP target_sp;
648d5b44036SJonas Devlieghere       bool used_upfloat = false;
64929cb868aSZachary Turner       if (exe_scope)
65029cb868aSZachary Turner         target_sp = exe_scope->CalculateTarget();
65129cb868aSZachary Turner       if (target_sp) {
652b482db6dSAlex Langford         auto type_system_or_err =
653b482db6dSAlex Langford             target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeC);
654b482db6dSAlex Langford         if (!type_system_or_err) {
655b482db6dSAlex Langford           llvm::consumeError(type_system_or_err.takeError());
656b482db6dSAlex Langford         } else {
657b482db6dSAlex Langford           auto &type_system = *type_system_or_err;
65829cb868aSZachary Turner           llvm::SmallVector<char, 256> sv;
65929cb868aSZachary Turner           // Show full precision when printing float values
66029cb868aSZachary Turner           const unsigned format_precision = 0;
661e5814d78SRaphael Isemann           const unsigned format_max_padding =
662e5814d78SRaphael Isemann               target_sp->GetMaxZeroPaddingInFloatFormat();
66329cb868aSZachary Turner 
66429cb868aSZachary Turner           const auto &semantics =
665b482db6dSAlex Langford               type_system.GetFloatTypeSemantics(item_byte_size);
666555130c3SPavel Labath 
667b482db6dSAlex Langford           // Recalculate the byte size in case of a difference. This is possible
668b482db6dSAlex Langford           // when item_byte_size is 16 (128-bit), because you could get back the
669b482db6dSAlex Langford           // x87DoubleExtended semantics which has a byte size of 10 (80-bit).
670b482db6dSAlex Langford           const size_t semantics_byte_size =
671b482db6dSAlex Langford               (llvm::APFloat::getSizeInBits(semantics) + 7) / 8;
6725e777e1eSAlex Langford           llvm::Optional<llvm::APInt> apint =
673b482db6dSAlex Langford               GetAPInt(DE, &offset, semantics_byte_size);
67496d1b4ddSKazu Hirata           if (apint) {
675*5cff5142SKazu Hirata             llvm::APFloat apfloat(semantics, apint.value());
67629cb868aSZachary Turner             apfloat.toString(sv, format_precision, format_max_padding);
67729cb868aSZachary Turner             if (!sv.empty()) {
67829cb868aSZachary Turner               s->Printf("%*.*s", (int)sv.size(), (int)sv.size(), sv.data());
679d5b44036SJonas Devlieghere               used_upfloat = true;
68029cb868aSZachary Turner             }
68129cb868aSZachary Turner           }
68229cb868aSZachary Turner         }
68329cb868aSZachary Turner       }
68429cb868aSZachary Turner 
685d5b44036SJonas Devlieghere       if (!used_upfloat) {
68629cb868aSZachary Turner         std::ostringstream ss;
68729cb868aSZachary Turner         if (item_byte_size == sizeof(float) || item_byte_size == 2) {
68829cb868aSZachary Turner           float f;
68929cb868aSZachary Turner           if (item_byte_size == 2) {
69029cb868aSZachary Turner             uint16_t half = DE.GetU16(&offset);
69129cb868aSZachary Turner             f = half2float(half);
69229cb868aSZachary Turner           } else {
69329cb868aSZachary Turner             f = DE.GetFloat(&offset);
69429cb868aSZachary Turner           }
69529cb868aSZachary Turner           ss.precision(std::numeric_limits<float>::digits10);
696ae58cf5fSRaphael Isemann           DumpFloatingPoint(ss, f);
69729cb868aSZachary Turner         } else if (item_byte_size == sizeof(double)) {
69829cb868aSZachary Turner           ss.precision(std::numeric_limits<double>::digits10);
699ae58cf5fSRaphael Isemann           DumpFloatingPoint(ss, DE.GetDouble(&offset));
70029cb868aSZachary Turner         } else if (item_byte_size == sizeof(long double) ||
70129cb868aSZachary Turner                    item_byte_size == 10) {
70229cb868aSZachary Turner           ss.precision(std::numeric_limits<long double>::digits10);
703ae58cf5fSRaphael Isemann           DumpFloatingPoint(ss, DE.GetLongDouble(&offset));
70429cb868aSZachary Turner         } else {
70529cb868aSZachary Turner           s->Printf("error: unsupported byte size (%" PRIu64
70629cb868aSZachary Turner                     ") for float format",
70729cb868aSZachary Turner                     (uint64_t)item_byte_size);
70829cb868aSZachary Turner           return offset;
70929cb868aSZachary Turner         }
71029cb868aSZachary Turner         ss.flush();
71129cb868aSZachary Turner         s->Printf("%s", ss.str().c_str());
71229cb868aSZachary Turner       }
71329cb868aSZachary Turner     } break;
71429cb868aSZachary Turner 
71529cb868aSZachary Turner     case eFormatUnicode16:
71629cb868aSZachary Turner       s->Printf("U+%4.4x", DE.GetU16(&offset));
71729cb868aSZachary Turner       break;
71829cb868aSZachary Turner 
71929cb868aSZachary Turner     case eFormatUnicode32:
72029cb868aSZachary Turner       s->Printf("U+0x%8.8x", DE.GetU32(&offset));
72129cb868aSZachary Turner       break;
72229cb868aSZachary Turner 
72329cb868aSZachary Turner     case eFormatAddressInfo: {
72429cb868aSZachary Turner       addr_t addr = DE.GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size,
72529cb868aSZachary Turner                                          item_bit_offset);
72629cb868aSZachary Turner       s->Printf("0x%*.*" PRIx64, (int)(2 * item_byte_size),
72729cb868aSZachary Turner                 (int)(2 * item_byte_size), addr);
72829cb868aSZachary Turner       if (exe_scope) {
72929cb868aSZachary Turner         TargetSP target_sp(exe_scope->CalculateTarget());
73029cb868aSZachary Turner         lldb_private::Address so_addr;
73129cb868aSZachary Turner         if (target_sp) {
73229cb868aSZachary Turner           if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr,
73329cb868aSZachary Turner                                                                  so_addr)) {
73429cb868aSZachary Turner             s->PutChar(' ');
73529cb868aSZachary Turner             so_addr.Dump(s, exe_scope, Address::DumpStyleResolvedDescription,
73629cb868aSZachary Turner                          Address::DumpStyleModuleWithFileAddress);
73729cb868aSZachary Turner           } else {
73829cb868aSZachary Turner             so_addr.SetOffset(addr);
73929cb868aSZachary Turner             so_addr.Dump(s, exe_scope,
74029cb868aSZachary Turner                          Address::DumpStyleResolvedPointerDescription);
741f7414759SJonas Devlieghere             if (ProcessSP process_sp = exe_scope->CalculateProcess()) {
742f7414759SJonas Devlieghere               if (ABISP abi_sp = process_sp->GetABI()) {
743f7414759SJonas Devlieghere                 addr_t addr_fixed = abi_sp->FixCodeAddress(addr);
744f7414759SJonas Devlieghere                 if (target_sp->GetSectionLoadList().ResolveLoadAddress(
745f7414759SJonas Devlieghere                         addr_fixed, so_addr)) {
746f7414759SJonas Devlieghere                   s->PutChar(' ');
747f7414759SJonas Devlieghere                   s->Printf("(0x%*.*" PRIx64 ")", (int)(2 * item_byte_size),
748f7414759SJonas Devlieghere                             (int)(2 * item_byte_size), addr_fixed);
749f7414759SJonas Devlieghere                   s->PutChar(' ');
750f7414759SJonas Devlieghere                   so_addr.Dump(s, exe_scope,
751f7414759SJonas Devlieghere                                Address::DumpStyleResolvedDescription,
752f7414759SJonas Devlieghere                                Address::DumpStyleModuleWithFileAddress);
753f7414759SJonas Devlieghere                 }
754f7414759SJonas Devlieghere               }
755f7414759SJonas Devlieghere             }
75629cb868aSZachary Turner           }
75729cb868aSZachary Turner         }
75829cb868aSZachary Turner       }
75929cb868aSZachary Turner     } break;
76029cb868aSZachary Turner 
76129cb868aSZachary Turner     case eFormatHexFloat:
76229cb868aSZachary Turner       if (sizeof(float) == item_byte_size) {
76329cb868aSZachary Turner         char float_cstr[256];
76429cb868aSZachary Turner         llvm::APFloat ap_float(DE.GetFloat(&offset));
76529cb868aSZachary Turner         ap_float.convertToHexString(float_cstr, 0, false,
76629cb868aSZachary Turner                                     llvm::APFloat::rmNearestTiesToEven);
76729cb868aSZachary Turner         s->Printf("%s", float_cstr);
76829cb868aSZachary Turner         break;
76929cb868aSZachary Turner       } else if (sizeof(double) == item_byte_size) {
77029cb868aSZachary Turner         char float_cstr[256];
77129cb868aSZachary Turner         llvm::APFloat ap_float(DE.GetDouble(&offset));
77229cb868aSZachary Turner         ap_float.convertToHexString(float_cstr, 0, false,
77329cb868aSZachary Turner                                     llvm::APFloat::rmNearestTiesToEven);
77429cb868aSZachary Turner         s->Printf("%s", float_cstr);
77529cb868aSZachary Turner         break;
77629cb868aSZachary Turner       } else {
77729cb868aSZachary Turner         s->Printf("error: unsupported byte size (%" PRIu64
77829cb868aSZachary Turner                   ") for hex float format",
77929cb868aSZachary Turner                   (uint64_t)item_byte_size);
78029cb868aSZachary Turner         return offset;
78129cb868aSZachary Turner       }
78229cb868aSZachary Turner       break;
78329cb868aSZachary Turner 
78429cb868aSZachary Turner     // please keep the single-item formats below in sync with
78505097246SAdrian Prantl     // FormatManager::GetSingleItemFormat if you fail to do so, users will
78605097246SAdrian Prantl     // start getting different outputs depending on internal implementation
78705097246SAdrian Prantl     // details they should not care about ||
78829cb868aSZachary Turner     case eFormatVectorOfChar: //   ||
78929cb868aSZachary Turner       s->PutChar('{');        //   \/
79029cb868aSZachary Turner       offset =
79129cb868aSZachary Turner           DumpDataExtractor(DE, s, offset, eFormatCharArray, 1, item_byte_size,
79229cb868aSZachary Turner                             item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
79329cb868aSZachary Turner       s->PutChar('}');
79429cb868aSZachary Turner       break;
79529cb868aSZachary Turner 
79629cb868aSZachary Turner     case eFormatVectorOfSInt8:
79729cb868aSZachary Turner       s->PutChar('{');
79829cb868aSZachary Turner       offset =
79929cb868aSZachary Turner           DumpDataExtractor(DE, s, offset, eFormatDecimal, 1, item_byte_size,
80029cb868aSZachary Turner                             item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
80129cb868aSZachary Turner       s->PutChar('}');
80229cb868aSZachary Turner       break;
80329cb868aSZachary Turner 
80429cb868aSZachary Turner     case eFormatVectorOfUInt8:
80529cb868aSZachary Turner       s->PutChar('{');
80629cb868aSZachary Turner       offset = DumpDataExtractor(DE, s, offset, eFormatHex, 1, item_byte_size,
80729cb868aSZachary Turner                                  item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
80829cb868aSZachary Turner       s->PutChar('}');
80929cb868aSZachary Turner       break;
81029cb868aSZachary Turner 
81129cb868aSZachary Turner     case eFormatVectorOfSInt16:
81229cb868aSZachary Turner       s->PutChar('{');
81329cb868aSZachary Turner       offset = DumpDataExtractor(
81429cb868aSZachary Turner           DE, s, offset, eFormatDecimal, sizeof(uint16_t),
81529cb868aSZachary Turner           item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t),
81629cb868aSZachary Turner           LLDB_INVALID_ADDRESS, 0, 0);
81729cb868aSZachary Turner       s->PutChar('}');
81829cb868aSZachary Turner       break;
81929cb868aSZachary Turner 
82029cb868aSZachary Turner     case eFormatVectorOfUInt16:
82129cb868aSZachary Turner       s->PutChar('{');
82229cb868aSZachary Turner       offset = DumpDataExtractor(DE, s, offset, eFormatHex, sizeof(uint16_t),
82329cb868aSZachary Turner                                  item_byte_size / sizeof(uint16_t),
82429cb868aSZachary Turner                                  item_byte_size / sizeof(uint16_t),
82529cb868aSZachary Turner                                  LLDB_INVALID_ADDRESS, 0, 0);
82629cb868aSZachary Turner       s->PutChar('}');
82729cb868aSZachary Turner       break;
82829cb868aSZachary Turner 
82929cb868aSZachary Turner     case eFormatVectorOfSInt32:
83029cb868aSZachary Turner       s->PutChar('{');
83129cb868aSZachary Turner       offset = DumpDataExtractor(
83229cb868aSZachary Turner           DE, s, offset, eFormatDecimal, sizeof(uint32_t),
83329cb868aSZachary Turner           item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t),
83429cb868aSZachary Turner           LLDB_INVALID_ADDRESS, 0, 0);
83529cb868aSZachary Turner       s->PutChar('}');
83629cb868aSZachary Turner       break;
83729cb868aSZachary Turner 
83829cb868aSZachary Turner     case eFormatVectorOfUInt32:
83929cb868aSZachary Turner       s->PutChar('{');
84029cb868aSZachary Turner       offset = DumpDataExtractor(DE, s, offset, eFormatHex, sizeof(uint32_t),
84129cb868aSZachary Turner                                  item_byte_size / sizeof(uint32_t),
84229cb868aSZachary Turner                                  item_byte_size / sizeof(uint32_t),
84329cb868aSZachary Turner                                  LLDB_INVALID_ADDRESS, 0, 0);
84429cb868aSZachary Turner       s->PutChar('}');
84529cb868aSZachary Turner       break;
84629cb868aSZachary Turner 
84729cb868aSZachary Turner     case eFormatVectorOfSInt64:
84829cb868aSZachary Turner       s->PutChar('{');
84929cb868aSZachary Turner       offset = DumpDataExtractor(
85029cb868aSZachary Turner           DE, s, offset, eFormatDecimal, sizeof(uint64_t),
85129cb868aSZachary Turner           item_byte_size / sizeof(uint64_t), item_byte_size / sizeof(uint64_t),
85229cb868aSZachary Turner           LLDB_INVALID_ADDRESS, 0, 0);
85329cb868aSZachary Turner       s->PutChar('}');
85429cb868aSZachary Turner       break;
85529cb868aSZachary Turner 
85629cb868aSZachary Turner     case eFormatVectorOfUInt64:
85729cb868aSZachary Turner       s->PutChar('{');
85829cb868aSZachary Turner       offset = DumpDataExtractor(DE, s, offset, eFormatHex, sizeof(uint64_t),
85929cb868aSZachary Turner                                  item_byte_size / sizeof(uint64_t),
86029cb868aSZachary Turner                                  item_byte_size / sizeof(uint64_t),
86129cb868aSZachary Turner                                  LLDB_INVALID_ADDRESS, 0, 0);
86229cb868aSZachary Turner       s->PutChar('}');
86329cb868aSZachary Turner       break;
86429cb868aSZachary Turner 
86529cb868aSZachary Turner     case eFormatVectorOfFloat16:
86629cb868aSZachary Turner       s->PutChar('{');
86729cb868aSZachary Turner       offset =
86829cb868aSZachary Turner           DumpDataExtractor(DE, s, offset, eFormatFloat, 2, item_byte_size / 2,
86929cb868aSZachary Turner                             item_byte_size / 2, LLDB_INVALID_ADDRESS, 0, 0);
87029cb868aSZachary Turner       s->PutChar('}');
87129cb868aSZachary Turner       break;
87229cb868aSZachary Turner 
87329cb868aSZachary Turner     case eFormatVectorOfFloat32:
87429cb868aSZachary Turner       s->PutChar('{');
87529cb868aSZachary Turner       offset =
87629cb868aSZachary Turner           DumpDataExtractor(DE, s, offset, eFormatFloat, 4, item_byte_size / 4,
87729cb868aSZachary Turner                             item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0);
87829cb868aSZachary Turner       s->PutChar('}');
87929cb868aSZachary Turner       break;
88029cb868aSZachary Turner 
88129cb868aSZachary Turner     case eFormatVectorOfFloat64:
88229cb868aSZachary Turner       s->PutChar('{');
88329cb868aSZachary Turner       offset =
88429cb868aSZachary Turner           DumpDataExtractor(DE, s, offset, eFormatFloat, 8, item_byte_size / 8,
88529cb868aSZachary Turner                             item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0);
88629cb868aSZachary Turner       s->PutChar('}');
88729cb868aSZachary Turner       break;
88829cb868aSZachary Turner 
88929cb868aSZachary Turner     case eFormatVectorOfUInt128:
89029cb868aSZachary Turner       s->PutChar('{');
89129cb868aSZachary Turner       offset =
89229cb868aSZachary Turner           DumpDataExtractor(DE, s, offset, eFormatHex, 16, item_byte_size / 16,
89329cb868aSZachary Turner                             item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0);
89429cb868aSZachary Turner       s->PutChar('}');
89529cb868aSZachary Turner       break;
89629cb868aSZachary Turner     }
89729cb868aSZachary Turner   }
89829cb868aSZachary Turner 
899070090d0SDavid Spickett   // If anything was printed we want to catch the end of the last line.
900070090d0SDavid Spickett   // Since we will exit the for loop above before we get a chance to append to
901070090d0SDavid Spickett   // it normally.
902070090d0SDavid Spickett   if (offset > line_start_offset) {
903070090d0SDavid Spickett     if (item_format == eFormatBytesWithASCII) {
904070090d0SDavid Spickett       s->Printf("%*s",
905070090d0SDavid Spickett                 static_cast<int>(
90629cb868aSZachary Turner                     (num_per_line - (offset - line_start_offset)) * 3 + 2),
90729cb868aSZachary Turner                 "");
90829cb868aSZachary Turner       DumpDataExtractor(DE, s, line_start_offset, eFormatCharPrintable, 1,
90929cb868aSZachary Turner                         offset - line_start_offset, SIZE_MAX,
91029cb868aSZachary Turner                         LLDB_INVALID_ADDRESS, 0, 0);
91129cb868aSZachary Turner     }
912070090d0SDavid Spickett 
913070090d0SDavid Spickett     if (base_addr != LLDB_INVALID_ADDRESS && memory_tag_map) {
914070090d0SDavid Spickett       size_t line_len = offset - line_start_offset;
915070090d0SDavid Spickett       lldb::addr_t line_base = base_addr + (offset - start_offset - line_len) /
916070090d0SDavid Spickett                                                DE.getTargetByteSize();
917070090d0SDavid Spickett       printMemoryTags(DE, s, line_base, line_len, memory_tag_map);
918070090d0SDavid Spickett     }
919070090d0SDavid Spickett   }
920070090d0SDavid Spickett 
92129cb868aSZachary Turner   return offset; // Return the offset at which we ended up
92229cb868aSZachary Turner }
9239739a552SZachary Turner 
DumpHexBytes(Stream * s,const void * src,size_t src_len,uint32_t bytes_per_line,lldb::addr_t base_addr)9249739a552SZachary Turner void lldb_private::DumpHexBytes(Stream *s, const void *src, size_t src_len,
9259739a552SZachary Turner                                 uint32_t bytes_per_line,
9269739a552SZachary Turner                                 lldb::addr_t base_addr) {
9279739a552SZachary Turner   DataExtractor data(src, src_len, lldb::eByteOrderLittle, 4);
9289739a552SZachary Turner   DumpDataExtractor(data, s,
9299739a552SZachary Turner                     0,                  // Offset into "src"
9309739a552SZachary Turner                     lldb::eFormatBytes, // Dump as hex bytes
9319739a552SZachary Turner                     1,              // Size of each item is 1 for single bytes
9329739a552SZachary Turner                     src_len,        // Number of bytes
9339739a552SZachary Turner                     bytes_per_line, // Num bytes per line
9349739a552SZachary Turner                     base_addr,      // Base address
9359739a552SZachary Turner                     0, 0);          // Bitfield info
9369739a552SZachary Turner }
937