FormattersHelpers.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //===-- FormattersHelpers.h --------------------------------------*- C++
  2. //-*-===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef LLDB_DATAFORMATTERS_FORMATTERSHELPERS_H
  10. #define LLDB_DATAFORMATTERS_FORMATTERSHELPERS_H
  11. #include "lldb/lldb-enumerations.h"
  12. #include "lldb/lldb-forward.h"
  13. #include "lldb/DataFormatters/TypeCategory.h"
  14. #include "lldb/DataFormatters/TypeFormat.h"
  15. #include "lldb/DataFormatters/TypeSummary.h"
  16. #include "lldb/DataFormatters/TypeSynthetic.h"
  17. namespace lldb_private {
  18. namespace formatters {
  19. void AddFormat(TypeCategoryImpl::SharedPointer category_sp, lldb::Format format,
  20. ConstString type_name, TypeFormatImpl::Flags flags,
  21. bool regex = false);
  22. void AddSummary(TypeCategoryImpl::SharedPointer category_sp,
  23. lldb::TypeSummaryImplSP summary_sp, ConstString type_name,
  24. bool regex = false);
  25. void AddStringSummary(TypeCategoryImpl::SharedPointer category_sp,
  26. const char *string, ConstString type_name,
  27. TypeSummaryImpl::Flags flags, bool regex = false);
  28. void AddOneLineSummary(TypeCategoryImpl::SharedPointer category_sp,
  29. ConstString type_name, TypeSummaryImpl::Flags flags,
  30. bool regex = false);
  31. /// Add a summary that is implemented by a C++ callback.
  32. void AddCXXSummary(TypeCategoryImpl::SharedPointer category_sp,
  33. CXXFunctionSummaryFormat::Callback funct,
  34. const char *description, ConstString type_name,
  35. TypeSummaryImpl::Flags flags, bool regex = false);
  36. /// Add a synthetic that is implemented by a C++ callback.
  37. void AddCXXSynthetic(TypeCategoryImpl::SharedPointer category_sp,
  38. CXXSyntheticChildren::CreateFrontEndCallback generator,
  39. const char *description, ConstString type_name,
  40. ScriptedSyntheticChildren::Flags flags,
  41. bool regex = false);
  42. void AddFilter(TypeCategoryImpl::SharedPointer category_sp,
  43. std::vector<std::string> children, const char *description,
  44. ConstString type_name, ScriptedSyntheticChildren::Flags flags,
  45. bool regex = false);
  46. size_t ExtractIndexFromString(const char *item_name);
  47. lldb::addr_t GetArrayAddressOrPointerValue(ValueObject &valobj);
  48. lldb::ValueObjectSP GetValueOfLibCXXCompressedPair(ValueObject &pair);
  49. time_t GetOSXEpoch();
  50. struct InferiorSizedWord {
  51. InferiorSizedWord(const InferiorSizedWord &word) : ptr_size(word.ptr_size) {
  52. if (ptr_size == 4)
  53. thirty_two = word.thirty_two;
  54. else
  55. sixty_four = word.sixty_four;
  56. }
  57. InferiorSizedWord operator=(const InferiorSizedWord &word) {
  58. ptr_size = word.ptr_size;
  59. if (ptr_size == 4)
  60. thirty_two = word.thirty_two;
  61. else
  62. sixty_four = word.sixty_four;
  63. return *this;
  64. }
  65. InferiorSizedWord(uint64_t val, Process &process)
  66. : ptr_size(process.GetAddressByteSize()) {
  67. if (ptr_size == 4)
  68. thirty_two = (uint32_t)val;
  69. else if (ptr_size == 8)
  70. sixty_four = val;
  71. else
  72. assert(false && "new pointer size is unknown");
  73. }
  74. bool IsNegative() const {
  75. if (ptr_size == 4)
  76. return ((int32_t)thirty_two) < 0;
  77. else
  78. return ((int64_t)sixty_four) < 0;
  79. }
  80. bool IsZero() const {
  81. if (ptr_size == 4)
  82. return thirty_two == 0;
  83. else
  84. return sixty_four == 0;
  85. }
  86. static InferiorSizedWord GetMaximum(Process &process) {
  87. if (process.GetAddressByteSize() == 4)
  88. return InferiorSizedWord(UINT32_MAX, 4);
  89. else
  90. return InferiorSizedWord(UINT64_MAX, 8);
  91. }
  92. InferiorSizedWord operator>>(int rhs) const {
  93. if (ptr_size == 4)
  94. return InferiorSizedWord(thirty_two >> rhs, 4);
  95. return InferiorSizedWord(sixty_four >> rhs, 8);
  96. }
  97. InferiorSizedWord operator<<(int rhs) const {
  98. if (ptr_size == 4)
  99. return InferiorSizedWord(thirty_two << rhs, 4);
  100. return InferiorSizedWord(sixty_four << rhs, 8);
  101. }
  102. InferiorSizedWord operator&(const InferiorSizedWord &word) const {
  103. if (ptr_size != word.ptr_size)
  104. return InferiorSizedWord(0, ptr_size);
  105. if (ptr_size == 4)
  106. return InferiorSizedWord(thirty_two & word.thirty_two, 4);
  107. return InferiorSizedWord(sixty_four & word.sixty_four, 8);
  108. }
  109. InferiorSizedWord operator&(int x) const {
  110. if (ptr_size == 4)
  111. return InferiorSizedWord(thirty_two & x, 4);
  112. return InferiorSizedWord(sixty_four & x, 8);
  113. }
  114. size_t GetBitSize() const { return ptr_size << 3; }
  115. size_t GetByteSize() const { return ptr_size; }
  116. uint64_t GetValue() const {
  117. if (ptr_size == 4)
  118. return (uint64_t)thirty_two;
  119. return sixty_four;
  120. }
  121. InferiorSizedWord SignExtend() const {
  122. if (ptr_size == 4)
  123. return InferiorSizedWord((int32_t)thirty_two, 4);
  124. return InferiorSizedWord((int64_t)sixty_four, 8);
  125. }
  126. uint8_t *CopyToBuffer(uint8_t *buffer) const {
  127. if (ptr_size == 4) {
  128. memcpy(buffer, &thirty_two, 4);
  129. return buffer + 4;
  130. } else {
  131. memcpy(buffer, &sixty_four, 8);
  132. return buffer + 8;
  133. }
  134. }
  135. DataExtractor
  136. GetAsData(lldb::ByteOrder byte_order = lldb::eByteOrderInvalid) const {
  137. if (ptr_size == 4)
  138. return DataExtractor(&thirty_two, 4, byte_order, 4);
  139. else
  140. return DataExtractor(&sixty_four, 8, byte_order, 8);
  141. }
  142. private:
  143. InferiorSizedWord(uint64_t val, size_t psz) : ptr_size(psz) {
  144. if (ptr_size == 4)
  145. thirty_two = (uint32_t)val;
  146. else
  147. sixty_four = val;
  148. }
  149. size_t ptr_size;
  150. union {
  151. uint32_t thirty_two;
  152. uint64_t sixty_four;
  153. };
  154. };
  155. } // namespace formatters
  156. } // namespace lldb_private
  157. #endif // LLDB_DATAFORMATTERS_FORMATTERSHELPERS_H