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