Skip to content

Commit 3f88445

Browse files
authored
Allow running agent in debugging mode for specific node task (#4838)
This change introduces a new knob called VSTSAGENT_DEBUG_TASK which can be set to either task GUID or task name and version (like AzureCLIV2). Once set, the agent will execute node process for any pipeline run which includes this task in debug mode, which will wait for the debugger to be attached.
1 parent 8b332ae commit 3f88445

File tree

7 files changed

+40
-0
lines changed

7 files changed

+40
-0
lines changed

src/Agent.Listener/Agent.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ 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+
settings.DebugMode = debugModeEnabled;
221+
store.SaveSettings(settings);
222+
if (debugModeEnabled)
223+
{
224+
Trace.Warning("Agent is running in debug mode, don't use it in production");
225+
}
219226
if (PlatformUtil.RunningOnWindows)
220227
{
221228
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
@@ -544,6 +544,11 @@ public bool GetRunOnce()
544544
TestFlag(Run?.RunOnce, Constants.Agent.CommandLine.Flags.Once);
545545
}
546546

547+
public bool GetDebugMode()
548+
{
549+
return TestFlag(Run?.DebugMode, Constants.Agent.CommandLine.Flags.DebugMode);
550+
}
551+
547552
public bool GetDeploymentPool()
548553
{
549554
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
@@ -199,6 +199,12 @@ public class AgentKnobs
199199
new EnvironmentKnobSource("VSTSAGENT_TRACE"),
200200
new BuiltInDefaultKnobSource(string.Empty));
201201

202+
public static readonly Knob DebugTask = new Knob(
203+
nameof(DebugTask),
204+
"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",
205+
new EnvironmentKnobSource("VSTSAGENT_DEBUG_TASK"),
206+
new BuiltInDefaultKnobSource(string.Empty));
207+
202208
public static readonly Knob DumpJobEventLogs = new Knob(
203209
nameof(DumpJobEventLogs),
204210
"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
@@ -132,6 +132,9 @@ public string Fingerprint
132132

133133
[DataMember(EmitDefaultValue = false)]
134134
public int MaxDedupParallelism { get; set; }
135+
136+
[DataMember(EmitDefaultValue = false)]
137+
public bool DebugMode { get; set; }
135138
}
136139

137140
[DataContract]

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

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

0 commit comments

Comments
 (0)