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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <assert.h>
  23. #include <sstream>
  24. #include <fstream>
  25. #include <iomanip>
  26. #include <execinfo.h>
  27. #include <cxxabi.h>
  28. #include <sys/time.h>
  29. namespace {
  30. const char *
  31. demangle (const char * mangled) throw()
  32. {
  33. static char buf[4096];
  34. int status;
  35. size_t length = sizeof(buf)-1;
  36. memset (buf, 0, sizeof(buf));
  37. if (!mangled)
  38. return 0;
  39. char * demangled = __cxxabiv1::__cxa_demangle(mangled,
  40. buf,
  41. &length,
  42. &status);
  43. if (demangled && !status)
  44. return demangled;
  45. else
  46. return mangled;
  47. }
  48. void
  49. printStackTrace (void **stackframes,
  50. int stackframe_size,
  51. std::ostream & out )
  52. {
  53. char **strings = 0;
  54. std::stringstream ss;
  55. // this might actually fail if memory is tight or we are in a
  56. // signal handler
  57. strings = backtrace_symbols (stackframes, stackframe_size);
  58. ss << "Backtrace :";
  59. if (stackframe_size == gltrace::MAX_STACKFRAMES)
  60. ss << "(possibly incomplete maximal number of frames exceeded):" << std::endl;
  61. else
  62. ss << std::endl;
  63. out << ss.str();
  64. // the first frame is the constructor of the exception
  65. // the last frame always seem to be bogus?
  66. for (int i = 0; strings && i < stackframe_size-1; ++i) {
  67. char libname[257], funcname[2049];
  68. unsigned int address=0, funcoffset = 0x0;
  69. memset (libname,0,sizeof(libname));
  70. memset (funcname,0,sizeof(funcname));
  71. strcpy (funcname,"??");
  72. strcpy (libname, "??");
  73. int scanned = sscanf (strings[i], "%256[^(] ( %2048[^+] + %x ) [ %x ]",
  74. libname,
  75. funcname,
  76. &funcoffset,
  77. &address);
  78. /* ok, so no function was mentioned in the backtrace */
  79. if (scanned < 4) {
  80. scanned = sscanf (strings[i], "%256[^([] [ %x ]",
  81. libname,
  82. &address);
  83. }
  84. if (funcname[0] == '_') {
  85. const char * demangled;
  86. if ((demangled = demangle(funcname) ) != funcname) {
  87. strncpy (funcname, demangled, sizeof(funcname)-1);
  88. }
  89. }
  90. else
  91. strcat (funcname," ()");
  92. out << "\t#" << i << std::hex << " 0x" << address << " in " << funcname
  93. << " at 0x" << funcoffset << " (from " << libname << ")" << std::endl;
  94. }
  95. free (strings);
  96. }
  97. } // anon namespace
  98. namespace gltrace {
  99. std::string getStackTrace(int count, int first) {
  100. ++first;
  101. std::stringstream ss;
  102. const int BA_MAX = 1000;
  103. assert(count + first <= BA_MAX);
  104. void *ba[BA_MAX];
  105. int n = backtrace(ba, count+first);
  106. printStackTrace( &ba[first], n-first, ss);
  107. return ss.str();
  108. }
  109. std::ostream &timeNow(std::ostream &os) {
  110. struct timeval now;
  111. struct tm t;
  112. static char *months[12] =
  113. {
  114. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  115. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  116. };
  117. gettimeofday (&now, 0);
  118. localtime_r ((time_t*) &now.tv_sec, &t);
  119. os
  120. << months[t.tm_mon] << " "
  121. << std::setw(2) << t.tm_mday << " "
  122. << std::setw(2) << t.tm_hour << ":"
  123. << std::setw(2) << t.tm_min << ":"
  124. << std::setw(2) << t.tm_sec << "."
  125. << std::setw(3) << now.tv_usec/1000;
  126. return os;
  127. }
  128. logstream::logstream(const char *filename) {
  129. if (!filename)
  130. init(std::cerr.rdbuf());
  131. else {
  132. file_os.reset(new std::ofstream(filename));
  133. if (file_os->good())
  134. init(file_os->rdbuf());
  135. else {
  136. std::cerr << "ERROR: gltrace: Failed to open '" << filename
  137. << "' for writing. Falling back to stderr." << std::endl;
  138. init(std::cerr.rdbuf());
  139. }
  140. }
  141. *this << std::setfill('0'); // setw used in timeNow
  142. }
  143. Config::Config() :
  144. logCalls(true),
  145. checkErrors(true),
  146. logTime(true),
  147. log(getenv("GLTRACE_LOGFILE")) {
  148. if (const char *v = getenv("GLTRACE_LOG_CALLS"))
  149. logCalls = strncmp("1", v, 1) == 0;
  150. if (const char *v = getenv("GLTRACE_CHECK_ERRORS"))
  151. checkErrors = strncmp("1", v, 1) == 0;
  152. if (const char *v = getenv("GLTRACE_LOG_TIME"))
  153. logTime = strncmp("1", v, 1) == 0;
  154. }
  155. // *The* config
  156. Config config;
  157. } // namespace gltrace