José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 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é Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 30 | #include <iostream> |
José Fonseca | 89851d0 | 2010-11-28 12:16:52 +0000 | [diff] [blame] | 31 | #include <list> |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 32 | |
Zack Rusin | 712429a | 2011-08-25 23:22:30 -0400 | [diff] [blame] | 33 | #include "trace_file.hpp" |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 34 | #include "trace_format.hpp" |
| 35 | #include "trace_model.hpp" |
| 36 | |
| 37 | |
| 38 | namespace Trace { |
| 39 | |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 40 | class Parser |
| 41 | { |
| 42 | protected: |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 43 | File *file; |
José Fonseca | fa92214 | 2010-11-25 09:36:04 +0000 | [diff] [blame] | 44 | |
José Fonseca | 1982897 | 2010-11-29 20:34:32 +0000 | [diff] [blame] | 45 | typedef std::list<Call *> CallList; |
| 46 | CallList calls; |
José Fonseca | 3495713 | 2010-11-25 16:14:45 +0000 | [diff] [blame] | 47 | |
José Fonseca | 6a6d3e4 | 2011-09-11 14:14:21 +0100 | [diff] [blame] | 48 | // 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é Fonseca | 1982897 | 2010-11-29 20:34:32 +0000 | [diff] [blame] | 68 | FunctionMap functions; |
José Fonseca | 1982897 | 2010-11-29 20:34:32 +0000 | [diff] [blame] | 69 | StructMap structs; |
José Fonseca | 1982897 | 2010-11-29 20:34:32 +0000 | [diff] [blame] | 70 | EnumMap enums; |
José Fonseca | 1982897 | 2010-11-29 20:34:32 +0000 | [diff] [blame] | 71 | BitmaskMap bitmasks; |
José Fonseca | d35973c | 2010-11-26 14:14:45 +0000 | [diff] [blame] | 72 | |
Zack Rusin | ebf971e | 2011-09-06 17:44:43 -0400 | [diff] [blame] | 73 | bool m_supportsSeeking; |
| 74 | |
José Fonseca | 1982897 | 2010-11-29 20:34:32 +0000 | [diff] [blame] | 75 | unsigned next_call_no; |
José Fonseca | 3495713 | 2010-11-25 16:14:45 +0000 | [diff] [blame] | 76 | |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 77 | public: |
José Fonseca | 6117e31 | 2011-04-15 23:52:58 +0100 | [diff] [blame] | 78 | unsigned long long version; |
José Fonseca | 9922183 | 2011-03-22 22:15:46 +0000 | [diff] [blame] | 79 | |
José Fonseca | 57dbaf5 | 2011-04-10 14:45:43 +0100 | [diff] [blame] | 80 | Parser(); |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 81 | |
José Fonseca | 57dbaf5 | 2011-04-10 14:45:43 +0100 | [diff] [blame] | 82 | ~Parser(); |
José Fonseca | 6f51d3b | 2010-11-22 19:56:19 +0000 | [diff] [blame] | 83 | |
José Fonseca | 57dbaf5 | 2011-04-10 14:45:43 +0100 | [diff] [blame] | 84 | bool open(const char *filename); |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 85 | |
José Fonseca | 57dbaf5 | 2011-04-10 14:45:43 +0100 | [diff] [blame] | 86 | void close(void); |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 87 | |
José Fonseca | 57dbaf5 | 2011-04-10 14:45:43 +0100 | [diff] [blame] | 88 | Call *parse_call(void); |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 89 | |
Zack Rusin | 712429a | 2011-08-25 23:22:30 -0400 | [diff] [blame] | 90 | bool supportsOffsets() const |
| 91 | { |
Zack Rusin | 7c76775 | 2011-09-01 11:33:51 -0400 | [diff] [blame] | 92 | return file->supportsOffsets(); |
Zack Rusin | 712429a | 2011-08-25 23:22:30 -0400 | [diff] [blame] | 93 | } |
| 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 Rusin | 7c76775 | 2011-09-01 11:33:51 -0400 | [diff] [blame] | 105 | unsigned currentCallNumber() const |
| 106 | { |
| 107 | return next_call_no; |
| 108 | } |
| 109 | |
| 110 | void setCurrentCallNumber(unsigned num) |
| 111 | { |
| 112 | next_call_no = num; |
| 113 | } |
Zack Rusin | e0df952 | 2011-09-01 01:50:56 -0400 | [diff] [blame] | 114 | |
Zack Rusin | 2b1bd4f | 2011-09-04 16:14:22 -0400 | [diff] [blame] | 115 | int percentRead() |
| 116 | { |
| 117 | return file->percentRead(); |
| 118 | } |
| 119 | |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 120 | Call *scan_call(); |
| 121 | |
José Fonseca | 57dbaf5 | 2011-04-10 14:45:43 +0100 | [diff] [blame] | 122 | protected: |
José Fonseca | 6a6d3e4 | 2011-09-11 14:14:21 +0100 | [diff] [blame] | 123 | FunctionSig *parse_function_sig(void); |
| 124 | StructSig *parse_struct_sig(); |
| 125 | EnumSig *parse_enum_sig(); |
| 126 | BitmaskSig *parse_bitmask_sig(); |
| 127 | |
José Fonseca | 57dbaf5 | 2011-04-10 14:45:43 +0100 | [diff] [blame] | 128 | void parse_enter(void); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 129 | void scan_enter(void); |
| 130 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 131 | Call *parse_leave(void); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 132 | Call *scan_leave(void); |
| 133 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 134 | bool parse_call_details(Call *call); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 135 | bool scan_call_details(Call *call); |
| 136 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 137 | void parse_arg(Call *call); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 138 | void scan_arg(Call *call); |
| 139 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 140 | Value *parse_value(void); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 141 | void scan_value(void); |
| 142 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 143 | Value *parse_sint(); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 144 | void scan_sint(); |
| 145 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 146 | Value *parse_uint(); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 147 | void scan_uint(); |
| 148 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 149 | Value *parse_float(); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 150 | void scan_float(); |
| 151 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 152 | Value *parse_double(); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 153 | void scan_double(); |
| 154 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 155 | Value *parse_string(); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 156 | void scan_string(); |
| 157 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 158 | Value *parse_enum(); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 159 | void scan_enum(); |
| 160 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 161 | Value *parse_bitmask(); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 162 | void scan_bitmask(); |
| 163 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 164 | Value *parse_array(void); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 165 | void scan_array(void); |
| 166 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 167 | Value *parse_blob(void); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 168 | void scan_blob(void); |
| 169 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 170 | Value *parse_struct(); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 171 | void scan_struct(); |
| 172 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 173 | Value *parse_opaque(); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 174 | void scan_opaque(); |
| 175 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 176 | const char * read_string(void); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 177 | void skip_string(void); |
| 178 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 179 | unsigned long long read_uint(void); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 180 | void skip_uint(void); |
| 181 | |
José Fonseca | 838decf | 2011-09-11 14:44:41 +0100 | [diff] [blame^] | 182 | inline int read_byte(void); |
Zack Rusin | 46c4a32 | 2011-09-02 01:08:49 -0400 | [diff] [blame] | 183 | inline void skip_byte(void); |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | |
José Fonseca | 7e32902 | 2010-11-19 17:05:18 +0000 | [diff] [blame] | 187 | } /* namespace Trace */ |
| 188 | |
| 189 | #endif /* _TRACE_PARSER_HPP_ */ |