debug_engine.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  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(void)
  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(void)
  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(void)
  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. bh_list_init(&instance->watch_point_list_read);
  326. bh_list_init(&instance->watch_point_list_write);
  327. instance->cluster = cluster;
  328. exec_env = bh_list_first_elem(&cluster->exec_env_list);
  329. bh_assert(exec_env);
  330. instance->current_tid = exec_env->handle;
  331. module_inst = wasm_runtime_get_module_inst(exec_env);
  332. bh_assert(module_inst);
  333. /* Allocate linear memory for evaluating expressions during debugging. If
  334. * the allocation failed, the debugger will not be able to evaluate
  335. * expressions */
  336. instance->exec_mem_info.size = DEBUG_EXECUTION_MEMORY_SIZE;
  337. instance->exec_mem_info.start_offset = wasm_runtime_module_malloc(
  338. module_inst, (uint64)instance->exec_mem_info.size, NULL);
  339. if (instance->exec_mem_info.start_offset == 0) {
  340. LOG_WARNING(
  341. "WASM Debug Engine warning: failed to allocate linear memory for "
  342. "execution. \n"
  343. "Will not be able to evaluate expressions during "
  344. "debugging");
  345. }
  346. instance->exec_mem_info.current_pos = instance->exec_mem_info.start_offset;
  347. if (!wasm_debug_control_thread_create(instance, port)) {
  348. LOG_ERROR("WASM Debug Engine error: failed to create control thread");
  349. goto fail3;
  350. }
  351. wasm_cluster_set_debug_inst(cluster, instance);
  352. return instance;
  353. fail3:
  354. os_cond_destroy(&instance->wait_cond);
  355. fail2:
  356. os_mutex_destroy(&instance->wait_lock);
  357. fail1:
  358. wasm_runtime_free(instance);
  359. return NULL;
  360. }
  361. static void
  362. wasm_debug_instance_destroy_breakpoints(WASMDebugInstance *instance)
  363. {
  364. WASMDebugBreakPoint *breakpoint, *next_bp;
  365. breakpoint = bh_list_first_elem(&instance->break_point_list);
  366. while (breakpoint) {
  367. next_bp = bh_list_elem_next(breakpoint);
  368. bh_list_remove(&instance->break_point_list, breakpoint);
  369. wasm_runtime_free(breakpoint);
  370. breakpoint = next_bp;
  371. }
  372. }
  373. static void
  374. wasm_debug_instance_destroy_watchpoints(WASMDebugInstance *instance,
  375. bh_list *watchpoints)
  376. {
  377. WASMDebugWatchPoint *watchpoint, *next;
  378. watchpoint = bh_list_first_elem(watchpoints);
  379. while (watchpoint) {
  380. next = bh_list_elem_next(watchpoint);
  381. bh_list_remove(watchpoints, watchpoint);
  382. wasm_runtime_free(watchpoint);
  383. watchpoint = next;
  384. }
  385. }
  386. void
  387. wasm_debug_instance_destroy(WASMCluster *cluster)
  388. {
  389. WASMDebugInstance *instance = NULL;
  390. if (!g_debug_engine) {
  391. return;
  392. }
  393. instance = cluster->debug_inst;
  394. if (instance) {
  395. /* destroy control thread */
  396. wasm_debug_control_thread_destroy(instance);
  397. os_mutex_lock(&g_debug_engine->instance_list_lock);
  398. bh_list_remove(&g_debug_engine->debug_instance_list, instance);
  399. os_mutex_unlock(&g_debug_engine->instance_list_lock);
  400. /* destroy all breakpoints */
  401. wasm_debug_instance_destroy_breakpoints(instance);
  402. wasm_debug_instance_destroy_watchpoints(
  403. instance, &instance->watch_point_list_read);
  404. wasm_debug_instance_destroy_watchpoints(
  405. instance, &instance->watch_point_list_write);
  406. os_mutex_destroy(&instance->wait_lock);
  407. os_cond_destroy(&instance->wait_cond);
  408. wasm_runtime_free(instance);
  409. cluster->debug_inst = NULL;
  410. }
  411. }
  412. WASMExecEnv *
  413. wasm_debug_instance_get_current_env(WASMDebugInstance *instance)
  414. {
  415. WASMExecEnv *exec_env = NULL;
  416. if (instance) {
  417. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  418. while (exec_env) {
  419. if (exec_env->handle == instance->current_tid)
  420. break;
  421. exec_env = bh_list_elem_next(exec_env);
  422. }
  423. }
  424. return exec_env;
  425. }
  426. #if WASM_ENABLE_LIBC_WASI != 0
  427. bool
  428. wasm_debug_instance_get_current_object_name(WASMDebugInstance *instance,
  429. char name_buffer[], uint32 len)
  430. {
  431. WASMExecEnv *exec_env;
  432. WASIArguments *wasi_args;
  433. WASMModuleInstance *module_inst;
  434. if (!instance)
  435. return false;
  436. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  437. if (!exec_env)
  438. return false;
  439. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  440. wasi_args = &module_inst->module->wasi_args;
  441. if (wasi_args && wasi_args->argc > 0) {
  442. char *argv_name = wasi_args->argv[0];
  443. uint32 name_len = (uint32)strlen(argv_name);
  444. printf("the module name is %s\n", argv_name);
  445. if (len - 1 >= name_len)
  446. bh_strcpy_s(name_buffer, len, argv_name);
  447. else
  448. bh_strcpy_s(name_buffer, len, argv_name + (name_len + 1 - len));
  449. return true;
  450. }
  451. return false;
  452. }
  453. #endif
  454. uint64
  455. wasm_debug_instance_get_pid(WASMDebugInstance *instance)
  456. {
  457. if (instance != NULL) {
  458. return (uint64)instance->id;
  459. }
  460. return (uint64)0;
  461. }
  462. korp_tid
  463. wasm_debug_instance_get_tid(WASMDebugInstance *instance)
  464. {
  465. if (instance != NULL) {
  466. return instance->current_tid;
  467. }
  468. return (korp_tid)(uintptr_t)0;
  469. }
  470. uint32
  471. wasm_debug_instance_get_tids(WASMDebugInstance *instance, korp_tid tids[],
  472. uint32 len)
  473. {
  474. WASMExecEnv *exec_env;
  475. uint32 i = 0, threads_num = 0;
  476. if (!instance)
  477. return 0;
  478. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  479. while (exec_env && i < len) {
  480. /* Some threads may not be ready */
  481. if (exec_env->handle != 0) {
  482. tids[i++] = exec_env->handle;
  483. threads_num++;
  484. }
  485. exec_env = bh_list_elem_next(exec_env);
  486. }
  487. LOG_VERBOSE("find %d tids\n", threads_num);
  488. return threads_num;
  489. }
  490. uint32
  491. wasm_debug_instance_get_thread_status(WASMDebugInstance *instance, korp_tid tid)
  492. {
  493. WASMExecEnv *exec_env = NULL;
  494. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  495. while (exec_env) {
  496. if (exec_env->handle == tid) {
  497. return (uint32)exec_env->current_status->signal_flag;
  498. }
  499. exec_env = bh_list_elem_next(exec_env);
  500. }
  501. return 0;
  502. }
  503. void
  504. wasm_debug_instance_set_cur_thread(WASMDebugInstance *instance, korp_tid tid)
  505. {
  506. instance->current_tid = tid;
  507. }
  508. uint64
  509. wasm_debug_instance_get_pc(WASMDebugInstance *instance)
  510. {
  511. WASMExecEnv *exec_env;
  512. if (!instance)
  513. return 0;
  514. exec_env = wasm_debug_instance_get_current_env(instance);
  515. if ((exec_env != NULL) && (exec_env->cur_frame != NULL)
  516. && (exec_env->cur_frame->ip != NULL)) {
  517. WASMModuleInstance *module_inst =
  518. (WASMModuleInstance *)exec_env->module_inst;
  519. return WASM_ADDR(
  520. WasmObj, instance->id,
  521. (exec_env->cur_frame->ip - module_inst->module->load_addr));
  522. }
  523. return 0;
  524. }
  525. uint64
  526. wasm_debug_instance_get_load_addr(WASMDebugInstance *instance)
  527. {
  528. WASMExecEnv *exec_env;
  529. if (!instance)
  530. return WASM_ADDR(WasmInvalid, 0, 0);
  531. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  532. if (exec_env) {
  533. return WASM_ADDR(WasmObj, instance->id, 0);
  534. }
  535. return WASM_ADDR(WasmInvalid, 0, 0);
  536. }
  537. WASMDebugMemoryInfo *
  538. wasm_debug_instance_get_memregion(WASMDebugInstance *instance, uint64 addr)
  539. {
  540. WASMDebugMemoryInfo *mem_info;
  541. WASMExecEnv *exec_env;
  542. WASMModuleInstance *module_inst;
  543. WASMMemoryInstance *memory;
  544. uint32 num_bytes_per_page;
  545. uint32 linear_mem_size = 0;
  546. if (!instance)
  547. return NULL;
  548. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  549. if (!exec_env)
  550. return NULL;
  551. if (!(mem_info = wasm_runtime_malloc(sizeof(WASMDebugMemoryInfo)))) {
  552. LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
  553. return NULL;
  554. }
  555. memset(mem_info, 0, sizeof(WASMDebugMemoryInfo));
  556. mem_info->start = WASM_ADDR(WasmInvalid, 0, 0);
  557. mem_info->size = 0;
  558. mem_info->name[0] = '\0';
  559. mem_info->permisson[0] = '\0';
  560. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  561. switch (WASM_ADDR_TYPE(addr)) {
  562. case WasmObj:
  563. if (WASM_ADDR_OFFSET(addr) < module_inst->module->load_size) {
  564. mem_info->start = WASM_ADDR(WasmObj, instance->id, 0);
  565. mem_info->size = module_inst->module->load_size;
  566. snprintf(mem_info->name, sizeof(mem_info->name), "%s",
  567. "module");
  568. snprintf(mem_info->permisson, sizeof(mem_info->permisson), "%s",
  569. "rx");
  570. }
  571. break;
  572. case WasmMemory:
  573. {
  574. memory = wasm_get_default_memory(module_inst);
  575. if (memory) {
  576. num_bytes_per_page = memory->num_bytes_per_page;
  577. linear_mem_size = num_bytes_per_page * memory->cur_page_count;
  578. }
  579. if (WASM_ADDR_OFFSET(addr) < linear_mem_size) {
  580. mem_info->start = WASM_ADDR(WasmMemory, instance->id, 0);
  581. mem_info->size = linear_mem_size;
  582. snprintf(mem_info->name, sizeof(mem_info->name), "%s",
  583. "memory");
  584. snprintf(mem_info->permisson, sizeof(mem_info->permisson), "%s",
  585. "rw");
  586. }
  587. break;
  588. }
  589. default:
  590. mem_info->start = WASM_ADDR(WasmInvalid, 0, 0);
  591. mem_info->size = 0;
  592. }
  593. return mem_info;
  594. }
  595. void
  596. wasm_debug_instance_destroy_memregion(WASMDebugInstance *instance,
  597. WASMDebugMemoryInfo *mem_info)
  598. {
  599. wasm_runtime_free(mem_info);
  600. }
  601. bool
  602. wasm_debug_instance_get_obj_mem(WASMDebugInstance *instance, uint64 offset,
  603. char *buf, uint64 *size)
  604. {
  605. WASMExecEnv *exec_env;
  606. WASMModuleInstance *module_inst;
  607. WASMDebugBreakPoint *breakpoint;
  608. WASMFastOPCodeNode *fast_opcode;
  609. if (!instance)
  610. return false;
  611. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  612. if (!exec_env)
  613. return false;
  614. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  615. if (offset + *size > module_inst->module->load_size) {
  616. LOG_VERBOSE("wasm_debug_instance_get_data_mem size overflow!\n");
  617. *size = module_inst->module->load_size >= offset
  618. ? module_inst->module->load_size - offset
  619. : 0;
  620. }
  621. bh_memcpy_s(buf, (uint32)*size, module_inst->module->load_addr + offset,
  622. (uint32)*size);
  623. breakpoint = bh_list_first_elem(&instance->break_point_list);
  624. while (breakpoint) {
  625. if (offset <= breakpoint->addr && breakpoint->addr < offset + *size) {
  626. bh_memcpy_s(buf + (breakpoint->addr - offset), sizeof(break_instr),
  627. &breakpoint->orignal_data, sizeof(break_instr));
  628. }
  629. breakpoint = bh_list_elem_next(breakpoint);
  630. }
  631. fast_opcode = bh_list_first_elem(&module_inst->module->fast_opcode_list);
  632. while (fast_opcode) {
  633. if (offset <= fast_opcode->offset
  634. && fast_opcode->offset < offset + *size) {
  635. *(uint8 *)(buf + (fast_opcode->offset - offset)) =
  636. fast_opcode->orig_op;
  637. }
  638. fast_opcode = bh_list_elem_next(fast_opcode);
  639. }
  640. return true;
  641. }
  642. bool
  643. wasm_debug_instance_get_linear_mem(WASMDebugInstance *instance, uint64 offset,
  644. char *buf, uint64 *size)
  645. {
  646. WASMExecEnv *exec_env;
  647. WASMModuleInstance *module_inst;
  648. WASMMemoryInstance *memory;
  649. uint32 num_bytes_per_page;
  650. uint32 linear_mem_size;
  651. if (!instance)
  652. return false;
  653. exec_env = wasm_debug_instance_get_current_env(instance);
  654. if (!exec_env)
  655. return false;
  656. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  657. memory = wasm_get_default_memory(module_inst);
  658. if (memory) {
  659. num_bytes_per_page = memory->num_bytes_per_page;
  660. linear_mem_size = num_bytes_per_page * memory->cur_page_count;
  661. if (offset + *size > linear_mem_size) {
  662. LOG_VERBOSE("wasm_debug_instance_get_linear_mem size overflow!\n");
  663. *size = linear_mem_size >= offset ? linear_mem_size - offset : 0;
  664. }
  665. bh_memcpy_s(buf, (uint32)*size, memory->memory_data + offset,
  666. (uint32)*size);
  667. return true;
  668. }
  669. return false;
  670. }
  671. bool
  672. wasm_debug_instance_set_linear_mem(WASMDebugInstance *instance, uint64 offset,
  673. char *buf, uint64 *size)
  674. {
  675. WASMExecEnv *exec_env;
  676. WASMModuleInstance *module_inst;
  677. WASMMemoryInstance *memory;
  678. uint32 num_bytes_per_page;
  679. uint32 linear_mem_size;
  680. if (!instance)
  681. return false;
  682. exec_env = wasm_debug_instance_get_current_env(instance);
  683. if (!exec_env)
  684. return false;
  685. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  686. memory = wasm_get_default_memory(module_inst);
  687. if (memory) {
  688. num_bytes_per_page = memory->num_bytes_per_page;
  689. linear_mem_size = num_bytes_per_page * memory->cur_page_count;
  690. if (offset + *size > linear_mem_size) {
  691. LOG_VERBOSE("wasm_debug_instance_get_linear_mem size overflow!\n");
  692. *size = linear_mem_size >= offset ? linear_mem_size - offset : 0;
  693. }
  694. bh_memcpy_s(memory->memory_data + offset, (uint32)*size, buf,
  695. (uint32)*size);
  696. return true;
  697. }
  698. return false;
  699. }
  700. bool
  701. wasm_debug_instance_get_mem(WASMDebugInstance *instance, uint64 addr, char *buf,
  702. uint64 *size)
  703. {
  704. switch (WASM_ADDR_TYPE(addr)) {
  705. case WasmMemory:
  706. return wasm_debug_instance_get_linear_mem(
  707. instance, WASM_ADDR_OFFSET(addr), buf, size);
  708. break;
  709. case WasmObj:
  710. return wasm_debug_instance_get_obj_mem(
  711. instance, WASM_ADDR_OFFSET(addr), buf, size);
  712. break;
  713. default:
  714. return false;
  715. }
  716. }
  717. bool
  718. wasm_debug_instance_set_mem(WASMDebugInstance *instance, uint64 addr, char *buf,
  719. uint64 *size)
  720. {
  721. switch (WASM_ADDR_TYPE(addr)) {
  722. case WasmMemory:
  723. return wasm_debug_instance_set_linear_mem(
  724. instance, WASM_ADDR_OFFSET(addr), buf, size);
  725. break;
  726. case WasmObj:
  727. default:
  728. return false;
  729. }
  730. }
  731. WASMDebugInstance *
  732. wasm_exec_env_get_instance(WASMExecEnv *exec_env)
  733. {
  734. WASMDebugInstance *instance = NULL;
  735. if (!g_debug_engine) {
  736. return NULL;
  737. }
  738. os_mutex_lock(&g_debug_engine->instance_list_lock);
  739. instance = bh_list_first_elem(&g_debug_engine->debug_instance_list);
  740. while (instance) {
  741. if (instance->cluster == exec_env->cluster)
  742. break;
  743. instance = bh_list_elem_next(instance);
  744. }
  745. os_mutex_unlock(&g_debug_engine->instance_list_lock);
  746. return instance;
  747. }
  748. uint32
  749. wasm_debug_instance_get_call_stack_pcs(WASMDebugInstance *instance,
  750. korp_tid tid, uint64 buf[], uint64 size)
  751. {
  752. WASMExecEnv *exec_env;
  753. struct WASMInterpFrame *frame;
  754. uint32 i = 0;
  755. if (!instance)
  756. return 0;
  757. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  758. while (exec_env) {
  759. if (exec_env->handle == tid) {
  760. WASMModuleInstance *module_inst =
  761. (WASMModuleInstance *)exec_env->module_inst;
  762. frame = exec_env->cur_frame;
  763. while (frame && i < size) {
  764. if (frame->ip != NULL) {
  765. buf[i++] =
  766. WASM_ADDR(WasmObj, instance->id,
  767. (frame->ip - module_inst->module->load_addr));
  768. }
  769. frame = frame->prev_frame;
  770. }
  771. return i;
  772. }
  773. exec_env = bh_list_elem_next(exec_env);
  774. }
  775. return 0;
  776. }
  777. bool
  778. wasm_debug_instance_add_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. if (!(breakpoint =
  797. wasm_runtime_malloc(sizeof(WASMDebugBreakPoint)))) {
  798. LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
  799. return false;
  800. }
  801. memset(breakpoint, 0, sizeof(WASMDebugBreakPoint));
  802. breakpoint->addr = offset;
  803. /* TODO: how to if more than one breakpoints are set
  804. at the same addr? */
  805. bh_memcpy_s(&breakpoint->orignal_data, (uint32)sizeof(break_instr),
  806. module_inst->module->load_addr + offset,
  807. (uint32)sizeof(break_instr));
  808. bh_memcpy_s(module_inst->module->load_addr + offset,
  809. (uint32)sizeof(break_instr), break_instr,
  810. (uint32)sizeof(break_instr));
  811. bh_list_insert(&instance->break_point_list, breakpoint);
  812. return true;
  813. }
  814. }
  815. return false;
  816. }
  817. bool
  818. wasm_debug_instance_remove_breakpoint(WASMDebugInstance *instance, uint64 addr,
  819. uint64 length)
  820. {
  821. WASMExecEnv *exec_env;
  822. WASMModuleInstance *module_inst;
  823. uint64 offset;
  824. if (!instance)
  825. return false;
  826. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  827. if (!exec_env)
  828. return false;
  829. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  830. if (WASM_ADDR_TYPE(addr) != WasmObj)
  831. return false;
  832. offset = WASM_ADDR_OFFSET(addr);
  833. if (length >= sizeof(break_instr)) {
  834. if (offset + sizeof(break_instr) <= module_inst->module->load_size) {
  835. WASMDebugBreakPoint *breakpoint =
  836. bh_list_first_elem(&instance->break_point_list);
  837. while (breakpoint) {
  838. WASMDebugBreakPoint *next_break = bh_list_elem_next(breakpoint);
  839. if (breakpoint->addr == offset) {
  840. /* TODO: how to if more than one breakpoints are set
  841. at the same addr? */
  842. bh_memcpy_s(module_inst->module->load_addr + offset,
  843. (uint32)sizeof(break_instr),
  844. &breakpoint->orignal_data,
  845. (uint32)sizeof(break_instr));
  846. bh_list_remove(&instance->break_point_list, breakpoint);
  847. wasm_runtime_free(breakpoint);
  848. }
  849. breakpoint = next_break;
  850. }
  851. }
  852. }
  853. return true;
  854. }
  855. static bool
  856. add_watchpoint(bh_list *list, uint64 addr, uint64 length)
  857. {
  858. WASMDebugWatchPoint *watchpoint;
  859. if (!(watchpoint = wasm_runtime_malloc(sizeof(WASMDebugWatchPoint)))) {
  860. LOG_ERROR("WASM Debug Engine error: failed to allocate memory for "
  861. "watchpoint");
  862. return false;
  863. }
  864. memset(watchpoint, 0, sizeof(WASMDebugWatchPoint));
  865. watchpoint->addr = addr;
  866. watchpoint->length = length;
  867. bh_list_insert(list, watchpoint);
  868. return true;
  869. }
  870. static bool
  871. remove_watchpoint(bh_list *list, uint64 addr, uint64 length)
  872. {
  873. WASMDebugWatchPoint *watchpoint = bh_list_first_elem(list);
  874. while (watchpoint) {
  875. WASMDebugWatchPoint *next = bh_list_elem_next(watchpoint);
  876. if (watchpoint->addr == addr && watchpoint->length == length) {
  877. bh_list_remove(list, watchpoint);
  878. wasm_runtime_free(watchpoint);
  879. }
  880. watchpoint = next;
  881. }
  882. return true;
  883. }
  884. bool
  885. wasm_debug_instance_watchpoint_write_add(WASMDebugInstance *instance,
  886. uint64 addr, uint64 length)
  887. {
  888. return add_watchpoint(&instance->watch_point_list_write, addr, length);
  889. }
  890. bool
  891. wasm_debug_instance_watchpoint_write_remove(WASMDebugInstance *instance,
  892. uint64 addr, uint64 length)
  893. {
  894. return remove_watchpoint(&instance->watch_point_list_write, addr, length);
  895. }
  896. bool
  897. wasm_debug_instance_watchpoint_read_add(WASMDebugInstance *instance,
  898. uint64 addr, uint64 length)
  899. {
  900. return add_watchpoint(&instance->watch_point_list_read, addr, length);
  901. }
  902. bool
  903. wasm_debug_instance_watchpoint_read_remove(WASMDebugInstance *instance,
  904. uint64 addr, uint64 length)
  905. {
  906. return remove_watchpoint(&instance->watch_point_list_read, addr, length);
  907. }
  908. bool
  909. wasm_debug_instance_on_failure(WASMDebugInstance *instance)
  910. {
  911. WASMExecEnv *exec_env;
  912. if (!instance)
  913. return false;
  914. os_mutex_lock(&instance->wait_lock);
  915. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  916. if (!exec_env) {
  917. os_mutex_unlock(&instance->wait_lock);
  918. return false;
  919. }
  920. if (instance->stopped_thread == NULL
  921. && instance->current_state == DBG_LAUNCHING) {
  922. /* if fail in start stage: may need wait for main thread to notify it */
  923. os_cond_wait(&instance->wait_cond, &instance->wait_lock);
  924. }
  925. instance->current_state = DBG_ERROR;
  926. instance->stopped_thread = NULL;
  927. /* terminate the wasm execution thread */
  928. while (exec_env) {
  929. /* Resume all threads so they can receive the TERM signal */
  930. os_mutex_lock(&exec_env->wait_lock);
  931. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TERM);
  932. exec_env->current_status->running_status = STATUS_RUNNING;
  933. os_cond_signal(&exec_env->wait_cond);
  934. os_mutex_unlock(&exec_env->wait_lock);
  935. exec_env = bh_list_elem_next(exec_env);
  936. }
  937. os_mutex_unlock(&instance->wait_lock);
  938. return true;
  939. }
  940. bool
  941. wasm_debug_instance_continue(WASMDebugInstance *instance)
  942. {
  943. WASMExecEnv *exec_env;
  944. if (!instance)
  945. return false;
  946. if (instance->current_state == APP_RUNNING) {
  947. LOG_VERBOSE("Already in running state, ignore continue request");
  948. return false;
  949. }
  950. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  951. if (!exec_env)
  952. return false;
  953. while (exec_env) {
  954. wasm_cluster_thread_continue(exec_env);
  955. exec_env = bh_list_elem_next(exec_env);
  956. }
  957. instance->current_state = APP_RUNNING;
  958. return true;
  959. }
  960. bool
  961. wasm_debug_instance_interrupt_all_threads(WASMDebugInstance *instance)
  962. {
  963. WASMExecEnv *exec_env;
  964. if (!instance)
  965. return false;
  966. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  967. if (!exec_env)
  968. return false;
  969. while (exec_env) {
  970. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TRAP);
  971. exec_env = bh_list_elem_next(exec_env);
  972. }
  973. return true;
  974. }
  975. bool
  976. wasm_debug_instance_detach(WASMDebugInstance *instance)
  977. {
  978. WASMExecEnv *exec_env;
  979. if (!instance)
  980. return false;
  981. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  982. if (!exec_env)
  983. return false;
  984. wasm_gdbserver_detach(instance->control_thread->server);
  985. while (exec_env) {
  986. if (instance->current_state == APP_STOPPED) {
  987. /* Resume all threads since remote debugger detached*/
  988. wasm_cluster_thread_continue(exec_env);
  989. }
  990. exec_env = bh_list_elem_next(exec_env);
  991. }
  992. /* relaunch, accept new debug connection */
  993. instance->current_state = DBG_LAUNCHING;
  994. instance->control_thread->status = DETACHED;
  995. instance->stopped_thread = NULL;
  996. return true;
  997. }
  998. bool
  999. wasm_debug_instance_kill(WASMDebugInstance *instance)
  1000. {
  1001. WASMExecEnv *exec_env;
  1002. if (!instance)
  1003. return false;
  1004. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  1005. if (!exec_env)
  1006. return false;
  1007. while (exec_env) {
  1008. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TERM);
  1009. if (instance->current_state == APP_STOPPED) {
  1010. /* Resume all threads so they can receive the TERM signal */
  1011. os_mutex_lock(&exec_env->wait_lock);
  1012. exec_env->current_status->running_status = STATUS_RUNNING;
  1013. os_cond_signal(&exec_env->wait_cond);
  1014. os_mutex_unlock(&exec_env->wait_lock);
  1015. }
  1016. exec_env = bh_list_elem_next(exec_env);
  1017. }
  1018. instance->current_state = APP_RUNNING;
  1019. return true;
  1020. }
  1021. bool
  1022. wasm_debug_instance_singlestep(WASMDebugInstance *instance, korp_tid tid)
  1023. {
  1024. WASMExecEnv *exec_env;
  1025. if (!instance)
  1026. return false;
  1027. if (instance->current_state == APP_RUNNING) {
  1028. LOG_VERBOSE("Already in running state, ignore step request");
  1029. return false;
  1030. }
  1031. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  1032. if (!exec_env)
  1033. return false;
  1034. while (exec_env) {
  1035. if (exec_env->handle == tid || tid == (korp_tid)(uintptr_t)~0LL) {
  1036. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_SINGSTEP);
  1037. wasm_cluster_thread_step(exec_env);
  1038. }
  1039. exec_env = bh_list_elem_next(exec_env);
  1040. }
  1041. instance->current_state = APP_RUNNING;
  1042. return true;
  1043. }
  1044. bool
  1045. wasm_debug_instance_get_local(WASMDebugInstance *instance, int32 frame_index,
  1046. int32 local_index, char buf[], int32 *size)
  1047. {
  1048. WASMExecEnv *exec_env;
  1049. struct WASMInterpFrame *frame;
  1050. WASMFunctionInstance *cur_func;
  1051. uint8 local_type = 0xFF;
  1052. uint32 local_offset;
  1053. int32 param_count;
  1054. int32 fi = 0;
  1055. if (!instance)
  1056. return false;
  1057. exec_env = wasm_debug_instance_get_current_env(instance);
  1058. if (!exec_env)
  1059. return false;
  1060. frame = exec_env->cur_frame;
  1061. while (frame && fi++ != frame_index) {
  1062. frame = frame->prev_frame;
  1063. }
  1064. if (!frame)
  1065. return false;
  1066. cur_func = frame->function;
  1067. if (!cur_func)
  1068. return false;
  1069. param_count = cur_func->param_count;
  1070. if (local_index >= param_count + cur_func->local_count)
  1071. return false;
  1072. local_offset = cur_func->local_offsets[local_index];
  1073. if (local_index < param_count)
  1074. local_type = cur_func->param_types[local_index];
  1075. else if (local_index < cur_func->local_count + param_count)
  1076. local_type = cur_func->local_types[local_index - param_count];
  1077. switch (local_type) {
  1078. case VALUE_TYPE_I32:
  1079. case VALUE_TYPE_F32:
  1080. *size = 4;
  1081. bh_memcpy_s(buf, 4, (char *)(frame->lp + local_offset), 4);
  1082. break;
  1083. case VALUE_TYPE_I64:
  1084. case VALUE_TYPE_F64:
  1085. *size = 8;
  1086. bh_memcpy_s(buf, 8, (char *)(frame->lp + local_offset), 8);
  1087. break;
  1088. default:
  1089. *size = 0;
  1090. break;
  1091. }
  1092. return true;
  1093. }
  1094. bool
  1095. wasm_debug_instance_get_global(WASMDebugInstance *instance, int32 frame_index,
  1096. int32 global_index, char buf[], int32 *size)
  1097. {
  1098. WASMExecEnv *exec_env;
  1099. struct WASMInterpFrame *frame;
  1100. WASMModuleInstance *module_inst;
  1101. WASMGlobalInstance *globals, *global;
  1102. uint8 *global_addr;
  1103. uint8 global_type = 0xFF;
  1104. uint8 *global_data;
  1105. int32 fi = 0;
  1106. if (!instance)
  1107. return false;
  1108. exec_env = wasm_debug_instance_get_current_env(instance);
  1109. if (!exec_env)
  1110. return false;
  1111. frame = exec_env->cur_frame;
  1112. while (frame && fi++ != frame_index) {
  1113. frame = frame->prev_frame;
  1114. }
  1115. if (!frame)
  1116. return false;
  1117. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  1118. global_data = module_inst->global_data;
  1119. globals = module_inst->e->globals;
  1120. if ((global_index < 0)
  1121. || ((uint32)global_index >= module_inst->e->global_count)) {
  1122. return false;
  1123. }
  1124. global = globals + global_index;
  1125. #if WASM_ENABLE_MULTI_MODULE == 0
  1126. global_addr = global_data + global->data_offset;
  1127. #else
  1128. global_addr = global->import_global_inst
  1129. ? global->import_module_inst->global_data
  1130. + global->import_global_inst->data_offset
  1131. : global_data + global->data_offset;
  1132. #endif
  1133. global_type = global->type;
  1134. switch (global_type) {
  1135. case VALUE_TYPE_I32:
  1136. case VALUE_TYPE_F32:
  1137. *size = 4;
  1138. bh_memcpy_s(buf, 4, (char *)(global_addr), 4);
  1139. break;
  1140. case VALUE_TYPE_I64:
  1141. case VALUE_TYPE_F64:
  1142. *size = 8;
  1143. bh_memcpy_s(buf, 8, (char *)(global_addr), 8);
  1144. break;
  1145. default:
  1146. *size = 0;
  1147. break;
  1148. }
  1149. return true;
  1150. }
  1151. uint64
  1152. wasm_debug_instance_mmap(WASMDebugInstance *instance, uint32 size,
  1153. int32 map_prot)
  1154. {
  1155. WASMExecEnv *exec_env;
  1156. uint32 offset = 0;
  1157. (void)map_prot;
  1158. if (!instance)
  1159. return 0;
  1160. exec_env = wasm_debug_instance_get_current_env(instance);
  1161. if (!exec_env)
  1162. return 0;
  1163. if (instance->exec_mem_info.start_offset == 0) {
  1164. return 0;
  1165. }
  1166. if (instance->exec_mem_info.current_pos
  1167. - instance->exec_mem_info.start_offset + size
  1168. <= (uint64)instance->exec_mem_info.size) {
  1169. offset = instance->exec_mem_info.current_pos;
  1170. instance->exec_mem_info.current_pos += size;
  1171. }
  1172. if (offset == 0) {
  1173. LOG_WARNING("the memory may be not enough for debug, try use larger "
  1174. "--heap-size");
  1175. return 0;
  1176. }
  1177. return WASM_ADDR(WasmMemory, 0, offset);
  1178. }
  1179. bool
  1180. wasm_debug_instance_ummap(WASMDebugInstance *instance, uint64 addr)
  1181. {
  1182. WASMExecEnv *exec_env;
  1183. if (!instance)
  1184. return false;
  1185. exec_env = wasm_debug_instance_get_current_env(instance);
  1186. if (!exec_env)
  1187. return false;
  1188. if (instance->exec_mem_info.start_offset == 0) {
  1189. return false;
  1190. }
  1191. (void)addr;
  1192. /* Currently we don't support to free the execution memory, simply return
  1193. * true here */
  1194. return true;
  1195. }