handler.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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_general_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_general_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_general_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. WASMExecEnv *exec_env;
  296. const char *exception;
  297. if (status == 0) {
  298. os_mutex_lock(&tmpbuf_lock);
  299. snprintf(tmpbuf, sizeof(tmpbuf), "W%02x", status);
  300. write_packet(server, tmpbuf);
  301. os_mutex_unlock(&tmpbuf_lock);
  302. return;
  303. }
  304. tids_count = wasm_debug_instance_get_tids(
  305. (WASMDebugInstance *)server->thread->debug_instance, tids, 20);
  306. pc = wasm_debug_instance_get_pc(
  307. (WASMDebugInstance *)server->thread->debug_instance);
  308. if (status == WAMR_SIG_SINGSTEP) {
  309. gdb_status = WAMR_SIG_TRAP;
  310. }
  311. os_mutex_lock(&tmpbuf_lock);
  312. // TODO: how name a wasm thread?
  313. len += snprintf(tmpbuf, sizeof(tmpbuf), "T%02xthread:%" PRIx64 ";name:%s;",
  314. gdb_status, (uint64)(uintptr_t)tid, "nobody");
  315. if (tids_count > 0) {
  316. len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, "threads:");
  317. while (i < tids_count) {
  318. if (i == tids_count - 1)
  319. len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
  320. "%" PRIx64 ";", (uint64)(uintptr_t)tids[i]);
  321. else
  322. len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
  323. "%" PRIx64 ",", (uint64)(uintptr_t)tids[i]);
  324. i++;
  325. }
  326. }
  327. mem2hex((void *)&pc, pc_string, 8);
  328. pc_string[8 * 2] = '\0';
  329. exec_env = wasm_debug_instance_get_current_env(
  330. (WASMDebugInstance *)server->thread->debug_instance);
  331. bh_assert(exec_env);
  332. exception =
  333. wasm_runtime_get_exception(wasm_runtime_get_module_inst(exec_env));
  334. if (exception) {
  335. /* When exception occurs, use reason:exception so the description can be
  336. * correctly processed by LLDB */
  337. uint32 exception_len = strlen(exception);
  338. len +=
  339. snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
  340. "thread-pcs:%" PRIx64 ";00:%s;reason:%s;description:", pc,
  341. pc_string, "exception");
  342. /* The description should be encoded as HEX */
  343. for (i = 0; i < exception_len; i++) {
  344. len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, "%02x",
  345. exception[i]);
  346. }
  347. len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len, ";");
  348. }
  349. else {
  350. if (status == WAMR_SIG_TRAP) {
  351. len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
  352. "thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
  353. pc_string, "breakpoint");
  354. }
  355. else if (status == WAMR_SIG_SINGSTEP) {
  356. len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
  357. "thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
  358. pc_string, "trace");
  359. }
  360. else if (status > 0) {
  361. len += snprintf(tmpbuf + len, sizeof(tmpbuf) - len,
  362. "thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
  363. pc_string, "signal");
  364. }
  365. }
  366. write_packet(server, tmpbuf);
  367. os_mutex_unlock(&tmpbuf_lock);
  368. }
  369. void
  370. handle_v_packet(WASMGDBServer *server, char *payload)
  371. {
  372. const char *name;
  373. char *args;
  374. args = strchr(payload, ';');
  375. if (args)
  376. *args++ = '\0';
  377. name = payload;
  378. LOG_VERBOSE("%s:%s\n", __FUNCTION__, payload);
  379. if (!strcmp("Cont?", name))
  380. write_packet(server, "vCont;c;C;s;S;");
  381. if (!strcmp("Cont", name)) {
  382. if (args) {
  383. if (args[0] == 's' || args[0] == 'c') {
  384. char *numstring = strchr(args, ':');
  385. if (numstring) {
  386. uint64 tid_number;
  387. korp_tid tid;
  388. *numstring++ = '\0';
  389. tid_number = strtoll(numstring, NULL, 16);
  390. tid = (korp_tid)(uintptr_t)tid_number;
  391. wasm_debug_instance_set_cur_thread(
  392. (WASMDebugInstance *)server->thread->debug_instance,
  393. tid);
  394. if (args[0] == 's') {
  395. wasm_debug_instance_singlestep(
  396. (WASMDebugInstance *)server->thread->debug_instance,
  397. tid);
  398. }
  399. else {
  400. wasm_debug_instance_continue(
  401. (WASMDebugInstance *)
  402. server->thread->debug_instance);
  403. }
  404. }
  405. }
  406. }
  407. }
  408. }
  409. void
  410. handle_threadstop_request(WASMGDBServer *server, char *payload)
  411. {
  412. korp_tid tid;
  413. uint32 status;
  414. WASMDebugInstance *debug_inst =
  415. (WASMDebugInstance *)server->thread->debug_instance;
  416. bh_assert(debug_inst);
  417. /* According to
  418. https://sourceware.org/gdb/onlinedocs/gdb/Packets.html#Packets, the "?"
  419. package should be sent when connection is first established to query the
  420. reason the target halted */
  421. bh_assert(debug_inst->current_state == DBG_LAUNCHING);
  422. /* Waiting for the stop event */
  423. os_mutex_lock(&debug_inst->wait_lock);
  424. while (!debug_inst->stopped_thread) {
  425. os_cond_wait(&debug_inst->wait_cond, &debug_inst->wait_lock);
  426. }
  427. os_mutex_unlock(&debug_inst->wait_lock);
  428. tid = debug_inst->stopped_thread->handle;
  429. status = (uint32)debug_inst->stopped_thread->current_status->signal_flag;
  430. wasm_debug_instance_set_cur_thread(debug_inst, tid);
  431. send_thread_stop_status(server, status, tid);
  432. debug_inst->current_state = APP_STOPPED;
  433. debug_inst->stopped_thread = NULL;
  434. }
  435. void
  436. handle_set_current_thread(WASMGDBServer *server, char *payload)
  437. {
  438. LOG_VERBOSE("%s:%s\n", __FUNCTION__, payload);
  439. if ('g' == *payload++) {
  440. uint64 tid = strtoll(payload, NULL, 16);
  441. if (tid > 0)
  442. wasm_debug_instance_set_cur_thread(
  443. (WASMDebugInstance *)server->thread->debug_instance,
  444. (korp_tid)(uintptr_t)tid);
  445. }
  446. write_packet(server, "OK");
  447. }
  448. void
  449. handle_get_register(WASMGDBServer *server, char *payload)
  450. {
  451. uint64 regdata;
  452. int32 i = strtol(payload, NULL, 16);
  453. if (i != 0) {
  454. write_packet(server, "E01");
  455. return;
  456. }
  457. regdata = wasm_debug_instance_get_pc(
  458. (WASMDebugInstance *)server->thread->debug_instance);
  459. os_mutex_lock(&tmpbuf_lock);
  460. mem2hex((void *)&regdata, tmpbuf, 8);
  461. tmpbuf[8 * 2] = '\0';
  462. write_packet(server, tmpbuf);
  463. os_mutex_unlock(&tmpbuf_lock);
  464. }
  465. void
  466. handle_get_json_request(WASMGDBServer *server, char *payload)
  467. {
  468. char *args;
  469. args = strchr(payload, ':');
  470. if (args)
  471. *args++ = '\0';
  472. write_packet(server, "");
  473. }
  474. void
  475. handle_get_read_binary_memory(WASMGDBServer *server, char *payload)
  476. {
  477. write_packet(server, "");
  478. }
  479. void
  480. handle_get_read_memory(WASMGDBServer *server, char *payload)
  481. {
  482. uint64 maddr, mlen;
  483. bool ret;
  484. os_mutex_lock(&tmpbuf_lock);
  485. snprintf(tmpbuf, sizeof(tmpbuf), "%s", "");
  486. if (sscanf(payload, "%" SCNx64 ",%" SCNx64, &maddr, &mlen) == 2) {
  487. char *buff;
  488. if (mlen * 2 > MAX_PACKET_SIZE) {
  489. LOG_ERROR("Buffer overflow!");
  490. mlen = MAX_PACKET_SIZE / 2;
  491. }
  492. buff = wasm_runtime_malloc(mlen);
  493. if (buff) {
  494. ret = wasm_debug_instance_get_mem(
  495. (WASMDebugInstance *)server->thread->debug_instance, maddr,
  496. buff, &mlen);
  497. if (ret) {
  498. mem2hex(buff, tmpbuf, mlen);
  499. }
  500. wasm_runtime_free(buff);
  501. }
  502. }
  503. write_packet(server, tmpbuf);
  504. os_mutex_unlock(&tmpbuf_lock);
  505. }
  506. void
  507. handle_get_write_memory(WASMGDBServer *server, char *payload)
  508. {
  509. size_t hex_len;
  510. int32 offset, act_len;
  511. uint64 maddr, mlen;
  512. char *buff;
  513. bool ret;
  514. os_mutex_lock(&tmpbuf_lock);
  515. snprintf(tmpbuf, sizeof(tmpbuf), "%s", "");
  516. if (sscanf(payload, "%" SCNx64 ",%" SCNx64 ":%n", &maddr, &mlen, &offset)
  517. == 2) {
  518. payload += offset;
  519. hex_len = strlen(payload);
  520. act_len = hex_len / 2 < mlen ? hex_len / 2 : mlen;
  521. buff = wasm_runtime_malloc(act_len);
  522. if (buff) {
  523. hex2mem(payload, buff, act_len);
  524. ret = wasm_debug_instance_set_mem(
  525. (WASMDebugInstance *)server->thread->debug_instance, maddr,
  526. buff, &mlen);
  527. if (ret) {
  528. snprintf(tmpbuf, sizeof(tmpbuf), "%s", "OK");
  529. }
  530. wasm_runtime_free(buff);
  531. }
  532. }
  533. write_packet(server, tmpbuf);
  534. os_mutex_unlock(&tmpbuf_lock);
  535. }
  536. void
  537. handle_add_break(WASMGDBServer *server, char *payload)
  538. {
  539. size_t type, length;
  540. uint64 addr;
  541. if (sscanf(payload, "%zx,%" SCNx64 ",%zx", &type, &addr, &length) == 3) {
  542. if (type == eBreakpointSoftware) {
  543. bool ret = wasm_debug_instance_add_breakpoint(
  544. (WASMDebugInstance *)server->thread->debug_instance, addr,
  545. length);
  546. if (ret)
  547. write_packet(server, "OK");
  548. else
  549. write_packet(server, "E01");
  550. return;
  551. }
  552. }
  553. write_packet(server, "");
  554. }
  555. void
  556. handle_remove_break(WASMGDBServer *server, char *payload)
  557. {
  558. size_t type, length;
  559. uint64 addr;
  560. if (sscanf(payload, "%zx,%" SCNx64 ",%zx", &type, &addr, &length) == 3) {
  561. if (type == eBreakpointSoftware) {
  562. bool ret = wasm_debug_instance_remove_breakpoint(
  563. (WASMDebugInstance *)server->thread->debug_instance, addr,
  564. length);
  565. if (ret)
  566. write_packet(server, "OK");
  567. else
  568. write_packet(server, "E01");
  569. return;
  570. }
  571. }
  572. write_packet(server, "");
  573. }
  574. void
  575. handle_continue_request(WASMGDBServer *server, char *payload)
  576. {
  577. wasm_debug_instance_continue(
  578. (WASMDebugInstance *)server->thread->debug_instance);
  579. }
  580. void
  581. handle_kill_request(WASMGDBServer *server, char *payload)
  582. {
  583. wasm_debug_instance_kill(
  584. (WASMDebugInstance *)server->thread->debug_instance);
  585. }
  586. static void
  587. handle_malloc(WASMGDBServer *server, char *payload)
  588. {
  589. char *args;
  590. uint64 addr, size;
  591. int32 map_prot = MMAP_PROT_NONE;
  592. args = strstr(payload, ",");
  593. if (args) {
  594. *args++ = '\0';
  595. }
  596. else {
  597. LOG_ERROR("Payload parse error during handle malloc");
  598. return;
  599. }
  600. os_mutex_lock(&tmpbuf_lock);
  601. snprintf(tmpbuf, sizeof(tmpbuf), "%s", "E03");
  602. size = strtoll(payload, NULL, 16);
  603. if (size > 0) {
  604. while (*args) {
  605. if (*args == 'r') {
  606. map_prot |= MMAP_PROT_READ;
  607. }
  608. if (*args == 'w') {
  609. map_prot |= MMAP_PROT_WRITE;
  610. }
  611. if (*args == 'x') {
  612. map_prot |= MMAP_PROT_EXEC;
  613. }
  614. args++;
  615. }
  616. addr = wasm_debug_instance_mmap(
  617. (WASMDebugInstance *)server->thread->debug_instance, size,
  618. map_prot);
  619. if (addr) {
  620. snprintf(tmpbuf, sizeof(tmpbuf), "%" PRIx64, addr);
  621. }
  622. }
  623. write_packet(server, tmpbuf);
  624. os_mutex_unlock(&tmpbuf_lock);
  625. }
  626. static void
  627. handle_free(WASMGDBServer *server, char *payload)
  628. {
  629. uint64 addr;
  630. bool ret;
  631. os_mutex_lock(&tmpbuf_lock);
  632. snprintf(tmpbuf, sizeof(tmpbuf), "%s", "E03");
  633. addr = strtoll(payload, NULL, 16);
  634. ret = wasm_debug_instance_ummap(
  635. (WASMDebugInstance *)server->thread->debug_instance, addr);
  636. if (ret) {
  637. snprintf(tmpbuf, sizeof(tmpbuf), "%s", "OK");
  638. }
  639. write_packet(server, tmpbuf);
  640. os_mutex_unlock(&tmpbuf_lock);
  641. }
  642. void
  643. handle____request(WASMGDBServer *server, char *payload)
  644. {
  645. char *args;
  646. if (payload[0] == 'M') {
  647. args = payload + 1;
  648. handle_malloc(server, args);
  649. }
  650. if (payload[0] == 'm') {
  651. args = payload + 1;
  652. handle_free(server, args);
  653. }
  654. }