handler.c 27 KB

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