Skip to content

Commit 1ba42fc

Browse files
kboomGrzegorz Gurgul (from Dev Box)
andauthored
Bring back debugging capabilities on the agent (#4926)
* Reapply "Allow running agent in debugging mode for specific node task (#4838)" (#4898) This reverts commit c660a56. * Fix of the production issue * unset debug --------- Co-authored-by: Grzegorz Gurgul (from Dev Box) <[email protected]>
1 parent 1bda91c commit 1ba42fc

File tree

7 files changed

+47
-0
lines changed

7 files changed

+47
-0
lines changed

src/Agent.Listener/Agent.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,20 @@ public async Task<int> ExecuteCommand(CommandSettings command)
216216
Trace.Info($"Set agent startup type - {startType}");
217217
HostContext.StartupType = startType;
218218

219+
bool debugModeEnabled = command.GetDebugMode();
220+
221+
if (debugModeEnabled)
222+
{
223+
Trace.Warning("Agent is running in debug mode, don't use it in production");
224+
settings.DebugMode = true;
225+
store.SaveSettings(settings);
226+
}
227+
else if (settings.DebugMode && !debugModeEnabled)
228+
{
229+
settings.DebugMode = false;
230+
store.SaveSettings(settings);
231+
}
232+
219233
if (PlatformUtil.RunningOnWindows)
220234
{
221235
if (store.IsAutoLogonConfigured())

src/Agent.Listener/CommandLine/RunAgent.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@ public class RunAgent : BaseCommand
1818

1919
[Option(Constants.Agent.CommandLine.Args.StartupType)]
2020
public string StartupType { get; set; }
21+
22+
[Option(Constants.Agent.CommandLine.Flags.DebugMode)]
23+
public bool DebugMode { get; set; }
2124
}
2225
}

src/Agent.Listener/CommandSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,11 @@ public bool GetRunOnce()
550550
TestFlag(Run?.RunOnce, Constants.Agent.CommandLine.Flags.Once);
551551
}
552552

553+
public bool GetDebugMode()
554+
{
555+
return TestFlag(Run?.DebugMode, Constants.Agent.CommandLine.Flags.DebugMode);
556+
}
557+
553558
public bool GetDeploymentPool()
554559
{
555560
return TestFlag(Configure?.DeploymentPool, Constants.Agent.CommandLine.Flags.DeploymentPool);

src/Agent.Sdk/Knob/AgentKnobs.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ public class AgentKnobs
213213
new EnvironmentKnobSource("VSTSAGENT_TRACE"),
214214
new BuiltInDefaultKnobSource(string.Empty));
215215

216+
public static readonly Knob DebugTask = new Knob(
217+
nameof(DebugTask),
218+
"If the agent executes a task which ID or name matches the value provided, it will run the task so that it will wait for debugger to attach",
219+
new EnvironmentKnobSource("VSTSAGENT_DEBUG_TASK"),
220+
new BuiltInDefaultKnobSource(string.Empty));
221+
216222
public static readonly Knob DumpJobEventLogs = new Knob(
217223
nameof(DumpJobEventLogs),
218224
"If true, dump event viewer logs",

src/Agent.Worker/Handlers/NodeHandler.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,21 @@ public async Task RunAsync()
224224
var sigtermTimeout = TimeSpan.FromMilliseconds(AgentKnobs.ProccessSigtermTimeout.GetValue(ExecutionContext).AsInt());
225225
var useGracefulShutdown = AgentKnobs.UseGracefulProcessShutdown.GetValue(ExecutionContext).AsBoolean();
226226

227+
var configStore = HostContext.GetService<IConfigurationStore>();
228+
var agentSettings = configStore.GetSettings();
229+
if (agentSettings.DebugMode)
230+
{
231+
var debugTask = AgentKnobs.DebugTask.GetValue(ExecutionContext).AsString();
232+
if (!string.IsNullOrEmpty(debugTask))
233+
{
234+
if (string.Equals(Task?.Id.ToString("D"), debugTask, StringComparison.OrdinalIgnoreCase) || string.Equals(Task?.Name, debugTask, StringComparison.OrdinalIgnoreCase))
235+
{
236+
arguments = $"--inspect-brk {arguments}";
237+
}
238+
}
239+
}
240+
241+
227242
try
228243
{
229244
// Execute the process. Exit code 0 should always be returned.

src/Microsoft.VisualStudio.Services.Agent/ConfigurationStore.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ public string Fingerprint
135135

136136
[DataMember(EmitDefaultValue = false)]
137137
public int MaxDedupParallelism { get; set; }
138+
139+
[DataMember(EmitDefaultValue = false)]
140+
public bool DebugMode { get; set; }
138141
}
139142

140143
[DataContract]

src/Microsoft.VisualStudio.Services.Agent/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ public static class Flags
226226
public const string NoRestart = "norestart";
227227
public const string LaunchBrowser = "launchbrowser";
228228
public const string Once = "once";
229+
public const string DebugMode = "debug";
229230
public const string RunAsAutoLogon = "runasautologon";
230231
public const string RunAsService = "runasservice";
231232
public const string PreventServiceStart = "preventservicestart";

0 commit comments

Comments
 (0)