mdbg.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C)2005-2020 Haxe Foundation
  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 in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <stdbool.h>
  23. #include <pthread.h>
  24. #include <mach/mach_types.h>
  25. #ifndef MDBG_DEBUG
  26. #define MDBG_DEBUG 0
  27. #endif
  28. #ifndef MDBG_LOG_LEVEL
  29. #define MDBG_LOG_LEVEL 0
  30. #endif
  31. #define MDBG_API(func) mdbg_##func
  32. typedef bool status_t;
  33. struct exception_ports_info;
  34. typedef struct debug_session {
  35. mach_port_t task;
  36. pid_t pid;
  37. uint64_t current_thread;
  38. mach_port_t exception_port;
  39. struct exception_ports_info* old_exception_ports;
  40. pthread_t exception_handler_thread;
  41. mach_port_t wait_sem;
  42. int process_status;
  43. } debug_session;
  44. /* x64 Registers */
  45. #define REG_RAX 1
  46. #define REG_RBX 2
  47. #define REG_RCX 3
  48. #define REG_RDX 4
  49. #define REG_RDI 5
  50. #define REG_RSI 6
  51. #define REG_RBP 7
  52. #define REG_RSP 8
  53. #define REG_R8 9
  54. #define REG_R9 10
  55. #define REG_R10 11
  56. #define REG_R11 12
  57. #define REG_R12 13
  58. #define REG_R13 14
  59. #define REG_R14 15
  60. #define REG_R15 16
  61. #define REG_RIP 17
  62. #define REG_RFLAGS 18
  63. /* x64 Debug Registers */
  64. #define REG_DR0 100
  65. #define REG_DR1 101
  66. #define REG_DR2 102
  67. #define REG_DR3 103
  68. #define REG_DR4 104
  69. #define REG_DR5 105
  70. #define REG_DR6 106
  71. #define REG_DR7 107
  72. /** Public API **/
  73. extern status_t MDBG_API(session_attach)( pid_t pid );
  74. extern status_t MDBG_API(session_detach)( pid_t pid );
  75. extern int MDBG_API(session_wait)(pid_t pid, int *thread, int timeout );
  76. extern status_t MDBG_API(session_pause)( pid_t pid) ;
  77. extern status_t MDBG_API(session_resume)( pid_t pid );
  78. extern debug_session* MDBG_API(session_get)( pid_t pid);
  79. extern status_t MDBG_API(read_memory)( pid_t pid, unsigned char* addr, unsigned char* dest, int size );
  80. extern status_t MDBG_API(write_memory)( pid_t pid, unsigned char* addr, unsigned char* src, int size );
  81. extern void* MDBG_API(read_register)( pid_t pid, int thread, int reg, bool is64 );
  82. extern status_t MDBG_API(write_register)( pid_t pid, int thread, int reg, void *value, bool is64 );