debug_engine.c 35 KB

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