@@ -26,7 +26,7 @@ internal abstract class DebugeeProcess
2626 private bool _stdoutEOF ;
2727 private bool _stderrEOF ;
2828 private bool _attachMode ;
29-
29+
3030 private Encoding _dapEncoding ;
3131
3232 private OneScriptDebuggerClient _debugger ;
@@ -41,9 +41,22 @@ public DebugeeProcess(PathHandlingStrategy pathHandling)
4141 {
4242 _strategy = pathHandling ;
4343 }
44-
45- public bool HasExited => _process ? . HasExited ?? true ;
46- public int ExitCode => _process . ExitCode ;
44+
45+ public bool HasExited
46+ {
47+ get
48+ {
49+ if ( _process != null )
50+ return _process . HasExited ;
51+
52+ if ( _attachMode && _debugger != null )
53+ return false ;
54+
55+ return true ;
56+ }
57+ }
58+
59+ public int ExitCode => _process ? . ExitCode ?? 0 ;
4760
4861 public int DebugPort { get ; set ; }
4962
@@ -56,14 +69,16 @@ public int ProtocolVersion
5669 _activeProtocolVersion = value ;
5770 }
5871 }
59-
72+
6073 public bool WaitOnStart { get ; set ; }
6174
75+ public WorkspaceMapper PathsMapper { get ; set ; }
76+
6277 public void Start ( )
6378 {
6479 _process = CreateProcess ( ) ;
6580 var psi = _process . StartInfo ;
66-
81+
6782 psi . RedirectStandardError = true ;
6883 psi . RedirectStandardOutput = true ;
6984
@@ -83,21 +98,57 @@ public void Start()
8398 _process . BeginOutputReadLine ( ) ;
8499 _process . BeginErrorReadLine ( ) ;
85100 }
86-
101+
87102 public void InitAttached ( )
88103 {
89104 var pid = _debugger . GetProcessId ( ) ;
90- _process = Process . GetProcessById ( pid ) ;
105+
106+ try
107+ {
108+ _process = Process . GetProcessById ( pid ) ;
109+ _process . EnableRaisingEvents = true ;
110+ _process . Exited += Process_Exited ;
111+ }
112+ catch
113+ {
114+ _process = null ;
115+ }
116+
91117 _attachMode = true ;
92- _process . EnableRaisingEvents = true ;
93- _process . Exited += Process_Exited ;
118+
94119 }
95-
120+
96121 public void Init ( JObject args )
97122 {
98123 InitInternal ( args ) ;
99124 }
100-
125+
126+ public void InitPathsMapper ( JObject args )
127+ {
128+ if ( args == null )
129+ {
130+ PathsMapper = null ;
131+ return ;
132+ }
133+
134+ try
135+ {
136+ var mappingToken = args [ "pathsMapping" ] ;
137+ if ( mappingToken == null || mappingToken . Type == JTokenType . Null )
138+ {
139+ PathsMapper = null ;
140+ return ;
141+ }
142+
143+ PathsMapper = mappingToken . ToObject < WorkspaceMapper > ( ) ;
144+ }
145+ catch ( Exception ex )
146+ {
147+ Log . Warning ( ex , "Failed to initialize paths mapper; path mapping will be disabled" ) ;
148+ PathsMapper = null ;
149+ }
150+ }
151+
101152 protected abstract Process CreateProcess ( ) ;
102153
103154 protected abstract void InitInternal ( JObject args ) ;
@@ -106,12 +157,12 @@ protected string ConvertClientPathToDebugger(string clientPath)
106157 {
107158 return _strategy . ConvertClientPathToDebugger ( clientPath ) ;
108159 }
109-
160+
110161 protected void LoadEnvironment ( ProcessStartInfo psi , IDictionary < string , string > variables )
111162 {
112163 if ( variables == null || variables . Count <= 0 )
113164 return ;
114-
165+
115166 foreach ( var pair in variables )
116167 {
117168 psi . EnvironmentVariables [ pair . Key ] = pair . Value ;
@@ -128,7 +179,7 @@ protected void SetEncoding(string encodingFromOptions)
128179 {
129180 _dapEncoding = Utilities . GetEncodingFromOptions ( encodingFromOptions ) ;
130181 }
131-
182+
132183 Log . Information ( "Encoding for debuggee output is {Encoding}" , _dapEncoding ) ;
133184 }
134185
@@ -142,10 +193,10 @@ public void SetClient(OneScriptDebuggerClient service)
142193 _debugger = service ;
143194 ProtocolVersion = service . ProtocolVersion ;
144195 }
145-
196+
146197 public event EventHandler < DebugeeOutputEventArgs > OutputReceived ;
147198 public event EventHandler ProcessExited ;
148-
199+
149200 private void Process_Exited ( object sender , EventArgs e )
150201 {
151202 _debugger ? . Stop ( ) ;
@@ -194,7 +245,7 @@ private void Terminate()
194245 {
195246 System . Threading . Thread . Sleep ( 100 ) ;
196247 }
197-
248+
198249 _terminated = true ;
199250 _process = null ;
200251 _debugger = null ;
@@ -211,7 +262,7 @@ public void HandleDisconnect(bool terminate)
211262 _debugger . Disconnect ( terminate ) ;
212263
213264 var mustKill = terminate && ! _attachMode ;
214-
265+
215266 if ( mustKill && _process != null && ! _process . HasExited )
216267 {
217268 Log . Debug ( "Stopping child process..." ) ;
@@ -225,12 +276,15 @@ public void HandleDisconnect(bool terminate)
225276 Log . Debug ( "Process killed" ) ;
226277 }
227278 }
228-
279+
229280 Log . Debug ( "Debuggee disconnected" ) ;
230281 }
231282
232283 public void Kill ( )
233284 {
285+ if ( _process == null )
286+ return ;
287+
234288 _process . Kill ( ) ;
235289 _process . WaitForExit ( 1500 ) ;
236290 }
@@ -259,30 +313,48 @@ public void SetExceptionsBreakpoints((string Id, string Condition)[] filters)
259313
260314 public Breakpoint [ ] SetBreakpoints ( IEnumerable < Breakpoint > breakpoints )
261315 {
262- var confirmedBreaks = _debugger . SetMachineBreakpoints ( breakpoints . ToArray ( ) ) ;
263-
316+ var breakpointsArray = breakpoints . ToArray ( ) ;
317+
318+ if ( PathsMapper != null )
319+ {
320+ for ( int i = 0 ; i < breakpointsArray . Length ; i ++ )
321+ {
322+ breakpointsArray [ i ] . Source = PathsMapper . LocalToRemote ( breakpointsArray [ i ] . Source ) ;
323+ }
324+ }
325+
326+ var confirmedBreaks = _debugger . SetMachineBreakpoints ( breakpointsArray ) ;
327+
264328 return confirmedBreaks ;
265329 }
266330
267331 public void BeginExecution ( int threadId )
268332 {
269333 _debugger . Execute ( threadId ) ;
270334 }
271-
335+
272336 public StackFrame [ ] GetStackTrace ( int threadId , int firstFrameIdx , int limit )
273337 {
274338 var allFrames = _debugger . GetStackFrames ( threadId ) ;
275-
339+ var pathsMapperInit = PathsMapper != null ;
340+
276341 if ( limit == 0 )
277342 limit = allFrames . Length ;
278343
279- if ( allFrames . Length < firstFrameIdx )
344+ if ( allFrames . Length < firstFrameIdx )
280345 return new StackFrame [ 0 ] ;
281346
282347 var result = new List < StackFrame > ( ) ;
283348 for ( int i = firstFrameIdx ; i < limit && i < allFrames . Length ; i ++ )
284349 {
350+
285351 allFrames [ i ] . ThreadId = threadId ;
352+
353+ if ( pathsMapperInit )
354+ {
355+ allFrames [ i ] . Source = PathsMapper . RemoteToLocal ( allFrames [ i ] . Source ) ;
356+ }
357+
286358 result . Add ( allFrames [ i ] ) ;
287359 }
288360
0 commit comments