<div dir="ltr">Hi, <div><br></div><div>I think this is causing a failure on the asan bot. </div><div>Please fix or revert ASAP</div><div><br></div><div><br></div><div><a href="http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-fast/builds/33102/steps/check-llvm%20asan/logs/stdio">http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-fast/builds/33102/steps/check-llvm%20asan/logs/stdio</a><br></div><div><pre style="font-family:"Courier New",courier,monotype,monospace;color:rgb(0,0,0);font-size:medium"><span class="gmail-stdout">Note: Google Test filter = GSYMTest.TestRanges
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from GSYMTest
[ RUN      ] GSYMTest.TestRanges
=================================================================
==19204==ERROR: AddressSanitizer: container-overflow on address 0x606000000390 at pc 0x00000052cde7 bp 0x7ffdbaad4740 sp 0x7ffdbaad4738
READ of size 8 at 0x606000000390 thread T0
    #0 0x52cde6 in endAddress /b/sanitizer-x86_64-linux-fast/build/llvm/include/llvm/DebugInfo/GSYM/Range.h:48:40
    #1 0x52cde6 in intersects /b/sanitizer-x86_64-linux-fast/build/llvm/include/llvm/DebugInfo/GSYM/Range.h:58
    #2 0x52cde6 in intersect /b/sanitizer-x86_64-linux-fast/build/llvm/include/llvm/DebugInfo/GSYM/Range.h:61
    #3 0x52cde6 in llvm::gsym::AddressRanges::insert(llvm::gsym::AddressRange const&) /b/sanitizer-x86_64-linux-fast/build/llvm/lib/DebugInfo/GSYM/Range.cpp:35
    #4 0x511b8d in GSYMTest_TestRanges_Test::TestBody() /b/sanitizer-x86_64-linux-fast/build/llvm/unittests/DebugInfo/GSYM/GSYMTest.cpp:350:10</span></pre></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jun 26, 2019 at 7:09 AM Greg Clayton via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Author: gclayton<br>
Date: Wed Jun 26 07:09:09 2019<br>
New Revision: 364427<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=364427&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=364427&view=rev</a><br>
Log:<br>
Add GSYM utility files along with unit tests.<br>
<br>
The full GSYM patch started with: <a href="https://reviews.llvm.org/D53379" rel="noreferrer" target="_blank">https://reviews.llvm.org/D53379</a><br>
<br>
In that patch we wanted to split up getting GSYM into the LLVM code base so we are not committing too much code at once.<br>
<br>
This is a first in a series of patches where I only add the foundation classes along with complete unit tests. They provide the foundation for encoding and decoding a GSYM file.<br>
<br>
File entries are defined in llvm::gsym::FileEntry. This class splits the file up into a directory and filename represented by uniqued string table offsets. This allows all files that are referred to in a GSYM file to be encoded as 1 based indexes into a global file table in the GSYM file.<br>
<br>
Function information in stored in llvm::gsym::FunctionInfo. This object represents a contiguous address range that has a name and range with an optional line table and inline call stack information.<br>
<br>
Line table entries are defined in llvm::gsym::LineEntry. They store only address, file and line information to keep the line tables simple and allows the information to be efficiently encoded in a subsequent patch.<br>
<br>
Inline information is defined in llvm::gsym::InlineInfo. These structs store the name of the inline function, along with one or more address ranges, and the file and line that called this function. They also contain any child inline information.<br>
<br>
There are also utility classes for address ranges in llvm::gsym::AddressRange, and string table support in llvm::gsym::StringTable which are simple classes.<br>
<br>
The unit tests test all the APIs on these simple classes so they will be ready for the next patches where we will create GSYM files and parse GSYM files.<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D63104" rel="noreferrer" target="_blank">https://reviews.llvm.org/D63104</a><br>
<br>
<br>
<br>
Added:<br>
    llvm/trunk/include/llvm/DebugInfo/GSYM/<br>
    llvm/trunk/include/llvm/DebugInfo/GSYM/FileEntry.h<br>
    llvm/trunk/include/llvm/DebugInfo/GSYM/FunctionInfo.h<br>
    llvm/trunk/include/llvm/DebugInfo/GSYM/InlineInfo.h<br>
    llvm/trunk/include/llvm/DebugInfo/GSYM/LineEntry.h<br>
    llvm/trunk/include/llvm/DebugInfo/GSYM/Range.h<br>
    llvm/trunk/include/llvm/DebugInfo/GSYM/StringTable.h<br>
    llvm/trunk/lib/DebugInfo/GSYM/<br>
    llvm/trunk/lib/DebugInfo/GSYM/CMakeLists.txt<br>
    llvm/trunk/lib/DebugInfo/GSYM/FunctionInfo.cpp<br>
    llvm/trunk/lib/DebugInfo/GSYM/InlineInfo.cpp<br>
    llvm/trunk/lib/DebugInfo/GSYM/Range.cpp<br>
    llvm/trunk/unittests/DebugInfo/GSYM/<br>
    llvm/trunk/unittests/DebugInfo/GSYM/CMakeLists.txt<br>
    llvm/trunk/unittests/DebugInfo/GSYM/GSYMTest.cpp<br>
Modified:<br>
    llvm/trunk/lib/DebugInfo/CMakeLists.txt<br>
    llvm/trunk/unittests/DebugInfo/CMakeLists.txt<br>
