brintos

brintos / llvm-project-archived public Read only

0
0
Text · 12.5 KiB · e9632c3 Raw
428 lines · cpp
1//===-- Stream.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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Utility/Stream.h"10 11#include "lldb/Utility/AnsiTerminal.h"12#include "lldb/Utility/Endian.h"13#include "lldb/Utility/VASPrintf.h"14#include "llvm/ADT/SmallString.h"15#include "llvm/Support/Format.h"16#include "llvm/Support/LEB128.h"17#include "llvm/Support/Regex.h"18 19#include <string>20 21#include <cinttypes>22#include <cstddef>23 24using namespace lldb;25using namespace lldb_private;26 27Stream::Stream(uint32_t flags, uint32_t addr_size, ByteOrder byte_order,28               bool colors)29    : m_flags(flags), m_addr_size(addr_size), m_byte_order(byte_order),30      m_forwarder(*this, colors) {}31 32Stream::Stream(bool colors)33    : m_flags(0), m_byte_order(endian::InlHostByteOrder()),34      m_forwarder(*this, colors) {}35 36// Destructor37Stream::~Stream() = default;38 39ByteOrder Stream::SetByteOrder(ByteOrder byte_order) {40  ByteOrder old_byte_order = m_byte_order;41  m_byte_order = byte_order;42  return old_byte_order;43}44 45// Put an offset "uval" out to the stream using the printf format in "format".46void Stream::Offset(uint32_t uval, const char *format) { Printf(format, uval); }47 48// Put an SLEB128 "uval" out to the stream using the printf format in "format".49size_t Stream::PutSLEB128(int64_t sval) {50  if (m_flags.Test(eBinary))51    return llvm::encodeSLEB128(sval, m_forwarder);52  else53    return Printf("0x%" PRIi64, sval);54}55 56// Put an ULEB128 "uval" out to the stream using the printf format in "format".57size_t Stream::PutULEB128(uint64_t uval) {58  if (m_flags.Test(eBinary))59    return llvm::encodeULEB128(uval, m_forwarder);60  else61    return Printf("0x%" PRIx64, uval);62}63 64// Print a raw NULL terminated C string to the stream.65size_t Stream::PutCString(llvm::StringRef str) {66  size_t bytes_written = 0;67  bytes_written = Write(str.data(), str.size());68 69  // when in binary mode, emit the NULL terminator70  if (m_flags.Test(eBinary))71    bytes_written += PutChar('\0');72  return bytes_written;73}74 75void Stream::PutCStringColorHighlighted(76    llvm::StringRef text, std::optional<HighlightSettings> pattern_info) {77  // Only apply color formatting when a pattern information is specified.78  // Otherwise, output the text without color formatting.79  if (!pattern_info.has_value()) {80    PutCString(text);81    return;82  }83 84  llvm::Regex reg_pattern(pattern_info->pattern);85  llvm::SmallVector<llvm::StringRef, 1> matches;86  llvm::StringRef remaining = text;87  std::string format_str = lldb_private::ansi::FormatAnsiTerminalCodes(88      pattern_info->prefix.str() + "%.*s" + pattern_info->suffix.str());89  while (reg_pattern.match(remaining, &matches)) {90    llvm::StringRef match = matches[0];91    size_t match_start_pos = match.data() - remaining.data();92    PutCString(remaining.take_front(match_start_pos));93    Printf(format_str.c_str(), match.size(), match.data());94    remaining = remaining.drop_front(match_start_pos + match.size());95  }96  if (remaining.size())97    PutCString(remaining);98}99 100// Print a double quoted NULL terminated C string to the stream using the101// printf format in "format".102void Stream::QuotedCString(const char *cstr, const char *format) {103  Printf(format, cstr);104}105 106// Put an address "addr" out to the stream with optional prefix and suffix107// strings.108void lldb_private::DumpAddress(llvm::raw_ostream &s, uint64_t addr,109                               uint32_t addr_size, const char *prefix,110                               const char *suffix) {111  if (prefix == nullptr)112    prefix = "";113  if (suffix == nullptr)114    suffix = "";115  s << prefix << llvm::format_hex(addr, 2 + 2 * addr_size) << suffix;116}117 118// Put an address range out to the stream with optional prefix and suffix119// strings.120void lldb_private::DumpAddressRange(llvm::raw_ostream &s, uint64_t lo_addr,121                                    uint64_t hi_addr, uint32_t addr_size,122                                    const char *prefix, const char *suffix) {123  if (prefix && prefix[0])124    s << prefix;125  DumpAddress(s, lo_addr, addr_size, "[");126  DumpAddress(s, hi_addr, addr_size, "-", ")");127  if (suffix && suffix[0])128    s << suffix;129}130 131size_t Stream::PutChar(char ch) { return Write(&ch, 1); }132 133// Print some formatted output to the stream.134size_t Stream::Printf(const char *format, ...) {135  va_list args;136  va_start(args, format);137  size_t result = PrintfVarArg(format, args);138  va_end(args);139  return result;140}141 142// Print some formatted output to the stream.143size_t Stream::PrintfVarArg(const char *format, va_list args) {144  llvm::SmallString<1024> buf;145  VASprintf(buf, format, args);146 147  // Include the NULL termination byte for binary output148  size_t length = buf.size();149  if (m_flags.Test(eBinary))150    ++length;151  return Write(buf.c_str(), length);152}153 154// Print and End of Line character to the stream155size_t Stream::EOL() { return PutChar('\n'); }156 157size_t Stream::Indent(llvm::StringRef str) {158  const size_t ind_length = PutCString(std::string(m_indent_level, ' '));159  const size_t str_length = PutCString(str);160  return ind_length + str_length;161}162 163// Stream a character "ch" out to this stream.164Stream &Stream::operator<<(char ch) {165  PutChar(ch);166  return *this;167}168 169// Stream the NULL terminated C string out to this stream.170Stream &Stream::operator<<(const char *s) {171  Printf("%s", s);172  return *this;173}174 175Stream &Stream::operator<<(llvm::StringRef str) {176  Write(str.data(), str.size());177  return *this;178}179 180// Stream the pointer value out to this stream.181Stream &Stream::operator<<(const void *p) {182  Printf("0x%.*tx", static_cast<int>(sizeof(const void *)) * 2, (ptrdiff_t)p);183  return *this;184}185 186// Get the current indentation level187unsigned Stream::GetIndentLevel() const { return m_indent_level; }188 189// Set the current indentation level190void Stream::SetIndentLevel(unsigned indent_level) {191  m_indent_level = indent_level;192}193 194// Increment the current indentation level195void Stream::IndentMore(unsigned amount) { m_indent_level += amount; }196 197// Decrement the current indentation level198void Stream::IndentLess(unsigned amount) {199  if (m_indent_level >= amount)200    m_indent_level -= amount;201  else202    m_indent_level = 0;203}204 205// Create an indentation scope that restores the original indent level when the206// object goes out of scope (RAII).207Stream::IndentScope Stream::MakeIndentScope(unsigned indent_amount) {208  IndentScope indent_scope(*this);209  IndentMore(indent_amount);210  return indent_scope;211}212 213// Get the address size in bytes214uint32_t Stream::GetAddressByteSize() const { return m_addr_size; }215 216// Set the address size in bytes217void Stream::SetAddressByteSize(uint32_t addr_size) { m_addr_size = addr_size; }218 219// The flags get accessor220Flags &Stream::GetFlags() { return m_flags; }221 222// The flags const get accessor223const Flags &Stream::GetFlags() const { return m_flags; }224 225// The byte order get accessor226 227lldb::ByteOrder Stream::GetByteOrder() const { return m_byte_order; }228 229size_t Stream::PrintfAsRawHex8(const char *format, ...) {230  va_list args;231  va_start(args, format);232 233  llvm::SmallString<1024> buf;234  VASprintf(buf, format, args);235 236  ByteDelta delta(*this);237  for (char C : buf)238    _PutHex8(C, false);239 240  va_end(args);241 242  return *delta;243}244 245size_t Stream::PutNHex8(size_t n, uint8_t uvalue) {246  ByteDelta delta(*this);247  for (size_t i = 0; i < n; ++i)248    _PutHex8(uvalue, false);249  return *delta;250}251 252void Stream::_PutHex8(uint8_t uvalue, bool add_prefix) {253  if (m_flags.Test(eBinary)) {254    Write(&uvalue, 1);255  } else {256    if (add_prefix)257      PutCString("0x");258 259    static char g_hex_to_ascii_hex_char[16] = {'0', '1', '2', '3', '4', '5',260                                               '6', '7', '8', '9', 'a', 'b',261                                               'c', 'd', 'e', 'f'};262    char nibble_chars[2];263    nibble_chars[0] = g_hex_to_ascii_hex_char[(uvalue >> 4) & 0xf];264    nibble_chars[1] = g_hex_to_ascii_hex_char[(uvalue >> 0) & 0xf];265    Write(nibble_chars, sizeof(nibble_chars));266  }267}268 269size_t Stream::PutHex8(uint8_t uvalue) {270  ByteDelta delta(*this);271  _PutHex8(uvalue, false);272  return *delta;273}274 275size_t Stream::PutHex16(uint16_t uvalue, ByteOrder byte_order) {276  ByteDelta delta(*this);277 278  if (byte_order == eByteOrderInvalid)279    byte_order = m_byte_order;280 281  if (byte_order == eByteOrderLittle) {282    for (size_t byte = 0; byte < sizeof(uvalue); ++byte)283      _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false);284  } else {285    for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte)286      _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false);287  }288  return *delta;289}290 291size_t Stream::PutHex32(uint32_t uvalue, ByteOrder byte_order) {292  ByteDelta delta(*this);293 294  if (byte_order == eByteOrderInvalid)295    byte_order = m_byte_order;296 297  if (byte_order == eByteOrderLittle) {298    for (size_t byte = 0; byte < sizeof(uvalue); ++byte)299      _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false);300  } else {301    for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte)302      _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false);303  }304  return *delta;305}306 307size_t Stream::PutHex64(uint64_t uvalue, ByteOrder byte_order) {308  ByteDelta delta(*this);309 310  if (byte_order == eByteOrderInvalid)311    byte_order = m_byte_order;312 313  if (byte_order == eByteOrderLittle) {314    for (size_t byte = 0; byte < sizeof(uvalue); ++byte)315      _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false);316  } else {317    for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte)318      _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false);319  }320  return *delta;321}322 323size_t Stream::PutMaxHex64(uint64_t uvalue, size_t byte_size,324                           lldb::ByteOrder byte_order) {325  switch (byte_size) {326  case 1:327    return PutHex8(static_cast<uint8_t>(uvalue));328  case 2:329    return PutHex16(static_cast<uint16_t>(uvalue), byte_order);330  case 4:331    return PutHex32(static_cast<uint32_t>(uvalue), byte_order);332  case 8:333    return PutHex64(uvalue, byte_order);334  }335  return 0;336}337 338size_t Stream::PutPointer(void *ptr) {339  return PutRawBytes(&ptr, sizeof(ptr), endian::InlHostByteOrder(),340                     endian::InlHostByteOrder());341}342 343size_t Stream::PutFloat(float f, ByteOrder byte_order) {344  if (byte_order == eByteOrderInvalid)345    byte_order = m_byte_order;346 347  return PutRawBytes(&f, sizeof(f), endian::InlHostByteOrder(), byte_order);348}349 350size_t Stream::PutDouble(double d, ByteOrder byte_order) {351  if (byte_order == eByteOrderInvalid)352    byte_order = m_byte_order;353 354  return PutRawBytes(&d, sizeof(d), endian::InlHostByteOrder(), byte_order);355}356 357size_t Stream::PutLongDouble(long double ld, ByteOrder byte_order) {358  if (byte_order == eByteOrderInvalid)359    byte_order = m_byte_order;360 361  return PutRawBytes(&ld, sizeof(ld), endian::InlHostByteOrder(), byte_order);362}363 364size_t Stream::PutRawBytes(const void *s, size_t src_len,365                           ByteOrder src_byte_order, ByteOrder dst_byte_order) {366  ByteDelta delta(*this);367 368  if (src_byte_order == eByteOrderInvalid)369    src_byte_order = m_byte_order;370 371  if (dst_byte_order == eByteOrderInvalid)372    dst_byte_order = m_byte_order;373 374  const uint8_t *src = static_cast<const uint8_t *>(s);375  bool binary_was_set = m_flags.Test(eBinary);376  if (!binary_was_set)377    m_flags.Set(eBinary);378  if (src_byte_order == dst_byte_order) {379    for (size_t i = 0; i < src_len; ++i)380      _PutHex8(src[i], false);381  } else {382    for (size_t i = src_len; i > 0; --i)383      _PutHex8(src[i - 1], false);384  }385  if (!binary_was_set)386    m_flags.Clear(eBinary);387 388  return *delta;389}390 391size_t Stream::PutBytesAsRawHex8(const void *s, size_t src_len,392                                 ByteOrder src_byte_order,393                                 ByteOrder dst_byte_order) {394  ByteDelta delta(*this);395 396  if (src_byte_order == eByteOrderInvalid)397    src_byte_order = m_byte_order;398 399  if (dst_byte_order == eByteOrderInvalid)400    dst_byte_order = m_byte_order;401 402  const uint8_t *src = static_cast<const uint8_t *>(s);403  bool binary_is_set = m_flags.Test(eBinary);404  m_flags.Clear(eBinary);405  if (src_byte_order == dst_byte_order) {406    for (size_t i = 0; i < src_len; ++i)407      _PutHex8(src[i], false);408  } else {409    for (size_t i = src_len; i > 0; --i)410      _PutHex8(src[i - 1], false);411  }412  if (binary_is_set)413    m_flags.Set(eBinary);414 415  return *delta;416}417 418size_t Stream::PutStringAsRawHex8(llvm::StringRef s) {419  ByteDelta delta(*this);420  bool binary_is_set = m_flags.Test(eBinary);421  m_flags.Clear(eBinary);422  for (char c : s)423    _PutHex8(c, false);424  if (binary_is_set)425    m_flags.Set(eBinary);426  return *delta;427}428