private void StoreFormattedInstruction(ulong instructionKey, TracePrefixFile.ImageFileInfo imageFileInfo, uint instructionAddress) { // Instruction already known? if (_formattedInstructions.ContainsKey(instructionKey)) { return; } // Store formatted instruction _formattedInstructions.TryAdd(instructionKey, _mapFileCollection.FormatAddress(imageFileInfo, instructionAddress)); }
/// <summary> /// Returns the MAP file object matching the given image ID and name. Caches the resolved ID in the map file ID lookup. /// </summary> /// <param name="imageFile">Image data.</param> /// <returns></returns> private MapFile ResolveMapFile(TracePrefixFile.ImageFileInfo imageFile) { // Map file known? if (_mapFileIdLookup.TryGetValue(imageFile.Id, out var mapFile)) { return(mapFile); } // Find MAP file with matching image name mapFile = _mapFiles.FirstOrDefault(m => string.Compare(imageFile.Name, m.ImageName, true, CultureInfo.InvariantCulture) == 0); _mapFileIdLookup.Add(imageFile.Id, mapFile); return(mapFile); }
/// <summary> /// Formats the given address data. /// </summary> /// <param name="imageFile">Image file.</param> /// <param name="address">Image relative address.</param> /// <returns></returns> public string FormatAddress(TracePrefixFile.ImageFileInfo imageFile, uint address) { // Does a map file exist? Does it contain a suitable entry? var mapFile = ResolveMapFile(imageFile); var symbolData = mapFile?.GetSymbolDataByAddress(address); if (symbolData == null) { // Just format the image name and the image offset return($"{imageFile.Name}:{address:X}"); } // Format image name, symbol name and symbol offset return($"{imageFile.Name}:{symbolData.Value.Name}+{(address - symbolData.Value.StartAddress):X}"); }
public override async Task PreprocessTraceAsync(TraceEntity traceEntity) { // First test case? if (_firstTestcase) { // Paths string rawTraceFileDirectory = Path.GetDirectoryName(traceEntity.RawTraceFilePath); string prefixDataFilePath = Path.Combine(rawTraceFileDirectory !, "prefix_data.txt"); // Suppress "possible null" warning string tracePrefixFilePath = Path.Combine(rawTraceFileDirectory, "prefix.trace"); // Read image data string[] imageDataLines = await File.ReadAllLinesAsync(prefixDataFilePath); int nextImageFileId = 0; List <TracePrefixFile.ImageFileInfo> imageFiles = new List <TracePrefixFile.ImageFileInfo>(); foreach (string line in imageDataLines) { string[] imageData = line.Split('\t'); var imageFile = new TracePrefixFile.ImageFileInfo { Id = nextImageFileId++, Interesting = byte.Parse(imageData[1]) != 0, StartAddress = ulong.Parse(imageData[2], NumberStyles.HexNumber), EndAddress = ulong.Parse(imageData[3], NumberStyles.HexNumber), Name = Path.GetFileName(imageData[4]) }; imageFiles.Add(imageFile); } _imageFiles = imageFiles.OrderBy(img => img.StartAddress).ToArray(); // Prepare writer for serializing trace data Dictionary <int, Allocation> tracePrefixAllocations; using var tracePrefixFileStream = new MemoryStream(); using (var tracePrefixFileWriter = new BinaryWriter(tracePrefixFileStream, Encoding.ASCII, true)) { // Write image files tracePrefixFileWriter.Write(_imageFiles.Length); foreach (var imageFile in _imageFiles) { imageFile.Store(tracePrefixFileWriter); } // Load and parse trace prefix data PreprocessFile(tracePrefixFilePath, true, tracePrefixFileWriter, out tracePrefixAllocations); } // Create trace prefix object var preprocessedTracePrefixData = tracePrefixFileStream.GetBuffer().AsMemory(0, (int)tracePrefixFileStream.Length); _tracePrefix = new TracePrefixFile(preprocessedTracePrefixData, tracePrefixAllocations); _firstTestcase = false; // Keep raw trace data? if (!_keepRawTraces) { File.Delete(prefixDataFilePath); File.Delete(tracePrefixFilePath); } // Store to disk? if (_storeTraces) { string outputPath = Path.Combine(_outputDirectory.FullName, "prefix.trace.preprocessed"); await using var writer = new BinaryWriter(File.Open(outputPath, FileMode.Create, FileAccess.Write, FileShare.None)); writer.Write(preprocessedTracePrefixData.Span); } } // Wait for trace prefix to be fully created (might happen in different thread) while (_tracePrefix == null) { await Task.Delay(10); } // Preprocess trace data string rawTraceFilePath = traceEntity.RawTraceFilePath; Dictionary <int, Allocation> allocations; using var traceFileStream = new MemoryStream(); using (var traceFileWriter = new BinaryWriter(traceFileStream, Encoding.ASCII, true)) PreprocessFile(rawTraceFilePath, false, traceFileWriter, out allocations); // Create trace file object var preprocessedTraceData = traceFileStream.GetBuffer().AsMemory(0, (int)traceFileStream.Length); var preprocessedTraceFile = new TraceFile(_tracePrefix, preprocessedTraceData, allocations); // Keep raw trace? if (!_keepRawTraces) { File.Delete(traceEntity.RawTraceFilePath); traceEntity.RawTraceFilePath = null; } // Store to disk? if (_storeTraces) { traceEntity.PreprocessedTraceFilePath = Path.Combine(_outputDirectory.FullName, Path.GetFileName(rawTraceFilePath) + ".preprocessed"); await using var writer = new BinaryWriter(File.Open(traceEntity.PreprocessedTraceFilePath, FileMode.Create, FileAccess.Write, FileShare.None)); writer.Write(preprocessedTraceData.Span); } // Keep trace data in memory for the analysis stages traceEntity.PreprocessedTraceFile = preprocessedTraceFile; }
public override async Task PreprocessTraceAsync(TraceEntity traceEntity) { // First test case? if (_firstTestcase) { // Read image data string prefixDataFilePath = Path.Combine(Path.GetDirectoryName(traceEntity.RawTraceFilePath), "prefix_data.txt"); string[] imageDataLines = await File.ReadAllLinesAsync(prefixDataFilePath); int nextImageFileId = 0; foreach (string line in imageDataLines) { string[] imageData = line.Split('\t'); var imageFile = new TracePrefixFile.ImageFileInfo { Id = nextImageFileId++, Interesting = byte.Parse(imageData[1]) != 0, StartAddress = ulong.Parse(imageData[2], NumberStyles.HexNumber), EndAddress = ulong.Parse(imageData[3], NumberStyles.HexNumber), Name = Path.GetFileName(imageData[4]) }; _imageFiles.Add(imageFile.StartAddress, imageFile); } // Handle trace prefix file string tracePrefixFilePath = Path.Combine(Path.GetDirectoryName(traceEntity.RawTraceFilePath), "prefix.trace"); _tracePrefix = (TracePrefixFile)PreprocessFile(tracePrefixFilePath, true); _firstTestcase = false; // Keep raw trace data? if (!_keepRawTraces) { File.Delete(prefixDataFilePath); File.Delete(tracePrefixFilePath); } // Store to disk? if (_storeTraces) { string outputPath = Path.Combine(_outputDirectory.FullName, "prefix.trace.preprocessed"); using (var writer = new BinaryWriter(File.Open(outputPath, FileMode.Create, FileAccess.Write, FileShare.None))) _tracePrefix.Save(writer); } } // Wait for trace prefix to be fully created (might happen in different thread) while (_tracePrefix == null) { await Task.Delay(10); } // Preprocess trace string rawTraceFilePath = traceEntity.RawTraceFilePath; var preprocessedTraceFile = PreprocessFile(rawTraceFilePath, false); // Keep raw trace? if (!_keepRawTraces) { File.Delete(traceEntity.RawTraceFilePath); traceEntity.RawTraceFilePath = null; } // Store to disk? if (_storeTraces) { traceEntity.PreprocessedTraceFilePath = Path.Combine(_outputDirectory.FullName, Path.GetFileName(rawTraceFilePath) + ".preprocessed"); using (var writer = new BinaryWriter(File.Open(traceEntity.PreprocessedTraceFilePath, FileMode.Create, FileAccess.Write, FileShare.None))) preprocessedTraceFile.Save(writer); } // Keep trace data in memory for the analysis stages traceEntity.PreprocessedTraceFile = preprocessedTraceFile; }