[Lldb-commits] [PATCH] D115178: Unify libstdcpp and libcxx formatters for `std::optional`

2021-12-06 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn created this revision.
zrthxn added a reviewer: wallace.
zrthxn requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115178

Files:
  lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp


Index: lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
===
--- lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
+++ lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
@@ -913,11 +913,11 @@
   SyntheticChildrenSP(new ScriptedSyntheticChildren(
   stl_deref_flags,
   "lldb.formatters.cpp.gnu_libstdcpp.StdMapLikeSynthProvider")));
-  cpp_category_sp->GetRegexTypeSyntheticsContainer()->Add(
-  RegularExpression("^std::optional<.+>(( )?&)?$"),
-  SyntheticChildrenSP(new ScriptedSyntheticChildren(
-  stl_synth_flags,
-  "lldb.formatters.cpp.gnu_libstdcpp.StdOptionalSynthProvider")));
+  // cpp_category_sp->GetRegexTypeSyntheticsContainer()->Add(
+  // RegularExpression("^std::optional<.+>(( )?&)?$"),
+  // SyntheticChildrenSP(new ScriptedSyntheticChildren(
+  // stl_synth_flags,
+  // "lldb.formatters.cpp.gnu_libstdcpp.StdOptionalSynthProvider")));
   cpp_category_sp->GetRegexTypeSyntheticsContainer()->Add(
   RegularExpression("^std::multiset<.+> >(( )?&)?$"),
   SyntheticChildrenSP(new ScriptedSyntheticChildren(


Index: lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
===
--- lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
+++ lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
@@ -913,11 +913,11 @@
   SyntheticChildrenSP(new ScriptedSyntheticChildren(
   stl_deref_flags,
   "lldb.formatters.cpp.gnu_libstdcpp.StdMapLikeSynthProvider")));
-  cpp_category_sp->GetRegexTypeSyntheticsContainer()->Add(
-  RegularExpression("^std::optional<.+>(( )?&)?$"),
-  SyntheticChildrenSP(new ScriptedSyntheticChildren(
-  stl_synth_flags,
-  "lldb.formatters.cpp.gnu_libstdcpp.StdOptionalSynthProvider")));
+  // cpp_category_sp->GetRegexTypeSyntheticsContainer()->Add(
+  // RegularExpression("^std::optional<.+>(( )?&)?$"),
+  // SyntheticChildrenSP(new ScriptedSyntheticChildren(
+  // stl_synth_flags,
+  // "lldb.formatters.cpp.gnu_libstdcpp.StdOptionalSynthProvider")));
   cpp_category_sp->GetRegexTypeSyntheticsContainer()->Add(
   RegularExpression("^std::multiset<.+> >(( )?&)?$"),
   SyntheticChildrenSP(new ScriptedSyntheticChildren(
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D115178: Unify libstdcpp and libcxx formatters for `std::optional`

2021-12-06 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 392154.
zrthxn added a comment.
Herald added a subscriber: mgorny.

Squash many commits


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115178/new/

https://reviews.llvm.org/D115178

Files:
  lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt
  lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
  lldb/source/Plugins/Language/CPlusPlus/GenericOptional.cpp
  lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h

Index: lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h
===
--- lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h
+++ lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h
@@ -45,6 +45,10 @@
 LibStdcppBitsetSyntheticFrontEndCreator(CXXSyntheticChildren *,
 lldb::ValueObjectSP);
 
+SyntheticChildrenFrontEnd *
+LibStdcppOptionalSyntheticFrontEndCreator(CXXSyntheticChildren *,
+lldb::ValueObjectSP);
+
 SyntheticChildrenFrontEnd *
 LibStdcppVectorIteratorSyntheticFrontEndCreator(CXXSyntheticChildren *,
 lldb::ValueObjectSP);
Index: lldb/source/Plugins/Language/CPlusPlus/GenericOptional.cpp
===
--- /dev/null
+++ lldb/source/Plugins/Language/CPlusPlus/GenericOptional.cpp
@@ -0,0 +1,106 @@
+//===-- GenericOptional.cpp //---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+// #include "LibCxx.h"
+#include "LibStdcpp.h"
+#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
+#include "lldb/DataFormatters/FormattersHelpers.h"
+#include "lldb/Target/Target.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+namespace {
+
+class GenericOptionalFrontend : public SyntheticChildrenFrontEnd {
+public:
+  enum class StdLib {
+LibCxx,
+LibStdcpp,
+  };
+
+  GenericOptionalFrontend(ValueObject &valobj, StdLib stdlib);
+
+  size_t GetIndexOfChildWithName(ConstString name) override {
+return formatters::ExtractIndexFromString(name.GetCString());
+  }
+
+  bool MightHaveChildren() override { return true; }
+  size_t CalculateNumChildren() override { return m_has_value ? 1U : 0U; }
+
+  ValueObjectSP GetChildAtIndex(size_t idx) override;
+  bool Update() override;
+
+private:
+  bool m_has_value = false;
+  StdLib m_stdlib;
+};
+
+} // namespace
+
+GenericOptionalFrontend::GenericOptionalFrontend(ValueObject &valobj, StdLib stdlib)
+: SyntheticChildrenFrontEnd(valobj), m_stdlib(stdlib) {
+  if (auto target_sp = m_backend.GetTargetSP()) {
+Update();
+  }
+}
+
+
+bool GenericOptionalFrontend::Update() {
+  ValueObjectSP val_sp = m_backend.GetChildMemberWithName(ConstString("_M_payload"), true);
+  
+  if (!val_sp->GetChildMemberWithName(ConstString("_M_engaged"), true))
+return false;
+
+  // _M_engaged is a bool flag and is true if the optional contains a value.
+  // Converting it to unsigned gives us a size of 1 if it contains a value
+  // and 0 if not.
+  m_has_value = val_sp
+  ->GetChildMemberWithName(ConstString("_M_engaged"), true)
+  ->GetValueAsUnsigned(0) != 0;
+
+  return false;
+}
+
+ValueObjectSP GenericOptionalFrontend::GetChildAtIndex(size_t _idx) {
+  if (!m_has_value)
+return ValueObjectSP();
+
+  // _M_value contains the underlying value of an optional if it has one.
+  ValueObjectSP val_sp(
+  m_backend.GetChildMemberWithName(ConstString("_M_payload"), true)
+  ->GetChildAtIndex(0, true)
+  ->GetChildMemberWithName(ConstString("_M_payload"), true)
+  ->GetChildMemberWithName(ConstString("_M_value"), true));
+
+  if (!val_sp)
+return ValueObjectSP();
+
+  CompilerType holder_type = val_sp->GetCompilerType();
+
+  if (!holder_type)
+return ValueObjectSP();
+
+  return val_sp->Clone(ConstString("Value"));
+}
+
+SyntheticChildrenFrontEnd *formatters::LibStdcppOptionalSyntheticFrontEndCreator(
+CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
+  if (valobj_sp)
+return new GenericOptionalFrontend(*valobj_sp,
+ GenericOptionalFrontend::StdLib::LibStdcpp);
+  return nullptr;
+}
+
+// SyntheticChildrenFrontEnd *formatters::LibcxxOptionalSyntheticFrontEndCreator(
+// CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
+//   if (valobj_sp)
+// return new GenericOptionalFrontend(*valobj_sp,
+//  GenericOptionalFrontend::StdLib::LibCxx);
+//   return nullptr;
+// }
Index: lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
===
--- 

[Lldb-commits] [PATCH] D115178: Unify libstdcpp and libcxx formatters for `std::optional`

2021-12-06 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 392197.
zrthxn added a comment.

Added libcxx support


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115178/new/

https://reviews.llvm.org/D115178

Files:
  lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt
  lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
  lldb/source/Plugins/Language/CPlusPlus/Generic.h
  lldb/source/Plugins/Language/CPlusPlus/GenericOptional.cpp
  lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
  lldb/source/Plugins/Language/CPlusPlus/LibCxxOptional.cpp
  lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h

Index: lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h
===
--- lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h
+++ lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h
@@ -45,6 +45,10 @@
 LibStdcppBitsetSyntheticFrontEndCreator(CXXSyntheticChildren *,
 lldb::ValueObjectSP);
 
+SyntheticChildrenFrontEnd *
+LibStdcppOptionalSyntheticFrontEndCreator(CXXSyntheticChildren *,
+  lldb::ValueObjectSP);
+
 SyntheticChildrenFrontEnd *
 LibStdcppVectorIteratorSyntheticFrontEndCreator(CXXSyntheticChildren *,
 lldb::ValueObjectSP);
Index: lldb/source/Plugins/Language/CPlusPlus/LibCxxOptional.cpp
===
--- lldb/source/Plugins/Language/CPlusPlus/LibCxxOptional.cpp
+++ lldb/source/Plugins/Language/CPlusPlus/LibCxxOptional.cpp
@@ -75,10 +75,10 @@
   return val_sp->Clone(ConstString("Value"));
 }
 
-SyntheticChildrenFrontEnd *
-formatters::LibcxxOptionalFrontEndCreator(CXXSyntheticChildren *,
-  lldb::ValueObjectSP valobj_sp) {
-  if (valobj_sp)
-return new OptionalFrontEnd(*valobj_sp);
-  return nullptr;
-}
+// SyntheticChildrenFrontEnd *
+// formatters::LibcxxOptionalFrontEndCreator(CXXSyntheticChildren *,
+//   lldb::ValueObjectSP valobj_sp) {
+//   if (valobj_sp)
+// return new OptionalFrontEnd(*valobj_sp);
+//   return nullptr;
+// }
Index: lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
===
--- lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
+++ lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
@@ -170,8 +170,8 @@
   lldb::ValueObjectSP);
 
 SyntheticChildrenFrontEnd *
-LibcxxOptionalFrontEndCreator(CXXSyntheticChildren *,
-  lldb::ValueObjectSP valobj_sp);
+LibcxxOptionalSyntheticFrontEndCreator(CXXSyntheticChildren *,
+   lldb::ValueObjectSP valobj_sp);
 
 SyntheticChildrenFrontEnd *
 LibcxxVariantFrontEndCreator(CXXSyntheticChildren *,
Index: lldb/source/Plugins/Language/CPlusPlus/GenericOptional.cpp
===
--- /dev/null
+++ lldb/source/Plugins/Language/CPlusPlus/GenericOptional.cpp
@@ -0,0 +1,136 @@
+//===-- GenericOptional.cpp
+---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Generic.h"
+#include "LibCxx.h"
+#include "LibStdcpp.h"
+#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
+#include "lldb/DataFormatters/FormattersHelpers.h"
+#include "lldb/Target/Target.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+namespace {
+
+class GenericOptionalFrontend : public SyntheticChildrenFrontEnd {
+public:
+  enum class StdLib {
+LibCxx,
+LibStdcpp,
+  };
+
+  GenericOptionalFrontend(ValueObject &valobj, StdLib stdlib);
+
+  size_t GetIndexOfChildWithName(ConstString name) override {
+return formatters::ExtractIndexFromString(name.GetCString());
+  }
+
+  bool MightHaveChildren() override { return true; }
+  size_t CalculateNumChildren() override { return m_has_value ? 1U : 0U; }
+
+  ValueObjectSP GetChildAtIndex(size_t idx) override;
+  bool Update() override;
+
+private:
+  bool m_has_value = false;
+  StdLib m_stdlib;
+};
+
+} // namespace
+
+GenericOptionalFrontend::GenericOptionalFrontend(ValueObject &valobj,
+ StdLib stdlib)
+: SyntheticChildrenFrontEnd(valobj), m_stdlib(stdlib) {
+  if (auto target_sp = m_backend.GetTargetSP()) {
+Update();
+  }
+}
+
+bool GenericOptionalFrontend::Update() {
+  ValueObjectSP engaged_sp;
+
+  if (m_stdlib == StdLib::LibCxx)
+engaged_sp =
+m_backend.GetChildMemberWithName(ConstString("__engaged_"), true);
+  else if (m_stdlib == StdLib::LibStdcpp)
+engaged_sp =
+m_backe

[Lldb-commits] [PATCH] D115178: Unify libstdcpp and libcxx formatters for `std::optional`

2021-12-07 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 392305.
zrthxn added a comment.

Removed old/dead code and commented out sections


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115178/new/

https://reviews.llvm.org/D115178

Files:
  lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt
  lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
  lldb/source/Plugins/Language/CPlusPlus/Generic.h
  lldb/source/Plugins/Language/CPlusPlus/GenericOptional.cpp
  lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
  lldb/source/Plugins/Language/CPlusPlus/LibCxxOptional.cpp
  lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h

Index: lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h
===
--- lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h
+++ lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h
@@ -45,6 +45,10 @@
 LibStdcppBitsetSyntheticFrontEndCreator(CXXSyntheticChildren *,
 lldb::ValueObjectSP);
 
+SyntheticChildrenFrontEnd *
+LibStdcppOptionalSyntheticFrontEndCreator(CXXSyntheticChildren *,
+  lldb::ValueObjectSP);
+
 SyntheticChildrenFrontEnd *
 LibStdcppVectorIteratorSyntheticFrontEndCreator(CXXSyntheticChildren *,
 lldb::ValueObjectSP);
Index: lldb/source/Plugins/Language/CPlusPlus/LibCxxOptional.cpp
===
--- lldb/source/Plugins/Language/CPlusPlus/LibCxxOptional.cpp
+++ /dev/null
@@ -1,84 +0,0 @@
-//===-- LibCxxOptional.cpp ===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===--===//
-
-#include "LibCxx.h"
-#include "lldb/DataFormatters/FormattersHelpers.h"
-
-using namespace lldb;
-using namespace lldb_private;
-
-namespace {
-
-class OptionalFrontEnd : public SyntheticChildrenFrontEnd {
-public:
-  OptionalFrontEnd(ValueObject &valobj) : SyntheticChildrenFrontEnd(valobj) {
-Update();
-  }
-
-  size_t GetIndexOfChildWithName(ConstString name) override {
-return formatters::ExtractIndexFromString(name.GetCString());
-  }
-
-  bool MightHaveChildren() override { return true; }
-  bool Update() override;
-  size_t CalculateNumChildren() override { return m_has_value ? 1U : 0U; }
-  ValueObjectSP GetChildAtIndex(size_t idx) override;
-
-private:
-  /// True iff the option contains a value.
-  bool m_has_value = false;
-};
-} // namespace
-
-bool OptionalFrontEnd::Update() {
-  ValueObjectSP engaged_sp(
-  m_backend.GetChildMemberWithName(ConstString("__engaged_"), true));
-
-  if (!engaged_sp)
-return false;
-
-  // __engaged_ is a bool flag and is true if the optional contains a value.
-  // Converting it to unsigned gives us a size of 1 if it contains a value
-  // and 0 if not.
-  m_has_value = engaged_sp->GetValueAsUnsigned(0) != 0;
-
-  return false;
-}
-
-ValueObjectSP OptionalFrontEnd::GetChildAtIndex(size_t idx) {
-  if (!m_has_value)
-return ValueObjectSP();
-
-  // __val_ contains the underlying value of an optional if it has one.
-  // Currently because it is part of an anonymous union GetChildMemberWithName()
-  // does not peer through and find it unless we are at the parent itself.
-  // We can obtain the parent through __engaged_.
-  ValueObjectSP val_sp(
-  m_backend.GetChildMemberWithName(ConstString("__engaged_"), true)
-  ->GetParent()
-  ->GetChildAtIndex(0, true)
-  ->GetChildMemberWithName(ConstString("__val_"), true));
-
-  if (!val_sp)
-return ValueObjectSP();
-
-  CompilerType holder_type = val_sp->GetCompilerType();
-
-  if (!holder_type)
-return ValueObjectSP();
-
-  return val_sp->Clone(ConstString("Value"));
-}
-
-SyntheticChildrenFrontEnd *
-formatters::LibcxxOptionalFrontEndCreator(CXXSyntheticChildren *,
-  lldb::ValueObjectSP valobj_sp) {
-  if (valobj_sp)
-return new OptionalFrontEnd(*valobj_sp);
-  return nullptr;
-}
Index: lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
===
--- lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
+++ lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
@@ -170,8 +170,8 @@
   lldb::ValueObjectSP);
 
 SyntheticChildrenFrontEnd *
-LibcxxOptionalFrontEndCreator(CXXSyntheticChildren *,
-  lldb::ValueObjectSP valobj_sp);
+LibcxxOptionalSyntheticFrontEndCreator(CXXSyntheticChildren *,
+   lldb::ValueObjectSP valobj_sp);
 
 SyntheticChildrenFrontEnd *
 LibcxxVariantFrontEndCreator(CXXSyntheticChildren *,
Index: lldb

[Lldb-commits] [PATCH] D115178: Unify libstdcpp and libcxx formatters for `std::optional`

2021-12-07 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 392308.
zrthxn added a comment.

Cosmetic changes and incorporate requested changes from review


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115178/new/

https://reviews.llvm.org/D115178

Files:
  lldb/source/Plugins/Language/CPlusPlus/CMakeLists.txt
  lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
  lldb/source/Plugins/Language/CPlusPlus/Generic.h
  lldb/source/Plugins/Language/CPlusPlus/GenericOptional.cpp
  lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
  lldb/source/Plugins/Language/CPlusPlus/LibCxxOptional.cpp
  lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h

Index: lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h
===
--- lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h
+++ lldb/source/Plugins/Language/CPlusPlus/LibStdcpp.h
@@ -45,6 +45,10 @@
 LibStdcppBitsetSyntheticFrontEndCreator(CXXSyntheticChildren *,
 lldb::ValueObjectSP);
 
+SyntheticChildrenFrontEnd *
+LibStdcppOptionalSyntheticFrontEndCreator(CXXSyntheticChildren *,
+  lldb::ValueObjectSP);
+
 SyntheticChildrenFrontEnd *
 LibStdcppVectorIteratorSyntheticFrontEndCreator(CXXSyntheticChildren *,
 lldb::ValueObjectSP);
Index: lldb/source/Plugins/Language/CPlusPlus/LibCxxOptional.cpp
===
--- lldb/source/Plugins/Language/CPlusPlus/LibCxxOptional.cpp
+++ /dev/null
@@ -1,84 +0,0 @@
-//===-- LibCxxOptional.cpp ===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===--===//
-
-#include "LibCxx.h"
-#include "lldb/DataFormatters/FormattersHelpers.h"
-
-using namespace lldb;
-using namespace lldb_private;
-
-namespace {
-
-class OptionalFrontEnd : public SyntheticChildrenFrontEnd {
-public:
-  OptionalFrontEnd(ValueObject &valobj) : SyntheticChildrenFrontEnd(valobj) {
-Update();
-  }
-
-  size_t GetIndexOfChildWithName(ConstString name) override {
-return formatters::ExtractIndexFromString(name.GetCString());
-  }
-
-  bool MightHaveChildren() override { return true; }
-  bool Update() override;
-  size_t CalculateNumChildren() override { return m_has_value ? 1U : 0U; }
-  ValueObjectSP GetChildAtIndex(size_t idx) override;
-
-private:
-  /// True iff the option contains a value.
-  bool m_has_value = false;
-};
-} // namespace
-
-bool OptionalFrontEnd::Update() {
-  ValueObjectSP engaged_sp(
-  m_backend.GetChildMemberWithName(ConstString("__engaged_"), true));
-
-  if (!engaged_sp)
-return false;
-
-  // __engaged_ is a bool flag and is true if the optional contains a value.
-  // Converting it to unsigned gives us a size of 1 if it contains a value
-  // and 0 if not.
-  m_has_value = engaged_sp->GetValueAsUnsigned(0) != 0;
-
-  return false;
-}
-
-ValueObjectSP OptionalFrontEnd::GetChildAtIndex(size_t idx) {
-  if (!m_has_value)
-return ValueObjectSP();
-
-  // __val_ contains the underlying value of an optional if it has one.
-  // Currently because it is part of an anonymous union GetChildMemberWithName()
-  // does not peer through and find it unless we are at the parent itself.
-  // We can obtain the parent through __engaged_.
-  ValueObjectSP val_sp(
-  m_backend.GetChildMemberWithName(ConstString("__engaged_"), true)
-  ->GetParent()
-  ->GetChildAtIndex(0, true)
-  ->GetChildMemberWithName(ConstString("__val_"), true));
-
-  if (!val_sp)
-return ValueObjectSP();
-
-  CompilerType holder_type = val_sp->GetCompilerType();
-
-  if (!holder_type)
-return ValueObjectSP();
-
-  return val_sp->Clone(ConstString("Value"));
-}
-
-SyntheticChildrenFrontEnd *
-formatters::LibcxxOptionalFrontEndCreator(CXXSyntheticChildren *,
-  lldb::ValueObjectSP valobj_sp) {
-  if (valobj_sp)
-return new OptionalFrontEnd(*valobj_sp);
-  return nullptr;
-}
Index: lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
===
--- lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
+++ lldb/source/Plugins/Language/CPlusPlus/LibCxx.h
@@ -170,8 +170,8 @@
   lldb::ValueObjectSP);
 
 SyntheticChildrenFrontEnd *
-LibcxxOptionalFrontEndCreator(CXXSyntheticChildren *,
-  lldb::ValueObjectSP valobj_sp);
+LibcxxOptionalSyntheticFrontEndCreator(CXXSyntheticChildren *,
+   lldb::ValueObjectSP valobj_sp);
 
 SyntheticChildrenFrontEnd *
 LibcxxVariantFrontEndCreator(CXXSyntheticChildren 

[Lldb-commits] [PATCH] D121935: added intel-pt build instructions for lldb

2022-03-17 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn created this revision.
Herald added a project: All.
zrthxn requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D121935

Files:
  lldb/source/Plugins/Trace/intel-pt/README.md


Index: lldb/source/Plugins/Trace/intel-pt/README.md
===
--- /dev/null
+++ lldb/source/Plugins/Trace/intel-pt/README.md
@@ -0,0 +1,90 @@
+## Instructions to build LLDB with Intel® PT Support
+
+### Before you get started
+
+1. Confirm that your processor is an Intel® x86_64 based processor.
+
+2. Check for the existance of this particular file on your Linux system
+```bash
+cat /sys/bus/event_source/devices/intel_pt/type
+```
+The output of this should be a number greater than ...?
+
+### Build Instructions
+
+1. Clone [LibIPT library](https://github.com/intel/libipt). The Intel® 
Processor Trace Decoder Library is Intel's® reference implementation for 
decoding Intel® PT. 
+```bash
+git clone g...@github.com:intel/libipt.git
+```
+
+2. Clone [the LLVM Project](https://github.com/llvm/llvm-project) in its 
entirety from Github or Phabricator.
+```bash
+git clone g...@github.com:llvm/llvm-project.git
+```
+
+3. Create build directories for both of these projects, outside the 
repositories.
+```bash
+mkdir lldb-build libipt-build
+```
+
+4. Start by building the libipt library first. LibIPT uses CMake and Make so 
*make* sure you have those. From here we will use `` and 
`` to represent the full paths of these directories. Change this 
in the following commands to the full path for your system.
+```bash
+cmake -S libipt -B 
+```
+Run make in the LibIPT build directory
+```bash
+cd 
+make
+cd ..
+```
+This will generate a few files in the `/lib` and 
`/libipt/include` directories.
+
+5. Now it is time to build LLDB and link the LibIPT build and header files 
into it. Start by configuring all the build files for LLDB from LLVM.
+```bash
+cmake \
+  -B lldb-build \
+  -G Ninja \
+-DLLVM_ENABLE_PROJECTS="clang;libcxx;lldb;libcxxabi" \
+-DLLDB_BUILD_INTEL_PT=ON \
+-DLIBIPT_INCLUDE_PATH="/libipt/include" \
+-DLIBIPT_LIBRARY_PATH="/lib" \
+  -S llvm-project/llvm
+```
+If this step goes right, you should see no errors and **no warnings for 
unused variables.**
+We have now configured the LLDB build to be built with Ninja. Make sure 
you have `ninja` installed.
+
+6. Build LLDB. This will take a while.
+```bash
+cd 
+ninja lldb lldb-server
+```
+
+7. When the build completes after a few decades, test it by running the 
version you just built.
+```bash
+./bin/lldb 
+```
+Inside LLDB, set a breakpoint and start the process runnning
+```lldb
+(lldb) break main
+(lldb) run
+```
+Now, if everything went well, when you run the following command, you 
should see no errors.
+```lldb
+(lldb) process trace start
+```
+You should also be able to see the instructions for any line.
+```lldb
+(lldb) next
+(lldb) thread trace dump instructions
+```
+
+Congratulations, you can now trace away!
+
+### References
+
+Some details about how Intel® Processor Trace works are in [this great 
blog 
post](https://engineering.fb.com/2021/04/27/developer-tools/reverse-debugging/).
+Other things about how LLDB works with this are included in [the RFC 
document](https://docs.google.com/document/d/1cOVTGp1sL_HBXjP9eB7qjVtDNr5xnuZvUUtv43G5eVI/edit#)
 for this.
+
+- https://easyperf.net/blog/2019/08/23/Intel-Processor-Trace
+- 
https://easyperf.net/blog/2019/08/30/Intel-PT-part2#appendix-how-to-build-gdb-with-intel-pt-support
+- https://reviews.llvm.org/D91679
\ No newline at end of file


Index: lldb/source/Plugins/Trace/intel-pt/README.md
===
--- /dev/null
+++ lldb/source/Plugins/Trace/intel-pt/README.md
@@ -0,0 +1,90 @@
+## Instructions to build LLDB with Intel® PT Support
+
+### Before you get started
+
+1. Confirm that your processor is an Intel® x86_64 based processor.
+
+2. Check for the existance of this particular file on your Linux system
+```bash
+cat /sys/bus/event_source/devices/intel_pt/type
+```
+The output of this should be a number greater than ...?
+
+### Build Instructions
+
+1. Clone [LibIPT library](https://github.com/intel/libipt). The Intel® Processor Trace Decoder Library is Intel's® reference implementation for decoding Intel® PT. 
+```bash
+git clone g...@github.com:intel/libipt.git
+```
+
+2. Clone [the LLVM Project](https://github.com/llvm/llvm-project) in its entirety from Github or Phabricator.
+```bash
+git clone g...@github.com:llvm/llvm-project.git
+```
+
+3. Create build directories for bo

[Lldb-commits] [PATCH] D121935: added intel-pt build instructions for lldb

2022-03-17 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn added a comment.

In D121935#3389954 , @JDevlieghere 
wrote:

> Can we include this in the `docs` folder instead? Having READMEs spread 
> throughout the sources isn't going to be particularly discoverable.

Sure can. Should I also use RST instead of MD? Also which particular folder in 
docs?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121935/new/

https://reviews.llvm.org/D121935

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D121942: Switched to RST

2022-03-17 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn created this revision.
Herald added a project: All.
zrthxn requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D121942

Files:
  lldb/docs/intel_pt.rst
  lldb/source/Plugins/Trace/intel-pt/README.md

Index: lldb/source/Plugins/Trace/intel-pt/README.md
===
--- lldb/source/Plugins/Trace/intel-pt/README.md
+++ /dev/null
@@ -1,90 +0,0 @@
-## Instructions to build LLDB with Intel® PT Support
-
-### Before you get started
-
-1. Confirm that your processor is an Intel® x86_64 based processor.
-
-2. Check for the existance of this particular file on your Linux system
-```bash
-cat /sys/bus/event_source/devices/intel_pt/type
-```
-The output of this should be a number greater than ...?
-
-### Build Instructions
-
-1. Clone [LibIPT library](https://github.com/intel/libipt). The Intel® Processor Trace Decoder Library is Intel's® reference implementation for decoding Intel® PT. 
-```bash
-git clone g...@github.com:intel/libipt.git
-```
-
-2. Clone [the LLVM Project](https://github.com/llvm/llvm-project) in its entirety from Github or Phabricator.
-```bash
-git clone g...@github.com:llvm/llvm-project.git
-```
-
-3. Create build directories for both of these projects, outside the repositories.
-```bash
-mkdir lldb-build libipt-build
-```
-
-4. Start by building the libipt library first. LibIPT uses CMake and Make so *make* sure you have those. From here we will use `` and `` to represent the full paths of these directories. Change this in the following commands to the full path for your system.
-```bash
-cmake -S libipt -B 
-```
-Run make in the LibIPT build directory
-```bash
-cd 
-make
-cd ..
-```
-This will generate a few files in the `/lib` and `/libipt/include` directories.
-
-5. Now it is time to build LLDB and link the LibIPT build and header files into it. Start by configuring all the build files for LLDB from LLVM.
-```bash
-cmake \
-  -B lldb-build \
-  -G Ninja \
--DLLVM_ENABLE_PROJECTS="clang;libcxx;lldb;libcxxabi" \
--DLLDB_BUILD_INTEL_PT=ON \
--DLIBIPT_INCLUDE_PATH="/libipt/include" \
--DLIBIPT_LIBRARY_PATH="/lib" \
-  -S llvm-project/llvm
-```
-If this step goes right, you should see no errors and **no warnings for unused variables.**
-We have now configured the LLDB build to be built with Ninja. Make sure you have `ninja` installed.
-
-6. Build LLDB. This will take a while.
-```bash
-cd 
-ninja lldb lldb-server
-```
-
-7. When the build completes after a few decades, test it by running the version you just built.
-```bash
-./bin/lldb 
-```
-Inside LLDB, set a breakpoint and start the process runnning
-```lldb
-(lldb) break main
-(lldb) run
-```
-Now, if everything went well, when you run the following command, you should see no errors.
-```lldb
-(lldb) process trace start
-```
-You should also be able to see the instructions for any line.
-```lldb
-(lldb) next
-(lldb) thread trace dump instructions
-```
-
-Congratulations, you can now trace away!
-
-### References
-
-Some details about how Intel® Processor Trace works are in [this great blog post](https://engineering.fb.com/2021/04/27/developer-tools/reverse-debugging/).
-Other things about how LLDB works with this are included in [the RFC document](https://docs.google.com/document/d/1cOVTGp1sL_HBXjP9eB7qjVtDNr5xnuZvUUtv43G5eVI/edit#) for this.
-
-- https://easyperf.net/blog/2019/08/23/Intel-Processor-Trace
-- https://easyperf.net/blog/2019/08/30/Intel-PT-part2#appendix-how-to-build-gdb-with-intel-pt-support
-- https://reviews.llvm.org/D91679
\ No newline at end of file
Index: lldb/docs/intel_pt.rst
===
--- /dev/null
+++ lldb/docs/intel_pt.rst
@@ -0,0 +1,210 @@
+Tracing with Intel Processor Trace (Intel PT)
+=
+
+.. contents::
+  :local:
+
+Intel PT is a technology available in modern Intel CPUs that allows efficient 
+tracing of all the instructions executed by a process. LLDB can collect traces and dump 
+them using its symbolication stack. You can read more here https://easyperf.net/blog/2019/08/23/Intel-Processor-Trace.
+
+Prerequisites
+-
+
+Confirm that your CPU supports Intel PT (see https://www.intel.com/content/www/us/en/support/articles/56730/processors.html) 
+and that your operating system is Linux.
+
+Check for the existance of this particular file on your Linux system
+:: 
+
+  $ cat /sys/bus/event_source/devices/intel_pt/type
+
+The output should be a number. Otherwise, try upgrading your kernel.
+
+
+Build Instructions
+--
+
+Clone and build the lo

[Lldb-commits] [PATCH] D121935: added intel-pt build instructions for lldb

2022-03-17 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 416287.
zrthxn added a comment.

Switched to RST


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121935/new/

https://reviews.llvm.org/D121935

Files:
  lldb/docs/intel_pt.rst

Index: lldb/docs/intel_pt.rst
===
--- /dev/null
+++ lldb/docs/intel_pt.rst
@@ -0,0 +1,210 @@
+Tracing with Intel Processor Trace (Intel PT)
+=
+
+.. contents::
+  :local:
+
+Intel PT is a technology available in modern Intel CPUs that allows efficient 
+tracing of all the instructions executed by a process. LLDB can collect traces and dump 
+them using its symbolication stack. You can read more here https://easyperf.net/blog/2019/08/23/Intel-Processor-Trace.
+
+Prerequisites
+-
+
+Confirm that your CPU supports Intel PT (see https://www.intel.com/content/www/us/en/support/articles/56730/processors.html) 
+and that your operating system is Linux.
+
+Check for the existance of this particular file on your Linux system
+:: 
+
+  $ cat /sys/bus/event_source/devices/intel_pt/type
+
+The output should be a number. Otherwise, try upgrading your kernel.
+
+
+Build Instructions
+--
+
+Clone and build the low level Intel PT decoder library [LibIPT library](https://github.com/intel/libipt).
+:: 
+
+  $ git clone g...@github.com:intel/libipt.git
+  $ mkdir libipt-build
+  $ cmake -S libipt -B libipt-build
+  $ cd libipt-build
+  $ make
+
+This will generate a few files in the `/lib` and `/libipt/include` directories.
+
+Configure and build LLDB with Intel PT support
+:: 
+
+  $ cmake \
+  -DLLDB_BUILD_INTEL_PT=ON \
+  -DLIBIPT_INCLUDE_PATH="/libipt/include" \
+  -DLIBIPT_LIBRARY_PATH="/lib" \
+  ... other common configuration parameters 
+
+If this step goes correctly, you should see no errors and **no warnings for unused variables.**
+:: 
+
+  $ cd lldb-build 
+  $ ninja lldb lldb-server # if using Ninja
+
+How to Use
+--
+
+When you are debugging a process, you can turn on intel-pt tracing, which will “record” all the instructions 
+that the process will execute. After turning it on, you can continue debugging, and at any breakpoint, 
+you can inspect the instruction list.
+ 
+For example:
+::
+  lldb 
+  > b main
+  > run
+  > process trace start # start tracing on all threads, including future ones
+  # keep debugging until you hit a breakpoint
+  
+  > thread trace dump instructions
+  # this should output something like
+
+  thread #2: tid = 2861133, total instructions = 5305673
+libc.so.6`__GI___libc_read + 45 at read.c:25:1
+  [4962255] 0x7fffeb64c63dsubq   $0x10, %rsp
+  [4962256] 0x7fffeb64c641movq   %rdi, -0x18(%rbp)
+libc.so.6`__GI___libc_read + 53 [inlined] __libc_read at read.c:26:10
+  [4962257] 0x7fffeb64c645callq  0x7fffeb66b640; __libc_enable_asynccancel
+libc.so.6`__libc_enable_asynccancel
+  [4962258] 0x7fffeb66b640movl   %fs:0x308, %eax
+libc.so.6`__libc_enable_asynccancel + 8
+  [4962259] 0x7fffeb66b648movl   %eax, %r11d
+
+  # you can keep pressing ENTER to see more and more instructions
+ 
+The number between brackets is the instruction index, and by default the current thread will be picked.
+ 
+Configuring the trace size
+--
+ 
+The CPU stores the instruction list in a compressed format in a ring buffer, which keeps the latest information. By default, LLDB uses a buffer of 4KB per thread, but you can change it by running. The size must be a power of 2 and at least 4KB.
+::
+  thread trace start all -s 
+ 
+For reference, a 1MB trace buffer can easily store around 5M instructions.
+ 
+Printing more instructions
+--
+ 
+If you want to dump more instructions at a time, you can run
+::
+  thread trace dump instructions -c 
+ 
+Printing the instructions of another thread
+---
+
+By default the current thread will be picked when dumping instructions, but you can do
+:: 
+  thread trace dump instructions <#thread index>
+  #e.g.
+  thread trace dump instructions 8
+ 
+to select another thread.
+ 
+Crash Analysis
+--
+ 
+What if you are debugging + tracing a process that crashes? Then you can just do
+::
+  thread trace dump instructions
+ 
+To inspect how it crashed! There's nothing special that you need to do. For example
+ 
+* thread #1, name = 'a.out', stop reason = signal SIGFPE: integer divide by zero
+frame #0: 0x004009f1 a.out`main at main.cpp:8:14
+  6   int x;
+  7   cin >> x;
+-> 8   cout << 12 / x << endl;
+  9   return 0;
+  10  }
+(lldb) thread trace dump instructions -c 5
+thread #1: tid = 604302, total instructions = 8388
+  libstdc++.so.6`std::istream::operator>>(int&) + 181
+[8383] 0x000

[Lldb-commits] [PATCH] D121935: added intel-pt build instructions for lldb

2022-03-18 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 416506.
zrthxn added a comment.
Herald added a subscriber: arphaman.

Referenced IPT doc from main page


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121935/new/

https://reviews.llvm.org/D121935

Files:
  lldb/docs/index.rst
  lldb/docs/use/intel_pt.rst

Index: lldb/docs/use/intel_pt.rst
===
--- /dev/null
+++ lldb/docs/use/intel_pt.rst
@@ -0,0 +1,209 @@
+Tracing with Intel Processor Trace (Intel PT)
+=
+
+.. contents::
+  :local:
+
+Intel PT is a technology available in modern Intel CPUs that allows efficient 
+tracing of all the instructions executed by a process. LLDB can collect traces and dump 
+them using its symbolication stack. You can read more here https://easyperf.net/blog/2019/08/23/Intel-Processor-Trace.
+
+Prerequisites
+-
+
+Confirm that your CPU supports Intel PT (see https://www.intel.com/content/www/us/en/support/articles/56730/processors.html) 
+and that your operating system is Linux.
+
+Check for the existence of this particular file on your Linux system
+:: 
+
+  $ cat /sys/bus/event_source/devices/intel_pt/type
+
+The output should be a number. Otherwise, try upgrading your kernel.
+
+
+Build Instructions
+--
+
+Clone and build the low level Intel PT decoder library [LibIPT library](https://github.com/intel/libipt).
+:: 
+
+  $ git clone g...@github.com:intel/libipt.git
+  $ mkdir libipt-build
+  $ cmake -S libipt -B libipt-build
+  $ cd libipt-build
+  $ make
+
+This will generate a few files in the `/lib` and `/libipt/include` directories.
+
+Configure and build LLDB with Intel PT support
+:: 
+
+  $ cmake \
+  -DLLDB_BUILD_INTEL_PT=ON \
+  -DLIBIPT_INCLUDE_PATH="/libipt/include" \
+  -DLIBIPT_LIBRARY_PATH="/lib" \
+  ... other common configuration parameters 
+
+:: 
+
+  $ cd  && ninja lldb lldb-server # if using Ninja
+
+
+How to Use
+--
+
+When you are debugging a process, you can turn on intel-pt tracing, which will “record” all the instructions 
+that the process will execute. After turning it on, you can continue debugging, and at any breakpoint, 
+you can inspect the instruction list.
+ 
+For example:
+::
+  lldb 
+  > b main
+  > run
+  > process trace start # start tracing on all threads, including future ones
+  # keep debugging until you hit a breakpoint
+  
+  > thread trace dump instructions
+  # this should output something like
+
+  thread #2: tid = 2861133, total instructions = 5305673
+libc.so.6`__GI___libc_read + 45 at read.c:25:1
+  [4962255] 0x7fffeb64c63dsubq   $0x10, %rsp
+  [4962256] 0x7fffeb64c641movq   %rdi, -0x18(%rbp)
+libc.so.6`__GI___libc_read + 53 [inlined] __libc_read at read.c:26:10
+  [4962257] 0x7fffeb64c645callq  0x7fffeb66b640; __libc_enable_asynccancel
+libc.so.6`__libc_enable_asynccancel
+  [4962258] 0x7fffeb66b640movl   %fs:0x308, %eax
+libc.so.6`__libc_enable_asynccancel + 8
+  [4962259] 0x7fffeb66b648movl   %eax, %r11d
+
+  # you can keep pressing ENTER to see more and more instructions
+ 
+The number between brackets is the instruction index, and by default the current thread will be picked.
+ 
+Configuring the trace size
+--
+ 
+The CPU stores the instruction list in a compressed format in a ring buffer, which keeps the latest information. By default, LLDB uses a buffer of 4KB per thread, but you can change it by running. The size must be a power of 2 and at least 4KB.
+::
+  thread trace start all -s 
+ 
+For reference, a 1MB trace buffer can easily store around 5M instructions.
+ 
+Printing more instructions
+--
+ 
+If you want to dump more instructions at a time, you can run
+::
+  thread trace dump instructions -c 
+ 
+Printing the instructions of another thread
+---
+
+By default the current thread will be picked when dumping instructions, but you can do
+:: 
+  thread trace dump instructions <#thread index>
+  #e.g.
+  thread trace dump instructions 8
+ 
+to select another thread.
+ 
+Crash Analysis
+--
+ 
+What if you are debugging + tracing a process that crashes? Then you can just do
+::
+  thread trace dump instructions
+ 
+To inspect how it crashed! There's nothing special that you need to do. For example
+:: 
+* thread #1, name = 'a.out', stop reason = signal SIGFPE: integer divide by zero
+frame #0: 0x004009f1 a.out`main at main.cpp:8:14
+  6   int x;
+  7   cin >> x;
+   -> 8   cout << 12 / x << endl;
+  9   return 0;
+  10  }
+(lldb) thread trace dump instructions -c 5
+thread #1: tid = 604302, total instructions = 8388
+  libstdc++.so.6`std::istream::operator>>(int&) + 181
+[8383] 0x77b41665p

[Lldb-commits] [PATCH] D121935: added intel-pt build instructions for lldb

2022-03-18 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn added a comment.

Here is the screenshot as well as the HTML file
The screenshot doesn't have the CSS theming...

F22478403: 
_home_alisamar_Desktop_lldb-build_tools_lldb_docs_html_use_intel_pt.html.png 


F22478406: intel_pt.html 


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121935/new/

https://reviews.llvm.org/D121935

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D121935: added intel-pt build instructions for lldb

2022-03-18 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 416532.
zrthxn added a comment.

Reflow to 80 cols


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121935/new/

https://reviews.llvm.org/D121935

Files:
  lldb/docs/index.rst
  lldb/docs/use/intel_pt.rst

Index: lldb/docs/use/intel_pt.rst
===
--- /dev/null
+++ lldb/docs/use/intel_pt.rst
@@ -0,0 +1,232 @@
+Tracing with Intel Processor Trace (Intel PT)
+=
+
+.. contents::
+  :local:
+
+Intel PT is a technology available in modern Intel CPUs that allows efficient 
+tracing of all the instructions executed by a process. 
+LLDB can collect traces and dump them using its symbolication stack. 
+You can read more here 
+https://easyperf.net/blog/2019/08/23/Intel-Processor-Trace.
+
+Prerequisites
+-
+
+Confirm that your CPU supports Intel PT 
+(see https://www.intel.com/content/www/us/en/support/articles/56730/processors.html) 
+and that your operating system is Linux.
+
+Check for the existence of this particular file on your Linux system
+:: 
+
+  $ cat /sys/bus/event_source/devices/intel_pt/type
+
+The output should be a number. Otherwise, try upgrading your kernel.
+
+
+Build Instructions
+--
+
+Clone and build the low level Intel PT 
+decoder library [LibIPT library](https://github.com/intel/libipt).
+:: 
+
+  $ git clone g...@github.com:intel/libipt.git
+  $ mkdir libipt-build
+  $ cmake -S libipt -B libipt-build
+  $ cd libipt-build
+  $ make
+
+This will generate a few files in the `/lib` 
+and `/libipt/include` directories.
+
+Configure and build LLDB with Intel PT support
+:: 
+
+  $ cmake \
+  -DLLDB_BUILD_INTEL_PT=ON \
+  -DLIBIPT_INCLUDE_PATH="/libipt/include" \
+  -DLIBIPT_LIBRARY_PATH="/lib" \
+  ... other common configuration parameters 
+
+:: 
+
+  $ cd  && ninja lldb lldb-server # if using Ninja
+
+
+How to Use
+--
+
+When you are debugging a process, you can turn on intel-pt tracing, 
+which will “record” all the instructions that the process will execute. 
+After turning it on, you can continue debugging, and at any breakpoint, 
+you can inspect the instruction list.
+ 
+For example:
+::
+  lldb 
+  > b main
+  > run
+  > process trace start # start tracing on all threads, including future ones
+  # keep debugging until you hit a breakpoint
+  
+  > thread trace dump instructions
+  # this should output something like
+
+  thread #2: tid = 2861133, total instructions = 5305673
+libc.so.6`__GI___libc_read + 45 at read.c:25:1
+  [4962255] 0x7fffeb64c63dsubq   $0x10, %rsp
+  [4962256] 0x7fffeb64c641movq   %rdi, -0x18(%rbp)
+libc.so.6`__GI___libc_read + 53 [inlined] __libc_read at read.c:26:10
+  [4962257] 0x7fffeb64c645callq  0x7fffeb66b640; __libc_enable_asynccancel
+libc.so.6`__libc_enable_asynccancel
+  [4962258] 0x7fffeb66b640movl   %fs:0x308, %eax
+libc.so.6`__libc_enable_asynccancel + 8
+  [4962259] 0x7fffeb66b648movl   %eax, %r11d
+
+  # you can keep pressing ENTER to see more and more instructions
+ 
+The number between brackets is the instruction index, 
+and by default the current thread will be picked.
+ 
+Configuring the trace size
+--
+ 
+The CPU stores the instruction list in a compressed format in a ring buffer, 
+which keeps the latest information. 
+By default, LLDB uses a buffer of 4KB per thread, 
+but you can change it by running. 
+The size must be a power of 2 and at least 4KB.
+::
+  thread trace start all -s 
+ 
+For reference, a 1MB trace buffer can easily store around 5M instructions.
+ 
+Printing more instructions
+--
+ 
+If you want to dump more instructions at a time, you can run
+::
+  thread trace dump instructions -c 
+ 
+Printing the instructions of another thread
+---
+
+By default the current thread will be picked when dumping instructions, 
+but you can do
+:: 
+  thread trace dump instructions <#thread index>
+  #e.g.
+  thread trace dump instructions 8
+ 
+to select another thread.
+ 
+Crash Analysis
+--
+ 
+What if you are debugging + tracing a process that crashes? 
+Then you can just do
+::
+  thread trace dump instructions
+ 
+To inspect how it crashed! There's nothing special that you need to do. 
+For example
+:: 
+* thread #1, name = 'a.out', stop reason = signal SIGFPE: integer divide by zero
+frame #0: 0x004009f1 a.out`main at main.cpp:8:14
+  6   int x;
+  7   cin >> x;
+   -> 8   cout << 12 / x << endl;
+  9   return 0;
+  10  }
+(lldb) thread trace dump instructions -c 5
+thread #1: tid = 604302, total instructions = 8388
+  libstdc++.so.6`std::istream::operator>>(int&) + 181
+[8383] 0x77b41665popq   %rbp
+[8384

[Lldb-commits] [PATCH] D122073: [docs] Fixed minor ordering issue

2022-03-19 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn created this revision.
zrthxn added a reviewer: wallace.
Herald added a subscriber: arphaman.
Herald added a project: All.
zrthxn requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122073

Files:
  lldb/docs/index.rst
  lldb/docs/use/intel_pt.rst


Index: lldb/docs/use/intel_pt.rst
===
--- lldb/docs/use/intel_pt.rst
+++ lldb/docs/use/intel_pt.rst
@@ -1,5 +1,5 @@
-Tracing with Intel Processor Trace (Intel PT)
-=
+Tracing with Intel Processor Trace
+==
 
 .. contents::
   :local:
Index: lldb/docs/index.rst
===
--- lldb/docs/index.rst
+++ lldb/docs/index.rst
@@ -137,9 +137,9 @@
use/python-reference
use/remote
use/qemu-testing
+   use/intel_pt
use/troubleshooting
use/links
-   use/intel_pt
 
 .. toctree::
:hidden:


Index: lldb/docs/use/intel_pt.rst
===
--- lldb/docs/use/intel_pt.rst
+++ lldb/docs/use/intel_pt.rst
@@ -1,5 +1,5 @@
-Tracing with Intel Processor Trace (Intel PT)
-=
+Tracing with Intel Processor Trace
+==
 
 .. contents::
   :local:
Index: lldb/docs/index.rst
===
--- lldb/docs/index.rst
+++ lldb/docs/index.rst
@@ -137,9 +137,9 @@
use/python-reference
use/remote
use/qemu-testing
+   use/intel_pt
use/troubleshooting
use/links
-   use/intel_pt
 
 .. toctree::
:hidden:
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122073: [docs] Fixed minor ordering issue

2022-03-19 Thread Alisamar Husain via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG1bcc28b884ff: [docs] Fixed minor ordering issue (authored by 
zrthxn).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122073/new/

https://reviews.llvm.org/D122073

Files:
  lldb/docs/index.rst
  lldb/docs/use/intel_pt.rst


Index: lldb/docs/use/intel_pt.rst
===
--- lldb/docs/use/intel_pt.rst
+++ lldb/docs/use/intel_pt.rst
@@ -1,5 +1,5 @@
-Tracing with Intel Processor Trace (Intel PT)
-=
+Tracing with Intel Processor Trace
+==
 
 .. contents::
   :local:
Index: lldb/docs/index.rst
===
--- lldb/docs/index.rst
+++ lldb/docs/index.rst
@@ -137,9 +137,9 @@
use/python-reference
use/remote
use/qemu-testing
+   use/intel_pt
use/troubleshooting
use/links
-   use/intel_pt
 
 .. toctree::
:hidden:


Index: lldb/docs/use/intel_pt.rst
===
--- lldb/docs/use/intel_pt.rst
+++ lldb/docs/use/intel_pt.rst
@@ -1,5 +1,5 @@
-Tracing with Intel Processor Trace (Intel PT)
-=
+Tracing with Intel Processor Trace
+==
 
 .. contents::
   :local:
Index: lldb/docs/index.rst
===
--- lldb/docs/index.rst
+++ lldb/docs/index.rst
@@ -137,9 +137,9 @@
use/python-reference
use/remote
use/qemu-testing
+   use/intel_pt
use/troubleshooting
use/links
-   use/intel_pt
 
 .. toctree::
:hidden:
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122076: [trace][intelpt] Instruction count in trace info

2022-03-19 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn created this revision.
zrthxn added a reviewer: wallace.
Herald added a project: All.
zrthxn requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Added a line to `thread trace dump info` results which shows total number of 
instructions executed until now.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122076

Files:
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,4 +38,5 @@
 substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes'''])
+  Raw trace size: 4096 bytes
+  Total number of instructions: 25'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -105,12 +105,15 @@
 
 void TraceIntelPT::DumpTraceInfo(Thread &thread, Stream &s, bool verbose) {
   Optional raw_size = GetRawTraceSize(thread);
+  size_t inst_size = (*Decode(thread)).GetInstructions().size();
   s.Printf("\nthread #%u: tid = %" PRIu64, thread.GetIndexID(), 
thread.GetID());
   if (!raw_size) {
 s.Printf(", not traced\n");
 return;
   }
-  s.Printf("\n  Raw trace size: %zu bytes\n", *raw_size);
+  s.Printf("\n");
+  s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
+  s.Printf("  Total number of instructions: %zu\n", inst_size);
   return;
 }
 


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,4 +38,5 @@
 substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes'''])
+  Raw trace size: 4096 bytes
+  Total number of instructions: 25'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -105,12 +105,15 @@
 
 void TraceIntelPT::DumpTraceInfo(Thread &thread, Stream &s, bool verbose) {
   Optional raw_size = GetRawTraceSize(thread);
+  size_t inst_size = (*Decode(thread)).GetInstructions().size();
   s.Printf("\nthread #%u: tid = %" PRIu64, thread.GetIndexID(), thread.GetID());
   if (!raw_size) {
 s.Printf(", not traced\n");
 return;
   }
-  s.Printf("\n  Raw trace size: %zu bytes\n", *raw_size);
+  s.Printf("\n");
+  s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
+  s.Printf("  Total number of instructions: %zu\n", inst_size);
   return;
 }
 
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122076: [trace][intelpt] Instruction count in trace info

2022-03-19 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 416736.
zrthxn added a comment.

Fix failing test


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122076/new/

https://reviews.llvm.org/D122076

Files:
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,4 +38,5 @@
 substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes'''])
+  Raw trace size: 4096 bytes
+  Total number of instructions: 21'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -105,12 +105,15 @@
 
 void TraceIntelPT::DumpTraceInfo(Thread &thread, Stream &s, bool verbose) {
   Optional raw_size = GetRawTraceSize(thread);
+  size_t inst_size = (*Decode(thread)).GetInstructions().size();
   s.Printf("\nthread #%u: tid = %" PRIu64, thread.GetIndexID(), 
thread.GetID());
   if (!raw_size) {
 s.Printf(", not traced\n");
 return;
   }
-  s.Printf("\n  Raw trace size: %zu bytes\n", *raw_size);
+  s.Printf("\n");
+  s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
+  s.Printf("  Total number of instructions: %zu\n", inst_size);
   return;
 }
 


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,4 +38,5 @@
 substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes'''])
+  Raw trace size: 4096 bytes
+  Total number of instructions: 21'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -105,12 +105,15 @@
 
 void TraceIntelPT::DumpTraceInfo(Thread &thread, Stream &s, bool verbose) {
   Optional raw_size = GetRawTraceSize(thread);
+  size_t inst_size = (*Decode(thread)).GetInstructions().size();
   s.Printf("\nthread #%u: tid = %" PRIu64, thread.GetIndexID(), thread.GetID());
   if (!raw_size) {
 s.Printf(", not traced\n");
 return;
   }
-  s.Printf("\n  Raw trace size: %zu bytes\n", *raw_size);
+  s.Printf("\n");
+  s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
+  s.Printf("  Total number of instructions: %zu\n", inst_size);
   return;
 }
 
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122076: [trace][intelpt] Instruction count in trace info

2022-03-19 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 416756.
zrthxn added a comment.

Using arrow op and made it inline. I think creating a variable looks a bit 
better but this is fine too.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122076/new/

https://reviews.llvm.org/D122076

Files:
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,4 +38,5 @@
 substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes'''])
+  Raw trace size: 4096 bytes
+  Total number of instructions: 21'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -110,7 +110,10 @@
 s.Printf(", not traced\n");
 return;
   }
-  s.Printf("\n  Raw trace size: %zu bytes\n", *raw_size);
+  s.Printf("\n");
+  s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
+  s.Printf("  Total number of instructions: %zu\n", 
+Decode(thread)->GetInstructions().size());
   return;
 }
 


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,4 +38,5 @@
 substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes'''])
+  Raw trace size: 4096 bytes
+  Total number of instructions: 21'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -110,7 +110,10 @@
 s.Printf(", not traced\n");
 return;
   }
-  s.Printf("\n  Raw trace size: %zu bytes\n", *raw_size);
+  s.Printf("\n");
+  s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
+  s.Printf("  Total number of instructions: %zu\n", 
+Decode(thread)->GetInstructions().size());
   return;
 }
 
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122076: [trace][intelpt] Instruction count in trace info

2022-03-19 Thread Alisamar Husain via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG8271220a9981: [trace][intelpt] Instruction count in trace 
info (authored by zrthxn).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122076/new/

https://reviews.llvm.org/D122076

Files:
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,4 +38,5 @@
 substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes'''])
+  Raw trace size: 4096 bytes
+  Total number of instructions: 21'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -110,7 +110,10 @@
 s.Printf(", not traced\n");
 return;
   }
-  s.Printf("\n  Raw trace size: %zu bytes\n", *raw_size);
+  s.Printf("\n");
+  s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
+  s.Printf("  Total number of instructions: %zu\n", 
+Decode(thread)->GetInstructions().size());
   return;
 }
 


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,4 +38,5 @@
 substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes'''])
+  Raw trace size: 4096 bytes
+  Total number of instructions: 21'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -110,7 +110,10 @@
 s.Printf(", not traced\n");
 return;
   }
-  s.Printf("\n  Raw trace size: %zu bytes\n", *raw_size);
+  s.Printf("\n");
+  s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
+  s.Printf("  Total number of instructions: %zu\n", 
+Decode(thread)->GetInstructions().size());
   return;
 }
 
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122093: [trace][intelpt] Added total memory usage by decoded trace

2022-03-20 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn created this revision.
zrthxn added reviewers: wallace, jj10306.
Herald added a project: All.
zrthxn requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

This fails currently but the basics are there


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122093

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp


Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -114,6 +114,9 @@
   s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
   s.Printf("  Total number of instructions: %zu\n", 
 Decode(thread)->GetInstructions().size());
+
+  size_t memsize = Decode(thread)->GetMemoryUsage();
+  s.Printf("  Total memory usage: %zu\n", memsize);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -82,6 +82,12 @@
   /// an error.
   lldb::addr_t GetLoadAddress() const;
 
+  /// Get the size in bytes of decoded instruction
+  ///
+  /// \return
+  ///   The size of the decoded instruction
+  static size_t GetMemoryUsage();
+
   /// \return
   /// An \a llvm::Error object if this class corresponds to an Error, or an
   /// \a llvm::Error::success otherwise.
@@ -150,6 +156,12 @@
   ///   The size of the trace.
   size_t GetRawTraceSize() const;
 
+  /// Get the size in bytes of decoded Intel PT trace
+  ///
+  /// \return
+  ///   The size of the decoded traces.
+  size_t GetMemoryUsage() const;
+
 private:
   lldb::ThreadSP m_thread_sp;
   std::vector m_instructions;
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
@@ -48,6 +48,14 @@
 
 lldb::addr_t IntelPTInstruction::GetLoadAddress() const { return m_pt_insn.ip; 
}
 
+size_t IntelPTInstruction::GetMemoryUsage() {
+  return (
+sizeof(m_pt_insn) + 
+sizeof(uint64_t) + sizeof(bool) 
+// sizeof(m_error)
+  );
+}
+
 Optional IntelPTInstruction::GetTimestampCounter() const {
   return m_timestamp;
 }
@@ -116,3 +124,13 @@
 lldb::TraceCursorUP DecodedThread::GetCursor() {
   return std::make_unique(m_thread_sp, shared_from_this());
 }
+
+size_t DecodedThread::GetMemoryUsage() const {
+  size_t total = 0;
+  total += sizeof(m_raw_trace_size);
+
+  for (size_t i = 0; i < m_instructions.size(); i++)
+total += m_instructions[i].GetMemoryUsage();
+  
+  return total;
+}


Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -114,6 +114,9 @@
   s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
   s.Printf("  Total number of instructions: %zu\n", 
 Decode(thread)->GetInstructions().size());
+
+  size_t memsize = Decode(thread)->GetMemoryUsage();
+  s.Printf("  Total memory usage: %zu\n", memsize);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -82,6 +82,12 @@
   /// an error.
   lldb::addr_t GetLoadAddress() const;
 
+  /// Get the size in bytes of decoded instruction
+  ///
+  /// \return
+  ///   The size of the decoded instruction
+  static size_t GetMemoryUsage();
+
   /// \return
   /// An \a llvm::Error object if this class corresponds to an Error, or an
   /// \a llvm::Error::success otherwise.
@@ -150,6 +156,12 @@
   ///   The size of the trace.
   size_t GetRawTraceSize() const;
 
+  /// Get the size in bytes of decoded Intel PT trace
+  ///
+  /// \return
+  ///   The size of the decoded traces.
+  size_t GetMemoryUsage() const;
+
 private:
   lldb::ThreadSP m_thread_sp;
   std::vector m_instructions;
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
@@ -48,6 +48,14 @@
 
 lldb::addr_t IntelPTInstruction::GetLoadAddress() const { return m_pt_insn.ip; }
 
+size_t IntelPTInstruction::GetMemoryUsage() {
+  return (
+sizeof(m_pt_insn) + 
+sizeof(uint64_t) + sizeof(bool) 
+// sizeof(m_error)
+  );
+}
+
 Optional IntelPTInstruction::GetTimestampCounter() const {
   return

[Lldb-commits] [PATCH] D122093: [trace][intelpt] Added total memory usage by decoded trace

2022-03-20 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 416764.
zrthxn added a comment.

Clean up, works fine now


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122093/new/

https://reviews.llvm.org/D122093

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp


Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -114,6 +114,8 @@
   s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
   s.Printf("  Total number of instructions: %zu\n", 
 Decode(thread)->GetInstructions().size());
+  s.Printf("  Total memory usage: %zu bytes\n", 
+Decode(thread)->GetMemoryUsage());
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -81,6 +81,19 @@
   /// The instruction pointer address, or \a LLDB_INVALID_ADDRESS if it is
   /// an error.
   lldb::addr_t GetLoadAddress() const;
+  
+  /// Get the size in bytes of decoded instruction.
+  /// This method is static because the size of \a IntelPTInstruction is not 
dynamic
+  ///
+  /// \return
+  ///   The size of the decoded instruction
+  static size_t GetMemoryUsage() {
+return (
+  sizeof(m_pt_insn) + 
+  sizeof(uint64_t) + 
+  sizeof(bool)
+);
+  }
 
   /// \return
   /// An \a llvm::Error object if this class corresponds to an Error, or an
@@ -150,6 +163,12 @@
   ///   The size of the trace.
   size_t GetRawTraceSize() const;
 
+  /// Get the size in bytes of decoded Intel PT trace
+  ///
+  /// \return
+  ///   The size of the decoded traces.
+  size_t GetMemoryUsage() const;
+
 private:
   lldb::ThreadSP m_thread_sp;
   std::vector m_instructions;
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
@@ -116,3 +116,12 @@
 lldb::TraceCursorUP DecodedThread::GetCursor() {
   return std::make_unique(m_thread_sp, shared_from_this());
 }
+
+size_t DecodedThread::GetMemoryUsage() const {
+  size_t total = sizeof(m_raw_trace_size);
+
+  for (const IntelPTInstruction &ins : m_instructions)
+total += ins.GetMemoryUsage();
+
+  return total;
+}


Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -114,6 +114,8 @@
   s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
   s.Printf("  Total number of instructions: %zu\n", 
 Decode(thread)->GetInstructions().size());
+  s.Printf("  Total memory usage: %zu bytes\n", 
+Decode(thread)->GetMemoryUsage());
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -81,6 +81,19 @@
   /// The instruction pointer address, or \a LLDB_INVALID_ADDRESS if it is
   /// an error.
   lldb::addr_t GetLoadAddress() const;
+  
+  /// Get the size in bytes of decoded instruction.
+  /// This method is static because the size of \a IntelPTInstruction is not dynamic
+  ///
+  /// \return
+  ///   The size of the decoded instruction
+  static size_t GetMemoryUsage() {
+return (
+  sizeof(m_pt_insn) + 
+  sizeof(uint64_t) + 
+  sizeof(bool)
+);
+  }
 
   /// \return
   /// An \a llvm::Error object if this class corresponds to an Error, or an
@@ -150,6 +163,12 @@
   ///   The size of the trace.
   size_t GetRawTraceSize() const;
 
+  /// Get the size in bytes of decoded Intel PT trace
+  ///
+  /// \return
+  ///   The size of the decoded traces.
+  size_t GetMemoryUsage() const;
+
 private:
   lldb::ThreadSP m_thread_sp;
   std::vector m_instructions;
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
@@ -116,3 +116,12 @@
 lldb::TraceCursorUP DecodedThread::GetCursor() {
   return std::make_unique(m_thread_sp, shared_from_this());
 }
+
+size_t DecodedThread::GetMemoryUsage() const {
+  size_t total = sizeof(m_raw_trace_size);
+
+  for (const IntelPTInstruction &ins : m_instructions)
+total += ins.GetMemoryUsage();
+
+  return total;
+}
___
ll

[Lldb-commits] [PATCH] D122093: [trace][intelpt] Added total memory usage by decoded trace

2022-03-20 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 416765.
zrthxn added a comment.

Fixed trace info test for this


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122093/new/

https://reviews.llvm.org/D122093

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -39,4 +39,5 @@
 
 thread #1: tid = 3842849
   Raw trace size: 4096 bytes
-  Total number of instructions: 21'''])
+  Total number of instructions: 21
+  Total memory usage: 1037 bytes'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -114,6 +114,8 @@
   s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
   s.Printf("  Total number of instructions: %zu\n", 
 Decode(thread)->GetInstructions().size());
+  s.Printf("  Total memory usage: %zu bytes\n", 
+Decode(thread)->GetMemoryUsage());
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -81,6 +81,19 @@
   /// The instruction pointer address, or \a LLDB_INVALID_ADDRESS if it is
   /// an error.
   lldb::addr_t GetLoadAddress() const;
+  
+  /// Get the size in bytes of decoded instruction.
+  /// This method is static because the size of \a IntelPTInstruction is not 
dynamic
+  ///
+  /// \return
+  ///   The size of the decoded instruction
+  static size_t GetMemoryUsage() {
+return (
+  sizeof(m_pt_insn) + 
+  sizeof(uint64_t) + 
+  sizeof(bool)
+);
+  }
 
   /// \return
   /// An \a llvm::Error object if this class corresponds to an Error, or an
@@ -150,6 +163,12 @@
   ///   The size of the trace.
   size_t GetRawTraceSize() const;
 
+  /// Get the size in bytes of decoded Intel PT trace
+  ///
+  /// \return
+  ///   The size of the decoded traces.
+  size_t GetMemoryUsage() const;
+
 private:
   lldb::ThreadSP m_thread_sp;
   std::vector m_instructions;
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
@@ -116,3 +116,12 @@
 lldb::TraceCursorUP DecodedThread::GetCursor() {
   return std::make_unique(m_thread_sp, shared_from_this());
 }
+
+size_t DecodedThread::GetMemoryUsage() const {
+  size_t total = sizeof(m_raw_trace_size);
+
+  for (const IntelPTInstruction &ins : m_instructions)
+total += ins.GetMemoryUsage();
+
+  return total;
+}


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -39,4 +39,5 @@
 
 thread #1: tid = 3842849
   Raw trace size: 4096 bytes
-  Total number of instructions: 21'''])
+  Total number of instructions: 21
+  Total memory usage: 1037 bytes'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -114,6 +114,8 @@
   s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
   s.Printf("  Total number of instructions: %zu\n", 
 Decode(thread)->GetInstructions().size());
+  s.Printf("  Total memory usage: %zu bytes\n", 
+Decode(thread)->GetMemoryUsage());
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -81,6 +81,19 @@
   /// The instruction pointer address, or \a LLDB_INVALID_ADDRESS if it is
   /// an error.
   lldb::addr_t GetLoadAddress() const;
+  
+  /// Get the size in bytes of decoded instruction.
+  /// This method is static because the size of \a IntelPTInstruction is not dynamic
+  ///
+  /// \return
+  ///   The size of the decoded instruction
+  static size_t GetMemoryUsage() {
+return (
+  sizeof(m_pt_insn) + 
+  sizeof(uint64_t) + 
+  sizeof(bool)
+);
+  }
 
   /// \return
   /// An \a llvm::Error object if this class corresponds to an Error, or an
@@ -150,6 +163,12 @@
   ///   The size of the

[Lldb-commits] [PATCH] D122093: [trace][intelpt] Added total memory usage by decoded trace

2022-03-20 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 416797.
zrthxn added a comment.

Changed to KiB and fixed how caclulation is done


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122093/new/

https://reviews.llvm.org/D122093

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,5 +38,6 @@
 substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes
-  Total number of instructions: 21'''])
+  Raw trace size: 4 KiB
+  Total number of instructions: 21
+  Total approximate memory usage: 5.38 KiB'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -105,15 +105,18 @@
 
 void TraceIntelPT::DumpTraceInfo(Thread &thread, Stream &s, bool verbose) {
   Optional raw_size = GetRawTraceSize(thread);
+  size_t mem_used = Decode(thread)->CalculateApproximateMemoryUsage();
   s.Printf("\nthread #%u: tid = %" PRIu64, thread.GetIndexID(), 
thread.GetID());
   if (!raw_size) {
 s.Printf(", not traced\n");
 return;
   }
   s.Printf("\n");
-  s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
-  s.Printf("  Total number of instructions: %zu\n", 
-Decode(thread)->GetInstructions().size());
+  s.Printf("  Raw trace size: %zu KiB\n", *raw_size / 1024);
+  s.Printf("  Total number of instructions: %zu\n",
+   Decode(thread)->GetInstructions().size());
+  s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
+   (float)mem_used / 1024);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -81,6 +81,11 @@
   /// The instruction pointer address, or \a LLDB_INVALID_ADDRESS if it is
   /// an error.
   lldb::addr_t GetLoadAddress() const;
+  
+  /// Get the size in bytes of non-error instance of this class
+  static size_t GetNonErrorMemoryUsage() {
+return sizeof(IntelPTInstruction);
+  }
 
   /// \return
   /// An \a llvm::Error object if this class corresponds to an Error, or an
@@ -112,6 +117,8 @@
   IntelPTInstruction(const IntelPTInstruction &other) = delete;
   const IntelPTInstruction &operator=(const IntelPTInstruction &other) = 
delete;
 
+  // When adding new members to this class, make sure to update 
+  // IntelPTInstruction:: GetNonErrorMemoryUsage() if needed.
   pt_insn m_pt_insn;
   llvm::Optional m_timestamp;
   std::unique_ptr m_error;
@@ -150,7 +157,13 @@
   ///   The size of the trace.
   size_t GetRawTraceSize() const;
 
+  /// The approximate size in bytes used by this instance, 
+  /// including all the already decoded instructions.
+  size_t CalculateApproximateMemoryUsage() const;
+
 private:
+  /// When adding new members to this class, make sure 
+  /// to update \a CalculateApproximateMemoryUsage() accordingly.
   lldb::ThreadSP m_thread_sp;
   std::vector m_instructions;
   size_t m_raw_trace_size;
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
@@ -116,3 +116,14 @@
 lldb::TraceCursorUP DecodedThread::GetCursor() {
   return std::make_unique(m_thread_sp, shared_from_this());
 }
+
+size_t DecodedThread::CalculateApproximateMemoryUsage() const {
+  size_t non_err_instruction_count = 0;
+  for (const IntelPTInstruction &insn : m_instructions)
+if (!insn.IsError())
+  non_err_instruction_count++;
+
+  return m_raw_trace_size
++ IntelPTInstruction::GetNonErrorMemoryUsage() * non_err_instruction_count
++ sizeof(DecodedThread);
+}


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,5 +38,6 @@
 substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes
-  Total number of instructions: 21'''])
+  Raw trace size: 4 KiB
+  Total number of instructions: 21
+  Total approximate memory usage: 5.38 KiB'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/

[Lldb-commits] [PATCH] D122093: [trace][intelpt] Added total memory usage by decoded trace

2022-03-20 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn marked 4 inline comments as done.
zrthxn added inline comments.



Comment at: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp:119
+  s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
+   (float)mem_used / 1024);
   return;

wallace wrote:
> use double instead of float. double has more precision
But since we're showing only two decimal places, which gets rounded up, the 
precision eventually gets thrown away... doesn't it?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122093/new/

https://reviews.llvm.org/D122093

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122093: [trace][intelpt] Added total memory usage by decoded trace

2022-03-20 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 416825.
zrthxn marked an inline comment as done.
zrthxn added a comment.

Cosmetic and minor changes


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122093/new/

https://reviews.llvm.org/D122093

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,5 +38,6 @@
 substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes
-  Total number of instructions: 21'''])
+  Raw trace size: 4 KiB
+  Total number of instructions: 21
+  Total approximate memory usage: 5.38 KiB'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -111,9 +111,13 @@
 return;
   }
   s.Printf("\n");
-  s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
-  s.Printf("  Total number of instructions: %zu\n", 
-Decode(thread)->GetInstructions().size());
+
+  size_t mem_used = Decode(thread)->CalculateApproximateMemoryUsage();
+  s.Printf("  Raw trace size: %zu KiB\n", *raw_size / 1024);
+  s.Printf("  Total number of instructions: %zu\n",
+   Decode(thread)->GetInstructions().size());
+  s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
+   (double)mem_used / 1024);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -81,6 +81,9 @@
   /// The instruction pointer address, or \a LLDB_INVALID_ADDRESS if it is
   /// an error.
   lldb::addr_t GetLoadAddress() const;
+  
+  /// Get the size in bytes of a non-error instance of this class
+  size_t GetNonErrorMemoryUsage() const;
 
   /// \return
   /// An \a llvm::Error object if this class corresponds to an Error, or an
@@ -112,6 +115,8 @@
   IntelPTInstruction(const IntelPTInstruction &other) = delete;
   const IntelPTInstruction &operator=(const IntelPTInstruction &other) = 
delete;
 
+  // When adding new members to this class, make sure to update 
+  // IntelPTInstruction::GetNonErrorMemoryUsage() if needed.
   pt_insn m_pt_insn;
   llvm::Optional m_timestamp;
   std::unique_ptr m_error;
@@ -150,7 +155,13 @@
   ///   The size of the trace.
   size_t GetRawTraceSize() const;
 
+  /// The approximate size in bytes used by this instance, 
+  /// including all the already decoded instructions.
+  size_t CalculateApproximateMemoryUsage() const;
+
 private:
+  /// When adding new members to this class, make sure 
+  /// to update \a CalculateApproximateMemoryUsage() accordingly.
   lldb::ThreadSP m_thread_sp;
   std::vector m_instructions;
   size_t m_raw_trace_size;
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
@@ -48,6 +48,8 @@
 
 lldb::addr_t IntelPTInstruction::GetLoadAddress() const { return m_pt_insn.ip; 
}
 
+size_t IntelPTInstruction::GetNonErrorMemoryUsage() const { return 
sizeof(IntelPTInstruction); }
+
 Optional IntelPTInstruction::GetTimestampCounter() const {
   return m_timestamp;
 }
@@ -116,3 +118,14 @@
 lldb::TraceCursorUP DecodedThread::GetCursor() {
   return std::make_unique(m_thread_sp, shared_from_this());
 }
+
+size_t DecodedThread::CalculateApproximateMemoryUsage() const {
+  size_t non_err_instruction_count = 0;
+  for (const IntelPTInstruction &insn : m_instructions)
+if (!insn.IsError())
+  non_err_instruction_count++;
+
+  return m_raw_trace_size
++ IntelPTInstruction::GetNonErrorMemoryUsage() * non_err_instruction_count
++ sizeof(DecodedThread);
+}


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,5 +38,6 @@
 substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes
-  Total number of instructions: 21'''])
+  Raw trace size: 4 KiB
+  Total number of instructions: 21
+  Total approximate memory usage: 5.38 KiB'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugi

[Lldb-commits] [PATCH] D122093: [trace][intelpt] Added total memory usage by decoded trace

2022-03-20 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 416826.
zrthxn added a comment.

Ingore errored instructions


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122093/new/

https://reviews.llvm.org/D122093

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,5 +38,6 @@
 substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes
-  Total number of instructions: 21'''])
+  Raw trace size: 4 KiB
+  Total number of instructions: 21
+  Total approximate memory usage: 5.38 KiB'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -111,9 +111,13 @@
 return;
   }
   s.Printf("\n");
-  s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
-  s.Printf("  Total number of instructions: %zu\n", 
-Decode(thread)->GetInstructions().size());
+
+  size_t mem_used = Decode(thread)->CalculateApproximateMemoryUsage();
+  s.Printf("  Raw trace size: %zu KiB\n", *raw_size / 1024);
+  s.Printf("  Total number of instructions: %zu\n",
+   Decode(thread)->GetInstructions().size());
+  s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
+   (double)mem_used / 1024);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -81,6 +81,9 @@
   /// The instruction pointer address, or \a LLDB_INVALID_ADDRESS if it is
   /// an error.
   lldb::addr_t GetLoadAddress() const;
+  
+  /// Get the size in bytes of a non-error instance of this class
+  static size_t GetNonErrorMemoryUsage();
 
   /// \return
   /// An \a llvm::Error object if this class corresponds to an Error, or an
@@ -112,6 +115,8 @@
   IntelPTInstruction(const IntelPTInstruction &other) = delete;
   const IntelPTInstruction &operator=(const IntelPTInstruction &other) = 
delete;
 
+  // When adding new members to this class, make sure to update 
+  // IntelPTInstruction::GetNonErrorMemoryUsage() if needed.
   pt_insn m_pt_insn;
   llvm::Optional m_timestamp;
   std::unique_ptr m_error;
@@ -150,7 +155,13 @@
   ///   The size of the trace.
   size_t GetRawTraceSize() const;
 
+  /// The approximate size in bytes used by this instance, 
+  /// including all the already decoded instructions.
+  size_t CalculateApproximateMemoryUsage() const;
+
 private:
+  /// When adding new members to this class, make sure 
+  /// to update \a CalculateApproximateMemoryUsage() accordingly.
   lldb::ThreadSP m_thread_sp;
   std::vector m_instructions;
   size_t m_raw_trace_size;
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
@@ -48,6 +48,8 @@
 
 lldb::addr_t IntelPTInstruction::GetLoadAddress() const { return m_pt_insn.ip; 
}
 
+size_t IntelPTInstruction::GetNonErrorMemoryUsage() { return 
sizeof(IntelPTInstruction); }
+
 Optional IntelPTInstruction::GetTimestampCounter() const {
   return m_timestamp;
 }
@@ -116,3 +118,9 @@
 lldb::TraceCursorUP DecodedThread::GetCursor() {
   return std::make_unique(m_thread_sp, shared_from_this());
 }
+
+size_t DecodedThread::CalculateApproximateMemoryUsage() const {
+  return m_raw_trace_size
++ IntelPTInstruction::GetNonErrorMemoryUsage() * m_instructions.size()
++ sizeof(DecodedThread);
+}


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,5 +38,6 @@
 substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes
-  Total number of instructions: 21'''])
+  Raw trace size: 4 KiB
+  Total number of instructions: 21
+  Total approximate memory usage: 5.38 KiB'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -111,9 +111,13 @@
 return;
   }
   s.Printf("\n");
-  s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
-  s.P

[Lldb-commits] [PATCH] D122093: [trace][intelpt] Added total memory usage by decoded trace

2022-03-21 Thread Alisamar Husain via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG37a466dd72b0: [trace][intelpt] Added total memory usage by 
decoded trace (authored by zrthxn).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122093/new/

https://reviews.llvm.org/D122093

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,5 +38,6 @@
 substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes
-  Total number of instructions: 21'''])
+  Raw trace size: 4 KiB
+  Total number of instructions: 21
+  Total approximate memory usage: 5.38 KiB'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -111,9 +111,13 @@
 return;
   }
   s.Printf("\n");
-  s.Printf("  Raw trace size: %zu bytes\n", *raw_size);
-  s.Printf("  Total number of instructions: %zu\n", 
-Decode(thread)->GetInstructions().size());
+
+  size_t mem_used = Decode(thread)->CalculateApproximateMemoryUsage();
+  s.Printf("  Raw trace size: %zu KiB\n", *raw_size / 1024);
+  s.Printf("  Total number of instructions: %zu\n",
+   Decode(thread)->GetInstructions().size());
+  s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
+   (double)mem_used / 1024);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -81,6 +81,9 @@
   /// The instruction pointer address, or \a LLDB_INVALID_ADDRESS if it is
   /// an error.
   lldb::addr_t GetLoadAddress() const;
+  
+  /// Get the size in bytes of a non-error instance of this class
+  static size_t GetNonErrorMemoryUsage();
 
   /// \return
   /// An \a llvm::Error object if this class corresponds to an Error, or an
@@ -112,6 +115,8 @@
   IntelPTInstruction(const IntelPTInstruction &other) = delete;
   const IntelPTInstruction &operator=(const IntelPTInstruction &other) = 
delete;
 
+  // When adding new members to this class, make sure to update 
+  // IntelPTInstruction::GetNonErrorMemoryUsage() if needed.
   pt_insn m_pt_insn;
   llvm::Optional m_timestamp;
   std::unique_ptr m_error;
@@ -150,7 +155,13 @@
   ///   The size of the trace.
   size_t GetRawTraceSize() const;
 
+  /// The approximate size in bytes used by this instance, 
+  /// including all the already decoded instructions.
+  size_t CalculateApproximateMemoryUsage() const;
+
 private:
+  /// When adding new members to this class, make sure 
+  /// to update \a CalculateApproximateMemoryUsage() accordingly.
   lldb::ThreadSP m_thread_sp;
   std::vector m_instructions;
   size_t m_raw_trace_size;
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
@@ -48,6 +48,8 @@
 
 lldb::addr_t IntelPTInstruction::GetLoadAddress() const { return m_pt_insn.ip; 
}
 
+size_t IntelPTInstruction::GetNonErrorMemoryUsage() { return 
sizeof(IntelPTInstruction); }
+
 Optional IntelPTInstruction::GetTimestampCounter() const {
   return m_timestamp;
 }
@@ -116,3 +118,9 @@
 lldb::TraceCursorUP DecodedThread::GetCursor() {
   return std::make_unique(m_thread_sp, shared_from_this());
 }
+
+size_t DecodedThread::CalculateApproximateMemoryUsage() const {
+  return m_raw_trace_size
++ IntelPTInstruction::GetNonErrorMemoryUsage() * m_instructions.size()
++ sizeof(DecodedThread);
+}


Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -38,5 +38,6 @@
 substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes
-  Total number of instructions: 21'''])
+  Raw trace size: 4 KiB
+  Total number of instructions: 21
+  Total approximate memory usage: 5.38 KiB'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -111,9 +111,13 @@
 retur

[Lldb-commits] [PATCH] D122114: [tests][intelpt] Fix outdated trace load test

2022-03-21 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn created this revision.
zrthxn added reviewers: wallace, jj10306.
Herald added a project: All.
zrthxn requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122114

Files:
  lldb/test/API/commands/trace/TestTraceLoad.py


Index: lldb/test/API/commands/trace/TestTraceLoad.py
===
--- lldb/test/API/commands/trace/TestTraceLoad.py
+++ lldb/test/API/commands/trace/TestTraceLoad.py
@@ -36,7 +36,9 @@
 self.expect("thread trace dump info", substrs=['''Trace technology: 
intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes'''])
+  Raw trace size: 4 KiB
+  Total number of instructions: 21
+  Total approximate memory usage: 5.38 KiB'''])
 
 def testLoadInvalidTraces(self):
 src_dir = self.getSourceDir()


Index: lldb/test/API/commands/trace/TestTraceLoad.py
===
--- lldb/test/API/commands/trace/TestTraceLoad.py
+++ lldb/test/API/commands/trace/TestTraceLoad.py
@@ -36,7 +36,9 @@
 self.expect("thread trace dump info", substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes'''])
+  Raw trace size: 4 KiB
+  Total number of instructions: 21
+  Total approximate memory usage: 5.38 KiB'''])
 
 def testLoadInvalidTraces(self):
 src_dir = self.getSourceDir()
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122114: [tests][intelpt] Fix outdated trace load test

2022-03-21 Thread Alisamar Husain via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGca47011e73e6: [tests][intelpt] Fix outdated trace load test 
(authored by zrthxn).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122114/new/

https://reviews.llvm.org/D122114

Files:
  lldb/test/API/commands/trace/TestTraceLoad.py


Index: lldb/test/API/commands/trace/TestTraceLoad.py
===
--- lldb/test/API/commands/trace/TestTraceLoad.py
+++ lldb/test/API/commands/trace/TestTraceLoad.py
@@ -36,7 +36,9 @@
 self.expect("thread trace dump info", substrs=['''Trace technology: 
intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes'''])
+  Raw trace size: 4 KiB
+  Total number of instructions: 21
+  Total approximate memory usage: 5.38 KiB'''])
 
 def testLoadInvalidTraces(self):
 src_dir = self.getSourceDir()


Index: lldb/test/API/commands/trace/TestTraceLoad.py
===
--- lldb/test/API/commands/trace/TestTraceLoad.py
+++ lldb/test/API/commands/trace/TestTraceLoad.py
@@ -36,7 +36,9 @@
 self.expect("thread trace dump info", substrs=['''Trace technology: intel-pt
 
 thread #1: tid = 3842849
-  Raw trace size: 4096 bytes'''])
+  Raw trace size: 4 KiB
+  Total number of instructions: 21
+  Total approximate memory usage: 5.38 KiB'''])
 
 def testLoadInvalidTraces(self):
 src_dir = self.getSourceDir()
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122293: [wip][intelpt] Refactoring instruction decoding for flexibility

2022-03-23 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn created this revision.
zrthxn added reviewers: wallace, jj10306.
Herald added a project: All.
zrthxn requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Refactored (but not yet tested) version of the instruction decoding code


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122293

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp

Index: lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
+++ lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
@@ -104,9 +104,11 @@
 ///
 /// \return
 ///   The decoded instructions.
-static std::vector
-DecodeInstructions(pt_insn_decoder &decoder) {
-  std::vector instructions;
+static DecodedThread DecodeInstructions(pt_insn_decoder &decoder,
+const ThreadSP &thread_sp,
+const size_t raw_trace_size) {
+  DecodedThread thread = DecodedThread(
+  thread_sp, std::vector(), raw_trace_size);
 
   while (true) {
 int errcode = FindNextSynchronizationPoint(decoder);
@@ -114,7 +116,8 @@
   break;
 
 if (errcode < 0) {
-  instructions.emplace_back(make_error(errcode));
+  thread.AppendError(make_error(errcode));
+  // instructions.emplace_back(make_error(errcode));
   break;
 }
 
@@ -123,7 +126,7 @@
 while (true) {
   errcode = ProcessPTEvents(decoder, errcode);
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode));
+thread.AppendError(make_error(errcode));
 break;
   }
   pt_insn insn;
@@ -133,7 +136,7 @@
 break;
 
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode, insn.ip));
+thread.AppendError(make_error(errcode, insn.ip));
 break;
   }
 
@@ -142,22 +145,21 @@
   if (time_error == -pte_invalid) {
 // This happens if we invoke the pt_insn_time method incorrectly,
 // but the instruction is good though.
-instructions.emplace_back(
-make_error(time_error, insn.ip));
-instructions.emplace_back(insn);
+thread.AppendError(make_error(time_error, insn.ip));
+thread.AppendInstructions(insn);
 break;
   }
   if (time_error == -pte_no_time) {
 // We simply don't have time information, i.e. None of TSC, MTC or CYC
 // was enabled.
-instructions.emplace_back(insn);
+thread.AppendInstructions(insn);
   } else {
-instructions.emplace_back(insn, time);
+thread.AppendInstructions(insn, time);
   }
 }
   }
 
-  return instructions;
+  return thread;
 }
 
 /// Callback used by libipt for reading the process memory.
@@ -176,9 +178,9 @@
   return bytes_read;
 }
 
-static Expected>
-DecodeInMemoryTrace(Process &process, TraceIntelPT &trace_intel_pt,
-MutableArrayRef buffer) {
+static Expected DecodeInMemoryTrace(
+const ThreadSP &thread_sp, Process &process, TraceIntelPT &trace_intel_pt,
+MutableArrayRef buffer, const size_t raw_trace_size) {
   Expected cpu_info = trace_intel_pt.GetCPUInfo();
   if (!cpu_info)
 return cpu_info.takeError();
@@ -203,77 +205,76 @@
   assert(errcode == 0);
   (void)errcode;
 
-  std::vector instructions = DecodeInstructions(*decoder);
+  DecodedThread decoded_thread =
+  DecodeInstructions(*decoder, thread_sp, raw_trace_size);
 
   pt_insn_free_decoder(decoder);
-  return instructions;
+  return decoded_thread.shared_from_this();
 }
+// ---
 
-static Expected>
-DecodeTraceFile(Process &process, TraceIntelPT &trace_intel_pt,
-const FileSpec &trace_file, size_t &raw_trace_size) {
-  ErrorOr> trace_or_error =
-  MemoryBuffer::getFile(trace_file.GetPath());
-  if (std::error_code err = trace_or_error.getError())
-return errorCodeToError(err);
-
-  MemoryBuffer &trace = **trace_or_error;
-  MutableArrayRef trace_data(
-  // The libipt library does not modify the trace buffer, hence the
-  // following cast is safe.
-  reinterpret_cast(const_cast(trace.getBufferStart())),
-  trace.getBufferSize());
-  raw_trace_size = trace_data.size();
-  return DecodeInMemoryTrace(process, trace_intel_pt, trace_data);
+DecodedThreadSP ThreadDecoder::Decode() {
+  if (!m_decoded_thread.hasValue())
+m_decoded_thread = DoDecode();
+  return *m_decoded_thread;
 }
 
-static Expected>
-DecodeLiveThread(Thread &thread, TraceIntelPT &trace, size_t &raw_trace_size) {
+// LiveThreadDecoder 
+
+LiveThreadDecoder::LiveThreadDecoder(Thread &thread, TraceIntelPT &trace)
+: m_thread_sp(thread.shared_from_this()), m_trace(trace) {}
+
+static Expected DecodeLiveThread(const ThreadSP &thread

[Lldb-commits] [PATCH] D122293: [wip][intelpt] Refactoring instruction decoding for flexibility

2022-03-23 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 417709.
zrthxn added a comment.

Introduced unordered map for errors in DecodedThread


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp

Index: lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
+++ lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
@@ -104,9 +104,11 @@
 ///
 /// \return
 ///   The decoded instructions.
-static std::vector
-DecodeInstructions(pt_insn_decoder &decoder) {
-  std::vector instructions;
+static DecodedThread DecodeInstructions(pt_insn_decoder &decoder,
+const ThreadSP &thread_sp,
+const size_t raw_trace_size) {
+  DecodedThread thread = DecodedThread(
+  thread_sp, std::vector(), raw_trace_size);
 
   while (true) {
 int errcode = FindNextSynchronizationPoint(decoder);
@@ -114,26 +116,27 @@
   break;
 
 if (errcode < 0) {
-  instructions.emplace_back(make_error(errcode));
+  thread.AppendError(0, make_error(errcode));
   break;
 }
 
 // We have synchronized, so we can start decoding
 // instructions and events.
+int insn_index = 0;
 while (true) {
   errcode = ProcessPTEvents(decoder, errcode);
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode));
+thread.AppendError(insn_index, make_error(errcode));
 break;
   }
-  pt_insn insn;
 
+  pt_insn insn;
   errcode = pt_insn_next(&decoder, &insn, sizeof(insn));
   if (errcode == -pte_eos)
 break;
 
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode, insn.ip));
+thread.AppendError(insn_index, make_error(errcode, insn.ip));
 break;
   }
 
@@ -142,22 +145,24 @@
   if (time_error == -pte_invalid) {
 // This happens if we invoke the pt_insn_time method incorrectly,
 // but the instruction is good though.
-instructions.emplace_back(
-make_error(time_error, insn.ip));
-instructions.emplace_back(insn);
+thread.AppendError(insn_index, make_error(time_error, insn.ip));
+thread.AppendInstruction(IntelPTInstruction(insn));
 break;
   }
+
   if (time_error == -pte_no_time) {
 // We simply don't have time information, i.e. None of TSC, MTC or CYC
 // was enabled.
-instructions.emplace_back(insn);
+thread.AppendInstruction(IntelPTInstruction(insn));
   } else {
-instructions.emplace_back(insn, time);
+thread.AppendInstruction(IntelPTInstruction(insn, time));
   }
+
+  insn_index++;
 }
   }
 
-  return instructions;
+  return thread;
 }
 
 /// Callback used by libipt for reading the process memory.
@@ -176,9 +181,9 @@
   return bytes_read;
 }
 
-static Expected>
-DecodeInMemoryTrace(Process &process, TraceIntelPT &trace_intel_pt,
-MutableArrayRef buffer) {
+static Expected DecodeInMemoryTrace(
+const ThreadSP &thread_sp, Process &process, TraceIntelPT &trace_intel_pt,
+MutableArrayRef buffer, const size_t raw_trace_size) {
   Expected cpu_info = trace_intel_pt.GetCPUInfo();
   if (!cpu_info)
 return cpu_info.takeError();
@@ -203,77 +208,76 @@
   assert(errcode == 0);
   (void)errcode;
 
-  std::vector instructions = DecodeInstructions(*decoder);
+  DecodedThread decoded_thread =
+  DecodeInstructions(*decoder, thread_sp, raw_trace_size);
 
   pt_insn_free_decoder(decoder);
-  return instructions;
+  return decoded_thread.shared_from_this();
 }
+// ---
 
-static Expected>
-DecodeTraceFile(Process &process, TraceIntelPT &trace_intel_pt,
-const FileSpec &trace_file, size_t &raw_trace_size) {
-  ErrorOr> trace_or_error =
-  MemoryBuffer::getFile(trace_file.GetPath());
-  if (std::error_code err = trace_or_error.getError())
-return errorCodeToError(err);
-
-  MemoryBuffer &trace = **trace_or_error;
-  MutableArrayRef trace_data(
-  // The libipt library does not modify the trace buffer, hence the
-  // following cast is safe.
-  reinterpret_cast(const_cast(trace.getBufferStart())),
-  trace.getBufferSize());
-  raw_trace_size = trace_data.size();
-  return DecodeInMemoryTrace(process, trace_intel_pt, trace_data);
+DecodedThreadSP ThreadDecoder::Decode() {
+  if (!m_decoded_thread.hasValue())
+m_decoded_thread = DoDecode();
+  return *m_decoded_thread;
 }
 
-static Expected>
-DecodeLiveThread(Thread &thread, TraceIntelPT &trace, size_t &raw_trace_size) {
+// LiveThreadDecoder 
+
+LiveThreadDecoder::LiveThreadDecoder(Thr

[Lldb-commits] [PATCH] D122293: [wip][intelpt] Refactoring instruction decoding for flexibility

2022-03-23 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn added inline comments.



Comment at: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h:172
   std::vector m_instructions;
+  std::vector m_errors;
+

jj10306 wrote:
> wallace wrote:
> > you need to have something like
> >   std::unordered_map m_errors;
> > 
> > that way, you'll be able to quickly look for the error associated with an 
> > instruction index. The IntelPTInstruction int his case, instead of storing 
> > the Error, can just store one bit of information has_error = true/false;
> nit: from https://llvm.org/docs/CodingStandards.html#c-standard-library 
> prefer `llvm::DenseMap` over `std::map`'s unless there's a specific reason 
> not to.
> 
> Don't forget to update `CalculateApproximateMemoryUsage()` as well! Also, 
> besides for being inline with the coding standards I linked above, using 
> `llvm::DenseMap` here has the actual advantage that it exposes its 
> approximate size via  `getMemorySize()`, whereas there is no easy way to get 
> the size of `std::map`.
DenseMap looks interesting we should try that.

Yes i will update the mem calculation and make it more accurate so some 
refactor will be needed. That'll be the next small patch once this works. 


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122293: [wip][intelpt] Refactoring instruction decoding for flexibility

2022-03-23 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn added inline comments.



Comment at: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h:152
+  ///   The errors of the trace.
+  std::unordered_map GetErrors() const;
+

jj10306 wrote:
> Return a reference here to avoid potential expensive copy when returning.
> 
> Something else to consider is if we need/want an API exposing all the entire 
> error map or if something like:
> `llvm::Error GetErrorByInstructionIndex(uint64_t insn_index) const` that 
> allows the caller to specify the key into the map would make more sense? 
> This also has the advantage that it hides the implementation detail of what 
> data type is being used under the hood to represent the error map!
> @wallace @zrthxn wdyt?
Yea I did think about that. In my opinion returning a reference would be good 
since with that you can get the size and error at each index and we don't need 
to have functions for each operation which would have long clunky names making 
the code less readable...


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122293: [wip][intelpt] Refactoring instruction decoding for flexibility

2022-03-23 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn marked 8 inline comments as done.
zrthxn added inline comments.



Comment at: lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp:149
+thread.AppendError(insn_index, make_error(time_error, 
insn.ip));
+thread.AppendInstruction(IntelPTInstruction(insn));
 break;

wallace wrote:
> This will create a copy of the IntelPTInstruction before storing it in the 
> vector. Instead, you should use the same semantics as vector::emplace_back(), 
> which uses paratemer packs/variadic templates. You can even rename Append to 
> Emplace in this case
Yea I was doing that before, the idea was to send those variadic args to 
emplace_back but that wasnt working. I introduced this to avoid having 2 
Appends since we already have 2 constructors which fulfill that requirement, 
and I can change this to std::move to avoid copies if thats a concern


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122293: [wip][intelpt] Refactoring instruction decoding for flexibility

2022-03-23 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 417832.
zrthxn added a comment.

A few changes to remove redundant things


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp

Index: lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
+++ lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
@@ -104,9 +104,11 @@
 ///
 /// \return
 ///   The decoded instructions.
-static std::vector
-DecodeInstructions(pt_insn_decoder &decoder) {
-  std::vector instructions;
+static DecodedThreadSP DecodeInstructions(pt_insn_decoder &decoder,
+const ThreadSP &thread_sp,
+const size_t raw_trace_size) {
+  DecodedThread decoded_thread = DecodedThread(
+  thread_sp, std::vector(), raw_trace_size);
 
   while (true) {
 int errcode = FindNextSynchronizationPoint(decoder);
@@ -114,7 +116,7 @@
   break;
 
 if (errcode < 0) {
-  instructions.emplace_back(make_error(errcode));
+  decoded_thread.AppendError(make_error(errcode));
   break;
 }
 
@@ -123,17 +125,17 @@
 while (true) {
   errcode = ProcessPTEvents(decoder, errcode);
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode));
+decoded_thread.AppendError(make_error(errcode));
 break;
   }
-  pt_insn insn;
 
+  pt_insn insn;
   errcode = pt_insn_next(&decoder, &insn, sizeof(insn));
   if (errcode == -pte_eos)
 break;
 
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode, insn.ip));
+decoded_thread.AppendError(make_error(errcode, insn.ip));
 break;
   }
 
@@ -142,22 +144,22 @@
   if (time_error == -pte_invalid) {
 // This happens if we invoke the pt_insn_time method incorrectly,
 // but the instruction is good though.
-instructions.emplace_back(
-make_error(time_error, insn.ip));
-instructions.emplace_back(insn);
+decoded_thread.AppendError(make_error(time_error, insn.ip));
+decoded_thread.AppendInstruction(IntelPTInstruction(insn));
 break;
   }
+
   if (time_error == -pte_no_time) {
 // We simply don't have time information, i.e. None of TSC, MTC or CYC
 // was enabled.
-instructions.emplace_back(insn);
+decoded_thread.AppendInstruction(IntelPTInstruction(insn));
   } else {
-instructions.emplace_back(insn, time);
+decoded_thread.AppendInstruction(IntelPTInstruction(insn, time));
   }
 }
   }
 
-  return instructions;
+  return std::make_shared(decoded_thread);
 }
 
 /// Callback used by libipt for reading the process memory.
@@ -176,9 +178,9 @@
   return bytes_read;
 }
 
-static Expected>
-DecodeInMemoryTrace(Process &process, TraceIntelPT &trace_intel_pt,
-MutableArrayRef buffer) {
+static Expected DecodeInMemoryTrace(
+const ThreadSP &thread_sp, Process &process, TraceIntelPT &trace_intel_pt,
+MutableArrayRef buffer) {
   Expected cpu_info = trace_intel_pt.GetCPUInfo();
   if (!cpu_info)
 return cpu_info.takeError();
@@ -203,77 +205,68 @@
   assert(errcode == 0);
   (void)errcode;
 
-  std::vector instructions = DecodeInstructions(*decoder);
+  DecodedThreadSP decoded_thread =
+  DecodeInstructions(*decoder, thread_sp, buffer.size());
 
   pt_insn_free_decoder(decoder);
-  return instructions;
+  return decoded_thread;
 }
+// ---
 
-static Expected>
-DecodeTraceFile(Process &process, TraceIntelPT &trace_intel_pt,
-const FileSpec &trace_file, size_t &raw_trace_size) {
-  ErrorOr> trace_or_error =
-  MemoryBuffer::getFile(trace_file.GetPath());
-  if (std::error_code err = trace_or_error.getError())
-return errorCodeToError(err);
-
-  MemoryBuffer &trace = **trace_or_error;
-  MutableArrayRef trace_data(
-  // The libipt library does not modify the trace buffer, hence the
-  // following cast is safe.
-  reinterpret_cast(const_cast(trace.getBufferStart())),
-  trace.getBufferSize());
-  raw_trace_size = trace_data.size();
-  return DecodeInMemoryTrace(process, trace_intel_pt, trace_data);
+DecodedThreadSP ThreadDecoder::Decode() {
+  if (!m_decoded_thread.hasValue())
+m_decoded_thread = DoDecode();
+  return *m_decoded_thread;
 }
 
-static Expected>
-DecodeLiveThread(Thread &thread, TraceIntelPT &trace, size_t &raw_trace_size) {
+// LiveThreadDecoder 
+
+LiveThreadDecoder::LiveThreadDecoder(Thread &thread, TraceIntelPT &trace)
+: m_thread_sp(thread.shared_from_this()), m_trace(trace) {}
+
+static Expected Deco

[Lldb-commits] [PATCH] D122293: [wip][intelpt] Refactoring instruction decoding for flexibility

2022-03-24 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 417969.
zrthxn added a comment.

Error gettting method


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp

Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -78,7 +78,7 @@
 }
 
 Error TraceCursorIntelPT::GetError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].ToError();
+  return m_decoded_thread_sp->GetError(m_pos);
 }
 
 lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
Index: lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
+++ lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
@@ -104,9 +104,11 @@
 ///
 /// \return
 ///   The decoded instructions.
-static std::vector
-DecodeInstructions(pt_insn_decoder &decoder) {
-  std::vector instructions;
+static DecodedThreadSP DecodeInstructions(pt_insn_decoder &decoder,
+  const ThreadSP &thread_sp,
+  const size_t raw_trace_size) {
+  DecodedThread decoded_thread = DecodedThread(
+  thread_sp, std::vector(), raw_trace_size);
 
   while (true) {
 int errcode = FindNextSynchronizationPoint(decoder);
@@ -114,7 +116,7 @@
   break;
 
 if (errcode < 0) {
-  instructions.emplace_back(make_error(errcode));
+  decoded_thread.AppendError(IntelPTError(errcode));
   break;
 }
 
@@ -123,17 +125,17 @@
 while (true) {
   errcode = ProcessPTEvents(decoder, errcode);
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode));
+decoded_thread.AppendError(IntelPTError(errcode));
 break;
   }
-  pt_insn insn;
 
+  pt_insn insn;
   errcode = pt_insn_next(&decoder, &insn, sizeof(insn));
   if (errcode == -pte_eos)
 break;
 
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode, insn.ip));
+decoded_thread.AppendError(IntelPTError(errcode, insn.ip));
 break;
   }
 
@@ -142,22 +144,22 @@
   if (time_error == -pte_invalid) {
 // This happens if we invoke the pt_insn_time method incorrectly,
 // but the instruction is good though.
-instructions.emplace_back(
-make_error(time_error, insn.ip));
-instructions.emplace_back(insn);
+decoded_thread.AppendError(IntelPTError(time_error, insn.ip));
+decoded_thread.AppendInstruction(IntelPTInstruction(insn));
 break;
   }
+
   if (time_error == -pte_no_time) {
 // We simply don't have time information, i.e. None of TSC, MTC or CYC
 // was enabled.
-instructions.emplace_back(insn);
+decoded_thread.AppendInstruction(IntelPTInstruction(insn));
   } else {
-instructions.emplace_back(insn, time);
+decoded_thread.AppendInstruction(IntelPTInstruction(insn, time));
   }
 }
   }
 
-  return instructions;
+  return decoded_thread.shared_from_this();
 }
 
 /// Callback used by libipt for reading the process memory.
@@ -176,8 +178,9 @@
   return bytes_read;
 }
 
-static Expected>
-DecodeInMemoryTrace(Process &process, TraceIntelPT &trace_intel_pt,
+static Expected
+DecodeInMemoryTrace(const ThreadSP &thread_sp, Process &process,
+TraceIntelPT &trace_intel_pt,
 MutableArrayRef buffer) {
   Expected cpu_info = trace_intel_pt.GetCPUInfo();
   if (!cpu_info)
@@ -203,77 +206,67 @@
   assert(errcode == 0);
   (void)errcode;
 
-  std::vector instructions = DecodeInstructions(*decoder);
+  DecodedThreadSP decoded_thread =
+  DecodeInstructions(*decoder, thread_sp, buffer.size());
 
   pt_insn_free_decoder(decoder);
-  return instructions;
+  return decoded_thread;
 }
+// ---
 
-static Expected>
-DecodeTraceFile(Process &process, TraceIntelPT &trace_intel_pt,
-const FileSpec &trace_file, size_t &raw_trace_size) {
-  ErrorOr> trace_or_error =
-  MemoryBuffer::getFile(trace_file.GetPath());
-  if (std::error_code err = trace_or_error.getError())
-return errorCodeToError(err);
-
-  MemoryBuffer &trace = **trace_or_error;
-  MutableArrayRef trace_data(
-  // The libipt library does not modify the trace buffer, hence the
-  // following cast is safe.
-  reinterpret_cast(const_cast(trace.getBufferStart())),
-  trace.getBufferSize());
-  raw_trace_size = trace_data.size();
-  return DecodeInMemoryTra

[Lldb-commits] [PATCH] D122293: [wip][intelpt] Refactoring instruction decoding for flexibility

2022-03-24 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 418000.
zrthxn added a comment.

Incorporate other comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp

Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -78,7 +78,7 @@
 }
 
 Error TraceCursorIntelPT::GetError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].ToError();
+  return m_decoded_thread_sp->GetErrorByInstructionIndex(m_pos);
 }
 
 lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
Index: lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
+++ lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
@@ -104,9 +104,12 @@
 ///
 /// \return
 ///   The decoded instructions.
-static std::vector
-DecodeInstructions(pt_insn_decoder &decoder) {
-  std::vector instructions;
+static DecodedThreadSP DecodeInstructions(pt_insn_decoder &decoder,
+  const ThreadSP &thread_sp,
+  const size_t raw_trace_size) {
+  DecodedThreadSP decoded_thread =
+  std::make_shared(DecodedThread(
+  thread_sp, std::vector(), raw_trace_size));
 
   while (true) {
 int errcode = FindNextSynchronizationPoint(decoder);
@@ -114,7 +117,7 @@
   break;
 
 if (errcode < 0) {
-  instructions.emplace_back(make_error(errcode));
+  decoded_thread->AppendError(IntelPTError(errcode));
   break;
 }
 
@@ -123,17 +126,17 @@
 while (true) {
   errcode = ProcessPTEvents(decoder, errcode);
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode));
+decoded_thread->AppendError(IntelPTError(errcode));
 break;
   }
-  pt_insn insn;
 
+  pt_insn insn;
   errcode = pt_insn_next(&decoder, &insn, sizeof(insn));
   if (errcode == -pte_eos)
 break;
 
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode, insn.ip));
+decoded_thread->AppendError(IntelPTError(errcode, insn.ip));
 break;
   }
 
@@ -142,22 +145,22 @@
   if (time_error == -pte_invalid) {
 // This happens if we invoke the pt_insn_time method incorrectly,
 // but the instruction is good though.
-instructions.emplace_back(
-make_error(time_error, insn.ip));
-instructions.emplace_back(insn);
+decoded_thread->AppendError(IntelPTError(time_error, insn.ip));
+decoded_thread->AppendInstruction(IntelPTInstruction(insn));
 break;
   }
+
   if (time_error == -pte_no_time) {
 // We simply don't have time information, i.e. None of TSC, MTC or CYC
 // was enabled.
-instructions.emplace_back(insn);
+decoded_thread->AppendInstruction(IntelPTInstruction(insn));
   } else {
-instructions.emplace_back(insn, time);
+decoded_thread->AppendInstruction(IntelPTInstruction(insn, time));
   }
 }
   }
 
-  return instructions;
+  return decoded_thread;
 }
 
 /// Callback used by libipt for reading the process memory.
@@ -176,8 +179,9 @@
   return bytes_read;
 }
 
-static Expected>
-DecodeInMemoryTrace(Process &process, TraceIntelPT &trace_intel_pt,
+static Expected
+DecodeInMemoryTrace(const ThreadSP &thread_sp, Process &process,
+TraceIntelPT &trace_intel_pt,
 MutableArrayRef buffer) {
   Expected cpu_info = trace_intel_pt.GetCPUInfo();
   if (!cpu_info)
@@ -203,77 +207,64 @@
   assert(errcode == 0);
   (void)errcode;
 
-  std::vector instructions = DecodeInstructions(*decoder);
+  DecodedThreadSP decoded_thread =
+  DecodeInstructions(*decoder, thread_sp, buffer.size());
 
   pt_insn_free_decoder(decoder);
-  return instructions;
+  return decoded_thread;
 }
+// ---
 
-static Expected>
-DecodeTraceFile(Process &process, TraceIntelPT &trace_intel_pt,
-const FileSpec &trace_file, size_t &raw_trace_size) {
-  ErrorOr> trace_or_error =
-  MemoryBuffer::getFile(trace_file.GetPath());
-  if (std::error_code err = trace_or_error.getError())
-return errorCodeToError(err);
-
-  MemoryBuffer &trace = **trace_or_error;
-  MutableArrayRef trace_data(
-  // The libipt library does not modify the trace buffer, hence the
-  // following cast is safe.
-  reinterpret_cast(const_cast(trace.getBufferStart())),
-  trace.getBufferSize());
-  raw_trace_size = trac

[Lldb-commits] [PATCH] D122293: [intelpt] Refactoring instruction decoding for flexibility

2022-03-25 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 418154.
zrthxn added a comment.

Resolved many runtime errors


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp

Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -78,7 +78,7 @@
 }
 
 Error TraceCursorIntelPT::GetError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].ToError();
+  return m_decoded_thread_sp->GetErrorByInstructionIndex(m_pos);
 }
 
 lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
Index: lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
+++ lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
@@ -104,9 +104,12 @@
 ///
 /// \return
 ///   The decoded instructions.
-static std::vector
-DecodeInstructions(pt_insn_decoder &decoder) {
-  std::vector instructions;
+static DecodedThreadSP DecodeInstructions(pt_insn_decoder &decoder,
+  const ThreadSP &thread_sp,
+  const size_t raw_trace_size) {
+  DecodedThreadSP decoded_thread =
+  std::make_shared(DecodedThread(
+  thread_sp, std::vector(), raw_trace_size));
 
   while (true) {
 int errcode = FindNextSynchronizationPoint(decoder);
@@ -114,7 +117,7 @@
   break;
 
 if (errcode < 0) {
-  instructions.emplace_back(make_error(errcode));
+  decoded_thread->AppendError(make_error(errcode));
   break;
 }
 
@@ -123,17 +126,17 @@
 while (true) {
   errcode = ProcessPTEvents(decoder, errcode);
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode));
+decoded_thread->AppendError(make_error(errcode));
 break;
   }
-  pt_insn insn;
 
+  pt_insn insn;
   errcode = pt_insn_next(&decoder, &insn, sizeof(insn));
   if (errcode == -pte_eos)
 break;
 
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode, insn.ip));
+decoded_thread->AppendError(make_error(errcode, insn.ip));
 break;
   }
 
@@ -142,22 +145,23 @@
   if (time_error == -pte_invalid) {
 // This happens if we invoke the pt_insn_time method incorrectly,
 // but the instruction is good though.
-instructions.emplace_back(
+decoded_thread->AppendError(
 make_error(time_error, insn.ip));
-instructions.emplace_back(insn);
+decoded_thread->AppendInstruction(IntelPTInstruction(insn));
 break;
   }
+
   if (time_error == -pte_no_time) {
 // We simply don't have time information, i.e. None of TSC, MTC or CYC
 // was enabled.
-instructions.emplace_back(insn);
+decoded_thread->AppendInstruction(IntelPTInstruction(insn));
   } else {
-instructions.emplace_back(insn, time);
+decoded_thread->AppendInstruction(IntelPTInstruction(insn, time));
   }
 }
   }
 
-  return instructions;
+  return decoded_thread;
 }
 
 /// Callback used by libipt for reading the process memory.
@@ -176,8 +180,9 @@
   return bytes_read;
 }
 
-static Expected>
-DecodeInMemoryTrace(Process &process, TraceIntelPT &trace_intel_pt,
+static Expected
+DecodeInMemoryTrace(const ThreadSP &thread_sp, Process &process,
+TraceIntelPT &trace_intel_pt,
 MutableArrayRef buffer) {
   Expected cpu_info = trace_intel_pt.GetCPUInfo();
   if (!cpu_info)
@@ -203,77 +208,74 @@
   assert(errcode == 0);
   (void)errcode;
 
-  std::vector instructions = DecodeInstructions(*decoder);
+  DecodedThreadSP decoded_thread =
+  DecodeInstructions(*decoder, thread_sp, buffer.size());
 
   pt_insn_free_decoder(decoder);
-  return instructions;
+  return decoded_thread;
 }
+// ---
 
-static Expected>
-DecodeTraceFile(Process &process, TraceIntelPT &trace_intel_pt,
-const FileSpec &trace_file, size_t &raw_trace_size) {
-  ErrorOr> trace_or_error =
-  MemoryBuffer::getFile(trace_file.GetPath());
-  if (std::error_code err = trace_or_error.getError())
-return errorCodeToError(err);
-
-  MemoryBuffer &trace = **trace_or_error;
-  MutableArrayRef trace_data(
-  // The libipt library does not modify the trace buffer, hence the
-  // following cast is safe.
-  reinterpret_cast(const_cast(trace.getBufferStart())),
-  trace.getBufferSize());
-  raw_trace_size = trace_data.size();
-  return DecodeInMemory

[Lldb-commits] [PATCH] D122293: [intelpt] Refactoring instruction decoding for flexibility

2022-03-25 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 418249.
zrthxn added a comment.

Refactor to use more templates and param packs


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp

Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -78,7 +78,7 @@
 }
 
 Error TraceCursorIntelPT::GetError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].ToError();
+  return m_decoded_thread_sp->GetErrorByInstructionIndex(m_pos);
 }
 
 lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
Index: lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
+++ lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
@@ -9,6 +9,7 @@
 
 #include "llvm/Support/MemoryBuffer.h"
 
+#include 
 #include "../common/ThreadPostMortemTrace.h"
 #include "DecodedThread.h"
 #include "TraceIntelPT.h"
@@ -104,9 +105,12 @@
 ///
 /// \return
 ///   The decoded instructions.
-static std::vector
-DecodeInstructions(pt_insn_decoder &decoder) {
-  std::vector instructions;
+static DecodedThreadSP DecodeInstructions(pt_insn_decoder &decoder,
+  const ThreadSP &thread_sp,
+  const size_t raw_trace_size) {
+  DecodedThreadSP decoded_thread =
+  std::make_shared(DecodedThread(
+  thread_sp, std::vector(), raw_trace_size));
 
   while (true) {
 int errcode = FindNextSynchronizationPoint(decoder);
@@ -114,7 +118,7 @@
   break;
 
 if (errcode < 0) {
-  instructions.emplace_back(make_error(errcode));
+  decoded_thread->AppendError(errcode);
   break;
 }
 
@@ -123,17 +127,17 @@
 while (true) {
   errcode = ProcessPTEvents(decoder, errcode);
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode));
+decoded_thread->AppendError(errcode);
 break;
   }
-  pt_insn insn;
 
+  pt_insn insn;
   errcode = pt_insn_next(&decoder, &insn, sizeof(insn));
   if (errcode == -pte_eos)
 break;
 
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode, insn.ip));
+decoded_thread->AppendError(errcode, insn.ip);
 break;
   }
 
@@ -142,22 +146,22 @@
   if (time_error == -pte_invalid) {
 // This happens if we invoke the pt_insn_time method incorrectly,
 // but the instruction is good though.
-instructions.emplace_back(
-make_error(time_error, insn.ip));
-instructions.emplace_back(insn);
+decoded_thread->AppendError(time_error, insn.ip);
+decoded_thread->AppendInstruction(insn);
 break;
   }
+
   if (time_error == -pte_no_time) {
 // We simply don't have time information, i.e. None of TSC, MTC or CYC
 // was enabled.
-instructions.emplace_back(insn);
+decoded_thread->AppendInstruction(insn);
   } else {
-instructions.emplace_back(insn, time);
+decoded_thread->AppendInstruction(insn, time);
   }
 }
   }
 
-  return instructions;
+  return decoded_thread;
 }
 
 /// Callback used by libipt for reading the process memory.
@@ -176,8 +180,9 @@
   return bytes_read;
 }
 
-static Expected>
-DecodeInMemoryTrace(Process &process, TraceIntelPT &trace_intel_pt,
+static Expected
+DecodeInMemoryTrace(const ThreadSP &thread_sp, Process &process,
+TraceIntelPT &trace_intel_pt,
 MutableArrayRef buffer) {
   Expected cpu_info = trace_intel_pt.GetCPUInfo();
   if (!cpu_info)
@@ -203,77 +208,72 @@
   assert(errcode == 0);
   (void)errcode;
 
-  std::vector instructions = DecodeInstructions(*decoder);
+  DecodedThreadSP decoded_thread =
+  DecodeInstructions(*decoder, thread_sp, buffer.size());
 
   pt_insn_free_decoder(decoder);
-  return instructions;
+  return decoded_thread;
 }
+// ---
 
-static Expected>
-DecodeTraceFile(Process &process, TraceIntelPT &trace_intel_pt,
-const FileSpec &trace_file, size_t &raw_trace_size) {
-  ErrorOr> trace_or_error =
-  MemoryBuffer::getFile(trace_file.GetPath());
-  if (std::error_code err = trace_or_error.getError())
-return errorCodeToError(err);
-
-  MemoryBuffer &trace = **trace_or_error;
-  MutableArrayRef trace_data(
-  // The libipt library does not modify the trace buffer, hence the
-  // following cast is safe.
-  reinterpret_cast(const_cast(trace

[Lldb-commits] [PATCH] D122293: [intelpt] Refactoring instruction decoding for flexibility

2022-03-25 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn marked 15 inline comments as done.
zrthxn added a comment.

Before refactor

  thread #1: tid = 37275
Raw trace size: 4 KiB
Total number of instructions: 21
Total approximate memory usage: 5.38 KiB

After refactor

  (lldb) thread trace dump info
  Trace technology: intel-pt
  
  thread #1: tid = 13690
Raw trace size: 4 KiB
Total number of instructions: 20
Total approximate memory usage: 5.34 KiB


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122293: [intelpt] Refactoring instruction decoding for flexibility

2022-03-25 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 418299.
zrthxn added a comment.

Incoporate more feedback,
Only parameter pack isnt done yet


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp

Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -78,7 +78,7 @@
 }
 
 Error TraceCursorIntelPT::GetError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].ToError();
+  return m_decoded_thread_sp->GetErrorByInstructionIndex(m_pos);
 }
 
 lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
Index: lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
+++ lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
@@ -16,6 +16,7 @@
 #include "lldb/Core/Section.h"
 #include "lldb/Target/Target.h"
 #include "lldb/Utility/StringExtractor.h"
+#include 
 
 using namespace lldb;
 using namespace lldb_private;
@@ -104,9 +105,11 @@
 ///
 /// \return
 ///   The decoded instructions.
-static std::vector
-DecodeInstructions(pt_insn_decoder &decoder) {
-  std::vector instructions;
+static DecodedThreadSP DecodeInstructions(pt_insn_decoder &decoder,
+  const ThreadSP &thread_sp,
+  const size_t raw_trace_size) {
+  DecodedThreadSP decoded_thread = std::make_shared(
+  thread_sp, std::vector(), raw_trace_size);
 
   while (true) {
 int errcode = FindNextSynchronizationPoint(decoder);
@@ -114,7 +117,7 @@
   break;
 
 if (errcode < 0) {
-  instructions.emplace_back(make_error(errcode));
+  decoded_thread->AppendError(errcode);
   break;
 }
 
@@ -123,17 +126,17 @@
 while (true) {
   errcode = ProcessPTEvents(decoder, errcode);
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode));
+decoded_thread->AppendError(errcode);
 break;
   }
-  pt_insn insn;
 
+  pt_insn insn;
   errcode = pt_insn_next(&decoder, &insn, sizeof(insn));
   if (errcode == -pte_eos)
 break;
 
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode, insn.ip));
+decoded_thread->AppendError(errcode, insn.ip);
 break;
   }
 
@@ -142,22 +145,22 @@
   if (time_error == -pte_invalid) {
 // This happens if we invoke the pt_insn_time method incorrectly,
 // but the instruction is good though.
-instructions.emplace_back(
-make_error(time_error, insn.ip));
-instructions.emplace_back(insn);
+decoded_thread->AppendError(time_error, insn.ip);
+decoded_thread->AppendInstruction(insn);
 break;
   }
+
   if (time_error == -pte_no_time) {
 // We simply don't have time information, i.e. None of TSC, MTC or CYC
 // was enabled.
-instructions.emplace_back(insn);
+decoded_thread->AppendInstruction(insn);
   } else {
-instructions.emplace_back(insn, time);
+decoded_thread->AppendInstruction(insn, time);
   }
 }
   }
 
-  return instructions;
+  return decoded_thread;
 }
 
 /// Callback used by libipt for reading the process memory.
@@ -176,8 +179,8 @@
   return bytes_read;
 }
 
-static Expected>
-DecodeInMemoryTrace(Process &process, TraceIntelPT &trace_intel_pt,
+static Expected
+DecodeInMemoryTrace(const ThreadSP &thread_sp, TraceIntelPT &trace_intel_pt,
 MutableArrayRef buffer) {
   Expected cpu_info = trace_intel_pt.GetCPUInfo();
   if (!cpu_info)
@@ -199,81 +202,71 @@
 
   pt_image *image = pt_insn_get_image(decoder);
 
-  int errcode = pt_image_set_callback(image, ReadProcessMemory, &process);
+  // ProcessSP process = thread_sp->GetProcess();
+  int errcode = pt_image_set_callback(image, ReadProcessMemory,
+  thread_sp->GetProcess().get());
   assert(errcode == 0);
   (void)errcode;
 
-  std::vector instructions = DecodeInstructions(*decoder);
+  DecodedThreadSP decoded_thread =
+  DecodeInstructions(*decoder, thread_sp, buffer.size());
 
   pt_insn_free_decoder(decoder);
-  return instructions;
+  return decoded_thread;
 }
+// ---
 
-static Expected>
-DecodeTraceFile(Process &process, TraceIntelPT &trace_intel_pt,
-const FileSpec &trace_file, size_t &raw_trace_size) {
-  ErrorOr> trace_or_error =
-  MemoryBuffer::getFile(trace_file.GetPath());
-  if (std::error_code err = 

[Lldb-commits] [PATCH] D122293: [intelpt] Refactoring instruction decoding for flexibility

2022-03-25 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 418306.
zrthxn added a comment.

Finalize diff


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp

Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -78,7 +78,7 @@
 }
 
 Error TraceCursorIntelPT::GetError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].ToError();
+  return m_decoded_thread_sp->GetErrorByInstructionIndex(m_pos);
 }
 
 lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
Index: lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
+++ lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
@@ -16,6 +16,7 @@
 #include "lldb/Core/Section.h"
 #include "lldb/Target/Target.h"
 #include "lldb/Utility/StringExtractor.h"
+#include 
 
 using namespace lldb;
 using namespace lldb_private;
@@ -104,9 +105,11 @@
 ///
 /// \return
 ///   The decoded instructions.
-static std::vector
-DecodeInstructions(pt_insn_decoder &decoder) {
-  std::vector instructions;
+static DecodedThreadSP DecodeInstructions(pt_insn_decoder &decoder,
+  const ThreadSP &thread_sp,
+  const size_t raw_trace_size) {
+  DecodedThreadSP decoded_thread = std::make_shared(
+  thread_sp, std::vector(), raw_trace_size);
 
   while (true) {
 int errcode = FindNextSynchronizationPoint(decoder);
@@ -114,7 +117,7 @@
   break;
 
 if (errcode < 0) {
-  instructions.emplace_back(make_error(errcode));
+  decoded_thread->AppendError(errcode);
   break;
 }
 
@@ -123,17 +126,17 @@
 while (true) {
   errcode = ProcessPTEvents(decoder, errcode);
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode));
+decoded_thread->AppendError(errcode);
 break;
   }
-  pt_insn insn;
 
+  pt_insn insn;
   errcode = pt_insn_next(&decoder, &insn, sizeof(insn));
   if (errcode == -pte_eos)
 break;
 
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode, insn.ip));
+decoded_thread->AppendError(errcode, insn.ip);
 break;
   }
 
@@ -142,22 +145,22 @@
   if (time_error == -pte_invalid) {
 // This happens if we invoke the pt_insn_time method incorrectly,
 // but the instruction is good though.
-instructions.emplace_back(
-make_error(time_error, insn.ip));
-instructions.emplace_back(insn);
+decoded_thread->AppendError(time_error, insn.ip);
+decoded_thread->AppendInstruction(insn);
 break;
   }
+
   if (time_error == -pte_no_time) {
 // We simply don't have time information, i.e. None of TSC, MTC or CYC
 // was enabled.
-instructions.emplace_back(insn);
+decoded_thread->AppendInstruction(insn);
   } else {
-instructions.emplace_back(insn, time);
+decoded_thread->AppendInstruction(insn, time);
   }
 }
   }
 
-  return instructions;
+  return decoded_thread;
 }
 
 /// Callback used by libipt for reading the process memory.
@@ -176,8 +179,8 @@
   return bytes_read;
 }
 
-static Expected>
-DecodeInMemoryTrace(Process &process, TraceIntelPT &trace_intel_pt,
+static Expected
+DecodeInMemoryTrace(const ThreadSP &thread_sp, TraceIntelPT &trace_intel_pt,
 MutableArrayRef buffer) {
   Expected cpu_info = trace_intel_pt.GetCPUInfo();
   if (!cpu_info)
@@ -199,81 +202,71 @@
 
   pt_image *image = pt_insn_get_image(decoder);
 
-  int errcode = pt_image_set_callback(image, ReadProcessMemory, &process);
+  // ProcessSP process = thread_sp->GetProcess();
+  int errcode = pt_image_set_callback(image, ReadProcessMemory,
+  thread_sp->GetProcess().get());
   assert(errcode == 0);
   (void)errcode;
 
-  std::vector instructions = DecodeInstructions(*decoder);
+  DecodedThreadSP decoded_thread =
+  DecodeInstructions(*decoder, thread_sp, buffer.size());
 
   pt_insn_free_decoder(decoder);
-  return instructions;
+  return decoded_thread;
 }
+// ---
 
-static Expected>
-DecodeTraceFile(Process &process, TraceIntelPT &trace_intel_pt,
-const FileSpec &trace_file, size_t &raw_trace_size) {
-  ErrorOr> trace_or_error =
-  MemoryBuffer::getFile(trace_file.GetPath());
-  if (std::error_code err = trace_or_error.getError())
-return errorCo

[Lldb-commits] [PATCH] D122293: [intelpt] Refactoring instruction decoding for flexibility

2022-03-25 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 418309.
zrthxn added a comment.

Added average memory per instruction


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp

Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -112,12 +112,15 @@
   }
   s.Printf("\n");
 
+  size_t insn_len = Decode(thread)->GetInstructions().size();
   size_t mem_used = Decode(thread)->CalculateApproximateMemoryUsage();
+  
   s.Printf("  Raw trace size: %zu KiB\n", *raw_size / 1024);
-  s.Printf("  Total number of instructions: %zu\n",
-   Decode(thread)->GetInstructions().size());
+  s.Printf("  Total number of instructions: %zu\n", insn_len);
   s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
(double)mem_used / 1024);
+  s.Printf("  Average memory usage per instruction: %0.2lf KiB\n",
+   ((double)mem_used/insn_len) / 1024);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -78,7 +78,7 @@
 }
 
 Error TraceCursorIntelPT::GetError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].ToError();
+  return m_decoded_thread_sp->GetErrorByInstructionIndex(m_pos);
 }
 
 lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
Index: lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
+++ lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
@@ -16,6 +16,7 @@
 #include "lldb/Core/Section.h"
 #include "lldb/Target/Target.h"
 #include "lldb/Utility/StringExtractor.h"
+#include 
 
 using namespace lldb;
 using namespace lldb_private;
@@ -104,9 +105,11 @@
 ///
 /// \return
 ///   The decoded instructions.
-static std::vector
-DecodeInstructions(pt_insn_decoder &decoder) {
-  std::vector instructions;
+static DecodedThreadSP DecodeInstructions(pt_insn_decoder &decoder,
+  const ThreadSP &thread_sp,
+  const size_t raw_trace_size) {
+  DecodedThreadSP decoded_thread = std::make_shared(
+  thread_sp, std::vector(), raw_trace_size);
 
   while (true) {
 int errcode = FindNextSynchronizationPoint(decoder);
@@ -114,7 +117,7 @@
   break;
 
 if (errcode < 0) {
-  instructions.emplace_back(make_error(errcode));
+  decoded_thread->AppendError(errcode);
   break;
 }
 
@@ -123,17 +126,17 @@
 while (true) {
   errcode = ProcessPTEvents(decoder, errcode);
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode));
+decoded_thread->AppendError(errcode);
 break;
   }
-  pt_insn insn;
 
+  pt_insn insn;
   errcode = pt_insn_next(&decoder, &insn, sizeof(insn));
   if (errcode == -pte_eos)
 break;
 
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode, insn.ip));
+decoded_thread->AppendError(errcode, insn.ip);
 break;
   }
 
@@ -142,22 +145,22 @@
   if (time_error == -pte_invalid) {
 // This happens if we invoke the pt_insn_time method incorrectly,
 // but the instruction is good though.
-instructions.emplace_back(
-make_error(time_error, insn.ip));
-instructions.emplace_back(insn);
+decoded_thread->AppendError(time_error, insn.ip);
+decoded_thread->AppendInstruction(insn);
 break;
   }
+
   if (time_error == -pte_no_time) {
 // We simply don't have time information, i.e. None of TSC, MTC or CYC
 // was enabled.
-instructions.emplace_back(insn);
+decoded_thread->AppendInstruction(insn);
   } else {
-instructions.emplace_back(insn, time);
+decoded_thread->AppendInstruction(insn, time);
   }
 }
   }
 
-  return instructions;
+  return decoded_thread;
 }
 
 /// Callback used by libipt for reading the process memory.
@@ -176,8 +179,8 @@
   return bytes_read;
 }
 
-static Expected>
-DecodeInMemoryTrace(Process &process, TraceIntelPT &trace_intel_pt,
+static Expected
+DecodeInMemoryTrace(const ThreadSP &thread_sp, TraceIntelPT &trace_intel_pt,
 MutableArrayRef buffer) {
   Expected cpu_info = trace_intel_pt.GetCPUInfo();
   if (!cpu_info)
@@ -1

[Lldb-commits] [PATCH] D122293: [intelpt] Refactoring instruction decoding for flexibility

2022-03-25 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn added inline comments.



Comment at: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp:77
 bool TraceCursorIntelPT::IsError() {
   return m_decoded_thread_sp->GetInstructions()[m_pos].IsError();
 }

jj10306 wrote:
> nit: should we update this to use the error map? I don't think there's a 
> significant difference performance wise, but the code would be a little 
> cleaner imo and consistent with how `GetError()` works.
That would sort of look like this I think

```
  if 
(m_decoded_thread_sp->GetErrorByInstructionIndex(m_pos).isA())
return false;
  else true;
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122293: [intelpt] Refactoring instruction decoding for flexibility

2022-03-25 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 418327.
zrthxn added a comment.

Updated tests


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py
  lldb/test/API/commands/trace/TestTraceLoad.py

Index: lldb/test/API/commands/trace/TestTraceLoad.py
===
--- lldb/test/API/commands/trace/TestTraceLoad.py
+++ lldb/test/API/commands/trace/TestTraceLoad.py
@@ -37,8 +37,9 @@
 
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
-  Total number of instructions: 21
-  Total approximate memory usage: 5.38 KiB'''])
+  Total number of instructions: 22
+  Total approximate memory usage: 6.38 KiB
+  Average memory usage per instruction: 296 bytes'''])
 
 def testLoadInvalidTraces(self):
 src_dir = self.getSourceDir()
Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -39,5 +39,6 @@
 
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
-  Total number of instructions: 21
-  Total approximate memory usage: 5.38 KiB'''])
+  Total number of instructions: 22
+  Total approximate memory usage: 6.38 KiB
+  Average memory usage per instruction: 296 bytes'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -112,12 +112,15 @@
   }
   s.Printf("\n");
 
+  size_t insn_len = Decode(thread)->GetInstructions().size();
   size_t mem_used = Decode(thread)->CalculateApproximateMemoryUsage();
+
   s.Printf("  Raw trace size: %zu KiB\n", *raw_size / 1024);
-  s.Printf("  Total number of instructions: %zu\n",
-   Decode(thread)->GetInstructions().size());
+  s.Printf("  Total number of instructions: %zu\n", insn_len);
   s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
(double)mem_used / 1024);
+  s.Printf("  Average memory usage per instruction: %zu bytes\n",
+   mem_used / insn_len);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -78,7 +78,7 @@
 }
 
 Error TraceCursorIntelPT::GetError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].ToError();
+  return m_decoded_thread_sp->GetErrorByInstructionIndex(m_pos);
 }
 
 lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
Index: lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
+++ lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
@@ -16,6 +16,7 @@
 #include "lldb/Core/Section.h"
 #include "lldb/Target/Target.h"
 #include "lldb/Utility/StringExtractor.h"
+#include 
 
 using namespace lldb;
 using namespace lldb_private;
@@ -104,9 +105,11 @@
 ///
 /// \return
 ///   The decoded instructions.
-static std::vector
-DecodeInstructions(pt_insn_decoder &decoder) {
-  std::vector instructions;
+static DecodedThreadSP DecodeInstructions(pt_insn_decoder &decoder,
+  const ThreadSP &thread_sp,
+  const size_t raw_trace_size) {
+  DecodedThreadSP decoded_thread = std::make_shared(
+  thread_sp, std::vector(), raw_trace_size);
 
   while (true) {
 int errcode = FindNextSynchronizationPoint(decoder);
@@ -114,7 +117,7 @@
   break;
 
 if (errcode < 0) {
-  instructions.emplace_back(make_error(errcode));
+  decoded_thread->AppendError(errcode);
   break;
 }
 
@@ -123,17 +126,17 @@
 while (true) {
   errcode = ProcessPTEvents(decoder, errcode);
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode));
+decoded_thread->AppendError(errcode);
 break;
   }
-  pt_insn insn;
 
+  pt_insn insn;
   errcode = pt_insn_next(&decoder, &insn, sizeof(insn));
   if (errcode == -pte_eos)
 break;
 
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode, insn.ip));
+decoded_thread->AppendError(errcode, insn.ip);
 break;
   }
 
@@ -142,22 +145,22 @@
   if (time_error == -pte_invalid) {
 // This happens if we invoke the pt_insn_time m

[Lldb-commits] [PATCH] D122293: [intelpt] Refactoring instruction decoding for flexibility

2022-03-25 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn marked 13 inline comments as done.
zrthxn added inline comments.



Comment at: lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp:25
 IntelPTError::IntelPTError(int libipt_error_code, lldb::addr_t address)
 : m_libipt_error_code(libipt_error_code), m_address(address) {
   assert(libipt_error_code < 0);

wallace wrote:
> 
This is needed at DecodedThread.h:66, not here


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122293: [intelpt] Refactoring instruction decoding for flexibility

2022-03-25 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn marked an inline comment as done.
zrthxn added a comment.

Test program info, 
(llvm-project/lldb/test/API/commands/trace/intelpt-trace/a.out)

  (lldb) thread trace dump info
  Trace technology: intel-pt
  
  thread #1: tid = 3842849
Raw trace size: 4 KiB
Total number of instructions: 22
Total approximate memory usage: 6.38 KiB
Average memory usage per instruction: 296 bytes


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122293: [intelpt] Refactoring instruction decoding for flexibility

2022-03-25 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 418376.
zrthxn added a comment.

Clean up and finalize


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122293/new/

https://reviews.llvm.org/D122293

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py
  lldb/test/API/commands/trace/TestTraceLoad.py

Index: lldb/test/API/commands/trace/TestTraceLoad.py
===
--- lldb/test/API/commands/trace/TestTraceLoad.py
+++ lldb/test/API/commands/trace/TestTraceLoad.py
@@ -37,8 +37,9 @@
 
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
-  Total number of instructions: 21
-  Total approximate memory usage: 5.38 KiB'''])
+  Total number of instructions: 22
+  Total approximate memory usage: 6.38 KiB
+  Average memory usage per instruction: 296 bytes'''])
 
 def testLoadInvalidTraces(self):
 src_dir = self.getSourceDir()
Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -39,5 +39,6 @@
 
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
-  Total number of instructions: 21
-  Total approximate memory usage: 5.38 KiB'''])
+  Total number of instructions: 22
+  Total approximate memory usage: 6.38 KiB
+  Average memory usage per instruction: 296 bytes'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -112,12 +112,15 @@
   }
   s.Printf("\n");
 
+  size_t insn_len = Decode(thread)->GetInstructions().size();
   size_t mem_used = Decode(thread)->CalculateApproximateMemoryUsage();
+
   s.Printf("  Raw trace size: %zu KiB\n", *raw_size / 1024);
-  s.Printf("  Total number of instructions: %zu\n",
-   Decode(thread)->GetInstructions().size());
+  s.Printf("  Total number of instructions: %zu\n", insn_len);
   s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
(double)mem_used / 1024);
+  s.Printf("  Average memory usage per instruction: %zu bytes\n",
+   mem_used / insn_len);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -78,7 +78,7 @@
 }
 
 Error TraceCursorIntelPT::GetError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].ToError();
+  return m_decoded_thread_sp->GetErrorByInstructionIndex(m_pos);
 }
 
 lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
Index: lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
+++ lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
@@ -16,6 +16,7 @@
 #include "lldb/Core/Section.h"
 #include "lldb/Target/Target.h"
 #include "lldb/Utility/StringExtractor.h"
+#include 
 
 using namespace lldb;
 using namespace lldb_private;
@@ -104,9 +105,11 @@
 ///
 /// \return
 ///   The decoded instructions.
-static std::vector
-DecodeInstructions(pt_insn_decoder &decoder) {
-  std::vector instructions;
+static DecodedThreadSP DecodeInstructions(pt_insn_decoder &decoder,
+  const ThreadSP &thread_sp,
+  const size_t raw_trace_size) {
+  DecodedThreadSP decoded_thread = std::make_shared(
+  thread_sp, std::vector(), raw_trace_size);
 
   while (true) {
 int errcode = FindNextSynchronizationPoint(decoder);
@@ -114,7 +117,7 @@
   break;
 
 if (errcode < 0) {
-  instructions.emplace_back(make_error(errcode));
+  decoded_thread->AppendError(errcode);
   break;
 }
 
@@ -123,17 +126,17 @@
 while (true) {
   errcode = ProcessPTEvents(decoder, errcode);
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode));
+decoded_thread->AppendError(errcode);
 break;
   }
-  pt_insn insn;
 
+  pt_insn insn;
   errcode = pt_insn_next(&decoder, &insn, sizeof(insn));
   if (errcode == -pte_eos)
 break;
 
   if (errcode < 0) {
-instructions.emplace_back(make_error(errcode, insn.ip));
+decoded_thread->AppendError(errcode, insn.ip);
 break;
   }
 
@@ -142,22 +145,22 @@
   if (time_error == -pte_invalid) {
 // This happens if we invoke the pt_ins

[Lldb-commits] [PATCH] D122603: [wip][intelpt] Refactor timestamps out of `IntelPTInstruction`

2022-03-28 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn created this revision.
zrthxn added reviewers: wallace, jj10306.
Herald added a project: All.
zrthxn requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122603

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h

Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
@@ -42,6 +42,8 @@
   DecodedThreadSP m_decoded_thread_sp;
   /// Internal instruction index currently pointing at.
   size_t m_pos;
+  /// Current instruction timestamp.
+  uint64_t m_currentts;
 };
 
 } // namespace trace_intel_pt
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -74,7 +74,7 @@
 }
 
 bool TraceCursorIntelPT::IsError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].IsError();
