]> git.cworth.org Git - apitrace/blob - retrace.hpp
os_posix: Fix return value for os::execute()
[apitrace] / retrace.hpp
1 /**************************************************************************
2  *
3  * Copyright 2011-2012 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 /**
84  * Similar to alloca(), but implemented with malloc.
85  */
86 class ScopedAllocator
87 {
88 private:
89     void *next;
90
91 public:
92     ScopedAllocator() :
93         next(NULL) {
94     }
95
96     inline void *
97     alloc(size_t size) {
98         if (!size) {
99             return NULL;
100         }
101
102         void * * buf = static_cast<void **>(malloc(sizeof(void *) + size));
103         if (!buf) {
104             return NULL;
105         }
106
107         *buf = next;
108         next = buf;
109
110         return &buf[1];
111     }
112
113     template< class T >
114     inline T *
115     alloc(size_t n = 1) {
116         return static_cast<T *>(alloc(sizeof(T) * n));
117     }
118
119     inline
120     ~ScopedAllocator() {
121         while (next) {
122             void *temp = *static_cast<void **>(next);
123             free(next);
124             next = temp;
125         }
126     }
127 };
128
129
130 void
131 addRegion(unsigned long long address, void *buffer, unsigned long long size);
132
133 void
134 delRegionByPointer(void *ptr);
135
136 void *
137 toPointer(trace::Value &value, bool bind = false);
138
139
140 /**
141  * Output verbosity when retracing files.
142  */
143 extern int verbosity;
144
145 /**
146  * Add profiling data to the dump when retracing.
147  */
148 extern bool profiling;
149
150
151 std::ostream &warning(trace::Call &call);
152
153
154 void ignore(trace::Call &call);
155 void unsupported(trace::Call &call);
156
157
158 typedef void (*Callback)(trace::Call &call);
159
160 struct Entry {
161     const char *name;
162     Callback callback;
163 };
164
165
166 struct stringComparer {
167   bool operator() (const char *a, const  char *b) const {
168     return strcmp(a, b) < 0;
169   }
170 };
171
172
173 extern const Entry stdc_callbacks[];
174
175
176 class Retracer
177 {
178     typedef std::map<const char *, Callback, stringComparer> Map;
179     Map map;
180
181     std::vector<Callback> callbacks;
182
183 public:
184     Retracer() {
185         addCallbacks(stdc_callbacks);
186     }
187
188     virtual ~Retracer() {}
189
190     void addCallback(const Entry *entry);
191     void addCallbacks(const Entry *entries);
192
193     void retrace(trace::Call &call);
194 };
195
196
197 } /* namespace retrace */
198
199 #endif /* _RETRACE_HPP_ */