debug_engine.c 42 KB

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