+  return m_decoded_thread_sp->GetInstructionIsError(m_pos);
 }
 
 const char *TraceCursorIntelPT::GetError() {
@@ -88,7 +88,8 @@
 Optional TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
   switch (counter_type) {
 case lldb::eTraceCounterTSC:
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetTimestampCounter();
+  // Hack to satisfy the the type of inherited member function
+  return Optional(m_decoded_thread_sp->GetInstructionTimestamp(m_pos));
   }
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
+++ lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
@@ -150,7 +150,8 @@
 // was enabled.
 decoded_thread_sp->AppendInstruction(insn);
   } else {
-decoded_thread_sp->AppendInstruction(insn, time);
+decoded_thread_sp->AppendInstruction(insn);
+decoded_thread_sp->AppendInstructionTimestamp(time);
   }
 }
   }
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -62,9 +62,6 @@
 /// As mentioned, any gap is represented as an error in this class.
 class IntelPTInstruction {
 public:
-  IntelPTInstruction(const pt_insn &pt_insn, uint64_t timestamp)
-  : m_pt_insn(pt_insn), m_timestamp(timestamp), m_is_error(false) {}
-
   IntelPTInstruction(const pt_insn &pt_insn)
   : m_pt_insn(pt_insn), m_is_error(false) {}
 
@@ -85,13 +82,6 @@
   /// Get the size in bytes of an instance of this class
   static size_t GetMemoryUsage();
 
-  /// Get the timestamp associated with the current instruction. The timestamp
-  /// is similar to what a rdtsc instruction would return.
-  ///
-  /// \return
-  /// The timestamp or \b llvm::None if not available.
-  llvm::Optional GetTimestampCounter() const;
-
   /// Get the \a lldb::TraceInstructionControlFlowType categories of the
   /// instruction.
   ///
@@ -113,7 +103,6 @@
   // When adding new members to this class, make sure to update
   // IntelPTInstruction::GetNonErrorMemoryUsage() if needed.
   pt_insn m_pt_insn;
-  llvm::Optional m_timestamp;
   bool m_is_error;
 };
 
