debug_engine.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 enum debug_state_t {
  33. /* Debugger state conversion sequence:
  34. * DBG_LAUNCHING ---> APP_STOPPED <---> APP_RUNNING
  35. */
  36. DBG_LAUNCHING,
  37. APP_RUNNING,
  38. APP_STOPPED
  39. } debug_state_t;
  40. typedef struct WASMDebugExecutionMemory {
  41. uint32 start_offset;
  42. uint32 size;
  43. uint32 current_pos;
  44. } WASMDebugExecutionMemory;
  45. struct WASMDebugInstance {
  46. struct WASMDebugInstance *next;
  47. WASMDebugControlThread *control_thread;
  48. bh_list break_point_list;
  49. WASMCluster *cluster;
  50. uint32 id;
  51. korp_tid current_tid;
  52. korp_mutex wait_lock;
  53. korp_cond wait_cond;
  54. /* Last stopped thread, it should be set to NULL when sending
  55. * out the thread stop reply */
  56. WASMExecEnv *volatile stopped_thread;
  57. /* Currently status of the debug instance, it will be set to
  58. * RUNNING when receiving STEP/CONTINUE commands, and set to
  59. * STOPPED when any thread stopped */
  60. volatile debug_state_t current_state;
  61. /* Execution memory info. During debugging, the debug client may request to
  62. * malloc a memory space to evaluate user expressions. We preserve a buffer
  63. * during creating debug instance, and use a simple bump pointer allocator
  64. * to serve lldb's memory request */
  65. WASMDebugExecutionMemory exec_mem_info;
  66. };
  67. typedef enum WASMDebugEventKind {
  68. BREAK_POINT_ADD,
  69. BREAK_POINT_REMOVE
  70. } WASMDebugEventKind;
  71. typedef struct WASMDebugEvent {
  72. WASMDebugEventKind kind;
  73. unsigned char metadata[0];
  74. } WASMDebugEvent;
  75. typedef struct WASMDebugMemoryInfo {
  76. uint64 start;
  77. uint64 size;
  78. char name[128];
  79. char permisson[4];
  80. } WASMDebugMemoryInfo;
  81. typedef enum WasmAddressType {
  82. WasmMemory = 0x00,
  83. WasmObj = 0x01,
  84. WasmInvalid = 0x03
  85. } WasmAddressType;
  86. #define WASM_ADDR(type, id, offset) \
  87. (((uint64)type << 62) | ((uint64)0 << 32) | ((uint64)offset << 0))
  88. #define WASM_ADDR_TYPE(addr) (((addr)&0xC000000000000000) >> 62)
  89. #define WASM_ADDR_OFFSET(addr) (((addr)&0x00000000FFFFFFFF))
  90. #define INVALIED_ADDR (0xFFFFFFFFFFFFFFFF)
  91. void
  92. on_thread_stop_event(WASMDebugInstance *debug_inst, WASMExecEnv *exec_env);
  93. WASMDebugInstance *
  94. wasm_debug_instance_create(WASMCluster *cluster, int32 port);
  95. void
  96. wasm_debug_instance_destroy(WASMCluster *cluster);
  97. WASMDebugInstance *
  98. wasm_exec_env_get_instance(WASMExecEnv *exec_env);
  99. bool
  100. wasm_debug_engine_init(char *ip_addr, int32 process_port);
  101. void
  102. wasm_debug_engine_destroy();
  103. WASMExecEnv *
  104. wasm_debug_instance_get_current_env(WASMDebugInstance *instance);
  105. uint64
  106. wasm_debug_instance_get_pid(WASMDebugInstance *instance);
  107. korp_tid
  108. wasm_debug_instance_get_tid(WASMDebugInstance *instance);
  109. uint32
  110. wasm_debug_instance_get_tids(WASMDebugInstance *instance, korp_tid tids[],
  111. uint32 len);
  112. void
  113. wasm_debug_instance_set_cur_thread(WASMDebugInstance *instance, korp_tid tid);
  114. uint64
  115. wasm_debug_instance_get_pc(WASMDebugInstance *instance);
  116. uint64
  117. wasm_debug_instance_get_load_addr(WASMDebugInstance *instance);
  118. WASMDebugMemoryInfo *
  119. wasm_debug_instance_get_memregion(WASMDebugInstance *instance, uint64 addr);
  120. void
  121. wasm_debug_instance_destroy_memregion(WASMDebugInstance *instance,
  122. WASMDebugMemoryInfo *mem_info);
  123. bool
  124. wasm_debug_instance_get_obj_mem(WASMDebugInstance *instance, uint64 addr,
  125. char *buf, uint64 *size);
  126. bool
  127. wasm_debug_instance_get_linear_mem(WASMDebugInstance *instance, uint64 addr,
  128. char *buf, uint64 *size);
  129. bool
  130. wasm_debug_instance_get_mem(WASMDebugInstance *instance, uint64 addr, char *buf,
  131. uint64 *size);
  132. bool
  133. wasm_debug_instance_set_mem(WASMDebugInstance *instance, uint64 addr, char *buf,
  134. uint64 *size);
  135. uint32
  136. wasm_debug_instance_get_call_stack_pcs(WASMDebugInstance *instance,
  137. korp_tid tid, uint64 buf[], uint64 size);
  138. bool
  139. wasm_debug_instance_add_breakpoint(WASMDebugInstance *instance, uint64 addr,
  140. uint64 length);
  141. bool
  142. wasm_debug_instance_remove_breakpoint(WASMDebugInstance *instance, uint64 addr,
  143. uint64 length);
  144. bool
  145. wasm_debug_instance_interrupt_all_threads(WASMDebugInstance *instance);
  146. bool
  147. wasm_debug_instance_continue(WASMDebugInstance *instance);
  148. bool
  149. wasm_debug_instance_detach(WASMDebugInstance *instance);
  150. bool
  151. wasm_debug_instance_kill(WASMDebugInstance *instance);
  152. uint32
  153. wasm_debug_instance_get_thread_status(WASMDebugInstance *instance,
  154. korp_tid tid);
  155. bool
  156. wasm_debug_instance_singlestep(WASMDebugInstance *instance, korp_tid tid);
  157. bool
  158. wasm_debug_instance_get_local(WASMDebugInstance *instance, int32 frame_index,
  159. int32 local_index, char buf[], int32 *size);
  160. bool
  161. wasm_debug_instance_get_global(WASMDebugInstance *instance, int32 frame_index,
  162. int32 global_index, char buf[], int32 *size);
  163. #if WASM_ENABLE_LIBC_WASI != 0
  164. bool
  165. wasm_debug_instance_get_current_object_name(WASMDebugInstance *instance,
  166. char name_buffer[], uint32 len);
  167. #endif
  168. uint64
  169. wasm_debug_instance_mmap(WASMDebugInstance *instance, uint32 size,
  170. int32 map_prot);
  171. bool
  172. wasm_debug_instance_ummap(WASMDebugInstance *instance, uint64 addr);
  173. #endif