debug_engine.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. /*
  2. * Copyright (C) 2021 Ant Group. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "debug_engine.h"
  6. #include "bh_log.h"
  7. #include "gdbserver.h"
  8. #include "platform_api_extension.h"
  9. #include "wasm_interp.h"
  10. #include "wasm_opcode.h"
  11. #include "wasm_runtime.h"
  12. static uint8 break_instr[] = { DEBUG_OP_BREAK };
  13. typedef struct WASMDebugEngine {
  14. struct WASMDebugEngine *next;
  15. WASMDebugControlThread *control_thread;
  16. char ip_addr[128];
  17. int platform_port;
  18. int process_base_port;
  19. bh_list debug_instance_list;
  20. bool active;
  21. } WASMDebugEngine;
  22. static WASMDebugEngine *g_debug_engine;
  23. static bool
  24. should_stop(WASMDebugControlThread *control_thread)
  25. {
  26. return control_thread->status != RUNNING;
  27. }
  28. static void *
  29. control_thread_routine(void *arg)
  30. {
  31. WASMDebugInstance *debug_inst = (WASMDebugInstance *)arg;
  32. WASMDebugControlThread *control_thread = NULL;
  33. WASMCluster *cluster = NULL;
  34. WASMExecEnv *exec_env;
  35. bh_assert(debug_inst);
  36. control_thread = debug_inst->control_thread;
  37. bh_assert(control_thread);
  38. cluster = debug_inst->cluster;
  39. bh_assert(cluster);
  40. exec_env = bh_list_first_elem(&cluster->exec_env_list);
  41. bh_assert(exec_env);
  42. os_mutex_lock(&exec_env->wait_lock);
  43. control_thread->status = RUNNING;
  44. debug_inst->id = g_debug_engine->debug_instance_list.len + 1;
  45. control_thread->debug_engine = g_debug_engine;
  46. control_thread->debug_instance = debug_inst;
  47. strcpy(control_thread->ip_addr, g_debug_engine->ip_addr);
  48. control_thread->port =
  49. (g_debug_engine->process_base_port == 0)
  50. ? 0
  51. : g_debug_engine->process_base_port + debug_inst->id;
  52. LOG_WARNING("control thread of debug object %p start\n", debug_inst);
  53. control_thread->server =
  54. wasm_launch_gdbserver(control_thread->ip_addr, control_thread->port);
  55. if (!control_thread->server) {
  56. LOG_ERROR("Failed to create debug server\n");
  57. os_cond_signal(&exec_env->wait_cond);
  58. os_mutex_unlock(&exec_env->wait_lock);
  59. return NULL;
  60. }
  61. control_thread->server->thread = control_thread;
  62. /* control thread ready, notify main thread */
  63. os_cond_signal(&exec_env->wait_cond);
  64. os_mutex_unlock(&exec_env->wait_lock);
  65. while (true) {
  66. os_mutex_lock(&control_thread->wait_lock);
  67. if (!should_stop(control_thread)) {
  68. if (!wasm_gdbserver_handle_packet(control_thread->server)) {
  69. control_thread->status = STOPPED;
  70. }
  71. }
  72. else {
  73. os_mutex_unlock(&control_thread->wait_lock);
  74. break;
  75. }
  76. os_mutex_unlock(&control_thread->wait_lock);
  77. }
  78. LOG_VERBOSE("control thread of debug object %p stop\n", debug_inst);
  79. return NULL;
  80. }
  81. static WASMDebugControlThread *
  82. wasm_debug_control_thread_create(WASMDebugInstance *debug_instance)
  83. {
  84. WASMDebugControlThread *control_thread;
  85. WASMCluster *cluster = debug_instance->cluster;
  86. WASMExecEnv *exec_env;
  87. bh_assert(cluster);
  88. exec_env = bh_list_first_elem(&cluster->exec_env_list);
  89. bh_assert(exec_env);
  90. if (!(control_thread =
  91. wasm_runtime_malloc(sizeof(WASMDebugControlThread)))) {
  92. LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
  93. return NULL;
  94. }
  95. memset(control_thread, 0, sizeof(WASMDebugControlThread));
  96. if (os_mutex_init(&control_thread->wait_lock) != 0)
  97. goto fail;
  98. debug_instance->control_thread = control_thread;
  99. os_mutex_lock(&exec_env->wait_lock);
  100. if (0
  101. != os_thread_create(&control_thread->tid, control_thread_routine,
  102. debug_instance, APP_THREAD_STACK_SIZE_MAX)) {
  103. os_mutex_unlock(&control_thread->wait_lock);
  104. goto fail1;
  105. }
  106. /* wait until the debug control thread ready */
  107. os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock);
  108. os_mutex_unlock(&exec_env->wait_lock);
  109. if (!control_thread->server)
  110. goto fail1;
  111. /* create control thread success, append debug instance to debug engine */
  112. bh_list_insert(&g_debug_engine->debug_instance_list, debug_instance);
  113. wasm_cluster_send_signal_all(debug_instance->cluster, WAMR_SIG_STOP);
  114. return control_thread;
  115. fail1:
  116. os_mutex_destroy(&control_thread->wait_lock);
  117. fail:
  118. wasm_runtime_free(control_thread);
  119. return NULL;
  120. }
  121. static void
  122. wasm_debug_control_thread_destroy(WASMDebugInstance *debug_instance)
  123. {
  124. WASMDebugControlThread *control_thread = debug_instance->control_thread;
  125. LOG_VERBOSE("control thread of debug object %p stop\n", debug_instance);
  126. control_thread->status = STOPPED;
  127. os_mutex_lock(&control_thread->wait_lock);
  128. wasm_close_gdbserver(control_thread->server);
  129. os_mutex_unlock(&control_thread->wait_lock);
  130. os_thread_join(control_thread->tid, NULL);
  131. wasm_runtime_free(control_thread->server);
  132. os_mutex_destroy(&control_thread->wait_lock);
  133. wasm_runtime_free(control_thread);
  134. }
  135. static WASMDebugEngine *
  136. wasm_debug_engine_create()
  137. {
  138. WASMDebugEngine *engine;
  139. if (!(engine = wasm_runtime_malloc(sizeof(WASMDebugEngine)))) {
  140. LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
  141. return NULL;
  142. }
  143. memset(engine, 0, sizeof(WASMDebugEngine));
  144. /* TODO: support Wasm platform in LLDB */
  145. /*
  146. engine->control_thread =
  147. wasm_debug_control_thread_create((WASMDebugObject *)engine);
  148. engine->control_thread->debug_engine = (WASMDebugObject *)engine;
  149. engine->control_thread->debug_instance = NULL;
  150. sprintf(engine->control_thread->ip_addr, "127.0.0.1");
  151. engine->control_thread->port = 1234;
  152. */
  153. bh_list_init(&engine->debug_instance_list);
  154. return engine;
  155. }
  156. bool
  157. wasm_debug_engine_init(char *ip_addr, int platform_port, int process_port)
  158. {
  159. if (g_debug_engine == NULL)
  160. g_debug_engine = wasm_debug_engine_create();
  161. if (g_debug_engine) {
  162. process_port -= 1;
  163. g_debug_engine->platform_port =
  164. platform_port > 0 ? platform_port : 1234;
  165. g_debug_engine->process_base_port =
  166. (process_port > 0) ? process_port : 0;
  167. if (ip_addr)
  168. sprintf(g_debug_engine->ip_addr, "%s", ip_addr);
  169. else
  170. sprintf(g_debug_engine->ip_addr, "%s", "127.0.0.1");
  171. g_debug_engine->active = true;
  172. }
  173. return g_debug_engine != NULL ? true : false;
  174. }
  175. void
  176. wasm_debug_set_engine_active(bool active)
  177. {
  178. if (g_debug_engine) {
  179. g_debug_engine->active = active;
  180. }
  181. }
  182. bool
  183. wasm_debug_get_engine_active(void)
  184. {
  185. if (g_debug_engine) {
  186. return g_debug_engine->active;
  187. }
  188. return false;
  189. }
  190. void
  191. wasm_debug_engine_destroy()
  192. {
  193. if (g_debug_engine) {
  194. wasm_runtime_free(g_debug_engine);
  195. g_debug_engine = NULL;
  196. }
  197. }
  198. /* A debug Instance is a debug "process" in gdb remote protocol
  199. and bound to a runtime cluster */
  200. WASMDebugInstance *
  201. wasm_debug_instance_create(WASMCluster *cluster)
  202. {
  203. WASMDebugInstance *instance;
  204. WASMExecEnv *exec_env;
  205. if (!g_debug_engine || !g_debug_engine->active) {
  206. return NULL;
  207. }
  208. if (!(instance = wasm_runtime_malloc(sizeof(WASMDebugInstance)))) {
  209. LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
  210. return NULL;
  211. }
  212. memset(instance, 0, sizeof(WASMDebugInstance));
  213. bh_list_init(&instance->break_point_list);
  214. instance->cluster = cluster;
  215. exec_env = bh_list_first_elem(&cluster->exec_env_list);
  216. bh_assert(exec_env);
  217. instance->current_tid = exec_env->handle;
  218. if (!wasm_debug_control_thread_create(instance)) {
  219. LOG_ERROR("WASM Debug Engine error: failed to create control thread");
  220. wasm_runtime_free(instance);
  221. return NULL;
  222. }
  223. return instance;
  224. }
  225. static WASMDebugInstance *
  226. wasm_cluster_get_debug_instance(WASMDebugEngine *engine, WASMCluster *cluster)
  227. {
  228. WASMDebugInstance *instance =
  229. bh_list_first_elem(&engine->debug_instance_list);
  230. while (instance) {
  231. if (instance->cluster == cluster)
  232. return instance;
  233. instance = bh_list_elem_next(instance);
  234. }
  235. return instance;
  236. }
  237. static void
  238. wasm_debug_instance_destroy_breakpoints(WASMDebugInstance *instance)
  239. {
  240. WASMDebugBreakPoint *breakpoint, *next_bp;
  241. breakpoint = bh_list_first_elem(&instance->break_point_list);
  242. while (breakpoint) {
  243. next_bp = bh_list_elem_next(breakpoint);
  244. bh_list_remove(&instance->break_point_list, breakpoint);
  245. wasm_runtime_free(breakpoint);
  246. breakpoint = next_bp;
  247. }
  248. }
  249. void
  250. wasm_debug_instance_destroy(WASMCluster *cluster)
  251. {
  252. WASMDebugInstance *instance = NULL;
  253. if (!g_debug_engine) {
  254. return;
  255. }
  256. instance = wasm_cluster_get_debug_instance(g_debug_engine, cluster);
  257. if (instance) {
  258. /* destroy control thread */
  259. wasm_debug_control_thread_destroy(instance);
  260. bh_list_remove(&g_debug_engine->debug_instance_list, instance);
  261. /* destroy all breakpoints */
  262. wasm_debug_instance_destroy_breakpoints(instance);
  263. wasm_runtime_free(instance);
  264. }
  265. }
  266. static WASMExecEnv *
  267. wasm_debug_instance_get_current_env(WASMDebugInstance *instance)
  268. {
  269. WASMExecEnv *exec_env = NULL;
  270. if (instance) {
  271. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  272. while (exec_env) {
  273. if (exec_env->handle == instance->current_tid)
  274. break;
  275. exec_env = bh_list_elem_next(exec_env);
  276. }
  277. }
  278. return exec_env;
  279. }
  280. #if WASM_ENABLE_LIBC_WASI != 0
  281. bool
  282. wasm_debug_instance_get_current_object_name(WASMDebugInstance *instance,
  283. char name_buffer[], int len)
  284. {
  285. WASMExecEnv *exec_env;
  286. WASIArguments *wasi_args;
  287. WASMModuleInstance *module_inst;
  288. if (!instance)
  289. return false;
  290. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  291. if (!exec_env)
  292. return false;
  293. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  294. wasi_args = &module_inst->module->wasi_args;
  295. if (wasi_args && wasi_args->argc > 0) {
  296. char *argv_name = wasi_args->argv[0];
  297. int name_len = strlen(argv_name);
  298. printf("the module name is %s\n", argv_name);
  299. if (len - 1 >= name_len)
  300. strcpy(name_buffer, argv_name);
  301. else
  302. strcpy(name_buffer, argv_name + (name_len + 1 - len));
  303. return true;
  304. }
  305. return false;
  306. }
  307. #endif
  308. uint64
  309. wasm_debug_instance_get_pid(WASMDebugInstance *instance)
  310. {
  311. if (instance != NULL) {
  312. return (uint64)instance->id;
  313. }
  314. return (uint64)0;
  315. }
  316. uint64
  317. wasm_debug_instance_get_tid(WASMDebugInstance *instance)
  318. {
  319. if (instance != NULL) {
  320. return (uint64)instance->current_tid;
  321. }
  322. return (uint64)0;
  323. }
  324. int
  325. wasm_debug_instance_get_tids(WASMDebugInstance *instance, uint64 tids[],
  326. int len)
  327. {
  328. WASMExecEnv *exec_env;
  329. int i = 0;
  330. if (!instance)
  331. return 0;
  332. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  333. while (exec_env && i < len) {
  334. tids[i++] = exec_env->handle;
  335. exec_env = bh_list_elem_next(exec_env);
  336. }
  337. LOG_VERBOSE("find %d tids\n", i);
  338. return i;
  339. }
  340. uint64
  341. wasm_debug_instance_wait_thread(WASMDebugInstance *instance, uint64 tid,
  342. uint32 *status)
  343. {
  344. WASMExecEnv *exec_env;
  345. WASMExecEnv *last_exec_env = NULL;
  346. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  347. while (exec_env) {
  348. last_exec_env = exec_env;
  349. if (instance->current_tid != 0
  350. && last_exec_env->handle == instance->current_tid) {
  351. break;
  352. }
  353. exec_env = bh_list_elem_next(exec_env);
  354. }
  355. if (last_exec_env) {
  356. wasm_cluster_wait_thread_status(last_exec_env, status);
  357. if (instance->current_tid == 0)
  358. instance->current_tid = last_exec_env->handle;
  359. return last_exec_env->handle;
  360. }
  361. else {
  362. *status = ~0;
  363. return 0;
  364. }
  365. }
  366. void
  367. wasm_debug_instance_set_cur_thread(WASMDebugInstance *instance, uint64 tid)
  368. {
  369. instance->current_tid = tid;
  370. }
  371. uint64
  372. wasm_debug_instance_get_pc(WASMDebugInstance *instance)
  373. {
  374. WASMExecEnv *exec_env;
  375. if (!instance)
  376. return 0;
  377. exec_env = wasm_debug_instance_get_current_env(instance);
  378. if ((exec_env->cur_frame != NULL) && (exec_env->cur_frame->ip != NULL)) {
  379. WASMModuleInstance *module_inst =
  380. (WASMModuleInstance *)exec_env->module_inst;
  381. return WASM_ADDR(
  382. WasmObj, instance->id,
  383. (exec_env->cur_frame->ip - module_inst->module->load_addr));
  384. }
  385. return 0;
  386. }
  387. uint64
  388. wasm_debug_instance_get_load_addr(WASMDebugInstance *instance)
  389. {
  390. WASMExecEnv *exec_env;
  391. if (!instance)
  392. return WASM_ADDR(WasmInvalid, 0, 0);
  393. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  394. if (exec_env) {
  395. return WASM_ADDR(WasmObj, instance->id, 0);
  396. }
  397. return WASM_ADDR(WasmInvalid, 0, 0);
  398. }
  399. WASMDebugMemoryInfo *
  400. wasm_debug_instance_get_memregion(WASMDebugInstance *instance, uint64 addr)
  401. {
  402. WASMDebugMemoryInfo *mem_info;
  403. WASMExecEnv *exec_env;
  404. WASMModuleInstance *module_inst;
  405. WASMMemoryInstance *memory;
  406. uint32 num_bytes_per_page;
  407. uint32 linear_mem_size = 0;
  408. if (!instance)
  409. return NULL;
  410. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  411. if (!exec_env)
  412. return NULL;
  413. if (!(mem_info = wasm_runtime_malloc(sizeof(WASMDebugMemoryInfo)))) {
  414. LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
  415. return NULL;
  416. }
  417. memset(mem_info, 0, sizeof(WASMDebugMemoryInfo));
  418. mem_info->start = WASM_ADDR(WasmInvalid, 0, 0);
  419. mem_info->size = 0;
  420. mem_info->name[0] = '\0';
  421. mem_info->permisson[0] = '\0';
  422. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  423. switch (WASM_ADDR_TYPE(addr)) {
  424. case WasmObj:
  425. if (WASM_ADDR_OFFSET(addr) < module_inst->module->load_size) {
  426. mem_info->start = WASM_ADDR(WasmObj, instance->id, 0);
  427. mem_info->size = module_inst->module->load_size;
  428. sprintf(mem_info->name, "%s", "module");
  429. sprintf(mem_info->permisson, "%s", "rx");
  430. }
  431. break;
  432. case WasmMemory:
  433. {
  434. memory = module_inst->default_memory;
  435. if (memory) {
  436. num_bytes_per_page = memory->num_bytes_per_page;
  437. linear_mem_size = num_bytes_per_page * memory->cur_page_count;
  438. }
  439. if (WASM_ADDR_OFFSET(addr) < linear_mem_size) {
  440. mem_info->start = WASM_ADDR(WasmMemory, instance->id, 0);
  441. mem_info->size = linear_mem_size;
  442. sprintf(mem_info->name, "%s", "memory");
  443. sprintf(mem_info->permisson, "%s", "rw");
  444. }
  445. break;
  446. }
  447. default:
  448. mem_info->start = WASM_ADDR(WasmInvalid, 0, 0);
  449. mem_info->size = 0;
  450. }
  451. return mem_info;
  452. }
  453. void
  454. wasm_debug_instance_destroy_memregion(WASMDebugInstance *instance,
  455. WASMDebugMemoryInfo *mem_info)
  456. {
  457. wasm_runtime_free(mem_info);
  458. }
  459. bool
  460. wasm_debug_instance_get_obj_mem(WASMDebugInstance *instance, uint64 offset,
  461. char *buf, uint64 *size)
  462. {
  463. WASMExecEnv *exec_env;
  464. WASMModuleInstance *module_inst;
  465. if (!instance)
  466. return false;
  467. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  468. if (!exec_env)
  469. return false;
  470. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  471. if (offset + *size > module_inst->module->load_size) {
  472. LOG_VERBOSE("wasm_debug_instance_get_data_mem size over flow!\n");
  473. *size = module_inst->module->load_size >= offset
  474. ? module_inst->module->load_size - offset
  475. : 0;
  476. }
  477. bh_memcpy_s(buf, *size, module_inst->module->load_addr + offset, *size);
  478. WASMDebugBreakPoint *breakpoint =
  479. bh_list_first_elem(&instance->break_point_list);
  480. while (breakpoint) {
  481. if (offset <= breakpoint->addr && breakpoint->addr < offset + *size) {
  482. bh_memcpy_s(buf + (breakpoint->addr - offset), sizeof(break_instr),
  483. &breakpoint->orignal_data, sizeof(break_instr));
  484. }
  485. breakpoint = bh_list_elem_next(breakpoint);
  486. }
  487. WASMFastOPCodeNode *fast_opcode =
  488. bh_list_first_elem(&module_inst->module->fast_opcode_list);
  489. while (fast_opcode) {
  490. if (offset <= fast_opcode->offset
  491. && fast_opcode->offset < offset + *size) {
  492. *(uint8 *)(buf + (fast_opcode->offset - offset)) =
  493. fast_opcode->orig_op;
  494. }
  495. fast_opcode = bh_list_elem_next(fast_opcode);
  496. }
  497. return true;
  498. }
  499. bool
  500. wasm_debug_instance_get_linear_mem(WASMDebugInstance *instance, uint64 offset,
  501. char *buf, uint64 *size)
  502. {
  503. WASMExecEnv *exec_env;
  504. WASMModuleInstance *module_inst;
  505. WASMMemoryInstance *memory;
  506. uint32 num_bytes_per_page;
  507. uint32 linear_mem_size;
  508. if (!instance)
  509. return false;
  510. exec_env = wasm_debug_instance_get_current_env(instance);
  511. if (!exec_env)
  512. return false;
  513. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  514. memory = module_inst->default_memory;
  515. if (memory) {
  516. num_bytes_per_page = memory->num_bytes_per_page;
  517. linear_mem_size = num_bytes_per_page * memory->cur_page_count;
  518. if (offset + *size > linear_mem_size) {
  519. LOG_VERBOSE("wasm_debug_instance_get_linear_mem size over flow!\n");
  520. *size = linear_mem_size >= offset ? linear_mem_size - offset : 0;
  521. }
  522. bh_memcpy_s(buf, *size, memory->memory_data + offset, *size);
  523. return true;
  524. }
  525. return false;
  526. }
  527. bool
  528. wasm_debug_instance_set_linear_mem(WASMDebugInstance *instance, uint64 offset,
  529. char *buf, uint64 *size)
  530. {
  531. WASMExecEnv *exec_env;
  532. WASMModuleInstance *module_inst;
  533. WASMMemoryInstance *memory;
  534. uint32 num_bytes_per_page;
  535. uint32 linear_mem_size;
  536. if (!instance)
  537. return false;
  538. exec_env = wasm_debug_instance_get_current_env(instance);
  539. if (!exec_env)
  540. return false;
  541. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  542. memory = module_inst->default_memory;
  543. if (memory) {
  544. num_bytes_per_page = memory->num_bytes_per_page;
  545. linear_mem_size = num_bytes_per_page * memory->cur_page_count;
  546. if (offset + *size > linear_mem_size) {
  547. LOG_VERBOSE("wasm_debug_instance_get_linear_mem size over flow!\n");
  548. *size = linear_mem_size >= offset ? linear_mem_size - offset : 0;
  549. }
  550. bh_memcpy_s(memory->memory_data + offset, *size, buf, *size);
  551. return true;
  552. }
  553. return false;
  554. }
  555. bool
  556. wasm_debug_instance_get_mem(WASMDebugInstance *instance, uint64 addr, char *buf,
  557. uint64 *size)
  558. {
  559. switch (WASM_ADDR_TYPE(addr)) {
  560. case WasmMemory:
  561. return wasm_debug_instance_get_linear_mem(
  562. instance, WASM_ADDR_OFFSET(addr), buf, size);
  563. break;
  564. case WasmObj:
  565. return wasm_debug_instance_get_obj_mem(
  566. instance, WASM_ADDR_OFFSET(addr), buf, size);
  567. break;
  568. default:
  569. return false;
  570. }
  571. }
  572. bool
  573. wasm_debug_instance_set_mem(WASMDebugInstance *instance, uint64 addr, char *buf,
  574. uint64 *size)
  575. {
  576. switch (WASM_ADDR_TYPE(addr)) {
  577. case WasmMemory:
  578. return wasm_debug_instance_set_linear_mem(
  579. instance, WASM_ADDR_OFFSET(addr), buf, size);
  580. break;
  581. case WasmObj:
  582. default:
  583. return false;
  584. }
  585. }
  586. WASMDebugInstance *
  587. wasm_exec_env_get_instance(WASMExecEnv *exec_env)
  588. {
  589. WASMDebugInstance *instance = NULL;
  590. bh_assert(g_debug_engine);
  591. instance = bh_list_first_elem(&g_debug_engine->debug_instance_list);
  592. while (instance) {
  593. if (instance->cluster == exec_env->cluster)
  594. break;
  595. instance = bh_list_elem_next(instance);
  596. }
  597. return instance;
  598. }
  599. int
  600. wasm_debug_instance_get_call_stack_pcs(WASMDebugInstance *instance, uint64 tid,
  601. uint64 buf[], uint64 size)
  602. {
  603. WASMExecEnv *exec_env;
  604. struct WASMInterpFrame *frame;
  605. uint64 i = 0;
  606. if (!instance)
  607. return 0;
  608. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  609. while (exec_env) {
  610. if (exec_env->handle == tid) {
  611. WASMModuleInstance *module_inst =
  612. (WASMModuleInstance *)exec_env->module_inst;
  613. frame = exec_env->cur_frame;
  614. while (frame && i < size) {
  615. if (frame->ip != NULL) {
  616. buf[i++] =
  617. WASM_ADDR(WasmObj, instance->id,
  618. (frame->ip - module_inst->module->load_addr));
  619. }
  620. frame = frame->prev_frame;
  621. }
  622. return i;
  623. }
  624. exec_env = bh_list_elem_next(exec_env);
  625. }
  626. return 0;
  627. }
  628. bool
  629. wasm_debug_instance_add_breakpoint(WASMDebugInstance *instance, uint64 addr,
  630. uint64 length)
  631. {
  632. WASMExecEnv *exec_env;
  633. WASMModuleInstance *module_inst;
  634. uint64 offset;
  635. if (!instance)
  636. return false;
  637. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  638. if (!exec_env)
  639. return false;
  640. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  641. if (WASM_ADDR_TYPE(addr) != WasmObj)
  642. return false;
  643. offset = WASM_ADDR_OFFSET(addr);
  644. if (length >= sizeof(break_instr)) {
  645. if (offset + sizeof(break_instr) <= module_inst->module->load_size) {
  646. WASMDebugBreakPoint *breakpoint;
  647. if (!(breakpoint =
  648. wasm_runtime_malloc(sizeof(WASMDebugBreakPoint)))) {
  649. LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
  650. return false;
  651. }
  652. memset(breakpoint, 0, sizeof(WASMDebugBreakPoint));
  653. breakpoint->addr = offset;
  654. /* TODO: how to if more than one breakpoints are set
  655. at the same addr? */
  656. bh_memcpy_s(&breakpoint->orignal_data, (uint32)sizeof(break_instr),
  657. module_inst->module->load_addr + offset,
  658. (uint32)sizeof(break_instr));
  659. bh_memcpy_s(module_inst->module->load_addr + offset,
  660. (uint32)sizeof(break_instr), break_instr,
  661. (uint32)sizeof(break_instr));
  662. bh_list_insert(&instance->break_point_list, breakpoint);
  663. return true;
  664. }
  665. }
  666. return false;
  667. }
  668. bool
  669. wasm_debug_instance_remove_breakpoint(WASMDebugInstance *instance, uint64 addr,
  670. uint64 length)
  671. {
  672. WASMExecEnv *exec_env;
  673. WASMModuleInstance *module_inst;
  674. uint64 offset;
  675. if (!instance)
  676. return false;
  677. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  678. if (!exec_env)
  679. return false;
  680. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  681. if (WASM_ADDR_TYPE(addr) != WasmObj)
  682. return false;
  683. offset = WASM_ADDR_OFFSET(addr);
  684. if (length >= sizeof(break_instr)) {
  685. if (offset + sizeof(break_instr) <= module_inst->module->load_size) {
  686. WASMDebugBreakPoint *breakpoint =
  687. bh_list_first_elem(&instance->break_point_list);
  688. while (breakpoint) {
  689. WASMDebugBreakPoint *next_break = bh_list_elem_next(breakpoint);
  690. if (breakpoint->addr == offset) {
  691. /* TODO: how to if more than one breakpoints are set
  692. at the same addr? */
  693. bh_memcpy_s(module_inst->module->load_addr + offset,
  694. (uint32)sizeof(break_instr),
  695. &breakpoint->orignal_data,
  696. (uint32)sizeof(break_instr));
  697. bh_list_remove(&instance->break_point_list, breakpoint);
  698. wasm_runtime_free(breakpoint);
  699. }
  700. breakpoint = next_break;
  701. }
  702. }
  703. }
  704. return true;
  705. }
  706. bool
  707. wasm_debug_instance_continue(WASMDebugInstance *instance)
  708. {
  709. WASMExecEnv *exec_env;
  710. if (!instance)
  711. return false;
  712. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  713. if (!exec_env)
  714. return false;
  715. while (exec_env) {
  716. wasm_cluster_thread_continue(exec_env);
  717. exec_env = bh_list_elem_next(exec_env);
  718. }
  719. return true;
  720. }
  721. bool
  722. wasm_debug_instance_kill(WASMDebugInstance *instance)
  723. {
  724. WASMExecEnv *exec_env;
  725. if (!instance)
  726. return false;
  727. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  728. if (!exec_env)
  729. return false;
  730. while (exec_env) {
  731. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TERM);
  732. exec_env = bh_list_elem_next(exec_env);
  733. }
  734. return true;
  735. }
  736. bool
  737. wasm_debug_instance_singlestep(WASMDebugInstance *instance, uint64 tid)
  738. {
  739. WASMExecEnv *exec_env;
  740. if (!instance)
  741. return false;
  742. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  743. if (!exec_env)
  744. return false;
  745. while (exec_env) {
  746. if (exec_env->handle == tid || tid == (uint64)~0) {
  747. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_SINGSTEP);
  748. wasm_cluster_thread_step(exec_env);
  749. }
  750. exec_env = bh_list_elem_next(exec_env);
  751. }
  752. return true;
  753. }
  754. bool
  755. wasm_debug_instance_get_local(WASMDebugInstance *instance, int frame_index,
  756. int local_index, char buf[], int *size)
  757. {
  758. WASMExecEnv *exec_env;
  759. struct WASMInterpFrame *frame;
  760. WASMFunctionInstance *cur_func;
  761. uint8 local_type = 0xFF;
  762. uint32 local_offset;
  763. int param_count;
  764. int fi = 0;
  765. if (!instance)
  766. return false;
  767. exec_env = wasm_debug_instance_get_current_env(instance);
  768. if (!exec_env)
  769. return false;
  770. frame = exec_env->cur_frame;
  771. while (frame && fi++ != frame_index) {
  772. frame = frame->prev_frame;
  773. }
  774. if (!frame)
  775. return false;
  776. cur_func = frame->function;
  777. if (!cur_func)
  778. return false;
  779. param_count = cur_func->param_count;
  780. if (local_index >= param_count + cur_func->local_count)
  781. return false;
  782. local_offset = cur_func->local_offsets[local_index];
  783. if (local_index < param_count)
  784. local_type = cur_func->param_types[local_index];
  785. else if (local_index < cur_func->local_count + param_count)
  786. local_type = cur_func->local_types[local_index - param_count];
  787. switch (local_type) {
  788. case VALUE_TYPE_I32:
  789. case VALUE_TYPE_F32:
  790. *size = 4;
  791. bh_memcpy_s(buf, 4, (char *)(frame->lp + local_offset), 4);
  792. break;
  793. case VALUE_TYPE_I64:
  794. case VALUE_TYPE_F64:
  795. *size = 8;
  796. bh_memcpy_s(buf, 8, (char *)(frame->lp + local_offset), 8);
  797. break;
  798. default:
  799. *size = 0;
  800. break;
  801. }
  802. return true;
  803. }
  804. bool
  805. wasm_debug_instance_get_global(WASMDebugInstance *instance, int frame_index,
  806. int global_index, char buf[], int *size)
  807. {
  808. WASMExecEnv *exec_env;
  809. struct WASMInterpFrame *frame;
  810. WASMModuleInstance *module_inst;
  811. WASMGlobalInstance *globals, *global;
  812. uint8 *global_addr;
  813. uint8 global_type = 0xFF;
  814. uint8 *global_data;
  815. int fi = 0;
  816. if (!instance)
  817. return false;
  818. exec_env = wasm_debug_instance_get_current_env(instance);
  819. if (!exec_env)
  820. return false;
  821. frame = exec_env->cur_frame;
  822. while (frame && fi++ != frame_index) {
  823. frame = frame->prev_frame;
  824. }
  825. if (!frame)
  826. return false;
  827. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  828. global_data = module_inst->global_data;
  829. globals = module_inst->globals;
  830. if ((global_index < 0)
  831. || ((uint32)global_index >= module_inst->global_count)) {
  832. return false;
  833. }
  834. global = globals + global_index;
  835. #if WASM_ENABLE_MULTI_MODULE == 0
  836. global_addr = global_data + global->data_offset;
  837. #else
  838. global_addr = global->import_global_inst
  839. ? global->import_module_inst->global_data
  840. + global->import_global_inst->data_offset
  841. : global_data + global->data_offset;
  842. #endif
  843. global_type = global->type;
  844. switch (global_type) {
  845. case VALUE_TYPE_I32:
  846. case VALUE_TYPE_F32:
  847. *size = 4;
  848. bh_memcpy_s(buf, 4, (char *)(global_addr), 4);
  849. break;
  850. case VALUE_TYPE_I64:
  851. case VALUE_TYPE_F64:
  852. *size = 8;
  853. bh_memcpy_s(buf, 8, (char *)(global_addr), 8);
  854. break;
  855. default:
  856. *size = 0;
  857. break;
  858. }
  859. return true;
  860. }
  861. uint64
  862. wasm_debug_instance_mmap(WASMDebugInstance *instance, uint32 size, int map_port)
  863. {
  864. WASMExecEnv *exec_env;
  865. WASMModuleInstance *module_inst;
  866. uint32 offset;
  867. void *native_addr;
  868. (void)map_port;
  869. if (!instance)
  870. return 0;
  871. exec_env = wasm_debug_instance_get_current_env(instance);
  872. if (!exec_env)
  873. return 0;
  874. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  875. /* TODO: malloc in wasi libc maybe not be thread safe, we hope LLDB will
  876. always ask for memory when threads stopped */
  877. offset = wasm_runtime_module_malloc((wasm_module_inst_t)module_inst, size,
  878. &native_addr);
  879. if (!offset)
  880. LOG_WARNING("the memory may be not enough for debug, try use larger "
  881. "--heap-size");
  882. return WASM_ADDR(WasmMemory, 0, offset);
  883. }
  884. bool
  885. wasm_debug_instance_ummap(WASMDebugInstance *instance, uint64 addr)
  886. {
  887. WASMExecEnv *exec_env;
  888. WASMModuleInstance *module_inst;
  889. uint32 offset;
  890. if (!instance)
  891. return false;
  892. exec_env = wasm_debug_instance_get_current_env(instance);
  893. if (!exec_env)
  894. return false;
  895. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  896. if (WASM_ADDR_TYPE(addr) == WasmMemory) {
  897. offset = WASM_ADDR_OFFSET(addr);
  898. wasm_runtime_module_free((wasm_module_inst_t)module_inst, offset);
  899. return true;
  900. }
  901. return false;
  902. }