@@ -131,6 +120,15 @@
   /// Utility constructor that initializes the trace with a provided error.
   DecodedThread(lldb::ThreadSP thread_sp, llvm::Error &&err);
 
+  /// Append a successfully decoded instruction.
+  void AppendInstruction(pt_insn instruction);
+
+  /// Append a timestamp at the index of the last instruction.
+  void AppendInstructionTimestamp(uint64_t timestamp);
+
+  /// Append a decoding error (i.e. an instruction that failed to be decoded).
+  void AppendError(llvm::Error &&error);
+
   /// Get the instructions from the decoded trace. Some of them might indicate
   /// errors (i.e. gaps) in the trace. For an instruction error, you can access
   /// its underlying error message with the \a GetErrorByInstructionIndex()
@@ -140,20 +138,21 @@
   ///   The instructions of the trace.
   llvm::ArrayRef GetInstructions() const;
 
+  /// Get timestamp of an instruction by its index.
+  uint64_t GetInstructionTimestamp(size_t index) const;
+
+  /// Check if the instruction at a given index was an error. 
+  /// -- Reasoning (this will be removed before committing)
+  /// This is faster and less wasteful of memory than creating an ArrayRef 
+  /// every t

[Lldb-commits] [PATCH] D122603: [wip][intelpt] Refactor timestamps out of `IntelPTInstruction`

