blob: 962c323d4d9ef379e8a0c074bf3962fcd813fc9d [file] [log] [blame]
José Fonseca7e329022010-11-19 17:05:18 +00001/**************************************************************************
2 *
3 * Copyright 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#ifndef _TRACE_PARSER_HPP_
27#define _TRACE_PARSER_HPP_
28
29
José Fonseca7e329022010-11-19 17:05:18 +000030#include <iostream>
José Fonseca89851d02010-11-28 12:16:52 +000031#include <list>
José Fonseca7e329022010-11-19 17:05:18 +000032
Zack Rusin712429a2011-08-25 23:22:30 -040033#include "trace_file.hpp"
José Fonseca7e329022010-11-19 17:05:18 +000034#include "trace_format.hpp"
35#include "trace_model.hpp"
36
37
38namespace Trace {
39
José Fonseca7e329022010-11-19 17:05:18 +000040class Parser
41{
42protected:
Zack Rusin5ce45e72011-08-05 13:43:46 -040043 File *file;
José Fonsecafa922142010-11-25 09:36:04 +000044
José Fonseca19828972010-11-29 20:34:32 +000045 typedef std::list<Call *> CallList;
46 CallList calls;
José Fonseca34957132010-11-25 16:14:45 +000047
José Fonseca6a6d3e42011-09-11 14:14:21 +010048 // Helper template that extends a base signature structure, with additional
49 // parsing information.
50 template< class T >
51 struct SigState : public T {
52 // Offset in the file of where signature was defined. It is used when
53 // reparsing to determine whether the signature definition is to be
54 // expected next or not.
55 File::Offset offset;
56 };
57
58 typedef SigState<FunctionSig> FunctionSigState;
59 typedef SigState<StructSig> StructSigState;
60 typedef SigState<EnumSig> EnumSigState;
61 typedef SigState<BitmaskSig> BitmaskSigState;
62
63 typedef std::vector<FunctionSigState *> FunctionMap;
64 typedef std::vector<StructSigState *> StructMap;
65 typedef std::vector<EnumSigState *> EnumMap;
66 typedef std::vector<BitmaskSigState *> BitmaskMap;
67
José Fonseca19828972010-11-29 20:34:32 +000068 FunctionMap functions;
José Fonseca19828972010-11-29 20:34:32 +000069 StructMap structs;
José Fonseca19828972010-11-29 20:34:32 +000070 EnumMap enums;
José Fonseca19828972010-11-29 20:34:32 +000071 BitmaskMap bitmasks;
José Fonsecad35973c2010-11-26 14:14:45 +000072
Zack Rusinebf971e2011-09-06 17:44:43 -040073 bool m_supportsSeeking;
74
José Fonseca19828972010-11-29 20:34:32 +000075 unsigned next_call_no;
José Fonseca34957132010-11-25 16:14:45 +000076
José Fonseca7e329022010-11-19 17:05:18 +000077public:
José Fonseca6117e312011-04-15 23:52:58 +010078 unsigned long long version;
José Fonseca99221832011-03-22 22:15:46 +000079
José Fonseca57dbaf52011-04-10 14:45:43 +010080 Parser();
José Fonseca7e329022010-11-19 17:05:18 +000081
José Fonseca57dbaf52011-04-10 14:45:43 +010082 ~Parser();
José Fonseca6f51d3b2010-11-22 19:56:19 +000083
José Fonseca57dbaf52011-04-10 14:45:43 +010084 bool open(const char *filename);
José Fonseca7e329022010-11-19 17:05:18 +000085
José Fonseca57dbaf52011-04-10 14:45:43 +010086 void close(void);
José Fonseca7e329022010-11-19 17:05:18 +000087
José Fonseca57dbaf52011-04-10 14:45:43 +010088 Call *parse_call(void);
José Fonseca7e329022010-11-19 17:05:18 +000089
Zack Rusin712429a2011-08-25 23:22:30 -040090 bool supportsOffsets() const
91 {
Zack Rusin7c767752011-09-01 11:33:51 -040092 return file->supportsOffsets();
Zack Rusin712429a2011-08-25 23:22:30 -040093 }
94
95 File::Offset currentOffset()
96 {
97 return file->currentOffset();
98 }
99
100 void setCurrentOffset(const File::Offset &offset)
101 {
102 file->setCurrentOffset(offset);
103 }
104
Zack Rusin7c767752011-09-01 11:33:51 -0400105 unsigned currentCallNumber() const
106 {
107 return next_call_no;
108 }
109
110 void setCurrentCallNumber(unsigned num)
111 {
112 next_call_no = num;
113 }
Zack Rusine0df9522011-09-01 01:50:56 -0400114
Zack Rusin2b1bd4f2011-09-04 16:14:22 -0400115 int percentRead()
116 {
117 return file->percentRead();
118 }
119
Zack Rusin46c4a322011-09-02 01:08:49 -0400120 Call *scan_call();
121
José Fonseca57dbaf52011-04-10 14:45:43 +0100122protected:
José Fonseca6a6d3e42011-09-11 14:14:21 +0100123 FunctionSig *parse_function_sig(void);
124 StructSig *parse_struct_sig();
125 EnumSig *parse_enum_sig();
126 BitmaskSig *parse_bitmask_sig();
127
José Fonseca57dbaf52011-04-10 14:45:43 +0100128 void parse_enter(void);
Zack Rusin46c4a322011-09-02 01:08:49 -0400129 void scan_enter(void);
130
José Fonseca838decf2011-09-11 14:44:41 +0100131 Call *parse_leave(void);
Zack Rusin46c4a322011-09-02 01:08:49 -0400132 Call *scan_leave(void);
133
José Fonseca838decf2011-09-11 14:44:41 +0100134 bool parse_call_details(Call *call);
Zack Rusin46c4a322011-09-02 01:08:49 -0400135 bool scan_call_details(Call *call);
136
José Fonseca838decf2011-09-11 14:44:41 +0100137 void parse_arg(Call *call);
Zack Rusin46c4a322011-09-02 01:08:49 -0400138 void scan_arg(Call *call);
139
José Fonseca838decf2011-09-11 14:44:41 +0100140 Value *parse_value(void);
Zack Rusin46c4a322011-09-02 01:08:49 -0400141 void scan_value(void);
142
José Fonseca838decf2011-09-11 14:44:41 +0100143 Value *parse_sint();
Zack Rusin46c4a322011-09-02 01:08:49 -0400144 void scan_sint();
145
José Fonseca838decf2011-09-11 14:44:41 +0100146 Value *parse_uint();
Zack Rusin46c4a322011-09-02 01:08:49 -0400147 void scan_uint();
148
José Fonseca838decf2011-09-11 14:44:41 +0100149 Value *parse_float();
Zack Rusin46c4a322011-09-02 01:08:49 -0400150 void scan_float();
151
José Fonseca838decf2011-09-11 14:44:41 +0100152 Value *parse_double();
Zack Rusin46c4a322011-09-02 01:08:49 -0400153 void scan_double();
154
José Fonseca838decf2011-09-11 14:44:41 +0100155 Value *parse_string();
Zack Rusin46c4a322011-09-02 01:08:49 -0400156 void scan_string();
157
José Fonseca838decf2011-09-11 14:44:41 +0100158 Value *parse_enum();
Zack Rusin46c4a322011-09-02 01:08:49 -0400159 void scan_enum();
160
José Fonseca838decf2011-09-11 14:44:41 +0100161 Value *parse_bitmask();
Zack Rusin46c4a322011-09-02 01:08:49 -0400162 void scan_bitmask();
163
José Fonseca838decf2011-09-11 14:44:41 +0100164 Value *parse_array(void);
Zack Rusin46c4a322011-09-02 01:08:49 -0400165 void scan_array(void);
166
José Fonseca838decf2011-09-11 14:44:41 +0100167 Value *parse_blob(void);
Zack Rusin46c4a322011-09-02 01:08:49 -0400168 void scan_blob(void);
169
José Fonseca838decf2011-09-11 14:44:41 +0100170 Value *parse_struct();
Zack Rusin46c4a322011-09-02 01:08:49 -0400171 void scan_struct();
172
José Fonseca838decf2011-09-11 14:44:41 +0100173 Value *parse_opaque();
Zack Rusin46c4a322011-09-02 01:08:49 -0400174 void scan_opaque();
175
José Fonseca838decf2011-09-11 14:44:41 +0100176 const char * read_string(void);
Zack Rusin46c4a322011-09-02 01:08:49 -0400177 void skip_string(void);
178
José Fonseca838decf2011-09-11 14:44:41 +0100179 unsigned long long read_uint(void);
Zack Rusin46c4a322011-09-02 01:08:49 -0400180 void skip_uint(void);
181
José Fonseca838decf2011-09-11 14:44:41 +0100182 inline int read_byte(void);
Zack Rusin46c4a322011-09-02 01:08:49 -0400183 inline void skip_byte(void);
José Fonseca7e329022010-11-19 17:05:18 +0000184};
185
186
José Fonseca7e329022010-11-19 17:05:18 +0000187} /* namespace Trace */
188
189#endif /* _TRACE_PARSER_HPP_ */