debug_engine.c 35 KB

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