1 //===---------------------ProcessStructReader.h ------------------*- C++-*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLDB_TARGET_PROCESSSTRUCTREADER_H
11 #define LLDB_TARGET_PROCESSSTRUCTREADER_H
13 #include "lldb/lldb-defines.h"
14 #include "lldb/lldb-types.h"
16 #include "lldb/Symbol/CompilerType.h"
17 #include "lldb/Target/Process.h"
18 #include "lldb/Utility/ConstString.h"
19 #include "lldb/Utility/DataBufferHeap.h"
20 #include "lldb/Utility/DataExtractor.h"
21 #include "lldb/Utility/Status.h"
23 #include <initializer_list>
27 namespace lldb_private {
28 class ProcessStructReader {
36 std::map<ConstString, FieldImpl> m_fields;
38 lldb::ByteOrder m_byte_order;
39 size_t m_addr_byte_size;
42 ProcessStructReader(Process *process, lldb::addr_t base_addr,
43 CompilerType struct_type) {
46 if (base_addr == 0 || base_addr == LLDB_INVALID_ADDRESS)
48 m_byte_order = process->GetByteOrder();
49 m_addr_byte_size = process->GetAddressByteSize();
51 for (size_t idx = 0; idx < struct_type.GetNumFields(); idx++) {
54 uint32_t bitfield_bit_size;
56 CompilerType field_type = struct_type.GetFieldAtIndex(
57 idx, name, &bit_offset, &bitfield_bit_size, &is_bitfield);
58 // no support for bitfields in here (yet)
61 auto size = field_type.GetByteSize(nullptr);
62 // no support for things larger than a uint64_t (yet)
63 if (!size || *size > 8)
65 ConstString const_name = ConstString(name.c_str());
66 size_t byte_index = static_cast<size_t>(bit_offset / 8);
67 m_fields[const_name] =
68 FieldImpl{field_type, byte_index, static_cast<size_t>(*size)};
70 auto total_size = struct_type.GetByteSize(nullptr);
73 lldb::DataBufferSP buffer_sp(new DataBufferHeap(*total_size, 0));
75 process->ReadMemoryFromInferior(base_addr, buffer_sp->GetBytes(),
79 m_data = DataExtractor(buffer_sp, m_byte_order, m_addr_byte_size);
82 template <typename RetType>
83 RetType GetField(ConstString name, RetType fail_value = RetType()) {
84 auto iter = m_fields.find(name), end = m_fields.end();
87 auto size = iter->second.size;
88 if (sizeof(RetType) < size)
90 lldb::offset_t offset = iter->second.offset;
91 if (offset + size > m_data.GetByteSize())
93 return (RetType)(m_data.GetMaxU64(&offset, size));
96 size_t GetOffsetOf(ConstString name, size_t fail_value = SIZE_MAX) {
97 auto iter = m_fields.find(name), end = m_fields.end();
100 return iter->second.offset;
105 #endif // utility_ProcessStructReader_h_