debug_engine.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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 "bh_log.h"
  7. #include "gdbserver.h"
  8. #include "platform_api_extension.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. int platform_port;
  18. int process_base_port;
  19. bh_list debug_instance_list;
  20. bool active;
  21. } WASMDebugEngine;
  22. static WASMDebugEngine *g_debug_engine;
  23. static bool
  24. should_stop(WASMDebugControlThread *control_thread)
  25. {
  26. return control_thread->status != RUNNING;
  27. }
  28. static void *
  29. control_thread_routine(void *arg)
  30. {
  31. WASMDebugInstance *debug_inst = (WASMDebugInstance *)arg;
  32. WASMDebugControlThread *control_thread = NULL;
  33. WASMCluster *cluster = NULL;
  34. WASMExecEnv *exec_env;
  35. bh_assert(debug_inst);
  36. control_thread = debug_inst->control_thread;
  37. bh_assert(control_thread);
  38. cluster = debug_inst->cluster;
  39. bh_assert(cluster);
  40. exec_env = bh_list_first_elem(&cluster->exec_env_list);
  41. bh_assert(exec_env);
  42. os_mutex_lock(&exec_env->wait_lock);
  43. control_thread->status = RUNNING;
  44. debug_inst->id = g_debug_engine->debug_instance_list.len + 1;
  45. control_thread->debug_engine = g_debug_engine;
  46. control_thread->debug_instance = debug_inst;
  47. strcpy(control_thread->ip_addr, g_debug_engine->ip_addr);
  48. control_thread->port = g_debug_engine->process_base_port + debug_inst->id;
  49. LOG_WARNING("control thread of debug object %p start at %s:%d\n",
  50. debug_inst, control_thread->ip_addr, control_thread->port);
  51. control_thread->server =
  52. wasm_launch_gdbserver(control_thread->ip_addr, control_thread->port);
  53. if (!control_thread->server) {
  54. LOG_ERROR("Failed to create debug server\n");
  55. os_cond_signal(&exec_env->wait_cond);
  56. os_mutex_unlock(&exec_env->wait_lock);
  57. return NULL;
  58. }
  59. control_thread->server->thread = control_thread;
  60. /* control thread ready, notify main thread */
  61. os_cond_signal(&exec_env->wait_cond);
  62. os_mutex_unlock(&exec_env->wait_lock);
  63. while (true) {
  64. os_mutex_lock(&control_thread->wait_lock);
  65. if (!should_stop(control_thread)) {
  66. if (!wasm_gdbserver_handle_packet(control_thread->server)) {
  67. control_thread->status = STOPPED;
  68. }
  69. }
  70. else {
  71. os_mutex_unlock(&control_thread->wait_lock);
  72. break;
  73. }
  74. os_mutex_unlock(&control_thread->wait_lock);
  75. }
  76. LOG_VERBOSE("control thread of debug object %p stop\n", debug_inst);
  77. return NULL;
  78. }
  79. static WASMDebugControlThread *
  80. wasm_debug_control_thread_create(WASMDebugInstance *debug_instance)
  81. {
  82. WASMDebugControlThread *control_thread;
  83. WASMCluster *cluster = debug_instance->cluster;
  84. WASMExecEnv *exec_env;
  85. bh_assert(cluster);
  86. exec_env = bh_list_first_elem(&cluster->exec_env_list);
  87. bh_assert(exec_env);
  88. if (!(control_thread =
  89. wasm_runtime_malloc(sizeof(WASMDebugControlThread)))) {
  90. LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
  91. return NULL;
  92. }
  93. memset(control_thread, 0, sizeof(WASMDebugControlThread));
  94. if (os_mutex_init(&control_thread->wait_lock) != 0)
  95. goto fail;
  96. debug_instance->control_thread = control_thread;
  97. os_mutex_lock(&exec_env->wait_lock);
  98. if (0
  99. != os_thread_create(&control_thread->tid, control_thread_routine,
  100. debug_instance, APP_THREAD_STACK_SIZE_MAX)) {
  101. os_mutex_unlock(&control_thread->wait_lock);
  102. goto fail1;
  103. }
  104. /* wait until the debug control thread ready */
  105. os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock);
  106. os_mutex_unlock(&exec_env->wait_lock);
  107. if (!control_thread->server)
  108. goto fail1;
  109. /* create control thread success, append debug instance to debug engine */
  110. bh_list_insert(&g_debug_engine->debug_instance_list, debug_instance);
  111. wasm_cluster_send_signal_all(debug_instance->cluster, WAMR_SIG_STOP);
  112. return control_thread;
  113. fail1:
  114. os_mutex_destroy(&control_thread->wait_lock);
  115. fail:
  116. wasm_runtime_free(control_thread);
  117. return NULL;
  118. }
  119. static void
  120. wasm_debug_control_thread_destroy(WASMDebugInstance *debug_instance)
  121. {
  122. WASMDebugControlThread *control_thread = debug_instance->control_thread;
  123. LOG_VERBOSE("control thread of debug object %p stop at %s:%d\n",
  124. debug_instance, control_thread->ip_addr, control_thread->port);
  125. control_thread->status = STOPPED;
  126. os_mutex_lock(&control_thread->wait_lock);
  127. wasm_close_gdbserver(control_thread->server);
  128. os_mutex_unlock(&control_thread->wait_lock);
  129. os_thread_join(control_thread->tid, NULL);
  130. wasm_runtime_free(control_thread->server);
  131. os_mutex_destroy(&control_thread->wait_lock);
  132. wasm_runtime_free(control_thread);
  133. }
  134. static WASMDebugEngine *
  135. wasm_debug_engine_create()
  136. {
  137. WASMDebugEngine *engine;
  138. if (!(engine = wasm_runtime_malloc(sizeof(WASMDebugEngine)))) {
  139. LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
  140. return NULL;
  141. }
  142. memset(engine, 0, sizeof(WASMDebugEngine));
  143. /* TODO: support Wasm platform in LLDB */
  144. /*
  145. engine->control_thread =
  146. wasm_debug_control_thread_create((WASMDebugObject *)engine);
  147. engine->control_thread->debug_engine = (WASMDebugObject *)engine;
  148. engine->control_thread->debug_instance = NULL;
  149. sprintf(engine->control_thread->ip_addr, "127.0.0.1");
  150. engine->control_thread->port = 1234;
  151. */
  152. bh_list_init(&engine->debug_instance_list);
  153. return engine;
  154. }
  155. bool
  156. wasm_debug_engine_init(char *ip_addr, int platform_port, int process_port)
  157. {
  158. if (g_debug_engine == NULL)
  159. g_debug_engine = wasm_debug_engine_create();
  160. if (g_debug_engine) {
  161. process_port -= 1;
  162. g_debug_engine->platform_port =
  163. platform_port > 0 ? platform_port : 1234;
  164. g_debug_engine->process_base_port =
  165. process_port > 0 ? process_port : 6169;
  166. if (ip_addr)
  167. sprintf(g_debug_engine->ip_addr, "%s", ip_addr);
  168. else
  169. sprintf(g_debug_engine->ip_addr, "%s", "127.0.0.1");
  170. g_debug_engine->active = true;
  171. }
  172. return g_debug_engine != NULL ? true : false;
  173. }
  174. void
  175. wasm_debug_set_engine_active(bool active)
  176. {
  177. if (g_debug_engine) {
  178. g_debug_engine->active = active;
  179. }
  180. }
  181. bool
  182. wasm_debug_get_engine_active(void)
  183. {
  184. if (g_debug_engine) {
  185. return g_debug_engine->active;
  186. }
  187. return false;
  188. }
  189. void
  190. wasm_debug_engine_destroy()
  191. {
  192. if (g_debug_engine) {
  193. wasm_runtime_free(g_debug_engine);
  194. g_debug_engine = NULL;
  195. }
  196. }
  197. /* A debug Instance is a debug "process" in gdb remote protocol
  198. and bound to a runtime cluster */
  199. WASMDebugInstance *
  200. wasm_debug_instance_create(WASMCluster *cluster)
  201. {
  202. WASMDebugInstance *instance;
  203. WASMExecEnv *exec_env;
  204. if (!g_debug_engine || !g_debug_engine->active) {
  205. return NULL;
  206. }
  207. if (!(instance = wasm_runtime_malloc(sizeof(WASMDebugInstance)))) {
  208. LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
  209. return NULL;
  210. }
  211. memset(instance, 0, sizeof(WASMDebugInstance));
  212. bh_list_init(&instance->break_point_list);
  213. instance->cluster = cluster;
  214. exec_env = bh_list_first_elem(&cluster->exec_env_list);
  215. bh_assert(exec_env);
  216. instance->current_tid = exec_env->handle;
  217. if (!wasm_debug_control_thread_create(instance)) {
  218. LOG_ERROR("WASM Debug Engine error: failed to create control thread");
  219. wasm_runtime_free(instance);
  220. return NULL;
  221. }
  222. return instance;
  223. }
  224. static WASMDebugInstance *
  225. wasm_cluster_get_debug_instance(WASMDebugEngine *engine, WASMCluster *cluster)
  226. {
  227. WASMDebugInstance *instance =
  228. bh_list_first_elem(&engine->debug_instance_list);
  229. while (instance) {
  230. if (instance->cluster == cluster)
  231. return instance;
  232. instance = bh_list_elem_next(instance);
  233. }
  234. return instance;
  235. }
  236. static void
  237. wasm_debug_instance_destroy_breakpoints(WASMDebugInstance *instance)
  238. {
  239. WASMDebugBreakPoint *breakpoint, *next_bp;
  240. breakpoint = bh_list_first_elem(&instance->break_point_list);
  241. while (breakpoint) {
  242. next_bp = bh_list_elem_next(breakpoint);
  243. bh_list_remove(&instance->break_point_list, breakpoint);
  244. wasm_runtime_free(breakpoint);
  245. breakpoint = next_bp;
  246. }
  247. }
  248. void
  249. wasm_debug_instance_destroy(WASMCluster *cluster)
  250. {
  251. WASMDebugInstance *instance = NULL;
  252. if (!g_debug_engine) {
  253. return;
  254. }
  255. instance = wasm_cluster_get_debug_instance(g_debug_engine, cluster);
  256. if (instance) {
  257. /* destroy control thread */
  258. wasm_debug_control_thread_destroy(instance);
  259. bh_list_remove(&g_debug_engine->debug_instance_list, instance);
  260. /* destroy all breakpoints */
  261. wasm_debug_instance_destroy_breakpoints(instance);
  262. wasm_runtime_free(instance);
  263. }
  264. }
  265. static WASMExecEnv *
  266. wasm_debug_instance_get_current_env(WASMDebugInstance *instance)
  267. {
  268. WASMExecEnv *exec_env = NULL;
  269. if (instance) {
  270. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  271. while (exec_env) {
  272. if (exec_env->handle == instance->current_tid)
  273. break;
  274. exec_env = bh_list_elem_next(exec_env);
  275. }
  276. }
  277. return exec_env;
  278. }
  279. #if WASM_ENABLE_LIBC_WASI != 0
  280. bool
  281. wasm_debug_instance_get_current_object_name(WASMDebugInstance *instance,
  282. char name_buffer[], int len)
  283. {
  284. WASMExecEnv *exec_env;
  285. WASIArguments *wasi_args;
  286. WASMModuleInstance *module_inst;
  287. if (!instance)
  288. return false;
  289. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  290. if (!exec_env)
  291. return false;
  292. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  293. wasi_args = &module_inst->module->wasi_args;
  294. if (wasi_args && wasi_args->argc > 0) {
  295. char *argv_name = wasi_args->argv[0];
  296. int name_len = strlen(argv_name);
  297. printf("the module name is %s\n", argv_name);
  298. if (len - 1 >= name_len)
  299. strcpy(name_buffer, argv_name);
  300. else
  301. strcpy(name_buffer, argv_name + (name_len + 1 - len));
  302. return true;
  303. }
  304. return false;
  305. }
  306. #endif
  307. uint64
  308. wasm_debug_instance_get_pid(WASMDebugInstance *instance)
  309. {
  310. if (instance != NULL) {
  311. return (uint64)instance->id;
  312. }
  313. return (uint64)0;
  314. }
  315. uint64
  316. wasm_debug_instance_get_tid(WASMDebugInstance *instance)
  317. {
  318. if (instance != NULL) {
  319. return (uint64)instance->current_tid;
  320. }
  321. return (uint64)0;
  322. }
  323. int
  324. wasm_debug_instance_get_tids(WASMDebugInstance *instance, uint64 tids[],
  325. int len)
  326. {
  327. WASMExecEnv *exec_env;
  328. int i = 0;
  329. if (!instance)
  330. return 0;
  331. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  332. while (exec_env && i < len) {
  333. tids[i++] = exec_env->handle;
  334. exec_env = bh_list_elem_next(exec_env);
  335. }
  336. LOG_VERBOSE("find %d tids\n", i);
  337. return i;
  338. }
  339. uint64
  340. wasm_debug_instance_wait_thread(WASMDebugInstance *instance, uint64 tid,
  341. uint32 *status)
  342. {
  343. WASMExecEnv *exec_env;
  344. WASMExecEnv *last_exec_env = NULL;
  345. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  346. while (exec_env) {
  347. last_exec_env = exec_env;
  348. if (instance->current_tid != 0
  349. && last_exec_env->handle == instance->current_tid) {
  350. break;
  351. }
  352. exec_env = bh_list_elem_next(exec_env);
  353. }
  354. if (last_exec_env) {
  355. wasm_cluster_wait_thread_status(last_exec_env, status);
  356. if (instance->current_tid == 0)
  357. instance->current_tid = last_exec_env->handle;
  358. return last_exec_env->handle;
  359. }
  360. else {
  361. *status = ~0;
  362. return 0;
  363. }
  364. }
  365. void
  366. wasm_debug_instance_set_cur_thread(WASMDebugInstance *instance, uint64 tid)
  367. {
  368. instance->current_tid = tid;
  369. }
  370. uint64
  371. wasm_debug_instance_get_pc(WASMDebugInstance *instance)
  372. {
  373. WASMExecEnv *exec_env;
  374. if (!instance)
  375. return 0;
  376. exec_env = wasm_debug_instance_get_current_env(instance);
  377. if ((exec_env->cur_frame != NULL) && (exec_env->cur_frame->ip != NULL)) {
  378. WASMModuleInstance *module_inst =
  379. (WASMModuleInstance *)exec_env->module_inst;
  380. return WASM_ADDR(
  381. WasmObj, instance->id,
  382. (exec_env->cur_frame->ip - module_inst->module->load_addr));
  383. }
  384. return 0;
  385. }
  386. uint64
  387. wasm_debug_instance_get_load_addr(WASMDebugInstance *instance)
  388. {
  389. WASMExecEnv *exec_env;
  390. if (!instance)
  391. return WASM_ADDR(WasmInvalid, 0, 0);
  392. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  393. if (exec_env) {
  394. return WASM_ADDR(WasmObj, instance->id, 0);
  395. }
  396. return WASM_ADDR(WasmInvalid, 0, 0);
  397. }
  398. WASMDebugMemoryInfo *
  399. wasm_debug_instance_get_memregion(WASMDebugInstance *instance, uint64 addr)
  400. {
  401. WASMDebugMemoryInfo *mem_info;
  402. WASMExecEnv *exec_env;
  403. WASMModuleInstance *module_inst;
  404. WASMMemoryInstance *memory;
  405. uint32 num_bytes_per_page;
  406. uint32 linear_mem_size = 0;
  407. if (!instance)
  408. return NULL;
  409. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  410. if (!exec_env)
  411. return NULL;
  412. if (!(mem_info = wasm_runtime_malloc(sizeof(WASMDebugMemoryInfo)))) {
  413. LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
  414. return NULL;
  415. }
  416. memset(mem_info, 0, sizeof(WASMDebugMemoryInfo));
  417. mem_info->start = WASM_ADDR(WasmInvalid, 0, 0);
  418. mem_info->size = 0;
  419. mem_info->name[0] = '\0';
  420. mem_info->permisson[0] = '\0';
  421. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  422. switch (WASM_ADDR_TYPE(addr)) {
  423. case WasmObj:
  424. if (WASM_ADDR_OFFSET(addr) < module_inst->module->load_size) {
  425. mem_info->start = WASM_ADDR(WasmObj, instance->id, 0);
  426. mem_info->size = module_inst->module->load_size;
  427. sprintf(mem_info->name, "%s", "module");
  428. sprintf(mem_info->permisson, "%s", "rx");
  429. }
  430. break;
  431. case WasmMemory:
  432. {
  433. memory = module_inst->default_memory;
  434. if (memory) {
  435. num_bytes_per_page = memory->num_bytes_per_page;
  436. linear_mem_size = num_bytes_per_page * memory->cur_page_count;
  437. }
  438. if (WASM_ADDR_OFFSET(addr) < linear_mem_size) {
  439. mem_info->start = WASM_ADDR(WasmMemory, instance->id, 0);
  440. mem_info->size = linear_mem_size;
  441. sprintf(mem_info->name, "%s", "memory");
  442. sprintf(mem_info->permisson, "%s", "rw");
  443. }
  444. break;
  445. }
  446. default:
  447. mem_info->start = WASM_ADDR(WasmInvalid, 0, 0);
  448. mem_info->size = 0;
  449. }
  450. return mem_info;
  451. }
  452. void
  453. wasm_debug_instance_destroy_memregion(WASMDebugInstance *instance,
  454. WASMDebugMemoryInfo *mem_info)
  455. {
  456. wasm_runtime_free(mem_info);
  457. }
  458. bool
  459. wasm_debug_instance_get_obj_mem(WASMDebugInstance *instance, uint64 offset,
  460. char *buf, uint64 *size)
  461. {
  462. WASMExecEnv *exec_env;
  463. WASMModuleInstance *module_inst;
  464. if (!instance)
  465. return false;
  466. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  467. if (!exec_env)
  468. return false;
  469. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  470. if (offset + *size > module_inst->module->load_size) {
  471. LOG_VERBOSE("wasm_debug_instance_get_data_mem size over flow!\n");
  472. *size = module_inst->module->load_size >= offset
  473. ? module_inst->module->load_size - offset
  474. : 0;
  475. }
  476. bh_memcpy_s(buf, *size, module_inst->module->load_addr + offset, *size);
  477. WASMDebugBreakPoint *breakpoint =
  478. bh_list_first_elem(&instance->break_point_list);
  479. while (breakpoint) {
  480. if (offset <= breakpoint->addr && breakpoint->addr < offset + *size) {
  481. bh_memcpy_s(buf + (breakpoint->addr - offset), sizeof(break_instr),
  482. &breakpoint->orignal_data, sizeof(break_instr));
  483. }
  484. breakpoint = bh_list_elem_next(breakpoint);
  485. }
  486. WASMFastOPCodeNode *fast_opcode =
  487. bh_list_first_elem(&module_inst->module->fast_opcode_list);
  488. while (fast_opcode) {
  489. if (offset <= fast_opcode->offset
  490. && fast_opcode->offset < offset + *size) {
  491. *(uint8 *)(buf + (fast_opcode->offset - offset)) =
  492. fast_opcode->orig_op;
  493. }
  494. fast_opcode = bh_list_elem_next(fast_opcode);
  495. }
  496. return true;
  497. }
  498. bool
  499. wasm_debug_instance_get_linear_mem(WASMDebugInstance *instance, uint64 offset,
  500. char *buf, uint64 *size)
  501. {
  502. WASMExecEnv *exec_env;
  503. WASMModuleInstance *module_inst;
  504. WASMMemoryInstance *memory;
  505. uint32 num_bytes_per_page;
  506. uint32 linear_mem_size;
  507. if (!instance)
  508. return false;
  509. exec_env = wasm_debug_instance_get_current_env(instance);
  510. if (!exec_env)
  511. return false;
  512. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  513. memory = module_inst->default_memory;
  514. if (memory) {
  515. num_bytes_per_page = memory->num_bytes_per_page;
  516. linear_mem_size = num_bytes_per_page * memory->cur_page_count;
  517. if (offset + *size > linear_mem_size) {
  518. LOG_VERBOSE("wasm_debug_instance_get_linear_mem size over flow!\n");
  519. *size = linear_mem_size >= offset ? linear_mem_size - offset : 0;
  520. }
  521. bh_memcpy_s(buf, *size, memory->memory_data + offset, *size);
  522. return true;
  523. }
  524. return false;
  525. }
  526. bool
  527. wasm_debug_instance_set_linear_mem(WASMDebugInstance *instance, uint64 offset,
  528. char *buf, uint64 *size)
  529. {
  530. WASMExecEnv *exec_env;
  531. WASMModuleInstance *module_inst;
  532. WASMMemoryInstance *memory;
  533. uint32 num_bytes_per_page;
  534. uint32 linear_mem_size;
  535. if (!instance)
  536. return false;
  537. exec_env = wasm_debug_instance_get_current_env(instance);
  538. if (!exec_env)
  539. return false;
  540. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  541. memory = module_inst->default_memory;
  542. if (memory) {
  543. num_bytes_per_page = memory->num_bytes_per_page;
  544. linear_mem_size = num_bytes_per_page * memory->cur_page_count;
  545. if (offset + *size > linear_mem_size) {
  546. LOG_VERBOSE("wasm_debug_instance_get_linear_mem size over flow!\n");
  547. *size = linear_mem_size >= offset ? linear_mem_size - offset : 0;
  548. }
  549. bh_memcpy_s(memory->memory_data + offset, *size, buf, *size);
  550. return true;
  551. }
  552. return false;
  553. }
  554. bool
  555. wasm_debug_instance_get_mem(WASMDebugInstance *instance, uint64 addr, char *buf,
  556. uint64 *size)
  557. {
  558. switch (WASM_ADDR_TYPE(addr)) {
  559. case WasmMemory:
  560. return wasm_debug_instance_get_linear_mem(
  561. instance, WASM_ADDR_OFFSET(addr), buf, size);
  562. break;
  563. case WasmObj:
  564. return wasm_debug_instance_get_obj_mem(
  565. instance, WASM_ADDR_OFFSET(addr), buf, size);
  566. break;
  567. default:
  568. return false;
  569. }
  570. }
  571. bool
  572. wasm_debug_instance_set_mem(WASMDebugInstance *instance, uint64 addr, char *buf,
  573. uint64 *size)
  574. {
  575. switch (WASM_ADDR_TYPE(addr)) {
  576. case WasmMemory:
  577. return wasm_debug_instance_set_linear_mem(
  578. instance, WASM_ADDR_OFFSET(addr), buf, size);
  579. break;
  580. case WasmObj:
  581. default:
  582. return false;
  583. }
  584. }
  585. WASMDebugInstance *
  586. wasm_exec_env_get_instance(WASMExecEnv *exec_env)
  587. {
  588. WASMDebugInstance *instance = NULL;
  589. bh_assert(g_debug_engine);
  590. instance = bh_list_first_elem(&g_debug_engine->debug_instance_list);
  591. while (instance) {
  592. if (instance->cluster == exec_env->cluster)
  593. break;
  594. instance = bh_list_elem_next(instance);
  595. }
  596. return instance;
  597. }
  598. int
  599. wasm_debug_instance_get_call_stack_pcs(WASMDebugInstance *instance, uint64 tid,
  600. uint64 buf[], uint64 size)
  601. {
  602. WASMExecEnv *exec_env;
  603. struct WASMInterpFrame *frame;
  604. uint64 i = 0;
  605. if (!instance)
  606. return 0;
  607. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  608. while (exec_env) {
  609. if (exec_env->handle == tid) {
  610. WASMModuleInstance *module_inst =
  611. (WASMModuleInstance *)exec_env->module_inst;
  612. frame = exec_env->cur_frame;
  613. while (frame && i < size) {
  614. if (frame->ip != NULL) {
  615. buf[i++] =
  616. WASM_ADDR(WasmObj, instance->id,
  617. (frame->ip - module_inst->module->load_addr));
  618. }
  619. frame = frame->prev_frame;
  620. }
  621. return i;
  622. }
  623. exec_env = bh_list_elem_next(exec_env);
  624. }
  625. return 0;
  626. }
  627. bool
  628. wasm_debug_instance_add_breakpoint(WASMDebugInstance *instance, uint64 addr,
  629. uint64 length)
  630. {
  631. WASMExecEnv *exec_env;
  632. WASMModuleInstance *module_inst;
  633. uint64 offset;
  634. if (!instance)
  635. return false;
  636. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  637. if (!exec_env)
  638. return false;
  639. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  640. if (WASM_ADDR_TYPE(addr) != WasmObj)
  641. return false;
  642. offset = WASM_ADDR_OFFSET(addr);
  643. if (length >= sizeof(break_instr)) {
  644. if (offset + sizeof(break_instr) <= module_inst->module->load_size) {
  645. WASMDebugBreakPoint *breakpoint;
  646. if (!(breakpoint =
  647. wasm_runtime_malloc(sizeof(WASMDebugBreakPoint)))) {
  648. LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
  649. return false;
  650. }
  651. memset(breakpoint, 0, sizeof(WASMDebugBreakPoint));
  652. breakpoint->addr = offset;
  653. /* TODO: how to if more than one breakpoints are set
  654. at the same addr? */
  655. bh_memcpy_s(&breakpoint->orignal_data, (uint32)sizeof(break_instr),
  656. module_inst->module->load_addr + offset,
  657. (uint32)sizeof(break_instr));
  658. bh_memcpy_s(module_inst->module->load_addr + offset,
  659. (uint32)sizeof(break_instr), break_instr,
  660. (uint32)sizeof(break_instr));
  661. bh_list_insert(&instance->break_point_list, breakpoint);
  662. return true;
  663. }
  664. }
  665. return false;
  666. }
  667. bool
  668. wasm_debug_instance_remove_breakpoint(WASMDebugInstance *instance, uint64 addr,
  669. uint64 length)
  670. {
  671. WASMExecEnv *exec_env;
  672. WASMModuleInstance *module_inst;
  673. uint64 offset;
  674. if (!instance)
  675. return false;
  676. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  677. if (!exec_env)
  678. return false;
  679. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  680. if (WASM_ADDR_TYPE(addr) != WasmObj)
  681. return false;
  682. offset = WASM_ADDR_OFFSET(addr);
  683. if (length >= sizeof(break_instr)) {
  684. if (offset + sizeof(break_instr) <= module_inst->module->load_size) {
  685. WASMDebugBreakPoint *breakpoint =
  686. bh_list_first_elem(&instance->break_point_list);
  687. while (breakpoint) {
  688. WASMDebugBreakPoint *next_break = bh_list_elem_next(breakpoint);
  689. if (breakpoint->addr == offset) {
  690. /* TODO: how to if more than one breakpoints are set
  691. at the same addr? */
  692. bh_memcpy_s(module_inst->module->load_addr + offset,
  693. (uint32)sizeof(break_instr),
  694. &breakpoint->orignal_data,
  695. (uint32)sizeof(break_instr));
  696. bh_list_remove(&instance->break_point_list, breakpoint);
  697. wasm_runtime_free(breakpoint);
  698. }
  699. breakpoint = next_break;
  700. }
  701. }
  702. }
  703. return true;
  704. }
  705. bool
  706. wasm_debug_instance_continue(WASMDebugInstance *instance)
  707. {
  708. WASMExecEnv *exec_env;
  709. if (!instance)
  710. return false;
  711. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  712. if (!exec_env)
  713. return false;
  714. while (exec_env) {
  715. wasm_cluster_thread_continue(exec_env);
  716. exec_env = bh_list_elem_next(exec_env);
  717. }
  718. return true;
  719. }
  720. bool
  721. wasm_debug_instance_kill(WASMDebugInstance *instance)
  722. {
  723. WASMExecEnv *exec_env;
  724. if (!instance)
  725. return false;
  726. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  727. if (!exec_env)
  728. return false;
  729. while (exec_env) {
  730. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TERM);
  731. exec_env = bh_list_elem_next(exec_env);
  732. }
  733. return true;
  734. }
  735. bool
  736. wasm_debug_instance_singlestep(WASMDebugInstance *instance, uint64 tid)
  737. {
  738. WASMExecEnv *exec_env;
  739. if (!instance)
  740. return false;
  741. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  742. if (!exec_env)
  743. return false;
  744. while (exec_env) {
  745. if (exec_env->handle == tid || tid == (uint64)~0) {
  746. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_SINGSTEP);
  747. wasm_cluster_thread_step(exec_env);
  748. }
  749. exec_env = bh_list_elem_next(exec_env);
  750. }
  751. return true;
  752. }
  753. bool
  754. wasm_debug_instance_get_local(WASMDebugInstance *instance, int frame_index,
  755. int local_index, char buf[], int *size)
  756. {
  757. WASMExecEnv *exec_env;
  758. struct WASMInterpFrame *frame;
  759. WASMFunctionInstance *cur_func;
  760. uint8 local_type = 0xFF;
  761. uint32 local_offset;
  762. int param_count;
  763. int fi = 0;
  764. if (!instance)
  765. return false;
  766. exec_env = wasm_debug_instance_get_current_env(instance);
  767. if (!exec_env)
  768. return false;
  769. frame = exec_env->cur_frame;
  770. while (frame && fi++ != frame_index) {
  771. frame = frame->prev_frame;
  772. }
  773. if (!frame)
  774. return false;
  775. cur_func = frame->function;
  776. if (!cur_func)
  777. return false;
  778. param_count = cur_func->param_count;
  779. if (local_index >= param_count + cur_func->local_count)
  780. return false;
  781. local_offset = cur_func->local_offsets[local_index];
  782. if (local_index < param_count)
  783. local_type = cur_func->param_types[local_index];
  784. else if (local_index < cur_func->local_count + param_count)
  785. local_type = cur_func->local_types[local_index - param_count];
  786. switch (local_type) {
  787. case VALUE_TYPE_I32:
  788. case VALUE_TYPE_F32:
  789. *size = 4;
  790. bh_memcpy_s(buf, 4, (char *)(frame->lp + local_offset), 4);
  791. break;
  792. case VALUE_TYPE_I64:
  793. case VALUE_TYPE_F64:
  794. *size = 8;
  795. bh_memcpy_s(buf, 8, (char *)(frame->lp + local_offset), 8);
  796. break;
  797. default:
  798. *size = 0;
  799. break;
  800. }
  801. return true;
  802. }
  803. bool
  804. wasm_debug_instance_get_global(WASMDebugInstance *instance, int frame_index,
  805. int global_index, char buf[], int *size)
  806. {
  807. WASMExecEnv *exec_env;
  808. struct WASMInterpFrame *frame;
  809. WASMModuleInstance *module_inst;
  810. WASMGlobalInstance *globals, *global;
  811. uint8 *global_addr;
  812. uint8 global_type = 0xFF;
  813. uint8 *global_data;
  814. int fi = 0;
  815. if (!instance)
  816. return false;
  817. exec_env = wasm_debug_instance_get_current_env(instance);
  818. if (!exec_env)
  819. return false;
  820. frame = exec_env->cur_frame;
  821. while (frame && fi++ != frame_index) {
  822. frame = frame->prev_frame;
  823. }
  824. if (!frame)
  825. return false;
  826. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  827. global_data = module_inst->global_data;
  828. globals = module_inst->globals;
  829. if ((global_index < 0)
  830. || ((uint32)global_index >= module_inst->global_count)) {
  831. return false;
  832. }
  833. global = globals + global_index;
  834. #if WASM_ENABLE_MULTI_MODULE == 0
  835. global_addr = global_data + global->data_offset;
  836. #else
  837. global_addr = global->import_global_inst
  838. ? global->import_module_inst->global_data
  839. + global->import_global_inst->data_offset
  840. : global_data + global->data_offset;
  841. #endif
  842. global_type = global->type;
  843. switch (global_type) {
  844. case VALUE_TYPE_I32:
  845. case VALUE_TYPE_F32:
  846. *size = 4;
  847. bh_memcpy_s(buf, 4, (char *)(global_addr), 4);
  848. break;
  849. case VALUE_TYPE_I64:
  850. case VALUE_TYPE_F64:
  851. *size = 8;
  852. bh_memcpy_s(buf, 8, (char *)(global_addr), 8);
  853. break;
  854. default:
  855. *size = 0;
  856. break;
  857. }
  858. return true;
  859. }
  860. uint64
  861. wasm_debug_instance_mmap(WASMDebugInstance *instance, uint32 size, int map_port)
  862. {
  863. WASMExecEnv *exec_env;
  864. WASMModuleInstance *module_inst;
  865. uint32 offset;
  866. void *native_addr;
  867. (void)map_port;
  868. if (!instance)
  869. return 0;
  870. exec_env = wasm_debug_instance_get_current_env(instance);
  871. if (!exec_env)
  872. return 0;
  873. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  874. /* TODO: malloc in wasi libc maybe not be thread safe, we hope LLDB will
  875. always ask for memory when threads stopped */
  876. offset = wasm_runtime_module_malloc((wasm_module_inst_t)module_inst, size,
  877. &native_addr);
  878. if (!offset)
  879. LOG_WARNING("the memory may be not enough for debug, try use larger "
  880. "--heap-size");
  881. return WASM_ADDR(WasmMemory, 0, offset);
  882. }
  883. bool
  884. wasm_debug_instance_ummap(WASMDebugInstance *instance, uint64 addr)
  885. {
  886. WASMExecEnv *exec_env;
  887. WASMModuleInstance *module_inst;
  888. uint32 offset;
  889. if (!instance)
  890. return false;
  891. exec_env = wasm_debug_instance_get_current_env(instance);
  892. if (!exec_env)
  893. return false;
  894. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  895. if (WASM_ADDR_TYPE(addr) == WasmMemory) {
  896. offset = WASM_ADDR_OFFSET(addr);
  897. wasm_runtime_module_free((wasm_module_inst_t)module_inst, offset);
  898. return true;
  899. }
  900. return false;
  901. }