2022-03-28 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 418660.
zrthxn added a comment.

Update cursor timestamp when we have a new one


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122603/new/

https://reviews.llvm.org/D122603

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h

Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
@@ -42,6 +42,8 @@
   DecodedThreadSP m_decoded_thread_sp;
   /// Internal instruction index currently pointing at.
   size_t m_pos;
+  /// Current instruction timestamp.
+  uint64_t m_currentts;
 };
 
 } // namespace trace_intel_pt
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -40,6 +40,8 @@
 
   while (canMoveOne()) {
 m_pos += IsForwards() ? 1 : -1;
+if (uint64_t ts = m_decoded_thread_sp->GetInstructionTimestamp(m_pos) != 0)
+  m_currentts = ts;
 if (!m_ignore_errors && IsError())
   return true;
 if (GetInstructionControlFlowType() & m_granularity)
@@ -74,7 +76,7 @@
 }
 
 bool TraceCursorIntelPT::IsError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].IsError();
+  return m_decoded_thread_sp->GetInstructionIsError(m_pos);
 }
 
 const char *TraceCursorIntelPT::GetError() {
@@ -88,7 +90,8 @@
 Optional TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
   switch (counter_type) {
 case lldb::eTraceCounterTSC:
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetTimestampCounter();
+  // Hack to satisfy the the type of inherited member function
+  return Optional(m_decoded_thread_sp->GetInstructionTimestamp(m_pos));
   }
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
+++ lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
@@ -150,7 +150,8 @@
 // was enabled.
 decoded_thread_sp->AppendInstruction(insn);
   } else {
-decoded_thread_sp->AppendInstruction(insn, time);
+decoded_thread_sp->AppendInstruction(insn);
+decoded_thread_sp->AppendInstructionTimestamp(time);
   }
 }
   }
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -62,9 +62,6 @@
 /// As mentioned, any gap is represented as an error in this class.
 class IntelPTInstruction {
 public:
-  IntelPTInstruction(const pt_insn &pt_insn, uint64_t timestamp)
-  : m_pt_insn(pt_insn), m_timestamp(timestamp), m_is_error(false) {}
-
   IntelPTInstruction(const pt_insn &pt_insn)
   : m_pt_insn(pt_insn), m_is_error(false) {}
 
@@ -85,13 +82,6 @@
   /// Get the size in bytes of an instance of this class
   static size_t GetMemoryUsage();
 
-  /// Get the timestamp associated with the current instruction. The timestamp
-  /// is similar to what a rdtsc instruction would return.
-  ///
-  /// \return
-  /// The timestamp or \b llvm::None if not available.
-  llvm::Optional GetTimestampCounter() const;
-
   /// Get the \a lldb::TraceInstructionControlFlowType categories of the
   /// instruction.
   ///
@@ -113,7 +103,6 @@
   // When adding new members to this class, make sure to update
   // IntelPTInstruction::GetNonErrorMemoryUsage() if needed.
   pt_insn m_pt_insn;
-  llvm::Optional m_timestamp;
   bool m_is_error;
 };
 
