debug_engine.c 33 KB

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