debug_engine.c 36 KB

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