@@ -131,6 +120,15 @@
   /// Utility constructor that initializes the trace with a provided error.
   DecodedThread(lldb::ThreadSP thread_sp, llvm::Error &&err);
 
+  /// Append a successfully decoded instruction.
+  void AppendInstruction(pt_insn instruction);
+
+  /// Append a timestamp at the index of the last instruction.
+  void AppendInstructionTimestamp(uint64_t timestamp);
+
+  /// Append a decoding error (i.e. an instruction that failed to be decoded).
+  void AppendError(llvm::Error &&error);
+
   /// Get the instructions from the decoded trace. Some of them might indicate
   /// errors (i.e. gaps) in the trace. For an instruction error, you can access
   /// its underlying error message with the \a GetErrorByInstructionIndex()
@@ -140,20 +138,21 @@
   ///   The instructions of the trace.
   llvm::ArrayRef GetInstructions() const;
 
+  /// Get timestamp of an instruction by its inde

[Lldb-commits] [PATCH] D122603: [wip][intelpt] Refactor timestamps out of `IntelPTInstruction`

2022-03-29 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 418937.
zrthxn marked 12 inline comments as done.
zrthxn added a comment.

Introduced TscRange for quicker operation


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122603/new/

https://reviews.llvm.org/D122603

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h

Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
@@ -42,6 +42,8 @@
   DecodedThreadSP m_decoded_thread_sp;
   /// Internal instruction index currently pointing at.
   size_t m_pos;
