Stream.h revision 4691fc522359004738cde0646af96e0a39181b6b
1//===-- Stream.h ------------------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef liblldb_Stream_h_ 11#define liblldb_Stream_h_ 12#if defined(__cplusplus) 13 14#include "lldb/lldb-private.h" 15#include "lldb/Core/Flags.h" 16#include <stdarg.h> 17 18namespace lldb_private { 19 20//---------------------------------------------------------------------- 21/// @class Stream Stream.h "lldb/Core/Stream.h" 22/// @brief A stream class that can stream formatted output to a file. 23//---------------------------------------------------------------------- 24class Stream 25{ 26public: 27 //------------------------------------------------------------------ 28 /// \a m_flags bit values. 29 //------------------------------------------------------------------ 30 enum 31 { 32 eVerbose = (1 << 0), ///< If set, verbose logging is enabled 33 eDebug = (1 << 1), ///< If set, debug logging is enabled 34 eAddPrefix = (1 << 2), ///< Add number prefixes for binary, octal and hex when eBinary is clear 35 eBinary = (1 << 3) ///< Get and put data as binary instead of as the default string mode. 36 }; 37 38 //------------------------------------------------------------------ 39 /// Construct with flags and address size and byte order. 40 /// 41 /// Construct with dump flags \a flags and the default address 42 /// size. \a flags can be any of the above enumeration logical OR'ed 43 /// together. 44 //------------------------------------------------------------------ 45 Stream (uint32_t flags, 46 uint32_t addr_size, 47 lldb::ByteOrder byte_order); 48 49 //------------------------------------------------------------------ 50 /// Construct a default Stream, not binary, host byte order and 51 /// host addr size. 52 /// 53 //------------------------------------------------------------------ 54 Stream (); 55 56 //------------------------------------------------------------------ 57 /// Destructor 58 //------------------------------------------------------------------ 59 virtual 60 ~Stream (); 61 62 //------------------------------------------------------------------ 63 // Subclasses must override these methods 64 //------------------------------------------------------------------ 65 66 //------------------------------------------------------------------ 67 /// Flush the stream. 68 /// 69 /// Subclasses should flush the stream to make any output appear 70 /// if the stream has any buffering. 71 //------------------------------------------------------------------ 72 virtual void 73 Flush () = 0; 74 75 //------------------------------------------------------------------ 76 /// Output character bytes to the stream. 77 /// 78 /// Appends \a src_len characters from the buffer \a src to the 79 /// stream. 80 /// 81 /// @param[in] src 82 /// A buffer containing at least \a src_len bytes of data. 83 /// 84 /// @param[in] src_len 85 /// A number of bytes to append to the stream. 86 /// 87 /// @return 88 /// The number of bytes that were appended to the stream. 89 //------------------------------------------------------------------ 90 virtual int 91 Write (const void *src, size_t src_len) = 0; 92 93 //------------------------------------------------------------------ 94 // Member functions 95 //------------------------------------------------------------------ 96 int 97 PutChar (char ch); 98 99 //------------------------------------------------------------------ 100 /// Set the byte_order value. 101 /// 102 /// Sets the byte order of the data to extract. Extracted values 103 /// will be swapped if necessary when decoding. 104 /// 105 /// @param[in] byte_order 106 /// The byte order value to use when extracting data. 107 /// 108 /// @return 109 /// The old byte order value. 110 //------------------------------------------------------------------ 111 lldb::ByteOrder 112 SetByteOrder (lldb::ByteOrder byte_order); 113 114 //------------------------------------------------------------------ 115 /// Format a C string from a printf style format and variable 116 /// arguments and encode and append the resulting C string as hex 117 /// bytes. 118 /// 119 /// @param[in] format 120 /// A printf style format string. 121 /// 122 /// @param[in] ... 123 /// Any additional arguments needed for the printf format string. 124 /// 125 /// @return 126 /// The number of bytes that were appended to the stream. 127 //------------------------------------------------------------------ 128 int 129 PrintfAsRawHex8 (const char *format, ...) __attribute__ ((format (printf, 2, 3))); 130 131 //------------------------------------------------------------------ 132 /// Format a C string from a printf style format and variable 133 /// arguments and encode and append the resulting C string as hex 134 /// bytes. 135 /// 136 /// @param[in] format 137 /// A printf style format string. 138 /// 139 /// @param[in] ... 140 /// Any additional arguments needed for the printf format string. 141 /// 142 /// @return 143 /// The number of bytes that were appended to the stream. 144 //------------------------------------------------------------------ 145 int 146 PutHex8 (uint8_t uvalue); 147 148 int 149 PutNHex8 (size_t n, uint8_t uvalue); 150 151 int 152 PutHex16 (uint16_t uvalue, 153 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid); 154 155 int 156 PutHex32 (uint32_t uvalue, 157 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid); 158 159 int 160 PutHex64 (uint64_t uvalue, 161 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid); 162 163 int 164 PutMaxHex64 (uint64_t uvalue, 165 size_t byte_size, 166 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid); 167 int 168 PutFloat (float f, 169 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid); 170 171 int 172 PutDouble (double d, 173 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid); 174 175 int 176 PutLongDouble (long double ld, 177 lldb::ByteOrder byte_order = lldb::eByteOrderInvalid); 178 179 int 180 PutPointer (void *ptr); 181 182 int 183 PutBytesAsRawHex8 (const void *src, 184 size_t src_len, 185 lldb::ByteOrder src_byte_order = lldb::eByteOrderInvalid, 186 lldb::ByteOrder dst_byte_order = lldb::eByteOrderInvalid); 187 188 int 189 PutRawBytes (const void *s, 190 size_t src_len, 191 lldb::ByteOrder src_byte_order = lldb::eByteOrderInvalid, 192 lldb::ByteOrder dst_byte_order = lldb::eByteOrderInvalid); 193 194 int 195 PutCStringAsRawHex8 (const char *s); 196 197 //------------------------------------------------------------------ 198 /// Output a NULL terminated C string \a cstr to the stream \a s. 199 /// 200 /// @param[in] cstr 201 /// A NULL terminated C string. 202 /// 203 /// @return 204 /// A reference to this class so multiple things can be streamed 205 /// in one statement. 206 //------------------------------------------------------------------ 207 Stream& 208 operator<< (const char *cstr); 209 210 //------------------------------------------------------------------ 211 /// Output a pointer value \a p to the stream \a s. 212 /// 213 /// @param[in] p 214 /// A void pointer. 215 /// 216 /// @return 217 /// A reference to this class so multiple things can be streamed 218 /// in one statement. 219 //------------------------------------------------------------------ 220 Stream& 221 operator<< (void *p); 222 223 //------------------------------------------------------------------ 224 /// Output a character \a ch to the stream \a s. 225 /// 226 /// @param[in] ch 227 /// A printable character value. 228 /// 229 /// @return 230 /// A reference to this class so multiple things can be streamed 231 /// in one statement. 232 //------------------------------------------------------------------ 233 Stream& 234 operator<< (char ch); 235 236 //------------------------------------------------------------------ 237 /// Output a uint8_t \a uval to the stream \a s. 238 /// 239 /// @param[in] uval 240 /// A uint8_t value. 241 /// 242 /// @return 243 /// A reference to this class so multiple things can be streamed 244 /// in one statement. 245 //------------------------------------------------------------------ 246 Stream& 247 operator<< (uint8_t uval); 248 249 //------------------------------------------------------------------ 250 /// Output a uint16_t \a uval to the stream \a s. 251 /// 252 /// @param[in] uval 253 /// A uint16_t value. 254 /// 255 /// @return 256 /// A reference to this class so multiple things can be streamed 257 /// in one statement. 258 //------------------------------------------------------------------ 259 Stream& 260 operator<< (uint16_t uval); 261 262 //------------------------------------------------------------------ 263 /// Output a uint32_t \a uval to the stream \a s. 264 /// 265 /// @param[in] uval 266 /// A uint32_t value. 267 /// 268 /// @return 269 /// A reference to this class so multiple things can be streamed 270 /// in one statement. 271 //------------------------------------------------------------------ 272 Stream& 273 operator<< (uint32_t uval); 274 275 //------------------------------------------------------------------ 276 /// Output a uint64_t \a uval to the stream \a s. 277 /// 278 /// @param[in] uval 279 /// A uint64_t value. 280 /// 281 /// @return 282 /// A reference to this class so multiple things can be streamed 283 /// in one statement. 284 //------------------------------------------------------------------ 285 Stream& 286 operator<< (uint64_t uval); 287 288 //------------------------------------------------------------------ 289 /// Output a int8_t \a sval to the stream \a s. 290 /// 291 /// @param[in] sval 292 /// A int8_t value. 293 /// 294 /// @return 295 /// A reference to this class so multiple things can be streamed 296 /// in one statement. 297 //------------------------------------------------------------------ 298 Stream& 299 operator<< (int8_t sval); 300 301 //------------------------------------------------------------------ 302 /// Output a int16_t \a sval to the stream \a s. 303 /// 304 /// @param[in] sval 305 /// A int16_t value. 306 /// 307 /// @return 308 /// A reference to this class so multiple things can be streamed 309 /// in one statement. 310 //------------------------------------------------------------------ 311 Stream& 312 operator<< (int16_t sval); 313 314 //------------------------------------------------------------------ 315 /// Output a int32_t \a sval to the stream \a s. 316 /// 317 /// @param[in] sval 318 /// A int32_t value. 319 /// 320 /// @return 321 /// A reference to this class so multiple things can be streamed 322 /// in one statement. 323 //------------------------------------------------------------------ 324 Stream& 325 operator<< (int32_t sval); 326 327 //------------------------------------------------------------------ 328 /// Output a int64_t \a sval to the stream \a s. 329 /// 330 /// @param[in] sval 331 /// A int64_t value. 332 /// 333 /// @return 334 /// A reference to this class so multiple things can be streamed 335 /// in one statement. 336 //------------------------------------------------------------------ 337 Stream& 338 operator<< (int64_t sval); 339 340 //------------------------------------------------------------------ 341 /// Output an address value to this stream. 342 /// 343 /// Put an address \a addr out to the stream with optional \a prefix 344 /// and \a suffix strings. 345 /// 346 /// @param[in] addr 347 /// An address value. 348 /// 349 /// @param[in] addr_size 350 /// Size in bytes of the address, used for formatting. 351 /// 352 /// @param[in] prefix 353 /// A prefix C string. If NULL, no prefix will be output. 354 /// 355 /// @param[in] suffix 356 /// A suffix C string. If NULL, no suffix will be output. 357 //------------------------------------------------------------------ 358 void 359 Address (uint64_t addr, int addr_size, const char *prefix = NULL, const char *suffix = NULL); 360 361 //------------------------------------------------------------------ 362 /// Output an address range to this stream. 363 /// 364 /// Put an address range \a lo_addr - \a hi_addr out to the stream 365 /// with optional \a prefix and \a suffix strings. 366 /// 367 /// @param[in] lo_addr 368 /// The start address of the address range. 369 /// 370 /// @param[in] hi_addr 371 /// The end address of the address range. 372 /// 373 /// @param[in] addr_size 374 /// Size in bytes of the address, used for formatting. 375 /// 376 /// @param[in] prefix 377 /// A prefix C string. If NULL, no prefix will be output. 378 /// 379 /// @param[in] suffix 380 /// A suffix C string. If NULL, no suffix will be output. 381 //------------------------------------------------------------------ 382 void 383 AddressRange(uint64_t lo_addr, uint64_t hi_addr, int addr_size, const char *prefix = NULL, const char *suffix = NULL); 384 385 //------------------------------------------------------------------ 386 /// Output a C string to the stream. 387 /// 388 /// Print a C string \a cstr to the stream. 389 /// 390 /// @param[in] cstr 391 /// The string to be output to the stream. 392 //------------------------------------------------------------------ 393 int 394 PutCString (const char *cstr); 395 396 //------------------------------------------------------------------ 397 /// Output and End of Line character to the stream. 398 //------------------------------------------------------------------ 399 int 400 EOL(); 401 402 //------------------------------------------------------------------ 403 /// Get the address size in bytes. 404 /// 405 /// @return 406 /// The size of an address in bytes that is used when outputting 407 /// address and pointer values to the stream. 408 //------------------------------------------------------------------ 409 uint8_t 410 GetAddressByteSize () const; 411 412 //------------------------------------------------------------------ 413 /// Test if debug logging is enabled. 414 /// 415 /// @return 416 // \b true if the debug flag bit is set in this stream, \b 417 // false otherwise. 418 //------------------------------------------------------------------ 419 bool 420 GetDebug() const; 421 422 //------------------------------------------------------------------ 423 /// The flags accessor. 424 /// 425 /// @return 426 /// A reference to the Flags member variable. 427 //------------------------------------------------------------------ 428 Flags& 429 GetFlags(); 430 431 //------------------------------------------------------------------ 432 /// The flags const accessor. 433 /// 434 /// @return 435 /// A const reference to the Flags member variable. 436 //------------------------------------------------------------------ 437 const Flags& 438 GetFlags() const; 439 440 //------------------------------------------------------------------ 441 //// The byte order accessor. 442 //// 443 //// @return 444 //// The byte order. 445 //------------------------------------------------------------------ 446 lldb::ByteOrder 447 GetByteOrder() const; 448 449 //------------------------------------------------------------------ 450 /// Get the current indentation level. 451 /// 452 /// @return 453 /// The current indentation level as an integer. 454 //------------------------------------------------------------------ 455 int 456 GetIndentLevel () const; 457 458 //------------------------------------------------------------------ 459 /// Test if verbose logging is enabled. 460 /// 461 /// @return 462 // \b true if the verbose flag bit is set in this stream, \b 463 // false otherwise. 464 //------------------------------------------------------------------ 465 bool 466 GetVerbose() const; 467 468 //------------------------------------------------------------------ 469 /// Indent the current line in the stream. 470 /// 471 /// Indent the current line using the current indentation level and 472 /// print an optional string following the idenatation spaces. 473 /// 474 /// @param[in] s 475 /// A C string to print following the indentation. If NULL, just 476 /// output the indentation characters. 477 //------------------------------------------------------------------ 478 int 479 Indent(const char *s = NULL); 480 481 //------------------------------------------------------------------ 482 /// Decrement the current indentation level. 483 //------------------------------------------------------------------ 484 void 485 IndentLess (int amount = 2); 486 487 //------------------------------------------------------------------ 488 /// Increment the current indentation level. 489 //------------------------------------------------------------------ 490 void 491 IndentMore (int amount = 2); 492 493 //------------------------------------------------------------------ 494 /// Output an offset value. 495 /// 496 /// Put an offset \a uval out to the stream using the printf format 497 /// in \a format. 498 /// 499 /// @param[in] offset 500 /// The offset value. 501 /// 502 /// @param[in] format 503 /// The printf style format to use when outputting the offset. 504 //------------------------------------------------------------------ 505 void 506 Offset (uint32_t offset, const char *format = "0x%8.8x: "); 507 508 //------------------------------------------------------------------ 509 /// Output printf formatted output to the stream. 510 /// 511 /// Print some formatted output to the stream. 512 /// 513 /// @param[in] format 514 /// A printf style format string. 515 /// 516 /// @param[in] ... 517 /// Variable arguments that are needed for the printf style 518 /// format string \a format. 519 //------------------------------------------------------------------ 520 int 521 Printf (const char *format, ...) __attribute__ ((format (printf, 2, 3))); 522 523 int 524 PrintfVarArg(const char *format, va_list args); 525 526 //------------------------------------------------------------------ 527 /// Output a quoted C string value to the stream. 528 /// 529 /// Print a double quoted NULL terminated C string to the stream 530 /// using the printf format in \a format. 531 /// 532 /// @param[in] cstr 533 /// A NULL terminated C string value. 534 /// 535 /// @param[in] format 536 /// The optional C string format that can be overridden. 537 //------------------------------------------------------------------ 538 void 539 QuotedCString (const char *cstr, const char *format = "\"%s\""); 540 541 //------------------------------------------------------------------ 542 /// Set the address size in bytes. 543 /// 544 /// @param[in] addr_size 545 /// The new size in bytes of an address to use when outputting 546 /// address and pointer values. 547 //------------------------------------------------------------------ 548 void 549 SetAddressByteSize (uint8_t addr_size); 550 551 //------------------------------------------------------------------ 552 /// Set the current indentation level. 553 /// 554 /// @param[in] level 555 /// The new indentation level. 556 //------------------------------------------------------------------ 557 void 558 SetIndentLevel (int level); 559 560 //------------------------------------------------------------------ 561 /// Output a SLEB128 number to the stream. 562 /// 563 /// Put an SLEB128 \a uval out to the stream using the printf format 564 /// in \a format. 565 /// 566 /// @param[in] uval 567 /// A uint64_t value that was extracted as a SLEB128 value. 568 /// 569 /// @param[in] format 570 /// The optional printf format that can be overridden. 571 //------------------------------------------------------------------ 572 int 573 PutSLEB128 (int64_t uval); 574 575 //------------------------------------------------------------------ 576 /// Output a ULEB128 number to the stream. 577 /// 578 /// Put an ULEB128 \a uval out to the stream using the printf format 579 /// in \a format. 580 /// 581 /// @param[in] uval 582 /// A uint64_t value that was extracted as a ULEB128 value. 583 /// 584 /// @param[in] format 585 /// The optional printf format that can be overridden. 586 //------------------------------------------------------------------ 587 int 588 PutULEB128 (uint64_t uval); 589 590 static void 591 UnitTest(Stream *s); 592 593private: 594 //------------------------------------------------------------------ 595 // Member variables 596 //------------------------------------------------------------------ 597 Flags m_flags; ///< Dump flags. 598 uint8_t m_addr_size; ///< Size of an address in bytes. 599 lldb::ByteOrder m_byte_order;///< Byte order to use when encoding scalar types. 600 int m_indent_level; ///< Indention level. 601 602 int _PutHex8 (uint8_t uvalue, bool add_prefix); 603}; 604 605} // namespace lldb_private 606 607#endif // #if defined(__cplusplus) 608#endif // liblldb_Stream_h_ 609 610