LLDB mainline
DumpRegisterInfo.cpp
Go to the documentation of this file.
1//===-- DumpRegisterInfo.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/Stream.h"
14
15#include "llvm/Support/Casting.h"
16
17#include <cinttypes>
18
19using namespace lldb;
20using namespace lldb_private;
21
22using SetInfo = std::pair<const char *, uint32_t>;
23
25 const RegisterInfo &info,
26 uint32_t terminal_width) {
27 std::vector<const char *> invalidates;
28 if (info.invalidate_regs) {
29 for (uint32_t *inv_regs = info.invalidate_regs;
30 *inv_regs != LLDB_INVALID_REGNUM; ++inv_regs) {
31 const RegisterInfo *inv_info =
33 assert(
34 inv_info &&
35 "Register invalidate list refers to a register that does not exist.");
36 invalidates.push_back(inv_info->name);
37 }
38 }
39
40 // We include the index here so that you can use it with "register read -s".
41 std::vector<SetInfo> in_sets;
42 for (uint32_t set_idx = 0; set_idx < ctx.GetRegisterSetCount(); ++set_idx) {
43 const RegisterSet *set = ctx.GetRegisterSet(set_idx);
44 assert(set && "Register set should be valid.");
45 for (uint32_t reg_idx = 0; reg_idx < set->num_registers; ++reg_idx) {
46 const RegisterInfo *set_reg_info =
47 ctx.GetRegisterInfoAtIndex(set->registers[reg_idx]);
48 assert(set_reg_info && "Register info should be valid.");
49
50 if (set_reg_info == &info) {
51 in_sets.push_back({set->name, set_idx});
52 break;
53 }
54 }
55 }
56
57 std::vector<const char *> read_from;
58 if (info.value_regs) {
59 for (uint32_t *read_regs = info.value_regs;
60 *read_regs != LLDB_INVALID_REGNUM; ++read_regs) {
61 const RegisterInfo *read_info =
63 assert(read_info && "Register value registers list refers to a register "
64 "that does not exist.");
65 read_from.push_back(read_info->name);
66 }
67 }
68
69 DoDumpRegisterInfo(strm, info.name, info.alt_name, info.byte_size,
70 invalidates, read_from, in_sets, info.register_type,
71 terminal_width);
72}
73
74template <typename ElementType>
75static void DumpList(Stream &strm, const char *title,
76 const std::vector<ElementType> &list,
77 std::function<void(Stream &, ElementType)> emitter) {
78 if (list.empty())
79 return;
80
81 strm.EOL();
82 strm << title;
83 bool first = true;
84 for (ElementType elem : list) {
85 if (!first)
86 strm << ", ";
87 first = false;
88 emitter(strm, elem);
89 }
90}
91
93 Stream &strm, const char *name, const char *alt_name, uint32_t byte_size,
94 const std::vector<const char *> &invalidates,
95 const std::vector<const char *> &read_from,
96 const std::vector<SetInfo> &in_sets, const RegisterType *register_type,
97 uint32_t terminal_width) {
98 strm << " Name: " << name;
99 if (alt_name)
100 strm << " (" << alt_name << ")";
101 strm.EOL();
102
103 // Size in bits may seem obvious for the usual 32 or 64 bit registers.
104 // When we get to vector registers, then scalable vector registers, it is very
105 // useful to know without the user doing extra work.
106 strm.Printf(" Size: %d bytes (%d bits)", byte_size, byte_size * 8);
107
108 std::function<void(Stream &, const char *)> emit_str =
109 [](Stream &strm, const char *s) { strm << s; };
110 DumpList(strm, "Invalidates: ", invalidates, emit_str);
111 DumpList(strm, " Read from: ", read_from, emit_str);
112
113 std::function<void(Stream &, SetInfo)> emit_set = [](Stream &strm,
114 SetInfo info) {
115 strm.Printf("%s (index %d)", info.first, info.second);
116 };
117 DumpList(strm, " In sets: ", in_sets, emit_set);
118
119 if (auto flags_type =
120 llvm::dyn_cast_if_present<RegisterTypeFlags>(register_type)) {
121 strm.Printf("\n\n%s", flags_type->AsTable(terminal_width).c_str());
122
123 std::string enumerators = flags_type->DumpEnums(terminal_width);
124 if (enumerators.size())
125 strm << "\n\n" << enumerators;
126 } else if (auto *union_type =
127 llvm::dyn_cast_if_present<RegisterTypeUnion>(register_type)) {
128 strm << "\n\n Union members:";
129 for (const RegisterTypeUnion::Field &field : union_type->GetFields()) {
130 strm.Printf("\n %s (%s", field.GetName().c_str(),
131 field.GetType()->GetID().c_str());
132 if (std::optional<uint64_t> byte_size = field.GetType()->GetByteSize())
133 strm.Printf(", %" PRIu64 " bytes", *byte_size);
134 strm.PutChar(')');
135 }
136 } else if (auto *vector_type =
137 llvm::dyn_cast_if_present<RegisterTypeVector>(register_type)) {
138 strm.Printf("\n\n Vector elements: %u", vector_type->GetCount());
139 }
140}
std::pair< const char *, uint32_t > SetInfo
static void DumpList(Stream &strm, const char *title, const std::vector< ElementType > &list, std::function< void(Stream &, ElementType)> emitter)
virtual const RegisterSet * GetRegisterSet(size_t reg_set)=0
virtual const RegisterInfo * GetRegisterInfoAtIndex(size_t reg)=0
const RegisterInfo * GetRegisterInfo(lldb::RegisterKind reg_kind, uint32_t reg_num)
virtual size_t GetRegisterSetCount()=0
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 PutChar(char ch)
Definition Stream.cpp:131
size_t EOL()
Output and End of Line character to the stream.
Definition Stream.cpp:155
#define LLDB_INVALID_REGNUM
A class that represents a running process on the host machine.
void DoDumpRegisterInfo(Stream &strm, const char *name, const char *alt_name, uint32_t byte_size, const std::vector< const char * > &invalidates, const std::vector< const char * > &read_from, const std::vector< std::pair< const char *, uint32_t > > &in_sets, const RegisterType *register_type, uint32_t terminal_width)
void DumpRegisterInfo(Stream &strm, RegisterContext &ctx, const RegisterInfo &info, uint32_t terminal_width)
@ eRegisterKindLLDB
lldb's internal register numbers
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 * value_regs
List of registers (terminated with LLDB_INVALID_REGNUM).
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.
uint32_t * invalidate_regs
List of registers (terminated with LLDB_INVALID_REGNUM).
Registers are grouped into register sets.
size_t num_registers
The number of registers in REGISTERS array below.
const uint32_t * registers
An array of register indicies in this set.
const char * name
Name of this register set.