+  /// Current instruction timestamp.
+  llvm::Optional m_current_tsc;
 };
 
 } // namespace trace_intel_pt
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -23,6 +23,7 @@
   assert(!m_decoded_thread_sp->GetInstructions().empty() &&
  "a trace should have at least one instruction or error");
   m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_current_tsc = m_decoded_thread_sp->GetTSCRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
@@ -40,6 +41,12 @@
 
   while (canMoveOne()) {
 m_pos += IsForwards() ? 1 : -1;
+
+if (m_pos > m_current_tsc->end_index)
+  m_current_tsc = m_decoded_thread_sp->GetNextTSCRange(*m_current_tsc);
+if (m_pos < m_current_tsc->start_index)
+  m_current_tsc = m_decoded_thread_sp->GetPrevTSCRange(*m_current_tsc);
+
 if (!m_ignore_errors && IsError())
   return true;
 if (GetInstructionControlFlowType() & m_granularity)
@@ -61,20 +68,24 @@
   switch (origin) {
   case TraceCursor::SeekType::Set:
 m_pos = fitPosToBounds(offset);
+m_current_tsc = m_decoded_thread_sp->GetTSCRange(m_pos);
 return m_pos;
   case TraceCursor::SeekType::End:
 m_pos = fitPosToBounds(offset + last_index);
+// not sure if this should be last_index - m_pos or just m_pos
+m_current_tsc = m_decoded_thread_sp->GetTSCRange(last_index - m_pos);
 return last_index - m_pos;
   case TraceCursor::SeekType::Current:
 int64_t new_pos = fitPosToBounds(offset + m_pos);
 int64_t dist = m_pos - new_pos;
 m_pos = new_pos;
+m_current_tsc = m_decoded_thread_sp->GetTSCRange(m_pos);
 return std::abs(dist);
   }
 }
 
 bool TraceCursorIntelPT::IsError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].IsError();
+  return m_decoded_thread_sp->IsInstructionAnError(m_pos);
 }
 
 const char *TraceCursorIntelPT::GetError() {
@@ -88,7 +99,7 @@
 Optional TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
   switch (counter_type) {
 case lldb::eTraceCounterTSC:
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetTimestampCounter();
+  return m_current_tsc->tsc;
   }
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -62,9 +62,6 @@
 /// As mentioned, any gap is represented as an error in this class.
 class IntelPTInstruction {
 public:
-  IntelPTInstruction(const pt_insn &pt_insn, uint64_t timestamp)
-  : m_pt_insn(pt_insn), m_timestamp(timestamp), m_is_error(false) {}
-
   IntelPTInstruction(const pt_insn &pt_insn)
   : m_pt_insn(pt_insn), m_is_error(false) {}
 
@@ -85,13 +82,6 @@
   /// Get the size in bytes of an instance of this class
   static size_t GetMemoryUsage();
 
-  /// Get the timestamp associated with the current instruction. The timestamp
-  /// is similar to what a rdtsc instruction would return.
-  ///
-  /// \return
-  /// The timestamp or \b llvm::None if not available.
-  llvm::Optional GetTimestampCounter() const;
-
   /// Get the \a lldb::TraceInstructionControlFlowType categories of the
   /// instruction.
   ///
@@ -113,7 +103,6 @@
   // When adding new members to this class, make sure to update
   // IntelPTInstruction::GetNonErrorMemoryUsage() if needed.
   pt_insn m_pt_insn;
-  llvm::Optional m_timestamp;
   bool m_is_error;
 };
 
@@ -131,6 +120,15 @@
   /// Utility constructor that initializes the trace with a provided error.
   DecodedThread(lldb::ThreadSP thread_sp, llvm::Error &&err);
 
+  /// Append a successfully decoded instruction.
+  void AppendInstruction(const pt_insn& instruction);
+
+  /// Append a timestamp at the index of the last instruction.
+  void AppendInstruction(const pt_insn& instruction, uint64_t timestamp);
+
+  //

[Lldb-commits] [PATCH] D122603: [intelpt] Refactor timestamps out of IntelPTInstruction

2022-03-29 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 418975.
zrthxn marked 7 inline comments as done.
zrthxn retitled this revision from "[wip][intelpt] Refactor timestamps out of 
`IntelPTInstruction`" to "[intelpt] Refactor timestamps out of 
IntelPTInstruction".
zrthxn added a comment.

Change TscRange to class


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122603/new/

https://reviews.llvm.org/D122603

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h

Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
@@ -42,6 +42,8 @@
   DecodedThreadSP m_decoded_thread_sp;
   /// Internal instruction index currently pointing at.
   size_t m_pos;
+  /// Current instruction timestamp.
+  llvm::Optional m_current_tsc;
 };
 
 } // namespace trace_intel_pt
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -23,6 +23,7 @@
   assert(!m_decoded_thread_sp->GetInstructions().empty() &&
  "a trace should have at least one instruction or error");
   m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
@@ -40,6 +41,16 @@
 
   while (canMoveOne()) {
 m_pos += IsForwards() ? 1 : -1;
+
+if (!m_current_tsc)
+  m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
+else if (!m_current_tsc->InRange(m_pos)) {
+  if (m_pos > m_current_tsc->GetEnd())
+m_current_tsc = m_current_tsc->Next();
+  if (m_pos < m_current_tsc->GetStart())
+m_current_tsc = m_current_tsc->Prev();
+}
+
 if (!m_ignore_errors && IsError())
   return true;
 if (GetInstructionControlFlowType() & m_granularity)
@@ -61,20 +72,23 @@
   switch (origin) {
   case TraceCursor::SeekType::Set:
 m_pos = fitPosToBounds(offset);
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return m_pos;
   case TraceCursor::SeekType::End:
 m_pos = fitPosToBounds(offset + last_index);
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(last_index - m_pos);
 return last_index - m_pos;
   case TraceCursor::SeekType::Current:
 int64_t new_pos = fitPosToBounds(offset + m_pos);
 int64_t dist = m_pos - new_pos;
 m_pos = new_pos;
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return std::abs(dist);
   }
 }
 
 bool TraceCursorIntelPT::IsError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].IsError();
+  return m_decoded_thread_sp->IsInstructionAnError(m_pos);
 }
 
 const char *TraceCursorIntelPT::GetError() {
@@ -85,10 +99,14 @@
   return m_decoded_thread_sp->GetInstructions()[m_pos].GetLoadAddress();
 }
 
-Optional TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
+Optional
+TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
+  if (!m_current_tsc)
+return None;
+
   switch (counter_type) {
-case lldb::eTraceCounterTSC:
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetTimestampCounter();
+  case lldb::eTraceCounterTSC:
+return m_current_tsc->GetTsc();
   }
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -62,9 +62,6 @@
 /// As mentioned, any gap is represented as an error in this class.
 class IntelPTInstruction {
 public:
-  IntelPTInstruction(const pt_insn &pt_insn, uint64_t timestamp)
-  : m_pt_insn(pt_insn), m_timestamp(timestamp), m_is_error(false) {}
-
   IntelPTInstruction(const pt_insn &pt_insn)
   : m_pt_insn(pt_insn), m_is_error(false) {}
 
@@ -85,13 +82,6 @@
   /// Get the size in bytes of an instance of this class
   static size_t GetMemoryUsage();
 
-  /// Get the timestamp associated with the current instruction. The timestamp
-  /// is similar to what a rdtsc instruction would return.
-  ///
-  /// \return
-  /// The timestamp or \b llvm::None if not available.
-  llvm::Optional GetTimestampCounter() const;
-
   /// Get the \a lldb::TraceInstructionControlFlowType categories of the
   /// instruction.
   ///
@@ -113,7 +103,6 @@
   // When adding new members to this class, make sure to update
   // IntelPTInstruction::GetNonErrorMemoryUsage() if needed.
   pt_insn m_pt_insn;
-  llvm::Optional m_timestamp;
   bool m_is_e

[Lldb-commits] [PATCH] D122603: [intelpt] Refactor timestamps out of IntelPTInstruction

2022-03-29 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 418977.
zrthxn added a comment.

Update memory calc function


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122603/new/

https://reviews.llvm.org/D122603

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp

Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -120,7 +120,7 @@
   s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
(double)mem_used / 1024);
   s.Printf("  Average memory usage per instruction: %zu bytes\n",
-   mem_used / insn_len);
+   (mem_used - *raw_size) / insn_len);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
@@ -42,6 +42,8 @@
   DecodedThreadSP m_decoded_thread_sp;
   /// Internal instruction index currently pointing at.
   size_t m_pos;
+  /// Current instruction timestamp.
+  llvm::Optional m_current_tsc;
 };
 
 } // namespace trace_intel_pt
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -23,6 +23,7 @@
   assert(!m_decoded_thread_sp->GetInstructions().empty() &&
  "a trace should have at least one instruction or error");
   m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
@@ -40,6 +41,16 @@
 
   while (canMoveOne()) {
 m_pos += IsForwards() ? 1 : -1;
+
+if (!m_current_tsc)
+  m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
+else if (!m_current_tsc->InRange(m_pos)) {
+  if (m_pos > m_current_tsc->GetEnd())
+m_current_tsc = m_current_tsc->Next();
+  if (m_pos < m_current_tsc->GetStart())
+m_current_tsc = m_current_tsc->Prev();
+}
+
 if (!m_ignore_errors && IsError())
   return true;
 if (GetInstructionControlFlowType() & m_granularity)
@@ -61,20 +72,23 @@
   switch (origin) {
   case TraceCursor::SeekType::Set:
 m_pos = fitPosToBounds(offset);
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return m_pos;
   case TraceCursor::SeekType::End:
 m_pos = fitPosToBounds(offset + last_index);
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(last_index - m_pos);
 return last_index - m_pos;
   case TraceCursor::SeekType::Current:
 int64_t new_pos = fitPosToBounds(offset + m_pos);
 int64_t dist = m_pos - new_pos;
 m_pos = new_pos;
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return std::abs(dist);
   }
 }
 
 bool TraceCursorIntelPT::IsError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].IsError();
+  return m_decoded_thread_sp->IsInstructionAnError(m_pos);
 }
 
 const char *TraceCursorIntelPT::GetError() {
@@ -85,10 +99,14 @@
   return m_decoded_thread_sp->GetInstructions()[m_pos].GetLoadAddress();
 }
 
-Optional TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
+Optional
+TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
+  if (!m_current_tsc)
+return None;
+
   switch (counter_type) {
-case lldb::eTraceCounterTSC:
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetTimestampCounter();
+  case lldb::eTraceCounterTSC:
+return m_current_tsc->GetTsc();
   }
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -62,9 +62,6 @@
 /// As mentioned, any gap is represented as an error in this class.
 class IntelPTInstruction {
 public:
-  IntelPTInstruction(const pt_insn &pt_insn, uint64_t timestamp)
-  : m_pt_insn(pt_insn), m_timestamp(timestamp), m_is_error(false) {}
-
   IntelPTInstruction(const pt_insn &pt_insn)
   : m_pt_insn(pt_insn), m_is_error(false) {}
 
@@ -85,13 +82,6 @@
   /// Get the size in bytes of an instance of this class
   static size_t GetMemoryUsage();
 
-  /// Get the timestamp associated with the current instruction. The timestamp
-  /// is similar to what a rdtsc instruction would return.
-  ///
-  /// \return
-  /// The timestamp or \b llvm::None if not

[Lldb-commits] [PATCH] D122603: [intelpt] Refactor timestamps out of IntelPTInstruction

2022-03-29 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 418981.
zrthxn added a comment.

Prevent crash on printing info when we have 0 instructions


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122603/new/

https://reviews.llvm.org/D122603

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp

Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -119,8 +119,9 @@
   s.Printf("  Total number of instructions: %zu\n", insn_len);
   s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
(double)mem_used / 1024);
-  s.Printf("  Average memory usage per instruction: %zu bytes\n",
-   mem_used / insn_len);
+  if (insn_len != 0)
+s.Printf("  Average memory usage per instruction: %zu bytes\n",
+(mem_used - *raw_size) / insn_len);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
@@ -42,6 +42,8 @@
   DecodedThreadSP m_decoded_thread_sp;
   /// Internal instruction index currently pointing at.
   size_t m_pos;
+  /// Current instruction timestamp.
+  llvm::Optional m_current_tsc;
 };
 
 } // namespace trace_intel_pt
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -23,6 +23,7 @@
   assert(!m_decoded_thread_sp->GetInstructions().empty() &&
  "a trace should have at least one instruction or error");
   m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
@@ -40,6 +41,16 @@
 
   while (canMoveOne()) {
 m_pos += IsForwards() ? 1 : -1;
+
+if (!m_current_tsc)
+  m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
+else if (!m_current_tsc->InRange(m_pos)) {
+  if (m_pos > m_current_tsc->GetEnd())
+m_current_tsc = m_current_tsc->Next();
+  if (m_pos < m_current_tsc->GetStart())
+m_current_tsc = m_current_tsc->Prev();
+}
+
 if (!m_ignore_errors && IsError())
   return true;
 if (GetInstructionControlFlowType() & m_granularity)
@@ -61,20 +72,23 @@
   switch (origin) {
   case TraceCursor::SeekType::Set:
 m_pos = fitPosToBounds(offset);
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return m_pos;
   case TraceCursor::SeekType::End:
 m_pos = fitPosToBounds(offset + last_index);
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(last_index - m_pos);
 return last_index - m_pos;
   case TraceCursor::SeekType::Current:
 int64_t new_pos = fitPosToBounds(offset + m_pos);
 int64_t dist = m_pos - new_pos;
 m_pos = new_pos;
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return std::abs(dist);
   }
 }
 
 bool TraceCursorIntelPT::IsError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].IsError();
+  return m_decoded_thread_sp->IsInstructionAnError(m_pos);
 }
 
 const char *TraceCursorIntelPT::GetError() {
@@ -85,10 +99,14 @@
   return m_decoded_thread_sp->GetInstructions()[m_pos].GetLoadAddress();
 }
 
-Optional TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
+Optional
+TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
+  if (!m_current_tsc)
+return None;
+
   switch (counter_type) {
-case lldb::eTraceCounterTSC:
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetTimestampCounter();
+  case lldb::eTraceCounterTSC:
+return m_current_tsc->GetTsc();
   }
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -62,9 +62,6 @@
 /// As mentioned, any gap is represented as an error in this class.
 class IntelPTInstruction {
 public:
-  IntelPTInstruction(const pt_insn &pt_insn, uint64_t timestamp)
-  : m_pt_insn(pt_insn), m_timestamp(timestamp), m_is_error(false) {}
-
   IntelPTInstruction(const pt_insn &pt_insn)
   : m_pt_insn(pt_insn), m_is_error(false) {}
 
@@ -85,13 +82,6 @@
   /// Get the size in bytes of an instance of this class
   static size_t GetMemoryUsage();
 
-  /// Get the timestam

[Lldb-commits] [PATCH] D122603: [intelpt] Refactor timestamps out of IntelPTInstruction

2022-03-31 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 419347.
zrthxn marked 23 inline comments as done.
zrthxn added a comment.

Included requested changes, removed extra members


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122603/new/

https://reviews.llvm.org/D122603

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp

Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -119,8 +119,9 @@
   s.Printf("  Total number of instructions: %zu\n", insn_len);
   s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
(double)mem_used / 1024);
-  s.Printf("  Average memory usage per instruction: %zu bytes\n",
-   mem_used / insn_len);
+  if (insn_len != 0)
+s.Printf("  Average memory usage per instruction: %zu bytes\n",
+ mem_used / insn_len);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
@@ -42,6 +42,8 @@
   DecodedThreadSP m_decoded_thread_sp;
   /// Internal instruction index currently pointing at.
   size_t m_pos;
+  /// Tsc range covering the current instruction.
+  llvm::Optional m_current_tsc;
 };
 
 } // namespace trace_intel_pt
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -23,6 +23,7 @@
   assert(!m_decoded_thread_sp->GetInstructions().empty() &&
  "a trace should have at least one instruction or error");
   m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
@@ -40,6 +41,13 @@
 
   while (canMoveOne()) {
 m_pos += IsForwards() ? 1 : -1;
+
+if (!m_current_tsc)
+  m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
+else if (!m_current_tsc->InRange(m_pos))
+  m_current_tsc =
+  IsForwards() ? m_current_tsc->Next() : m_current_tsc->Prev();
+
 if (!m_ignore_errors && IsError())
   return true;
 if (GetInstructionControlFlowType() & m_granularity)
@@ -61,20 +69,23 @@
   switch (origin) {
   case TraceCursor::SeekType::Set:
 m_pos = fitPosToBounds(offset);
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return m_pos;
   case TraceCursor::SeekType::End:
 m_pos = fitPosToBounds(offset + last_index);
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(last_index - m_pos);
 return last_index - m_pos;
   case TraceCursor::SeekType::Current:
 int64_t new_pos = fitPosToBounds(offset + m_pos);
 int64_t dist = m_pos - new_pos;
 m_pos = new_pos;
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return std::abs(dist);
   }
 }
 
 bool TraceCursorIntelPT::IsError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].IsError();
+  return m_decoded_thread_sp->IsInstructionAnError(m_pos);
 }
 
 const char *TraceCursorIntelPT::GetError() {
@@ -85,10 +96,14 @@
   return m_decoded_thread_sp->GetInstructions()[m_pos].GetLoadAddress();
 }
 
-Optional TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
+Optional
+TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
+  if (!m_current_tsc)
+return None;
+
   switch (counter_type) {
-case lldb::eTraceCounterTSC:
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetTimestampCounter();
+  case lldb::eTraceCounterTSC:
+return m_current_tsc->GetTsc();
   }
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -62,9 +62,6 @@
 /// As mentioned, any gap is represented as an error in this class.
 class IntelPTInstruction {
 public:
-  IntelPTInstruction(const pt_insn &pt_insn, uint64_t timestamp)
-  : m_pt_insn(pt_insn), m_timestamp(timestamp), m_is_error(false) {}
-
   IntelPTInstruction(const pt_insn &pt_insn)
   : m_pt_insn(pt_insn), m_is_error(false) {}
 
@@ -85,13 +82,6 @@
   /// Get the size in bytes of an instance of this class
   static size_t GetMemoryUsage();
 
-  /// Get the timestamp associated with the current instruction. The timestamp
-  /// is

[Lldb-commits] [PATCH] D122603: [intelpt] Refactor timestamps out of IntelPTInstruction

2022-03-31 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn added inline comments.



Comment at: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp:45-52
+if (!m_current_tsc)
+  m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
+else if (!m_current_tsc->InRange(m_pos)) {
+  if (m_pos > m_current_tsc->GetEnd())
+m_current_tsc = m_current_tsc->Next();
+  if (m_pos < m_current_tsc->GetStart())
+m_current_tsc = m_current_tsc->Prev();

wallace wrote:
> No need to do `m_current_tsc = 
> m_decoded_thread_sp->CalculateTscRange(m_pos);` because its value has already 
> been calculated in the constructor. We can simplify this as well
It is possible that when TraceCursorIntelPT is created the m_current_tsc is 
None, for example when just started the trace and tried to dump instructions... 
But then if a tsc is emitted later, this would cause it to remain None since we 
don't re-calculate it if it was initially None



Comment at: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp:102-103
 
-Optional TraceCursorIntelPT::GetCounter(lldb::TraceCounter 
counter_type) {
+Optional
+TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
+  if (!m_current_tsc)

wallace wrote:
> are you using git clang-format? I'm curious why this line changed
Yes  I am. I think its because its longer than 80 chars.



Comment at: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp:104-110
+  if (!m_current_tsc)
+return None;
+
   switch (counter_type) {
-case lldb::eTraceCounterTSC:
-  return 
m_decoded_thread_sp->GetInstructions()[m_pos].GetTimestampCounter();
+  case lldb::eTraceCounterTSC:
+return m_current_tsc->GetTsc();
   }

wallace wrote:
> 
m_current_tsc is already checked at the beginning of this function


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122603/new/

https://reviews.llvm.org/D122603

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122603: [intelpt] Refactor timestamps out of IntelPTInstruction

2022-03-31 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 419348.
zrthxn added a comment.

Change tsc check anyway


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122603/new/

https://reviews.llvm.org/D122603

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp

Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -119,8 +119,9 @@
   s.Printf("  Total number of instructions: %zu\n", insn_len);
   s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
(double)mem_used / 1024);
-  s.Printf("  Average memory usage per instruction: %zu bytes\n",
-   mem_used / insn_len);
+  if (insn_len != 0)
+s.Printf("  Average memory usage per instruction: %zu bytes\n",
+ mem_used / insn_len);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
@@ -42,6 +42,8 @@
   DecodedThreadSP m_decoded_thread_sp;
   /// Internal instruction index currently pointing at.
   size_t m_pos;
+  /// Tsc range covering the current instruction.
+  llvm::Optional m_current_tsc;
 };
 
 } // namespace trace_intel_pt
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -23,6 +23,7 @@
   assert(!m_decoded_thread_sp->GetInstructions().empty() &&
  "a trace should have at least one instruction or error");
   m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
