debug_engine.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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. } WASMDebugEngine;
  22. void
  23. on_thread_stop_event(WASMDebugInstance *debug_inst, WASMExecEnv *exec_env)
  24. {
  25. os_mutex_lock(&debug_inst->wait_lock);
  26. debug_inst->stopped_thread = exec_env;
  27. if (debug_inst->current_state == DBG_LAUNCHING) {
  28. /* In launching phase, send a signal so that handle_threadstop_request
  29. * can be woken up */
  30. os_cond_signal(&debug_inst->wait_cond);
  31. }
  32. os_mutex_unlock(&debug_inst->wait_lock);
  33. }
  34. static WASMDebugEngine *g_debug_engine;
  35. static uint32 current_instance_id = 1;
  36. static uint32
  37. allocate_instance_id()
  38. {
  39. uint32 id;
  40. bh_assert(g_debug_engine);
  41. os_mutex_lock(&g_debug_engine->instance_list_lock);
  42. id = current_instance_id++;
  43. os_mutex_unlock(&g_debug_engine->instance_list_lock);
  44. return id;
  45. }
  46. static bool
  47. should_stop(WASMDebugControlThread *control_thread)
  48. {
  49. return control_thread->status != RUNNING;
  50. }
  51. static void *
  52. control_thread_routine(void *arg)
  53. {
  54. WASMDebugInstance *debug_inst = (WASMDebugInstance *)arg;
  55. WASMDebugControlThread *control_thread = NULL;
  56. control_thread = debug_inst->control_thread;
  57. bh_assert(control_thread);
  58. os_mutex_lock(&debug_inst->wait_lock);
  59. control_thread->status = RUNNING;
  60. debug_inst->id = allocate_instance_id();
  61. control_thread->debug_engine = g_debug_engine;
  62. control_thread->debug_instance = debug_inst;
  63. bh_strcpy_s(control_thread->ip_addr, sizeof(control_thread->ip_addr),
  64. g_debug_engine->ip_addr);
  65. control_thread->port =
  66. (g_debug_engine->process_base_port == 0)
  67. ? 0
  68. : g_debug_engine->process_base_port + debug_inst->id;
  69. LOG_WARNING("control thread of debug object %p start\n", debug_inst);
  70. control_thread->server =
  71. wasm_create_gdbserver(control_thread->ip_addr, &control_thread->port);
  72. if (!control_thread->server) {
  73. LOG_ERROR("Failed to create debug server\n");
  74. os_cond_signal(&debug_inst->wait_cond);
  75. os_mutex_unlock(&debug_inst->wait_lock);
  76. return NULL;
  77. }
  78. control_thread->server->thread = control_thread;
  79. /*
  80. * wasm gdbserver created, the execution thread
  81. * doesn't need to wait for the debugger connection,
  82. * so we wake up the execution thread before listen
  83. */
  84. os_cond_signal(&debug_inst->wait_cond);
  85. os_mutex_unlock(&debug_inst->wait_lock);
  86. /* wait lldb client to connect */
  87. if (!wasm_gdbserver_listen(control_thread->server)) {
  88. LOG_ERROR("Failed while connecting debugger\n");
  89. wasm_runtime_free(control_thread->server);
  90. return NULL;
  91. }
  92. while (true) {
  93. os_mutex_lock(&control_thread->wait_lock);
  94. if (!should_stop(control_thread)) {
  95. /* send thread stop reply */
  96. if (debug_inst->stopped_thread
  97. && debug_inst->current_state == APP_RUNNING) {
  98. uint32 status;
  99. korp_tid tid;
  100. status =
  101. (uint32)
  102. debug_inst->stopped_thread->current_status->signal_flag;
  103. tid = debug_inst->stopped_thread->handle;
  104. if (debug_inst->stopped_thread->current_status->running_status
  105. == STATUS_EXIT) {
  106. /* If the thread exits, report "W00" if it's the last thread
  107. * in the cluster, otherwise ignore this event */
  108. status = 0;
  109. /* By design, all the other threads should have been stopped
  110. * at this moment, so it is safe to access the
  111. * exec_env_list.len without lock */
  112. if (debug_inst->cluster->exec_env_list.len != 1) {
  113. debug_inst->stopped_thread = NULL;
  114. /* The exiting thread may wait for the signal */
  115. os_cond_signal(&debug_inst->wait_cond);
  116. os_mutex_unlock(&control_thread->wait_lock);
  117. continue;
  118. }
  119. }
  120. wasm_debug_instance_set_cur_thread(
  121. debug_inst, debug_inst->stopped_thread->handle);
  122. send_thread_stop_status(control_thread->server, status, tid);
  123. debug_inst->current_state = APP_STOPPED;
  124. debug_inst->stopped_thread = NULL;
  125. if (status == 0) {
  126. /* The exiting thread may wait for the signal */
  127. os_cond_signal(&debug_inst->wait_cond);
  128. }
  129. }
  130. /* Processing incoming requests */
  131. if (!wasm_gdbserver_handle_packet(control_thread->server)) {
  132. control_thread->status = STOPPED;
  133. }
  134. }
  135. else {
  136. os_mutex_unlock(&control_thread->wait_lock);
  137. break;
  138. }
  139. os_mutex_unlock(&control_thread->wait_lock);
  140. }
  141. LOG_VERBOSE("control thread of debug object [%p] stopped\n", debug_inst);
  142. return NULL;
  143. }
  144. static WASMDebugControlThread *
  145. wasm_debug_control_thread_create(WASMDebugInstance *debug_instance)
  146. {
  147. WASMDebugControlThread *control_thread;
  148. if (!(control_thread =
  149. wasm_runtime_malloc(sizeof(WASMDebugControlThread)))) {
  150. LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
  151. return NULL;
  152. }
  153. memset(control_thread, 0, sizeof(WASMDebugControlThread));
  154. if (os_mutex_init(&control_thread->wait_lock) != 0)
  155. goto fail;
  156. debug_instance->control_thread = control_thread;
  157. os_mutex_lock(&debug_instance->wait_lock);
  158. if (0
  159. != os_thread_create(&control_thread->tid, control_thread_routine,
  160. debug_instance, APP_THREAD_STACK_SIZE_MAX)) {
  161. os_mutex_unlock(&debug_instance->wait_lock);
  162. goto fail1;
  163. }
  164. /* wait until the debug control thread ready */
  165. os_cond_wait(&debug_instance->wait_cond, &debug_instance->wait_lock);
  166. os_mutex_unlock(&debug_instance->wait_lock);
  167. if (!control_thread->server) {
  168. os_thread_join(control_thread->tid, NULL);
  169. goto fail1;
  170. }
  171. os_mutex_lock(&g_debug_engine->instance_list_lock);
  172. /* create control thread success, append debug instance to debug engine */
  173. bh_list_insert(&g_debug_engine->debug_instance_list, debug_instance);
  174. os_mutex_unlock(&g_debug_engine->instance_list_lock);
  175. wasm_cluster_send_signal_all(debug_instance->cluster, WAMR_SIG_STOP);
  176. return control_thread;
  177. fail1:
  178. os_mutex_destroy(&control_thread->wait_lock);
  179. fail:
  180. wasm_runtime_free(control_thread);
  181. return NULL;
  182. }
  183. static void
  184. wasm_debug_control_thread_destroy(WASMDebugInstance *debug_instance)
  185. {
  186. WASMDebugControlThread *control_thread = debug_instance->control_thread;
  187. LOG_VERBOSE("stopping control thread of debug object [%p]\n",
  188. debug_instance);
  189. control_thread->status = STOPPED;
  190. os_mutex_lock(&control_thread->wait_lock);
  191. wasm_close_gdbserver(control_thread->server);
  192. os_mutex_unlock(&control_thread->wait_lock);
  193. os_thread_join(control_thread->tid, NULL);
  194. wasm_runtime_free(control_thread->server);
  195. os_mutex_destroy(&control_thread->wait_lock);
  196. wasm_runtime_free(control_thread);
  197. }
  198. static WASMDebugEngine *
  199. wasm_debug_engine_create()
  200. {
  201. WASMDebugEngine *engine;
  202. if (!(engine = wasm_runtime_malloc(sizeof(WASMDebugEngine)))) {
  203. LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
  204. return NULL;
  205. }
  206. memset(engine, 0, sizeof(WASMDebugEngine));
  207. if (os_mutex_init(&engine->instance_list_lock) != 0) {
  208. wasm_runtime_free(engine);
  209. LOG_ERROR("WASM Debug Engine error: failed to init mutex");
  210. return NULL;
  211. }
  212. /* reset current instance id */
  213. current_instance_id = 1;
  214. /* TODO: support Wasm platform in LLDB */
  215. /*
  216. engine->control_thread =
  217. wasm_debug_control_thread_create((WASMDebugObject *)engine);
  218. engine->control_thread->debug_engine = (WASMDebugObject *)engine;
  219. engine->control_thread->debug_instance = NULL;
  220. sprintf(engine->control_thread->ip_addr, "127.0.0.1");
  221. engine->control_thread->port = 1234;
  222. */
  223. bh_list_init(&engine->debug_instance_list);
  224. return engine;
  225. }
  226. void
  227. wasm_debug_engine_destroy()
  228. {
  229. if (g_debug_engine) {
  230. wasm_debug_handler_deinit();
  231. os_mutex_destroy(&g_debug_engine->instance_list_lock);
  232. wasm_runtime_free(g_debug_engine);
  233. g_debug_engine = NULL;
  234. }
  235. }
  236. bool
  237. wasm_debug_engine_init(char *ip_addr, int32 platform_port, int32 process_port)
  238. {
  239. if (wasm_debug_handler_init() != 0) {
  240. return false;
  241. }
  242. if (g_debug_engine == NULL) {
  243. g_debug_engine = wasm_debug_engine_create();
  244. }
  245. if (g_debug_engine) {
  246. process_port -= 1;
  247. g_debug_engine->platform_port =
  248. platform_port > 0 ? platform_port : 1234;
  249. g_debug_engine->process_base_port =
  250. (process_port > 0) ? process_port : 0;
  251. if (ip_addr)
  252. snprintf(g_debug_engine->ip_addr, sizeof(g_debug_engine->ip_addr),
  253. "%s", ip_addr);
  254. else
  255. snprintf(g_debug_engine->ip_addr, sizeof(g_debug_engine->ip_addr),
  256. "%s", "127.0.0.1");
  257. }
  258. else {
  259. wasm_debug_handler_deinit();
  260. }
  261. return g_debug_engine != NULL ? true : false;
  262. }
  263. /* A debug Instance is a debug "process" in gdb remote protocol
  264. and bound to a runtime cluster */
  265. WASMDebugInstance *
  266. wasm_debug_instance_create(WASMCluster *cluster)
  267. {
  268. WASMDebugInstance *instance;
  269. WASMExecEnv *exec_env = NULL;
  270. wasm_module_inst_t module_inst = NULL;
  271. if (!g_debug_engine) {
  272. return NULL;
  273. }
  274. if (!(instance = wasm_runtime_malloc(sizeof(WASMDebugInstance)))) {
  275. LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
  276. return NULL;
  277. }
  278. memset(instance, 0, sizeof(WASMDebugInstance));
  279. if (os_mutex_init(&instance->wait_lock) != 0) {
  280. goto fail1;
  281. }
  282. if (os_cond_init(&instance->wait_cond) != 0) {
  283. goto fail2;
  284. }
  285. bh_list_init(&instance->break_point_list);
  286. instance->cluster = cluster;
  287. exec_env = bh_list_first_elem(&cluster->exec_env_list);
  288. bh_assert(exec_env);
  289. instance->current_tid = exec_env->handle;
  290. module_inst = wasm_runtime_get_module_inst(exec_env);
  291. bh_assert(module_inst);
  292. /* Allocate linear memory for evaluating expressions during debugging. If
  293. * the allocation failed, the debugger will not be able to evaluate
  294. * expressions */
  295. instance->exec_mem_info.size = DEBUG_EXECUTION_MEMORY_SIZE;
  296. instance->exec_mem_info.start_offset = wasm_runtime_module_malloc(
  297. module_inst, instance->exec_mem_info.size, NULL);
  298. if (instance->exec_mem_info.start_offset == 0) {
  299. LOG_WARNING(
  300. "WASM Debug Engine warning: failed to allocate linear memory for "
  301. "execution. \n"
  302. "Will not be able to evaluate expressions during "
  303. "debugging");
  304. }
  305. instance->exec_mem_info.current_pos = instance->exec_mem_info.start_offset;
  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. 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. if (!g_debug_engine) {
  678. return NULL;
  679. }
  680. os_mutex_lock(&g_debug_engine->instance_list_lock);
  681. instance = bh_list_first_elem(&g_debug_engine->debug_instance_list);
  682. while (instance) {
  683. if (instance->cluster == exec_env->cluster)
  684. break;
  685. instance = bh_list_elem_next(instance);
  686. }
  687. os_mutex_unlock(&g_debug_engine->instance_list_lock);
  688. return instance;
  689. }
  690. uint32
  691. wasm_debug_instance_get_call_stack_pcs(WASMDebugInstance *instance,
  692. korp_tid tid, uint64 buf[], uint64 size)
  693. {
  694. WASMExecEnv *exec_env;
  695. struct WASMInterpFrame *frame;
  696. uint32 i = 0;
  697. if (!instance)
  698. return 0;
  699. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  700. while (exec_env) {
  701. if (exec_env->handle == tid) {
  702. WASMModuleInstance *module_inst =
  703. (WASMModuleInstance *)exec_env->module_inst;
  704. frame = exec_env->cur_frame;
  705. while (frame && i < size) {
  706. if (frame->ip != NULL) {
  707. buf[i++] =
  708. WASM_ADDR(WasmObj, instance->id,
  709. (frame->ip - module_inst->module->load_addr));
  710. }
  711. frame = frame->prev_frame;
  712. }
  713. return i;
  714. }
  715. exec_env = bh_list_elem_next(exec_env);
  716. }
  717. return 0;
  718. }
  719. bool
  720. wasm_debug_instance_add_breakpoint(WASMDebugInstance *instance, uint64 addr,
  721. uint64 length)
  722. {
  723. WASMExecEnv *exec_env;
  724. WASMModuleInstance *module_inst;
  725. uint64 offset;
  726. if (!instance)
  727. return false;
  728. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  729. if (!exec_env)
  730. return false;
  731. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  732. if (WASM_ADDR_TYPE(addr) != WasmObj)
  733. return false;
  734. offset = WASM_ADDR_OFFSET(addr);
  735. if (length >= sizeof(break_instr)) {
  736. if (offset + sizeof(break_instr) <= module_inst->module->load_size) {
  737. WASMDebugBreakPoint *breakpoint;
  738. if (!(breakpoint =
  739. wasm_runtime_malloc(sizeof(WASMDebugBreakPoint)))) {
  740. LOG_ERROR("WASM Debug Engine error: failed to allocate memory");
  741. return false;
  742. }
  743. memset(breakpoint, 0, sizeof(WASMDebugBreakPoint));
  744. breakpoint->addr = offset;
  745. /* TODO: how to if more than one breakpoints are set
  746. at the same addr? */
  747. bh_memcpy_s(&breakpoint->orignal_data, (uint32)sizeof(break_instr),
  748. module_inst->module->load_addr + offset,
  749. (uint32)sizeof(break_instr));
  750. bh_memcpy_s(module_inst->module->load_addr + offset,
  751. (uint32)sizeof(break_instr), break_instr,
  752. (uint32)sizeof(break_instr));
  753. bh_list_insert(&instance->break_point_list, breakpoint);
  754. return true;
  755. }
  756. }
  757. return false;
  758. }
  759. bool
  760. wasm_debug_instance_remove_breakpoint(WASMDebugInstance *instance, uint64 addr,
  761. uint64 length)
  762. {
  763. WASMExecEnv *exec_env;
  764. WASMModuleInstance *module_inst;
  765. uint64 offset;
  766. if (!instance)
  767. return false;
  768. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  769. if (!exec_env)
  770. return false;
  771. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  772. if (WASM_ADDR_TYPE(addr) != WasmObj)
  773. return false;
  774. offset = WASM_ADDR_OFFSET(addr);
  775. if (length >= sizeof(break_instr)) {
  776. if (offset + sizeof(break_instr) <= module_inst->module->load_size) {
  777. WASMDebugBreakPoint *breakpoint =
  778. bh_list_first_elem(&instance->break_point_list);
  779. while (breakpoint) {
  780. WASMDebugBreakPoint *next_break = bh_list_elem_next(breakpoint);
  781. if (breakpoint->addr == offset) {
  782. /* TODO: how to if more than one breakpoints are set
  783. at the same addr? */
  784. bh_memcpy_s(module_inst->module->load_addr + offset,
  785. (uint32)sizeof(break_instr),
  786. &breakpoint->orignal_data,
  787. (uint32)sizeof(break_instr));
  788. bh_list_remove(&instance->break_point_list, breakpoint);
  789. wasm_runtime_free(breakpoint);
  790. }
  791. breakpoint = next_break;
  792. }
  793. }
  794. }
  795. return true;
  796. }
  797. bool
  798. wasm_debug_instance_continue(WASMDebugInstance *instance)
  799. {
  800. WASMExecEnv *exec_env;
  801. if (!instance)
  802. return false;
  803. if (instance->current_state == APP_RUNNING) {
  804. LOG_VERBOSE("Already in running state, ignore continue request");
  805. return false;
  806. }
  807. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  808. if (!exec_env)
  809. return false;
  810. while (exec_env) {
  811. wasm_cluster_thread_continue(exec_env);
  812. exec_env = bh_list_elem_next(exec_env);
  813. }
  814. instance->current_state = APP_RUNNING;
  815. return true;
  816. }
  817. bool
  818. wasm_debug_instance_interrupt_all_threads(WASMDebugInstance *instance)
  819. {
  820. WASMExecEnv *exec_env;
  821. if (!instance)
  822. return false;
  823. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  824. if (!exec_env)
  825. return false;
  826. while (exec_env) {
  827. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TRAP);
  828. exec_env = bh_list_elem_next(exec_env);
  829. }
  830. return true;
  831. }
  832. bool
  833. wasm_debug_instance_kill(WASMDebugInstance *instance)
  834. {
  835. WASMExecEnv *exec_env;
  836. if (!instance)
  837. return false;
  838. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  839. if (!exec_env)
  840. return false;
  841. while (exec_env) {
  842. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TERM);
  843. if (instance->current_state == APP_STOPPED) {
  844. /* Resume all threads so they can receive the TERM signal */
  845. os_mutex_lock(&exec_env->wait_lock);
  846. exec_env->current_status->running_status = STATUS_RUNNING;
  847. os_cond_signal(&exec_env->wait_cond);
  848. os_mutex_unlock(&exec_env->wait_lock);
  849. }
  850. exec_env = bh_list_elem_next(exec_env);
  851. }
  852. instance->current_state = APP_RUNNING;
  853. return true;
  854. }
  855. bool
  856. wasm_debug_instance_singlestep(WASMDebugInstance *instance, korp_tid tid)
  857. {
  858. WASMExecEnv *exec_env;
  859. if (!instance)
  860. return false;
  861. if (instance->current_state == APP_RUNNING) {
  862. LOG_VERBOSE("Already in running state, ignore step request");
  863. return false;
  864. }
  865. exec_env = bh_list_first_elem(&instance->cluster->exec_env_list);
  866. if (!exec_env)
  867. return false;
  868. while (exec_env) {
  869. if (exec_env->handle == tid || tid == (korp_tid)(uintptr_t)~0LL) {
  870. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_SINGSTEP);
  871. wasm_cluster_thread_step(exec_env);
  872. }
  873. exec_env = bh_list_elem_next(exec_env);
  874. }
  875. instance->current_state = APP_RUNNING;
  876. return true;
  877. }
  878. bool
  879. wasm_debug_instance_get_local(WASMDebugInstance *instance, int32 frame_index,
  880. int32 local_index, char buf[], int32 *size)
  881. {
  882. WASMExecEnv *exec_env;
  883. struct WASMInterpFrame *frame;
  884. WASMFunctionInstance *cur_func;
  885. uint8 local_type = 0xFF;
  886. uint32 local_offset;
  887. int32 param_count;
  888. int32 fi = 0;
  889. if (!instance)
  890. return false;
  891. exec_env = wasm_debug_instance_get_current_env(instance);
  892. if (!exec_env)
  893. return false;
  894. frame = exec_env->cur_frame;
  895. while (frame && fi++ != frame_index) {
  896. frame = frame->prev_frame;
  897. }
  898. if (!frame)
  899. return false;
  900. cur_func = frame->function;
  901. if (!cur_func)
  902. return false;
  903. param_count = cur_func->param_count;
  904. if (local_index >= param_count + cur_func->local_count)
  905. return false;
  906. local_offset = cur_func->local_offsets[local_index];
  907. if (local_index < param_count)
  908. local_type = cur_func->param_types[local_index];
  909. else if (local_index < cur_func->local_count + param_count)
  910. local_type = cur_func->local_types[local_index - param_count];
  911. switch (local_type) {
  912. case VALUE_TYPE_I32:
  913. case VALUE_TYPE_F32:
  914. *size = 4;
  915. bh_memcpy_s(buf, 4, (char *)(frame->lp + local_offset), 4);
  916. break;
  917. case VALUE_TYPE_I64:
  918. case VALUE_TYPE_F64:
  919. *size = 8;
  920. bh_memcpy_s(buf, 8, (char *)(frame->lp + local_offset), 8);
  921. break;
  922. default:
  923. *size = 0;
  924. break;
  925. }
  926. return true;
  927. }
  928. bool
  929. wasm_debug_instance_get_global(WASMDebugInstance *instance, int32 frame_index,
  930. int32 global_index, char buf[], int32 *size)
  931. {
  932. WASMExecEnv *exec_env;
  933. struct WASMInterpFrame *frame;
  934. WASMModuleInstance *module_inst;
  935. WASMGlobalInstance *globals, *global;
  936. uint8 *global_addr;
  937. uint8 global_type = 0xFF;
  938. uint8 *global_data;
  939. int32 fi = 0;
  940. if (!instance)
  941. return false;
  942. exec_env = wasm_debug_instance_get_current_env(instance);
  943. if (!exec_env)
  944. return false;
  945. frame = exec_env->cur_frame;
  946. while (frame && fi++ != frame_index) {
  947. frame = frame->prev_frame;
  948. }
  949. if (!frame)
  950. return false;
  951. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  952. global_data = module_inst->global_data;
  953. globals = module_inst->globals;
  954. if ((global_index < 0)
  955. || ((uint32)global_index >= module_inst->global_count)) {
  956. return false;
  957. }
  958. global = globals + global_index;
  959. #if WASM_ENABLE_MULTI_MODULE == 0
  960. global_addr = global_data + global->data_offset;
  961. #else
  962. global_addr = global->import_global_inst
  963. ? global->import_module_inst->global_data
  964. + global->import_global_inst->data_offset
  965. : global_data + global->data_offset;
  966. #endif
  967. global_type = global->type;
  968. switch (global_type) {
  969. case VALUE_TYPE_I32:
  970. case VALUE_TYPE_F32:
  971. *size = 4;
  972. bh_memcpy_s(buf, 4, (char *)(global_addr), 4);
  973. break;
  974. case VALUE_TYPE_I64:
  975. case VALUE_TYPE_F64:
  976. *size = 8;
  977. bh_memcpy_s(buf, 8, (char *)(global_addr), 8);
  978. break;
  979. default:
  980. *size = 0;
  981. break;
  982. }
  983. return true;
  984. }
  985. uint64
  986. wasm_debug_instance_mmap(WASMDebugInstance *instance, uint32 size,
  987. int32 map_prot)
  988. {
  989. WASMExecEnv *exec_env;
  990. uint32 offset = 0;
  991. (void)map_prot;
  992. if (!instance)
  993. return 0;
  994. exec_env = wasm_debug_instance_get_current_env(instance);
  995. if (!exec_env)
  996. return 0;
  997. if (instance->exec_mem_info.start_offset == 0) {
  998. return 0;
  999. }
  1000. if ((uint64)instance->exec_mem_info.current_pos
  1001. - instance->exec_mem_info.start_offset + size
  1002. <= (uint64)instance->exec_mem_info.size) {
  1003. offset = instance->exec_mem_info.current_pos;
  1004. instance->exec_mem_info.current_pos += size;
  1005. }
  1006. if (offset == 0) {
  1007. LOG_WARNING("the memory may be not enough for debug, try use larger "
  1008. "--heap-size");
  1009. return 0;
  1010. }
  1011. return WASM_ADDR(WasmMemory, 0, offset);
  1012. }
  1013. bool
  1014. wasm_debug_instance_ummap(WASMDebugInstance *instance, uint64 addr)
  1015. {
  1016. WASMExecEnv *exec_env;
  1017. if (!instance)
  1018. return false;
  1019. exec_env = wasm_debug_instance_get_current_env(instance);
  1020. if (!exec_env)
  1021. return false;
  1022. if (instance->exec_mem_info.start_offset == 0) {
  1023. return false;
  1024. }
  1025. (void)addr;
  1026. /* Currently we don't support to free the execution memory, simply return
  1027. * true here */
  1028. return true;
  1029. }