service.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { DebugSession } from './session';
  2. import { IDebugger } from './tokens';
  3. import { DebugProtocol } from 'vscode-debugprotocol';
  4. import { Debugger } from './debugger';
  5. export class DebugService {
  6. constructor(session: DebugSession | null, debuggerModel: Debugger.Model) {
  7. this.session = session;
  8. this.model = debuggerModel;
  9. }
  10. private _session: DebugSession;
  11. // private _currentFrame: DebugProtocol.StackFrame;
  12. private _debuggerModel: Debugger.Model;
  13. set session(session: DebugSession) {
  14. this._session = session;
  15. }
  16. get session() {
  17. return this._session;
  18. }
  19. get model() {
  20. return this._debuggerModel;
  21. }
  22. set model(model: Debugger.Model) {
  23. this._debuggerModel = model;
  24. }
  25. // this will change for after execute cell
  26. async launch(code: string): Promise<void> {
  27. let threadId: number = 1;
  28. this.session.eventMessage.connect(
  29. (sender: DebugSession, event: IDebugger.ISession.Event) => {
  30. const eventName = event.event;
  31. if (eventName === 'thread') {
  32. const msg = event as DebugProtocol.ThreadEvent;
  33. threadId = msg.body.threadId;
  34. }
  35. }
  36. );
  37. const breakpoints: DebugProtocol.SourceBreakpoint[] = this.setBreakpoints();
  38. const reply = await this.session
  39. .sendRequest('dumpCell', {
  40. code
  41. })
  42. .catch(error => error);
  43. await this.session.sendRequest('setBreakpoints', {
  44. breakpoints: breakpoints,
  45. source: { path: reply.body.sourcePath },
  46. sourceModified: false
  47. });
  48. await this.session.sendRequest('configurationDone', {});
  49. this.session.client.kernel.requestExecute({ code });
  50. const stackFrameReply = await this.getFrames(threadId);
  51. const scopeReply = await this.getScopes(stackFrameReply);
  52. const variablesReply = await this.getVariables(scopeReply);
  53. console.log({ variablesReply, scopeReply, stackFrameReply });
  54. }
  55. getFrames = async (threadId: number) => {
  56. const reply = await this.session.sendRequest('stackTrace', {
  57. threadId
  58. });
  59. const stackFrames = reply.body.stackFrames;
  60. return stackFrames;
  61. };
  62. getScopes = async (frame: DebugProtocol.StackFrame[]) => {
  63. const reply = await this.session.sendRequest('scopes', {
  64. frameId: frame[0].id
  65. });
  66. return reply.body.scopes;
  67. };
  68. getVariables = async (scopes: DebugProtocol.Scope[]) => {
  69. const reply = await this.session.sendRequest('variables', {
  70. variablesReference: scopes[0].variablesReference
  71. });
  72. return reply.body.variables;
  73. };
  74. setBreakpoints = (): DebugProtocol.SourceBreakpoint[] => {
  75. return this.model.sidebar.breakpoints.model.breakpoints.map(breakpoint => {
  76. return {
  77. line: breakpoint.line
  78. };
  79. });
  80. };
  81. }