@@ -40,6 +41,13 @@
 
   while (canMoveOne()) {
 m_pos += IsForwards() ? 1 : -1;
+
+if (!m_current_tsc)
+  m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
+else if (!m_current_tsc->InRange(m_pos))
+  m_current_tsc =
+  IsForwards() ? m_current_tsc->Next() : m_current_tsc->Prev();
+
 if (!m_ignore_errors && IsError())
   return true;
 if (GetInstructionControlFlowType() & m_granularity)
@@ -61,20 +69,23 @@
   switch (origin) {
   case TraceCursor::SeekType::Set:
 m_pos = fitPosToBounds(offset);
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return m_pos;
   case TraceCursor::SeekType::End:
 m_pos = fitPosToBounds(offset + last_index);
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(last_index - m_pos);
 return last_index - m_pos;
   case TraceCursor::SeekType::Current:
 int64_t new_pos = fitPosToBounds(offset + m_pos);
 int64_t dist = m_pos - new_pos;
 m_pos = new_pos;
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return std::abs(dist);
   }
 }
 
 bool TraceCursorIntelPT::IsError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].IsError();
+  return m_decoded_thread_sp->IsInstructionAnError(m_pos);
 }
 
 const char *TraceCursorIntelPT::GetError() {
@@ -85,10 +96,14 @@
   return m_decoded_thread_sp->GetInstructions()[m_pos].GetLoadAddress();
 }
 
-Optional TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
+Optional
+TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
   switch (counter_type) {
-case lldb::eTraceCounterTSC:
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetTimestampCounter();
+  case lldb::eTraceCounterTSC:
+if (m_current_tsc)
+  return m_current_tsc->GetTsc();
+else
+  return llvm::None;
   }
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -62,9 +62,6 @@
 /// As mentioned, any gap is represented as an error in this class.
 class IntelPTInstruction {
 public:
-  IntelPTInstruction(const pt_insn &pt_insn, uint64_t timestamp)
-  : m_pt_insn(pt_insn), m_timestamp(timestamp), m_is_error(false) {}
-
   IntelPTInstruction(const pt_insn &pt_insn)
   : m_pt_insn(pt_insn), m_is_error(false) {}
 
@@ -85,13 +82,6 @@
   /// Get the size in bytes of an instance of this class
   static size_t GetMemoryUsage();
 
-  /// Get the timestamp associated with the current instruction. The timestamp
-  /// is similar to what a rdtsc instruction would return

[Lldb-commits] [PATCH] D122603: [intelpt] Refactor timestamps out of IntelPTInstruction

2022-03-31 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 419517.
zrthxn marked an inline comment as done.
zrthxn added a comment.

Fixed issue with TSC becoming invalid midway through trace


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122603/new/

https://reviews.llvm.org/D122603

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp

Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -119,8 +119,9 @@
   s.Printf("  Total number of instructions: %zu\n", insn_len);
   s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
(double)mem_used / 1024);
-  s.Printf("  Average memory usage per instruction: %zu bytes\n",
-   mem_used / insn_len);
+  if (insn_len != 0)
+s.Printf("  Average memory usage per instruction: %zu bytes\n",
+ mem_used / insn_len);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
@@ -42,6 +42,8 @@
   DecodedThreadSP m_decoded_thread_sp;
   /// Internal instruction index currently pointing at.
   size_t m_pos;
+  /// Tsc range covering the current instruction.
+  llvm::Optional m_current_tsc;
 };
 
 } // namespace trace_intel_pt
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -23,6 +23,7 @@
   assert(!m_decoded_thread_sp->GetInstructions().empty() &&
  "a trace should have at least one instruction or error");
   m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
@@ -40,6 +41,11 @@
 
   while (canMoveOne()) {
 m_pos += IsForwards() ? 1 : -1;
+
+if (m_current_tsc && !m_current_tsc->InRange(m_pos))
+  m_current_tsc =
+  IsForwards() ? m_current_tsc->Next() : m_current_tsc->Prev();
+
 if (!m_ignore_errors && IsError())
   return true;
 if (GetInstructionControlFlowType() & m_granularity)
@@ -61,20 +67,23 @@
   switch (origin) {
   case TraceCursor::SeekType::Set:
 m_pos = fitPosToBounds(offset);
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return m_pos;
   case TraceCursor::SeekType::End:
 m_pos = fitPosToBounds(offset + last_index);
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return last_index - m_pos;
   case TraceCursor::SeekType::Current:
 int64_t new_pos = fitPosToBounds(offset + m_pos);
 int64_t dist = m_pos - new_pos;
 m_pos = new_pos;
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return std::abs(dist);
   }
 }
 
 bool TraceCursorIntelPT::IsError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].IsError();
+  return m_decoded_thread_sp->IsInstructionAnError(m_pos);
 }
 
 const char *TraceCursorIntelPT::GetError() {
@@ -85,10 +94,14 @@
   return m_decoded_thread_sp->GetInstructions()[m_pos].GetLoadAddress();
 }
 
-Optional TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
+Optional
+TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
   switch (counter_type) {
-case lldb::eTraceCounterTSC:
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetTimestampCounter();
+  case lldb::eTraceCounterTSC:
+if (m_current_tsc)
+  return m_current_tsc->GetTsc();
+else
+  return llvm::None;
   }
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -62,9 +62,6 @@
 /// As mentioned, any gap is represented as an error in this class.
 class IntelPTInstruction {
 public:
-  IntelPTInstruction(const pt_insn &pt_insn, uint64_t timestamp)
-  : m_pt_insn(pt_insn), m_timestamp(timestamp), m_is_error(false) {}
-
   IntelPTInstruction(const pt_insn &pt_insn)
   : m_pt_insn(pt_insn), m_is_error(false) {}
 
@@ -85,13 +82,6 @@
   /// Get the size in bytes of an instance of this class
   static size_t GetMemoryUsage();
 
-  /// Get the timestamp associated with the current instruction. The timestamp
-  /// is similar to what a rdtsc instruction would return.
-  ///
-  /// \ret

[Lldb-commits] [PATCH] D122603: [intelpt] Refactor timestamps out of IntelPTInstruction

2022-03-31 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 419519.
zrthxn added a comment.

Updated tests according to new memory usage calculation


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122603/new/

https://reviews.llvm.org/D122603

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py
  lldb/test/API/commands/trace/TestTraceLoad.py

Index: lldb/test/API/commands/trace/TestTraceLoad.py
===
--- lldb/test/API/commands/trace/TestTraceLoad.py
+++ lldb/test/API/commands/trace/TestTraceLoad.py
@@ -38,8 +38,8 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 5.31 KiB
-  Average memory usage per instruction: 259 bytes'''])
+  Total approximate memory usage: 0.98 KiB
+  Average memory usage per instruction: 48 bytes'''])
 
 def testLoadInvalidTraces(self):
 src_dir = self.getSourceDir()
Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -40,5 +40,5 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 5.31 KiB
-  Average memory usage per instruction: 259 bytes'''])
+  Total approximate memory usage: 0.98 KiB
+  Average memory usage per instruction: 48 bytes'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -119,8 +119,9 @@
   s.Printf("  Total number of instructions: %zu\n", insn_len);
   s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
(double)mem_used / 1024);
-  s.Printf("  Average memory usage per instruction: %zu bytes\n",
-   mem_used / insn_len);
+  if (insn_len != 0)
+s.Printf("  Average memory usage per instruction: %zu bytes\n",
+ mem_used / insn_len);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
@@ -42,6 +42,8 @@
   DecodedThreadSP m_decoded_thread_sp;
   /// Internal instruction index currently pointing at.
   size_t m_pos;
+  /// Tsc range covering the current instruction.
+  llvm::Optional m_current_tsc;
 };
 
 } // namespace trace_intel_pt
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -23,6 +23,7 @@
   assert(!m_decoded_thread_sp->GetInstructions().empty() &&
  "a trace should have at least one instruction or error");
   m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
@@ -40,6 +41,11 @@
 
   while (canMoveOne()) {
 m_pos += IsForwards() ? 1 : -1;
+
+if (m_current_tsc && !m_current_tsc->InRange(m_pos))
+  m_current_tsc =
+  IsForwards() ? m_current_tsc->Next() : m_current_tsc->Prev();
+
 if (!m_ignore_errors && IsError())
   return true;
 if (GetInstructionControlFlowType() & m_granularity)
@@ -61,20 +67,23 @@
   switch (origin) {
   case TraceCursor::SeekType::Set:
 m_pos = fitPosToBounds(offset);
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return m_pos;
   case TraceCursor::SeekType::End:
 m_pos = fitPosToBounds(offset + last_index);
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return last_index - m_pos;
   case TraceCursor::SeekType::Current:
 int64_t new_pos = fitPosToBounds(offset + m_pos);
 int64_t dist = m_pos - new_pos;
 m_pos = new_pos;
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return std::abs(dist);
   }
 }
 
 bool TraceCursorIntelPT::IsError() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].IsError();
+  return m_decoded_thread_sp->IsInstructionAnError(m_pos);
 }
 
 const char *TraceCursorIntelPT::GetError() {
@@ -85,10 +94,14 @@
   return m_decoded_thread_sp->GetInstructions()[m_pos].GetLoadAddress();
 }
 
-Optional TraceCursorIntelPT::GetCounter(lldb::TraceCounter counter_type) {
+Optional
+TraceCursorIntelPT::GetCou

[Lldb-commits] [PATCH] D122603: [intelpt] Refactor timestamps out of IntelPTInstruction

2022-03-31 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 419560.
zrthxn marked 12 inline comments as done.
zrthxn added a comment.

Incorporate feedback and update tests


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122603/new/

https://reviews.llvm.org/D122603

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py
  lldb/test/API/commands/trace/TestTraceLoad.py

Index: lldb/test/API/commands/trace/TestTraceLoad.py
===
--- lldb/test/API/commands/trace/TestTraceLoad.py
+++ lldb/test/API/commands/trace/TestTraceLoad.py
@@ -38,8 +38,8 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 5.31 KiB
-  Average memory usage per instruction: 259 bytes'''])
+  Total approximate memory usage: 0.98 KiB
+  Average memory usage per instruction: 48.00 bytes'''])
 
 def testLoadInvalidTraces(self):
 src_dir = self.getSourceDir()
Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -40,5 +40,5 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 5.31 KiB
-  Average memory usage per instruction: 259 bytes'''])
+  Total approximate memory usage: 0.98 KiB
+  Average memory usage per instruction: 48.00 bytes'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -119,8 +119,9 @@
   s.Printf("  Total number of instructions: %zu\n", insn_len);
   s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
(double)mem_used / 1024);
-  s.Printf("  Average memory usage per instruction: %zu bytes\n",
-   mem_used / insn_len);
+  if (insn_len != 0)
+s.Printf("  Average memory usage per instruction: %0.2lf bytes\n",
+ (double)mem_used / insn_len);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
@@ -42,6 +42,8 @@
   DecodedThreadSP m_decoded_thread_sp;
   /// Internal instruction index currently pointing at.
   size_t m_pos;
+  /// Tsc range covering the current instruction.
+  llvm::Optional m_tsc_range;
 };
 
 } // namespace trace_intel_pt
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -23,6 +23,7 @@
   assert(!m_decoded_thread_sp->GetInstructions().empty() &&
  "a trace should have at least one instruction or error");
   m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
@@ -40,6 +41,10 @@
 
   while (canMoveOne()) {
 m_pos += IsForwards() ? 1 : -1;
+
+if (m_tsc_range && !m_tsc_range->InRange(m_pos))
+  m_tsc_range = IsForwards() ? m_tsc_range->Next() : m_tsc_range->Prev();
+
 if (!m_ignore_errors && IsError())
   return true;
 if (GetInstructionControlFlowType() & m_granularity)
@@ -58,23 +63,29 @@
 return std::min(std::max((int64_t)0, raw_pos), last_index);
   };
 
-  switch (origin) {
-  case TraceCursor::SeekType::Set:
-m_pos = fitPosToBounds(offset);
-return m_pos;
-  case TraceCursor::SeekType::End:
-m_pos = fitPosToBounds(offset + last_index);
-return last_index - m_pos;
-  case TraceCursor::SeekType::Current:
-int64_t new_pos = fitPosToBounds(offset + m_pos);
-int64_t dist = m_pos - new_pos;
-m_pos = new_pos;
-return std::abs(dist);
-  }
+  auto FindDistanceAndSetPos = [&]() -> int64_t {
+switch (origin) {
+case TraceCursor::SeekType::Set:
+  m_pos = fitPosToBounds(offset);
+  return m_pos;
+case TraceCursor::SeekType::End:
+  m_pos = fitPosToBounds(offset + last_index);
+  return last_index - m_pos;
+case TraceCursor::SeekType::Current:
+  int64_t new_pos = fitPosToBounds(offset + m_pos);
+  int64_t dist = m_pos - new_pos;
+  m_pos = new_pos;
+  return std::abs(dist);
+}
+  };
+
+  auto dist = FindDistanceAndSetPos();
+  m_

[Lldb-commits] [PATCH] D122603: [intelpt] Refactor timestamps out of IntelPTInstruction

2022-03-31 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn added inline comments.



Comment at: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp:67-82
   switch (origin) {
   case TraceCursor::SeekType::Set:
 m_pos = fitPosToBounds(offset);
+m_current_tsc = m_decoded_thread_sp->CalculateTscRange(m_pos);
 return m_pos;
   case TraceCursor::SeekType::End:
 m_pos = fitPosToBounds(offset + last_index);

wallace wrote:
> we can simplify this so that we only invoke CalculateTscRange once
This is incorrect The converted code always returns 0. I've refactored it to 
have CalculateTscRange once but its a side-effect-y function and will need some 
future attention.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122603/new/

https://reviews.llvm.org/D122603

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122603: [intelpt] Refactor timestamps out of IntelPTInstruction

2022-03-31 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 419619.
zrthxn marked 3 inline comments as done.
zrthxn added a comment.

Dont use auto for simple types


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122603/new/

https://reviews.llvm.org/D122603

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py
  lldb/test/API/commands/trace/TestTraceLoad.py

Index: lldb/test/API/commands/trace/TestTraceLoad.py
===
--- lldb/test/API/commands/trace/TestTraceLoad.py
+++ lldb/test/API/commands/trace/TestTraceLoad.py
@@ -38,8 +38,8 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 5.31 KiB
-  Average memory usage per instruction: 259 bytes'''])
+  Total approximate memory usage: 0.98 KiB
+  Average memory usage per instruction: 48.00 bytes'''])
 
 def testLoadInvalidTraces(self):
 src_dir = self.getSourceDir()
Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -40,5 +40,5 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 5.31 KiB
-  Average memory usage per instruction: 259 bytes'''])
+  Total approximate memory usage: 0.98 KiB
+  Average memory usage per instruction: 48.00 bytes'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -119,8 +119,9 @@
   s.Printf("  Total number of instructions: %zu\n", insn_len);
   s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
(double)mem_used / 1024);
-  s.Printf("  Average memory usage per instruction: %zu bytes\n",
-   mem_used / insn_len);
+  if (insn_len != 0)
+s.Printf("  Average memory usage per instruction: %0.2lf bytes\n",
+ (double)mem_used / insn_len);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
@@ -42,6 +42,8 @@
   DecodedThreadSP m_decoded_thread_sp;
   /// Internal instruction index currently pointing at.
   size_t m_pos;
+  /// Tsc range covering the current instruction.
+  llvm::Optional m_tsc_range;
 };
 
 } // namespace trace_intel_pt
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -23,6 +23,7 @@
   assert(!m_decoded_thread_sp->GetInstructions().empty() &&
  "a trace should have at least one instruction or error");
   m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
@@ -40,6 +41,10 @@
 
   while (canMoveOne()) {
 m_pos += IsForwards() ? 1 : -1;
+
+if (m_tsc_range && !m_tsc_range->InRange(m_pos))
+  m_tsc_range = IsForwards() ? m_tsc_range->Next() : m_tsc_range->Prev();
+
 if (!m_ignore_errors && IsError())
   return true;
 if (GetInstructionControlFlowType() & m_granularity)
@@ -58,23 +63,29 @@
 return std::min(std::max((int64_t)0, raw_pos), last_index);
   };
 
-  switch (origin) {
-  case TraceCursor::SeekType::Set:
-m_pos = fitPosToBounds(offset);
-return m_pos;
-  case TraceCursor::SeekType::End:
-m_pos = fitPosToBounds(offset + last_index);
-return last_index - m_pos;
-  case TraceCursor::SeekType::Current:
-int64_t new_pos = fitPosToBounds(offset + m_pos);
-int64_t dist = m_pos - new_pos;
-m_pos = new_pos;
-return std::abs(dist);
-  }
+  auto FindDistanceAndSetPos = [&]() -> int64_t {
+switch (origin) {
+case TraceCursor::SeekType::Set:
+  m_pos = fitPosToBounds(offset);
+  return m_pos;
+case TraceCursor::SeekType::End:
+  m_pos = fitPosToBounds(offset + last_index);
+  return last_index - m_pos;
+case TraceCursor::SeekType::Current:
+  int64_t new_pos = fitPosToBounds(offset + m_pos);
+  int64_t dist = m_pos - new_pos;
+  m_pos = new_pos;
+  return std::abs(dist);
+}
+  };
+  
+	int64_t dist = FindDistanceAndSetPos();
+  m_tsc_

[Lldb-commits] [PATCH] D122603: [intelpt] Refactor timestamps out of IntelPTInstruction

2022-03-31 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn added inline comments.



Comment at: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h:145
+  private:
+friend class DecodedThread;
+

wallace wrote:
> jj10306 wrote:
> > nit: No need to friend the enclosing class since C++11 - 
> > https://en.cppreference.com/w/cpp/language/nested_types
> TIL!
We need the friend because we are using a private constructor from outside, in 
DecodedThread::CalculateTscRange and a couple other places. The idea is to let 
only DecodedThread create TscRange. 


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122603/new/

https://reviews.llvm.org/D122603

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D122603: [intelpt] Refactor timestamps out of IntelPTInstruction

2022-04-01 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 419656.
zrthxn edited the summary of this revision.
zrthxn added a comment.

The difference in memory usage is appreciable with large number of 
instructions, as shown below

  # Before (with current metrics, total memory does not include raw trace size)
Raw trace size: 2048 KiB
Total number of instructions: 94
Total approximate memory usage: 56143.10 KiB
Average memory usage per instruction: 63.87 bytes

  # After
Raw trace size: 2048 KiB
Total number of instructions: 94
Total approximate memory usage: 42187.69 KiB
Average memory usage per instruction: 48.00 bytes


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122603/new/

https://reviews.llvm.org/D122603

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py
  lldb/test/API/commands/trace/TestTraceLoad.py

Index: lldb/test/API/commands/trace/TestTraceLoad.py
===
--- lldb/test/API/commands/trace/TestTraceLoad.py
+++ lldb/test/API/commands/trace/TestTraceLoad.py
@@ -38,8 +38,8 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 5.31 KiB
-  Average memory usage per instruction: 259 bytes'''])
+  Total approximate memory usage: 0.98 KiB
+  Average memory usage per instruction: 48.00 bytes'''])
 
 def testLoadInvalidTraces(self):
 src_dir = self.getSourceDir()
Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -40,5 +40,5 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 5.31 KiB
-  Average memory usage per instruction: 259 bytes'''])
+  Total approximate memory usage: 0.98 KiB
+  Average memory usage per instruction: 48.00 bytes'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -119,8 +119,9 @@
   s.Printf("  Total number of instructions: %zu\n", insn_len);
   s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
(double)mem_used / 1024);
-  s.Printf("  Average memory usage per instruction: %zu bytes\n",
-   mem_used / insn_len);
+  if (insn_len != 0)
+s.Printf("  Average memory usage per instruction: %0.2lf bytes\n",
+ (double)mem_used / insn_len);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
@@ -42,6 +42,8 @@
   DecodedThreadSP m_decoded_thread_sp;
   /// Internal instruction index currently pointing at.
   size_t m_pos;
+  /// Tsc range covering the current instruction.
+  llvm::Optional m_tsc_range;
 };
 
 } // namespace trace_intel_pt
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -23,6 +23,7 @@
   assert(!m_decoded_thread_sp->GetInstructions().empty() &&
  "a trace should have at least one instruction or error");
   m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
@@ -40,6 +41,10 @@
 
   while (canMoveOne()) {
 m_pos += IsForwards() ? 1 : -1;
+
+if (m_tsc_range && !m_tsc_range->InRange(m_pos))
+  m_tsc_range = IsForwards() ? m_tsc_range->Next() : m_tsc_range->Prev();
+
 if (!m_ignore_errors && IsError())
   return true;
 if (GetInstructionControlFlowType() & m_granularity)
@@ -58,23 +63,29 @@
 return std::min(std::max((int64_t)0, raw_pos), last_index);
   };
 
-  switch (origin) {
-  case TraceCursor::SeekType::Set:
-m_pos = fitPosToBounds(offset);
-return m_pos;
-  case TraceCursor::SeekType::End:
-m_pos = fitPosToBounds(offset + last_index);
-return last_index - m_pos;
-  case TraceCursor::SeekType::Current:
-int64_t new_pos = fitPosToBounds(offset + m_pos);
-int64_t dist = m_pos - new_pos;
-m_pos = new_pos;
-return std::abs(dist);
-  }
+  auto FindDistanceAndSetPos = [&]() -> int64_t 

[Lldb-commits] [PATCH] D122603: [intelpt] Refactor timestamps out of IntelPTInstruction

2022-04-01 Thread Alisamar Husain via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGca922a3559d7: [intelpt] Refactor timestamps out of 
`IntelPTInstruction` (authored by zrthxn).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122603/new/

https://reviews.llvm.org/D122603

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py
  lldb/test/API/commands/trace/TestTraceLoad.py