<br>
Added: llvm/trunk/include/llvm/DebugInfo/GSYM/FileEntry.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/GSYM/FileEntry.h?rev=364427&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/GSYM/FileEntry.h?rev=364427&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/DebugInfo/GSYM/FileEntry.h (added)<br>
+++ llvm/trunk/include/llvm/DebugInfo/GSYM/FileEntry.h Wed Jun 26 07:09:09 2019<br>
@@ -0,0 +1,69 @@<br>
+//===- FileEntry.h ----------------------------------------------*- C++ -*-===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#ifndef LLVM_DEBUGINFO_GSYM_FILEENTRY_H<br>
+#define LLVM_DEBUGINFO_GSYM_FILEENTRY_H<br>
+<br>
+#include "llvm/ADT/DenseMapInfo.h"<br>
+#include "llvm/ADT/Hashing.h"<br>
+#include <functional><br>
+#include <stdint.h><br>
+#include <utility><br>
+<br>
+namespace llvm {<br>
+namespace gsym {<br>
+<br>
+/// Files in GSYM are contained in FileEntry structs where we split the <br>
+/// directory and basename into two different strings in the string <br>
+/// table. This allows paths to shared commont directory and filename<br>
+/// strings and saves space.<br>
+struct FileEntry {<br>
+<br>
+  /// Offsets in the string table.<br>
+  /// @{<br>
+  uint32_t Dir = 0;<br>
+  uint32_t Base = 0;<br>
+  /// @}<br>
+<br>
+  FileEntry() = default;<br>
+  FileEntry(uint32_t D, uint32_t B) : Dir(D), Base(B) {}<br>
+<br>
+  // Implement operator== so that FileEntry can be used as key in<br>
+  // unordered containers.<br>
+  bool operator==(const FileEntry &RHS) const {<br>
+    return Base == RHS.Base && Dir == RHS.Dir;<br>
+  };<br>
+  bool operator!=(const FileEntry &RHS) const {<br>
+    return Base != RHS.Base || Dir != RHS.Dir;<br>
+  };<br>
+};<br>
+<br>
+} // namespace gsym<br>
+<br>
+template <> struct DenseMapInfo<gsym::FileEntry> {<br>
+  static inline gsym::FileEntry getEmptyKey() {<br>
+    const auto key = DenseMapInfo<uint32_t>::getEmptyKey();<br>
+    return gsym::FileEntry(key, key);<br>
+    <br>
+  }<br>
+  static inline gsym::FileEntry getTombstoneKey() {<br>
+    const auto key = DenseMapInfo<uint32_t>::getTombstoneKey();<br>
+    return gsym::FileEntry(key, key);<br>
+  }<br>
+  static unsigned getHashValue(const gsym::FileEntry &Val) {<br>
+    return llvm::hash_combine(DenseMapInfo<uint32_t>::getHashValue(Val.Dir),<br>
+                              DenseMapInfo<uint32_t>::getHashValue(Val.Base));<br>
+  }<br>
+  static bool isEqual(const gsym::FileEntry &LHS, const gsym::FileEntry &RHS) {<br>
+    return LHS == RHS;<br>
+  }<br>
+};<br>
+<br>
+} // namespace llvm<br>
+#endif // #ifndef LLVM_DEBUGINFO_GSYM_FILEENTRY_H<br>
<br>
Added: llvm/trunk/include/llvm/DebugInfo/GSYM/FunctionInfo.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/GSYM/FunctionInfo.h?rev=364427&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/GSYM/FunctionInfo.h?rev=364427&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/DebugInfo/GSYM/FunctionInfo.h (added)<br>
+++ llvm/trunk/include/llvm/DebugInfo/GSYM/FunctionInfo.h Wed Jun 26 07:09:09 2019<br>
@@ -0,0 +1,107 @@<br>
+//===- FunctionInfo.h -------------------------------------------*- C++ -*-===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#ifndef LLVM_DEBUGINFO_GSYM_FUNCTIONINFO_H<br>
+#define LLVM_DEBUGINFO_GSYM_FUNCTIONINFO_H<br>
+<br>
+#include "llvm/DebugInfo/GSYM/InlineInfo.h"<br>
+#include "llvm/DebugInfo/GSYM/LineEntry.h"<br>
+#include "llvm/DebugInfo/GSYM/Range.h"<br>
+#include "llvm/DebugInfo/GSYM/StringTable.h"<br>
+#include <tuple><br>
+#include <vector><br>
+<br>
+namespace llvm {<br>
+class raw_ostream;<br>
+namespace gsym {<br>
+<br>
+/// Function information in GSYM files encodes information for one <br>
+/// contiguous address range. The name of the function is encoded as<br>
+/// a string table offset and allows multiple functions with the same<br>
+/// name to share the name string in the string table. Line tables are<br>
+/// stored in a sorted vector of gsym::LineEntry objects and are split<br>
+/// into line tables for each function. If a function has a discontiguous<br>
+/// range, it will be split into two gsym::FunctionInfo objects. If the<br>
+/// function has inline functions, the information will be encoded in<br>
+/// the "Inline" member, see gsym::InlineInfo for more information.<br>
+struct FunctionInfo {<br>
+  AddressRange Range;<br>
+  uint32_t Name; ///< String table offset in the string table.<br>
+  std::vector<gsym::LineEntry> Lines;<br>
+  InlineInfo Inline;<br>
+<br>
+  FunctionInfo(uint64_t Addr = 0, uint64_t Size = 0, uint32_t N = 0)<br>
+      : Range(Addr, Addr + Size), Name(N) {}<br>
+<br>
+  bool hasRichInfo() const {<br>
+    /// Returns whether we have something else than range and name. When<br>
+    /// converting information from a symbol table and from debug info, we <br>
+    /// might end up with multiple FunctionInfo objects for the same range<br>
+    /// and we need to be able to tell which one is the better object to use.<br>
+    return !Lines.empty() || Inline.isValid();<br>
+  }<br>
+<br>
+  bool isValid() const {<br>
+    /// Address and size can be zero and there can be no line entries for a<br>
+    /// symbol so the only indication this entry is valid is if the name is<br>
+    /// not zero. This can happen when extracting information from symbol <br>
+    /// tables that do not encode symbol sizes. In that case only the <br>
+    /// address and name will be filled in.<br>
+    return Name != 0;<br>
+  }<br>
+<br>
+  uint64_t startAddress() const { return Range.startAddress(); }<br>
+  uint64_t endAddress() const { return Range.endAddress(); }<br>
+  uint64_t size() const { return Range.size(); }<br>
+  void setStartAddress(uint64_t Addr) { Range.setStartAddress(Addr); }<br>
+  void setEndAddress(uint64_t Addr) { Range.setEndAddress(Addr); }<br>
+  void setSize(uint64_t Size) { Range.setSize(Size); }<br>
+<br>
+  void clear() {<br>
+    Range.clear();<br>
+    Name = 0;<br>
+    Lines.clear();<br>
+    Inline.clear();<br>
+  }<br>
+};<br>
+<br>
+inline bool operator==(const FunctionInfo &LHS, const FunctionInfo &RHS) {<br>
+  return LHS.Range == RHS.Range && LHS.Name == RHS.Name &&<br>
+         LHS.Lines == RHS.Lines && LHS.Inline == RHS.Inline;<br>
+}<br>
+inline bool operator!=(const FunctionInfo &LHS, const FunctionInfo &RHS) {<br>
+  return !(LHS == RHS);<br>
+}<br>
+/// This sorting will order things consistently by address range first, but then<br>
+/// followed by inlining being valid and line tables. We might end up with a<br>
+/// FunctionInfo from debug info that will have the same range as one from the<br>
+/// symbol table, but we want to quickly be able to sort and use the best version<br>
+/// when creating the final GSYM file. <br>
+inline bool operator<(const FunctionInfo &LHS, const FunctionInfo &RHS) {<br>
+  // First sort by address range<br>
+  if (LHS.Range != RHS.Range)<br>
+    return LHS.Range < RHS.Range;<br>
+<br>
+  // Then sort by inline<br>
+  if (LHS.Inline.isValid() != RHS.Inline.isValid())<br>
+    return RHS.Inline.isValid();<br>
+<br>
+  // If the number of lines is the same, then compare line table entries<br>
+  if (LHS.Lines.size() == RHS.Lines.size())<br>
+    return LHS.Lines < RHS.Lines;<br>
+  // Then sort by number of line table entries (more is better)<br>
+  return LHS.Lines.size() < RHS.Lines.size();<br>
+}<br>
+<br>
+raw_ostream &operator<<(raw_ostream &OS, const FunctionInfo &R);<br>
+<br>
+} // namespace gsym<br>
+} // namespace llvm<br>
+<br>
+#endif // #ifndef LLVM_DEBUGINFO_GSYM_FUNCTIONINFO_H<br>
<br>
Added: llvm/trunk/include/llvm/DebugInfo/GSYM/InlineInfo.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/GSYM/InlineInfo.h?rev=364427&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/GSYM/InlineInfo.h?rev=364427&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/DebugInfo/GSYM/InlineInfo.h (added)<br>
+++ llvm/trunk/include/llvm/DebugInfo/GSYM/InlineInfo.h Wed Jun 26 07:09:09 2019<br>
@@ -0,0 +1,77 @@<br>
+//===- InlineInfo.h ---------------------------------------------*- C++ -*-===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#ifndef LLVM_DEBUGINFO_GSYM_INLINEINFO_H<br>
+#define LLVM_DEBUGINFO_GSYM_INLINEINFO_H<br>
+<br>
+#include "llvm/ADT/Optional.h"<br>
+#include "llvm/DebugInfo/GSYM/Range.h"<br>
+#include <stdint.h><br>
+#include <vector><br>
+<br>
+<br>
+namespace llvm {<br>
+class raw_ostream;<br>
+<br>
+namespace gsym {<br>
+<br>
+/// Inline information stores the name of the inline function along with<br>
+/// an array of address ranges. It also stores the call file and call line<br>
+/// that called this inline function. This allows us to unwind inline call <br>
+/// stacks back to the inline or concrete function that called this <br>
+/// function. Inlined functions contained in this function are stored in the<br>
+/// "Children" variable. All address ranges must be sorted and all address<br>
+/// ranges of all children must be contained in the ranges of this function.<br>
+/// Any clients that encode information will need to ensure the ranges are<br>
+/// all contined correctly or lookups could fail. Add ranges in these objects<br>
+/// must be contained in the top level FunctionInfo address ranges as well.<br>
+struct InlineInfo {<br>
+<br>
+  uint32_t Name; ///< String table offset in the string table.<br>
+  uint32_t CallFile; ///< 1 based file index in the file table.<br>
+  uint32_t CallLine; ///< Source line number.<br>
+  AddressRanges Ranges;<br>
+  std::vector<InlineInfo> Children;<br>
+  InlineInfo() : Name(0), CallFile(0), CallLine(0) {}<br>
+  void clear() {<br>
+    Name = 0;<br>
+    CallFile = 0;<br>
+    CallLine = 0;<br>
+    Ranges.clear();<br>
+    Children.clear();<br>
+  }<br>
+  bool isValid() const { return !Ranges.empty(); }<br>
+  /// Lookup an address in the InlineInfo object<br>
+  ///<br>
+  /// This function is used to symbolicate an inline call stack and can <br>
+  /// turn one address in the program into one or more inline call stacks<br>
+  /// and have the stack trace show the original call site from <br>
+  /// non-inlined code.<br>
+  ///<br>
+  /// \param Addr the address to lookup<br>
+  /// \param InlineStack a vector of InlineInfo objects that describe the<br>
+  /// inline call stack for a given address.<br>
+  ///<br>
+  /// \returns true if successful, false otherwise<br>
+  typedef std::vector<const InlineInfo *> InlineArray;<br>
+  llvm::Optional<InlineArray> getInlineStack(uint64_t Addr) const;<br>
+};<br>
+<br>
+inline bool operator==(const InlineInfo &LHS, const InlineInfo &RHS) {<br>
+  return LHS.Name == RHS.Name && LHS.CallFile == RHS.CallFile &&<br>
+         LHS.CallLine == RHS.CallLine && LHS.Ranges == RHS.Ranges &&<br>
+         LHS.Children == RHS.Children;<br>
+}<br>
+<br>
+raw_ostream &operator<<(raw_ostream &OS, const InlineInfo &FI);<br>
+<br>
+} // namespace gsym<br>
+} // namespace llvm<br>
+<br>
+#endif // #ifndef LLVM_DEBUGINFO_GSYM_INLINEINFO_H<br>
<br>
Added: llvm/trunk/include/llvm/DebugInfo/GSYM/LineEntry.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/GSYM/LineEntry.h?rev=364427&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/GSYM/LineEntry.h?rev=364427&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/DebugInfo/GSYM/LineEntry.h (added)<br>
+++ llvm/trunk/include/llvm/DebugInfo/GSYM/LineEntry.h Wed Jun 26 07:09:09 2019<br>
@@ -0,0 +1,48 @@<br>
+//===- LineEntry.h ----------------------------------------------*- C++ -*-===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#ifndef LLVM_DEBUGINFO_GSYM_LINEENTRY_H<br>
+#define LLVM_DEBUGINFO_GSYM_LINEENTRY_H<br>
+<br>
+#include "llvm/DebugInfo/GSYM/Range.h"<br>
+<br>
+namespace llvm {<br>
+namespace gsym {<br>
+<br>
+/// Line entries are used to encode the line tables in FunctionInfo objects.<br>
+/// They are stored as a sorted vector of these objects and store the<br>
+/// address, file and line of the line table row for a given address. The<br>
+/// size of a line table entry is calculated by looking at the next entry<br>
+/// in the FunctionInfo's vector of entries.<br>
+struct LineEntry {<br>
+  uint64_t Addr; ///< Start address of this line entry.<br>
+  uint32_t File; ///< 1 based index of file in FileTable<br>
+  uint32_t Line; ///< Source line number.<br>
+  LineEntry(uint64_t A = 0, uint32_t F = 0, uint32_t L = 0)<br>
+      : Addr(A), File(F), Line(L) {}<br>
+  bool isValid() { return File != 0; }<br>
+};<br>
+<br>
+inline raw_ostream &operator<<(raw_ostream &OS, const LineEntry &LE) {<br>
+  return OS << "addr=" << HEX64(LE.Addr) << ", file=" << format("%3u", LE.File) <br>
+      << ", line=" << format("%3u", LE.Line);<br>
+}<br>
+<br>
+inline bool operator==(const LineEntry &LHS, const LineEntry &RHS) {<br>
+  return LHS.Addr == RHS.Addr && LHS.File == RHS.File && LHS.Line == RHS.Line;<br>
+}<br>
+inline bool operator!=(const LineEntry &LHS, const LineEntry &RHS) {<br>
+  return !(LHS == RHS);<br>
+}<br>
+inline bool operator<(const LineEntry &LHS, const LineEntry &RHS) {<br>
+  return LHS.Addr < RHS.Addr;<br>
+}<br>
+} // namespace gsym<br>
+} // namespace llvm<br>
+#endif // #ifndef LLVM_DEBUGINFO_GSYM_LINEENTRY_H<br>
<br>
Added: llvm/trunk/include/llvm/DebugInfo/GSYM/Range.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/GSYM/Range.h?rev=364427&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/GSYM/Range.h?rev=364427&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/DebugInfo/GSYM/Range.h (added)<br>
+++ llvm/trunk/include/llvm/DebugInfo/GSYM/Range.h Wed Jun 26 07:09:09 2019<br>
@@ -0,0 +1,123 @@<br>
+//===- AddressRange.h -------------------------------------------*- C++ -*-===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#ifndef LLVM_DEBUGINFO_GSYM_RANGE_H<br>
+#define LLVM_DEBUGINFO_GSYM_RANGE_H<br>
+<br>
+#include "llvm/Support/Format.h"<br>
+#include "llvm/Support/raw_ostream.h"<br>
+#include <stdint.h><br>
+#include <vector><br>
+<br>
+#define HEX8(v) llvm::format_hex(v, 4)<br>
+#define HEX16(v) llvm::format_hex(v, 6)<br>
+#define HEX32(v) llvm::format_hex(v, 10)<br>
+#define HEX64(v) llvm::format_hex(v, 18)<br>
+<br>
+namespace llvm {<br>
+class raw_ostream;<br>
+<br>
+namespace gsym {<br>
+<br>
+/// A class that represents an address range. The range is specified using<br>
+/// a start and an end address. <br>
+class AddressRange {<br>
+  uint64_t Start;<br>
+  uint64_t End;<br>
+public:<br>
+  AddressRange(uint64_t S = 0, uint64_t E = 0) : Start(S), End(E) {}<br>
+  /// Access to the size must use the size() accessor to ensure the correct<br>
+  /// answer. This allows an AddressRange to be constructed with invalid<br>
+  /// address ranges where the end address is less that the start address<br>
+  /// either because it was not set, or because of incorrect data.<br>
+  uint64_t size() const { return Start < End ? End - Start : 0; }<br>
+  void setStartAddress(uint64_t Addr) { Start = Addr; }<br>
+  void setEndAddress(uint64_t Addr) { End = Addr; }<br>
+  void setSize(uint64_t Size) { End = Start + Size; }<br>
+  uint64_t startAddress() const { return Start; }<br>
+  /// Access to the end address must use the size() accessor to ensure the<br>
+  /// correct answer. This allows an AddressRange to be constructed with<br>
+  /// invalid address ranges where the end address is less that the start <br>
+  /// address either because it was not set, or because of incorrect data.<br>
+  uint64_t endAddress() const { return Start + size(); }<br>
+  void clear() {<br>
+    Start = 0;<br>
+    End = 0;<br>
+  }<br>
+  bool contains(uint64_t Addr) const { return Start <= Addr && Addr < endAddress(); }<br>
+  bool isContiguousWith(const AddressRange &R) const {<br>
+    return (Start <= R.endAddress()) && (endAddress() >= R.Start);<br>
+  }<br>
+  bool intersects(const AddressRange &R) const {<br>
+    return (Start < R.endAddress()) && (endAddress() > R.Start);<br>
+  }<br>
+  bool intersect(const AddressRange &R) {<br>
+    if (intersects(R)) {<br>
+      Start = std::min<uint64_t>(Start, R.Start);<br>
+      End = std::max<uint64_t>(endAddress(), R.endAddress());<br>
+      return true;<br>
+    }<br>
+    return false;<br>
+  }<br>
+};<br>
+<br>
+inline bool operator==(const AddressRange &LHS, const AddressRange &RHS) {<br>
+  return LHS.startAddress() == RHS.startAddress() && LHS.endAddress() == RHS.endAddress();<br>
+}<br>
+inline bool operator!=(const AddressRange &LHS, const AddressRange &RHS) {<br>
+  return LHS.startAddress() != RHS.startAddress() || LHS.endAddress() != RHS.endAddress();<br>
+}<br>
+inline bool operator<(const AddressRange &LHS, const AddressRange &RHS) {<br>
+  if (LHS.startAddress() == RHS.startAddress())<br>
+    return LHS.endAddress() < RHS.endAddress();<br>
+  return LHS.startAddress() < RHS.startAddress();<br>
+}<br>
+inline bool operator<(const AddressRange &LHS, uint64_t Addr) {<br>
+  return LHS.startAddress() < Addr;<br>
+}<br>
+inline bool operator<(uint64_t Addr, const AddressRange &RHS) {<br>
+  return Addr < RHS.startAddress();<br>
+}<br>
+<br>
+raw_ostream &operator<<(raw_ostream &OS, const AddressRange &R);<br>
+<br>
+/// The AddressRanges class helps normalize address range collections.<br>
+/// This class keeps a sorted vector of AddressRange objects and can perform<br>
+/// insertions and searches efficiently. The address ranges are always sorted<br>
+/// and never contain any invalid or empty address ranges. This allows us to<br>
+/// emit address ranges into the GSYM file efficiently. Intersecting address<br>
+/// ranges are combined during insertion so that we can emit the most compact<br>
+/// representation for address ranges when writing to disk.<br>
+class AddressRanges {<br>
+protected:<br>
+  typedef std::vector<AddressRange> Collection;<br>
+  Collection Ranges;<br>
+public:<br>
+  void clear() { Ranges.clear(); }<br>
+  bool empty() const { return Ranges.empty(); }<br>
+  bool contains(uint64_t Addr) const;<br>
+  void insert(const AddressRange &R);<br>
+  size_t size() const { return Ranges.size(); }<br>
+  bool operator==(const AddressRanges &RHS) const {<br>
+    return Ranges == RHS.Ranges;<br>
+  }<br>
+  const AddressRange &operator[](size_t i) const {<br>
+    assert(i < Ranges.size());<br>
+    return Ranges[i];<br>
+  }<br>
+  Collection::const_iterator begin() const { return Ranges.begin(); }<br>
+  Collection::const_iterator end() const { return Ranges.end(); }<br>
+};<br>
+<br>
+raw_ostream &operator<<(raw_ostream &OS, const AddressRanges &AR);<br>
+<br>
+} // namespace gsym<br>
+} // namespace llvm<br>
+<br>
+#endif // #ifndef LLVM_DEBUGINFO_GSYM_RANGE_H<br>
<br>
Added: llvm/trunk/include/llvm/DebugInfo/GSYM/StringTable.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/GSYM/StringTable.h?rev=364427&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/GSYM/StringTable.h?rev=364427&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/DebugInfo/GSYM/StringTable.h (added)<br>
+++ llvm/trunk/include/llvm/DebugInfo/GSYM/StringTable.h Wed Jun 26 07:09:09 2019<br>
@@ -0,0 +1,54 @@<br>
+//===- StringTable.h --------------------------------------------*- C++ -*-===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#ifndef LLVM_DEBUGINFO_GSYM_STRINGTABLE_H<br>
+#define LLVM_DEBUGINFO_GSYM_STRINGTABLE_H<br>
+<br>
+#include "llvm/ADT/Optional.h"<br>
+#include "llvm/ADT/StringRef.h"<br>
+#include "llvm/DebugInfo/GSYM/Range.h"<br>
+#include <stdint.h><br>
+#include <string><br>
+<br>
+<br>
+namespace llvm {<br>
+namespace gsym {<br>
+<br>
+/// String tables in GSYM files are required to start with an empty <br>
+/// string at offset zero. Strings must be UTF8 NULL terminated strings.<br>
+struct StringTable {<br>
+  StringRef Data;<br>
+  StringTable() : Data() {}<br>
+  StringTable(StringRef D) : Data(D) {}<br>
+  StringRef operator[](size_t Offset) const { return getString(Offset); }<br>
+  StringRef getString(uint32_t Offset) const {<br>
+    if (Offset < Data.size()) {<br>
+      auto End = Data.find('\0', Offset);<br>
+      return Data.substr(Offset, End - Offset);<br>
+    }<br>
+    return StringRef();<br>
+  }<br>
+  void clear() { Data = StringRef(); }<br>
+};<br>
+<br>
+inline raw_ostream &operator<<(raw_ostream &OS, const StringTable &S) {<br>
+  OS << "String table:\n";<br>
+  uint32_t Offset = 0;<br>
+  const size_t Size = S.Data.size();<br>
+  while (Offset < Size) {<br>
+    StringRef Str = S.getString(Offset);<br>
+    OS << HEX32(Offset) << ": \"" << Str << "\"\n";<br>
+    Offset += Str.size() + 1;<br>
+  }<br>
+  return OS;<br>
+}<br>
+<br>
+} // namespace gsym<br>
+} // namespace llvm<br>
+#endif // #ifndef LLVM_DEBUGINFO_GSYM_STRINGTABLE_H<br>
<br>
Modified: llvm/trunk/lib/DebugInfo/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/CMakeLists.txt?rev=364427&r1=364426&r2=364427&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/CMakeLists.txt?rev=364427&r1=364426&r2=364427&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/DebugInfo/CMakeLists.txt (original)<br>
+++ llvm/trunk/lib/DebugInfo/CMakeLists.txt Wed Jun 26 07:09:09 2019<br>
@@ -1,4 +1,5 @@<br>
 add_subdirectory(DWARF)<br>
