1258945Sroberto//===-- Stream.h ------------------------------------------------*- C++ -*-===// 2280849Scy// 3258945Sroberto// The LLVM Compiler Infrastructure 4258945Sroberto// 5258945Sroberto// This file is distributed under the University of Illinois Open Source 6258945Sroberto// License. See LICENSE.TXT for details. 7258945Sroberto// 8258945Sroberto//===----------------------------------------------------------------------===// 9258945Sroberto 10258945Sroberto#ifndef liblldb_Stream_h_ 11258945Sroberto#define liblldb_Stream_h_ 12258945Sroberto#if defined(__cplusplus) 13258945Sroberto 14258945Sroberto#include "lldb/lldb-private.h" 15258945Sroberto#include "lldb/Core/Flags.h" 16258945Sroberto#include <stdarg.h> 17258945Sroberto 18280849Scynamespace lldb_private { 19258945Sroberto 20258945Sroberto//---------------------------------------------------------------------- 21258945Sroberto/// @class Stream Stream.h "lldb/Core/Stream.h" 22258945Sroberto/// @brief A stream class that can stream formatted output to a file. 23258945Sroberto//---------------------------------------------------------------------- 24258945Srobertoclass Stream 25258945Sroberto{ 26258945Srobertopublic: 27258945Sroberto //------------------------------------------------------------------ 28258945Sroberto /// \a m_flags bit values. 29258945Sroberto //------------------------------------------------------------------ 30258945Sroberto enum 31258945Sroberto { 32258945Sroberto eVerbose = (1 << 0), ///< If set, verbose logging is enabled 33258945Sroberto eDebug = (1 << 1), ///< If set, debug logging is enabled 34258945Sroberto eAddPrefix = (1 << 2), ///< Add number prefixes for binary, octal and hex when eBinary is clear 35258945Sroberto eBinary = (1 << 3) ///< Get and put data as binary instead of as the default string mode. 36258945Sroberto }; 37258945Sroberto 38258945Sroberto //------------------------------------------------------------------ 39258945Sroberto /// Construct with flags and address size and byte order. 40258945Sroberto /// 41258945Sroberto /// Construct with dump flags \a flags and the default address 42258945Sroberto /// size. \a flags can be any of the above enumeration logical OR'ed 43258945Sroberto /// together. 44258945Sroberto //------------------------------------------------------------------ 45258945Sroberto Stream (uint32_t flags, 46258945Sroberto uint32_t addr_size, 47258945Sroberto lldb::ByteOrder byte_order); 48258945Sroberto 49258945Sroberto //------------------------------------------------------------------ 50258945Sroberto /// Construct a default Stream, not binary, host byte order and 51258945Sroberto /// host addr size. 52258945Sroberto /// 53258945Sroberto //------------------------------------------------------------------ 54258945Sroberto Stream (); 55258945Sroberto 56258945Sroberto //------------------------------------------------------------------ 57258945Sroberto /// Destructor 58258945Sroberto //------------------------------------------------------------------ 59258945Sroberto virtual 60258945Sroberto ~Stream (); 61258945Sroberto 62258945Sroberto //------------------------------------------------------------------ 63258945Sroberto // Subclasses must override these methods 64258945Sroberto //------------------------------------------------------------------ 65258945Sroberto 66258945Sroberto //------------------------------------------------------------------ 67258945Sroberto /// Flush the stream. 68258945Sroberto /// 69258945Sroberto /// Subclasses should flush the stream to make any output appear 70258945Sroberto /// if the stream has any buffering. 71258945Sroberto //------------------------------------------------------------------ 72258945Sroberto virtual void 73258945Sroberto Flush () = 0; 74258945Sroberto 75258945Sroberto //------------------------------------------------------------------ 76258945Sroberto /// Output character bytes to the stream. 77258945Sroberto /// 78258945Sroberto /// Appends \a src_len characters from the buffer \a src to the 79258945Sroberto /// stream. 80258945Sroberto /// 81258945Sroberto /// @param[in] src 82258945Sroberto /// A buffer containing at least \a src_len bytes of data. 83258945Sroberto /// 84258945Sroberto /// @param[in] src_len 85258945Sroberto /// A number of bytes to append to the stream. 86258945Sroberto /// 87258945Sroberto /// @return 88258945Sroberto /// The number of bytes that were appended to the stream. 89258945Sroberto //------------------------------------------------------------------ 90258945Sroberto virtual size_t 91258945Sroberto Write (const void *src, size_t src_len) = 0; 92258945Sroberto 93258945Sroberto //------------------------------------------------------------------ 94258945Sroberto // Member functions 95258945Sroberto //------------------------------------------------------------------ 96258945Sroberto size_t 97258945Sroberto PutChar (char ch); 98258945Sroberto 99258945Sroberto //------------------------------------------------------------------ 100258945Sroberto /// Set the byte_order value. 101258945Sroberto /// 102258945Sroberto /// Sets the byte order of the data to extract. Extracted values 103258945Sroberto /// will be swapped if necessary when decoding. 104258945Sroberto /// 105258945Sroberto /// @param[in] byte_order 106258945Sroberto /// The byte order value to use when extracting data. 107258945Sroberto /// 108258945Sroberto /// @return 109258945Sroberto /// The old byte order value. 110258945Sroberto //------------------------------------------------------------------ 111258945Sroberto lldb::ByteOrder 112258945Sroberto SetByteOrder (lldb::ByteOrder byte_order); 113258945Sroberto 114258945Sroberto //------------------------------------------------------------------ 115258945Sroberto /// Format a C string from a printf style format and variable 116258945Sroberto /// arguments and encode and append the resulting C string as hex 117258945Sroberto /// bytes. 118258945Sroberto /// 119258945Sroberto /// @param[in] format 120258945Sroberto /// A printf style format string. 121258945Sroberto /// 122258945Sroberto /// @param[in] ... 123258945Sroberto /// Any additional arguments needed for the printf format string. 124258945Sroberto /// 125258945Sroberto /// @return 126258945Sroberto /// The number of bytes that were appended to the stream. 127258945Sroberto //------------------------------------------------------------------ 128258945Sroberto size_t 129258945Sroberto PrintfAsRawHex8 (const char *format, ...) __attribute__ ((format (printf, 2, 3))); 130258945Sroberto 131258945Sroberto //------------------------------------------------------------------ 132258945Sroberto /// Format a C string from a printf style format and variable 133258945Sroberto /// arguments and encode and append the resulting C string as hex 134258945Sroberto /// bytes. 135258945Sroberto /// 136258945Sroberto /// @param[in] format 137258945Sroberto /// A printf style format string. 138258945Sroberto /// 139258945Sroberto /// @param[in] ... 140258945Sroberto /// Any additional arguments needed for the printf format string. 141258945Sroberto /// 142258945Sroberto /// @return 143258945Sroberto /// The number of bytes that were appended to the stream. 144258945Sroberto //------------------------------------------------------------------ 145258945Sroberto size_t 146258945Sroberto PutHex8 (uint8_t uvalue); 147258945Sroberto 148258945Sroberto size_t 149280849Scy PutNHex8 (size_t n, uint8_t uvalue); 150280849Scy 151280849Scy size_t 152280849Scy PutHex16 (uint16_t uvalue, 153280849Scy lldb::ByteOrder byte_order = lldb::eByteOrderInvalid); 154258945Sroberto 155258945Sroberto size_t 156258945Sroberto PutHex32 (uint32_t uvalue, 157258945Sroberto lldb::ByteOrder byte_order = lldb::eByteOrderInvalid); 158258945Sroberto 159258945Sroberto size_t 160258945Sroberto PutHex64 (uint64_t uvalue, 161258945Sroberto lldb::ByteOrder byte_order = lldb::eByteOrderInvalid); 162258945Sroberto 163258945Sroberto size_t 164258945Sroberto PutMaxHex64 (uint64_t uvalue, 165258945Sroberto size_t byte_size, 166258945Sroberto lldb::ByteOrder byte_order = lldb::eByteOrderInvalid); 167258945Sroberto size_t 168258945Sroberto PutFloat (float f, 169258945Sroberto lldb::ByteOrder byte_order = lldb::eByteOrderInvalid); 170258945Sroberto 171258945Sroberto size_t 172258945Sroberto PutDouble (double d, 173258945Sroberto lldb::ByteOrder byte_order = lldb::eByteOrderInvalid); 174258945Sroberto 175258945Sroberto size_t 176258945Sroberto PutLongDouble (long double ld, 177258945Sroberto lldb::ByteOrder byte_order = lldb::eByteOrderInvalid); 178258945Sroberto 179258945Sroberto size_t 180258945Sroberto PutPointer (void *ptr); 181258945Sroberto 182258945Sroberto // Append \a src_len bytes from \a src to the stream as hex characters 183258945Sroberto // (two ascii characters per byte of input data) 184258945Sroberto size_t 185258945Sroberto PutBytesAsRawHex8 (const void *src, 186258945Sroberto size_t src_len, 187258945Sroberto lldb::ByteOrder src_byte_order = lldb::eByteOrderInvalid, 188258945Sroberto lldb::ByteOrder dst_byte_order = lldb::eByteOrderInvalid); 189258945Sroberto 190258945Sroberto // Append \a src_len bytes from \a s to the stream as binary data. 191258945Sroberto size_t 192258945Sroberto PutRawBytes (const void *s, 193258945Sroberto size_t src_len, 194258945Sroberto lldb::ByteOrder src_byte_order = lldb::eByteOrderInvalid, 195258945Sroberto lldb::ByteOrder dst_byte_order = lldb::eByteOrderInvalid); 196258945Sroberto 197258945Sroberto size_t 198258945Sroberto PutCStringAsRawHex8 (const char *s); 199258945Sroberto 200258945Sroberto //------------------------------------------------------------------ 201258945Sroberto /// Output a NULL terminated C string \a cstr to the stream \a s. 202258945Sroberto /// 203258945Sroberto /// @param[in] cstr 204258945Sroberto /// A NULL terminated C string. 205258945Sroberto /// 206258945Sroberto /// @return 207258945Sroberto /// A reference to this class so multiple things can be streamed 208258945Sroberto /// in one statement. 209258945Sroberto //------------------------------------------------------------------ 210258945Sroberto Stream& 211258945Sroberto operator<< (const char *cstr); 212258945Sroberto 213258945Sroberto //------------------------------------------------------------------ 214258945Sroberto /// Output a pointer value \a p to the stream \a s. 215258945Sroberto /// 216258945Sroberto /// @param[in] p 217258945Sroberto /// A void pointer. 218258945Sroberto /// 219258945Sroberto /// @return 220258945Sroberto /// A reference to this class so multiple things can be streamed 221258945Sroberto /// in one statement. 222258945Sroberto //------------------------------------------------------------------ 223280849Scy Stream& 224280849Scy operator<< (void *p); 225280849Scy 226280849Scy //------------------------------------------------------------------ 227280849Scy /// Output a character \a ch to the stream \a s. 228280849Scy /// 229280849Scy /// @param[in] ch 230280849Scy /// A printable character value. 231280849Scy /// 232280849Scy /// @return 233280849Scy /// A reference to this class so multiple things can be streamed 234280849Scy /// in one statement. 235258945Sroberto //------------------------------------------------------------------ 236258945Sroberto Stream& 237258945Sroberto operator<< (char ch); 238258945Sroberto 239258945Sroberto //------------------------------------------------------------------ 240258945Sroberto /// Output a uint8_t \a uval to the stream \a s. 241258945Sroberto /// 242258945Sroberto /// @param[in] uval 243258945Sroberto /// A uint8_t value. 244258945Sroberto /// 245258945Sroberto /// @return 246258945Sroberto /// A reference to this class so multiple things can be streamed 247258945Sroberto /// in one statement. 248258945Sroberto //------------------------------------------------------------------ 249258945Sroberto Stream& 250258945Sroberto operator<< (uint8_t uval); 251258945Sroberto 252258945Sroberto //------------------------------------------------------------------ 253258945Sroberto /// Output a uint16_t \a uval to the stream \a s. 254258945Sroberto /// 255258945Sroberto /// @param[in] uval 256258945Sroberto /// A uint16_t value. 257258945Sroberto /// 258258945Sroberto /// @return 259258945Sroberto /// A reference to this class so multiple things can be streamed 260258945Sroberto /// in one statement. 261258945Sroberto //------------------------------------------------------------------ 262258945Sroberto Stream& 263258945Sroberto operator<< (uint16_t uval); 264258945Sroberto 265258945Sroberto //------------------------------------------------------------------ 266258945Sroberto /// Output a uint32_t \a uval to the stream \a s. 267258945Sroberto /// 268258945Sroberto /// @param[in] uval 269258945Sroberto /// A uint32_t value. 270258945Sroberto /// 271258945Sroberto /// @return 272258945Sroberto /// A reference to this class so multiple things can be streamed 273258945Sroberto /// in one statement. 274258945Sroberto //------------------------------------------------------------------ 275258945Sroberto Stream& 276258945Sroberto operator<< (uint32_t uval); 277258945Sroberto 278258945Sroberto //------------------------------------------------------------------ 279258945Sroberto /// Output a uint64_t \a uval to the stream \a s. 280258945Sroberto /// 281258945Sroberto /// @param[in] uval 282258945Sroberto /// A uint64_t value. 283258945Sroberto /// 284258945Sroberto /// @return 285258945Sroberto /// A reference to this class so multiple things can be streamed 286258945Sroberto /// in one statement. 287258945Sroberto //------------------------------------------------------------------ 288258945Sroberto Stream& 289258945Sroberto operator<< (uint64_t uval); 290258945Sroberto 291258945Sroberto //------------------------------------------------------------------ 292258945Sroberto /// Output a int8_t \a sval to the stream \a s. 293258945Sroberto /// 294258945Sroberto /// @param[in] sval 295258945Sroberto /// A int8_t value. 296258945Sroberto /// 297258945Sroberto /// @return 298258945Sroberto /// A reference to this class so multiple things can be streamed 299258945Sroberto /// in one statement. 300258945Sroberto //------------------------------------------------------------------ 301258945Sroberto Stream& 302258945Sroberto operator<< (int8_t sval); 303258945Sroberto 304280849Scy //------------------------------------------------------------------ 305280849Scy /// Output a int16_t \a sval to the stream \a s. 306280849Scy /// 307280849Scy /// @param[in] sval 308280849Scy /// A int16_t value. 309280849Scy /// 310280849Scy /// @return 311280849Scy /// A reference to this class so multiple things can be streamed 312280849Scy /// in one statement. 313280849Scy //------------------------------------------------------------------ 314280849Scy Stream& 315258945Sroberto operator<< (int16_t sval); 316258945Sroberto 317258945Sroberto //------------------------------------------------------------------ 318258945Sroberto /// Output a int32_t \a sval to the stream \a s. 319258945Sroberto /// 320258945Sroberto /// @param[in] sval 321258945Sroberto /// A int32_t value. 322258945Sroberto /// 323258945Sroberto /// @return 324258945Sroberto /// A reference to this class so multiple things can be streamed 325258945Sroberto /// in one statement. 326258945Sroberto //------------------------------------------------------------------ 327258945Sroberto Stream& 328258945Sroberto operator<< (int32_t sval); 329258945Sroberto 330258945Sroberto //------------------------------------------------------------------ 331258945Sroberto /// Output a int64_t \a sval to the stream \a s. 332258945Sroberto /// 333258945Sroberto /// @param[in] sval 334258945Sroberto /// A int64_t value. 335258945Sroberto /// 336258945Sroberto /// @return 337258945Sroberto /// A reference to this class so multiple things can be streamed 338258945Sroberto /// in one statement. 339258945Sroberto //------------------------------------------------------------------ 340258945Sroberto Stream& 341258945Sroberto operator<< (int64_t sval); 342258945Sroberto 343258945Sroberto //------------------------------------------------------------------ 344258945Sroberto /// Output an address value to this stream. 345258945Sroberto /// 346258945Sroberto /// Put an address \a addr out to the stream with optional \a prefix 347258945Sroberto /// and \a suffix strings. 348258945Sroberto /// 349258945Sroberto /// @param[in] addr 350258945Sroberto /// An address value. 351258945Sroberto /// 352258945Sroberto /// @param[in] addr_size 353258945Sroberto /// Size in bytes of the address, used for formatting. 354258945Sroberto /// 355258945Sroberto /// @param[in] prefix 356258945Sroberto /// A prefix C string. If NULL, no prefix will be output. 357258945Sroberto /// 358258945Sroberto /// @param[in] suffix 359258945Sroberto /// A suffix C string. If NULL, no suffix will be output. 360258945Sroberto //------------------------------------------------------------------ 361258945Sroberto void 362258945Sroberto Address (uint64_t addr, uint32_t addr_size, const char *prefix = NULL, const char *suffix = NULL); 363258945Sroberto 364258945Sroberto //------------------------------------------------------------------ 365258945Sroberto /// Output an address range to this stream. 366258945Sroberto /// 367258945Sroberto /// Put an address range \a lo_addr - \a hi_addr out to the stream 368 /// with optional \a prefix and \a suffix strings. 369 /// 370 /// @param[in] lo_addr 371 /// The start address of the address range. 372 /// 373 /// @param[in] hi_addr 374 /// The end address of the address range. 375 /// 376 /// @param[in] addr_size 377 /// Size in bytes of the address, used for formatting. 378 /// 379 /// @param[in] prefix 380 /// A prefix C string. If NULL, no prefix will be output. 381 /// 382 /// @param[in] suffix 383 /// A suffix C string. If NULL, no suffix will be output. 384 //------------------------------------------------------------------ 385 void 386 AddressRange(uint64_t lo_addr, uint64_t hi_addr, uint32_t addr_size, const char *prefix = NULL, const char *suffix = NULL); 387 388 //------------------------------------------------------------------ 389 /// Output a C string to the stream. 390 /// 391 /// Print a C string \a cstr to the stream. 392 /// 393 /// @param[in] cstr 394 /// The string to be output to the stream. 395 //------------------------------------------------------------------ 396 size_t 397 PutCString (const char *cstr); 398 399 //------------------------------------------------------------------ 400 /// Output and End of Line character to the stream. 401 //------------------------------------------------------------------ 402 size_t 403 EOL(); 404 405 //------------------------------------------------------------------ 406 /// Get the address size in bytes. 407 /// 408 /// @return 409 /// The size of an address in bytes that is used when outputting 410 /// address and pointer values to the stream. 411 //------------------------------------------------------------------ 412 uint32_t 413 GetAddressByteSize () const; 414 415 //------------------------------------------------------------------ 416 /// Test if debug logging is enabled. 417 /// 418 /// @return 419 // \b true if the debug flag bit is set in this stream, \b 420 // false otherwise. 421 //------------------------------------------------------------------ 422 bool 423 GetDebug() const; 424 425 //------------------------------------------------------------------ 426 /// The flags accessor. 427 /// 428 /// @return 429 /// A reference to the Flags member variable. 430 //------------------------------------------------------------------ 431 Flags& 432 GetFlags(); 433 434 //------------------------------------------------------------------ 435 /// The flags const accessor. 436 /// 437 /// @return 438 /// A const reference to the Flags member variable. 439 //------------------------------------------------------------------ 440 const Flags& 441 GetFlags() const; 442 443 //------------------------------------------------------------------ 444 //// The byte order accessor. 445 //// 446 //// @return 447 //// The byte order. 448 //------------------------------------------------------------------ 449 lldb::ByteOrder 450 GetByteOrder() const; 451 452 //------------------------------------------------------------------ 453 /// Get the current indentation level. 454 /// 455 /// @return 456 /// The current indentation level as an integer. 457 //------------------------------------------------------------------ 458 int 459 GetIndentLevel () const; 460 461 //------------------------------------------------------------------ 462 /// Test if verbose logging is enabled. 463 /// 464 /// @return 465 // \b true if the verbose flag bit is set in this stream, \b 466 // false otherwise. 467 //------------------------------------------------------------------ 468 bool 469 GetVerbose() const; 470 471 //------------------------------------------------------------------ 472 /// Indent the current line in the stream. 473 /// 474 /// Indent the current line using the current indentation level and 475 /// print an optional string following the idenatation spaces. 476 /// 477 /// @param[in] s 478 /// A C string to print following the indentation. If NULL, just 479 /// output the indentation characters. 480 //------------------------------------------------------------------ 481 size_t 482 Indent(const char *s = NULL); 483 484 //------------------------------------------------------------------ 485 /// Decrement the current indentation level. 486 //------------------------------------------------------------------ 487 void 488 IndentLess (int amount = 2); 489 490 //------------------------------------------------------------------ 491 /// Increment the current indentation level. 492 //------------------------------------------------------------------ 493 void 494 IndentMore (int amount = 2); 495 496 //------------------------------------------------------------------ 497 /// Output an offset value. 498 /// 499 /// Put an offset \a uval out to the stream using the printf format 500 /// in \a format. 501 /// 502 /// @param[in] offset 503 /// The offset value. 504 /// 505 /// @param[in] format 506 /// The printf style format to use when outputting the offset. 507 //------------------------------------------------------------------ 508 void 509 Offset (uint32_t offset, const char *format = "0x%8.8x: "); 510 511 //------------------------------------------------------------------ 512 /// Output printf formatted output to the stream. 513 /// 514 /// Print some formatted output to the stream. 515 /// 516 /// @param[in] format 517 /// A printf style format string. 518 /// 519 /// @param[in] ... 520 /// Variable arguments that are needed for the printf style 521 /// format string \a format. 522 //------------------------------------------------------------------ 523 size_t 524 Printf (const char *format, ...) __attribute__ ((format (printf, 2, 3))); 525 526 size_t 527 PrintfVarArg(const char *format, va_list args); 528 529 //------------------------------------------------------------------ 530 /// Output a quoted C string value to the stream. 531 /// 532 /// Print a double quoted NULL terminated C string to the stream 533 /// using the printf format in \a format. 534 /// 535 /// @param[in] cstr 536 /// A NULL terminated C string value. 537 /// 538 /// @param[in] format 539 /// The optional C string format that can be overridden. 540 //------------------------------------------------------------------ 541 void 542 QuotedCString (const char *cstr, const char *format = "\"%s\""); 543 544 //------------------------------------------------------------------ 545 /// Set the address size in bytes. 546 /// 547 /// @param[in] addr_size 548 /// The new size in bytes of an address to use when outputting 549 /// address and pointer values. 550 //------------------------------------------------------------------ 551 void 552 SetAddressByteSize (uint32_t addr_size); 553 554 //------------------------------------------------------------------ 555 /// Set the current indentation level. 556 /// 557 /// @param[in] level 558 /// The new indentation level. 559 //------------------------------------------------------------------ 560 void 561 SetIndentLevel (int level); 562 563 //------------------------------------------------------------------ 564 /// Output a SLEB128 number to the stream. 565 /// 566 /// Put an SLEB128 \a uval out to the stream using the printf format 567 /// in \a format. 568 /// 569 /// @param[in] uval 570 /// A uint64_t value that was extracted as a SLEB128 value. 571 /// 572 /// @param[in] format 573 /// The optional printf format that can be overridden. 574 //------------------------------------------------------------------ 575 size_t 576 PutSLEB128 (int64_t uval); 577 578 //------------------------------------------------------------------ 579 /// Output a ULEB128 number to the stream. 580 /// 581 /// Put an ULEB128 \a uval out to the stream using the printf format 582 /// in \a format. 583 /// 584 /// @param[in] uval 585 /// A uint64_t value that was extracted as a ULEB128 value. 586 /// 587 /// @param[in] format 588 /// The optional printf format that can be overridden. 589 //------------------------------------------------------------------ 590 size_t 591 PutULEB128 (uint64_t uval); 592 593 static void 594 UnitTest(Stream *s); 595 596protected: 597 //------------------------------------------------------------------ 598 // Member variables 599 //------------------------------------------------------------------ 600 Flags m_flags; ///< Dump flags. 601 uint32_t m_addr_size; ///< Size of an address in bytes. 602 lldb::ByteOrder m_byte_order; ///< Byte order to use when encoding scalar types. 603 int m_indent_level; ///< Indention level. 604 605 size_t _PutHex8 (uint8_t uvalue, bool add_prefix); 606}; 607 608} // namespace lldb_private 609 610#endif // #if defined(__cplusplus) 611#endif // liblldb_Stream_h_ 612 613