Index: lldb/test/API/commands/trace/TestTraceLoad.py
===
--- lldb/test/API/commands/trace/TestTraceLoad.py
+++ lldb/test/API/commands/trace/TestTraceLoad.py
@@ -38,8 +38,8 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 5.31 KiB
-  Average memory usage per instruction: 259 bytes'''])
+  Total approximate memory usage: 0.98 KiB
+  Average memory usage per instruction: 48.00 bytes'''])
 
 def testLoadInvalidTraces(self):
 src_dir = self.getSourceDir()
Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -40,5 +40,5 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 5.31 KiB
-  Average memory usage per instruction: 259 bytes'''])
+  Total approximate memory usage: 0.98 KiB
+  Average memory usage per instruction: 48.00 bytes'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -119,8 +119,9 @@
   s.Printf("  Total number of instructions: %zu\n", insn_len);
   s.Printf("  Total approximate memory usage: %0.2lf KiB\n",
(double)mem_used / 1024);
-  s.Printf("  Average memory usage per instruction: %zu bytes\n",
-   mem_used / insn_len);
+  if (insn_len != 0)
+s.Printf("  Average memory usage per instruction: %0.2lf bytes\n",
+ (double)mem_used / insn_len);
   return;
 }
 
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
@@ -42,6 +42,8 @@
   DecodedThreadSP m_decoded_thread_sp;
   /// Internal instruction index currently pointing at.
   size_t m_pos;
+  /// Tsc range covering the current instruction.
+  llvm::Optional m_tsc_range;
 };
 
 } // namespace trace_intel_pt
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -23,6 +23,7 @@
   assert(!m_decoded_thread_sp->GetInstructions().empty() &&
  "a trace should have at least one instruction or error");
   m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
@@ -40,6 +41,10 @@
 
   while (canMoveOne()) {
 m_pos += IsForwards() ? 1 : -1;
+
+if (m_tsc_range && !m_tsc_range->InRange(m_pos))
+  m_tsc_range = IsForwards() ? m_tsc_range->Next() : m_tsc_range->Prev();
+
 if (!m_ignore_errors && IsError())
   return true;
 if (GetInstructionControlFlowType() & m_granularity)
@@ -58,23 +63,29 @@
 return std::min(std::max((int64_t)0, raw_pos), last_index);
   };
 
-  switch (origin) {
-  case TraceCursor::SeekType::Set:
-m_pos = fitPosToBounds(offset);
-return m_pos;
-  case TraceCursor::SeekType::End:
-m_pos = fitPosToBounds(offset + last_index);
-return last_index - m_pos;
-  case TraceCursor::SeekType::Current:
-int64_t new_pos = fitPosToBounds(offset + m_pos);
-int64_t dist = m_pos - new_pos;
-m_pos = new_pos;
-return std::abs(dist);
-  }
+  auto FindDistanceAndSetPos = [&]() -> int64_t {
+switch (origin) {
+case TraceCursor::SeekType::Set:
+  m_pos = fitPosToBounds(offset);
+  return m_pos;
+case TraceCursor::SeekType::End:
+  m_pos = fitPosToBounds(offset + last_index);
+  return last_index - m_pos;
+case TraceCursor::SeekType::Current:
+  int64_t new_pos = fitPosToBounds(offset + m_pos);
+  int64_t dist = m_pos - new_pos;
+  m_pos = new_pos;
+  return std::abs(dist);
+}
+  };
+  
+	int6

[Lldb-commits] [PATCH] D122991: [lldb][intelpt] Remove `IntelPTInstruction` and move methods to `DecodedThread`

2022-04-03 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn created this revision.
zrthxn added a reviewer: wallace.
Herald added a project: All.
zrthxn requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

This is to reduce the size of the trace further and has appreciable results.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122991

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp

Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -78,8 +78,8 @@
   return std::abs(dist);
 }
   };
-  
-	int64_t dist = FindDistanceAndSetPos();
+
+  int64_t dist = FindDistanceAndSetPos();
   m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos);
   return dist;
 }
@@ -93,7 +93,7 @@
 }
 
 lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetLoadAddress();
+  return m_decoded_thread_sp->GetInstructionLoadAddress(m_pos);
 }
 
 Optional
@@ -111,8 +111,8 @@
 TraceCursorIntelPT::GetInstructionControlFlowType() {
   lldb::addr_t next_load_address =
   m_pos + 1 < GetInternalInstructionSize()
-  ? m_decoded_thread_sp->GetInstructions()[m_pos + 1].GetLoadAddress()
+  ? m_decoded_thread_sp->GetInstructionLoadAddress(m_pos + 1)
   : LLDB_INVALID_ADDRESS;
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetControlFlowType(
-  next_load_address);
+  return m_decoded_thread_sp->GetInstructionControlFlowType(m_pos,
+next_load_address);
 }
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -51,61 +51,6 @@
   lldb::addr_t m_address;
 };
 
-/// \class IntelPTInstruction
-/// An instruction obtained from decoding a trace. It is either an actual
-/// instruction or an error indicating a gap in the trace.
-///
-/// Gaps in the trace can come in a few flavors:
-///   - tracing gaps (e.g. tracing was paused and then resumed)
-///   - tracing errors (e.g. buffer overflow)
-///   - decoding errors (e.g. some memory region couldn't be decoded)
-/// As mentioned, any gap is represented as an error in this class.
-class IntelPTInstruction {
-public:
-  IntelPTInstruction(const pt_insn &pt_insn)
-  : m_pt_insn(pt_insn), m_is_error(false) {}
-
-  /// Error constructor
-  IntelPTInstruction();
-
-  /// Check if this object represents an error (i.e. a gap).
-  ///
-  /// \return
-  /// Whether this object represents an error.
-  bool IsError() const;
-
-  /// \return
-  /// The instruction pointer address, or \a LLDB_INVALID_ADDRESS if it is
-  /// an error.
-  lldb::addr_t GetLoadAddress() const;
-
-  /// Get the size in bytes of an instance of this class
-  static size_t GetMemoryUsage();
-
-  /// Get the \a lldb::TraceInstructionControlFlowType categories of the
-  /// instruction.
-  ///
-  /// \param[in] next_load_address
-  /// The address of the next instruction in the trace or \b
-  /// LLDB_INVALID_ADDRESS if not available.
-  ///
-  /// \return
-  /// The control flow categories, or \b 0 if the instruction is an error.
-  lldb::TraceInstructionControlFlowType
-  GetControlFlowType(lldb::addr_t next_load_address) const;
-
-  IntelPTInstruction(IntelPTInstruction &&other) = default;
-
-private:
-  IntelPTInstruction(const IntelPTInstruction &other) = delete;
-  const IntelPTInstruction &operator=(const IntelPTInstruction &other) = delete;
-
-  // When adding new members to this class, make sure to update
-  // IntelPTInstruction::GetMemoryUsage() if needed.
-  pt_insn m_pt_insn;
-  bool m_is_error;
-};
-
 /// \class DecodedThread
 /// Class holding the instructions and function call hierarchy obtained from
 /// decoding a trace, as well as a position cursor used when reverse debugging
@@ -178,14 +123,32 @@
   /// Append a decoding error (i.e. an instruction that failed to be decoded).
   void AppendError(llvm::Error &&error);
 
-  /// Get the instructions from the decoded trace. Some of them might indicate
-  /// errors (i.e. gaps) in the trace. For an instruction error, you can access
-  /// its underlying error message with the \a GetErrorByInstructionIndex()
-  /// method.
+  /// Get the instruction pointers from the decoded trace. Some of them might
+  /// indicate errors (i.e. gaps) in the trace. For an instruction error, you
+  /// can access its underlying error message with the \a
+  /// GetErrorByInstructionIndex() method.
   ///
   /// \return
   ///   The instructions of the trace.
-  llvm::ArrayRef GetIns

[Lldb-commits] [PATCH] D122991: [lldb][intelpt] Remove `IntelPTInstruction` and move methods to `DecodedThread`

2022-04-04 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 420245.
zrthxn added a comment.

Change in memory use is appreciable.

  thread #1: tid = 40371
Raw trace size: 4 KiB
Total number of instructions: 145011
Total approximate memory usage: 1840.96 KiB
Average memory usage per instruction: 13.00 bytes
  
Number of TSC decoding errors: 0

Tests still need to be updated.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122991/new/

https://reviews.llvm.org/D122991

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp

Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -112,7 +112,7 @@
   }
   s << "\n";
   DecodedThreadSP decoded_trace_sp = Decode(thread);
-  size_t insn_len = decoded_trace_sp->GetInstructions().size();
+  size_t insn_len = decoded_trace_sp->GetInstructionPointers().size();
   size_t mem_used = decoded_trace_sp->CalculateApproximateMemoryUsage();
 
   s.Format("  Raw trace size: {0} KiB\n", *raw_size / 1024);
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -20,14 +20,14 @@
 TraceCursorIntelPT::TraceCursorIntelPT(ThreadSP thread_sp,
DecodedThreadSP decoded_thread_sp)
 : TraceCursor(thread_sp), m_decoded_thread_sp(decoded_thread_sp) {
-  assert(!m_decoded_thread_sp->GetInstructions().empty() &&
+  assert(!m_decoded_thread_sp->GetInstructionPointers().empty() &&
  "a trace should have at least one instruction or error");
-  m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_pos = m_decoded_thread_sp->GetInstructionPointers().size() - 1;
   m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
-  return m_decoded_thread_sp->GetInstructions().size();
+  return m_decoded_thread_sp->GetInstructionPointers().size();
 }
 
 bool TraceCursorIntelPT::Next() {
@@ -78,8 +78,8 @@
   return std::abs(dist);
 }
   };
-  
-	int64_t dist = FindDistanceAndSetPos();
+
+  int64_t dist = FindDistanceAndSetPos();
   m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos);
   return dist;
 }
@@ -93,7 +93,7 @@
 }
 
 lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetLoadAddress();
+  return m_decoded_thread_sp->GetInstructionLoadAddress(m_pos);
 }
 
 Optional
@@ -111,8 +111,8 @@
 TraceCursorIntelPT::GetInstructionControlFlowType() {
   lldb::addr_t next_load_address =
   m_pos + 1 < GetInternalInstructionSize()
-  ? m_decoded_thread_sp->GetInstructions()[m_pos + 1].GetLoadAddress()
+  ? m_decoded_thread_sp->GetInstructionLoadAddress(m_pos + 1)
   : LLDB_INVALID_ADDRESS;
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetControlFlowType(
-  next_load_address);
+  return m_decoded_thread_sp->GetInstructionControlFlowType(m_pos,
+next_load_address);
 }
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -51,61 +51,6 @@
   lldb::addr_t m_address;
 };
 
-/// \class IntelPTInstruction
-/// An instruction obtained from decoding a trace. It is either an actual
-/// instruction or an error indicating a gap in the trace.
-///
-/// Gaps in the trace can come in a few flavors:
-///   - tracing gaps (e.g. tracing was paused and then resumed)
-///   - tracing errors (e.g. buffer overflow)
-///   - decoding errors (e.g. some memory region couldn't be decoded)
-/// As mentioned, any gap is represented as an error in this class.
-class IntelPTInstruction {
-public:
-  IntelPTInstruction(const pt_insn &pt_insn)
-  : m_pt_insn(pt_insn), m_is_error(false) {}
-
-  /// Error constructor
-  IntelPTInstruction();
-
-  /// Check if this object represents an error (i.e. a gap).
-  ///
-  /// \return
-  /// Whether this object represents an error.
-  bool IsError() const;
-
-  /// \return
-  /// The instruction pointer address, or \a LLDB_INVALID_ADDRESS if it is
-  /// an error.
-  lldb::addr_t GetLoadAddress() const;
-
-  /// Get the size in bytes of an instance of this class
-  static size_t GetMemoryUsage();
-
-  /// Get the \a lldb::TraceInstructionControlFlowType categories of the
-  /// instruction.
-  ///
-  /// \param[

[Lldb-commits] [PATCH] D122991: [lldb][intelpt] Remove `IntelPTInstruction` and move methods to `DecodedThread`

2022-04-04 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 420256.
zrthxn added a comment.

Updated tests


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122991/new/

https://reviews.llvm.org/D122991

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py
  lldb/test/API/commands/trace/TestTraceLoad.py

Index: lldb/test/API/commands/trace/TestTraceLoad.py
===
--- lldb/test/API/commands/trace/TestTraceLoad.py
+++ lldb/test/API/commands/trace/TestTraceLoad.py
@@ -38,8 +38,10 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 0.98 KiB
-  Average memory usage per instruction: 48.00 bytes'''])
+  Total approximate memory usage: 0.27 KiB
+  Average memory usage per instruction: 13.00 bytes
+
+  Number of TSC decoding errors: 0'''])
 
 def testLoadInvalidTraces(self):
 src_dir = self.getSourceDir()
Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -40,7 +40,7 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 0.98 KiB
-  Average memory usage per instruction: 48.00 bytes
+  Total approximate memory usage: 0.27 KiB
+  Average memory usage per instruction: 13.00 bytes
 
   Number of TSC decoding errors: 0'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -112,7 +112,7 @@
   }
   s << "\n";
   DecodedThreadSP decoded_trace_sp = Decode(thread);
-  size_t insn_len = decoded_trace_sp->GetInstructions().size();
+  size_t insn_len = decoded_trace_sp->GetInstructionPointers().size();
   size_t mem_used = decoded_trace_sp->CalculateApproximateMemoryUsage();
 
   s.Format("  Raw trace size: {0} KiB\n", *raw_size / 1024);
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -20,14 +20,14 @@
 TraceCursorIntelPT::TraceCursorIntelPT(ThreadSP thread_sp,
DecodedThreadSP decoded_thread_sp)
 : TraceCursor(thread_sp), m_decoded_thread_sp(decoded_thread_sp) {
-  assert(!m_decoded_thread_sp->GetInstructions().empty() &&
+  assert(!m_decoded_thread_sp->GetInstructionPointers().empty() &&
  "a trace should have at least one instruction or error");
-  m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_pos = m_decoded_thread_sp->GetInstructionPointers().size() - 1;
   m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
-  return m_decoded_thread_sp->GetInstructions().size();
+  return m_decoded_thread_sp->GetInstructionPointers().size();
 }
 
 bool TraceCursorIntelPT::Next() {
@@ -78,8 +78,8 @@
   return std::abs(dist);
 }
   };
-  
-	int64_t dist = FindDistanceAndSetPos();
+
+  int64_t dist = FindDistanceAndSetPos();
   m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos);
   return dist;
 }
@@ -93,7 +93,7 @@
 }
 
 lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetLoadAddress();
+  return m_decoded_thread_sp->GetInstructionLoadAddress(m_pos);
 }
 
 Optional
@@ -111,8 +111,8 @@
 TraceCursorIntelPT::GetInstructionControlFlowType() {
   lldb::addr_t next_load_address =
   m_pos + 1 < GetInternalInstructionSize()
-  ? m_decoded_thread_sp->GetInstructions()[m_pos + 1].GetLoadAddress()
+  ? m_decoded_thread_sp->GetInstructionLoadAddress(m_pos + 1)
   : LLDB_INVALID_ADDRESS;
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetControlFlowType(
-  next_load_address);
+  return m_decoded_thread_sp->GetInstructionControlFlowType(m_pos,
+next_load_address);
 }
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -51,61 +51,6 @@
   lldb::addr_t m_address;
 };
 
-/// \class IntelPTInstruction
-/// An instruction obtained from decoding a trace. It is either an actual
-/// instruction or an error indicating a gap in the 

[Lldb-commits] [PATCH] D122991: [lldb][intelpt] Remove `IntelPTInstruction` and move methods to `DecodedThread`

2022-04-05 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 420401.
zrthxn marked 12 inline comments as done.
zrthxn added a comment.

Address all comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122991/new/

https://reviews.llvm.org/D122991

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py
  lldb/test/API/commands/trace/TestTraceLoad.py

Index: lldb/test/API/commands/trace/TestTraceLoad.py
===
--- lldb/test/API/commands/trace/TestTraceLoad.py
+++ lldb/test/API/commands/trace/TestTraceLoad.py
@@ -38,8 +38,10 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 0.98 KiB
-  Average memory usage per instruction: 48.00 bytes'''])
+  Total approximate memory usage: 0.27 KiB
+  Average memory usage per instruction: 13.00 bytes
+
+  Number of TSC decoding errors: 0'''])
 
 def testLoadInvalidTraces(self):
 src_dir = self.getSourceDir()
Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -40,7 +40,7 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 0.98 KiB
-  Average memory usage per instruction: 48.00 bytes
+  Total approximate memory usage: 0.27 KiB
+  Average memory usage per instruction: 13.00 bytes
 
   Number of TSC decoding errors: 0'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -112,7 +112,7 @@
   }
   s << "\n";
   DecodedThreadSP decoded_trace_sp = Decode(thread);
-  size_t insn_len = decoded_trace_sp->GetInstructions().size();
+  size_t insn_len = decoded_trace_sp->GetInstructionsCount();
   size_t mem_used = decoded_trace_sp->CalculateApproximateMemoryUsage();
 
   s.Format("  Raw trace size: {0} KiB\n", *raw_size / 1024);
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -20,14 +20,14 @@
 TraceCursorIntelPT::TraceCursorIntelPT(ThreadSP thread_sp,
DecodedThreadSP decoded_thread_sp)
 : TraceCursor(thread_sp), m_decoded_thread_sp(decoded_thread_sp) {
-  assert(!m_decoded_thread_sp->GetInstructions().empty() &&
+  assert(m_decoded_thread_sp->GetInstructionsCount() > 0 &&
  "a trace should have at least one instruction or error");
-  m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_pos = m_decoded_thread_sp->GetInstructionsCount() - 1;
   m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
-  return m_decoded_thread_sp->GetInstructions().size();
+  return m_decoded_thread_sp->GetInstructionsCount();
 }
 
 bool TraceCursorIntelPT::Next() {
@@ -78,8 +78,8 @@
   return std::abs(dist);
 }
   };
-  
-	int64_t dist = FindDistanceAndSetPos();
+
+  int64_t dist = FindDistanceAndSetPos();
   m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos);
   return dist;
 }
@@ -93,7 +93,7 @@
 }
 
 lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetLoadAddress();
+  return m_decoded_thread_sp->GetInstructionLoadAddress(m_pos);
 }
 
 Optional
@@ -109,10 +109,5 @@
 
 TraceInstructionControlFlowType
 TraceCursorIntelPT::GetInstructionControlFlowType() {
-  lldb::addr_t next_load_address =
-  m_pos + 1 < GetInternalInstructionSize()
-  ? m_decoded_thread_sp->GetInstructions()[m_pos + 1].GetLoadAddress()
-  : LLDB_INVALID_ADDRESS;
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetControlFlowType(
-  next_load_address);
+  return m_decoded_thread_sp->GetInstructionControlFlowType(m_pos);
 }
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -51,61 +51,6 @@
   lldb::addr_t m_address;
 };
 
-/// \class IntelPTInstruction
-/// An instruction obtained from decoding a trace. It is either an actual
-/// instruction or an error indicating a gap in the trace.
-///
-/// Gaps in the trace can come in a few flavors:
-///   - tracing gaps (e.g. tracing wa

[Lldb-commits] [PATCH] D122991: [lldb][intelpt] Remove `IntelPTInstruction` and move methods to `DecodedThread`

2022-04-05 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn updated this revision to Diff 420543.
zrthxn marked an inline comment as done.
zrthxn added a comment.

Removed unnecessary comment


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122991/new/

https://reviews.llvm.org/D122991

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py
  lldb/test/API/commands/trace/TestTraceLoad.py

Index: lldb/test/API/commands/trace/TestTraceLoad.py
===
--- lldb/test/API/commands/trace/TestTraceLoad.py
+++ lldb/test/API/commands/trace/TestTraceLoad.py
@@ -38,8 +38,10 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 0.98 KiB
-  Average memory usage per instruction: 48.00 bytes'''])
+  Total approximate memory usage: 0.27 KiB
+  Average memory usage per instruction: 13.00 bytes
+
+  Number of TSC decoding errors: 0'''])
 
 def testLoadInvalidTraces(self):
 src_dir = self.getSourceDir()
Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -40,7 +40,7 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 0.98 KiB
-  Average memory usage per instruction: 48.00 bytes
+  Total approximate memory usage: 0.27 KiB
+  Average memory usage per instruction: 13.00 bytes
 
   Number of TSC decoding errors: 0'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -112,7 +112,7 @@
   }
   s << "\n";
   DecodedThreadSP decoded_trace_sp = Decode(thread);
-  size_t insn_len = decoded_trace_sp->GetInstructions().size();
+  size_t insn_len = decoded_trace_sp->GetInstructionsCount();
   size_t mem_used = decoded_trace_sp->CalculateApproximateMemoryUsage();
 
   s.Format("  Raw trace size: {0} KiB\n", *raw_size / 1024);
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -20,14 +20,14 @@
 TraceCursorIntelPT::TraceCursorIntelPT(ThreadSP thread_sp,
DecodedThreadSP decoded_thread_sp)
 : TraceCursor(thread_sp), m_decoded_thread_sp(decoded_thread_sp) {
-  assert(!m_decoded_thread_sp->GetInstructions().empty() &&
+  assert(m_decoded_thread_sp->GetInstructionsCount() > 0 &&
  "a trace should have at least one instruction or error");
-  m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_pos = m_decoded_thread_sp->GetInstructionsCount() - 1;
   m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
-  return m_decoded_thread_sp->GetInstructions().size();
+  return m_decoded_thread_sp->GetInstructionsCount();
 }
 
 bool TraceCursorIntelPT::Next() {
@@ -78,8 +78,8 @@
   return std::abs(dist);
 }
   };
-  
-	int64_t dist = FindDistanceAndSetPos();
+
+  int64_t dist = FindDistanceAndSetPos();
   m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos);
   return dist;
 }
@@ -93,7 +93,7 @@
 }
 
 lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetLoadAddress();
+  return m_decoded_thread_sp->GetInstructionLoadAddress(m_pos);
 }
 
 Optional
@@ -109,10 +109,5 @@
 
 TraceInstructionControlFlowType
 TraceCursorIntelPT::GetInstructionControlFlowType() {
-  lldb::addr_t next_load_address =
-  m_pos + 1 < GetInternalInstructionSize()
-  ? m_decoded_thread_sp->GetInstructions()[m_pos + 1].GetLoadAddress()
-  : LLDB_INVALID_ADDRESS;
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetControlFlowType(
-  next_load_address);
+  return m_decoded_thread_sp->GetInstructionControlFlowType(m_pos);
 }
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -51,61 +51,6 @@
   lldb::addr_t m_address;
 };
 
-/// \class IntelPTInstruction
-/// An instruction obtained from decoding a trace. It is either an actual
-/// instruction or an error indicating a gap in the trace.
-///
-/// Gaps in the trace can come in a few flavors:
-///   - tracing gaps (e.g. trac

[Lldb-commits] [PATCH] D122991: [lldb][intelpt] Remove `IntelPTInstruction` and move methods to `DecodedThread`

2022-04-05 Thread Alisamar Husain via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGd849959071c8: [lldb][intelpt] Remove `IntelPTInstruction` 
and move methods to `DecodedThread` (authored by zrthxn).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122991/new/

https://reviews.llvm.org/D122991

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
  lldb/test/API/commands/trace/TestTraceDumpInfo.py
  lldb/test/API/commands/trace/TestTraceLoad.py

Index: lldb/test/API/commands/trace/TestTraceLoad.py
===
--- lldb/test/API/commands/trace/TestTraceLoad.py
+++ lldb/test/API/commands/trace/TestTraceLoad.py
@@ -38,8 +38,10 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 0.98 KiB
-  Average memory usage per instruction: 48.00 bytes'''])
+  Total approximate memory usage: 0.27 KiB
+  Average memory usage per instruction: 13.00 bytes
+
+  Number of TSC decoding errors: 0'''])
 
 def testLoadInvalidTraces(self):
 src_dir = self.getSourceDir()
Index: lldb/test/API/commands/trace/TestTraceDumpInfo.py
===
--- lldb/test/API/commands/trace/TestTraceDumpInfo.py
+++ lldb/test/API/commands/trace/TestTraceDumpInfo.py
@@ -40,7 +40,7 @@
 thread #1: tid = 3842849
   Raw trace size: 4 KiB
   Total number of instructions: 21
-  Total approximate memory usage: 0.98 KiB
-  Average memory usage per instruction: 48.00 bytes
+  Total approximate memory usage: 0.27 KiB
+  Average memory usage per instruction: 13.00 bytes
 
   Number of TSC decoding errors: 0'''])
Index: lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceIntelPT.cpp
@@ -112,7 +112,7 @@
   }
   s << "\n";
   DecodedThreadSP decoded_trace_sp = Decode(thread);
-  size_t insn_len = decoded_trace_sp->GetInstructions().size();
+  size_t insn_len = decoded_trace_sp->GetInstructionsCount();
   size_t mem_used = decoded_trace_sp->CalculateApproximateMemoryUsage();
 
   s.Format("  Raw trace size: {0} KiB\n", *raw_size / 1024);
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -20,14 +20,14 @@
 TraceCursorIntelPT::TraceCursorIntelPT(ThreadSP thread_sp,
DecodedThreadSP decoded_thread_sp)
 : TraceCursor(thread_sp), m_decoded_thread_sp(decoded_thread_sp) {
-  assert(!m_decoded_thread_sp->GetInstructions().empty() &&
+  assert(m_decoded_thread_sp->GetInstructionsCount() > 0 &&
  "a trace should have at least one instruction or error");
-  m_pos = m_decoded_thread_sp->GetInstructions().size() - 1;
+  m_pos = m_decoded_thread_sp->GetInstructionsCount() - 1;
   m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
-  return m_decoded_thread_sp->GetInstructions().size();
+  return m_decoded_thread_sp->GetInstructionsCount();
 }
 
 bool TraceCursorIntelPT::Next() {
@@ -78,8 +78,8 @@
   return std::abs(dist);
 }
   };
-  
-	int64_t dist = FindDistanceAndSetPos();
+
+  int64_t dist = FindDistanceAndSetPos();
   m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos);
   return dist;
 }
@@ -93,7 +93,7 @@
 }
 
 lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetLoadAddress();
+  return m_decoded_thread_sp->GetInstructionLoadAddress(m_pos);
 }
 
 Optional
@@ -109,10 +109,5 @@
 
 TraceInstructionControlFlowType
 TraceCursorIntelPT::GetInstructionControlFlowType() {
-  lldb::addr_t next_load_address =
-  m_pos + 1 < GetInternalInstructionSize()
-  ? m_decoded_thread_sp->GetInstructions()[m_pos + 1].GetLoadAddress()
-  : LLDB_INVALID_ADDRESS;
-  return m_decoded_thread_sp->GetInstructions()[m_pos].GetControlFlowType(
-  next_load_address);
+  return m_decoded_thread_sp->GetInstructionControlFlowType(m_pos);
 }
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -51,61 +51,6 @@
   lldb::addr_t m_address;
 };
 
-/// \class IntelPTInstruction
-/// An instruction obtained from decoding a trace. It is either an actual
-/// instruction or an error indicating a gap in the trace.
-///
-/// Gaps in

[Lldb-commits] [PATCH] D123375: [lldb][intelpt] Reduce trace memory usage by grouping instructions

2022-04-08 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn created this revision.
zrthxn added reviewers: wallace, jj10306.
Herald added a project: All.
zrthxn requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Grouping instructions with the same high 48 bits and storing prefixes. Simple 
lookup improved to get xponentially faster instruction address lookups.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D123375

Files:
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.cpp
  lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
  lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
  lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h

Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.h
@@ -46,6 +46,8 @@
   DecodedThreadSP m_decoded_thread_sp;
   /// Internal instruction index currently pointing at.
   size_t m_pos;
+  /// Internal instruction ID currently pointing at.
+  lldb::user_id_t m_id;
   /// Tsc range covering the current instruction.
   llvm::Optional m_tsc_range;
 };
Index: lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
+++ lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
@@ -23,8 +23,9 @@
   assert(m_decoded_thread_sp->GetInstructionsCount() > 0 &&
  "a trace should have at least one instruction or error");
   m_pos = m_decoded_thread_sp->GetInstructionsCount() - 1;
+  m_id = m_decoded_thread_sp->ToID(m_pos);
   m_tsc_range =
-  m_decoded_thread_sp->CalculateTscRange(m_pos, /*hint_range*/ None);
+  m_decoded_thread_sp->CalculateTscRangeByIndex(m_pos, /*hint_range*/ None);
 }
 
 size_t TraceCursorIntelPT::GetInternalInstructionSize() {
@@ -42,6 +43,7 @@
 
   while (canMoveOne()) {
 m_pos += IsForwards() ? 1 : -1;
+m_id = m_decoded_thread_sp->ToID(m_pos);
 
 if (m_tsc_range && !m_tsc_range->InRange(m_pos))
   m_tsc_range = IsForwards() ? m_tsc_range->Next() : m_tsc_range->Prev();
@@ -81,12 +83,14 @@
   };
 
   int64_t dist = FindDistanceAndSetPos();
-  m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos, m_tsc_range);
+  m_id = m_decoded_thread_sp->ToID(m_pos);
+  m_tsc_range =
+  m_decoded_thread_sp->CalculateTscRangeByIndex(m_pos, m_tsc_range);
   return dist;
 }
 
 bool TraceCursorIntelPT::IsError() {
-  return m_decoded_thread_sp->IsInstructionAnError(m_pos);
+  return m_decoded_thread_sp->IsInstructionAnError(m_id);
 }
 
 const char *TraceCursorIntelPT::GetError() {
@@ -94,7 +98,7 @@
 }
 
 lldb::addr_t TraceCursorIntelPT::GetLoadAddress() {
-  return m_decoded_thread_sp->GetInstructionLoadAddress(m_pos);
+  return m_decoded_thread_sp->GetInstructionLoadAddress(m_id);
 }
 
 Optional
@@ -110,16 +114,19 @@
 
 TraceInstructionControlFlowType
 TraceCursorIntelPT::GetInstructionControlFlowType() {
-  return m_decoded_thread_sp->GetInstructionControlFlowType(m_pos);
+  return m_decoded_thread_sp->GetInstructionControlFlowType(m_id);
 }
 
 bool TraceCursorIntelPT::GoToId(user_id_t id) {
-  if (m_decoded_thread_sp->GetInstructionsCount() <= id)
+  size_t idx = m_decoded_thread_sp->ToIndex(id);
+  if (m_decoded_thread_sp->GetInstructionsCount() <= idx)
 return false;
-  m_pos = id;
-  m_tsc_range = m_decoded_thread_sp->CalculateTscRange(m_pos, m_tsc_range);
+  m_pos = idx;
+  m_id = id;
+  m_tsc_range =
+  m_decoded_thread_sp->CalculateTscRangeByIndex(m_pos, m_tsc_range);
 
   return true;
 }
 
-user_id_t TraceCursorIntelPT::GetId() const { return m_pos; }
+user_id_t TraceCursorIntelPT::GetId() const { return m_id; }
Index: lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
===
--- lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
+++ lldb/source/Plugins/Trace/intel-pt/IntelPTDecoder.cpp
@@ -197,6 +197,8 @@
   AppendInstruction(decoded_thread, insn, tsc_info);
 }
   }
+
+  decoded_thread.CalcIPBlockSizes();
 }
 
 /// Callback used by libipt for reading the process memory.
Index: lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
===
--- lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
+++ lldb/source/Plugins/Trace/intel-pt/DecodedThread.h
@@ -109,6 +109,31 @@
 void RecordError(int libipt_error_code);
   };
 
+  /// \struct InstructionPointer
+  /// Structure that stores a 6 byte prefix a list of 2 byte suffixes for each
+  /// instruction that is appended to the trace, in order. Concatenating the
+  /// prefix and suffix for any instruction ID (which is the concatenation of
+  /// two indices) will give the instruction load address.
+  ///
+  /// Generally most consecutive inst

[Lldb-commits] [PATCH] D123375: [lldb][intelpt] Reduce trace memory usage by grouping instructions

2022-04-08 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn added a comment.

Example of improvement in memory usage

  thread #1: tid = 42805
Raw trace size: 4096 KiB
Total number of instructions: 94
Total approximate memory usage: 4394.58 KiB
Average memory usage per instruction: 5.00 bytes

where previously the same trace took ~12 KiB and 13 bytes per instruction


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D123375/new/

https://reviews.llvm.org/D123375

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D123375: [lldb][intelpt] Reduce trace memory usage by grouping instructions

2022-04-18 Thread Alisamar Husain via Phabricator via lldb-commits
zrthxn added a comment.

I've tried most of these changes to see the effect it has, but in my opinion 
this adds quite a lot of code complexity for not enough benefit in terms of 
memory usage which was our goal. I think this will make the DecodedThread even 
more of a monolith class with single-use subclasses... are you sure we should 
continue with this?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D123375/new/

https://reviews.llvm.org/D123375

___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits