handler.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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. if (!wasm_debug_instance_get_current_object_name(
  97. (WASMDebugInstance *)server->thread->debug_instance, objname,
  98. 128)) {
  99. objname[0] = 0; /* use an empty string */
  100. }
  101. snprintf(tmpbuf, MAX_PACKET_SIZE,
  102. "l<library-list><library name=\"%s\"><section "
  103. "address=\"0x%" PRIx64 "\"/></library></library-list>",
  104. objname, addr);
  105. #else
  106. snprintf(tmpbuf, MAX_PACKET_SIZE,
  107. "l<library-list><library name=\"%s\"><section "
  108. "address=\"0x%" PRIx64 "\"/></library></library-list>",
  109. "nobody.wasm", addr);
  110. #endif
  111. write_packet(server, tmpbuf);
  112. os_mutex_unlock(&tmpbuf_lock);
  113. }
  114. }
  115. void
  116. process_wasm_local(WASMGDBServer *server, char *args)
  117. {
  118. int32 frame_index;
  119. int32 local_index;
  120. char buf[16];
  121. int32 size = 16;
  122. bool ret;
  123. os_mutex_lock(&tmpbuf_lock);
  124. snprintf(tmpbuf, MAX_PACKET_SIZE, "E01");
  125. if (sscanf(args, "%" PRId32 ";%" PRId32, &frame_index, &local_index) == 2) {
  126. ret = wasm_debug_instance_get_local(
  127. (WASMDebugInstance *)server->thread->debug_instance, frame_index,
  128. local_index, buf, &size);
  129. if (ret && size > 0) {
  130. mem2hex(buf, tmpbuf, size);
  131. }
  132. }
  133. write_packet(server, tmpbuf);
  134. os_mutex_unlock(&tmpbuf_lock);
  135. }
  136. void
  137. process_wasm_global(WASMGDBServer *server, char *args)
  138. {
  139. int32 frame_index;
  140. int32 global_index;
  141. char buf[16];
  142. int32 size = 16;
  143. bool ret;
  144. os_mutex_lock(&tmpbuf_lock);
  145. snprintf(tmpbuf, MAX_PACKET_SIZE, "E01");
  146. if (sscanf(args, "%" PRId32 ";%" PRId32, &frame_index, &global_index)
  147. == 2) {
  148. ret = wasm_debug_instance_get_global(
  149. (WASMDebugInstance *)server->thread->debug_instance, frame_index,
  150. global_index, buf, &size);
  151. if (ret && size > 0) {
  152. mem2hex(buf, tmpbuf, size);
  153. }
  154. }
  155. write_packet(server, tmpbuf);
  156. os_mutex_unlock(&tmpbuf_lock);
  157. }
  158. void
  159. handle_general_query(WASMGDBServer *server, char *payload)
  160. {
  161. const char *name;
  162. char *args;
  163. char triple[256];
  164. args = strchr(payload, ':');
  165. if (args)
  166. *args++ = '\0';
  167. name = payload;
  168. LOG_VERBOSE("%s:%s\n", __FUNCTION__, payload);
  169. if (!strcmp(name, "C")) {
  170. uint64 pid, tid;
  171. pid = wasm_debug_instance_get_pid(
  172. (WASMDebugInstance *)server->thread->debug_instance);
  173. tid = (uint64)(uintptr_t)wasm_debug_instance_get_tid(
  174. (WASMDebugInstance *)server->thread->debug_instance);
  175. os_mutex_lock(&tmpbuf_lock);
  176. snprintf(tmpbuf, MAX_PACKET_SIZE, "QCp%" PRIx64 ".%" PRIx64 "", pid,
  177. tid);
  178. write_packet(server, tmpbuf);
  179. os_mutex_unlock(&tmpbuf_lock);
  180. }
  181. if (!strcmp(name, "Supported")) {
  182. os_mutex_lock(&tmpbuf_lock);
  183. snprintf(tmpbuf, MAX_PACKET_SIZE,
  184. "qXfer:libraries:read+;PacketSize=%" PRIx32 ";",
  185. MAX_PACKET_SIZE);
  186. write_packet(server, tmpbuf);
  187. os_mutex_unlock(&tmpbuf_lock);
  188. }
  189. if (!strcmp(name, "Xfer")) {
  190. name = args;
  191. if (!args) {
  192. LOG_ERROR("payload parse error during handle_general_query");
  193. return;
  194. }
  195. args = strchr(args, ':');
  196. if (args) {
  197. *args++ = '\0';
  198. process_xfer(server, name, args);
  199. }
  200. }
  201. if (!strcmp(name, "HostInfo")) {
  202. mem2hex("wasm32-wamr-wasi-wasm", triple,
  203. strlen("wasm32-wamr-wasi-wasm"));
  204. os_mutex_lock(&tmpbuf_lock);
  205. snprintf(tmpbuf, MAX_PACKET_SIZE,
  206. "vendor:wamr;ostype:wasi;arch:wasm32;"
  207. "triple:%s;endian:little;ptrsize:4;",
  208. triple);
  209. write_packet(server, tmpbuf);
  210. os_mutex_unlock(&tmpbuf_lock);
  211. }
  212. if (!strcmp(name, "ModuleInfo")) {
  213. write_packet(server, "");
  214. }
  215. if (!strcmp(name, "GetWorkingDir")) {
  216. os_mutex_lock(&tmpbuf_lock);
  217. if (getcwd(tmpbuf, PATH_MAX))
  218. write_packet(server, tmpbuf);
  219. os_mutex_unlock(&tmpbuf_lock);
  220. }
  221. if (!strcmp(name, "QueryGDBServer")) {
  222. write_packet(server, "");
  223. }
  224. if (!strcmp(name, "VAttachOrWaitSupported")) {
  225. write_packet(server, "");
  226. }
  227. if (!strcmp(name, "ProcessInfo")) {
  228. // Todo: process id parent-pid
  229. uint64 pid;
  230. pid = wasm_debug_instance_get_pid(
  231. (WASMDebugInstance *)server->thread->debug_instance);
  232. mem2hex("wasm32-wamr-wasi-wasm", triple,
  233. strlen("wasm32-wamr-wasi-wasm"));
  234. os_mutex_lock(&tmpbuf_lock);
  235. snprintf(tmpbuf, MAX_PACKET_SIZE,
  236. "pid:%" PRIx64 ";parent-pid:%" PRIx64
  237. ";vendor:wamr;ostype:wasi;arch:wasm32;"
  238. "triple:%s;endian:little;ptrsize:4;",
  239. pid, pid, triple);
  240. write_packet(server, tmpbuf);
  241. os_mutex_unlock(&tmpbuf_lock);
  242. }
  243. if (!strcmp(name, "RegisterInfo0")) {
  244. os_mutex_lock(&tmpbuf_lock);
  245. snprintf(
  246. tmpbuf, MAX_PACKET_SIZE,
  247. "name:pc;alt-name:pc;bitsize:64;offset:0;encoding:uint;format:hex;"
  248. "set:General Purpose Registers;gcc:16;dwarf:16;generic:pc;");
  249. write_packet(server, tmpbuf);
  250. os_mutex_unlock(&tmpbuf_lock);
  251. }
  252. else if (!strncmp(name, "RegisterInfo", strlen("RegisterInfo"))) {
  253. write_packet(server, "E45");
  254. }
  255. if (!strcmp(name, "StructuredDataPlugins")) {
  256. write_packet(server, "");
  257. }
  258. if (args && (!strcmp(name, "MemoryRegionInfo"))) {
  259. uint64 addr = strtoll(args, NULL, 16);
  260. WASMDebugMemoryInfo *mem_info = wasm_debug_instance_get_memregion(
  261. (WASMDebugInstance *)server->thread->debug_instance, addr);
  262. if (mem_info) {
  263. char name_buf[256];
  264. mem2hex(mem_info->name, name_buf, strlen(mem_info->name));
  265. os_mutex_lock(&tmpbuf_lock);
  266. snprintf(tmpbuf, MAX_PACKET_SIZE,
  267. "start:%" PRIx64 ";size:%" PRIx64
  268. ";permissions:%s;name:%s;",
  269. (uint64)mem_info->start, mem_info->size,
  270. mem_info->permisson, name_buf);
  271. write_packet(server, tmpbuf);
  272. os_mutex_unlock(&tmpbuf_lock);
  273. wasm_debug_instance_destroy_memregion(
  274. (WASMDebugInstance *)server->thread->debug_instance, mem_info);
  275. }
  276. }
  277. if (!strcmp(name, "WasmData")) {
  278. }
  279. if (!strcmp(name, "WasmMem")) {
  280. }
  281. if (!strcmp(name, "Symbol")) {
  282. write_packet(server, "");
  283. }
  284. if (args && (!strcmp(name, "WasmCallStack"))) {
  285. uint64 tid = strtoll(args, NULL, 16);
  286. uint64 buf[1024 / sizeof(uint64)];
  287. uint32 count = wasm_debug_instance_get_call_stack_pcs(
  288. (WASMDebugInstance *)server->thread->debug_instance,
  289. (korp_tid)(uintptr_t)tid, buf, 1024 / sizeof(uint64));
  290. if (count > 0) {
  291. os_mutex_lock(&tmpbuf_lock);
  292. mem2hex((char *)buf, tmpbuf, count * sizeof(uint64));
  293. write_packet(server, tmpbuf);
  294. os_mutex_unlock(&tmpbuf_lock);
  295. }
  296. else
  297. write_packet(server, "");
  298. }
  299. if (args && (!strcmp(name, "WasmLocal"))) {
  300. process_wasm_local(server, args);
  301. }
  302. if (args && (!strcmp(name, "WasmGlobal"))) {
  303. process_wasm_global(server, args);
  304. }
  305. if (!strcmp(name, "Offsets")) {
  306. write_packet(server, "");
  307. }
  308. if (!strncmp(name, "ThreadStopInfo", strlen("ThreadStopInfo"))) {
  309. int32 prefix_len = strlen("ThreadStopInfo");
  310. uint64 tid_number = strtoll(name + prefix_len, NULL, 16);
  311. korp_tid tid = (korp_tid)(uintptr_t)tid_number;
  312. uint32 status;
  313. status = wasm_debug_instance_get_thread_status(
  314. server->thread->debug_instance, tid);
  315. send_thread_stop_status(server, status, tid);
  316. }
  317. if (!strcmp(name, "WatchpointSupportInfo")) {
  318. os_mutex_lock(&tmpbuf_lock);
  319. // Any uint32 is OK for the watchpoint support
  320. snprintf(tmpbuf, MAX_PACKET_SIZE, "num:32;");
  321. write_packet(server, tmpbuf);
  322. os_mutex_unlock(&tmpbuf_lock);
  323. }
  324. }
  325. void
  326. send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid)
  327. {
  328. int32 len = 0;
  329. uint64 pc;
  330. korp_tid tids[20];
  331. char pc_string[17];
  332. uint32 tids_count, i = 0;
  333. uint32 gdb_status = status;
  334. WASMExecEnv *exec_env;
  335. const char *exception;
  336. if (status == 0) {
  337. os_mutex_lock(&tmpbuf_lock);
  338. snprintf(tmpbuf, MAX_PACKET_SIZE, "W%02x", status);
  339. write_packet(server, tmpbuf);
  340. os_mutex_unlock(&tmpbuf_lock);
  341. return;
  342. }
  343. tids_count = wasm_debug_instance_get_tids(
  344. (WASMDebugInstance *)server->thread->debug_instance, tids, 20);
  345. pc = wasm_debug_instance_get_pc(
  346. (WASMDebugInstance *)server->thread->debug_instance);
  347. if (status == WAMR_SIG_SINGSTEP) {
  348. gdb_status = WAMR_SIG_TRAP;
  349. }
  350. os_mutex_lock(&tmpbuf_lock);
  351. // TODO: how name a wasm thread?
  352. len += snprintf(tmpbuf, MAX_PACKET_SIZE, "T%02xthread:%" PRIx64 ";name:%s;",
  353. gdb_status, (uint64)(uintptr_t)tid, "nobody");
  354. if (tids_count > 0) {
  355. len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "threads:");
  356. while (i < tids_count) {
  357. if (i == tids_count - 1)
  358. len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
  359. "%" PRIx64 ";", (uint64)(uintptr_t)tids[i]);
  360. else
  361. len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
  362. "%" PRIx64 ",", (uint64)(uintptr_t)tids[i]);
  363. i++;
  364. }
  365. }
  366. mem2hex((void *)&pc, pc_string, 8);
  367. pc_string[8 * 2] = '\0';
  368. exec_env = wasm_debug_instance_get_current_env(
  369. (WASMDebugInstance *)server->thread->debug_instance);
  370. bh_assert(exec_env);
  371. exception =
  372. wasm_runtime_get_exception(wasm_runtime_get_module_inst(exec_env));
  373. if (exception) {
  374. /* When exception occurs, use reason:exception so the description can be
  375. * correctly processed by LLDB */
  376. uint32 exception_len = strlen(exception);
  377. len +=
  378. snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
  379. "thread-pcs:%" PRIx64 ";00:%s;reason:%s;description:", pc,
  380. pc_string, "exception");
  381. /* The description should be encoded as HEX */
  382. for (i = 0; i < exception_len; i++) {
  383. len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, "%02x",
  384. exception[i]);
  385. }
  386. len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len, ";");
  387. }
  388. else {
  389. if (status == WAMR_SIG_TRAP) {
  390. len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
  391. "thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
  392. pc_string, "breakpoint");
  393. }
  394. else if (status == WAMR_SIG_SINGSTEP) {
  395. len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
  396. "thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
  397. pc_string, "trace");
  398. }
  399. else if (status > 0) {
  400. len += snprintf(tmpbuf + len, MAX_PACKET_SIZE - len,
  401. "thread-pcs:%" PRIx64 ";00:%s;reason:%s;", pc,
  402. pc_string, "signal");
  403. }
  404. }
  405. write_packet(server, tmpbuf);
  406. os_mutex_unlock(&tmpbuf_lock);
  407. }
  408. void
  409. handle_v_packet(WASMGDBServer *server, char *payload)
  410. {
  411. const char *name;
  412. char *args;
  413. args = strchr(payload, ';');
  414. if (args)
  415. *args++ = '\0';
  416. name = payload;
  417. LOG_VERBOSE("%s:%s\n", __FUNCTION__, payload);
  418. if (!strcmp("Cont?", name))
  419. write_packet(server, "vCont;c;C;s;S;");
  420. if (!strcmp("Cont", name)) {
  421. if (args) {
  422. if (args[0] == 's' || args[0] == 'c') {
  423. char *numstring = strchr(args, ':');
  424. if (numstring) {
  425. uint64 tid_number;
  426. korp_tid tid;
  427. *numstring++ = '\0';
  428. tid_number = strtoll(numstring, NULL, 16);
  429. tid = (korp_tid)(uintptr_t)tid_number;
  430. wasm_debug_instance_set_cur_thread(
  431. (WASMDebugInstance *)server->thread->debug_instance,
  432. tid);
  433. if (args[0] == 's') {
  434. wasm_debug_instance_singlestep(
  435. (WASMDebugInstance *)server->thread->debug_instance,
  436. tid);
  437. }
  438. else {
  439. wasm_debug_instance_continue(
  440. (WASMDebugInstance *)
  441. server->thread->debug_instance);
  442. }
  443. }
  444. }
  445. }
  446. }
  447. }
  448. void
  449. handle_threadstop_request(WASMGDBServer *server, char *payload)
  450. {
  451. korp_tid tid;
  452. uint32 status;
  453. WASMDebugInstance *debug_inst =
  454. (WASMDebugInstance *)server->thread->debug_instance;
  455. bh_assert(debug_inst);
  456. /* According to
  457. https://sourceware.org/gdb/onlinedocs/gdb/Packets.html#Packets, the "?"
  458. package should be sent when connection is first established to query the
  459. reason the target halted */
  460. bh_assert(debug_inst->current_state == DBG_LAUNCHING);
  461. /* Waiting for the stop event */
  462. os_mutex_lock(&debug_inst->wait_lock);
  463. while (!debug_inst->stopped_thread) {
  464. os_cond_wait(&debug_inst->wait_cond, &debug_inst->wait_lock);
  465. }
  466. os_mutex_unlock(&debug_inst->wait_lock);
  467. tid = debug_inst->stopped_thread->handle;
  468. status = (uint32)debug_inst->stopped_thread->current_status->signal_flag;
  469. wasm_debug_instance_set_cur_thread(debug_inst, tid);
  470. send_thread_stop_status(server, status, tid);
  471. debug_inst->current_state = APP_STOPPED;
  472. debug_inst->stopped_thread = NULL;
  473. }
  474. void
  475. handle_set_current_thread(WASMGDBServer *server, char *payload)
  476. {
  477. LOG_VERBOSE("%s:%s\n", __FUNCTION__, payload);
  478. if ('g' == *payload++) {
  479. uint64 tid = strtoll(payload, NULL, 16);
  480. if (tid > 0)
  481. wasm_debug_instance_set_cur_thread(
  482. (WASMDebugInstance *)server->thread->debug_instance,
  483. (korp_tid)(uintptr_t)tid);
  484. }
  485. write_packet(server, "OK");
  486. }
  487. void
  488. handle_get_register(WASMGDBServer *server, char *payload)
  489. {
  490. uint64 regdata;
  491. int32 i = strtol(payload, NULL, 16);
  492. if (i != 0) {
  493. write_packet(server, "E01");
  494. return;
  495. }
  496. regdata = wasm_debug_instance_get_pc(
  497. (WASMDebugInstance *)server->thread->debug_instance);
  498. os_mutex_lock(&tmpbuf_lock);
  499. mem2hex((void *)&regdata, tmpbuf, 8);
  500. tmpbuf[8 * 2] = '\0';
  501. write_packet(server, tmpbuf);
  502. os_mutex_unlock(&tmpbuf_lock);
  503. }
  504. void
  505. handle_get_json_request(WASMGDBServer *server, char *payload)
  506. {
  507. char *args;
  508. args = strchr(payload, ':');
  509. if (args)
  510. *args++ = '\0';
  511. write_packet(server, "");
  512. }
  513. void
  514. handle_get_read_binary_memory(WASMGDBServer *server, char *payload)
  515. {
  516. write_packet(server, "");
  517. }
  518. void
  519. handle_get_read_memory(WASMGDBServer *server, char *payload)
  520. {
  521. uint64 maddr, mlen;
  522. bool ret;
  523. os_mutex_lock(&tmpbuf_lock);
  524. snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "");
  525. if (sscanf(payload, "%" SCNx64 ",%" SCNx64, &maddr, &mlen) == 2) {
  526. char *buff;
  527. if (mlen * 2 > MAX_PACKET_SIZE) {
  528. LOG_ERROR("Buffer overflow!");
  529. mlen = MAX_PACKET_SIZE / 2;
  530. }
  531. buff = wasm_runtime_malloc(mlen);
  532. if (buff) {
  533. ret = wasm_debug_instance_get_mem(
  534. (WASMDebugInstance *)server->thread->debug_instance, maddr,
  535. buff, &mlen);
  536. if (ret) {
  537. mem2hex(buff, tmpbuf, mlen);
  538. }
  539. wasm_runtime_free(buff);
  540. }
  541. }
  542. write_packet(server, tmpbuf);
  543. os_mutex_unlock(&tmpbuf_lock);
  544. }
  545. void
  546. handle_get_write_memory(WASMGDBServer *server, char *payload)
  547. {
  548. size_t hex_len;
  549. int32 offset, act_len;
  550. uint64 maddr, mlen;
  551. char *buff;
  552. bool ret;
  553. os_mutex_lock(&tmpbuf_lock);
  554. snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "");
  555. if (sscanf(payload, "%" SCNx64 ",%" SCNx64 ":%n", &maddr, &mlen, &offset)
  556. == 2) {
  557. payload += offset;
  558. hex_len = strlen(payload);
  559. act_len = hex_len / 2 < mlen ? hex_len / 2 : mlen;
  560. buff = wasm_runtime_malloc(act_len);
  561. if (buff) {
  562. hex2mem(payload, buff, act_len);
  563. ret = wasm_debug_instance_set_mem(
  564. (WASMDebugInstance *)server->thread->debug_instance, maddr,
  565. buff, &mlen);
  566. if (ret) {
  567. snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "OK");
  568. }
  569. wasm_runtime_free(buff);
  570. }
  571. }
  572. write_packet(server, tmpbuf);
  573. os_mutex_unlock(&tmpbuf_lock);
  574. }
  575. void
  576. handle_breakpoint_software_add(WASMGDBServer *server, uint64 addr,
  577. size_t length)
  578. {
  579. bool ret = wasm_debug_instance_add_breakpoint(
  580. (WASMDebugInstance *)server->thread->debug_instance, addr, length);
  581. write_packet(server, ret ? "OK" : "EO1");
  582. }
  583. void
  584. handle_breakpoint_software_remove(WASMGDBServer *server, uint64 addr,
  585. size_t length)
  586. {
  587. bool ret = wasm_debug_instance_remove_breakpoint(
  588. (WASMDebugInstance *)server->thread->debug_instance, addr, length);
  589. write_packet(server, ret ? "OK" : "EO1");
  590. }
  591. void
  592. handle_watchpoint_write_add(WASMGDBServer *server, uint64 addr, size_t length)
  593. {
  594. bool ret = wasm_debug_instance_watchpoint_write_add(
  595. (WASMDebugInstance *)server->thread->debug_instance, addr, length);
  596. write_packet(server, ret ? "OK" : "EO1");
  597. }
  598. void
  599. handle_watchpoint_write_remove(WASMGDBServer *server, uint64 addr,
  600. size_t length)
  601. {
  602. bool ret = wasm_debug_instance_watchpoint_write_remove(
  603. (WASMDebugInstance *)server->thread->debug_instance, addr, length);
  604. write_packet(server, ret ? "OK" : "EO1");
  605. }
  606. void
  607. handle_watchpoint_read_add(WASMGDBServer *server, uint64 addr, size_t length)
  608. {
  609. bool ret = wasm_debug_instance_watchpoint_read_add(
  610. (WASMDebugInstance *)server->thread->debug_instance, addr, length);
  611. write_packet(server, ret ? "OK" : "EO1");
  612. }
  613. void
  614. handle_watchpoint_read_remove(WASMGDBServer *server, uint64 addr, size_t length)
  615. {
  616. bool ret = wasm_debug_instance_watchpoint_read_remove(
  617. (WASMDebugInstance *)server->thread->debug_instance, addr, length);
  618. write_packet(server, ret ? "OK" : "EO1");
  619. }
  620. void
  621. handle_add_break(WASMGDBServer *server, char *payload)
  622. {
  623. int arg_c;
  624. size_t type, length;
  625. uint64 addr;
  626. if ((arg_c = sscanf(payload, "%zx,%" SCNx64 ",%zx", &type, &addr, &length))
  627. != 3) {
  628. LOG_ERROR("Unsupported number of add break arguments %d", arg_c);
  629. write_packet(server, "");
  630. return;
  631. }
  632. switch (type) {
  633. case eBreakpointSoftware:
  634. handle_breakpoint_software_add(server, addr, length);
  635. break;
  636. case eWatchpointWrite:
  637. handle_watchpoint_write_add(server, addr, length);
  638. break;
  639. case eWatchpointRead:
  640. handle_watchpoint_read_add(server, addr, length);
  641. break;
  642. case eWatchpointReadWrite:
  643. handle_watchpoint_write_add(server, addr, length);
  644. handle_watchpoint_read_add(server, addr, length);
  645. break;
  646. default:
  647. LOG_ERROR("Unsupported breakpoint type %zu", type);
  648. write_packet(server, "");
  649. break;
  650. }
  651. }
  652. void
  653. handle_remove_break(WASMGDBServer *server, char *payload)
  654. {
  655. int arg_c;
  656. size_t type, length;
  657. uint64 addr;
  658. if ((arg_c = sscanf(payload, "%zx,%" SCNx64 ",%zx", &type, &addr, &length))
  659. != 3) {
  660. LOG_ERROR("Unsupported number of remove break arguments %d", arg_c);
  661. write_packet(server, "");
  662. return;
  663. }
  664. switch (type) {
  665. case eBreakpointSoftware:
  666. handle_breakpoint_software_remove(server, addr, length);
  667. break;
  668. case eWatchpointWrite:
  669. handle_watchpoint_write_remove(server, addr, length);
  670. break;
  671. case eWatchpointRead:
  672. handle_watchpoint_read_remove(server, addr, length);
  673. break;
  674. case eWatchpointReadWrite:
  675. handle_watchpoint_write_remove(server, addr, length);
  676. handle_watchpoint_read_remove(server, addr, length);
  677. break;
  678. default:
  679. LOG_ERROR("Unsupported breakpoint type %zu", type);
  680. write_packet(server, "");
  681. break;
  682. }
  683. }
  684. void
  685. handle_continue_request(WASMGDBServer *server, char *payload)
  686. {
  687. wasm_debug_instance_continue(
  688. (WASMDebugInstance *)server->thread->debug_instance);
  689. }
  690. void
  691. handle_kill_request(WASMGDBServer *server, char *payload)
  692. {
  693. wasm_debug_instance_kill(
  694. (WASMDebugInstance *)server->thread->debug_instance);
  695. }
  696. static void
  697. handle_malloc(WASMGDBServer *server, char *payload)
  698. {
  699. char *args;
  700. uint64 addr, size;
  701. int32 map_prot = MMAP_PROT_NONE;
  702. args = strstr(payload, ",");
  703. if (args) {
  704. *args++ = '\0';
  705. }
  706. else {
  707. LOG_ERROR("Payload parse error during handle malloc");
  708. return;
  709. }
  710. os_mutex_lock(&tmpbuf_lock);
  711. snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "E03");
  712. size = strtoll(payload, NULL, 16);
  713. if (size > 0) {
  714. while (*args) {
  715. if (*args == 'r') {
  716. map_prot |= MMAP_PROT_READ;
  717. }
  718. if (*args == 'w') {
  719. map_prot |= MMAP_PROT_WRITE;
  720. }
  721. if (*args == 'x') {
  722. map_prot |= MMAP_PROT_EXEC;
  723. }
  724. args++;
  725. }
  726. addr = wasm_debug_instance_mmap(
  727. (WASMDebugInstance *)server->thread->debug_instance, size,
  728. map_prot);
  729. if (addr) {
  730. snprintf(tmpbuf, MAX_PACKET_SIZE, "%" PRIx64, addr);
  731. }
  732. }
  733. write_packet(server, tmpbuf);
  734. os_mutex_unlock(&tmpbuf_lock);
  735. }
  736. static void
  737. handle_free(WASMGDBServer *server, char *payload)
  738. {
  739. uint64 addr;
  740. bool ret;
  741. os_mutex_lock(&tmpbuf_lock);
  742. snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "E03");
  743. addr = strtoll(payload, NULL, 16);
  744. ret = wasm_debug_instance_ummap(
  745. (WASMDebugInstance *)server->thread->debug_instance, addr);
  746. if (ret) {
  747. snprintf(tmpbuf, MAX_PACKET_SIZE, "%s", "OK");
  748. }
  749. write_packet(server, tmpbuf);
  750. os_mutex_unlock(&tmpbuf_lock);
  751. }
  752. void
  753. handle____request(WASMGDBServer *server, char *payload)
  754. {
  755. char *args;
  756. if (payload[0] == 'M') {
  757. args = payload + 1;
  758. handle_malloc(server, args);
  759. }
  760. if (payload[0] == 'm') {
  761. args = payload + 1;
  762. handle_free(server, args);
  763. }
  764. }
  765. void
  766. handle_detach_request(WASMGDBServer *server, char *payload)
  767. {
  768. if (payload != NULL) {
  769. write_packet(server, "OK");
  770. }
  771. wasm_debug_instance_detach(
  772. (WASMDebugInstance *)server->thread->debug_instance);
  773. }