LLDB mainline
DumpRegisterValue.cpp
Go to the documentation of this file.
1//===-- DumpRegisterValue.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
13#include "lldb/Utility/Endian.h"
21#include "llvm/ADT/bit.h"
22
23using namespace lldb;
24
25template <typename T>
27 lldb_private::CompilerType &fields_compiler_type,
28 T value,
31 lldb::ByteOrder target_order = exe_scope->CalculateProcess()->GetByteOrder();
32
33 // For the bitfield types we generate, it is expected that the fields are
34 // in what is usually a big endian order. Most significant field first.
35 // This is also clang's internal ordering and the order we want to print
36 // them. On a big endian host this all matches up, for a little endian
37 // host we have to swap the order of the fields before display.
38 if (target_order == lldb::ByteOrder::eByteOrderLittle) {
39 value = flags_type.ReverseFieldOrder(value);
40 }
41
42 // Then we need to match the target's endian on a byte level as well.
43 if (lldb_private::endian::InlHostByteOrder() != target_order)
44 value = llvm::byteswap(value);
45
46 lldb_private::DataExtractor data_extractor{
47 &value, sizeof(T), lldb_private::endian::InlHostByteOrder(), 8};
48
50 exe_scope, fields_compiler_type, lldb_private::ConstString(),
51 data_extractor);
54 [](llvm::StringRef varname) {
55 // Unnamed bit-fields are padding that we don't want to show.
56 return varname.size();
57 };
58 dump_options.SetChildPrintingDecider(decider).SetHideRootType(true);
59
60 if (llvm::Error error = vobj_sp->Dump(strm, dump_options))
61 strm << "error: " << toString(std::move(error));
62}
63
65 const lldb_private::DataExtractor &data_extractor,
69 exe_scope, type, lldb_private::ConstString(), data_extractor);
71 dump_options.SetHideRootType(true).SetShowSummary(false);
72
73 if (llvm::Error error = vobj_sp->Dump(strm, dump_options))
74 strm << "error: " << toString(std::move(error));
75}
76
78 const RegisterInfo &reg_info,
79 bool prefix_with_name,
80 bool prefix_with_alt_name, Format format,
81 uint32_t reg_name_right_align_at,
82 ExecutionContextScope *exe_scope,
83 bool print_type, TargetSP target_sp) {
84 DataExtractor data;
85 if (!reg_val.GetData(data))
86 return;
87
88 bool name_printed = false;
89 // For simplicity, alignment of the register name printing applies only in
90 // the most common case where:
91 //
92 // prefix_with_name^prefix_with_alt_name is true
93 //
94 StreamString format_string;
95 if (reg_name_right_align_at && (prefix_with_name ^ prefix_with_alt_name))
96 format_string.Printf("%%%us", reg_name_right_align_at);
97 else
98 format_string.Printf("%%s");
99 std::string fmt = std::string(format_string.GetString());
100 if (prefix_with_name) {
101 if (reg_info.name) {
102 s.Printf(fmt.c_str(), reg_info.name);
103 name_printed = true;
104 } else if (reg_info.alt_name) {
105 s.Printf(fmt.c_str(), reg_info.alt_name);
106 prefix_with_alt_name = false;
107 name_printed = true;
108 }
109 }
110 if (prefix_with_alt_name) {
111 if (name_printed)
112 s.PutChar('/');
113 if (reg_info.alt_name) {
114 s.Printf(fmt.c_str(), reg_info.alt_name);
115 name_printed = true;
116 } else if (!name_printed) {
117 // No alternate name but we were asked to display a name, so show the
118 // main name
119 s.Printf(fmt.c_str(), reg_info.name);
120 name_printed = true;
121 }
122 }
123 if (name_printed)
124 s.PutCString(" = ");
125
126 if (format == eFormatDefault)
127 format = reg_info.format;
128
129 DumpDataExtractor(data, &s,
130 0, // Offset in "data"
131 format, // Format to use when dumping
132 reg_info.byte_size, // item_byte_size
133 1, // item_count
134 UINT32_MAX, // num_per_line
135 LLDB_INVALID_ADDRESS, // base_addr
136 0, // item_bit_size
137 0, // item_bit_offset
138 exe_scope);
139
140 if (!print_type || !exe_scope || !target_sp)
141 return;
142
143 const RegisterTypeFlags *flags_type =
144 llvm::dyn_cast_if_present<RegisterTypeFlags>(reg_info.register_type);
145 if (!flags_type &&
146 !llvm::isa_and_present<RegisterTypeVector>(reg_info.register_type))
147 return;
148 if (flags_type && reg_info.byte_size != 4 && reg_info.byte_size != 8)
149 return;
150
151 CompilerType register_compiler_type = target_sp->GetRegisterType(reg_info);
152 if (!register_compiler_type.IsValid())
153 return;
154
155 // Use a new stream so we can remove a trailing newline later.
156 StreamString register_type_stream;
157
158 if (flags_type) {
159 if (reg_info.byte_size == 4) {
160 dump_type_value(*flags_type, register_compiler_type,
161 reg_val.GetAsUInt32(), exe_scope, register_type_stream);
162 } else if (reg_info.byte_size == 8) {
163 dump_type_value(*flags_type, register_compiler_type,
164 reg_val.GetAsUInt64(), exe_scope, register_type_stream);
165 }
166 } else {
167 lldb::ProcessSP process_sp = exe_scope->CalculateProcess();
168 if (!process_sp)
169 return;
170
171 // ValueObjectConstResult interprets its data using the target's layout.
172 DataExtractor structured_data;
173 if (!reg_val.GetData(structured_data, reg_info.byte_size,
174 process_sp->GetByteOrder()))
175 return;
176 structured_data.SetAddressByteSize(process_sp->GetAddressByteSize());
177 dump_type_value(register_compiler_type, structured_data, exe_scope,
178 register_type_stream);
179 }
180
181 // Registers are indented like:
182 // (lldb) register read foo
183 // foo = 0x12345678
184 // So we need to indent to match that.
185
186 // First drop the extra newline that the value printer added. The register
187 // command will add one itself.
188 llvm::StringRef register_type_str =
189 register_type_stream.GetString().drop_back();
190
191 // End the line that contains " foo = 0x12345678".
192 s.EOL();
193
194 // Then split the value lines and indent each one.
195 bool first = true;
196 while (register_type_str.size()) {
197 std::pair<llvm::StringRef, llvm::StringRef> split =
198 register_type_str.split('\n');
199 register_type_str = split.second;
200 // Indent as much as the stream does.
201 s.Indent();
202 // Indent further to match where the register name finishes.
203 s.Printf(fmt.c_str(), "");
204
205 // Lines after the first won't have " = " so compensate for that.
206 if (!first)
207 s << " ";
208 first = false;
209
210 s << split.first;
211
212 // On the last line we don't want a newline because the command will add
213 // one too.
214 if (register_type_str.size())
215 s.EOL();
216 }
217}
static llvm::raw_ostream & error(Stream &strm)
static void dump_type_value(const lldb_private::RegisterTypeFlags &flags_type, lldb_private::CompilerType &fields_compiler_type, T value, lldb_private::ExecutionContextScope *exe_scope, lldb_private::Stream &strm)
static std::string toString(const Checksum &checksum)
Generic representation of a type in a programming language.
A uniqued constant string class.
Definition ConstString.h:40
An data extractor class.
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
DumpValueObjectOptions & SetHideRootType(bool hide_root_type=false)
DumpValueObjectOptions & SetChildPrintingDecider(ChildPrintingDecider decider)
DumpValueObjectOptions & SetShowSummary(bool show=true)
std::function< bool(llvm::StringRef)> ChildPrintingDecider
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
virtual lldb::ProcessSP CalculateProcess()=0
bool GetData(DataExtractor &data) const
uint64_t GetAsUInt64(uint64_t fail_value=UINT64_MAX, bool *success_ptr=nullptr) const
uint32_t GetAsUInt32(uint32_t fail_value=UINT32_MAX, bool *success_ptr=nullptr) const
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition Stream.cpp:157
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
size_t PutChar(char ch)
Definition Stream.cpp:131
size_t EOL()
Output and End of Line character to the stream.
Definition Stream.cpp:155
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, lldb::ByteOrder byte_order, uint32_t addr_byte_size, lldb::addr_t address=LLDB_INVALID_ADDRESS, ValueObjectManager *manager=nullptr)
These routines create ValueObjectConstResult ValueObjects from various data sources.
#define LLDB_INVALID_ADDRESS
#define UINT32_MAX
lldb::ByteOrder InlHostByteOrder()
Definition Endian.h:25
void DumpRegisterValue(const RegisterValue &reg_val, Stream &s, const RegisterInfo &reg_info, bool prefix_with_name, bool prefix_with_alt_name, lldb::Format format, uint32_t reg_name_right_align_at=0, ExecutionContextScope *exe_scope=nullptr, bool print_type=false, lldb::TargetSP target_sp=nullptr)
lldb::offset_t DumpDataExtractor(const DataExtractor &DE, Stream *s, lldb::offset_t offset, lldb::Format item_format, size_t item_byte_size, size_t item_count, size_t num_per_line, uint64_t base_addr, uint32_t item_bit_size, uint32_t item_bit_offset, ExecutionContextScope *exe_scope=nullptr, bool show_memory_tags=false)
Dumps item_count objects into the stream s.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Format
Display format definitions.
std::shared_ptr< lldb_private::Process > ProcessSP
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::Target > TargetSP
Every register is described in detail including its name, alternate name (optional),...
const char * alt_name
Alternate name of this register, can be NULL.
uint32_t byte_size
Size in bytes of the register.
const RegisterType * register_type
If not nullptr, a type defined by XML descriptions.
const char * name
Name of this register, can't be NULL.
lldb::Format format
Default display format.