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 | |
10 | #ifndef LLDB_DATAFORMATTERS_FORMATTERSHELPERS_H |
11 | #define LLDB_DATAFORMATTERS_FORMATTERSHELPERS_H |
12 | |
13 | #include "lldb/lldb-enumerations.h" |
14 | #include "lldb/lldb-forward.h" |
15 | |
16 | #include "lldb/DataFormatters/TypeCategory.h" |
17 | #include "lldb/DataFormatters/TypeFormat.h" |
18 | #include "lldb/DataFormatters/TypeSummary.h" |
19 | #include "lldb/DataFormatters/TypeSynthetic.h" |
20 | |
21 | namespace lldb_private { |
22 | namespace formatters { |
23 | void AddFormat(TypeCategoryImpl::SharedPointer category_sp, lldb::Format format, |
24 | llvm::StringRef type_name, TypeFormatImpl::Flags flags, |
25 | bool regex = false); |
26 | |
27 | void AddSummary(TypeCategoryImpl::SharedPointer category_sp, |
28 | lldb::TypeSummaryImplSP summary_sp, llvm::StringRef type_name, |
29 | bool regex = false); |
30 | |
31 | void AddStringSummary(TypeCategoryImpl::SharedPointer category_sp, |
32 | const char *string, llvm::StringRef type_name, |
33 | TypeSummaryImpl::Flags flags, bool regex = false); |
34 | |
35 | void AddOneLineSummary(TypeCategoryImpl::SharedPointer category_sp, |
36 | llvm::StringRef type_name, TypeSummaryImpl::Flags flags, |
37 | bool regex = false); |
38 | |
39 | /// Add a summary that is implemented by a C++ callback. |
40 | void AddCXXSummary(TypeCategoryImpl::SharedPointer category_sp, |
41 | CXXFunctionSummaryFormat::Callback funct, |
42 | const char *description, llvm::StringRef type_name, |
43 | TypeSummaryImpl::Flags flags, bool regex = false); |
44 | |
45 | /// Add a synthetic that is implemented by a C++ callback. |
46 | void AddCXXSynthetic(TypeCategoryImpl::SharedPointer category_sp, |
47 | CXXSyntheticChildren::CreateFrontEndCallback generator, |
48 | const char *description, llvm::StringRef type_name, |
49 | ScriptedSyntheticChildren::Flags flags, |
50 | bool regex = false); |
51 | |
52 | void AddFilter(TypeCategoryImpl::SharedPointer category_sp, |
53 | std::vector<std::string> children, const char *description, |
54 | llvm::StringRef type_name, |
55 | ScriptedSyntheticChildren::Flags flags, bool regex = false); |
56 | |
57 | size_t (const char *item_name); |
58 | |
59 | Address GetArrayAddressOrPointerValue(ValueObject &valobj); |
60 | |
61 | time_t GetOSXEpoch(); |
62 | |
63 | struct InferiorSizedWord { |
64 | |
65 | InferiorSizedWord(const InferiorSizedWord &word) : ptr_size(word.ptr_size) { |
66 | if (ptr_size == 4) |
67 | thirty_two = word.thirty_two; |
68 | else |
69 | sixty_four = word.sixty_four; |
70 | } |
71 | |
72 | InferiorSizedWord operator=(const InferiorSizedWord &word) { |
73 | ptr_size = word.ptr_size; |
74 | if (ptr_size == 4) |
75 | thirty_two = word.thirty_two; |
76 | else |
77 | sixty_four = word.sixty_four; |
78 | return *this; |
79 | } |
80 | |
81 | InferiorSizedWord(uint64_t val, Process &process) |
82 | : ptr_size(process.GetAddressByteSize()) { |
83 | if (ptr_size == 4) |
84 | thirty_two = (uint32_t)val; |
85 | else if (ptr_size == 8) |
86 | sixty_four = val; |
87 | else |
88 | assert(false && "new pointer size is unknown" ); |
89 | } |
90 | |
91 | bool IsNegative() const { |
92 | if (ptr_size == 4) |
93 | return ((int32_t)thirty_two) < 0; |
94 | else |
95 | return ((int64_t)sixty_four) < 0; |
96 | } |
97 | |
98 | bool IsZero() const { |
99 | if (ptr_size == 4) |
100 | return thirty_two == 0; |
101 | else |
102 | return sixty_four == 0; |
103 | } |
104 | |
105 | static InferiorSizedWord GetMaximum(Process &process) { |
106 | if (process.GetAddressByteSize() == 4) |
107 | return InferiorSizedWord(UINT32_MAX, 4); |
108 | else |
109 | return InferiorSizedWord(UINT64_MAX, 8); |
110 | } |
111 | |
112 | InferiorSizedWord operator>>(int rhs) const { |
113 | if (ptr_size == 4) |
114 | return InferiorSizedWord(thirty_two >> rhs, 4); |
115 | return InferiorSizedWord(sixty_four >> rhs, 8); |
116 | } |
117 | |
118 | InferiorSizedWord operator<<(int rhs) const { |
119 | if (ptr_size == 4) |
120 | return InferiorSizedWord(thirty_two << rhs, 4); |
121 | return InferiorSizedWord(sixty_four << rhs, 8); |
122 | } |
123 | |
124 | InferiorSizedWord operator&(const InferiorSizedWord &word) const { |
125 | if (ptr_size != word.ptr_size) |
126 | return InferiorSizedWord(0, ptr_size); |
127 | if (ptr_size == 4) |
128 | return InferiorSizedWord(thirty_two & word.thirty_two, 4); |
129 | return InferiorSizedWord(sixty_four & word.sixty_four, 8); |
130 | } |
131 | |
132 | InferiorSizedWord operator&(int x) const { |
133 | if (ptr_size == 4) |
134 | return InferiorSizedWord(thirty_two & x, 4); |
135 | return InferiorSizedWord(sixty_four & x, 8); |
136 | } |
137 | |
138 | size_t GetBitSize() const { return ptr_size << 3; } |
139 | |
140 | size_t GetByteSize() const { return ptr_size; } |
141 | |
142 | uint64_t GetValue() const { |
143 | if (ptr_size == 4) |
144 | return (uint64_t)thirty_two; |
145 | return sixty_four; |
146 | } |
147 | |
148 | InferiorSizedWord SignExtend() const { |
149 | if (ptr_size == 4) |
150 | return InferiorSizedWord((int32_t)thirty_two, 4); |
151 | return InferiorSizedWord((int64_t)sixty_four, 8); |
152 | } |
153 | |
154 | uint8_t *CopyToBuffer(uint8_t *buffer) const { |
155 | if (ptr_size == 4) { |
156 | memcpy(dest: buffer, src: &thirty_two, n: 4); |
157 | return buffer + 4; |
158 | } else { |
159 | memcpy(dest: buffer, src: &sixty_four, n: 8); |
160 | return buffer + 8; |
161 | } |
162 | } |
163 | |
164 | DataExtractor |
165 | GetAsData(lldb::ByteOrder byte_order = lldb::eByteOrderInvalid) const { |
166 | if (ptr_size == 4) |
167 | return DataExtractor(&thirty_two, 4, byte_order, 4); |
168 | else |
169 | return DataExtractor(&sixty_four, 8, byte_order, 8); |
170 | } |
171 | |
172 | private: |
173 | InferiorSizedWord(uint64_t val, size_t psz) : ptr_size(psz) { |
174 | if (ptr_size == 4) |
175 | thirty_two = (uint32_t)val; |
176 | else |
177 | sixty_four = val; |
178 | } |
179 | |
180 | size_t ptr_size; |
181 | union { |
182 | uint32_t thirty_two; |
183 | uint64_t sixty_four; |
184 | }; |
185 | }; |
186 | } // namespace formatters |
187 | } // namespace lldb_private |
188 | |
189 | #endif // LLDB_DATAFORMATTERS_FORMATTERSHELPERS_H |
190 | |