debugger_posix.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/debug/debugger.h"
  5. #include "base/build_config.h"
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <sys/param.h>
  11. #include <sys/stat.h>
  12. #include <sys/types.h>
  13. #include <unistd.h>
  14. #include <vector>
  15. #if defined(__GLIBCXX__)
  16. #include <cxxabi.h>
  17. #endif
  18. #if defined(OS_MACOSX)
  19. #include <AvailabilityMacros.h>
  20. #endif
  21. #if defined(OS_MACOSX) || defined(OS_BSD)
  22. #include <sys/sysctl.h>
  23. #endif
  24. #if defined(OS_FREEBSD)
  25. #include <sys/user.h>
  26. #endif
  27. #include <ostream>
  28. #include "base/basictypes.h"
  29. #include "base/logging.h"
  30. #include "base/memory/scoped_ptr.h"
  31. #include "base/posix/eintr_wrapper.h"
  32. #include "base/safe_strerror_posix.h"
  33. #include "base/strings/string_piece.h"
  34. #if defined(USE_SYMBOLIZE)
  35. #include "base/third_party/symbolize/symbolize.h"
  36. #endif
  37. #if defined(OS_ANDROID)
  38. #include "base/threading/platform_thread.h"
  39. #endif
  40. namespace base {
  41. namespace debug {
  42. #if defined(OS_MACOSX) || defined(OS_BSD)
  43. // Based on Apple's recommended method as described in
  44. // http://developer.apple.com/qa/qa2004/qa1361.html
  45. bool BeingDebugged() {
  46. // NOTE: This code MUST be async-signal safe (it's used by in-process
  47. // stack dumping signal handler). NO malloc or stdio is allowed here.
  48. //
  49. // While some code used below may be async-signal unsafe, note how
  50. // the result is cached (see |is_set| and |being_debugged| static variables
  51. // right below). If this code is properly warmed-up early
  52. // in the start-up process, it should be safe to use later.
  53. // If the process is sandboxed then we can't use the sysctl, so cache the
  54. // value.
  55. static bool is_set = false;
  56. static bool being_debugged = false;
  57. if (is_set)
  58. return being_debugged;
  59. // Initialize mib, which tells sysctl what info we want. In this case,
  60. // we're looking for information about a specific process ID.
  61. int mib[] = {
  62. CTL_KERN,
  63. KERN_PROC,
  64. KERN_PROC_PID,
  65. getpid()
  66. #if defined(OS_OPENBSD)
  67. , sizeof(struct kinfo_proc),
  68. 0
  69. #endif
  70. };
  71. // Caution: struct kinfo_proc is marked __APPLE_API_UNSTABLE. The source and
  72. // binary interfaces may change.
  73. struct kinfo_proc info;
  74. size_t info_size = sizeof(info);
  75. #if defined(OS_OPENBSD)
  76. if (sysctl(mib, arraysize(mib), NULL, &info_size, NULL, 0) < 0)
  77. return -1;
  78. mib[5] = (info_size / sizeof(struct kinfo_proc));
  79. #endif
  80. int sysctl_result = sysctl(mib, arraysize(mib), &info, &info_size, NULL, 0);
  81. DCHECK_EQ(sysctl_result, 0);
  82. if (sysctl_result != 0) {
  83. is_set = true;
  84. being_debugged = false;
  85. return being_debugged;
  86. }
  87. // This process is being debugged if the P_TRACED flag is set.
  88. is_set = true;
  89. #if defined(OS_FREEBSD)
  90. being_debugged = (info.ki_flag & P_TRACED) != 0;
  91. #elif defined(OS_BSD)
  92. being_debugged = (info.p_flag & P_TRACED) != 0;
  93. #else
  94. being_debugged = (info.kp_proc.p_flag & P_TRACED) != 0;
  95. #endif
  96. return being_debugged;
  97. }
  98. #elif defined(OS_LINUX) || defined(OS_ANDROID)
  99. // We can look in /proc/self/status for TracerPid. We are likely used in crash
  100. // handling, so we are careful not to use the heap or have side effects.
  101. // Another option that is common is to try to ptrace yourself, but then we
  102. // can't detach without forking(), and that's not so great.
  103. // static
  104. bool BeingDebugged() {
  105. // NOTE: This code MUST be async-signal safe (it's used by in-process
  106. // stack dumping signal handler). NO malloc or stdio is allowed here.
  107. int status_fd = open("/proc/self/status", O_RDONLY);
  108. if (status_fd == -1)
  109. return false;
  110. // We assume our line will be in the first 1024 characters and that we can
  111. // read this much all at once. In practice this will generally be true.
  112. // This simplifies and speeds up things considerably.
  113. char buf[1024];
  114. ssize_t num_read = HANDLE_EINTR(read(status_fd, buf, sizeof(buf)));
  115. if (IGNORE_EINTR(close(status_fd)) < 0)
  116. return false;
  117. if (num_read <= 0)
  118. return false;
  119. StringPiece status(buf, num_read);
  120. StringPiece tracer("TracerPid:\t");
  121. StringPiece::size_type pid_index = status.find(tracer);
  122. if (pid_index == StringPiece::npos)
  123. return false;
  124. // Our pid is 0 without a debugger, assume this for any pid starting with 0.
  125. pid_index += tracer.size();
  126. return pid_index < status.size() && status[pid_index] != '0';
  127. }
  128. #else
  129. bool BeingDebugged() {
  130. NOTIMPLEMENTED();
  131. return false;
  132. }
  133. #endif
  134. // We want to break into the debugger in Debug mode, and cause a crash dump in
  135. // Release mode. Breakpad behaves as follows:
  136. //
  137. // +-------+-----------------+-----------------+
  138. // | OS | Dump on SIGTRAP | Dump on SIGABRT |
  139. // +-------+-----------------+-----------------+
  140. // | Linux | N | Y |
  141. // | Mac | Y | N |
  142. // +-------+-----------------+-----------------+
  143. //
  144. // Thus we do the following:
  145. // Linux: Debug mode if a debugger is attached, send SIGTRAP; otherwise send
  146. // SIGABRT
  147. // Mac: Always send SIGTRAP.
  148. #if defined(ARCH_CPU_ARMEL)
  149. #define DEBUG_BREAK_ASM() asm("bkpt 0")
  150. #elif defined(ARCH_CPU_ARM64)
  151. #define DEBUG_BREAK_ASM() asm("brk 0")
  152. #elif defined(ARCH_CPU_MIPS_FAMILY)
  153. #define DEBUG_BREAK_ASM() asm("break 2")
  154. #elif defined(ARCH_CPU_X86_FAMILY)
  155. #define DEBUG_BREAK_ASM() asm("int3")
  156. #endif
  157. #if defined(NDEBUG) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
  158. #define DEBUG_BREAK() abort()
  159. #elif defined(OS_NACL)
  160. // The NaCl verifier doesn't let use use int3. For now, we call abort(). We
  161. // should ask for advice from some NaCl experts about the optimum thing here.
  162. // http://code.google.com/p/nativeclient/issues/detail?id=645
  163. #define DEBUG_BREAK() abort()
  164. #elif !defined(OS_MACOSX)
  165. // Though Android has a "helpful" process called debuggerd to catch native
  166. // signals on the general assumption that they are fatal errors. If no debugger
  167. // is attached, we call abort since Breakpad needs SIGABRT to create a dump.
  168. // When debugger is attached, for ARM platform the bkpt instruction appears
  169. // to cause SIGBUS which is trapped by debuggerd, and we've had great
  170. // difficulty continuing in a debugger once we stop from SIG triggered by native
  171. // code, use GDB to set |go| to 1 to resume execution; for X86 platform, use
  172. // "int3" to setup breakpiont and raise SIGTRAP.
  173. //
  174. // On other POSIX architectures, except Mac OS X, we use the same logic to
  175. // ensure that breakpad creates a dump on crashes while it is still possible to
  176. // use a debugger.
  177. namespace {
  178. void DebugBreak() {
  179. if (!BeingDebugged()) {
  180. abort();
  181. } else {
  182. #if defined(DEBUG_BREAK_ASM)
  183. DEBUG_BREAK_ASM();
  184. #else
  185. volatile int go = 0;
  186. while (!go) {
  187. base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
  188. }
  189. #endif
  190. }
  191. }
  192. } // namespace
  193. #define DEBUG_BREAK() DebugBreak()
  194. #elif defined(DEBUG_BREAK_ASM)
  195. #define DEBUG_BREAK() DEBUG_BREAK_ASM()
  196. #else
  197. #error "Don't know how to debug break on this architecture/OS"
  198. #endif
  199. void BreakDebugger() {
  200. // NOTE: This code MUST be async-signal safe (it's used by in-process
  201. // stack dumping signal handler). NO malloc or stdio is allowed here.
  202. DEBUG_BREAK();
  203. #if defined(OS_ANDROID) && !defined(OFFICIAL_BUILD)
  204. // For Android development we always build release (debug builds are
  205. // unmanageably large), so the unofficial build is used for debugging. It is
  206. // helpful to be able to insert BreakDebugger() statements in the source,
  207. // attach the debugger, inspect the state of the program and then resume it by
  208. // setting the 'go' variable above.
  209. #elif defined(NDEBUG)
  210. // Terminate the program after signaling the debug break.
  211. _exit(1);
  212. #endif
  213. }
  214. } // namespace debug
  215. } // namespace base