This source file includes following definitions.
- SpawnDebuggerOnProcess
- BeingDebugged
- BeingDebugged
- BeingDebugged
- DebugBreak
- BreakDebugger
#include "base/debug/debugger.h"
#include "build/build_config.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <vector>
#if defined(__GLIBCXX__)
#include <cxxabi.h>
#endif
#if defined(OS_MACOSX)
#include <AvailabilityMacros.h>
#endif
#if defined(OS_MACOSX) || defined(OS_BSD)
#include <sys/sysctl.h>
#endif
#if defined(OS_FREEBSD)
#include <sys/user.h>
#endif
#include <ostream>
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/posix/eintr_wrapper.h"
#include "base/safe_strerror_posix.h"
#include "base/strings/string_piece.h"
#include "base/strings/stringprintf.h"
#if defined(USE_SYMBOLIZE)
#include "base/third_party/symbolize/symbolize.h"
#endif
#if defined(OS_ANDROID)
#include "base/threading/platform_thread.h"
#endif
namespace base {
namespace debug {
bool SpawnDebuggerOnProcess(unsigned process_id) {
#if OS_ANDROID || OS_NACL
NOTIMPLEMENTED();
return false;
#else
const std::string debug_cmd =
StringPrintf("xterm -e 'gdb --pid=%u' &", process_id);
LOG(WARNING) << "Starting debugger on pid " << process_id
<< " with command `" << debug_cmd << "`";
int ret = system(debug_cmd.c_str());
if (ret == -1)
return false;
return true;
#endif
}
#if defined(OS_MACOSX) || defined(OS_BSD)
bool BeingDebugged() {
static bool is_set = false;
static bool being_debugged = false;
if (is_set)
return being_debugged;
int mib[] = {
CTL_KERN,
KERN_PROC,
KERN_PROC_PID,
getpid()
#if defined(OS_OPENBSD)
, sizeof(struct kinfo_proc),
0
#endif
};
struct kinfo_proc info;
size_t info_size = sizeof(info);
#if defined(OS_OPENBSD)
if (sysctl(mib, arraysize(mib), NULL, &info_size, NULL, 0) < 0)
return -1;
mib[5] = (info_size / sizeof(struct kinfo_proc));
#endif
int sysctl_result = sysctl(mib, arraysize(mib), &info, &info_size, NULL, 0);
DCHECK_EQ(sysctl_result, 0);
if (sysctl_result != 0) {
is_set = true;
being_debugged = false;
return being_debugged;
}
is_set = true;
#if defined(OS_FREEBSD)
being_debugged = (info.ki_flag & P_TRACED) != 0;
#elif defined(OS_BSD)
being_debugged = (info.p_flag & P_TRACED) != 0;
#else
being_debugged = (info.kp_proc.p_flag & P_TRACED) != 0;
#endif
return being_debugged;
}
#elif defined(OS_LINUX) || defined(OS_ANDROID)
bool BeingDebugged() {
int status_fd = open("/proc/self/status", O_RDONLY);
if (status_fd == -1)
return false;
char buf[1024];
ssize_t num_read = HANDLE_EINTR(read(status_fd, buf, sizeof(buf)));
if (IGNORE_EINTR(close(status_fd)) < 0)
return false;
if (num_read <= 0)
return false;
StringPiece status(buf, num_read);
StringPiece tracer("TracerPid:\t");
StringPiece::size_type pid_index = status.find(tracer);
if (pid_index == StringPiece::npos)
return false;
pid_index += tracer.size();
return pid_index < status.size() && status[pid_index] != '0';
}
#else
bool BeingDebugged() {
NOTIMPLEMENTED();
return false;
}
#endif
#if defined(ARCH_CPU_ARMEL)
#define DEBUG_BREAK_ASM() asm("bkpt 0")
#elif defined(ARCH_CPU_ARM64)
#define DEBUG_BREAK_ASM() asm("brk 0")
#elif defined(ARCH_CPU_MIPS_FAMILY)
#define DEBUG_BREAK_ASM() asm("break 2")
#elif defined(ARCH_CPU_X86_FAMILY)
#define DEBUG_BREAK_ASM() asm("int3")
#endif
#if defined(NDEBUG) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
#define DEBUG_BREAK() abort()
#elif defined(OS_NACL)
#define DEBUG_BREAK() abort()
#elif !defined(OS_MACOSX)
namespace {
void DebugBreak() {
if (!BeingDebugged()) {
abort();
} else {
#if defined(DEBUG_BREAK_ASM)
DEBUG_BREAK_ASM();
#else
volatile int go = 0;
while (!go) {
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
}
#endif
}
}
}
#define DEBUG_BREAK() DebugBreak()
#elif defined(DEBUG_BREAK_ASM)
#define DEBUG_BREAK() DEBUG_BREAK_ASM()
#else
#error "Don't know how to debug break on this architecture/OS"
#endif
void BreakDebugger() {
DEBUG_BREAK();
#if defined(OS_ANDROID) && !defined(OFFICIAL_BUILD)
#elif defined(NDEBUG)
_exit(1);
#endif
}
}
}