+add_subdirectory(GSYM)<br>
 add_subdirectory(MSF)<br>
 add_subdirectory(CodeView)<br>
 add_subdirectory(PDB)<br>
<br>
Added: llvm/trunk/lib/DebugInfo/GSYM/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/GSYM/CMakeLists.txt?rev=364427&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/GSYM/CMakeLists.txt?rev=364427&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/DebugInfo/GSYM/CMakeLists.txt (added)<br>
+++ llvm/trunk/lib/DebugInfo/GSYM/CMakeLists.txt Wed Jun 26 07:09:09 2019<br>
@@ -0,0 +1,9 @@<br>
+add_llvm_library(LLVMDebugInfoGSYM<br>
+  FunctionInfo.cpp<br>
+  InlineInfo.cpp<br>
+  Range.cpp<br>
+<br>
+  ADDITIONAL_HEADER_DIRS<br>
+  ${LLVM_MAIN_INCLUDE_DIR}/llvm/DebugInfo/GSYM<br>
+  ${LLVM_MAIN_INCLUDE_DIR}/llvm/DebugInfo<br>
+  )<br>
<br>
Added: llvm/trunk/lib/DebugInfo/GSYM/FunctionInfo.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/GSYM/FunctionInfo.cpp?rev=364427&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/GSYM/FunctionInfo.cpp?rev=364427&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/DebugInfo/GSYM/FunctionInfo.cpp (added)<br>
+++ llvm/trunk/lib/DebugInfo/GSYM/FunctionInfo.cpp Wed Jun 26 07:09:09 2019<br>
@@ -0,0 +1,23 @@<br>
+//===- FunctionInfo.cpp -----------------------------------------*- C++ -*-===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/DebugInfo/GSYM/FunctionInfo.h"<br>
+<br>
+using namespace llvm;<br>
+using namespace gsym;<br>
+<br>
+raw_ostream &llvm::gsym::operator<<(raw_ostream &OS, const FunctionInfo &FI) {<br>
+  OS << '[' << HEX64(FI.Range.startAddress()) << '-'<br>
+     << HEX64(FI.Range.endAddress()) << "): "<br>
+     << "Name=" << HEX32(FI.Name) << '\n';<br>
+  for (const auto &Line : FI.Lines)<br>
+    OS << Line << '\n';<br>
+  OS << FI.Inline;<br>
+  return OS;<br>
+}<br>
<br>
Added: llvm/trunk/lib/DebugInfo/GSYM/InlineInfo.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/GSYM/InlineInfo.cpp?rev=364427&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/GSYM/InlineInfo.cpp?rev=364427&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/DebugInfo/GSYM/InlineInfo.cpp (added)<br>
+++ llvm/trunk/lib/DebugInfo/GSYM/InlineInfo.cpp Wed Jun 26 07:09:09 2019<br>
@@ -0,0 +1,59 @@<br>
+//===- InlineInfo.cpp -------------------------------------------*- C++ -*-===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/DebugInfo/GSYM/FileEntry.h"<br>
+#include "llvm/DebugInfo/GSYM/InlineInfo.h"<br>
+#include <algorithm><br>
+#include <inttypes.h><br>
+<br>
+using namespace llvm;<br>
+using namespace gsym;<br>
+<br>
+<br>
+raw_ostream &llvm::gsym::operator<<(raw_ostream &OS, const InlineInfo &II) {<br>
+  if (!II.isValid())<br>
+    return OS;<br>
+  bool First = true;<br>
+  for (auto Range : II.Ranges) {<br>
+    if (First)<br>
+      First = false;<br>
+    else<br>
+      OS << ' ';<br>
+    OS << Range;<br>
+  }<br>
+  OS << " Name = " << HEX32(II.Name) << ", CallFile = " << II.CallFile<br>
+     << ", CallLine = " << II.CallFile << '\n';<br>
+  for (const auto &Child : II.Children)<br>
+    OS << Child;<br>
+  return OS;<br>
+}<br>
+<br>
+static bool getInlineStackHelper(const InlineInfo &II, uint64_t Addr, <br>
+    std::vector<const InlineInfo *> &InlineStack) {<br>
+  if (II.Ranges.contains(Addr)) {<br>
+    // If this is the top level that represents the concrete function,<br>
+    // there will be no name and we shoud clear the inline stack. Otherwise<br>
+    // we have found an inline call stack that we need to insert.<br>
+    if (II.Name != 0)<br>
+      InlineStack.insert(InlineStack.begin(), &II);<br>
+    for (const auto &Child : II.Children) {<br>
+      if (::getInlineStackHelper(Child, Addr, InlineStack))<br>
+        break;<br>
+    }<br>
+    return !InlineStack.empty();<br>
+  }<br>
+  return false;<br>
+}<br>
+<br>
+llvm::Optional<InlineInfo::InlineArray> InlineInfo::getInlineStack(uint64_t Addr) const {<br>
+  InlineArray Result;<br>
+  if (getInlineStackHelper(*this, Addr, Result))<br>
+    return Result;<br>
+  return llvm::None;<br>
+}<br>
<br>
Added: llvm/trunk/lib/DebugInfo/GSYM/Range.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/GSYM/Range.cpp?rev=364427&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/GSYM/Range.cpp?rev=364427&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/DebugInfo/GSYM/Range.cpp (added)<br>
+++ llvm/trunk/lib/DebugInfo/GSYM/Range.cpp Wed Jun 26 07:09:09 2019<br>
@@ -0,0 +1,71 @@<br>
+//===- Range.cpp ------------------------------------------------*- C++ -*-===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/DebugInfo/GSYM/Range.h"<br>
+#include <algorithm><br>
+#include <inttypes.h><br>
+<br>
+using namespace llvm;<br>
+using namespace gsym;<br>
+<br>
+<br>
+void AddressRanges::insert(const AddressRange &Range) {<br>
+  if (Range.size() == 0)<br>
+    return;<br>
+  // Ranges.insert(std::upper_bound(Ranges.begin(), Ranges.end(), Range), Range);<br>
+<br>
+  // // Check if an existing range intersects with this range, and if so, <br>
+  // // grow the intersecting ranges instead of adding a new one.<br>
+  auto Begin = Ranges.begin();<br>
+  auto End = Ranges.end();<br>
+  const auto Iter = std::upper_bound(Begin, End, Range);<br>
+  if (Iter != Begin) {<br>
+    auto PrevIter = Iter - 1;<br>
+    // If the previous range itersects with "Range" they will be combined.<br>
+    if (PrevIter->intersect(Range)) {<br>
+      // Now check if the previous range intersects with the next range since<br>
+      // the previous range was combined. If so, combine them and remove the<br>
+      // next range.<br>
+      if (PrevIter->intersect(*Iter))<br>
+        Ranges.erase(Iter);<br>
+      return;<br>
+    }<br>
+  }<br>
+  // If the next range intersects with "Range", combined and return.<br>
+  if (Iter != End && Iter->intersect(Range))<br>
+    return;<br>
+  Ranges.insert(Iter, Range);<br>
+}<br>
+<br>
+bool AddressRanges::contains(uint64_t Addr) const {<br>
+  if (Ranges.empty())<br>
+    return false;<br>
+  auto Begin = Ranges.begin();<br>
+  auto Pos = std::upper_bound(Begin, Ranges.end(), Addr);<br>
+  if (Pos == Begin)<br>
+    return false;<br>
+  --Pos;<br>
+  return Pos->contains(Addr);<br>
+}<br>
+<br>
+raw_ostream &llvm::gsym::operator<<(raw_ostream &OS, const AddressRange &R) {<br>
+  return OS << '[' << HEX64(R.startAddress()) << " - " << HEX64(R.endAddress())<br>
+            << ")";<br>
+}<br>
+<br>
+raw_ostream &llvm::gsym::operator<<(raw_ostream &OS, const AddressRanges &AR) {<br>
+  size_t Size = AR.size();<br>
+  for (size_t I=0; I<Size; ++I) {<br>
+    if (I)<br>
+      OS << ' ';<br>
+    OS << AR[I];<br>
+  }<br>
+  return OS;<br>
+}<br>
+<br>
<br>
Modified: llvm/trunk/unittests/DebugInfo/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/DebugInfo/CMakeLists.txt?rev=364427&r1=364426&r2=364427&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/DebugInfo/CMakeLists.txt?rev=364427&r1=364426&r2=364427&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/unittests/DebugInfo/CMakeLists.txt (original)<br>
+++ llvm/trunk/unittests/DebugInfo/CMakeLists.txt Wed Jun 26 07:09:09 2019<br>
@@ -1,4 +1,5 @@<br>
 add_subdirectory(CodeView)<br>
 add_subdirectory(DWARF)<br>
