1 /**************************************************************************
2  *
3  * Copyright 2007-2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25 
26 /*
27  * Trace writing functions.
28  */
29 
30 #pragma once
31 
32 
33 #include <stddef.h>
34 
35 #include <vector>
36 
37 #include "trace_model.hpp"
38 
39 namespace trace {
40     class OutStream;
41 
42     class Writer {
43     protected:
44         OutStream *m_file;
45         unsigned call_no;
46 
47         std::vector<bool> functions;
48         std::vector<bool> structs;
49         std::vector<bool> enums;
50         std::vector<bool> bitmasks;
51         std::vector<bool> frames;
52 
53     public:
54         Writer();
55         ~Writer();
56 
57         bool open(const char *filename,
58                   unsigned semanticVersion,
59                   const Properties &properties);
60         void close(void);
61 
62         unsigned beginEnter(const FunctionSig *sig, unsigned thread_id);
63         void endEnter(void);
64 
65         void beginLeave(unsigned call);
66         void endLeave(void);
67 
68         void beginArg(unsigned index);
endArg(void)69         inline void endArg(void) {}
70 
71         void beginReturn(void);
endReturn(void)72         inline void endReturn(void) {}
73 
74         void beginBacktrace(unsigned num_frames);
75         void writeStackFrame(const RawStackFrame *frame);
endBacktrace(void)76         inline void endBacktrace(void) {}
77 
78         void writeFlags(unsigned flags);
79 
80         void beginArray(size_t length);
endArray(void)81         inline void endArray(void) {}
82 
beginElement(void)83         inline void beginElement(void) {}
endElement(void)84         inline void endElement(void) {}
85 
86         void beginStruct(const StructSig *sig);
endStruct(void)87         inline void endStruct(void) {}
88 
89         void beginRepr(void);
endRepr(void)90         inline void endRepr(void) {}
91 
92         void writeBool(bool value);
93         void writeSInt(signed long long value);
94         void writeUInt(unsigned long long value);
95         void writeFloat(float value);
96         void writeDouble(double value);
97         void writeString(const char *str);
98         void writeString(const char *str, size_t size);
99         void writeWString(const wchar_t *str);
100         void writeWString(const wchar_t *str, size_t size);
101         void writeBlob(const void *data, size_t size);
102         void writeEnum(const EnumSig *sig, signed long long value);
103         void writeBitmask(const BitmaskSig *sig, unsigned long long value);
104         void writeNull(void);
105         void writePointer(unsigned long long addr);
106 
107         void writeCall(Call *call);
108 
109     private:
beginProperties(void)110         inline void beginProperties(void) {}
111         void writeProperty(const char *name, const char *value);
112         void endProperties(void);
113 
114     protected:
115         void inline _write(const void *sBuffer, size_t dwBytesToWrite);
116         void inline _writeByte(char c);
117         void inline _writeUInt(unsigned long long value);
118         void inline _writeFloat(float value);
119         void inline _writeDouble(double value);
120         void inline _writeString(const char *str);
121 
122     };
123 
124 } /* namespace trace */
125 
126