Stream.h revision b352ebdfef166f3d0363fe9af50dcf063638d9f7
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, ...);
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,
186        lldb::ByteOrder dst_byte_order);
187
188    int
189    PutRawBytes (const void *s, size_t src_len,
190                 lldb::ByteOrder src_byte_order,
191                 lldb::ByteOrder dst_byte_order);
192
193    int
194    PutCStringAsRawHex8 (const char *s);
195
196    //------------------------------------------------------------------
197    /// Output a NULL terminated C string \a cstr to the stream \a s.
198    ///
199    /// @param[in] cstr
200    ///     A NULL terminated C string.
201    ///
202    /// @return
203    ///     A reference to this class so multiple things can be streamed
204    ///     in one statement.
205    //------------------------------------------------------------------
206    Stream&
207    operator<< (const char *cstr);
208
209    //------------------------------------------------------------------
210    /// Output a pointer value \a p to the stream \a s.
211    ///
212    /// @param[in] p
213    ///     A void pointer.
214    ///
215    /// @return
216    ///     A reference to this class so multiple things can be streamed
217    ///     in one statement.
218    //------------------------------------------------------------------
219    Stream&
220    operator<< (void *p);
221
222    //------------------------------------------------------------------
223    /// Output a character \a ch to the stream \a s.
224    ///
225    /// @param[in] ch
226    ///     A printable character value.
227    ///
228    /// @return
229    ///     A reference to this class so multiple things can be streamed
230    ///     in one statement.
231    //------------------------------------------------------------------
232    Stream&
233    operator<< (char ch);
234
235    //------------------------------------------------------------------
236    /// Output a uint8_t \a uval to the stream \a s.
237    ///
238    /// @param[in] uval
239    ///     A uint8_t value.
240    ///
241    /// @return
242    ///     A reference to this class so multiple things can be streamed
243    ///     in one statement.
244    //------------------------------------------------------------------
245    Stream&
246    operator<< (uint8_t uval);
247
248    //------------------------------------------------------------------
249    /// Output a uint16_t \a uval to the stream \a s.
250    ///
251    /// @param[in] uval
252    ///     A uint16_t value.
253    ///
254    /// @return
255    ///     A reference to this class so multiple things can be streamed
256    ///     in one statement.
257    //------------------------------------------------------------------
258    Stream&
259    operator<< (uint16_t uval);
260
261    //------------------------------------------------------------------
262    /// Output a uint32_t \a uval to the stream \a s.
263    ///
264    /// @param[in] uval
265    ///     A uint32_t value.
266    ///
267    /// @return
268    ///     A reference to this class so multiple things can be streamed
269    ///     in one statement.
270    //------------------------------------------------------------------
271    Stream&
272    operator<< (uint32_t uval);
273
274    //------------------------------------------------------------------
275    /// Output a uint64_t \a uval to the stream \a s.
276    ///
277    /// @param[in] uval
278    ///     A uint64_t value.
279    ///
280    /// @return
281    ///     A reference to this class so multiple things can be streamed
282    ///     in one statement.
283    //------------------------------------------------------------------
284    Stream&
285    operator<< (uint64_t uval);
286
287    //------------------------------------------------------------------
288    /// Output a int8_t \a sval to the stream \a s.
289    ///
290    /// @param[in] sval
291    ///     A int8_t value.
292    ///
293    /// @return
294    ///     A reference to this class so multiple things can be streamed
295    ///     in one statement.
296    //------------------------------------------------------------------
297    Stream&
298    operator<< (int8_t sval);
299
300    //------------------------------------------------------------------
301    /// Output a int16_t \a sval to the stream \a s.
302    ///
303    /// @param[in] sval
304    ///     A int16_t value.
305    ///
306    /// @return
307    ///     A reference to this class so multiple things can be streamed
308    ///     in one statement.
309    //------------------------------------------------------------------
310    Stream&
311    operator<< (int16_t sval);
312
313    //------------------------------------------------------------------
314    /// Output a int32_t \a sval to the stream \a s.
315    ///
316    /// @param[in] sval
317    ///     A int32_t value.
318    ///
319    /// @return
320    ///     A reference to this class so multiple things can be streamed
321    ///     in one statement.
322    //------------------------------------------------------------------
323    Stream&
324    operator<< (int32_t sval);
325
326    //------------------------------------------------------------------
327    /// Output a int64_t \a sval to the stream \a s.
328    ///
329    /// @param[in] sval
330    ///     A int64_t value.
331    ///
332    /// @return
333    ///     A reference to this class so multiple things can be streamed
334    ///     in one statement.
335    //------------------------------------------------------------------
336    Stream&
337    operator<< (int64_t sval);
338
339    //------------------------------------------------------------------
340    /// Output an address value to this stream.
341    ///
342    /// Put an address \a addr out to the stream with optional \a prefix
343    /// and \a suffix strings.
344    ///
345    /// @param[in] addr
346    ///     An address value.
347    ///
348    /// @param[in] addr_size
349    ///     Size in bytes of the address, used for formatting.
350    ///
351    /// @param[in] prefix
352    ///     A prefix C string. If NULL, no prefix will be output.
353    ///
354    /// @param[in] suffix
355    ///     A suffix C string. If NULL, no suffix will be output.
356    //------------------------------------------------------------------
357    void
358    Address (uint64_t addr, int addr_size, const char *prefix = NULL, const char *suffix = NULL);
359
360    //------------------------------------------------------------------
361    /// Output an address range to this stream.
362    ///
363    /// Put an address range \a lo_addr - \a hi_addr out to the stream
364    /// with optional \a prefix and \a suffix strings.
365    ///
366    /// @param[in] lo_addr
367    ///     The start address of the address range.
368    ///
369    /// @param[in] hi_addr
370    ///     The end address of the address range.
371    ///
372    /// @param[in] addr_size
373    ///     Size in bytes of the address, used for formatting.
374    ///
375    /// @param[in] prefix
376    ///     A prefix C string. If NULL, no prefix will be output.
377    ///
378    /// @param[in] suffix
379    ///     A suffix C string. If NULL, no suffix will be output.
380    //------------------------------------------------------------------
381    void
382    AddressRange(uint64_t lo_addr, uint64_t hi_addr, int addr_size, const char *prefix = NULL, const char *suffix = NULL);
383
384    //------------------------------------------------------------------
385    /// Output a C string to the stream with optional format.
386    ///
387    /// Print a C string \a cstr to the stream using the printf format
388    /// in \a format.
389    ///
390    /// @param[in] format
391    ///     The printf style format to use when outputting the C string.
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, ...);
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