handler.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /*
  2. * Copyright (C) 2021 Ant Group. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_platform.h"
  6. #include "handler.h"
  7. #include "debug_engine.h"
  8. #include "packets.h"
  9. #include "utils.h"
  10. #include "wasm_runtime.h"
  11. /*
  12. * Note: A moderate MAX_PACKET_SIZE is ok because
  13. * LLDB queries our buffer size (via qSupported PacketSize)
  14. * and limits packet sizes accordingly.
  15. */
  16. #if defined(DEBUG_MAX_PACKET_SIZE)
  17. #define MAX_PACKET_SIZE DEBUG_MAX_PACKET_SIZE
  18. #else
  19. #define MAX_PACKET_SIZE (4096)
  20. #endif
  21. /*
  22. * Note: It's assumed that MAX_PACKET_SIZE is reasonably large.
  23. * See GetWorkingDir, WasmCallStack, etc.
  24. */
  25. #if MAX_PACKET_SIZE < PATH_MAX || MAX_PACKET_SIZE < (2048 + 1)
  26. #error MAX_PACKET_SIZE is too small
  27. #endif
  28. static char *tmpbuf;
  29. static korp_mutex tmpbuf_lock;
  30. int
  31. wasm_debug_handler_init()
  32. {
  33. int ret;
  34. tmpbuf = wasm_runtime_malloc(MAX_PACKET_SIZE);
  35. if (tmpbuf == NULL) {
  36. LOG_ERROR("debug-engine: Packet buffer allocation failure");
  37. return BHT_ERROR;
  38. }
  39. ret = os_mutex_init(&tmpbuf_lock);
  40. if (ret != BHT_OK) {
  41. wasm_runtime_free(tmpbuf);
  42. tmpbuf = NULL;
  43. }
  44. return ret;
  45. }
  46. void
  47. wasm_debug_handler_deinit()
  48. {
  49. wasm_runtime_free(tmpbuf);
  50. tmpbuf = NULL;
  51. os_mutex_destroy(&tmpbuf_lock);
  52. }
  53. void
  54. handle_interrupt(WASMGDBServer *server)
  55. {
  56. wasm_debug_instance_interrupt_all_threads(server->thread->debug_instance);
  57. }
  58. void
  59. handle_general_set(WASMGDBServer *server, char *payload)
  60. {
  61. const char *name;
  62. char *args;
  63. args = strchr(payload, ':');
  64. if (args)
  65. *args++ = '\0';
  66. name = payload;
  67. LOG_VERBOSE("%s:%s\n", __FUNCTION__, payload);
  68. if (!strcmp(name, "StartNoAckMode")) {
  69. server->noack = true;
  70. write_packet(server, "OK");
  71. }
  72. if (!strcmp(name, "ThreadSuffixSupported")) {
  73. write_packet(server, "");
  74. }
  75. if (!strcmp(name, "ListThreadsInStopReply")) {
  76. write_packet(server, "");
  77. }
  78. if (!strcmp(name, "EnableErrorStrings")) {
  79. write_packet(server, "OK");
  80. }
  81. }
  82. static void
  83. process_xfer(WASMGDBServer *server, const char *name, char *args)
  84. {
  85. const char *mode = args;
  86. args = strchr(args, ':');
  87. if (args)
  88. *args++ = '\0';
  89. if (!strcmp(name, "libraries") && !strcmp(mode, "read")) {
  90. // TODO: how to get current wasm file name?
  91. uint64 addr = wasm_debug_instance_get_load_addr(
  92. (WASMDebugInstance *)server->thread->debug_instance);
  93. os_mutex_lock(&tmpbuf_lock);
  94. #if WASM_ENABLE_LIBC_WASI != 0
  95. char objname[128];
  96. wasm_debug_instance_get_current_object_name(
  97. (WASMDebugInstance *)server->thread->debug_instance, objname, 128);
  98. snprintf(tmpbuf, MAX_PACKET_SIZE,
  99. "l<library-list><library name=\"%s\"><section "
  100. "address=\"0x%" PRIx64 "\"/></library></library-list>",
  101. objname, addr);
  102. #else
  103. snprintf(tmpbuf, MAX_PACKET_SIZE,
  104. "l<library-list><library name=\"%s\"><section "
  105. "address=\"0x%" PRIx64 "\"/></library></library-list>",
  106. "nobody.wasm", addr);
  107. #endif
  108. write_packet(server, tmpbuf);
  109. os_mutex_unlock(&tmpbuf_lock);
  110. }
  111. }
  112. void
  113. process_wasm_local(WASMGDBServer *server, char *args)
  114. {
  115. int32 frame_index;
  116. int32 local_index;
  117. char buf[16];
  118. int32 size = 16;
  119. bool ret;
  120. os_mutex_lock(&tmpbuf_lock);
  121. snprintf(tmpbuf, MAX_PACKET_SIZE, "E01");
  122. if (sscanf(args, "%" PRId32 ";%" PRId32, &frame_index, &local_index) == 2) {
  123. ret = wasm_debug_instance_get_local(
  124. (WASMDebugInstance *)server->thread->debug_instance, frame_index,
  125. local_index, buf, &size);
  126. if (ret && size > 0) {
  127. mem2hex(buf, tmpbuf, size);
  128. }
  129. }
  130. write_packet(server, tmpbuf);
  131. os_mutex_unlock(&tmpbuf_lock);
  132. }
  133. void
  134. process_wasm_global(WASMGDBServer *server, char *args)
  135. {
  136. int32 frame_index;
  137. int32 global_index;
  138. char buf[16];
  139. int32 size = 16;
  140. bool ret;
  141. os_mutex_lock(&tmpbuf_lock);
  142. snprintf(tmpbuf, MAX_PACKET_SIZE, "E01");
  143. if (sscanf(args, "%" PRId32 ";%" PRId32, &frame_index, &global_index)
  144. == 2) {
  145. ret = wasm_debug_instance_get_global(
  146. (WASMDebugInstance *)server->thread->debug_instance, frame_index,
  147. global_index, buf, &size);
  148. if (ret && size > 0) {
  149. mem2hex(buf, tmpbuf, size);
  150. }
  151. }
  152. write_packet(server, tmpbuf);
  153. os_mutex_unlock(&tmpbuf_lock);
  154. }
  155. void
  156. handle_general_query(WASMGDBServer *server, char *payload)
  157. {
  158. const char *name;
  159. char *args;
  160. char triple[256];
  161. args = strchr(payload, ':');
  162. if (args)
  163. *args++ = '\0';
  164. name = payload;
  165. LOG_VERBOSE("%s:%s\n", __FUNCTION__, payload);
  166. if (!strcmp(name, "C")) {
  167. uint64 pid, tid;
  168. pid = wasm_debug_instance_get_pid(
  169. (WASMDebugInstance *)server->thread->debug_instance);
  170. tid = (uint64)(uintptr_t)wasm_debug_instance_get_tid(
  171. (WASMDebugInstance *)server->thread->debug_instance);
  172. os_mutex_lock(&tmpbuf_lock);
  173. snprintf(tmpbuf, MAX_PACKET_SIZE, "QCp%" PRIx64 ".%" PRIx64 "", pid,
  174. tid);
  175. write_packet(server, tmpbuf);
  176. os_mutex_unlock(&tmpbuf_lock);
  177. }
  178. if (!strcmp(name, "Supported")) {
  179. os_mutex_lock(&tmpbuf_lock);
  180. snprintf(tmpbuf, MAX_PACKET_SIZE,
  181. "qXfer:libraries:read+;PacketSize=%" PRIx32 ";",
  182. MAX_PACKET_SIZE);
  183. write_packet(server, tmpbuf);
  184. os_mutex_unlock(&tmpbuf_lock);
  185. }
  186. if (!strcmp(name, "Xfer")) {
  187. name = args;
  188. if (!args) {
  189. LOG_ERROR("payload parse error during handle_general_query");
  190. return;
  191. }
  192. args = strchr(args, ':');
  193. if (args) {
  194. *args++ = '\0';
  195. process_xfer(server, name, args);
  196. }
  197. }
  198. if (!strcmp(name, "HostInfo")) {
  199. mem2hex("wasm32-wamr-wasi-wasm", triple,
  200. strlen("wasm32-wamr-wasi-wasm"));
  201. os_mutex_lock(&tmpbuf_lock);
  202. snprintf(tmpbuf, MAX_PACKET_SIZE,
  203. "vendor:wamr;ostype:wasi;arch:wasm32;"
  204. "triple:%s;endian:little;ptrsize:4;",
  205. triple);
  206. write_packet(server, tmpbuf);
  207. os_mutex_unlock(&tmpbuf_lock);
  208. }
  209. if (!strcmp(name, "ModuleInfo")) {
  210. write_packet(server, "");
  211. }
  212. if (!strcmp(name, "GetWorkingDir")) {
  213. os_mutex_lock(&tmpbuf_lock);
  214. if (getcwd(tmpbuf, PATH_MAX))
  215. write_packet(server, tmpbuf);
  216. os_mutex_unlock(&tmpbuf_lock);
  217. }
  218. if (!strcmp(name, "QueryGDBServer")) {
  219. write_packet(server, "");
  220. }
  221. if (!strcmp(name, "VAttachOrWaitSupported")) {
  222. write_packet(server, "");
  223. }
  224. if (!strcmp(name, "ProcessInfo")) {
  225. // Todo: process id parent-pid
  226. uint64 pid;
  227. pid = wasm_debug_instance_get_pid(
  228. (WASMDebugInstance *)server->thread->debug_instance);
  229. mem2hex("wasm32-wamr-wasi-wasm", triple,
  230. strlen("wasm32-wamr-wasi-wasm"));
  231. os_mutex_lock(&tmpbuf_lock);
  232. snprintf(tmpbuf, MAX_PACKET_SIZE,
  233. "pid:%" PRIx64 ";parent-pid:%" PRIx64
  234. ";vendor:wamr;ostype:wasi;arch:wasm32;"
  235. "triple:%s;endian:little;ptrsize:4;",
  236. pid, pid, triple);
  237. write_packet(server, tmpbuf);
  238. os_mutex_unlock(&tmpbuf_lock);
  239. }
  240. if (!strcmp(name, "RegisterInfo0")) {
  241. os_mutex_lock(&tmpbuf_lock);
  242. snprintf(
  243. tmpbuf, MAX_PACKET_SIZE,
  244. "name:pc;alt-name:pc;bitsize:64;offset:0;encoding:uint;format:hex;"
  245. "set:General Purpose Registers;gcc:16;dwarf:16;generic:pc;");
  246. write_packet(server, tmpbuf);
  247. os_mutex_unlock(&tmpbuf_lock);
  248. }
  249. else if (!strncmp(name, "RegisterInfo", strlen("RegisterInfo"))) {
  250. write_packet(server, "E45");
  251. }
  252. if (!strcmp(name, "StructuredDataPlugins")) {
  253. write_packet(server, "");
  254. }
  255. if (args && (!strcmp(name, "MemoryRegionInfo"))) {
  256. uint64 addr = strtoll(args, NULL, 16);
  257. WASMDebugMemoryInfo *mem_info = wasm_debug_instance_get_memregion(
  258. (WASMDebugInstance *)server->thread->debug_instance, addr);
  259. if (mem_info) {
  260. char name_buf[256];
  261. mem2hex(mem_info->name, name_buf, strlen(mem_info->name));
  262. os_mutex_lock(&tmpbuf_lock);
  263. snprintf(tmpbuf, MAX_PACKET_SIZE,
  264. "start:%" PRIx64 ";size:%" PRIx64
  265. ";permissions:%s;name:%s;",
  266. (uint64)mem_info->start, mem_info->size,
  267. mem_info->permisson, name_buf);
  268. write_packet(server, tmpbuf);
  269. os_mutex_unlock(&tmpbuf_lock);
  270. wasm_debug_instance_destroy_memregion(
  271. (WASMDebugInstance *)server->thread->debug_instance, mem_info);
  272. }
  273. }
  274. if (!strcmp(name, "WasmData")) {
  275. }
  276. if (!strcmp(name, "WasmMem")) {
  277. }
  278. if (!strcmp(name, "Symbol")) {
  279. write_packet(server, "");
  280. }
  281. if (args && (!strcmp(name, "WasmCallStack"))) {
  282. uint64 tid = strtoll(args, NULL, 16);
  283. uint64 buf[1024 / sizeof(uint64)];
  284. uint32 count = wasm_debug_instance_get_call_stack_pcs(
  285. (WASMDebugInstance *)server->thread->debug_instance,
  286. (korp_tid)(uintptr_t)tid, buf, 1024 / sizeof(uint64));
  287. if (count > 0) {
  288. os_mutex_lock(&tmpbuf_lock);
  289. mem2hex((char *)buf, tmpbuf, count * sizeof(uint64));
  290. write_packet(server, tmpbuf);
  291. os_mutex_unlock(&tmpbuf_lock);
  292. }
  293. else
  294. write_packet(server, "");
  295. }
  296. if (args && (!strcmp(name, "WasmLocal"))) {
  297. process_wasm_local(server, args);
  298. }
  299. if (args && (!strcmp(name, "WasmGlobal"))) {
  300. process_wasm_global(server, args);
  301. }
  302. if (!strcmp(name, "Offsets")) {
  303. write_packet(server, "");
  304. }
  305. if (!strncmp(name, "ThreadStopInfo", strlen("ThreadStopInfo"))) {
  306. int32 prefix_len = strlen("ThreadStopInfo");
  307. uint64 tid_number = strtoll(name + prefix_len, NULL, 16);
  308. korp_tid tid = (korp_tid)(uintptr_t)tid_number;
  309. uint32 status;
  310. status = wasm_debug_instance_get_thread_status(
  311. server->thread->debug_instance, tid);
  312. send_thread_stop_status(server, status, tid);
  313. }
  314. }
  315. void
  316. send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid)
  317. {
  318. int32 len = 0;
  319. uint64 pc;
  320. korp_tid tids[20];
  321. char pc_string[17];
  322. uint32 tids_count, i = 0;
  323. uint32 gdb_status = status;
  324. WASMExecEnv *exec_env;
  325. const char *exception;
  326. if (status == 0) {
  327. os_mutex_lock(&tmpbuf_lock);
  328. snprintf(tmpbuf, MAX_PACKET_SIZE, "W%02x", status);
  329. write_packet(server, tmpbuf);
  330. os_mutex_unlock(&tmpbuf_lock);
  331. return;
  332. }
  333. tids_count = wasm_debug_instance_get_tids(
  334. (WASMDebugInstance *)server->thread->debug_instance, tids, 20);
  335. pc = wasm_debug_instance_get_pc(
  336. (WASMDebugInstance *)server->thread->debug_instance);
  337. if (status == WAMR_SIG_SINGSTEP) {
  338. gdb_status = WAMR_SIG_TRAP;
  339. }
  340. os_mutex_lock(&tmpbuf_lock);
  341. // TODO: how name a wasm thread?
  342. len += snprintf(tmpbuf, MAX_PACKET_SIZE, "T%02xthread:%" PRIx64 ";name:%s;",
  343. gdb_status, (uint64)(uintptr_t)tid, "nobody");
  344. if (tids_count > 0) {
  345. len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "threads:");
  346. while (i < tids_count) {
  347. if (i == tids_count - 1)
  348. len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
  349. "%" PRIx64 ";", (uint64)(uintptr_t)tids[i]);
  350. else
  351. len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
  352. "%" PRIx64 ",", (uint64)(uintptr_t)tids[i]);
  353. i++;
  354. }
  355. }
  356. mem2hex((void *)&pc, pc_string, 8);
  357. pc_string[8 * 2] = '\0';
  358. exec_env = wasm_debug_instance_get_current_env(
  359. (WASMDebugInstance *)server->thread->debug_instance);
  360. bh_assert(exec_env);
  361. exception =
  362. wasm_runtime_get_exception(wasm_runtime_get_module_inst(exec_env));
  363. if (exception) {
  364. /* When exception occurs, use reason:exception so the description can be
  365. * correctly processed by LLDB */
  366. uint32 exception_len = strlen(exception);
  367. len +=
  368. snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
  369. "thread-pcs:%" PRIx64 ";00:%s;reason:%s;description:", pc,
  370. pc_string, "exception");
  371. /* The description should be encoded as HEX */
  372. for (i = 0; i < exception_len; i++) {
  373. len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "%02x",
  374. exception[i]);
  375. }
  376. len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, ";");
  377. }
  378. else {
  379. if (status == WAMR_SIG_TRAP) {
  380. len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
  381. "thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
  382. pc_string, "breakpoint");
  383. }
  384. else if (status == WAMR_SIG_SINGSTEP) {
  385. len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
  386. "thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
  387. pc_string, "trace");
  388. }
  389. else if (status > 0) {
  390. len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
  391. "thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
  392. pc_string, "signal");
  393. }
  394. }
  395. write_packet(server, tmpbuf);
  396. os_mutex_unlock(&tmpbuf_lock);
  397. }
  398. void
  399. handle_v_packet(WASMGDBServer *server, char *payload)
  400. {
  401. const char *name;
  402. char *args;
  403. args = strchr(payload, ';');
  404. if (args)
  405. *args++ = '\0';
  406. name = payload;
  407. LOG_VERBOSE("%s:%s\n", __FUNCTION__, payload);
  408. if (!strcmp("Cont?", name))
  409. write_packet(server, "vCont;c;C;s;S;");
  410. if (!strcmp("Cont", name)) {
  411. if (args) {
  412. if (args[0] == 's' || args[0] == 'c') {
  413. char *numstring = strchr(args, ':');
  414. if (numstring) {
  415. uint64 tid_number;
  416. korp_tid tid;
  417. *numstring++ = '\0';
  418. tid_number = strtoll(numstring, NULL, 16);
  419. tid = (korp_tid)(uintptr_t)tid_number;
  420. wasm_debug_instance_set_cur_thread(
  421. (WASMDebugInstance *)server->thread->debug_instance,
  422. tid);
  423. if (args[0] == 's') {
  424. wasm_debug_instance_singlestep(
  425. (WASMDebugInstance *)server->thread->debug_instance,
  426. tid);
  427. }
  428. else {
  429. wasm_debug_instance_continue(
  430. (WASMDebugInstance *)
  431. server->thread->debug_instance);
  432. }
  433. }
  434. }
  435. }
  436. }
  437. }
  438. void
  439. handle_threadstop_request(WASMGDBServer *server, char *payload)
  440. {
  441. korp_tid tid;
  442. uint32 status;
  443. WASMDebugInstance *debug_inst =
  444. (WASMDebugInstance *)server->thread->debug_instance;
  445. bh_assert(debug_inst);
  446. /* According to
  447. https://sourceware.org/gdb/onlinedocs/gdb/Packets.html#Packets, the "?"
  448. package should be sent when connection is first established to query the
  449. reason the target halted */
  450. bh_assert(debug_inst->current_state == DBG_LAUNCHING);
  451. /* Waiting for the stop event */
  452. os_mutex_lock(&debug_inst->wait_lock);
  453. while (!debug_inst->stopped_thread) {
  454. os_cond_wait(&debug_inst->wait_cond, &debug_inst->wait_lock);
  455. }
  456. os_mutex_unlock(&debug_inst->wait_lock);
  457. tid = debug_inst->stopped_thread->handle;
  458. status = (uint32)debug_inst->stopped_thread->current_status->signal_flag;
  459. wasm_debug_instance_set_cur_thread(debug_inst, tid);
  460. send_thread_stop_status(server, status, tid);
  461. debug_inst->current_state = APP_STOPPED;
  462. debug_inst->stopped_thread = NULL;
  463. }
  464. void
  465. handle_set_current_thread(WASMGDBServer *server, char *payload)
  466. {
  467. LOG_VERBOSE("%s:%s\n", __FUNCTION__, payload);
  468. if ('g' == *payload++) {
  469. uint64 tid = strtoll(payload, NULL, 16);
  470. if (tid > 0)
  471. wasm_debug_instance_set_cur_thread(
  472. (WASMDebugInstance *)server->thread->debug_instance,
  473. (korp_tid)(uintptr_t)tid);
  474. }
  475. write_packet(server, "OK");
  476. }
  477. void
  478. handle_get_register(WASMGDBServer *server, char *payload)
  479. {
  480. uint64 regdata;
  481. int32 i = strtol(payload, NULL, 16);
  482. if (i != 0) {
  483. write_packet(server, "E01");
  484. return;
  485. }
  486. regdata = wasm_debug_instance_get_pc(
  487. (WASMDebugInstance *)server->thread->debug_instance);
  488. os_mutex_lock(&tmpbuf_lock);
  489. mem2hex((void *)&regdata, tmpbuf, 8);
  490. tmpbuf[8 * 2] = '\0';
  491. write_packet(server, tmpbuf);
  492. os_mutex_unlock(&tmpbuf_lock);
  493. }
  494. void
  495. handle_get_json_request(WASMGDBServer *server, char *payload)
  496. {
  497. char *args;
  498. args = strchr(payload, ':');
  499. if (args)
  500. *args++ = '\0';
  501. write_packet(server, "");
  502. }
  503. void
  504. handle_get_read_binary_memory(WASMGDBServer *server, char *payload)
  505. {
  506. write_packet(server, "");
  507. }
  508. void
  509. handle_get_read_memory(WASMGDBServer *server, char *payload)
  510. {
  511. uint64 maddr, mlen;
  512. bool ret;
  513. os_mutex_lock(&tmpbuf_lock);
  514. snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "");
  515. if (sscanf(payload, "%" SCNx64 ",%" SCNx64, &maddr, &mlen) == 2) {
  516. char *buff;
  517. if (mlen * 2 > MAX_PACKET_SIZE) {
  518. LOG_ERROR("Buffer overflow!");
  519. mlen = MAX_PACKET_SIZE / 2;
  520. }
  521. buff = wasm_runtime_malloc(mlen);
  522. if (buff) {
  523. ret = wasm_debug_instance_get_mem(
  524. (WASMDebugInstance *)server->thread->debug_instance, maddr,
  525. buff, &mlen);
  526. if (ret) {
  527. mem2hex(buff, tmpbuf, mlen);
  528. }
  529. wasm_runtime_free(buff);
  530. }
  531. }
  532. write_packet(server, tmpbuf);
  533. os_mutex_unlock(&tmpbuf_lock);
  534. }
  535. void
  536. handle_get_write_memory(WASMGDBServer *server, char *payload)
  537. {
  538. size_t hex_len;
  539. int32 offset, act_len;
  540. uint64 maddr, mlen;
  541. char *buff;
  542. bool ret;
  543. os_mutex_lock(&tmpbuf_lock);
  544. snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "");
  545. if (sscanf(payload, "%" SCNx64 ",%" SCNx64 ":%n", &maddr, &mlen, &offset)
  546. == 2) {
  547. payload += offset;
  548. hex_len = strlen(payload);
  549. act_len = hex_len / 2 < mlen ? hex_len / 2 : mlen;
  550. buff = wasm_runtime_malloc(act_len);
  551. if (buff) {
  552. hex2mem(payload, buff, act_len);
  553. ret = wasm_debug_instance_set_mem(
  554. (WASMDebugInstance *)server->thread->debug_instance, maddr,
  555. buff, &mlen);
  556. if (ret) {
  557. snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "OK");
  558. }
  559. wasm_runtime_free(buff);
  560. }
  561. }
  562. write_packet(server, tmpbuf);
  563. os_mutex_unlock(&tmpbuf_lock);
  564. }
  565. void
  566. handle_add_break(WASMGDBServer *server, char *payload)
  567. {
  568. size_t type, length;
  569. uint64 addr;
  570. if (sscanf(payload, "%zx,%" SCNx64 ",%zx", &type, &addr, &length) == 3) {
  571. if (type == eBreakpointSoftware) {
  572. bool ret = wasm_debug_instance_add_breakpoint(
  573. (WASMDebugInstance *)server->thread->debug_instance, addr,
  574. length);
  575. if (ret)
  576. write_packet(server, "OK");
  577. else
  578. write_packet(server, "E01");
  579. return;
  580. }
  581. }
  582. write_packet(server, "");
  583. }
  584. void
  585. handle_remove_break(WASMGDBServer *server, char *payload)
  586. {
  587. size_t type, length;
  588. uint64 addr;
  589. if (sscanf(payload, "%zx,%" SCNx64 ",%zx", &type, &addr, &length) == 3) {
  590. if (type == eBreakpointSoftware) {
  591. bool ret = wasm_debug_instance_remove_breakpoint(
  592. (WASMDebugInstance *)server->thread->debug_instance, addr,
  593. length);
  594. if (ret)
  595. write_packet(server, "OK");
  596. else
  597. write_packet(server, "E01");
  598. return;
  599. }
  600. }
  601. write_packet(server, "");
  602. }
  603. void
  604. handle_continue_request(WASMGDBServer *server, char *payload)
  605. {
  606. wasm_debug_instance_continue(
  607. (WASMDebugInstance *)server->thread->debug_instance);
  608. }
  609. void
  610. handle_kill_request(WASMGDBServer *server, char *payload)
  611. {
  612. wasm_debug_instance_kill(
  613. (WASMDebugInstance *)server->thread->debug_instance);
  614. }
  615. static void
  616. handle_malloc(WASMGDBServer *server, char *payload)
  617. {
  618. char *args;
  619. uint64 addr, size;
  620. int32 map_prot = MMAP_PROT_NONE;
  621. args = strstr(payload, ",");
  622. if (args) {
  623. *args++ = '\0';
  624. }
  625. else {
  626. LOG_ERROR("Payload parse error during handle malloc");
  627. return;
  628. }
  629. os_mutex_lock(&tmpbuf_lock);
  630. snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "E03");
  631. size = strtoll(payload, NULL, 16);
  632. if (size > 0) {
  633. while (*args) {
  634. if (*args == 'r') {
  635. map_prot |= MMAP_PROT_READ;
  636. }
  637. if (*args == 'w') {
  638. map_prot |= MMAP_PROT_WRITE;
  639. }
  640. if (*args == 'x') {
  641. map_prot |= MMAP_PROT_EXEC;
  642. }
  643. args++;
  644. }
  645. addr = wasm_debug_instance_mmap(
  646. (WASMDebugInstance *)server->thread->debug_instance, size,
  647. map_prot);
  648. if (addr) {
  649. snprintf(tmpbuf, MAX_PACKET_SIZE, "%" PRIx64, addr);
  650. }
  651. }
  652. write_packet(server, tmpbuf);
  653. os_mutex_unlock(&tmpbuf_lock);
  654. }
  655. static void
  656. handle_free(WASMGDBServer *server, char *payload)
  657. {
  658. uint64 addr;
  659. bool ret;
  660. os_mutex_lock(&tmpbuf_lock);
  661. snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "E03");
  662. addr = strtoll(payload, NULL, 16);
  663. ret = wasm_debug_instance_ummap(
  664. (WASMDebugInstance *)server->thread->debug_instance, addr);
  665. if (ret) {
  666. snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "OK");
  667. }
  668. write_packet(server, tmpbuf);
  669. os_mutex_unlock(&tmpbuf_lock);
  670. }
  671. void
  672. handle____request(WASMGDBServer *server, char *payload)
  673. {
  674. char *args;
  675. if (payload[0] == 'M') {
  676. args = payload + 1;
  677. handle_malloc(server, args);
  678. }
  679. if (payload[0] == 'm') {
  680. args = payload + 1;
  681. handle_free(server, args);
  682. }
  683. }