session.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { KernelMessage, Session } from '@jupyterlab/services';
  4. import { PromiseDelegate } from '@lumino/coreutils';
  5. import { ISignal, Signal } from '@lumino/signaling';
  6. import { IDebugger } from './tokens';
  7. /**
  8. * A concrete implementation of IDebugger.ISession.
  9. */
  10. export class DebuggerSession implements IDebugger.ISession {
  11. /**
  12. * Instantiate a new debug session
  13. *
  14. * @param options - The debug session instantiation options.
  15. */
  16. constructor(options: DebuggerSession.IOptions) {
  17. this.connection = options.connection;
  18. }
  19. /**
  20. * Whether the debug session is disposed.
  21. */
  22. get isDisposed(): boolean {
  23. return this._isDisposed;
  24. }
  25. /**
  26. * A signal emitted when the debug session is disposed.
  27. */
  28. get disposed(): ISignal<this, void> {
  29. return this._disposed;
  30. }
  31. /**
  32. * Returns the API session connection to connect to a debugger.
  33. */
  34. get connection(): Session.ISessionConnection | null {
  35. return this._connection;
  36. }
  37. /**
  38. * Sets the API session connection to connect to a debugger to
  39. * the given parameter.
  40. *
  41. * @param connection - The new API session connection.
  42. */
  43. set connection(connection: Session.ISessionConnection | null) {
  44. if (this._connection) {
  45. this._connection.iopubMessage.disconnect(this._handleEvent, this);
  46. }
  47. this._connection = connection;
  48. if (!this._connection) {
  49. this._isStarted = false;
  50. return;
  51. }
  52. this._connection.iopubMessage.connect(this._handleEvent, this);
  53. this._ready = new PromiseDelegate<void>();
  54. const future = this.connection?.kernel?.requestDebug({
  55. type: 'request',
  56. seq: 0,
  57. command: 'debugInfo'
  58. });
  59. if (future) {
  60. future.onReply = (msg: KernelMessage.IDebugReplyMsg): void => {
  61. this._ready.resolve();
  62. future.dispose();
  63. };
  64. }
  65. }
  66. /**
  67. * Whether the debug session is started
  68. */
  69. get isStarted(): boolean {
  70. return this._isStarted;
  71. }
  72. /**
  73. * Signal emitted for debug event messages.
  74. */
  75. get eventMessage(): ISignal<IDebugger.ISession, IDebugger.ISession.Event> {
  76. return this._eventMessage;
  77. }
  78. /**
  79. * Dispose the debug session.
  80. */
  81. dispose(): void {
  82. if (this._isDisposed) {
  83. return;
  84. }
  85. this._isDisposed = true;
  86. this._disposed.emit();
  87. Signal.clearData(this);
  88. }
  89. /**
  90. * Start a new debug session
  91. */
  92. async start(): Promise<void> {
  93. await this.sendRequest('initialize', {
  94. clientID: 'jupyterlab',
  95. clientName: 'JupyterLab',
  96. adapterID: this.connection?.kernel?.name ?? '',
  97. pathFormat: 'path',
  98. linesStartAt1: true,
  99. columnsStartAt1: true,
  100. supportsVariableType: true,
  101. supportsVariablePaging: true,
  102. supportsRunInTerminalRequest: true,
  103. locale: 'en-us'
  104. });
  105. this._isStarted = true;
  106. await this.sendRequest('attach', {});
  107. }
  108. /**
  109. * Stop the running debug session.
  110. */
  111. async stop(): Promise<void> {
  112. await this.sendRequest('disconnect', {
  113. restart: false,
  114. terminateDebuggee: true
  115. });
  116. this._isStarted = false;
  117. }
  118. /**
  119. * Restore the state of a debug session.
  120. */
  121. async restoreState(): Promise<IDebugger.ISession.Response['debugInfo']> {
  122. const message = await this.sendRequest('debugInfo', {});
  123. this._isStarted = message.body.isStarted;
  124. return message;
  125. }
  126. /**
  127. * Send a custom debug request to the kernel.
  128. *
  129. * @param command debug command.
  130. * @param args arguments for the debug command.
  131. */
  132. async sendRequest<K extends keyof IDebugger.ISession.Request>(
  133. command: K,
  134. args: IDebugger.ISession.Request[K]
  135. ): Promise<IDebugger.ISession.Response[K]> {
  136. await this._ready.promise;
  137. const message = await this._sendDebugMessage({
  138. type: 'request',
  139. seq: this._seq++,
  140. command,
  141. arguments: args
  142. });
  143. return message.content as IDebugger.ISession.Response[K];
  144. }
  145. /**
  146. * Handle debug events sent on the 'iopub' channel.
  147. *
  148. * @param sender - the emitter of the event.
  149. * @param message - the event message.
  150. */
  151. private _handleEvent(
  152. sender: Session.ISessionConnection,
  153. message: KernelMessage.IIOPubMessage
  154. ): void {
  155. const msgType = message.header.msg_type;
  156. if (msgType !== 'debug_event') {
  157. return;
  158. }
  159. const event = message.content as IDebugger.ISession.Event;
  160. this._eventMessage.emit(event);
  161. }
  162. /**
  163. * Send a debug request message to the kernel.
  164. *
  165. * @param msg debug request message to send to the kernel.
  166. */
  167. private async _sendDebugMessage(
  168. msg: KernelMessage.IDebugRequestMsg['content']
  169. ): Promise<KernelMessage.IDebugReplyMsg> {
  170. const kernel = this.connection?.kernel;
  171. if (!kernel) {
  172. return Promise.reject(
  173. new Error('A kernel is required to send debug messages.')
  174. );
  175. }
  176. const reply = new PromiseDelegate<KernelMessage.IDebugReplyMsg>();
  177. const future = kernel.requestDebug(msg);
  178. future.onReply = (msg: KernelMessage.IDebugReplyMsg): void => {
  179. reply.resolve(msg);
  180. };
  181. await future.done;
  182. return reply.promise;
  183. }
  184. private _seq = 0;
  185. private _ready = new PromiseDelegate<void>();
  186. private _connection: Session.ISessionConnection | null;
  187. private _isDisposed = false;
  188. private _isStarted = false;
  189. private _disposed = new Signal<this, void>(this);
  190. private _eventMessage = new Signal<
  191. IDebugger.ISession,
  192. IDebugger.ISession.Event
  193. >(this);
  194. }
  195. /**
  196. * A namespace for `DebuggerSession` statics.
  197. */
  198. export namespace DebuggerSession {
  199. /**
  200. * Instantiation options for a `DebuggerSession`.
  201. */
  202. export interface IOptions {
  203. /**
  204. * The session connection used by the debug session.
  205. */
  206. connection: Session.ISessionConnection;
  207. }
  208. }