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 {
52namespace {
53
54// Returns the type of the current exception object or NULL if the thread is
55// not currently in a catch block.
56extern "C" const std::type_info *__cxa_current_exception_type();
57
58// Using run-time type information, returns an std::type_info* corresponding to
59// the most-derived class of a pointer to an object. The pointed-to type must
60// be a polymorphic class. (i.e. The class must have a virtual function or a
61// base class with a virtual function.)
62//
63// The function can return NULL if the object's vtable comes from an object
64// file compiled without -frtti.
65template <typename T>
66RTTI_DUMP_UNUSED const std::type_info *runtime_typeid(const volatile T *dynptr) {
67 T *dynptr_unqual = const_cast<T*>(dynptr);
68
69 // Use dynamic_cast<void*> to ensure that T is polymorphic. The result is
70 // discarded just in case the most-derived object vtable and the subobject
71 // vtable point to different typeinfo objects. (XXX: I *think* that's
72 // impossible, though.)
73 (void)sizeof(dynamic_cast<void*>(dynptr_unqual));
74
75 void *vptr = *reinterpret_cast<void**>(dynptr_unqual);
76 void *typeid_void = reinterpret_cast<void**>(vptr)[-1];
77 return reinterpret_cast<const std::type_info*>(typeid_void);
78}
79
80// Returns the name of the DSO or binary containing the given address.
81RTTI_DUMP_UNUSED std::string dladdr_fname(const void *ptr) {
82 Dl_info info = { 0 };
83 if (!dladdr(const_cast<void*>(ptr), &info)) {
84 char buf[64];
85 snprintf(buf, sizeof(buf), "[error: dladdr failed - %d]", errno);
86 return buf;
87 } else {
88 return std::string(info.dli_fname);
89 }
90}
91
92// Dump the address of the std::type_info object, its name, and the shared
93// object where the type_info object is defined.
95void dump_type(const std::type_info *type, const char *label="dump_type", int indent=0) {
96 const std::string prefix = label + std::string(": ") + std::string(indent, ' ');
97 if (type == NULL) {
98 RTTI_DUMP_LOG("%sERROR: dump_type called with type==NULL!", prefix.c_str());
99 } else {
100 struct type_info {
101 virtual ~type_info() {}
102 const char *type_name;
103 };
104 assert(sizeof(type_info) == sizeof(std::type_info));
105 const char *const name = type->name();
106 const char *const raw_name = reinterpret_cast<const type_info*>(type)->type_name;
107 if (name == raw_name) {
108 RTTI_DUMP_LOG("%stype %s:", prefix.c_str(), name);
109 } else if (raw_name + 1 == name) {
110 RTTI_DUMP_LOG("%stype %s (raw name == '%s' @ %p):", prefix.c_str(), name, raw_name, raw_name);
111 } else {
112 RTTI_DUMP_LOG("%stype %s (raw name == %p):", prefix.c_str(), name, raw_name);
113 }
114 RTTI_DUMP_LOG("%s type_info obj: %p (in %s)",
115 prefix.c_str(), type, dladdr_fname(type).c_str());
116 RTTI_DUMP_LOG("%s type_info name: %p (in %s)",
117 prefix.c_str(), name, dladdr_fname(name).c_str());
118 }
119}
120
121// Call from a catch block to dump the type of the current exception.
123void dump_current_exception(const char *label="dump_current_exception") {
124 const std::type_info *type = __cxa_current_exception_type();
125 if (type != NULL) {
126 dump_type(type, label);
127 } else {
128 RTTI_DUMP_LOG("%s: ERROR: dump_current_exception called outside a catch block!", label);
129 }
130}
131
132namespace hierarchy_dumper_internals {
133
134 // std::type_info has virtual member functions, so the most-derived type of
135 // a pointer to a std::type_info object can be determined at run-time by
136 // looking for the std::type_info's own std::type_info. We rely upon this
137 // property to walk a class's RTTI graph at run-time.
138
139 struct __class_type_info : std::type_info {};
140
141 struct __si_class_type_info : __class_type_info {
142 const __class_type_info *__base_type;
143 };
144
145 struct __base_class_type_info {
146 const __class_type_info* __base_type;
148 };
149
150 struct __vmi_class_type_info : __class_type_info {
151 unsigned int __flags;
152 unsigned int __base_count;
153 __base_class_type_info __base_info[1];
154 };
155
156 class Dumper {
157 const char *label_;
158 std::set<const std::type_info*> seen_;
159 public:
160 Dumper(const char *label) : label_(label) {}
161 void dump_type(const std::type_info *info, int indent);
162 };
163
164 const int kIndent = 4;
165
166 void Dumper::dump_type(const std::type_info *info, int indent) {
167 ::rtti_dump::dump_type(info, label_, indent * kIndent);
168
169 if (info == NULL) {
170 return;
171 }
172
173 const std::type_info *info_type = runtime_typeid(info);
174 __base_class_type_info lone_base = { 0 };
175 const __base_class_type_info *base_table = NULL;
176 unsigned int base_count = 0;
177
178 // Determine type equality using a string comparison, because this dumping
179 // system doesn't trust std::type_info::operator== to work with multiple
180 // shared objects.
181
182 const int sub_indent_sp = (indent + 1) * kIndent;
183
184 if (info_type == NULL) {
185 // I don't think info_type can ever be NULL here.
186 RTTI_DUMP_LOG("%s: %*sERROR: runtime_typeid(info) was NULL!", label_, sub_indent_sp, "");
187 } else if (!strcmp(info_type->name(), "N10__cxxabiv120__si_class_type_infoE")) {
188 const __si_class_type_info &infox =
189 *reinterpret_cast<const __si_class_type_info*>(info);
190 lone_base.__base_type = infox.__base_type;
191 base_count = 1;
192 base_table = &lone_base;
193 } else if (!strcmp(info_type->name(), "N10__cxxabiv121__vmi_class_type_infoE")) {
194 const __vmi_class_type_info &infox =
195 *reinterpret_cast<const __vmi_class_type_info*>(info);
196 base_count = infox.__base_count;
197 base_table = infox.__base_info;
198 }
199
200 if (base_count > 0) {
201 if (seen_.find(info) != seen_.end()) {
202 RTTI_DUMP_LOG("%s: %*sbase classes: ...elided...", label_, sub_indent_sp, "");
203 } else {
204 RTTI_DUMP_LOG("%s: %*sbase classes:", label_, sub_indent_sp, "");
205 seen_.insert(info);
206 for (unsigned int i = 0; i < base_count; ++i) {
207 dump_type(base_table[i].__base_type, indent + 2);
208 }
209 }
210 }
211 }
212} // namespace hierarchy_dumper_internals
213
214// Dump the std::type_info object, and if it represents a class with base
215// classes, then dumps the class hierarchy.
217void dump_class_hierarchy(const std::type_info *info, const char *label="dump_class_hierarchy") {
218 hierarchy_dumper_internals::Dumper dumper(label);
219 dumper.dump_type(info, 0);
220}
221
222} // anonymous namespace
223} // namespace rtti_dump
224
225#endif // RTTI_DUMP
const char * name
Definition: DXF_plotter.cpp:57
unsigned int __base_count
Definition: rtti_dump.h:152
long __offset_flags
Definition: rtti_dump.h:147
const char * label_
Definition: rtti_dump.h:157
#define RTTI_DUMP_UNUSED
Definition: rtti_dump.h:47
const __class_type_info * __base_type
Definition: rtti_dump.h:142
#define RTTI_DUMP_LOG(fmt,...)
Definition: rtti_dump.h:43
std::set< const std::type_info * > seen_
Definition: rtti_dump.h:158
unsigned int __flags
Definition: rtti_dump.h:151
__base_class_type_info __base_info[1]
Definition: rtti_dump.h:153