Clone of mesa.
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.

gltrace_support.cc 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 2006 Thomas Sondergaard All Rights Reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  18. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "gltrace_support.h"
  22. #include <cstdlib>
  23. #include <cstring>
  24. #include <assert.h>
  25. #include <sstream>
  26. #include <fstream>
  27. #include <iomanip>
  28. #include <execinfo.h>
  29. #include <cxxabi.h>
  30. #include <sys/time.h>
  31. namespace {
  32. const char *
  33. demangle (const char * mangled) throw()
  34. {
  35. static char buf[4096];
  36. int status;
  37. size_t length = sizeof(buf)-1;
  38. memset (buf, 0, sizeof(buf));
  39. if (!mangled)
  40. return 0;
  41. char * demangled = __cxxabiv1::__cxa_demangle(mangled,
  42. buf,
  43. &length,
  44. &status);
  45. if (demangled && !status)
  46. return demangled;
  47. else
  48. return mangled;
  49. }
  50. void
  51. printStackTrace (void **stackframes,
  52. int stackframe_size,
  53. std::ostream & out )
  54. {
  55. char **strings = 0;
  56. std::stringstream ss;
  57. // this might actually fail if memory is tight or we are in a
  58. // signal handler
  59. strings = backtrace_symbols (stackframes, stackframe_size);
  60. ss << "Backtrace :";
  61. if (stackframe_size == gltrace::MAX_STACKFRAMES)
  62. ss << "(possibly incomplete maximal number of frames exceeded):" << std::endl;
  63. else
  64. ss << std::endl;
  65. out << ss.str();
  66. // the first frame is the constructor of the exception
  67. // the last frame always seem to be bogus?
  68. for (int i = 0; strings && i < stackframe_size-1; ++i) {
  69. char libname[257], funcname[2049];
  70. unsigned int address=0, funcoffset = 0x0;
  71. memset (libname,0,sizeof(libname));
  72. memset (funcname,0,sizeof(funcname));
  73. strcpy (funcname,"??");
  74. strcpy (libname, "??");
  75. int scanned = sscanf (strings[i], "%256[^(] ( %2048[^+] + %x ) [ %x ]",
  76. libname,
  77. funcname,
  78. &funcoffset,
  79. &address);
  80. /* ok, so no function was mentioned in the backtrace */
  81. if (scanned < 4) {
  82. scanned = sscanf (strings[i], "%256[^([] [ %x ]",
  83. libname,
  84. &address);
  85. }
  86. if (funcname[0] == '_') {
  87. const char * demangled;
  88. if ((demangled = demangle(funcname) ) != funcname) {
  89. strncpy (funcname, demangled, sizeof(funcname)-1);
  90. }
  91. }
  92. else
  93. strcat (funcname," ()");
  94. out << "\t#" << i << std::hex << " 0x" << address << " in " << funcname
  95. << " at 0x" << funcoffset << " (from " << libname << ")" << std::endl;
  96. }
  97. free (strings);
  98. }
  99. } // anon namespace
  100. namespace gltrace {
  101. std::string getStackTrace(int count, int first) {
  102. ++first;
  103. std::stringstream ss;
  104. const int BA_MAX = 1000;
  105. assert(count + first <= BA_MAX);
  106. void *ba[BA_MAX];
  107. int n = backtrace(ba, count+first);
  108. printStackTrace( &ba[first], n-first, ss);
  109. return ss.str();
  110. }
  111. std::ostream &timeNow(std::ostream &os) {
  112. struct timeval now;
  113. struct tm t;
  114. static char const *months[12] =
  115. {
  116. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  117. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  118. };
  119. gettimeofday (&now, 0);
  120. localtime_r ((time_t*) &now.tv_sec, &t);
  121. os
  122. << months[t.tm_mon] << " "
  123. << std::setw(2) << t.tm_mday << " "
  124. << std::setw(2) << t.tm_hour << ":"
  125. << std::setw(2) << t.tm_min << ":"
  126. << std::setw(2) << t.tm_sec << "."
  127. << std::setw(3) << now.tv_usec/1000;
  128. return os;
  129. }
  130. logstream::logstream(const char *filename) {
  131. if (!filename)
  132. init(std::cerr.rdbuf());
  133. else {
  134. file_os.reset(new std::ofstream(filename));
  135. if (file_os->good())
  136. init(file_os->rdbuf());
  137. else {
  138. std::cerr << "ERROR: gltrace: Failed to open '" << filename
  139. << "' for writing. Falling back to stderr." << std::endl;
  140. init(std::cerr.rdbuf());
  141. }
  142. }
  143. *this << std::setfill('0'); // setw used in timeNow
  144. }
  145. Config::Config() :
  146. logCalls(true),
  147. checkErrors(true),
  148. logTime(true),
  149. log(getenv("GLTRACE_LOGFILE")) {
  150. if (const char *v = getenv("GLTRACE_LOG_CALLS"))
  151. logCalls = strncmp("1", v, 1) == 0;
  152. if (const char *v = getenv("GLTRACE_CHECK_ERRORS"))
  153. checkErrors = strncmp("1", v, 1) == 0;
  154. if (const char *v = getenv("GLTRACE_LOG_TIME"))
  155. logTime = strncmp("1", v, 1) == 0;
  156. }
  157. // *The* config
  158. Config config;
  159. } // namespace gltrace