handler.c 20 KB

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