debug_engine.c 39 KB

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