diff options
Diffstat (limited to 'lib/common/Utils.cpp')
-rw-r--r-- | lib/common/Utils.cpp | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/lib/common/Utils.cpp b/lib/common/Utils.cpp index 6f21330d..3137d980 100644 --- a/lib/common/Utils.cpp +++ b/lib/common/Utils.cpp @@ -24,9 +24,23 @@ #include <cxxabi.h> #endif +<<<<<<< HEAD #include "Utils.h" #include "CommonException.h" #include "Logging.h" +======= +#ifdef HAVE_DLFCN_H + #include <dlfcn.h> +#endif + +#ifdef NEED_BOX_VERSION_H +# include "BoxVersion.h" +#endif + +#include "CommonException.h" +#include "Logging.h" +#include "Utils.h" +>>>>>>> 0.12 #include "MemLeakFindOn.h" @@ -73,16 +87,84 @@ void SplitString(const std::string &String, char SplitOn, std::vector<std::strin } #ifdef SHOW_BACKTRACE_ON_EXCEPTION +<<<<<<< HEAD void DumpStackBacktrace() { void *array[10]; size_t size = backtrace (array, 10); char **strings = backtrace_symbols (array, size); +======= +static std::string demangle(const std::string& mangled_name) +{ + #ifdef HAVE_CXXABI_H + int status; + +#include "MemLeakFindOff.h" + char* result = abi::__cxa_demangle(mangled_name.c_str(), + NULL, NULL, &status); +#include "MemLeakFindOn.h" + + if (result == NULL) + { + if (status == 0) + { + BOX_WARNING("Demangle failed but no error: " << + mangled_name); + } + else if (status == -1) + { + BOX_WARNING("Demangle failed with " + "memory allocation error: " << + mangled_name); + } + else if (status == -2) + { + // Probably non-C++ name, don't demangle + /* + BOX_WARNING("Demangle failed with " + "with invalid name: " << + mangled_name); + */ + } + else if (status == -3) + { + BOX_WARNING("Demangle failed with " + "with invalid argument: " << + mangled_name); + } + else + { + BOX_WARNING("Demangle failed with " + "with unknown error " << status << + ": " << mangled_name); + } + + return std::string(mangled_name); + } + else + { + std::string output = result; +#include "MemLeakFindOff.h" + free(result); +#include "MemLeakFindOn.h" + return output; + } + #else // !HAVE_CXXABI_H + return mangled_name; + #endif // HAVE_CXXABI_H +} + +void DumpStackBacktrace() +{ + void *array[10]; + size_t size = backtrace(array, 10); +>>>>>>> 0.12 BOX_TRACE("Obtained " << size << " stack frames."); for(size_t i = 0; i < size; i++) { +<<<<<<< HEAD // Demangling code copied from // cctbx_sources/boost_adaptbx/meta_ext.cpp, BSD license @@ -155,6 +237,40 @@ void DumpStackBacktrace() #include "MemLeakFindOn.h" } #endif +======= + std::ostringstream output; + output << "Stack frame " << i << ": "; + + #ifdef HAVE_DLADDR + Dl_info info; + int result = dladdr(array[i], &info); + + if(result == 0) + { + BOX_LOG_SYS_WARNING("Failed to resolve " + "backtrace address " << array[i]); + output << "unresolved address " << array[i]; + } + else if(info.dli_sname == NULL) + { + output << "unknown address " << array[i]; + } + else + { + uint64_t diff = (uint64_t) array[i]; + diff -= (uint64_t) info.dli_saddr; + output << demangle(info.dli_sname) << "+" << + (void *)diff; + } + #else + output << "address " << array[i]; + #endif // HAVE_DLADDR + + BOX_TRACE(output.str()); + } +} +#endif // SHOW_BACKTRACE_ON_EXCEPTION +>>>>>>> 0.12 @@ -313,3 +429,34 @@ std::string FormatUsageLineStart(const std::string& rName, return result.str(); } +<<<<<<< HEAD +======= + +std::string BoxGetTemporaryDirectoryName() +{ +#ifdef WIN32 + // http://msdn.microsoft.com/library/default.asp? + // url=/library/en-us/fileio/fs/creating_and_using_a_temporary_file.asp + + DWORD dwRetVal; + char lpPathBuffer[1024]; + DWORD dwBufSize = sizeof(lpPathBuffer); + + // Get the temp path. + dwRetVal = GetTempPath(dwBufSize, // length of the buffer + lpPathBuffer); // buffer for path + if (dwRetVal > dwBufSize) + { + THROW_EXCEPTION(CommonException, TempDirPathTooLong) + } + + return std::string(lpPathBuffer); +#elif defined TEMP_DIRECTORY_NAME + return std::string(TEMP_DIRECTORY_NAME); +#else + #error non-static temporary directory names not supported yet +#endif +} + + +>>>>>>> 0.12 |