handler.c 22 KB

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