debugbreak.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Copyright (c) 2013, Scott Tsai
  2. *
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  18. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef DEBUG_BREAK_H
  27. #define DEBUG_BREAK_H
  28. #if defined(_MSC_VER) || defined(__MINGW32__)
  29. #define debug_break __debugbreak
  30. #else
  31. #include <signal.h>
  32. #include <unistd.h>
  33. #include <sys/syscall.h>
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. enum {
  38. /* gcc optimizers consider code after __builtin_trap() dead.
  39. * Making __builtin_trap() unsuitable for breaking into the debugger */
  40. DEBUG_BREAK_PREFER_BUILTIN_TRAP_TO_SIGTRAP = 0,
  41. };
  42. #if defined(__i386__) || defined(__x86_64__)
  43. enum { HAVE_TRAP_INSTRUCTION = 1, };
  44. __attribute__((gnu_inline, always_inline))
  45. static void __inline__ trap_instruction(void)
  46. {
  47. __asm__ volatile("int $0x03");
  48. }
  49. #elif defined(__thumb__)
  50. enum { HAVE_TRAP_INSTRUCTION = 1, };
  51. /* FIXME: handle __THUMB_INTERWORK__ */
  52. __attribute__((gnu_inline, always_inline))
  53. static void __inline__ trap_instruction(void)
  54. {
  55. /* See 'arm-linux-tdep.c' in GDB source.
  56. * Both instruction sequences below works. */
  57. #if 1
  58. /* 'eabi_linux_thumb_le_breakpoint' */
  59. __asm__ volatile(".inst 0xde01");
  60. #else
  61. /* 'eabi_linux_thumb2_le_breakpoint' */
  62. __asm__ volatile(".inst.w 0xf7f0a000");
  63. #endif
  64. /* Known problem:
  65. * After a breakpoint hit, can't stepi, step, or continue in GDB.
  66. * 'step' stuck on the same instruction.
  67. *
  68. * Workaround: a new GDB command,
  69. * 'debugbreak-step' is defined in debugbreak-gdb.py
  70. * that does:
  71. * (gdb) set $instruction_len = 2
  72. * (gdb) tbreak *($pc + $instruction_len)
  73. * (gdb) jump *($pc + $instruction_len)
  74. */
  75. }
  76. #elif defined(__arm__) && !defined(__thumb__)
  77. enum { HAVE_TRAP_INSTRUCTION = 1, };
  78. __attribute__((gnu_inline, always_inline))
  79. static void __inline__ trap_instruction(void)
  80. {
  81. /* See 'arm-linux-tdep.c' in GDB source,
  82. * 'eabi_linux_arm_le_breakpoint' */
  83. __asm__ volatile(".inst 0xe7f001f0");
  84. /* Has same known problem and workaround
  85. * as Thumb mode */
  86. }
  87. #else
  88. enum { HAVE_TRAP_INSTRUCTION = 0, };
  89. #endif
  90. __attribute__((gnu_inline, always_inline))
  91. static void __inline__ debug_break(void)
  92. {
  93. if (HAVE_TRAP_INSTRUCTION) {
  94. #if defined(ETH_EMSCRIPTEN)
  95. asm("debugger");
  96. #else
  97. trap_instruction();
  98. #endif
  99. } else if (DEBUG_BREAK_PREFER_BUILTIN_TRAP_TO_SIGTRAP) {
  100. /* raises SIGILL on Linux x86{,-64}, to continue in gdb:
  101. * (gdb) handle SIGILL stop nopass
  102. * */
  103. __builtin_trap();
  104. } else {
  105. raise(SIGTRAP);
  106. }
  107. }
  108. #ifdef __cplusplus
  109. }
  110. #endif
  111. #endif
  112. #endif