debug_engine.h 6.0 KB

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