ecalDebugClient.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. * Debug client implementation for the ECAL debugger.
  3. */
  4. import * as net from "net";
  5. import { EventEmitter } from "events";
  6. import PromiseSocket from "promise-socket";
  7. import {
  8. LogOutputStream,
  9. DebugStatus,
  10. ThreadInspection,
  11. ContType,
  12. } from "./types";
  13. interface BacklogCommand {
  14. cmd: string;
  15. args?: string[];
  16. }
  17. /**
  18. * Debug client for ECAL debug server.
  19. */
  20. export class ECALDebugClient extends EventEmitter {
  21. private socket: PromiseSocket<net.Socket>;
  22. private socketLock: any;
  23. private connected: boolean = false;
  24. private backlog: BacklogCommand[] = [];
  25. private threadInspection: Record<number, ThreadInspection> = {};
  26. private doReload: boolean = false;
  27. /**
  28. * Create a new debug client.
  29. */
  30. public constructor(protected out: LogOutputStream) {
  31. super();
  32. this.socket = new PromiseSocket(new net.Socket());
  33. const AsyncLock = require("async-lock");
  34. this.socketLock = new AsyncLock();
  35. }
  36. public async connect(host: string, port: number) {
  37. try {
  38. this.out.log(`Connecting to: ${host}:${port}`);
  39. await this.socket.connect({ port, host });
  40. this.connected = true;
  41. this.pollEvents(); // Start emitting events
  42. } catch (e) {
  43. this.out.error(`Could not connect to debug server: ${e}`);
  44. }
  45. }
  46. public async status(): Promise<DebugStatus | null> {
  47. try {
  48. return (await this.sendCommand("status")) as DebugStatus;
  49. } catch (e) {
  50. this.out.error(`Could not query for status: ${e}`);
  51. return null;
  52. }
  53. }
  54. public reload() {
  55. this.doReload = true;
  56. }
  57. public async describe(tid: number): Promise<ThreadInspection | null> {
  58. try {
  59. return (await this.sendCommand("describe", [
  60. String(tid),
  61. ])) as ThreadInspection;
  62. } catch (e) {
  63. this.out.error(`Could not inspect thread ${tid}: ${e}`);
  64. return null;
  65. }
  66. }
  67. public async cont(tid: number, type: ContType) {
  68. try {
  69. await this.sendCommand("cont", [String(tid), type]);
  70. delete this.threadInspection[tid];
  71. } catch (e) {
  72. this.out.error(`Could not continue thread ${tid}: ${e}`);
  73. }
  74. }
  75. public async setBreakpoint(breakpoint: string) {
  76. try {
  77. (await this.sendCommand(`break ${breakpoint}`)) as DebugStatus;
  78. } catch (e) {
  79. this.out.error(`Could not set breakpoint ${breakpoint}: ${e}`);
  80. }
  81. }
  82. public async clearBreakpoints(source: string) {
  83. try {
  84. (await this.sendCommand("rmbreak", [source])) as DebugStatus;
  85. } catch (e) {
  86. this.out.error(`Could not remove breakpoints for ${source}: ${e}`);
  87. }
  88. }
  89. public async shutdown() {
  90. this.connected = false;
  91. await this.socket.destroy();
  92. }
  93. /**
  94. * PollEvents is the polling loop for debug events.
  95. */
  96. private async pollEvents() {
  97. let nextLoop = 1000;
  98. try {
  99. const status = await this.status();
  100. this.emit("status", status);
  101. for (const [tidString, thread] of Object.entries(status?.threads || [])) {
  102. const tid = parseInt(tidString);
  103. if (thread.threadRunning === false && !this.threadInspection[tid]) {
  104. // A thread was stopped inspect it
  105. let inspection: ThreadInspection = {
  106. callStack: [],
  107. threadRunning: false,
  108. };
  109. try {
  110. inspection = (await this.sendCommand("describe", [
  111. String(tid),
  112. ])) as ThreadInspection;
  113. } catch (e) {
  114. this.out.error(`Could not get description for ${tid}: ${e}`);
  115. }
  116. this.threadInspection[tid] = inspection;
  117. this.emit("pauseOnBreakpoint", { tid, inspection });
  118. }
  119. }
  120. if (this.doReload) {
  121. this.doReload = false;
  122. this.out.log(`Reloading interpreter state`);
  123. try {
  124. await this.sendCommandString("@reload\r\n");
  125. } catch (e) {
  126. this.out.error(`Could not reload the interpreter state: ${e}`);
  127. }
  128. }
  129. } catch (e) {
  130. this.out.error(`Error during event loop: ${e}`);
  131. nextLoop = 5000;
  132. }
  133. if (this.connected) {
  134. setTimeout(this.pollEvents.bind(this), nextLoop);
  135. } else {
  136. this.out.log("Stop emitting events" + nextLoop);
  137. }
  138. }
  139. public async sendCommand(cmd: string, args?: string[]): Promise<any> {
  140. // Create or process the backlog depending on the connection status
  141. if (!this.connected) {
  142. this.backlog.push({
  143. cmd,
  144. args,
  145. });
  146. return null;
  147. } else if (this.backlog.length > 0) {
  148. const backlog = this.backlog;
  149. this.backlog = [];
  150. for (const item of backlog) {
  151. await this.sendCommand(item.cmd, item.args);
  152. }
  153. }
  154. return await this.sendCommandString(
  155. `##${cmd} ${args ? args.join(" ") : ""}\r\n`
  156. );
  157. }
  158. public async sendCommandString(cmdString: string): Promise<any> {
  159. // Socket needs to be locked. Reading and writing to the socket is seen
  160. // by the interpreter as async (i/o bound) code. Separate calls to
  161. // sendCommand will be executed in different event loops. Without the lock
  162. // the different sendCommand calls would mix their responses.
  163. return await this.socketLock.acquire("socket", async () => {
  164. await this.socket.write(cmdString, "utf8");
  165. let text = "";
  166. while (!text.endsWith("\n\n")) {
  167. text += await this.socket.read(1);
  168. }
  169. let res: any = {};
  170. try {
  171. res = JSON.parse(text);
  172. } catch (e) {
  173. throw new Error(`Could not parse response: ${text} - error:${e}`);
  174. }
  175. if (res?.DebuggerError) {
  176. throw new Error(
  177. `Unexpected internal error for command "${cmdString}": ${res.DebuggerError}`
  178. );
  179. }
  180. if (res?.EncodedOutput !== undefined) {
  181. res = Buffer.from(res.EncodedOutput, "base64").toString("utf8");
  182. }
  183. return res;
  184. });
  185. }
  186. }