File size: 15,960 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Management.Automation.Remoting.Internal;
using System.Management.Automation.Runspaces;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// This cmdlet takes a Job object and checks to see if it is debuggable. If it
/// is debuggable then it breaks into the job debugger in step mode. If it is not
/// debuggable then it is treated as a parent job and each child job is checked if
/// it is debuggable and if it is will break into its job debugger in step mode.
/// For multiple debuggable child jobs, each job execution will be halted and the
/// debugger will step to each job execution point sequentially.
///
/// When a job is debugged its output data is written to host and the executing job
/// script will break into the host debugger, in step mode, at the next stoppable
/// execution point.
/// </summary>
[SuppressMessage("Microsoft.PowerShell", "PS1012:CallShouldProcessOnlyIfDeclaringSupport")]
[Cmdlet(VerbsDiagnostic.Debug, "Job", SupportsShouldProcess = true, DefaultParameterSetName = DebugJobCommand.JobParameterSet,
HelpUri = "https://go.microsoft.com/fwlink/?LinkId=330208")]
public sealed class DebugJobCommand : PSCmdlet
{
#region Strings
private const string JobParameterSet = "JobParameterSet";
private const string JobNameParameterSet = "JobNameParameterSet";
private const string JobIdParameterSet = "JobIdParameterSet";
private const string JobInstanceIdParameterSet = "JobInstanceIdParameterSet";
#endregion
#region Private members
private Job _job;
private Debugger _debugger;
private PSDataCollection<PSStreamObject> _debugCollection;
#endregion
#region Parameters
/// <summary>
/// The Job object to be debugged.
/// </summary>
[Parameter(Position = 0,
Mandatory = true,
ValueFromPipelineByPropertyName = true,
ValueFromPipeline = true,
ParameterSetName = DebugJobCommand.JobParameterSet)]
public Job Job
{
get;
set;
}
/// <summary>
/// The Job object name to be debugged.
/// </summary>
[Parameter(Position = 0,
Mandatory = true,
ParameterSetName = DebugJobCommand.JobNameParameterSet)]
public string Name
{
get;
set;
}
/// <summary>
/// The Job object Id to be debugged.
/// </summary>
[Parameter(Position = 0,
Mandatory = true,
ParameterSetName = DebugJobCommand.JobIdParameterSet)]
public int Id
{
get;
set;
}
/// <summary>
/// The Job object InstanceId to be debugged.
/// </summary>
[Parameter(Position = 0,
Mandatory = true,
ParameterSetName = DebugJobCommand.JobInstanceIdParameterSet)]
public Guid InstanceId
{
get;
set;
}
/// <summary>
/// Gets or sets a flag that tells PowerShell to automatically perform a BreakAll when the debugger is attached to the remote target.
/// </summary>
[Parameter]
public SwitchParameter BreakAll { get; set; }
#endregion
#region Overrides
/// <summary>
/// End processing. Do work.
/// </summary>
protected override void EndProcessing()
{
switch (ParameterSetName)
{
case DebugJobCommand.JobParameterSet:
_job = Job;
break;
case DebugJobCommand.JobNameParameterSet:
_job = GetJobByName(Name);
break;
case DebugJobCommand.JobIdParameterSet:
_job = GetJobById(Id);
break;
case DebugJobCommand.JobInstanceIdParameterSet:
_job = GetJobByInstanceId(InstanceId);
break;
}
if (!ShouldProcess(_job.Name, VerbsDiagnostic.Debug))
{
return;
}
Runspace runspace = LocalRunspace.DefaultRunspace;
if (runspace == null || runspace.Debugger == null)
{
ThrowTerminatingError(
new ErrorRecord(
new PSInvalidOperationException(RemotingErrorIdStrings.CannotDebugJobNoHostDebugger),
"DebugJobNoHostDebugger",
ErrorCategory.InvalidOperation,
this)
);
}
if ((runspace.Debugger.DebugMode == DebugModes.Default) || (runspace.Debugger.DebugMode == DebugModes.None))
{
ThrowTerminatingError(
new ErrorRecord(
new PSInvalidOperationException(RemotingErrorIdStrings.CannotDebugJobInvalidDebuggerMode),
"DebugJobWrongDebugMode",
ErrorCategory.InvalidOperation,
this)
);
}
if (this.Host == null || this.Host.UI == null)
{
ThrowTerminatingError(
new ErrorRecord(
new PSInvalidOperationException(RemotingErrorIdStrings.CannotDebugJobNoHostUI),
"DebugJobNoHostAvailable",
ErrorCategory.InvalidOperation,
this)
);
}
if (!CheckForDebuggableJob())
{
ThrowTerminatingError(
new ErrorRecord(
new PSInvalidOperationException(DebuggerStrings.NoDebuggableJobsFound),
"DebugJobNoDebuggableJobsFound",
ErrorCategory.InvalidOperation,
this)
);
}
// Set up host script debugger to debug the job.
_debugger = runspace.Debugger;
_debugger.DebugJob(_job, breakAll: BreakAll);
// Blocking call. Send job output to host UI while debugging and wait for Job completion.
WaitAndReceiveJobOutput();
}
/// <summary>
/// Stop processing.
/// </summary>
protected override void StopProcessing()
{
// Cancel job debugging.
Debugger debugger = _debugger;
if ((debugger != null) && (_job != null))
{
debugger.StopDebugJob(_job);
}
// Unblock the data collection.
PSDataCollection<PSStreamObject> debugCollection = _debugCollection;
debugCollection?.Complete();
}
#endregion
#region Private methods
/// <summary>
/// Check for debuggable job. Job must implement IJobDebugger and also
/// must be running or in Debug stopped state.
/// </summary>
/// <returns></returns>
private bool CheckForDebuggableJob()
{
// Check passed in job object.
bool debuggableJobFound = GetJobDebuggable(_job);
if (!debuggableJobFound)
{
// Assume passed in job is a container job and check child jobs.
foreach (var cJob in _job.ChildJobs)
{
debuggableJobFound = GetJobDebuggable(cJob);
if (debuggableJobFound)
{
break;
}
}
}
return debuggableJobFound;
}
private static bool GetJobDebuggable(Job job)
{
if (job is IJobDebugger)
{
return ((job.JobStateInfo.State == JobState.Running) ||
(job.JobStateInfo.State == JobState.AtBreakpoint));
}
return false;
}
private void WaitAndReceiveJobOutput()
{
_debugCollection = new PSDataCollection<PSStreamObject>();
_debugCollection.BlockingEnumerator = true;
try
{
AddEventHandlers();
// This call blocks (blocking enumerator) until the job completes
// or this command is cancelled.
foreach (var streamItem in _debugCollection)
{
streamItem?.WriteStreamObject(this);
}
}
catch (Exception)
{
// Terminate job on exception.
if (!_job.IsFinishedState(_job.JobStateInfo.State))
{
_job.StopJob();
}
throw;
}
finally
{
RemoveEventHandlers();
_debugCollection = null;
}
}
private void HandleJobStateChangedEvent(object sender, JobStateEventArgs stateChangedArgs)
{
Job job = sender as Job;
if (job.IsFinishedState(stateChangedArgs.JobStateInfo.State))
{
_debugCollection.Complete();
}
}
private void HandleResultsDataAdding(object sender, DataAddingEventArgs dataAddingArgs)
{
if (_debugCollection.IsOpen)
{
PSStreamObject streamObject = dataAddingArgs.ItemAdded as PSStreamObject;
if (streamObject != null)
{
try
{
_debugCollection.Add(streamObject);
}
catch (PSInvalidOperationException) { }
}
}
}
private void HandleDebuggerNestedDebuggingCancelledEvent(object sender, EventArgs e)
{
StopProcessing();
}
private void AddEventHandlers()
{
_job.StateChanged += HandleJobStateChangedEvent;
_debugger.NestedDebuggingCancelledEvent += HandleDebuggerNestedDebuggingCancelledEvent;
if (_job.ChildJobs.Count == 0)
{
// No child jobs, monitor this job's results collection.
_job.Results.DataAdding += HandleResultsDataAdding;
}
else
{
// Monitor each child job's results collections.
foreach (var childJob in _job.ChildJobs)
{
childJob.Results.DataAdding += HandleResultsDataAdding;
}
}
}
private void RemoveEventHandlers()
{
_job.StateChanged -= HandleJobStateChangedEvent;
_debugger.NestedDebuggingCancelledEvent -= HandleDebuggerNestedDebuggingCancelledEvent;
if (_job.ChildJobs.Count == 0)
{
// Remove single job DataAdding event handler.
_job.Results.DataAdding -= HandleResultsDataAdding;
}
else
{
// Remove each child job's DataAdding event handler.
foreach (var childJob in _job.ChildJobs)
{
childJob.Results.DataAdding -= HandleResultsDataAdding;
}
}
}
private Job GetJobByName(string name)
{
// Search jobs in job repository.
List<Job> jobs1 = new List<Job>();
WildcardPattern pattern =
WildcardPattern.Get(name, WildcardOptions.IgnoreCase | WildcardOptions.Compiled);
foreach (Job job in JobRepository.Jobs)
{
if (pattern.IsMatch(job.Name))
{
jobs1.Add(job);
}
}
// Search jobs in job manager.
List<Job2> jobs2 = JobManager.GetJobsByName(name, this, false, false, false, null);
int jobCount = jobs1.Count + jobs2.Count;
if (jobCount == 1)
{
return (jobs1.Count > 0) ? jobs1[0] : jobs2[0];
}
if (jobCount > 1)
{
ThrowTerminatingError(
new ErrorRecord(
new PSInvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.FoundMultipleJobsWithName, name)),
"DebugJobFoundMultipleJobsWithName",
ErrorCategory.InvalidOperation,
this)
);
return null;
}
ThrowTerminatingError(
new ErrorRecord(
new PSInvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.CannotFindJobWithName, name)),
"DebugJobCannotFindJobWithName",
ErrorCategory.InvalidOperation,
this)
);
return null;
}
private Job GetJobById(int id)
{
// Search jobs in job repository.
List<Job> jobs1 = new List<Job>();
foreach (Job job in JobRepository.Jobs)
{
if (job.Id == id)
{
jobs1.Add(job);
}
}
// Search jobs in job manager.
Job job2 = JobManager.GetJobById(id, this, false, false, false);
if ((jobs1.Count == 0) && (job2 == null))
{
ThrowTerminatingError(
new ErrorRecord(
new PSInvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.CannotFindJobWithId, id)),
"DebugJobCannotFindJobWithId",
ErrorCategory.InvalidOperation,
this)
);
return null;
}
if ((jobs1.Count > 1) ||
(jobs1.Count == 1) && (job2 != null))
{
ThrowTerminatingError(
new ErrorRecord(
new PSInvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.FoundMultipleJobsWithId, id)),
"DebugJobFoundMultipleJobsWithId",
ErrorCategory.InvalidOperation,
this)
);
return null;
}
return (jobs1.Count > 0) ? jobs1[0] : job2;
}
private Job GetJobByInstanceId(Guid instanceId)
{
// Search jobs in job repository.
foreach (Job job in JobRepository.Jobs)
{
if (job.InstanceId == instanceId)
{
return job;
}
}
// Search jobs in job manager.
Job2 job2 = JobManager.GetJobByInstanceId(instanceId, this, false, false, false);
if (job2 != null)
{
return job2;
}
ThrowTerminatingError(
new ErrorRecord(
new PSInvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.CannotFindJobWithInstanceId, instanceId)),
"DebugJobCannotFindJobWithInstanceId",
ErrorCategory.InvalidOperation,
this)
);
return null;
}
#endregion
}
}
|