debug_engine.c 37 KB

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