@@ -46,8 +46,10 @@ export class MI2DebugSession extends DebugSession {
4646 public miDebugger : MI2 ;
4747 protected commandServer : net . Server ;
4848 protected serverPath : string ;
49+ protected sessionPid : string | undefined = undefined ;
4950 protected threadGroupPids = new Map < string , string > ( ) ;
50- protected threadToPid = new Map < number , string > ( ) ;
51+ public threadToPid = new Map < number , string > ( ) ;
52+ protected mi2Inferiors = new Array ( ) ;
5153 protected inferiorServers = new Array ( ) ;
5254
5355 public constructor ( debuggerLinesStartAt1 : boolean , isServer : boolean = false ) {
@@ -144,30 +146,72 @@ export class MI2DebugSession extends DebugSession {
144146 }
145147
146148 protected handleBreakpoint ( info : MINode ) {
149+ let threadId = info . record ( "thread-id" ) ;
150+
151+ this . miDebugger . log ( "stdout" , `Got theadid ${ threadId } ` ) ;
152+
153+ let threadPid = this . threadToPid . get ( parseInt ( info . record ( "thread-id" ) , 10 ) ) ;
154+
147155 const event = new StoppedEvent ( "breakpoint" , parseInt ( info . record ( "thread-id" ) ) ) ;
148156 ( event as DebugProtocol . StoppedEvent ) . body . allThreadsStopped = info . record ( "stopped-threads" ) === "all" ;
149- this . sendEvent ( event ) ;
157+
158+ this . miDebugger . log ( "stdout" , `Handling breakpoint for threadPid == this.sessionPid (${ threadPid } == ${ this . sessionPid } )` )
159+
160+ if ( threadPid == this . sessionPid ) {
161+ this . sendEvent ( event ) ;
162+ } else {
163+ this . mi2Inferiors . forEach ( inferior => {
164+ if ( threadPid == inferior . sessionPid ) inferior . sendEvent ( event )
165+ } ) ;
166+ }
150167 }
151168
152169 protected handleBreak ( info ?: MINode ) {
170+ let threadPid = this . threadToPid . get ( parseInt ( info . record ( "thread-id" ) , 10 ) ) ;
171+
153172 const event = new StoppedEvent ( "step" , info ? parseInt ( info . record ( "thread-id" ) ) : 1 ) ;
154173 ( event as DebugProtocol . StoppedEvent ) . body . allThreadsStopped = info ? info . record ( "stopped-threads" ) === "all" : true ;
155- this . sendEvent ( event ) ;
174+
175+ if ( threadPid == this . sessionPid ) {
176+ this . sendEvent ( event ) ;
177+ } else {
178+ this . mi2Inferiors . forEach ( inferior => {
179+ if ( threadPid == inferior . sessionPid ) inferior . sendEvent ( event )
180+ } ) ;
181+ }
156182 }
157183
158184 protected handlePause ( info : MINode ) {
185+ let threadPid = this . threadToPid . get ( parseInt ( info . record ( "thread-id" ) , 10 ) ) ;
186+
159187 const event = new StoppedEvent ( "user request" , parseInt ( info . record ( "thread-id" ) ) ) ;
160188 ( event as DebugProtocol . StoppedEvent ) . body . allThreadsStopped = info . record ( "stopped-threads" ) === "all" ;
161- this . sendEvent ( event ) ;
189+
190+ if ( threadPid == this . sessionPid ) {
191+ this . sendEvent ( event ) ;
192+ } else {
193+ this . mi2Inferiors . forEach ( inferior => {
194+ if ( threadPid == inferior . sessionPid ) inferior . sendEvent ( event )
195+ } ) ;
196+ }
162197 }
163198
164199 protected stopEvent ( info : MINode ) {
165200 if ( ! this . started )
166201 this . crashed = true ;
167202 if ( ! this . quit ) {
203+ let threadPid = this . threadToPid . get ( parseInt ( info . record ( "thread-id" ) , 10 ) ) ;
204+
168205 const event = new StoppedEvent ( "exception" , parseInt ( info . record ( "thread-id" ) ) ) ;
169206 ( event as DebugProtocol . StoppedEvent ) . body . allThreadsStopped = info . record ( "stopped-threads" ) === "all" ;
170- this . sendEvent ( event ) ;
207+
208+ if ( threadPid == this . sessionPid ) {
209+ this . sendEvent ( event ) ;
210+ } else {
211+ this . mi2Inferiors . forEach ( inferior => {
212+ if ( threadPid == inferior . sessionPid ) inferior . sendEvent ( event )
213+ } ) ;
214+ }
171215 }
172216 }
173217
@@ -177,15 +221,28 @@ export class MI2DebugSession extends DebugSession {
177221 let threadPid = this . threadGroupPids . get ( info . record ( "group-id" ) ) ;
178222 this . threadToPid . set ( threadId , threadPid ) ;
179223
180- this . sendEvent ( new ThreadEvent ( "started" , threadId ) ) ;
224+ if ( threadPid == this . sessionPid ) {
225+ this . sendEvent ( new ThreadEvent ( "started" , threadId ) ) ;
226+ } else {
227+ this . mi2Inferiors . forEach ( inferior => {
228+ if ( threadPid == inferior . sessionPid ) inferior . sendEvent ( new ThreadEvent ( "started" , threadId ) ) ;
229+ } ) ;
230+ }
181231 }
182232
183233 protected threadExitedEvent ( info : MINode ) {
184234 let threadId = parseInt ( info . record ( "id" ) , 10 ) ;
185235
236+ let threadPid = this . threadGroupPids . get ( info . record ( "group-id" ) ) ;
186237 this . threadToPid . delete ( info . record ( "group-id" ) ) ;
187238
188- this . sendEvent ( new ThreadEvent ( "exited" , threadId ) ) ;
239+ if ( threadPid == this . sessionPid ) {
240+ this . sendEvent ( new ThreadEvent ( "exited" , threadId ) ) ;
241+ } else {
242+ this . mi2Inferiors . forEach ( inferior => {
243+ if ( threadPid == inferior . sessionPid ) inferior . sendEvent ( new ThreadEvent ( "exited" , threadId ) ) ;
244+ } ) ;
245+ }
189246 }
190247
191248 private openInferiorDebugServer ( superiorServer : MI2DebugSession ) {
@@ -205,6 +262,8 @@ export class MI2DebugSession extends DebugSession {
205262 const session = new MI2InferiorSession ( superiorServer ) ;
206263 session . setRunAsServer ( true ) ;
207264 session . start ( socket , socket ) ;
265+
266+ this . mi2Inferiors . push ( session ) ;
208267 } ) . listen ( port ) ;
209268
210269 this . inferiorServers . push ( server ) ;
@@ -213,7 +272,12 @@ export class MI2DebugSession extends DebugSession {
213272 }
214273
215274 protected threadGroupStartedEvent ( info : MINode ) {
216- let pid = parseInt ( info . record ( "pid" ) , 10 ) ;
275+ let pid = info . record ( "pid" ) ;
276+
277+ if ( typeof this . sessionPid === "undefined" ) {
278+ this . miDebugger . log ( "stdout" , `Updated this.sessionPid to ${ pid } ` )
279+ this . sessionPid = pid ;
280+ }
217281
218282 this . miDebugger . log ( "stdout" , "threadGroupStartedEvent" )
219283 this . miDebugger . log ( "stdout" , pid . toString ( ) )
@@ -240,6 +304,17 @@ export class MI2DebugSession extends DebugSession {
240304 }
241305
242306 protected threadGroupExitedEvent ( info : MINode ) {
307+ let pid = this . threadGroupPids . get ( info . record ( "id" ) ) ;
308+
309+ if ( pid == this . sessionPid ) {
310+ this . miDebugger . log ( "stdout" , "this.sesionPid = undefind" ) ;
311+ // Session has no thread group anymore. Next started thread group will be debugged by this session
312+ this . sessionPid = undefined ;
313+ }
314+
315+ this . miDebugger . log ( "stdout" , "threadGroupExitedEvent" ) ;
316+ this . miDebugger . log ( "stdout" , pid ) ;
317+
243318 this . threadGroupPids . delete ( info . record ( "id" ) ) ;
244319 }
245320
@@ -350,6 +425,8 @@ export class MI2DebugSession extends DebugSession {
350425 }
351426
352427 public override threadsRequest ( response : DebugProtocol . ThreadsResponse ) : void {
428+ this . miDebugger . log ( "stdout" , `Received superior thread request for ${ this . sessionPid } ` ) ;
429+
353430 if ( ! this . miDebugger ) {
354431 this . sendResponse ( response ) ;
355432 return ;
@@ -361,10 +438,11 @@ export class MI2DebugSession extends DebugSession {
361438 for ( const thread of threads ) {
362439 const threadName = thread . name || thread . targetId || "<unnamed>" ;
363440
364- if ( this . threadGroupPids . size > 1 ) {
365- let pid = this . threadToPid . get ( thread . id ) ;
366- response . body . threads . push ( new Thread ( thread . id , `(${ pid } ) ${ thread . id } :${ threadName } ` ) ) ;
367- } else {
441+ let pid = this . threadToPid . get ( thread . id ) ;
442+
443+ this . miDebugger . log ( "stdout" , `pid == this.sessionPid (${ pid } == ${ this . sessionPid } )` )
444+
445+ if ( pid == this . sessionPid ) {
368446 response . body . threads . push ( new Thread ( thread . id , `${ thread . id } :${ threadName } ` ) ) ;
369447 }
370448 }
@@ -386,7 +464,9 @@ export class MI2DebugSession extends DebugSession {
386464 return [ frameId & 0xffff , frameId >> 16 ] ;
387465 }
388466
389- public override stackTraceRequest ( response : DebugProtocol . StackTraceResponse , args : DebugProtocol . StackTraceArguments ) : void {
467+ public inferiorStackTraceRequest ( response : DebugProtocol . StackTraceResponse , args : DebugProtocol . StackTraceArguments ,
468+ cb_good : ( res : DebugProtocol . StackTraceResponse ) => any ,
469+ cb_bad : ( res : DebugProtocol . StackTraceResponse , codeOrMessage : number , err : string ) => any ) {
390470 this . miDebugger . getStack ( args . startFrame , args . levels , args . threadId ) . then ( stack => {
391471 const ret : StackFrame [ ] = [ ] ;
392472 stack . forEach ( element => {
@@ -414,12 +494,19 @@ export class MI2DebugSession extends DebugSession {
414494 response . body = {
415495 stackFrames : ret
416496 } ;
417- this . sendResponse ( response ) ;
497+ cb_good ( response ) ;
418498 } , err => {
419- this . sendErrorResponse ( response , 12 , `Failed to get Stack Trace: ${ err . toString ( ) } ` ) ;
499+ cb_bad ( response , 12 , `Failed to get Stack Trace: ${ err . toString ( ) } ` ) ;
420500 } ) ;
421501 }
422502
503+ public override stackTraceRequest ( response : DebugProtocol . StackTraceResponse , args : DebugProtocol . StackTraceArguments ) : void {
504+ this . inferiorStackTraceRequest ( response , args ,
505+ ( r : DebugProtocol . StackTraceResponse ) => this . sendResponse ( r ) ,
506+ ( r : DebugProtocol . StackTraceResponse , c : number , e : string ) => this . sendErrorResponse ( r , c , e )
507+ )
508+ }
509+
423510 public override configurationDoneRequest ( response : DebugProtocol . ConfigurationDoneResponse , args : DebugProtocol . ConfigurationDoneArguments ) : void {
424511 const promises : Thenable < any > [ ] = [ ] ;
425512 let entryPoint : string | undefined = undefined ;
0 commit comments