service.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import { ISignal, Signal } from '@phosphor/signaling';
  2. import { DebugProtocol } from 'vscode-debugprotocol';
  3. import { Debugger } from './debugger';
  4. import { IDebugger } from './tokens';
  5. import { Variables } from './variables';
  6. import { Callstack } from './callstack';
  7. export class DebugService implements IDebugger.IService {
  8. constructor(debuggerModel: Debugger.Model) {
  9. // Avoids setting session with invalid client
  10. // session should be set only when a notebook or
  11. // a console get the focus.
  12. // TODO: also checks that the notebook or console
  13. // runs a kernel with debugging ability
  14. this._session = null;
  15. this._model = debuggerModel;
  16. }
  17. set session(session: IDebugger.ISession) {
  18. if (this._session === session) {
  19. return;
  20. }
  21. if (this._session) {
  22. this._session.dispose();
  23. }
  24. this._session = session;
  25. this._session.eventMessage.connect((_, event) => {
  26. if (event.event === 'stopped') {
  27. this._threadStopped.add(event.body.threadId);
  28. } else if (event.event === 'continued') {
  29. this._threadStopped.delete(event.body.threadId);
  30. }
  31. this._eventMessage.emit(event);
  32. });
  33. this._sessionChanged.emit(session);
  34. }
  35. get session() {
  36. return this._session;
  37. }
  38. canStart(): boolean {
  39. return this._session !== null && !this._session.isStarted;
  40. }
  41. isStarted(): boolean {
  42. return this._session !== null && this._session.isStarted;
  43. }
  44. isThreadStopped(): boolean {
  45. return this._threadStopped.has(this.currentThread());
  46. }
  47. async continue(): Promise<void> {
  48. try {
  49. await this.session.sendRequest('continue', {
  50. threadId: this.currentThread()
  51. });
  52. this._threadStopped.delete(this.currentThread());
  53. } catch (err) {
  54. console.error('Error:', err.message);
  55. }
  56. }
  57. async next(): Promise<void> {
  58. try {
  59. await this.session.sendRequest('next', {
  60. threadId: this.currentThread()
  61. });
  62. } catch (err) {
  63. console.error('Error:', err.message);
  64. }
  65. }
  66. async stepIn(): Promise<void> {
  67. try {
  68. await this.session.sendRequest('stepIn', {
  69. threadId: this.currentThread()
  70. });
  71. } catch (err) {
  72. console.error('Error:', err.message);
  73. }
  74. }
  75. get sessionChanged(): ISignal<IDebugger.IService, IDebugger.ISession> {
  76. return this._sessionChanged;
  77. }
  78. get eventMessage(): ISignal<IDebugger.IService, IDebugger.ISession.Event> {
  79. return this._eventMessage;
  80. }
  81. // this will change for after execute cell
  82. async launch(code: string): Promise<void> {
  83. let threadId: number = 1;
  84. this.frames = [];
  85. this.session.eventMessage.connect((_, event: IDebugger.ISession.Event) => {
  86. const eventName = event.event;
  87. if (eventName === 'thread') {
  88. const msg = event as DebugProtocol.ThreadEvent;
  89. threadId = msg.body.threadId;
  90. }
  91. });
  92. const breakpoints: DebugProtocol.SourceBreakpoint[] = this.setBreakpoints();
  93. const reply = await this.session.sendRequest('dumpCell', {
  94. code
  95. });
  96. await this.session.sendRequest('setBreakpoints', {
  97. breakpoints: breakpoints,
  98. source: { path: reply.body.sourcePath },
  99. sourceModified: false
  100. });
  101. await this.session.sendRequest('configurationDone', {});
  102. this.session.client.kernel.requestExecute({ code });
  103. const stackFrames = await this.getFrames(threadId);
  104. stackFrames.forEach(async (frame, index) => {
  105. const scopes = await this.getScopes(frame);
  106. const variables = await this.getVariables(scopes);
  107. const values = this.convertScope(scopes, variables);
  108. this.frames.push({
  109. id: frame.id,
  110. scopes: values
  111. });
  112. if (index === 0) {
  113. this._model.sidebar.variables.model.scopes = values;
  114. this._model.currentLineChanged.emit(frame.line);
  115. }
  116. });
  117. if (stackFrames) {
  118. this._model.sidebar.callstack.model.frames = stackFrames;
  119. }
  120. this._model.sidebar.callstack.model.currentFrameChanged.connect(
  121. this.onChangeFrame
  122. );
  123. }
  124. onChangeFrame = (_: Callstack.IModel, update: Callstack.IFrame) => {
  125. const frame = this.frames.find(ele => ele.id === update.id);
  126. if (frame && frame.scopes) {
  127. this._model.sidebar.variables.model.scopes = frame.scopes;
  128. }
  129. };
  130. getFrames = async (threadId: number) => {
  131. const reply = await this.session.sendRequest('stackTrace', {
  132. threadId
  133. });
  134. const stackFrames = reply.body.stackFrames;
  135. return stackFrames;
  136. };
  137. getScopes = async (frame: DebugProtocol.StackFrame) => {
  138. if (!frame) {
  139. return;
  140. }
  141. const reply = await this.session.sendRequest('scopes', {
  142. frameId: frame.id
  143. });
  144. return reply.body.scopes;
  145. };
  146. getVariables = async (scopes: DebugProtocol.Scope[]) => {
  147. if (!scopes || scopes.length === 0) {
  148. return;
  149. }
  150. const reply = await this.session.sendRequest('variables', {
  151. variablesReference: scopes[0].variablesReference
  152. });
  153. return reply.body.variables;
  154. };
  155. setBreakpoints = (): DebugProtocol.SourceBreakpoint[] => {
  156. return this._model.sidebar.breakpoints.model.breakpoints.map(breakpoint => {
  157. return {
  158. line: breakpoint.line
  159. };
  160. });
  161. };
  162. protected convertScope = (
  163. scopes: DebugProtocol.Scope[],
  164. variables: DebugProtocol.Variable[]
  165. ): Variables.IScope[] => {
  166. if (!variables || !scopes) {
  167. return;
  168. }
  169. return scopes.map(scope => {
  170. return {
  171. name: scope.name,
  172. variables: variables.map(variable => {
  173. return { ...variable };
  174. })
  175. };
  176. });
  177. };
  178. private currentThread(): number {
  179. // TODO: ask the model for the current thread ID
  180. return 1;
  181. }
  182. private _session: IDebugger.ISession;
  183. private _sessionChanged = new Signal<IDebugger.IService, IDebugger.ISession>(
  184. this
  185. );
  186. private _eventMessage = new Signal<
  187. IDebugger.IService,
  188. IDebugger.ISession.Event
  189. >(this);
  190. private _model: Debugger.Model;
  191. private frames: Frame[];
  192. // TODO: move this in model
  193. private _threadStopped = new Set();
  194. }
  195. export type Frame = {
  196. id: number;
  197. scopes: Variables.IScope[];
  198. };