-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Allow emitting line number information into native AOT apps #122227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
MichalStrehovsky
wants to merge
4
commits into
dotnet:main
Choose a base branch
from
MichalStrehovsky:fix68714
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...eclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/StackTraceDocumentsNode.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Text; | ||
| using Internal.Text; | ||
|
|
||
| namespace ILCompiler.DependencyAnalysis | ||
| { | ||
| /// <summary> | ||
| /// Contains information about source files in this compilation. | ||
| /// </summary> | ||
| public sealed class StackTraceDocumentsNode : ObjectNode, ISymbolDefinitionNode, INodeWithSize | ||
| { | ||
| private Dictionary<string, int> _documentToIndex = new Dictionary<string, int>(StringComparer.Ordinal); | ||
| private List<string> _documents = new List<string>(); | ||
| private int? _size; | ||
|
|
||
| public void AppendMangledName(NameMangler nameMangler, Utf8StringBuilder sb) | ||
| { | ||
| sb.Append(nameMangler.CompilationUnitPrefix).Append("__stacktrace_documents"u8); | ||
| } | ||
|
|
||
| int INodeWithSize.Size => _size.Value; | ||
| public int Offset => 0; | ||
| public override bool IsShareable => false; | ||
|
|
||
| public override ObjectNodeSection GetSection(NodeFactory factory) | ||
| { | ||
| if (factory.Target.IsWindows || factory.Target.SupportsRelativePointers) | ||
| return ObjectNodeSection.ReadOnlyDataSection; | ||
| else | ||
| return ObjectNodeSection.DataSection; | ||
| } | ||
|
|
||
| public int GetDocumentId(string documentName) | ||
| { | ||
| if (!_documentToIndex.TryGetValue(documentName, out int index)) | ||
| { | ||
| index = _documents.Count; | ||
| _documents.Add(documentName); | ||
| _documentToIndex.Add(documentName, index); | ||
| } | ||
|
|
||
| return index; | ||
| } | ||
|
|
||
| public override bool StaticDependenciesAreComputed => true; | ||
| protected override string GetName(NodeFactory factory) => this.GetMangledName(factory.NameMangler); | ||
|
|
||
| public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false) | ||
| { | ||
| // This node does not trigger generation of other nodes. | ||
| if (relocsOnly) | ||
| return new ObjectData(Array.Empty<byte>(), Array.Empty<Relocation>(), 1, new ISymbolDefinitionNode[] { this }); | ||
|
|
||
| // Zero out the hashtable so that we crash if someone tries to use this after emission | ||
| _documentToIndex = null; | ||
|
|
||
| var ms = new MemoryStream(); | ||
| var bw = new BinaryWriter(ms); | ||
|
|
||
| // We write out: | ||
| // (Int32) Number of documents | ||
| // (Int32) Offset of document1 from beginning of blob | ||
| // (Int32) Offset of document2 from beginning of blob | ||
| // ... | ||
| // (Int32) Offset of documentN from beginning of blob | ||
| // Null-terminated UTF-8 bytes of document1 | ||
| // Null-terminated UTF-8 bytes of document2 | ||
| // ... | ||
| // Null-terminated UTF-8 bytes of documentN | ||
|
|
||
| bw.Write(_documents.Count); | ||
|
|
||
| int position = sizeof(int) /* count of documents */ + _documents.Count * sizeof(int); | ||
| for (int i = 0; i < _documents.Count; i++) | ||
| { | ||
| bw.Write(position); | ||
| position += Encoding.UTF8.GetByteCount(_documents[i]) + 1; | ||
| } | ||
|
|
||
| for (int i = 0; i < _documents.Count; i++) | ||
| { | ||
| bw.Write(Encoding.UTF8.GetBytes(_documents[i])); | ||
| bw.Write((byte)0); | ||
| } | ||
|
|
||
| _size = checked((int)ms.Length); | ||
|
|
||
| return new ObjectData(ms.ToArray(), Array.Empty<Relocation>(), 1, new ISymbolDefinitionNode[] { this }); | ||
| } | ||
|
|
||
| protected internal override int Phase => (int)ObjectNodePhase.Ordered; | ||
| public override int ClassCode => (int)ObjectNodeOrder.StackTraceDocumentsNode; | ||
| } | ||
| } |
140 changes: 140 additions & 0 deletions
140
...lr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/StackTraceLineNumbersNode.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,140 @@ | ||||||
| // Licensed to the .NET Foundation under one or more agreements. | ||||||
| // The .NET Foundation licenses this file to you under the MIT license. | ||||||
|
|
||||||
| using System; | ||||||
| using System.IO; | ||||||
|
|
||||||
| using Internal.Text; | ||||||
| using Internal.TypeSystem; | ||||||
| using Internal.NativeFormat; | ||||||
| using Internal; | ||||||
|
|
||||||
| using Debug = System.Diagnostics.Debug; | ||||||
|
|
||||||
| namespace ILCompiler.DependencyAnalysis | ||||||
| { | ||||||
| /// <summary> | ||||||
| /// Contains information about mapping native code offsets to line numbers. | ||||||
| /// </summary> | ||||||
| public sealed class StackTraceLineNumbersNode : ObjectNode, ISymbolDefinitionNode, INodeWithSize | ||||||
| { | ||||||
| private int? _size; | ||||||
| private readonly ExternalReferencesTableNode _externalReferences; | ||||||
| private readonly StackTraceDocumentsNode _documents; | ||||||
|
|
||||||
| public StackTraceLineNumbersNode(ExternalReferencesTableNode externalReferences, StackTraceDocumentsNode documents) | ||||||
| { | ||||||
| _externalReferences = externalReferences; | ||||||
| _documents = documents; | ||||||
| } | ||||||
|
|
||||||
| public void AppendMangledName(NameMangler nameMangler, Utf8StringBuilder sb) | ||||||
| { | ||||||
| sb.Append(nameMangler.CompilationUnitPrefix).Append("__stacktrace_line_numbers"u8); | ||||||
| } | ||||||
|
|
||||||
| int INodeWithSize.Size => _size.Value; | ||||||
| public int Offset => 0; | ||||||
| public override bool IsShareable => false; | ||||||
| public override ObjectNodeSection GetSection(NodeFactory factory) => _externalReferences.GetSection(factory); | ||||||
| public override bool StaticDependenciesAreComputed => true; | ||||||
| protected override string GetName(NodeFactory factory) => this.GetMangledName(factory.NameMangler); | ||||||
|
|
||||||
| public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false) | ||||||
| { | ||||||
| // This node does not trigger generation of other nodes. | ||||||
| if (relocsOnly) | ||||||
| return new ObjectData(Array.Empty<byte>(), Array.Empty<Relocation>(), 1, new ISymbolDefinitionNode[] { this }); | ||||||
|
|
||||||
| NativeWriter nativeWriter = new NativeWriter(); | ||||||
| VertexHashtable hashtable = new VertexHashtable(); | ||||||
| Section nativeSection = nativeWriter.NewSection(); | ||||||
| nativeSection.Place(hashtable); | ||||||
|
|
||||||
| foreach (StackTraceMapping mapping in factory.MetadataManager.GetStackTraceMapping(factory)) | ||||||
| { | ||||||
| var entrypointSymbol = factory.MethodEntrypoint(mapping.Method); | ||||||
| if (entrypointSymbol is not INodeWithDebugInfo debugInfo) | ||||||
| continue; | ||||||
|
|
||||||
| BlobVertex blob = CreateLineNumbersBlob(_documents, debugInfo); | ||||||
| if (blob == null) | ||||||
| continue; | ||||||
|
|
||||||
| Vertex methodPointer = nativeWriter.GetUnsignedConstant(_externalReferences.GetIndex(entrypointSymbol)); | ||||||
| var hashtableEntry = nativeWriter.GetTuple(methodPointer, blob); | ||||||
|
|
||||||
| uint hashcode = VersionResilientHashCode.CombineThreeValuesIntoHash((uint)mapping.OwningTypeHandle, (uint)mapping.MethodNameHandle, (uint)mapping.MethodSignatureHandle); | ||||||
| hashtable.Append(hashcode, nativeSection.Place(hashtableEntry)); | ||||||
| } | ||||||
|
|
||||||
| foreach (ReflectionStackTraceMapping mapping in factory.MetadataManager.GetReflectionStackTraceMappings(factory)) | ||||||
| { | ||||||
| var entrypointSymbol = factory.MethodEntrypoint(mapping.Method); | ||||||
| if (entrypointSymbol is not INodeWithDebugInfo debugInfo) | ||||||
| continue; | ||||||
|
|
||||||
| BlobVertex blob = CreateLineNumbersBlob(_documents, debugInfo); | ||||||
| if (blob == null) | ||||||
| continue; | ||||||
|
|
||||||
| Vertex methodPointer = nativeWriter.GetUnsignedConstant(_externalReferences.GetIndex(entrypointSymbol)); | ||||||
| var hashtableEntry = nativeWriter.GetTuple(methodPointer, blob); | ||||||
|
|
||||||
| uint hashcode = VersionResilientHashCode.CombineTwoValuesIntoHash((uint)mapping.OwningTypeHandle, (uint)mapping.MethodHandle); | ||||||
| hashtable.Append(hashcode, nativeSection.Place(hashtableEntry)); | ||||||
| } | ||||||
|
|
||||||
| static BlobVertex CreateLineNumbersBlob(StackTraceDocumentsNode documents, INodeWithDebugInfo debugInfoNode) | ||||||
| { | ||||||
| var ms = new MemoryStream(); | ||||||
| var bw = new BinaryWriter(ms); | ||||||
|
|
||||||
| int currentNativeOffset = 0; | ||||||
| int currentLineNumber = 0; | ||||||
| string currentDocument = null; | ||||||
| foreach (NativeSequencePoint sequencePoint in debugInfoNode.GetNativeSequencePoints()) | ||||||
| { | ||||||
| if (currentLineNumber == sequencePoint.LineNumber && currentDocument == sequencePoint.FileName) | ||||||
| continue; | ||||||
|
|
||||||
| // Make sure a zero native offset delta is not possible because we use it below | ||||||
| // to indicate an update to the current document. | ||||||
| if (currentDocument != null && currentNativeOffset == sequencePoint.NativeOffset) | ||||||
| continue; | ||||||
|
|
||||||
| if (currentDocument != sequencePoint.FileName) | ||||||
| { | ||||||
| // We start with currentDocument == null, so the reader knows the first byte of the output | ||||||
| // is a document number. Otherwise we use NativeOffsetDelta == 0 as a marker that the next | ||||||
| // byte is a document number and not a native offset delta. | ||||||
| if (currentDocument != null) | ||||||
| bw.Write7BitEncodedInt((byte)0); | ||||||
|
||||||
| bw.Write7BitEncodedInt((byte)0); | |
| bw.Write7BitEncodedInt(0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing flag check: The loop should verify that
(mapping.Flags & StackTraceRecordFlags.HasLineNumbers) != 0before attempting to generate line numbers. Currently, it will attempt to generate line numbers for all methods in the stack trace mapping, regardless of whether theHasLineNumbersflag is set. This could result in generating line numbers for methods where line number emission was not intended according to the policy.