You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
9.7 KiB

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