Advertisement
Guest User

getMethodName

a guest
Jun 23rd, 2010
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. /*
  2.                              DON'T TELL I DID IT PUBLIC LICENCE
  3.    
  4.     Everyone is permitted to copy and distribute verbatim copies of this licence document,
  5.     and changing it is allowed as long as the name is changed.
  6.    
  7.                              DON'T TELL I DID IT PUBLIC LICENCE
  8.                TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  9.    
  10.     0. You may copy, modify and distribute this program under source form and binary form
  11.        as long as you DO NOT include the name of the original author with it.
  12.  
  13.     1. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. THE
  14.     PROGRAM IS PROVIDED β€œAS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
  15.     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16.     A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
  17.     IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY
  18.     SERVICING, REPAIR OR CORRECTION.
  19. */
  20.  
  21. #include <dlfcn.h>
  22.  
  23. template<typename T>
  24. const void* voidify(T method)
  25. {
  26.     void* pointer;
  27.     asm ("movq %rdi, -8(%rbp)");
  28.     return pointer;
  29. }
  30.  
  31. template<typename T, typename M>
  32. const char* getMethodName(const T* object, M method)
  33. {
  34.     Dl_info info;
  35.     const void* methodPointer;
  36.    
  37.     if (sizeof method == 8) methodPointer = voidify(method); // actual method pointer
  38.     else // vtable entry
  39.     {
  40.         struct VTableEntry
  41.         {
  42.             unsigned long long __pfn;
  43.             unsigned long long __delta;
  44.         };
  45.         const VTableEntry* entry = static_cast<const VTableEntry*>(voidify(&method));
  46.         const void* const* vtable = *(const void* const* const* const)object;
  47.         methodPointer = vtable[(entry->__pfn - 1) / sizeof(void*)];
  48.     }
  49.    
  50.     if (dladdr(voidify(methodPointer), &info))
  51.         return info.dli_sname;
  52.     return NULL;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement