Skip to content

Commit

Permalink
Bring back debugging capabilities on the agent (#4926)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
kboom and Grzegorz Gurgul (from Dev Box) authored Aug 6, 2024
1 parent 1bda91c commit 1ba42fc
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Agent.Listener/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ public async Task<int> ExecuteCommand(CommandSettings command)
Trace.Info($"Set agent startup type - {startType}");
HostContext.StartupType = startType;

bool debugModeEnabled = command.GetDebugMode();

if (debugModeEnabled)
{
Trace.Warning("Agent is running in debug mode, don't use it in production");
settings.DebugMode = true;
store.SaveSettings(settings);
}
else if (settings.DebugMode && !debugModeEnabled)
{
settings.DebugMode = false;
store.SaveSettings(settings);
}

if (PlatformUtil.RunningOnWindows)
{
if (store.IsAutoLogonConfigured())
Expand Down
3 changes: 3 additions & 0 deletions src/Agent.Listener/CommandLine/RunAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ public class RunAgent : BaseCommand

[Option(Constants.Agent.CommandLine.Args.StartupType)]
public string StartupType { get; set; }

[Option(Constants.Agent.CommandLine.Flags.DebugMode)]
public bool DebugMode { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/Agent.Listener/CommandSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,11 @@ public bool GetRunOnce()
TestFlag(Run?.RunOnce, Constants.Agent.CommandLine.Flags.Once);
}

public bool GetDebugMode()
{
return TestFlag(Run?.DebugMode, Constants.Agent.CommandLine.Flags.DebugMode);
}

public bool GetDeploymentPool()
{
return TestFlag(Configure?.DeploymentPool, Constants.Agent.CommandLine.Flags.DeploymentPool);
Expand Down
6 changes: 6 additions & 0 deletions src/Agent.Sdk/Knob/AgentKnobs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ public class AgentKnobs
new EnvironmentKnobSource("VSTSAGENT_TRACE"),
new BuiltInDefaultKnobSource(string.Empty));

public static readonly Knob DebugTask = new Knob(
nameof(DebugTask),
"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",
new EnvironmentKnobSource("VSTSAGENT_DEBUG_TASK"),
new BuiltInDefaultKnobSource(string.Empty));

public static readonly Knob DumpJobEventLogs = new Knob(
nameof(DumpJobEventLogs),
"If true, dump event viewer logs",
Expand Down
15 changes: 15 additions & 0 deletions src/Agent.Worker/Handlers/NodeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,21 @@ public async Task RunAsync()
var sigtermTimeout = TimeSpan.FromMilliseconds(AgentKnobs.ProccessSigtermTimeout.GetValue(ExecutionContext).AsInt());
var useGracefulShutdown = AgentKnobs.UseGracefulProcessShutdown.GetValue(ExecutionContext).AsBoolean();

var configStore = HostContext.GetService<IConfigurationStore>();
var agentSettings = configStore.GetSettings();
if (agentSettings.DebugMode)
{
var debugTask = AgentKnobs.DebugTask.GetValue(ExecutionContext).AsString();
if (!string.IsNullOrEmpty(debugTask))
{
if (string.Equals(Task?.Id.ToString("D"), debugTask, StringComparison.OrdinalIgnoreCase) || string.Equals(Task?.Name, debugTask, StringComparison.OrdinalIgnoreCase))
{
arguments = $"--inspect-brk {arguments}";
}
}
}


try
{
// Execute the process. Exit code 0 should always be returned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public string Fingerprint

[DataMember(EmitDefaultValue = false)]
public int MaxDedupParallelism { get; set; }

[DataMember(EmitDefaultValue = false)]
public bool DebugMode { get; set; }
}

[DataContract]
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.VisualStudio.Services.Agent/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ public static class Flags
public const string NoRestart = "norestart";
public const string LaunchBrowser = "launchbrowser";
public const string Once = "once";
public const string DebugMode = "debug";
public const string RunAsAutoLogon = "runasautologon";
public const string RunAsService = "runasservice";
public const string PreventServiceStart = "preventservicestart";
Expand Down

0 comments on commit 1ba42fc

Please sign in to comment.