protected override async Task <bool> AcceptCommand(MessageId id, string method, JObject args, CancellationToken token) { if (!contexts.TryGetValue(id, out var context)) { return(false); } switch (method) { case "Debugger.enable": { var resp = await SendCommand(id, method, args, token); context.DebuggerId = resp.Value ["debuggerId"]?.ToString(); if (await IsRuntimeAlreadyReadyAlready(id, token)) { await RuntimeReady(id, token); } SendResponse(id, resp, token); return(true); } case "Debugger.getScriptSource": { var script = args? ["scriptId"]?.Value <string> (); return(await OnGetScriptSource(id, script, token)); } case "Runtime.compileScript": { var exp = args? ["expression"]?.Value <string> (); if (exp.StartsWith("//dotnet:", StringComparison.Ordinal)) { OnCompileDotnetScript(id, token); return(true); } break; } case "Debugger.getPossibleBreakpoints": { var resp = await SendCommand(id, method, args, token); if (resp.IsOk && resp.Value["locations"].HasValues) { SendResponse(id, resp, token); return(true); } var start = SourceLocation.Parse(args? ["start"] as JObject); //FIXME support variant where restrictToFunction=true and end is omitted var end = SourceLocation.Parse(args? ["end"] as JObject); if (start != null && end != null && await GetPossibleBreakpoints(id, start, end, token)) { return(true); } SendResponse(id, resp, token); return(true); } case "Debugger.setBreakpoint": { break; } case "Debugger.setBreakpointByUrl": { var resp = await SendCommand(id, method, args, token); if (!resp.IsOk) { SendResponse(id, resp, token); return(true); } var bpid = resp.Value["breakpointId"]?.ToString(); var request = BreakpointRequest.Parse(bpid, args); context.BreakpointRequests[bpid] = request; if (await IsRuntimeAlreadyReadyAlready(id, token)) { var store = await RuntimeReady(id, token); Log("verbose", $"BP req {args}"); await SetBreakpoint(id, store, request, token); } SendResponse(id, Result.OkFromObject(request.AsSetBreakpointByUrlResponse()), token); return(true); } case "Debugger.removeBreakpoint": { return(await RemoveBreakpoint(id, args, token)); } case "Debugger.resume": { await OnResume(id, token); break; } case "Debugger.stepInto": { return(await Step(id, StepKind.Into, token)); } case "Debugger.stepOut": { return(await Step(id, StepKind.Out, token)); } case "Debugger.stepOver": { return(await Step(id, StepKind.Over, token)); } case "Debugger.evaluateOnCallFrame": { var objId = args? ["callFrameId"]?.Value <string> (); if (objId.StartsWith("dotnet:", StringComparison.Ordinal)) { var parts = objId.Split(new char [] { ':' }); if (parts.Length < 3) { return(true); } switch (parts [1]) { case "scope": { await GetEvaluateOnCallFrame(id, int.Parse(parts [2]), args? ["expression"]?.Value <string> (), token); break; } } return(true); } return(false); } case "Runtime.getProperties": { var objId = args? ["objectId"]?.Value <string> (); if (objId.StartsWith("dotnet:", StringComparison.Ordinal)) { var parts = objId.Split(new char [] { ':' }); if (parts.Length < 3) { return(true); } switch (parts[1]) { case "scope": { await GetScopeProperties(id, int.Parse(parts[2]), token); break; } case "object": { await GetDetails(id, MonoCommands.GetObjectProperties(int.Parse(parts[2]), expandValueTypes: false), token); break; } case "array": { await GetArrayDetails(id, objId, parts, token); break; } case "valuetype": { await GetDetailsForValueType(id, objId, get_props_cmd_fn : () => { if (parts.Length < 4) { return(null); } var containerObjId = int.Parse(parts[2]); return(MonoCommands.GetObjectProperties(containerObjId, expandValueTypes: true)); }, token); break; } } return(true); } break; } // Protocol extensions case "Dotnet-test.setBreakpointByMethod": { Console.WriteLine("set-breakpoint-by-method: " + id + " " + args); var store = await RuntimeReady(id, token); string aname = args ["assemblyName"]?.Value <string> (); string typeName = args ["typeName"]?.Value <string> (); string methodName = args ["methodName"]?.Value <string> (); if (aname == null || typeName == null || methodName == null) { SendResponse(id, Result.Err("Invalid protocol message '" + args + "'."), token); return(true); } // GetAssemblyByName seems to work on file names var assembly = store.GetAssemblyByName(aname); if (assembly == null) { assembly = store.GetAssemblyByName(aname + ".exe"); } if (assembly == null) { assembly = store.GetAssemblyByName(aname + ".dll"); } if (assembly == null) { SendResponse(id, Result.Err("Assembly '" + aname + "' not found."), token); return(true); } var type = assembly.GetTypeByName(typeName); if (type == null) { SendResponse(id, Result.Err($"Type '{typeName}' not found."), token); return(true); } var methodInfo = type.Methods.FirstOrDefault(m => m.Name == methodName); if (methodInfo == null) { SendResponse(id, Result.Err($"Method '{typeName}:{methodName}' not found."), token); return(true); } bpIdGenerator++; string bpid = "by-method-" + bpIdGenerator.ToString(); var request = new BreakpointRequest(bpid, methodInfo); context.BreakpointRequests[bpid] = request; var loc = methodInfo.StartLocation; var bp = await SetMonoBreakpoint(id, bpid, loc, token); if (bp.State != BreakpointState.Active) { // FIXME: throw new NotImplementedException(); } var resolvedLocation = new { breakpointId = bpid, location = loc.AsLocation() }; SendEvent(id, "Debugger.breakpointResolved", JObject.FromObject(resolvedLocation), token); SendResponse(id, Result.OkFromObject(new { result = new { breakpointId = bpid, locations = new object [] { loc.AsLocation() } } }), token); return(true); } } return(false); }
internal async Task <JToken> TryGetVariableValue(MessageId msg_id, int scope_id, string expression, bool only_search_on_this, CancellationToken token) { JToken thisValue = null; var context = GetContext(msg_id); if (context.CallStack == null) { return(null); } if (TryFindVariableValueInCache(context, expression, only_search_on_this, out JToken obj)) { return(obj); } var scope = context.CallStack.FirstOrDefault(s => s.Id == scope_id); var vars = scope.Method.GetLiveVarsAt(scope.Location.CliLocation.Offset); //get_this int [] var_ids = { }; var res = await SendMonoCommand(msg_id, MonoCommands.GetScopeVariables(scope.Id, var_ids), token); var values = res.Value? ["result"]? ["value"]?.Values <JObject> ().ToArray(); thisValue = values.FirstOrDefault(v => v ["name"].Value <string> () == "this"); if (!only_search_on_this) { if (thisValue != null && expression == "this") { return(thisValue); } //search in locals var var_id = vars.SingleOrDefault(v => v.Name == expression); if (var_id != null) { res = await SendMonoCommand(msg_id, MonoCommands.GetScopeVariables(scope.Id, new int [] { var_id.Index }), token); values = res.Value? ["result"]? ["value"]?.Values <JObject> ().ToArray(); return(values [0]); } } //search in scope if (thisValue != null) { var objectId = thisValue ["value"] ["objectId"].Value <string> (); var parts = objectId.Split(new char [] { ':' }); res = await SendMonoCommand(msg_id, MonoCommands.GetObjectProperties(int.Parse(parts [2]), expandValueTypes: false), token); values = res.Value? ["result"]? ["value"]?.Values <JObject> ().ToArray(); var foundValue = values.FirstOrDefault(v => v ["name"].Value <string> () == expression); if (foundValue != null) { foundValue["fromThis"] = true; context.LocalsCache[foundValue ["name"].Value <string> ()] = foundValue; return(foundValue); } } return(null); }
protected override async Task <bool> AcceptCommand(MessageId id, string method, JObject args, CancellationToken token) { if (!contexts.TryGetValue(id, out var context)) { return(false); } switch (method) { case "Debugger.enable": { var resp = await SendCommand(id, method, args, token); context.DebuggerId = resp.Value ["debuggerId"]?.ToString(); if (await IsRuntimeAlreadyReadyAlready(id, token)) { await RuntimeReady(id, token); } SendResponse(id, resp, token); return(true); } case "Debugger.getScriptSource": { var script = args? ["scriptId"]?.Value <string> (); return(await OnGetScriptSource(id, script, token)); } case "Runtime.compileScript": { var exp = args? ["expression"]?.Value <string> (); if (exp.StartsWith("//dotnet:", StringComparison.Ordinal)) { OnCompileDotnetScript(id, token); return(true); } break; } case "Debugger.getPossibleBreakpoints": { var resp = await SendCommand(id, method, args, token); if (resp.IsOk && resp.Value["locations"].HasValues) { SendResponse(id, resp, token); return(true); } var start = SourceLocation.Parse(args? ["start"] as JObject); //FIXME support variant where restrictToFunction=true and end is omitted var end = SourceLocation.Parse(args? ["end"] as JObject); if (start != null && end != null && await GetPossibleBreakpoints(id, start, end, token)) { return(true); } SendResponse(id, resp, token); return(true); } case "Debugger.setBreakpoint": { break; } case "Debugger.setBreakpointByUrl": { var resp = await SendCommand(id, method, args, token); if (!resp.IsOk) { SendResponse(id, resp, token); return(true); } var bpid = resp.Value["breakpointId"]?.ToString(); var request = BreakpointRequest.Parse(bpid, args); context.BreakpointRequests[bpid] = request; if (await IsRuntimeAlreadyReadyAlready(id, token)) { var store = await RuntimeReady(id, token); Log("verbose", $"BP req {args}"); await SetBreakpoint(id, store, request, token); } SendResponse(id, Result.OkFromObject(request.AsSetBreakpointByUrlResponse()), token); return(true); } case "Debugger.removeBreakpoint": { return(await RemoveBreakpoint(id, args, token)); } case "Debugger.resume": { await OnResume(id, token); break; } case "Debugger.stepInto": { return(await Step(id, StepKind.Into, token)); } case "Debugger.stepOut": { return(await Step(id, StepKind.Out, token)); } case "Debugger.stepOver": { return(await Step(id, StepKind.Over, token)); } case "Debugger.evaluateOnCallFrame": { var objId = args? ["callFrameId"]?.Value <string> (); if (objId.StartsWith("dotnet:", StringComparison.Ordinal)) { var parts = objId.Split(new char [] { ':' }); if (parts.Length < 3) { return(true); } switch (parts [1]) { case "scope": { await GetEvaluateOnCallFrame(id, int.Parse(parts [2]), args? ["expression"]?.Value <string> (), token); break; } } return(true); } return(false); } case "Runtime.getProperties": { var objId = args? ["objectId"]?.Value <string> (); if (objId.StartsWith("dotnet:", StringComparison.Ordinal)) { var parts = objId.Split(new char [] { ':' }); if (parts.Length < 3) { return(true); } switch (parts[1]) { case "scope": { await GetScopeProperties(id, int.Parse(parts[2]), token); break; } case "object": { await GetDetails(id, MonoCommands.GetObjectProperties(int.Parse(parts[2]), expandValueTypes: false), token); break; } case "array": { await GetArrayDetails(id, objId, parts, token); break; } case "valuetype": { await GetDetailsForValueType(id, objId, get_props_cmd_fn : () => { if (parts.Length < 4) { return(null); } var containerObjId = int.Parse(parts[2]); return(MonoCommands.GetObjectProperties(containerObjId, expandValueTypes: true)); }, token); break; } } return(true); } break; } } return(false); }