session.ts 5.1 KB

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