+add_subdirectory(GSYM)<br>
 add_subdirectory(MSF)<br>
 add_subdirectory(PDB)<br>
<br>
Added: llvm/trunk/unittests/DebugInfo/GSYM/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/DebugInfo/GSYM/CMakeLists.txt?rev=364427&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/DebugInfo/GSYM/CMakeLists.txt?rev=364427&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/unittests/DebugInfo/GSYM/CMakeLists.txt (added)<br>
+++ llvm/trunk/unittests/DebugInfo/GSYM/CMakeLists.txt Wed Jun 26 07:09:09 2019<br>
@@ -0,0 +1,15 @@<br>
+set(LLVM_LINK_COMPONENTS<br>
+  ${LLVM_TARGETS_TO_BUILD}<br>
+  AsmPrinter<br>
+  DebugInfoGSYM<br>
+  MC<br>
+  Object<br>
+  ObjectYAML<br>
+  Support<br>
+  )<br>
+<br>
+add_llvm_unittest(DebugInfoGSYMTests<br>
+  GSYMTest.cpp<br>
+  )<br>
+<br>
+target_link_libraries(DebugInfoGSYMTests PRIVATE LLVMTestingSupport)<br>
<br>
Added: llvm/trunk/unittests/DebugInfo/GSYM/GSYMTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/DebugInfo/GSYM/GSYMTest.cpp?rev=364427&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/DebugInfo/GSYM/GSYMTest.cpp?rev=364427&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/unittests/DebugInfo/GSYM/GSYMTest.cpp (added)<br>
+++ llvm/trunk/unittests/DebugInfo/GSYM/GSYMTest.cpp Wed Jun 26 07:09:09 2019<br>
@@ -0,0 +1,390 @@<br>
+//===- llvm/unittest/DebugInfo/GSYMTest.cpp -------------------------------===//<br>
+//<br>
+//                     The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/ADT/DenseMap.h"<br>
+#include "llvm/DebugInfo/GSYM/FileEntry.h"<br>
+#include "llvm/DebugInfo/GSYM/FunctionInfo.h"<br>
+#include "llvm/DebugInfo/GSYM/InlineInfo.h"<br>
+#include "llvm/DebugInfo/GSYM/Range.h"<br>
+#include "llvm/DebugInfo/GSYM/StringTable.h"<br>
+#include "llvm/Testing/Support/Error.h"<br>
+<br>
+#include "gtest/gtest.h"<br>
+#include <sstream><br>
+#include <string><br>
+<br>
+using namespace llvm;<br>
+using namespace gsym;<br>
+<br>
+TEST(GSYMTest, TestFileEntry) {<br>
+  // Make sure default constructed GSYM FileEntry has zeroes in the <br>
+  // directory and basename string table indexes.<br>
+  FileEntry empty1;<br>
+  FileEntry empty2;<br>
+  EXPECT_EQ(empty1.Dir, 0u);<br>
+  EXPECT_EQ(empty1.Base, 0u);<br>
+  // Verify equality operator works<br>
+  FileEntry a1(10,30);<br>
+  FileEntry a2(10,30);<br>
+  FileEntry b(10,40);<br>
+  EXPECT_EQ(empty1, empty2);<br>
+  EXPECT_EQ(a1, a2);<br>
+  EXPECT_NE(a1, b);<br>
+  EXPECT_NE(a1, empty1);<br>
+  // Test we can use llvm::gsym::FileEntry in llvm::DenseMap.<br>
+  DenseMap<FileEntry, uint32_t> EntryToIndex;<br>
+  constexpr uint32_t Index1 = 1;<br>
+  constexpr uint32_t Index2 = 1;<br>
+  auto R = EntryToIndex.insert(std::make_pair(a1, Index1));<br>
+  EXPECT_TRUE(R.second);<br>
+  EXPECT_EQ(R.first->second, Index1);<br>
+  R = EntryToIndex.insert(std::make_pair(a1, Index1));<br>
+  EXPECT_FALSE(R.second);<br>
+  EXPECT_EQ(R.first->second, Index1);<br>
+  R = EntryToIndex.insert(std::make_pair(b, Index2));<br>
+  EXPECT_TRUE(R.second);<br>
+  EXPECT_EQ(R.first->second, Index2);<br>
+  R = EntryToIndex.insert(std::make_pair(a1, Index2));<br>
+  EXPECT_FALSE(R.second);<br>
+  EXPECT_EQ(R.first->second, Index2);<br>
+}<br>
+<br>
+<br>
+TEST(GSYMTest, TestFunctionInfo) {<br>
+  // Test GSYM FunctionInfo structs and functionality.<br>
+  FunctionInfo invalid;<br>
+  EXPECT_FALSE(invalid.isValid());<br>
+  EXPECT_FALSE(invalid.hasRichInfo());<br>
+  const uint64_t StartAddr = 0x1000;<br>
+  const uint64_t EndAddr = 0x1100;<br>
+  const uint64_t Size = EndAddr - StartAddr;<br>
+  const uint32_t NameOffset = 30;<br>
+  FunctionInfo FI(StartAddr, Size, NameOffset);<br>
+  EXPECT_TRUE(FI.isValid());<br>
+  EXPECT_FALSE(FI.hasRichInfo());<br>
+  EXPECT_EQ(FI.startAddress(), StartAddr);<br>
+  EXPECT_EQ(FI.endAddress(), EndAddr);<br>
+  EXPECT_EQ(FI.size(), Size);<br>
+  const uint32_t FileIdx = 1;<br>
+  const uint32_t Line = 12;<br>
+  FI.Lines.push_back(LineEntry(StartAddr,FileIdx,Line));<br>
+  EXPECT_TRUE(FI.hasRichInfo());<br>
+  FI.clear();<br>
+  EXPECT_FALSE(FI.isValid());<br>
+  EXPECT_FALSE(FI.hasRichInfo());<br>
+<br>
+  FunctionInfo A1(0x1000, 0x100, NameOffset);<br>
+  FunctionInfo A2(0x1000, 0x100, NameOffset);<br>
+  FunctionInfo B;<br>
+  // Check == operator<br>
+  EXPECT_EQ(A1, A2);<br>
+  // Make sure things are not equal if they only differ by start address.<br>
+  B = A2;<br>
+  B.setStartAddress(0x2000);<br>
+  EXPECT_NE(B, A2);<br>
+  // Make sure things are not equal if they only differ by size.<br>
+  B = A2;<br>
+  B.setSize(0x101);<br>
+  EXPECT_NE(B, A2);<br>
+  // Make sure things are not equal if they only differ by name.<br>
+  B = A2;<br>
+  B.Name = 60;<br>
+  EXPECT_NE(B, A2);<br>
+  // Check < operator.<br>
+  // Check less than where address differs.<br>
+  B = A2;<br>
+  B.setStartAddress(A2.startAddress() + 0x1000);<br>
+  EXPECT_LT(A1, B);<br>
+  <br>
+  // We use the < operator to take a variety of different FunctionInfo<br>
+  // structs from a variety of sources: symtab, debug info, runtime info<br>
+  // and we sort them and want the sorting to allow us to quickly get the<br>
+  // best version of a function info. <br>
+  FunctionInfo FISymtab(StartAddr, Size, NameOffset);<br>
+  FunctionInfo FIWithLines(StartAddr, Size, NameOffset);<br>
+  FIWithLines.Lines.push_back(LineEntry(StartAddr,FileIdx,Line));<br>
+  // Test that a FunctionInfo with just a name and size is less than one<br>
+  // that has name, size and any number of line table entries<br>
+  EXPECT_LT(FISymtab, FIWithLines);<br>
+<br>
+  FunctionInfo FIWithLinesAndInline = FIWithLines;<br>
+  FIWithLinesAndInline.Inline.Ranges.insert(AddressRange(StartAddr, StartAddr + 0x10));<br>
+  // Test that a FunctionInfo with name, size, and line entries is less than<br>
+  // the same one with valid inline info<br>
+  EXPECT_LT(FIWithLines, FIWithLinesAndInline);<br>
+<br>
+  // Test if we have an entry with lines and one with more lines for the same<br>
+  // range, the ones with more lines is greater than the one with less.<br>
+  FunctionInfo FIWithMoreLines = FIWithLines;<br>
+  FIWithMoreLines.Lines.push_back(LineEntry(StartAddr,FileIdx,Line+5));<br>
+  EXPECT_LT(FIWithLines, FIWithMoreLines);<br>
+<br>
+  // Test that if we have the same number of lines we compare the line entries<br>
+  // in the FunctionInfo.Lines vector.<br>
+  FunctionInfo FIWithLinesWithHigherAddress = FIWithLines;<br>
+  FIWithLinesWithHigherAddress.Lines[0].Addr += 0x10;<br>
+  EXPECT_LT(FIWithLines, FIWithLinesWithHigherAddress);<br>
+}<br>
+<br>
+TEST(GSYMTest, TestInlineInfo) {<br>
+  // Test InlineInfo structs.<br>
+  InlineInfo II;<br>
+  EXPECT_FALSE(II.isValid());<br>
+  II.Ranges.insert(AddressRange(0x1000,0x2000));<br>
+  // Make sure InlineInfo in valid with just an address range since<br>
+  // top level InlineInfo objects have ranges with no name, call file<br>
+  // or call line<br>
+  EXPECT_TRUE(II.isValid());<br>
+  // Make sure InlineInfo isn't after being cleared.<br>
+  II.clear();<br>
+  EXPECT_FALSE(II.isValid());<br>
+<br>
+  // Create an InlineInfo that contains the following data. The<br>
+  // indentation of the address range indicates the parent child<br>
+  // relationships of the InlineInfo objects:<br>
+  //<br>
+  // Variable    Range and values<br>
+  // =========== ====================================================<br>
+  // Root        [0x100-0x200) (no name, file, or line)<br>
+  // Inline1       [0x150-0x160) Name = 1, File = 1, Line = 11<br>
+  // Inline1Sub1     [0x152-0x155) Name = 2, File = 2, Line = 22<br>
+  // Inline1Sub2     [0x157-0x158) Name = 3, File = 3, Line = 33<br>
+  InlineInfo Root;<br>
+  Root.Ranges.insert(AddressRange(0x100,0x200));<br>
+  InlineInfo Inline1;<br>
+  Inline1.Ranges.insert(AddressRange(0x150,0x160));<br>
+  Inline1.Name = 1;<br>
+  Inline1.CallFile = 1;<br>
+  Inline1.CallLine = 11;<br>
+  InlineInfo Inline1Sub1;<br>
+  Inline1Sub1.Ranges.insert(AddressRange(0x152, 0x155));<br>
+  Inline1Sub1.Name = 2;<br>
+  Inline1Sub1.CallFile = 2;<br>
+  Inline1Sub1.CallLine = 22;<br>
+  InlineInfo Inline1Sub2;<br>
+  Inline1Sub2.Ranges.insert(AddressRange(0x157,0x158));<br>
+  Inline1Sub2.Name = 3;<br>
+  Inline1Sub2.CallFile = 3;<br>
+  Inline1Sub2.CallLine = 33;<br>
+  Inline1.Children.push_back(Inline1Sub1);<br>
+  Inline1.Children.push_back(Inline1Sub2);<br>
+  Root.Children.push_back(Inline1);<br>
+<br>
+  // Make sure an address that is out of range won't match<br>
+  EXPECT_FALSE(Root.getInlineStack(0x50));<br>
+<br>
+  // Verify that we get no inline stacks for addresses out of [0x100-0x200) <br>
+  EXPECT_FALSE(Root.getInlineStack(Root.Ranges[0].startAddress()-1));<br>
+  EXPECT_FALSE(Root.getInlineStack(Root.Ranges[0].endAddress()));<br>
+<br>
+  // Verify we get no inline stack entries for addresses that are in<br>
+  // [0x100-0x200) but not in [0x150-0x160)<br>
+  EXPECT_FALSE(Root.getInlineStack(Inline1.Ranges[0].startAddress()-1));<br>
+  EXPECT_FALSE(Root.getInlineStack(Inline1.Ranges[0].endAddress()));<br>
+<br>
+<br>
+  // Verify we get one inline stack entry for addresses that are in<br>
+  // [[0x150-0x160)) but not in [0x152-0x155) or [0x157-0x158)<br>
+  auto InlineInfos = Root.getInlineStack(Inline1.Ranges[0].startAddress());<br>
+  ASSERT_TRUE(InlineInfos);<br>
+  ASSERT_EQ(InlineInfos->size(), 1u);<br>
+  ASSERT_EQ(*InlineInfos->at(0), Inline1);<br>
+  InlineInfos = Root.getInlineStack(Inline1.Ranges[0].endAddress()-1);<br>
+  EXPECT_TRUE(InlineInfos);<br>
+  ASSERT_EQ(InlineInfos->size(), 1u);<br>
+  ASSERT_EQ(*InlineInfos->at(0), Inline1);<br>
+<br>
+  // Verify we get two inline stack entries for addresses that are in<br>
+  // [0x152-0x155)<br>
+  InlineInfos = Root.getInlineStack(Inline1Sub1.Ranges[0].startAddress());<br>
+  EXPECT_TRUE(InlineInfos);<br>
+  ASSERT_EQ(InlineInfos->size(), 2u);<br>
+  ASSERT_EQ(*InlineInfos->at(0), Inline1Sub1);<br>
+  ASSERT_EQ(*InlineInfos->at(1), Inline1);<br>
+  InlineInfos = Root.getInlineStack(Inline1Sub1.Ranges[0].endAddress()-1);<br>
+  EXPECT_TRUE(InlineInfos);<br>
+  ASSERT_EQ(InlineInfos->size(), 2u);<br>
+  ASSERT_EQ(*InlineInfos->at(0), Inline1Sub1);<br>
+  ASSERT_EQ(*InlineInfos->at(1), Inline1);<br>
+<br>
+  // Verify we get two inline stack entries for addresses that are in<br>
+  // [0x157-0x158)<br>
+  InlineInfos = Root.getInlineStack(Inline1Sub2.Ranges[0].startAddress());<br>
+  EXPECT_TRUE(InlineInfos);<br>
+  ASSERT_EQ(InlineInfos->size(), 2u);<br>
+  ASSERT_EQ(*InlineInfos->at(0), Inline1Sub2);<br>
+  ASSERT_EQ(*InlineInfos->at(1), Inline1);<br>
+  InlineInfos = Root.getInlineStack(Inline1Sub2.Ranges[0].endAddress()-1);<br>
+  EXPECT_TRUE(InlineInfos);<br>
+  ASSERT_EQ(InlineInfos->size(), 2u);<br>
+  ASSERT_EQ(*InlineInfos->at(0), Inline1Sub2);<br>
+  ASSERT_EQ(*InlineInfos->at(1), Inline1);<br>
+} <br>
+<br>
+TEST(GSYMTest, TestLineEntry) {<br>
+  // test llvm::gsym::LineEntry structs.<br>
+  const uint64_t ValidAddr = 0x1000;<br>
+  const uint64_t InvalidFileIdx = 0;<br>
+  const uint32_t ValidFileIdx = 1;<br>
+  const uint32_t ValidLine = 5;<br>
+<br>
+  LineEntry Invalid;<br>
+  EXPECT_FALSE(Invalid.isValid());<br>
+  // Make sure that an entry is invalid if it has a bad file index.<br>
+  LineEntry BadFile(ValidAddr, InvalidFileIdx, ValidLine);<br>
+  EXPECT_FALSE(BadFile.isValid());<br>
+  // Test operators<br>
+  LineEntry E1(ValidAddr, ValidFileIdx, ValidLine);<br>
+  LineEntry E2(ValidAddr, ValidFileIdx, ValidLine);<br>
+  LineEntry DifferentAddr(ValidAddr+1, ValidFileIdx, ValidLine);<br>
+  LineEntry DifferentFile(ValidAddr, ValidFileIdx+1, ValidLine);<br>
+  LineEntry DifferentLine(ValidAddr, ValidFileIdx, ValidLine+1);<br>
+  EXPECT_TRUE(E1.isValid());<br>
+  EXPECT_EQ(E1, E2);<br>
+  EXPECT_NE(E1, DifferentAddr);<br>
+  EXPECT_NE(E1, DifferentFile);<br>
+  EXPECT_NE(E1, DifferentLine);<br>
+  EXPECT_LT(E1, DifferentAddr);<br>
+}<br>
+<br>
+TEST(GSYMTest, TestRanges) {<br>
+  // test llvm::gsym::AddressRange.<br>
+  const uint64_t StartAddr = 0x1000;<br>
+  const uint64_t EndAddr = 0x2000;<br>
+  // Verify constructor and API to ensure it takes start and end address.<br>
+  const AddressRange Range(StartAddr, EndAddr);<br>
+  EXPECT_EQ(Range.startAddress(), StartAddr);<br>
+  EXPECT_EQ(Range.endAddress(), EndAddr);<br>
+  EXPECT_EQ(Range.size(), EndAddr-StartAddr);<br>
+<br>
+  // Verify llvm::gsym::AddressRange::contains().<br>
+  EXPECT_FALSE(Range.contains(0));<br>
+  EXPECT_FALSE(Range.contains(StartAddr-1));<br>
+  EXPECT_TRUE(Range.contains(StartAddr));<br>
+  EXPECT_TRUE(Range.contains(EndAddr-1));<br>
+  EXPECT_FALSE(Range.contains(EndAddr));<br>
+  EXPECT_FALSE(Range.contains(UINT64_MAX));<br>
+<br>
+  const AddressRange RangeSame(StartAddr, EndAddr);<br>
+  const AddressRange RangeDifferentStart(StartAddr+1, EndAddr);<br>
+  const AddressRange RangeDifferentEnd(StartAddr, EndAddr+1);<br>
+  const AddressRange RangeDifferentStartEnd(StartAddr+1, EndAddr+1);<br>
+  // Test == and != with values that are the same<br>
+  EXPECT_EQ(Range, RangeSame);<br>
+  EXPECT_FALSE(Range != RangeSame);<br>
+  // Test == and != with values that are the different<br>
+  EXPECT_NE(Range, RangeDifferentStart);<br>
+  EXPECT_NE(Range, RangeDifferentEnd);<br>
+  EXPECT_NE(Range, RangeDifferentStartEnd);<br>
+  EXPECT_FALSE(Range == RangeDifferentStart);<br>
+  EXPECT_FALSE(Range == RangeDifferentEnd);<br>
+  EXPECT_FALSE(Range == RangeDifferentStartEnd);<br>
+<br>
+  // Test "bool operator<(const AddressRange &, const AddressRange &)".<br>
+  EXPECT_FALSE(Range < RangeSame);<br>
+  EXPECT_FALSE(RangeSame < Range);<br>
+  EXPECT_LT(Range, RangeDifferentStart);<br>
+  EXPECT_LT(Range, RangeDifferentEnd);<br>
+  EXPECT_LT(Range, RangeDifferentStartEnd);<br>
+  // Test "bool operator<(const AddressRange &, uint64_t)"<br>
+  EXPECT_LT(Range, StartAddr + 1);<br>
+  // Test "bool operator<(uint64_t, const AddressRange &)"<br>
+  EXPECT_LT(StartAddr - 1, Range);<br>
+<br>
+  // Verify llvm::gsym::AddressRange::isContiguousWith() and<br>
+  // llvm::gsym::AddressRange::intersects().<br>
+  const AddressRange EndsBeforeRangeStart(0, StartAddr-1);<br>
+  const AddressRange EndsAtRangeStart(0, StartAddr);<br>
+  const AddressRange OverlapsRangeStart(StartAddr-1, StartAddr+1);<br>
+  const AddressRange InsideRange(StartAddr+1, EndAddr-1);<br>
+  const AddressRange OverlapsRangeEnd(EndAddr-1, EndAddr+1);<br>
+  const AddressRange StartsAtRangeEnd(EndAddr, EndAddr+0x100);<br>
+  const AddressRange StartsAfterRangeEnd(EndAddr+1, EndAddr+0x100);<br>
+<br>
+  EXPECT_FALSE(Range.isContiguousWith(EndsBeforeRangeStart));<br>
+  EXPECT_TRUE(Range.isContiguousWith(EndsAtRangeStart));<br>
+  EXPECT_TRUE(Range.isContiguousWith(OverlapsRangeStart));<br>
+  EXPECT_TRUE(Range.isContiguousWith(InsideRange));<br>
+  EXPECT_TRUE(Range.isContiguousWith(OverlapsRangeEnd));<br>
+  EXPECT_TRUE(Range.isContiguousWith(StartsAtRangeEnd));<br>
+  EXPECT_FALSE(Range.isContiguousWith(StartsAfterRangeEnd));<br>
+<br>
+  EXPECT_FALSE(Range.intersects(EndsBeforeRangeStart));<br>
+  EXPECT_FALSE(Range.intersects(EndsAtRangeStart));<br>
+  EXPECT_TRUE(Range.intersects(OverlapsRangeStart));<br>
+  EXPECT_TRUE(Range.intersects(InsideRange));<br>
+  EXPECT_TRUE(Range.intersects(OverlapsRangeEnd));<br>
+  EXPECT_FALSE(Range.intersects(StartsAtRangeEnd));<br>
+  EXPECT_FALSE(Range.intersects(StartsAfterRangeEnd));<br>
+<br>
+  // Test the functions that maintain GSYM address ranges:<br>
+  //  "bool AddressRange::contains(uint64_t Addr) const;"<br>
+  //  "void AddressRanges::insert(const AddressRange &R);"<br>
+  AddressRanges Ranges;<br>
+  Ranges.insert(AddressRange(0x1000, 0x2000));<br>
+  Ranges.insert(AddressRange(0x2000, 0x3000));<br>
+  Ranges.insert(AddressRange(0x4000, 0x5000));<br>
+<br>
+  EXPECT_FALSE(Ranges.contains(0));<br>
+  EXPECT_FALSE(Ranges.contains(0x1000-1));<br>
+  EXPECT_TRUE(Ranges.contains(0x1000));<br>
+  EXPECT_TRUE(Ranges.contains(0x2000));<br>
+  EXPECT_TRUE(Ranges.contains(0x4000));<br>
+  EXPECT_TRUE(Ranges.contains(0x2000-1));<br>
+  EXPECT_TRUE(Ranges.contains(0x3000-1));<br>
+  EXPECT_FALSE(Ranges.contains(0x3000+1));<br>
+  EXPECT_TRUE(Ranges.contains(0x5000-1));<br>
+  EXPECT_FALSE(Ranges.contains(0x5000+1));<br>
+  EXPECT_FALSE(Ranges.contains(UINT64_MAX));<br>
+<br>
+  // Verify that intersecting ranges get combined<br>
+  Ranges.clear();<br>
+  Ranges.insert(AddressRange(0x1100, 0x1F00));<br>
+  // Verify a wholy contained range that is added doesn't do anything.<br>
+  Ranges.insert(AddressRange(0x1500, 0x1F00));<br>
+  EXPECT_EQ(Ranges.size(), 1u);<br>
+  EXPECT_EQ(Ranges[0], AddressRange(0x1100, 0x1F00));<br>
+<br>
+  // Verify a range that starts before and intersects gets combined.<br>
+  Ranges.insert(AddressRange(0x1000, Ranges[0].startAddress()+1));<br>
+  EXPECT_EQ(Ranges.size(), 1u);<br>
+  EXPECT_EQ(Ranges[0], AddressRange(0x1000, 0x1F00));<br>
+<br>
+  // Verify a range that starts inside and extends ranges gets combined.<br>
+  Ranges.insert(AddressRange(Ranges[0].endAddress()-1, 0x2000));<br>
+  EXPECT_EQ(Ranges.size(), 1u);<br>
+  EXPECT_EQ(Ranges[0], AddressRange(0x1000, 0x2000));<br>
+<br>
+  // Verify that adjacent ranges don't get combined<br>
+  Ranges.insert(AddressRange(0x2000, 0x3000));<br>
+  EXPECT_EQ(Ranges.size(), 2u);<br>
+  EXPECT_EQ(Ranges[0], AddressRange(0x1000, 0x2000));<br>
+  EXPECT_EQ(Ranges[1], AddressRange(0x2000, 0x3000));<br>
+  // Verify if we add an address range that intersects two ranges<br>
+  // that they get combined<br>
+  Ranges.insert(AddressRange(Ranges[0].endAddress()-1, <br>
+                             Ranges[1].startAddress()+1));<br>
+  EXPECT_EQ(Ranges.size(), 1u);<br>
+  EXPECT_EQ(Ranges[0], AddressRange(0x1000, 0x3000));<br>
+<br>
+<br>
+}<br>
+<br>
+TEST(GSYMTest, TestStringTable) {<br>
+  StringTable StrTab(StringRef("\0Hello\0World\0", 13));<br>
+  // Test extracting strings from a string table.<br>
+  EXPECT_EQ(StrTab.getString(0), "");<br>
+  EXPECT_EQ(StrTab.getString(1), "Hello");<br>
+  EXPECT_EQ(StrTab.getString(7), "World");<br>
+  EXPECT_EQ(StrTab.getString(8), "orld");<br>
+  // Test pointing to last NULL terminator gets empty string.<br>
+  EXPECT_EQ(StrTab.getString(12), "");<br>
+  // Test pointing to past end gets empty string.<br>
+  EXPECT_EQ(StrTab.getString(13), "");<br>
+}<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>