The new Tooling API has the ApexExecutionOverlayAction
object.
Specifies an Apex code snippet or SOQL query to execute at a specific
line of code in an Apex class or trigger and optionally generate a
heap dump.
You could use this to capture the heap dump for the required user.
Specifically, try setting:
ActionScriptType
to 'None'
ExecutableEntityId
to the Apex class Id
ExpirationDate
to some time in the future. Maybe an hour out
IsDumpingHeap
to true
Iteration
to 1
- The
Line
to "The line number of the heap dump marker"
- the
ScopeId
to the required UserId
I hacked something together in a .NET to try it out.
SforceService toolingService = // Get an instance of the ToolingService
string apexClassId = "01p70000CLASSID";
// This is the session owners UserId
//string heapUserId = "005700000000FOO";
// This is an Id of another user in the Org
string heapUserId = "005700000000BAR";
int heapDumpLine = 27;
ApexExecutionOverlayAction apexExecutionOverlayAction = new SalesforceTooling.ApexExecutionOverlayAction();
apexExecutionOverlayAction.ActionScriptType = "None";
apexExecutionOverlayAction.ExecutableEntityId = apexClassId;
apexExecutionOverlayAction.IsDumpingHeap = true;
apexExecutionOverlayAction.IsDumpingHeapSpecified = true;
apexExecutionOverlayAction.Iteration = 1;
apexExecutionOverlayAction.IterationSpecified = true;
apexExecutionOverlayAction.Line = heapDumpLine;
apexExecutionOverlayAction.LineSpecified = true;
apexExecutionOverlayAction.ScopeId = heapUserId;
SaveResult[] results = toolingService.create(new sObject[] { apexExecutionOverlayAction });
SaveResult triggerSaveResult = results[0];
if (!triggerSaveResult.success)
{
Assert.Fail(triggerSaveResult.errors[0].message);
}
Assert.IsTrue(triggerSaveResult.success);
Note that this didn't make the Heap Dump Location appear in the developer console for the session owner. I logged in as the target user, but it appears I've broken the developer console for that user:
However, after hitting the dump point you can pull the resulting dump out from the ToolingService using the ApexLog. E.g.
QueryResult apexdumpLogResult = toolingService.query("Select Id, Application, DurationMilliseconds, Location, LogUserId, Operation from ApexLog where Location = 'HeapDump'");