debug_engine.c 35 KB

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