session.ts 6.0 KB

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