debug_engine.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (C) 2021 Ant Group. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _DEBUG_ENGINE_H
  6. #define _DEBUG_ENGINE_H
  7. #include "bh_list.h"
  8. #include "gdbserver.h"
  9. #include "thread_manager.h"
  10. typedef enum WASMDebugControlThreadStatus {
  11. RUNNING,
  12. DETACHED,
  13. STOPPED,
  14. } WASMDebugControlThreadStatus;
  15. struct WASMDebugEngine;
  16. struct WASMDebugInstance;
  17. typedef struct WASMDebugControlThread {
  18. WASMGDBServer *server;
  19. korp_tid tid;
  20. korp_mutex wait_lock;
  21. char ip_addr[128];
  22. int port;
  23. WASMDebugControlThreadStatus status;
  24. struct WASMDebugEngine *debug_engine;
  25. struct WASMDebugInstance *debug_instance;
  26. } WASMDebugControlThread;
  27. typedef struct WASMDebugBreakPoint {
  28. struct WASMDebugBreakPoint *next;
  29. uint64 addr;
  30. uint64 orignal_data;
  31. } WASMDebugBreakPoint;
  32. typedef struct WASMDebugWatchPoint {
  33. bh_list_link next;
  34. uint64 addr;
  35. uint64 length;
  36. } WASMDebugWatchPoint;
  37. typedef enum debug_state_t {
  38. /* Debugger state conversion sequence:
  39. * DBG_LAUNCHING ---> APP_STOPPED <---> APP_RUNNING
  40. */
  41. DBG_LAUNCHING,
  42. APP_RUNNING,
  43. APP_STOPPED,
  44. DBG_ERROR
  45. } debug_state_t;
  46. typedef struct WASMDebugExecutionMemory {
  47. uint64 start_offset;
  48. uint64 current_pos;
  49. uint32 size;
  50. } WASMDebugExecutionMemory;
  51. struct WASMDebugInstance {
  52. struct WASMDebugInstance *next;
  53. WASMDebugControlThread *control_thread;
  54. bh_list break_point_list;
  55. bh_list watch_point_list_read;
  56. bh_list watch_point_list_write;
  57. WASMCluster *cluster;
  58. uint32 id;
  59. korp_tid current_tid;
  60. korp_mutex wait_lock;
  61. korp_cond wait_cond;
  62. /* Last stopped thread, it should be set to NULL when sending
  63. * out the thread stop reply */
  64. WASMExecEnv *volatile stopped_thread;
  65. /* Currently status of the debug instance, it will be set to
  66. * RUNNING when receiving STEP/CONTINUE commands, and set to
  67. * STOPPED when any thread stopped */
  68. volatile debug_state_t current_state;
  69. /* Execution memory info. During debugging, the debug client may request to
  70. * malloc a memory space to evaluate user expressions. We preserve a buffer
  71. * during creating debug instance, and use a simple bump pointer allocator
  72. * to serve lldb's memory request */
  73. WASMDebugExecutionMemory exec_mem_info;
  74. };
  75. typedef enum WASMDebugEventKind {
  76. BREAK_POINT_ADD,
  77. BREAK_POINT_REMOVE
  78. } WASMDebugEventKind;
  79. typedef struct WASMDebugEvent {
  80. WASMDebugEventKind kind;
  81. unsigned char metadata[0];
  82. } WASMDebugEvent;
  83. typedef struct WASMDebugMemoryInfo {
  84. uint64 start;
  85. uint64 size;
  86. char name[128];
  87. char permisson[4];
  88. } WASMDebugMemoryInfo;
  89. typedef enum WasmAddressType {
  90. WasmMemory = 0x00,
  91. WasmObj = 0x01,
  92. WasmInvalid = 0x03
  93. } WasmAddressType;
  94. #define WASM_ADDR(type, id, offset) \
  95. (((uint64)type << 62) | ((uint64)0 << 32) | ((uint64)offset << 0))
  96. #define WASM_ADDR_TYPE(addr) (((addr)&0xC000000000000000) >> 62)
  97. #define WASM_ADDR_OFFSET(addr) (((addr)&0x00000000FFFFFFFF))
  98. #define INVALIED_ADDR (0xFFFFFFFFFFFFFFFF)
  99. void
  100. on_thread_stop_event(WASMDebugInstance *debug_inst, WASMExecEnv *exec_env);
  101. void
  102. on_thread_exit_event(WASMDebugInstance *debug_inst, WASMExecEnv *exec_env);
  103. WASMDebugInstance *
  104. wasm_debug_instance_create(WASMCluster *cluster, int32 port);
  105. void
  106. wasm_debug_instance_destroy(WASMCluster *cluster);
  107. WASMDebugInstance *
  108. wasm_exec_env_get_instance(WASMExecEnv *exec_env);
  109. bool
  110. wasm_debug_engine_init(char *ip_addr, int32 process_port);
  111. void
  112. wasm_debug_engine_destroy(void);
  113. WASMExecEnv *
  114. wasm_debug_instance_get_current_env(WASMDebugInstance *instance);
  115. uint64
  116. wasm_debug_instance_get_pid(WASMDebugInstance *instance);
  117. korp_tid
  118. wasm_debug_instance_get_tid(WASMDebugInstance *instance);
  119. uint32
  120. wasm_debug_instance_get_tids(WASMDebugInstance *instance, korp_tid tids[],
  121. uint32 len);
  122. void
  123. wasm_debug_instance_set_cur_thread(WASMDebugInstance *instance, korp_tid tid);
  124. uint64
  125. wasm_debug_instance_get_pc(WASMDebugInstance *instance);
  126. uint64
  127. wasm_debug_instance_get_load_addr(WASMDebugInstance *instance);
  128. WASMDebugMemoryInfo *
  129. wasm_debug_instance_get_memregion(WASMDebugInstance *instance, uint64 addr);
  130. void
  131. wasm_debug_instance_destroy_memregion(WASMDebugInstance *instance,
  132. WASMDebugMemoryInfo *mem_info);
  133. bool
  134. wasm_debug_instance_get_obj_mem(WASMDebugInstance *instance, uint64 addr,
  135. char *buf, uint64 *size);
  136. bool
  137. wasm_debug_instance_get_linear_mem(WASMDebugInstance *instance, uint64 addr,
  138. char *buf, uint64 *size);
  139. bool
  140. wasm_debug_instance_get_mem(WASMDebugInstance *instance, uint64 addr, char *buf,
  141. uint64 *size);
  142. bool
  143. wasm_debug_instance_set_mem(WASMDebugInstance *instance, uint64 addr, char *buf,
  144. uint64 *size);
  145. uint32
  146. wasm_debug_instance_get_call_stack_pcs(WASMDebugInstance *instance,
  147. korp_tid tid, uint64 buf[], uint64 size);
  148. bool
  149. wasm_debug_instance_add_breakpoint(WASMDebugInstance *instance, uint64 addr,
  150. uint64 length);
  151. bool
  152. wasm_debug_instance_remove_breakpoint(WASMDebugInstance *instance, uint64 addr,
  153. uint64 length);
  154. bool
  155. wasm_debug_instance_watchpoint_write_add(WASMDebugInstance *instance,
  156. uint64 addr, uint64 length);
  157. bool
  158. wasm_debug_instance_watchpoint_write_remove(WASMDebugInstance *instance,
  159. uint64 addr, uint64 length);
  160. bool
  161. wasm_debug_instance_watchpoint_read_add(WASMDebugInstance *instance,
  162. uint64 addr, uint64 length);
  163. bool
  164. wasm_debug_instance_watchpoint_read_remove(WASMDebugInstance *instance,
  165. uint64 addr, uint64 length);
  166. bool
  167. wasm_debug_instance_on_failure(WASMDebugInstance *instance);
  168. bool
  169. wasm_debug_instance_interrupt_all_threads(WASMDebugInstance *instance);
  170. bool
  171. wasm_debug_instance_continue(WASMDebugInstance *instance);
  172. bool
  173. wasm_debug_instance_detach(WASMDebugInstance *instance);
  174. bool
  175. wasm_debug_instance_kill(WASMDebugInstance *instance);
  176. uint32
  177. wasm_debug_instance_get_thread_status(WASMDebugInstance *instance,
  178. korp_tid tid);
  179. bool
  180. wasm_debug_instance_singlestep(WASMDebugInstance *instance, korp_tid tid);
  181. bool
  182. wasm_debug_instance_get_local(WASMDebugInstance *instance, int32 frame_index,
  183. int32 local_index, char buf[], int32 *size);
  184. bool
  185. wasm_debug_instance_get_global(WASMDebugInstance *instance, int32 frame_index,
  186. int32 global_index, char buf[], int32 *size);
  187. #if WASM_ENABLE_LIBC_WASI != 0
  188. bool
  189. wasm_debug_instance_get_current_object_name(WASMDebugInstance *instance,
  190. char name_buffer[], uint32 len);
  191. #endif
  192. uint64
  193. wasm_debug_instance_mmap(WASMDebugInstance *instance, uint32 size,
  194. int32 map_prot);
  195. bool
  196. wasm_debug_instance_ummap(WASMDebugInstance *instance, uint64 addr);
  197. #endif