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
16#include "lldb/Utility/Endian.h"
20#include "llvm/ADT/bit.h"
21
22using namespace lldb;
23
24template <typename T>
25static void dump_type_value(lldb_private::CompilerType &fields_type, T value,
27 const lldb_private::RegisterInfo &reg_info,
29 lldb::ByteOrder target_order = exe_scope->CalculateProcess()->GetByteOrder();
30
31 // For the bitfield types we generate, it is expected that the fields are
32 // in what is usually a big endian order. Most significant field first.
33 // This is also clang's internal ordering and the order we want to print
34 // them. On a big endian host this all matches up, for a little endian
35 // host we have to swap the order of the fields before display.
36 if (target_order == lldb::ByteOrder::eByteOrderLittle) {
37 value = reg_info.flags_type->ReverseFieldOrder(value);
38 }
39
40 // Then we need to match the target's endian on a byte level as well.
41 if (lldb_private::endian::InlHostByteOrder() != target_order)
42 value = llvm::byteswap(value);
43
44 lldb_private::DataExtractor data_extractor{
45 &value, sizeof(T), lldb_private::endian::InlHostByteOrder(), 8};
46
48 exe_scope, fields_type, lldb_private::ConstString(), data_extractor);
51 [](lldb_private::ConstString varname) {
52 // Unnamed bit-fields are padding that we don't want to show.
53 return varname.GetLength();
54 };
55 dump_options.SetChildPrintingDecider(decider).SetHideRootType(true);
56
57 vobj_sp->Dump(strm, dump_options);
58}
59
61 const RegisterInfo &reg_info,
62 bool prefix_with_name,
63 bool prefix_with_alt_name, Format format,
64 uint32_t reg_name_right_align_at,
65 ExecutionContextScope *exe_scope,
66 bool print_flags, TargetSP target_sp) {
67 DataExtractor data;
68 if (!reg_val.GetData(data))
69 return;
70
71 bool name_printed = false;
72 // For simplicity, alignment of the register name printing applies only in
73 // the most common case where:
74 //
75 // prefix_with_name^prefix_with_alt_name is true
76 //
77 StreamString format_string;
78 if (reg_name_right_align_at && (prefix_with_name ^ prefix_with_alt_name))
79 format_string.Printf("%%%us", reg_name_right_align_at);
80 else
81 format_string.Printf("%%s");
82 std::string fmt = std::string(format_string.GetString());
83 if (prefix_with_name) {
84 if (reg_info.name) {
85 s.Printf(fmt.c_str(), reg_info.name);
86 name_printed = true;
87 } else if (reg_info.alt_name) {
88 s.Printf(fmt.c_str(), reg_info.alt_name);
89 prefix_with_alt_name = false;
90 name_printed = true;
91 }
92 }
93 if (prefix_with_alt_name) {
94 if (name_printed)
95 s.PutChar('/');
96 if (reg_info.alt_name) {
97 s.Printf(fmt.c_str(), reg_info.alt_name);
98 name_printed = true;
99 } else if (!name_printed) {
100 // No alternate name but we were asked to display a name, so show the
101 // main name
102 s.Printf(fmt.c_str(), reg_info.name);
103 name_printed = true;
104 }
105 }
106 if (name_printed)
107 s.PutCString(" = ");
108
109 if (format == eFormatDefault)
110 format = reg_info.format;
111
112 DumpDataExtractor(data, &s,
113 0, // Offset in "data"
114 format, // Format to use when dumping
115 reg_info.byte_size, // item_byte_size
116 1, // item_count
117 UINT32_MAX, // num_per_line
118 LLDB_INVALID_ADDRESS, // base_addr
119 0, // item_bit_size
120 0, // item_bit_offset
121 exe_scope);
122
123 if (!print_flags || !reg_info.flags_type || !exe_scope || !target_sp ||
124 (reg_info.byte_size != 4 && reg_info.byte_size != 8))
125 return;
126
127 CompilerType fields_type = target_sp->GetRegisterType(
128 reg_info.name, *reg_info.flags_type, reg_info.byte_size);
129
130 // Use a new stream so we can remove a trailing newline later.
131 StreamString fields_stream;
132
133 if (reg_info.byte_size == 4) {
134 dump_type_value(fields_type, reg_val.GetAsUInt32(), exe_scope, reg_info,
135 fields_stream);
136 } else {
137 dump_type_value(fields_type, reg_val.GetAsUInt64(), exe_scope, reg_info,
138 fields_stream);
139 }
140
141 // Registers are indented like:
142 // (lldb) register read foo
143 // foo = 0x12345678
144 // So we need to indent to match that.
145
146 // First drop the extra newline that the value printer added. The register
147 // command will add one itself.
148 llvm::StringRef fields_str = fields_stream.GetString().drop_back();
149
150 // End the line that contains " foo = 0x12345678".
151 s.EOL();
152
153 // Then split the value lines and indent each one.
154 bool first = true;
155 while (fields_str.size()) {
156 std::pair<llvm::StringRef, llvm::StringRef> split = fields_str.split('\n');
157 fields_str = split.second;
158 // Indent as far as the register name did.
159 s.Printf(fmt.c_str(), "");
160
161 // Lines after the first won't have " = " so compensate for that.
162 if (!first)
163 s << " ";
164 first = false;
165
166 s << split.first;
167
168 // On the last line we don't want a newline because the command will add
169 // one too.
170 if (fields_str.size())
171 s.EOL();
172 }
173}
static void dump_type_value(lldb_private::CompilerType &fields_type, T value, lldb_private::ExecutionContextScope *exe_scope, const lldb_private::RegisterInfo &reg_info, lldb_private::Stream &strm)
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
A uniqued constant string class.
Definition: ConstString.h:40
An data extractor class.
Definition: DataExtractor.h:48
DumpValueObjectOptions & SetHideRootType(bool hide_root_type=false)
std::function< bool(ConstString)> ChildPrintingDecider
DumpValueObjectOptions & SetChildPrintingDecider(ChildPrintingDecider decider)
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
virtual lldb::ProcessSP CalculateProcess()=0
T ReverseFieldOrder(T value) const
Definition: RegisterFlags.h:98
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 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:65
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)
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
#define UINT32_MAX
Definition: lldb-defines.h:19
lldb::ByteOrder InlHostByteOrder()
Definition: Endian.h:25
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.
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_flags=false, lldb::TargetSP target_sp=nullptr)
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:472
Format
Display format definitions.
ByteOrder
Byte ordering definitions.
@ eByteOrderLittle
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:436
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 RegisterFlags * flags_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.