license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 1 | // Copyright (c) 2006-2008 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. |
deanm@google.com | c2b652a | 2008-08-13 20:15:11 +0900 | [diff] [blame] | 4 | |
| 5 | #include "base/debug_util.h" |
| 6 | |
| 7 | #include <unistd.h> |
| 8 | #include <sys/sysctl.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <fcntl.h> |
| 12 | |
| 13 | #include "base/notimplemented.h" |
| 14 | #include "base/string_piece.h" |
| 15 | |
| 16 | bool DebugUtil::SpawnDebuggerOnProcess(unsigned /* process_id */) { |
| 17 | NOTIMPLEMENTED(); |
| 18 | return false; |
| 19 | } |
| 20 | |
| 21 | #if defined(OS_MACOSX) |
| 22 | // http://developer.apple.com/qa/qa2004/qa1361.html |
| 23 | bool DebugUtil::BeingDebugged() { |
| 24 | NOTIMPLEMENTED(); |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | #elif defined(OS_LINUX) |
| 29 | // We can look in /proc/self/status for TracerPid. We are likely used in crash |
| 30 | // handling, so we are careful not to use the heap or have side effects. |
| 31 | // Another option that is common is to try to ptrace yourself, but then we |
| 32 | // can't detach without forking(), and that's not so great. |
| 33 | bool DebugUtil::BeingDebugged() { |
| 34 | int status_fd = open("/proc/self/status", O_RDONLY); |
| 35 | if (status_fd == -1) |
| 36 | return false; |
| 37 | |
| 38 | // We assume our line will be in the first 1024 characters and that we can |
| 39 | // read this much all at once. In practice this will generally be true. |
| 40 | // This simplifies and speeds up things considerably. |
| 41 | char buf[1024]; |
| 42 | |
| 43 | ssize_t num_read = read(status_fd, buf, sizeof(buf)); |
| 44 | close(status_fd); |
| 45 | |
| 46 | if (num_read <= 0) |
| 47 | return false; |
| 48 | |
| 49 | StringPiece status(buf, num_read); |
| 50 | StringPiece tracer("TracerPid:\t"); |
| 51 | |
| 52 | StringPiece::size_type pid_index = status.find(tracer); |
| 53 | if (pid_index == StringPiece::npos) |
| 54 | return false; |
| 55 | |
| 56 | // Our pid is 0 without a debugger, assume this for any pid starting with 0. |
| 57 | pid_index += tracer.size(); |
| 58 | return pid_index < status.size() && status[pid_index] != '0'; |
| 59 | } |
| 60 | #endif |
| 61 | |
| 62 | // static |
| 63 | void DebugUtil::BreakDebugger() { |
| 64 | asm ("int3"); |
| 65 | } |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 66 | |