handler.c 26 KB

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