debug_engine.c 35 KB

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