KiCad PCB EDA Suite
Loading...
Searching...
No Matches
rtti_dump.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// This file is a standalone one-header-file library using only C++03.
18//
19// It assumes that the program is compiled for the "Itanium" C++ ABI
20// (http://itanium-cxx-abi.github.io/cxx-abi/), a common C++ ABI used on many
21// CPU architectures and operating systems.
22//
23
24#ifndef RTTI_DUMP
25#define RTTI_DUMP
26
27#include <assert.h>
28#include <dlfcn.h>
29#include <errno.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33
34#include <set>
35#include <string>
36#include <typeinfo>
37
38#if defined( __ANDROID__ ) && !defined( RTTI_DUMP_USE_PRINTF )
39#include <android/log.h>
40#define RTTI_DUMP_LOG( fmt, ... ) \
41 __android_log_print( ANDROID_LOG_INFO, "rtti_dump", fmt, ##__VA_ARGS__ )
42#else
43#define RTTI_DUMP_LOG( fmt, ... ) printf( fmt "\n", ##__VA_ARGS__ )
44#endif
45
46// Avoid compiler warnings.
47#define RTTI_DUMP_UNUSED __attribute__( ( unused ) )
48
49// Use an anonymous namespace so this header file can be included at the top of
50// multiple C++ source files without breaking the One Definition Rule.
51namespace rtti_dump
52{
53namespace
54{
55
56 // Returns the type of the current exception object or NULL if the thread is
57 // not currently in a catch block.
58 extern "C" const std::type_info* __cxa_current_exception_type();
59
60 // Using run-time type information, returns an std::type_info* corresponding to
61 // the most-derived class of a pointer to an object. The pointed-to type must
62 // be a polymorphic class. (i.e. The class must have a virtual function or a
63 // base class with a virtual function.)
64 //
65 // The function can return NULL if the object's vtable comes from an object
66 // file compiled without -frtti.
67 template <typename T>
68 RTTI_DUMP_UNUSED const std::type_info* runtime_typeid( const volatile T* dynptr )
69 {
70 T* dynptr_unqual = const_cast<T*>( dynptr );
71
72 // Use dynamic_cast<void*> to ensure that T is polymorphic. The result is
73 // discarded just in case the most-derived object vtable and the subobject
74 // vtable point to different typeinfo objects. (XXX: I *think* that's
75 // impossible, though.)
76 (void) sizeof( dynamic_cast<void*>( dynptr_unqual ) );
77
78 void* vptr = *reinterpret_cast<void**>( dynptr_unqual );
79 void* typeid_void = reinterpret_cast<void**>( vptr )[-1];
80 return reinterpret_cast<const std::type_info*>( typeid_void );
81 }
82
83 // Returns the name of the DSO or binary containing the given address.
84 RTTI_DUMP_UNUSED std::string dladdr_fname( const void* ptr )
85 {
86 Dl_info info = { 0 };
87
88 if( !dladdr( const_cast<void*>( ptr ), &info ) )
89 {
90 char buf[64];
91 snprintf( buf, sizeof( buf ), "[error: dladdr failed - %d]", errno );
92 return buf;
93 }
94 else
95 {
96 return std::string( info.dli_fname );
97 }
98 }
99
100 // Dump the address of the std::type_info object, its name, and the shared
101 // object where the type_info object is defined.
103 void dump_type( const std::type_info* type, const char* label = "dump_type", int indent = 0 )
104 {
105 const std::string prefix = label + std::string( ": " ) + std::string( indent, ' ' );
106
107 if( type == NULL )
108 {
109 RTTI_DUMP_LOG( "%sERROR: dump_type called with type==NULL!", prefix.c_str() );
110 }
111 else
112 {
113 struct type_info
114 {
115 virtual ~type_info() {}
116 const char* type_name;
117 };
118
119 assert( sizeof( type_info ) == sizeof( std::type_info ) );
120 const char* const name = type->name();
121 const char* const raw_name = reinterpret_cast<const type_info*>( type )->type_name;
122
123 if( name == raw_name )
124 {
125 RTTI_DUMP_LOG( "%stype %s:", prefix.c_str(), name );
126 }
127 else if( raw_name + 1 == name )
128 {
129 RTTI_DUMP_LOG( "%stype %s (raw name == '%s' @ %p):", prefix.c_str(), name, raw_name,
130 raw_name );
131 }
132 else
133 {
134 RTTI_DUMP_LOG( "%stype %s (raw name == %p):", prefix.c_str(), name, raw_name );
135 }
136
137 RTTI_DUMP_LOG( "%s type_info obj: %p (in %s)", prefix.c_str(), type,
138 dladdr_fname( type ).c_str() );
139 RTTI_DUMP_LOG( "%s type_info name: %p (in %s)", prefix.c_str(), name,
140 dladdr_fname( name ).c_str() );
141 }
142 }
143
144 // Call from a catch block to dump the type of the current exception.
146 void dump_current_exception( const char* label = "dump_current_exception" )
147 {
148 const std::type_info* type = __cxa_current_exception_type();
149
150 if( type != NULL )
151 {
152 dump_type( type, label );
153 }
154 else
155 {
156 RTTI_DUMP_LOG( "%s: ERROR: dump_current_exception called outside a catch block!",
157 label );
158 }
159 }
160
161 namespace hierarchy_dumper_internals
162 {
163
164 // std::type_info has virtual member functions, so the most-derived type of
165 // a pointer to a std::type_info object can be determined at run-time by
166 // looking for the std::type_info's own std::type_info. We rely upon this
167 // property to walk a class's RTTI graph at run-time.
168
169 struct __class_type_info : std::type_info
170 {
171 };
172
173 struct __si_class_type_info : __class_type_info
174 {
175 const __class_type_info* __base_type;
176 };
177
178 struct __base_class_type_info
179 {
180 const __class_type_info* __base_type;
182 };
183
184 struct __vmi_class_type_info : __class_type_info
185 {
186 unsigned int __flags;
187 unsigned int __base_count;
188 __base_class_type_info __base_info[1];
189 };
190
191 class Dumper
192 {
193 const char* label_;
194 std::set<const std::type_info*> seen_;
195
196 public:
197 Dumper( const char* label ) : label_( label ) {}
198 void dump_type( const std::type_info* info, int indent );
199 };
200
201 const int kIndent = 4;
202
203 void Dumper::dump_type( const std::type_info* info, int indent )
204 {
205 ::rtti_dump::dump_type( info, label_, indent * kIndent );
206
207 if( info == NULL )
208 {
209 return;
210 }
211
212 const std::type_info* info_type = runtime_typeid( info );
213 __base_class_type_info lone_base = { 0 };
214 const __base_class_type_info* base_table = NULL;
215 unsigned int base_count = 0;
216
217 // Determine type equality using a string comparison, because this dumping
218 // system doesn't trust std::type_info::operator== to work with multiple
219 // shared objects.
220
221 const int sub_indent_sp = ( indent + 1 ) * kIndent;
222
223 if( info_type == NULL )
224 {
225 // I don't think info_type can ever be NULL here.
226 RTTI_DUMP_LOG( "%s: %*sERROR: runtime_typeid(info) was NULL!", label_,
227 sub_indent_sp, "" );
228 }
229 else if( !strcmp( info_type->name(), "N10__cxxabiv120__si_class_type_infoE" ) )
230 {
231 const __si_class_type_info& infox =
232 *reinterpret_cast<const __si_class_type_info*>( info );
233 lone_base.__base_type = infox.__base_type;
234 base_count = 1;
235 base_table = &lone_base;
236 }
237 else if( !strcmp( info_type->name(), "N10__cxxabiv121__vmi_class_type_infoE" ) )
238 {
239 const __vmi_class_type_info& infox =
240 *reinterpret_cast<const __vmi_class_type_info*>( info );
241 base_count = infox.__base_count;
242 base_table = infox.__base_info;
243 }
244
245 if( base_count > 0 )
246 {
247 if( seen_.find( info ) != seen_.end() )
248 {
249 RTTI_DUMP_LOG( "%s: %*sbase classes: ...elided...", label_, sub_indent_sp, "" );
250 }
251 else
252 {
253 RTTI_DUMP_LOG( "%s: %*sbase classes:", label_, sub_indent_sp, "" );
254 seen_.insert( info );
255
256 for( unsigned int i = 0; i < base_count; ++i )
257 {
258 dump_type( base_table[i].__base_type, indent + 2 );
259 }
260 }
261 }
262 }
263 } // namespace hierarchy_dumper_internals
264
265 // Dump the std::type_info object, and if it represents a class with base
266 // classes, then dumps the class hierarchy.
268 void dump_class_hierarchy( const std::type_info* info,
269 const char* label = "dump_class_hierarchy" )
270 {
271 hierarchy_dumper_internals::Dumper dumper( label );
272 dumper.dump_type( info, 0 );
273 }
274
275} // anonymous namespace
276} // namespace rtti_dump
277
278#endif // RTTI_DUMP
const char * name
Definition: DXF_plotter.cpp:59
unsigned int __base_count
Definition: rtti_dump.h:187
long __offset_flags
Definition: rtti_dump.h:181
const char * label_
Definition: rtti_dump.h:193
#define RTTI_DUMP_UNUSED
Definition: rtti_dump.h:47
const __class_type_info * __base_type
Definition: rtti_dump.h:175
#define RTTI_DUMP_LOG(fmt,...)
Definition: rtti_dump.h:43
std::set< const std::type_info * > seen_
Definition: rtti_dump.h:194
unsigned int __flags
Definition: rtti_dump.h:186
__base_class_type_info __base_info[1]
Definition: rtti_dump.h:188