ecalDebugClient.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. console.log("#### Thread was stopped!!");
  105. // A thread was stopped inspect it
  106. let inspection: ThreadInspection = {
  107. callStack: [],
  108. threadRunning: false,
  109. };
  110. try {
  111. inspection = (await this.sendCommand("describe", [
  112. String(tid),
  113. ])) as ThreadInspection;
  114. } catch (e) {
  115. this.out.error(`Could not get description for ${tid}: ${e}`);
  116. }
  117. this.threadInspection[tid] = inspection;
  118. this.emit("pauseOnBreakpoint", { tid, inspection });
  119. }
  120. }
  121. if (this.doReload) {
  122. this.doReload = false;
  123. this.out.log(`Reloading interpreter state`);
  124. try {
  125. await this.sendCommandString("@reload\r\n");
  126. } catch (e) {
  127. this.out.error(`Could not reload the interpreter state: ${e}`);
  128. }
  129. }
  130. } catch (e) {
  131. this.out.error(`Error during event loop: ${e}`);
  132. nextLoop = 5000;
  133. }
  134. if (this.connected) {
  135. setTimeout(this.pollEvents.bind(this), nextLoop);
  136. } else {
  137. this.out.log("Stop emitting events" + nextLoop);
  138. }
  139. }
  140. public async sendCommand(cmd: string, args?: string[]): Promise<any> {
  141. // Create or process the backlog depending on the connection status
  142. if (!this.connected) {
  143. this.backlog.push({
  144. cmd,
  145. args,
  146. });
  147. return null;
  148. } else if (this.backlog.length > 0) {
  149. const backlog = this.backlog;
  150. this.backlog = [];
  151. for (const item of backlog) {
  152. await this.sendCommand(item.cmd, item.args);
  153. }
  154. }
  155. return await this.sendCommandString(
  156. `##${cmd} ${args ? args.join(" ") : ""}\r\n`
  157. );
  158. }
  159. public async sendCommandString(cmdString: string): Promise<any> {
  160. // Socket needs to be locked. Reading and writing to the socket is seen
  161. // by the interpreter as async (i/o bound) code. Separate calls to
  162. // sendCommand will be executed in different event loops. Without the lock
  163. // the different sendCommand calls would mix their responses.
  164. return await this.socketLock.acquire("socket", async () => {
  165. await this.socket.write(cmdString, "utf8");
  166. let text = "";
  167. while (!text.endsWith("\n\n")) {
  168. text += await this.socket.read(1);
  169. }
  170. let res: any = {};
  171. try {
  172. res = JSON.parse(text);
  173. } catch (e) {
  174. throw new Error(`Could not parse response: ${text} - error:${e}`);
  175. }
  176. if (res?.DebuggerError) {
  177. throw new Error(
  178. `Unexpected internal error for command "${cmdString}": ${res.DebuggerError}`
  179. );
  180. }
  181. if (res?.EncodedOutput !== undefined) {
  182. res = Buffer.from(res.EncodedOutput, "base64").toString("utf8");
  183. }
  184. return res;
  185. });
  186. }
  187. }