LLDB mainline
MsvcStl.cpp
Go to the documentation of this file.
1//===-- MsvcStl.cpp -------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "MsvcStl.h"
10
11#include "lldb/Core/Debugger.h"
16#include "lldb/Utility/Status.h"
17#include "lldb/Utility/Stream.h"
19
21
22#include "lldb/lldb-forward.h"
23#include <optional>
24#include <tuple>
25
26using namespace lldb;
27using namespace lldb_private;
28using namespace lldb_private::formatters;
29
31
32template <StringElementType element_type>
33static constexpr uint64_t StringElementByteSize() {
34 switch (element_type) {
35 case StringElementType::ASCII:
36 case StringElementType::UTF8:
37 return 1;
38 case StringElementType::UTF16:
39 return 2;
40 case StringElementType::UTF32:
41 return 3;
42 }
43 return 0;
44}
45
47 return valobj.GetChildAtNamePath({"_Mypair", "_Myval2"});
48}
49
50/// Determine the size in bytes of \p valobj (a MSVC STL std::string object) and
51/// extract its data payload. Return the size + payload pair.
52static std::optional<std::pair<uint64_t, ValueObjectSP>>
53ExtractMsvcStlStringInfo(ValueObject &valobj, uint64_t element_size) {
54 ValueObjectSP valobj_pair_sp = ExtractMsvcStlStringData(valobj);
55 if (!valobj_pair_sp || !valobj_pair_sp->GetError().Success())
56 return {};
57
58 ValueObjectSP size_sp = valobj_pair_sp->GetChildMemberWithName("_Mysize");
59 ValueObjectSP capacity_sp = valobj_pair_sp->GetChildMemberWithName("_Myres");
60 ValueObjectSP bx_sp = valobj_pair_sp->GetChildMemberWithName("_Bx");
61 if (!size_sp || !capacity_sp || !bx_sp)
62 return {};
63
64 bool success = false;
65 uint64_t size = size_sp->GetValueAsUnsigned(0, &success);
66 if (!success)
67 return {};
68 uint64_t capacity = capacity_sp->GetValueAsUnsigned(0, &success);
69 if (!success)
70 return {};
71
72 size_t bufSize = std::max<size_t>(16 / element_size, 1);
73 bool isShortString = capacity < bufSize;
74
75 if (isShortString) {
76 ValueObjectSP buf_sp = bx_sp->GetChildMemberWithName("_Buf");
77 if (buf_sp)
78 return std::make_pair(size, buf_sp);
79 return {};
80 }
81 ValueObjectSP ptr_sp = bx_sp->GetChildMemberWithName("_Ptr");
82 if (ptr_sp)
83 return std::make_pair(size, ptr_sp);
84 return {};
85}
86
87template <StringPrinter::StringElementType element_type>
88static bool
90 const TypeSummaryOptions &summary_options,
91 std::string prefix_token) {
92 auto string_info =
94 if (!string_info)
95 return false;
96 auto [size, location_sp] = *string_info;
97
99 stream, summary_options, location_sp, size, prefix_token);
100}
101template <StringPrinter::StringElementType element_type>
102static bool formatStringImpl(ValueObject &valobj, Stream &stream,
103 const TypeSummaryOptions &summary_options,
104 std::string prefix_token) {
105 StreamString scratch_stream;
107 valobj, scratch_stream, summary_options, prefix_token);
108 if (success)
109 stream << scratch_stream.GetData();
110 else
111 stream << "Summary Unavailable";
112 return true;
113}
114
115template <StringPrinter::StringElementType element_type>
116static bool formatStringViewImpl(ValueObject &valobj, Stream &stream,
117 const TypeSummaryOptions &summary_options,
118 std::string prefix_token) {
119 auto data_sp = valobj.GetChildMemberWithName("_Mydata");
120 auto size_sp = valobj.GetChildMemberWithName("_Mysize");
121 if (!data_sp || !size_sp)
122 return false;
123
124 bool success = false;
125 uint64_t size = size_sp->GetValueAsUnsigned(0, &success);
126 if (!success) {
127 stream << "Summary Unavailable";
128 return true;
129 }
130
131 StreamString scratch_stream;
133 scratch_stream, summary_options, data_sp, size, prefix_token);
134
135 if (success)
136 stream << scratch_stream.GetData();
137 else
138 stream << "Summary Unavailable";
139 return true;
140}
141
143 std::vector<uint32_t> indexes;
144 return valobj.GetCompilerType().GetIndexOfChildMemberWithName("_Mypair", true,
145 indexes) > 0;
146}
147
149 std::vector<uint32_t> indexes;
150 return valobj.GetCompilerType().GetIndexOfChildMemberWithName("_Mydata", true,
151 indexes) > 0;
152}
153
155 ValueObject &valobj, Stream &stream,
156 const TypeSummaryOptions &summary_options) {
157 return formatStringImpl<StringElementType::UTF16>(valobj, stream,
158 summary_options, "L");
159}
160
161template <>
163 StringElementType::ASCII>(ValueObject &valobj, Stream &stream,
164 const TypeSummaryOptions &summary_options) {
166 valobj, stream, summary_options, "");
167}
168template <>
170 StringElementType::UTF8>(ValueObject &valobj, Stream &stream,
171 const TypeSummaryOptions &summary_options) {
173 valobj, stream, summary_options, "u8");
174}
175template <>
177 StringElementType::UTF16>(ValueObject &valobj, Stream &stream,
178 const TypeSummaryOptions &summary_options) {
180 valobj, stream, summary_options, "u");
181}
182template <>
184 StringElementType::UTF32>(ValueObject &valobj, Stream &stream,
185 const TypeSummaryOptions &summary_options) {
187 valobj, stream, summary_options, "U");
188}
189
191 ValueObject &valobj, Stream &stream,
192 const TypeSummaryOptions &summary_options) {
194 summary_options, "L");
195}
196
197template <>
199 StringElementType::ASCII>(ValueObject &valobj, Stream &stream,
200 const TypeSummaryOptions &summary_options) {
202 summary_options, "");
203}
204template <>
206 StringElementType::UTF8>(ValueObject &valobj, Stream &stream,
207 const TypeSummaryOptions &summary_options) {
209 summary_options, "u8");
210}
211template <>
213 StringElementType::UTF16>(ValueObject &valobj, Stream &stream,
214 const TypeSummaryOptions &summary_options) {
216 summary_options, "u");
217}
218template <>
220 StringElementType::UTF32>(ValueObject &valobj, Stream &stream,
221 const TypeSummaryOptions &summary_options) {
223 summary_options, "U");
224}
static std::optional< std::pair< uint64_t, ValueObjectSP > > ExtractMsvcStlStringInfo(ValueObject &valobj, uint64_t element_size)
Determine the size in bytes of valobj (a MSVC STL std::string object) and extract its data payload.
Definition MsvcStl.cpp:53
static bool formatStringImpl(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &summary_options, std::string prefix_token)
Definition MsvcStl.cpp:102
static constexpr uint64_t StringElementByteSize()
Definition MsvcStl.cpp:33
static bool MsvcStlStringSummaryProviderImpl(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &summary_options, std::string prefix_token)
Definition MsvcStl.cpp:89
static bool formatStringViewImpl(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &summary_options, std::string prefix_token)
Definition MsvcStl.cpp:116
static ValueObjectSP ExtractMsvcStlStringData(ValueObject &valobj)
Definition MsvcStl.cpp:46
StringPrinter::StringElementType StringElementType
size_t GetIndexOfChildMemberWithName(llvm::StringRef name, bool omit_empty_base_classes, std::vector< uint32_t > &child_indexes) const
Lookup a child member given a name.
const char * GetData() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
CompilerType GetCompilerType()
virtual lldb::ValueObjectSP GetChildMemberWithName(llvm::StringRef name, bool can_create=true)
lldb::ValueObjectSP GetChildAtNamePath(llvm::ArrayRef< llvm::StringRef > names)
bool IsMsvcStlStringType(ValueObject &valobj)
Definition MsvcStl.cpp:142
bool StringBufferSummaryProvider(Stream &stream, const TypeSummaryOptions &summary_options, lldb::ValueObjectSP location_sp, uint64_t size, std::string prefix_token)
Print a summary for a string buffer to stream.
bool MsvcStlStringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &summary_options)
bool MsvcStlStringViewSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &summary_options)
bool IsMsvcStlStringViewType(ValueObject &valobj)
Definition MsvcStl.cpp:148
bool MsvcStlWStringViewSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition MsvcStl.cpp:190
bool MsvcStlWStringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition MsvcStl.cpp:154
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP