ecalDebugClient.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /**
  27. * Create a new debug client.
  28. */
  29. public constructor(protected out: LogOutputStream) {
  30. super();
  31. this.socket = new PromiseSocket(new net.Socket());
  32. const AsyncLock = require("async-lock");
  33. this.socketLock = new AsyncLock();
  34. }
  35. public async conect(host: string, port: number) {
  36. try {
  37. this.out.log(`Connecting to: ${host}:${port}`);
  38. await this.socket.connect({ port, host });
  39. // this.socket.setTimeout(2000);
  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 async describe(tid: number): Promise<ThreadInspection | null> {
  55. try {
  56. return (await this.sendCommand("describe", [
  57. String(tid),
  58. ])) as ThreadInspection;
  59. } catch (e) {
  60. this.out.error(`Could not inspect thread ${tid}: ${e}`);
  61. return null;
  62. }
  63. }
  64. public async cont(tid: number, type: ContType) {
  65. try {
  66. await this.sendCommand("cont", [String(tid), type]);
  67. delete this.threadInspection[tid];
  68. } catch (e) {
  69. this.out.error(`Could not continue thread ${tid}: ${e}`);
  70. }
  71. }
  72. public async setBreakpoint(breakpoint: string) {
  73. try {
  74. (await this.sendCommand(`break ${breakpoint}`)) as DebugStatus;
  75. } catch (e) {
  76. this.out.error(`Could not set breakpoint ${breakpoint}: ${e}`);
  77. }
  78. }
  79. public async clearBreakpoints(source: string) {
  80. try {
  81. (await this.sendCommand("rmbreak", [source])) as DebugStatus;
  82. } catch (e) {
  83. this.out.error(`Could not remove breakpoints for ${source}: ${e}`);
  84. }
  85. }
  86. public async shutdown() {
  87. this.connected = false;
  88. await this.socket.destroy();
  89. }
  90. /**
  91. * PollEvents is the polling loop for debug events.
  92. */
  93. private async pollEvents() {
  94. let nextLoop = 1000;
  95. try {
  96. const status = await this.status();
  97. this.emit("status", status);
  98. for (const [tidString, thread] of Object.entries(status?.threads || [])) {
  99. const tid = parseInt(tidString);
  100. if (thread.threadRunning === false && !this.threadInspection[tid]) {
  101. console.log("#### Thread was stopped!!");
  102. // A thread was stopped inspect it
  103. let inspection: ThreadInspection = {
  104. callStack: [],
  105. threadRunning: false,
  106. };
  107. try {
  108. inspection = (await this.sendCommand("describe", [
  109. String(tid),
  110. ])) as ThreadInspection;
  111. } catch (e) {
  112. this.out.error(`Could not get description for ${tid}: ${e}`);
  113. }
  114. this.threadInspection[tid] = inspection;
  115. this.emit("pauseOnBreakpoint", { tid, inspection });
  116. }
  117. }
  118. } catch (e) {
  119. this.out.error(`Error during event loop: ${e}`);
  120. nextLoop = 5000;
  121. }
  122. if (this.connected) {
  123. setTimeout(this.pollEvents.bind(this), nextLoop);
  124. } else {
  125. this.out.log("Stop emitting events" + nextLoop);
  126. }
  127. }
  128. public async sendCommand(cmd: string, args?: string[]): Promise<any> {
  129. // Create or process the backlog depending on the connection status
  130. if (!this.connected) {
  131. this.backlog.push({
  132. cmd,
  133. args,
  134. });
  135. return null;
  136. } else if (this.backlog.length > 0) {
  137. const backlog = this.backlog;
  138. this.backlog = [];
  139. for (const item of backlog) {
  140. await this.sendCommand(item.cmd, item.args);
  141. }
  142. }
  143. return await this.sendCommandString(
  144. `##${cmd} ${args ? args.join(" ") : ""}\r\n`
  145. );
  146. }
  147. public async sendCommandString(cmdString: string): Promise<any> {
  148. // Socket needs to be locked. Reading and writing to the socket is seen
  149. // by the interpreter as async (i/o bound) code. Separate calls to
  150. // sendCommand will be executed in different event loops. Without the lock
  151. // the different sendCommand calls would mix their responses.
  152. return await this.socketLock.acquire("socket", async () => {
  153. await this.socket.write(cmdString, "utf8");
  154. let text = "";
  155. while (!text.endsWith("\n\n")) {
  156. text += await this.socket.read(1);
  157. }
  158. let res: any = {};
  159. try {
  160. res = JSON.parse(text);
  161. } catch (e) {
  162. throw new Error(`Could not parse response: ${text} - error:${e}`);
  163. }
  164. if (res?.DebuggerError) {
  165. throw new Error(
  166. `Unexpected internal error for command "${cmdString}": ${res.DebuggerError}`
  167. );
  168. }
  169. if (res?.EncodedOutput !== undefined) {
  170. res = Buffer.from(res.EncodedOutput, "base64").toString("utf8");
  171. }
  172. return res;
  173. });
  174. }
  175. }