debug_engine.c 32 KB

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