private BidirectionalGraph <Point, Edge <Point> > BuildGraph(HashSet <Point> map) { var location = map.First(); var graph = new QuikGraph.BidirectionalGraph <Point, Edge <Point> >(); foreach (var point in map) { graph.AddVertex(point); } foreach (var point in map) { foreach (var dir in Enum.GetValues(typeof(Direction)).Cast <Direction>()) { var target = point.GetPoint(dir); if (map.Contains(target)) { graph.AddEdge(new Edge <Point>(point, target)); } } } return(graph); }
private void AddThreadItems(ItemNode?parentProcess, TraceRecord trace) { string nodeName = $"PROCNODE_{trace.PID}_{trace.LaunchedTime}"; ItemNode?startProcess = null; lock (_lock) { if (!addedNodes.TryGetValue(nodeName, out startProcess)) { startProcess = new ItemNode(nodeName, Logging.eTimelineEvent.ProcessStart, trace); sbgraph.AddVertex(startProcess); addedNodes[nodeName] = startProcess; if (parentProcess != null) { sbgraph.AddEdge(new Edge <ItemNode>(parentProcess, startProcess)); } } var threads = trace.ProtoGraphs; foreach (var thread in threads) { string threadName = $"THREADNODE_{thread.TraceData.randID}_{thread.ThreadID}"; if (!addedNodes.ContainsKey(threadName)) { ItemNode threadNode = new ItemNode(threadName, Logging.eTimelineEvent.ThreadStart, thread); sbgraph.AddVertex(threadNode); sbgraph.AddEdge(new Edge <ItemNode>(startProcess, threadNode)); addedNodes[threadName] = threadNode; } } var timelineEntries = trace.GetTimeLineEntries(); foreach (Logging.TIMELINE_EVENT timelineEvent in timelineEntries) { if (timelineEvent.TimelineEventType == Logging.eTimelineEvent.APICall) { var call = (Logging.APICALL)(timelineEvent.Item); if (call.APIDetails != null) { APIDetailsWin.API_ENTRY apiinfo = call.APIDetails.Value; if (apiinfo.Effects != null) { ProtoGraph?caller = call.Graph; if (caller is null || call.Node is null || call.Index >= call.Node.callRecordsIndexs.Count) { Logging.RecordLogEvent($"Warning: Call {call.APIDetails.Value.ModuleName}:{call.APIDetails.Value.Symbol} tried to place call {call.Index} on timeline, but only {call.Node?.callRecordsIndexs.Count} recorded"); continue; } int recordsIndex = (int)call.Node.callRecordsIndexs[call.Index]; if (recordsIndex >= caller.SymbolCallRecords.Count) { Logging.RecordLogEvent($"Warning: Call {call.APIDetails.Value.ModuleName}:{call.APIDetails.Value.Symbol} tried to place record {recordsIndex} on timeline, but caller has only {caller.SymbolCallRecords} recorded"); continue; } APICALLDATA APICallRecord = caller.SymbolCallRecords[recordsIndex]; string threadName = $"THREADNODE_{caller.TraceData.randID}_{caller.ThreadID}"; ItemNode threadNode = addedNodes[threadName]; foreach (APIDetailsWin.InteractionEffect effectBase in apiinfo.Effects) { switch (effectBase) { case APIDetailsWin.LinkReferenceEffect linkEffect: { APIDetailsWin.API_PARAM_ENTRY entityParamRecord = apiinfo.LoggedParams[linkEffect.EntityIndex]; APIDetailsWin.API_PARAM_ENTRY referenceParamRecord = apiinfo.LoggedParams[linkEffect.ReferenceIndex]; int entityParamLoggedIndex = APICallRecord.argList.FindIndex(x => x.Item1 == entityParamRecord.Index); int referenceParamLoggedIndex = APICallRecord.argList.FindIndex(x => x.Item1 == referenceParamRecord.Index); if (entityParamLoggedIndex == -1 || referenceParamLoggedIndex == -1) { string error = $"API call record for {apiinfo.ModuleName}:{apiinfo.Symbol} [LinkReference] didn't have correct parameters. The instrumentation library or apidata file may not match."; timelineEvent.MetaError = error; Logging.RecordLogEvent(error, Logging.LogFilterType.Debug); break; } if (!_interactionEntities.TryGetValue(entityParamRecord.EntityType, out Dictionary <string, ItemNode>?entityDict)) { entityDict = new Dictionary <string, ItemNode>(); _interactionEntities.Add(entityParamRecord.EntityType, entityDict); } string entityString = APICallRecord.argList[entityParamLoggedIndex].Item2; ItemNode entityNode; if (!entityDict.ContainsKey(entityString)) { entityNode = new ItemNode(entityString, Logging.eTimelineEvent.APICall, timelineEvent); entityDict.Add(entityString, entityNode); sbgraph.AddVertex(entityNode); addedNodes[entityString] = entityNode; } else { entityNode = addedNodes[entityString]; } AddAPIEdge(threadNode, entityNode, apiinfo.Label); if (!_timelineEventEntities.TryGetValue(timelineEvent, out ItemNode? existingEntity)) { _timelineEventEntities.Add(timelineEvent, entityNode); } Debug.Assert(existingEntity == null || existingEntity == entityNode); //link this entity to the reference the api all created (eg associate a file path with the file handle that 'CreateFile' created) string referenceString = APICallRecord.argList[referenceParamLoggedIndex].Item2; if (referenceParamRecord.NoCase) { referenceString = referenceString.ToLower(); } if (!_interactionEntityReferences.ContainsKey(referenceParamRecord.RawType)) { _interactionEntityReferences.Add(referenceParamRecord.RawType, new Dictionary <string, ItemNode>()); } if (!_interactionEntityReferences[referenceParamRecord.RawType].ContainsKey(referenceString)) { _interactionEntityReferences[referenceParamRecord.RawType].Add(referenceString, entityNode); } break; } // this api performs some action on a refererence to an entity // record this as a label on the edge and link this event to the entity case APIDetailsWin.UseReferenceEffect useEffect: { APIDetailsWin.API_PARAM_ENTRY referenceParamRecord = apiinfo.LoggedParams[useEffect.ReferenceIndex]; int referenceParamLoggedIndex = APICallRecord.argList.FindIndex(x => x.Item1 == referenceParamRecord.Index); if (referenceParamLoggedIndex == -1) { timelineEvent.MetaError = $"API call record for {apiinfo.ModuleName}:{apiinfo.Symbol} [UseReference] didn't have correct parameters"; Logging.RecordLogEvent(timelineEvent.MetaError, Logging.LogFilterType.Debug); break; } string referenceString = APICallRecord.argList[referenceParamLoggedIndex].Item2; if (referenceParamRecord.NoCase) { referenceString = referenceString.ToLower(); } bool resolvedReference = false; if (_interactionEntityReferences.TryGetValue(referenceParamRecord.RawType, out Dictionary <string, ItemNode>?typeEntityList)) { if (typeEntityList.TryGetValue(referenceString, out ItemNode? entityNode)) { resolvedReference = true; if (!_timelineEventEntities.ContainsKey(timelineEvent)) { _timelineEventEntities.Add(timelineEvent, entityNode); } AddAPIEdge(threadNode, entityNode, apiinfo.Label); } } if (!resolvedReference) { timelineEvent.MetaError = $"API call record for {apiinfo.ModuleName}:{apiinfo.Symbol} [UseReference] reference was not linked to an entity ({referenceString})"; Logging.RecordLogEvent(timelineEvent.MetaError, Logging.LogFilterType.Debug); } break; } // this api invalidates a reference // remove the link between the reference and the entity it references case APIDetailsWin.DestroyReferenceEffect destroyEffect: { APIDetailsWin.API_PARAM_ENTRY referenceParamRecord = apiinfo.LoggedParams[destroyEffect.ReferenceIndex]; int referenceParamLoggedIndex = APICallRecord.argList.FindIndex(x => x.Item1 == referenceParamRecord.Index); if (referenceParamLoggedIndex == -1) { timelineEvent.MetaError = $"API call record for {apiinfo.ModuleName}:{apiinfo.Symbol} [DestroyReference] didn't have correct parameters"; Logging.RecordLogEvent(timelineEvent.MetaError, Logging.LogFilterType.Debug); break; } string referenceString = APICallRecord.argList[referenceParamLoggedIndex].Item2; if (referenceParamRecord.NoCase) { referenceString = referenceString.ToLower(); } bool resolvedReference = false; if (_interactionEntityReferences.TryGetValue(referenceParamRecord.RawType, out Dictionary <string, ItemNode>?typeEntityList)) { if (typeEntityList.TryGetValue(referenceString, out ItemNode? entityNode)) { resolvedReference = true; if (!_timelineEventEntities.ContainsKey(timelineEvent)) { _timelineEventEntities.Add(timelineEvent, entityNode); } AddAPIEdge(threadNode, entityNode, apiinfo.Label); typeEntityList.Remove(referenceString); } } if (!resolvedReference) { timelineEvent.MetaError = $"API call record for {apiinfo.ModuleName}:{apiinfo.Symbol} [DestroyReference] reference was not linked to an entity ({referenceString})"; Logging.RecordLogEvent(timelineEvent.MetaError, Logging.LogFilterType.Debug); } break; } default: timelineEvent.MetaError = $"API call record for {apiinfo.ModuleName}:{apiinfo.Symbol}: had invalid effect {effectBase}"; Logging.RecordLogEvent(timelineEvent.MetaError, Logging.LogFilterType.Debug); break; } } } } } } if (startProcess is not null) { foreach (var child in trace.GetChildren()) { string childName = $"PROCNODE_{child.PID}_{child.LaunchedTime}"; if (!addedNodes.TryGetValue(childName, out ItemNode? childProcess) || childProcess is null) { childProcess = new ItemNode(childName, Logging.eTimelineEvent.ProcessStart, child); sbgraph.AddVertex(childProcess); addedNodes[childName] = childProcess; sbgraph.AddEdge(new Edge <ItemNode>(startProcess, childProcess)); } AddThreadItems(parentProcess, child); } } } }