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 ValueObject &valobj, Stream &stream,
150 const TypeSummaryOptions &summary_options) {
151 return formatStringImpl<StringElementType::UTF16>(valobj, stream,
152 summary_options, "L");
153}
154
155template <>
157 StringElementType::ASCII>(ValueObject &valobj, Stream &stream,
158 const TypeSummaryOptions &summary_options) {
160 valobj, stream, summary_options, "");
161}
162template <>
164 StringElementType::UTF8>(ValueObject &valobj, Stream &stream,
165 const TypeSummaryOptions &summary_options) {
167 valobj, stream, summary_options, "u8");
168}
169template <>
171 StringElementType::UTF16>(ValueObject &valobj, Stream &stream,
172 const TypeSummaryOptions &summary_options) {
174 valobj, stream, summary_options, "u");
175}
176template <>
178 StringElementType::UTF32>(ValueObject &valobj, Stream &stream,
179 const TypeSummaryOptions &summary_options) {
181 valobj, stream, summary_options, "U");
182}
183
185 ValueObject &valobj, Stream &stream,
186 const TypeSummaryOptions &summary_options) {
188 summary_options, "L");
189}
190
191template <>
193 StringElementType::ASCII>(ValueObject &valobj, Stream &stream,
194 const TypeSummaryOptions &summary_options) {
196 summary_options, "");
197}
198template <>
200 StringElementType::UTF8>(ValueObject &valobj, Stream &stream,
201 const TypeSummaryOptions &summary_options) {
203 summary_options, "u8");
204}
205template <>
207 StringElementType::UTF16>(ValueObject &valobj, Stream &stream,
208 const TypeSummaryOptions &summary_options) {
210 summary_options, "u");
211}
212template <>
214 StringElementType::UTF32>(ValueObject &valobj, Stream &stream,
215 const TypeSummaryOptions &summary_options) {
217 summary_options, "U");
218}
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 MsvcStlWStringViewSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition MsvcStl.cpp:184
bool MsvcStlWStringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition MsvcStl.cpp:148
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP