|
| 1 | +using BSPEngine; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading; |
| 8 | + |
| 9 | +namespace RenesasDebugPackage |
| 10 | +{ |
| 11 | + public class RenesasDebugController : IDebugMethodController, ICustomSettingsTypeProvider |
| 12 | + { |
| 13 | + public virtual Type[] SettingsObjectTypes => new[] { typeof(RenesasDebugSettings) }; |
| 14 | + |
| 15 | + public virtual ICustomSettingsTypeProvider TypeProvider => this; |
| 16 | + |
| 17 | + public virtual bool SupportsConnectionTesting => false; |
| 18 | + |
| 19 | + public virtual ICustomDebugMethodConfigurator CreateConfigurator(LoadedBSP.LoadedDebugMethod method, IBSPConfiguratorHost host) |
| 20 | + { |
| 21 | + return new GUI.RenesasDebugSettingsControl(method, host, this).Controller; |
| 22 | + } |
| 23 | + |
| 24 | + public IGDBStubInstance StartGDBStub(IDebugStartService startService, DebugStartContext context) |
| 25 | + { |
| 26 | + var settings = (RenesasDebugSettings)context.Configuration ?? new RenesasDebugSettings(); |
| 27 | + |
| 28 | + var cmdLine = new RenesasGDBServerCommandLine(settings.CommandLineArguments); |
| 29 | + int gdbPort; |
| 30 | + using (var allocator = startService.BeginAllocatingTCPPorts()) |
| 31 | + { |
| 32 | + cmdLine.GDBPort = gdbPort = allocator.AllocateUnusedTCPPort("SYS:GDB_PORT"); |
| 33 | + cmdLine.AuxiliaryPort = allocator.AllocateUnusedTCPPort("com.sysprogs.renesas.auxiliary_port"); |
| 34 | + } |
| 35 | + |
| 36 | + string debugComponentLinkFile = Path.Combine(GetOpenOCDDirectory(context.Method.Directory), "DebugCompLink.txt"); |
| 37 | + if (!File.Exists(debugComponentLinkFile)) |
| 38 | + throw new Exception($"{debugComponentLinkFile} does not exist"); |
| 39 | + |
| 40 | + cmdLine.DeviceID = startService.MCU.ExpandedMCU.ID; |
| 41 | + if (cmdLine.DebugInterface == null) |
| 42 | + cmdLine.DebugInterface = "EZ"; |
| 43 | + |
| 44 | + string debugComponentDir = File.ReadAllText(debugComponentLinkFile); |
| 45 | + string e2gdbServer = Path.Combine(debugComponentDir, "e2-server-gdb.exe"); |
| 46 | + if (!File.Exists(e2gdbServer)) |
| 47 | + throw new Exception("Could not find " + e2gdbServer); |
| 48 | + |
| 49 | + var tool = startService.LaunchCommandLineTool(new CommandLineToolLaunchInfo |
| 50 | + { |
| 51 | + Command = e2gdbServer, |
| 52 | + Arguments = cmdLine.CommandLine, |
| 53 | + WorkingDirectory = Path.GetDirectoryName(e2gdbServer) |
| 54 | + }); |
| 55 | + |
| 56 | + return new RenesasGDBStub(context, settings, cmdLine, gdbPort, tool); |
| 57 | + } |
| 58 | + |
| 59 | + protected virtual string GetOpenOCDDirectory(string methodDir) |
| 60 | + { |
| 61 | + return methodDir; |
| 62 | + } |
| 63 | + |
| 64 | + public virtual object TryConvertLegacyConfiguration(IBSPConfiguratorHost host, string methodDirectory, Dictionary<string, string> legacyConfiguration) => null; |
| 65 | + |
| 66 | + class RenesasGDBStub : IGDBStubInstance |
| 67 | + { |
| 68 | + private readonly int _GDBPort; |
| 69 | + private readonly RenesasDebugSettings _Settings; |
| 70 | + private readonly RenesasGDBServerCommandLine _CmdLine; |
| 71 | + |
| 72 | + public RenesasGDBStub(DebugStartContext context, RenesasDebugSettings settings, RenesasGDBServerCommandLine cmdLine, int gdbPort, IExternalToolInstance tool) |
| 73 | + { |
| 74 | + Tool = tool; |
| 75 | + _CmdLine = cmdLine; |
| 76 | + _GDBPort = gdbPort; |
| 77 | + _Settings = settings; |
| 78 | + } |
| 79 | + |
| 80 | + public IExternalToolInstance Tool { get; private set; } |
| 81 | + |
| 82 | + |
| 83 | + public object LocalGDBEndpoint => _GDBPort; |
| 84 | + |
| 85 | + public IConsoleOutputClassifier OutputClassifier => null; |
| 86 | + |
| 87 | + public virtual void ConnectGDBToStub(IDebugStartService service, ISimpleGDBSession session) |
| 88 | + { |
| 89 | + string[] regularStartupCommands = new[] |
| 90 | + { |
| 91 | + "-gdb-set breakpoint pending on", |
| 92 | + "-gdb-set detach-on-fork on", |
| 93 | + "-gdb-set python print-stack none", |
| 94 | + "-gdb-set print object on", |
| 95 | + "-gdb-set print sevenbit-strings on", |
| 96 | + "-gdb-set host-charset UTF-8", |
| 97 | + "-gdb-set target-charset WINDOWS-1252", |
| 98 | + "-gdb-set target-wide-charset UTF-16", |
| 99 | + "-gdb-set pagination off", |
| 100 | + "-gdb-set auto-solib-add on", |
| 101 | + "inferior 1", |
| 102 | + "set remotetimeout 10", |
| 103 | + "set tcp connect-timeout 30", |
| 104 | + }; |
| 105 | + |
| 106 | + SimpleGDBCommandResult result; |
| 107 | + foreach(var cmd in regularStartupCommands) |
| 108 | + { |
| 109 | + result = session.RunGDBCommand(cmd); |
| 110 | + if (!result.IsDone) |
| 111 | + throw new Exception("GDB command failed: " + cmd); |
| 112 | + } |
| 113 | + |
| 114 | + session.EnableAsyncMode(GDBAsyncMode.AsyncWithTemporaryBreaks, true, true); |
| 115 | + session.ConnectToExtendedRemote(null, _GDBPort, true); |
| 116 | + |
| 117 | + result = session.RunGDBCommand("mon is_target_connected"); |
| 118 | + if (!result.IsDone) |
| 119 | + throw new Exception("The target did not report connection state"); |
| 120 | + |
| 121 | + if (result.StubOutput?.FirstOrDefault(l=>l.Trim() == "Connection status=connected.") == null) |
| 122 | + throw new Exception("The Renesas gdb stub is not connected to the target."); |
| 123 | + |
| 124 | + result = session.RunGDBCommand("monitor get_no_hw_bkpts_available"); |
| 125 | + if (result.IsDone && result.StubOutput != null) |
| 126 | + foreach(var line in result.StubOutput) |
| 127 | + { |
| 128 | + if (int.TryParse(line.Trim(), out var tmp)) |
| 129 | + { |
| 130 | + session.RunGDBCommand($"set remote hardware-breakpoint-limit " + tmp); |
| 131 | + break; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + result = session.RunGDBCommand("monitor get_target_max_address"); |
| 136 | + if (result.IsDone && result.StubOutput != null) |
| 137 | + foreach (var line in result.StubOutput) |
| 138 | + { |
| 139 | + string trimmedLine = line.Trim(); |
| 140 | + if (trimmedLine.StartsWith("0x") && int.TryParse(trimmedLine.Substring(2), System.Globalization.NumberStyles.HexNumber, null, out var tmp)) |
| 141 | + { |
| 142 | + session.RunGDBCommand($"mem 0x0 0x{tmp+1:x} rw 8 nocache"); |
| 143 | + break; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + result = session.RunGDBCommand("monitor configuration_complete"); |
| 148 | + result = session.RunGDBCommand("monitor prg_download_start_on_connect"); |
| 149 | + |
| 150 | + result = session.RunGDBCommand("load"); |
| 151 | + if (!result.IsDone) |
| 152 | + throw new Exception("Failed to program FLASH memory"); |
| 153 | + |
| 154 | + string[] finalCommands = new[] |
| 155 | + { |
| 156 | + "monitor prg_download_end", |
| 157 | + "monitor reset", |
| 158 | + "monitor enable_stopped_notify_on_connect", |
| 159 | + "monitor enable_execute_on_connect", |
| 160 | + }; |
| 161 | + |
| 162 | + using (var awaiter = session.InterceptFirstStoppedEvent()) |
| 163 | + { |
| 164 | + foreach (var cmd in finalCommands) |
| 165 | + { |
| 166 | + result = session.RunGDBCommand(cmd); |
| 167 | + if (!result.IsDone) |
| 168 | + throw new Exception("GDB command failed: " + cmd); |
| 169 | + } |
| 170 | + |
| 171 | + while (!awaiter.WaitForStop(100)) |
| 172 | + session.RunGDBCommand("monitor do_nothing"); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + public ILiveMemoryEvaluator CreateLiveMemoryEvaluator(IDebugStartService service) => null; |
| 177 | + |
| 178 | + public void Dispose() |
| 179 | + { |
| 180 | + } |
| 181 | + |
| 182 | + public string TryGetMeaningfulErrorMessageFromStubOutput() |
| 183 | + { |
| 184 | + return null; |
| 185 | + } |
| 186 | + |
| 187 | + public bool WaitForToolToStart(ManualResetEvent cancelEvent) |
| 188 | + { |
| 189 | + while (!cancelEvent.WaitOne(100)) |
| 190 | + { |
| 191 | + if (!Tool.IsRunning || Tool.AllText.Contains("\nFinished target connection")) |
| 192 | + return true; |
| 193 | + } |
| 194 | + return true; |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | +} |
0 commit comments