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