1 //===---------------------ProcessStructReader.h ------------------*- C++-*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_TARGET_PROCESSSTRUCTREADER_H
10 #define LLDB_TARGET_PROCESSSTRUCTREADER_H
12 #include "lldb/lldb-defines.h"
13 #include "lldb/lldb-types.h"
15 #include "lldb/Symbol/CompilerType.h"
16 #include "lldb/Target/Process.h"
17 #include "lldb/Utility/ConstString.h"
18 #include "lldb/Utility/DataBufferHeap.h"
19 #include "lldb/Utility/DataExtractor.h"
20 #include "lldb/Utility/Status.h"
22 #include <initializer_list>
26 namespace lldb_private {
27 class ProcessStructReader {
35 std::map<ConstString, FieldImpl> m_fields;
37 lldb::ByteOrder m_byte_order;
38 size_t m_addr_byte_size;
41 ProcessStructReader(Process *process, lldb::addr_t base_addr,
42 CompilerType struct_type) {
45 if (base_addr == 0 || base_addr == LLDB_INVALID_ADDRESS)
47 m_byte_order = process->GetByteOrder();
48 m_addr_byte_size = process->GetAddressByteSize();
50 for (size_t idx = 0; idx < struct_type.GetNumFields(); idx++) {
53 uint32_t bitfield_bit_size;
55 CompilerType field_type = struct_type.GetFieldAtIndex(
56 idx, name, &bit_offset, &bitfield_bit_size, &is_bitfield);
57 // no support for bitfields in here (yet)
60 auto size = field_type.GetByteSize(nullptr);
61 // no support for things larger than a uint64_t (yet)
62 if (!size || *size > 8)
64 ConstString const_name = ConstString(name.c_str());
65 size_t byte_index = static_cast<size_t>(bit_offset / 8);
66 m_fields[const_name] =
67 FieldImpl{field_type, byte_index, static_cast<size_t>(*size)};
69 auto total_size = struct_type.GetByteSize(nullptr);
72 lldb::DataBufferSP buffer_sp(new DataBufferHeap(*total_size, 0));
74 process->ReadMemoryFromInferior(base_addr, buffer_sp->GetBytes(),
78 m_data = DataExtractor(buffer_sp, m_byte_order, m_addr_byte_size);
81 template <typename RetType>
82 RetType GetField(ConstString name, RetType fail_value = RetType()) {
83 auto iter = m_fields.find(name), end = m_fields.end();
86 auto size = iter->second.size;
87 if (sizeof(RetType) < size)
89 lldb::offset_t offset = iter->second.offset;
90 if (offset + size > m_data.GetByteSize())
92 return (RetType)(m_data.GetMaxU64(&offset, size));
95 size_t GetOffsetOf(ConstString name, size_t fail_value = SIZE_MAX) {
96 auto iter = m_fields.find(name), end = m_fields.end();
99 return iter->second.offset;
104 #endif // utility_ProcessStructReader_h_