]> git.cworth.org Git - apitrace/blob - retrace.hpp
Implement getTime for OS X
[apitrace] / retrace.hpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
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 _RETRACE_HPP_
27 #define _RETRACE_HPP_
28
29 #include <string.h>
30
31 #include <list>
32 #include <map>
33 #include <ostream>
34
35 #include "trace_model.hpp"
36
37
38 namespace retrace {
39
40
41 /**
42  * Handle map.
43  *
44  * It is just like a regular std::map<T, T> container, but lookups of missing
45  * keys return the key instead of default constructor.
46  *
47  * This is necessary for several GL named objects, where one can either request
48  * the implementation to generate an unique name, or pick a value never used
49  * before.
50  *
51  * XXX: In some cases, instead of returning the key, it would make more sense
52  * to return an unused data value (e.g., container count).
53  */
54 template <class T>
55 class map
56 {
57 private:
58     typedef std::map<T, T> base_type;
59     base_type base;
60
61 public:
62
63     T & operator[] (const T &key) {
64         typename base_type::iterator it;
65         it = base.find(key);
66         if (it == base.end()) {
67             return (base[key] = key);
68         }
69         return it->second;
70     }
71     
72     const T & operator[] (const T &key) const {
73         typename base_type::const_iterator it;
74         it = base.find(key);
75         if (it == base.end()) {
76             return (base[key] = key);
77         }
78         return it->second;
79     }
80 };
81
82
83 void
84 addRegion(unsigned long long address, void *buffer, unsigned long long size);
85
86 void
87 delRegionByPointer(void *ptr);
88
89 void *
90 toPointer(trace::Value &value, bool bind = false);
91
92
93 /**
94  * Output verbosity when retracing files.
95  */
96 extern int verbosity;
97
98 /**
99  * Add profiling data to the dump when retracing.
100  */
101 extern bool profiling;
102
103
104 std::ostream &warning(trace::Call &call);
105
106
107 void ignore(trace::Call &call);
108 void unsupported(trace::Call &call);
109
110
111 typedef void (*Callback)(trace::Call &call);
112
113 struct Entry {
114     const char *name;
115     Callback callback;
116 };
117
118
119 struct stringComparer {
120   bool operator() (const char *a, const  char *b) const {
121     return strcmp(a, b) < 0;
122   }
123 };
124
125
126 extern const Entry stdc_callbacks[];
127
128
129 class Retracer
130 {
131     typedef std::map<const char *, Callback, stringComparer> Map;
132     Map map;
133
134     std::vector<Callback> callbacks;
135
136 public:
137     Retracer() {
138         addCallbacks(stdc_callbacks);
139     }
140
141     virtual ~Retracer() {}
142
143     void addCallback(const Entry *entry);
144     void addCallbacks(const Entry *entries);
145
146     void retrace(trace::Call &call);
147 };
148
149
150 } /* namespace retrace */
151
152 #endif /* _RETRACE_HPP_ */