]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/lldb/Utility/ProcessStructReader.h
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / include / lldb / Utility / ProcessStructReader.h
1 //===---------------------ProcessStructReader.h ------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef utility_ProcessStructReader_h_
12 #define utility_ProcessStructReader_h_
13
14 #include "lldb/lldb-defines.h"
15 #include "lldb/lldb-types.h"
16
17 #include "lldb/Core/ConstString.h"
18 #include "lldb/Core/DataExtractor.h"
19 #include "lldb/Core/Error.h"
20 #include "lldb/Symbol/CompilerType.h"
21 #include "lldb/Target/Process.h"
22
23 #include <initializer_list>
24 #include <map>
25 #include <string>
26
27 namespace lldb_private {
28 class ProcessStructReader {
29 protected:
30   struct FieldImpl {
31     CompilerType type;
32     size_t offset;
33     size_t size;
34   };
35
36   std::map<ConstString, FieldImpl> m_fields;
37   DataExtractor m_data;
38   lldb::ByteOrder m_byte_order;
39   size_t m_addr_byte_size;
40
41 public:
42   ProcessStructReader(Process *process, lldb::addr_t base_addr,
43                       CompilerType struct_type) {
44     if (!process)
45       return;
46     if (base_addr == 0 || base_addr == LLDB_INVALID_ADDRESS)
47       return;
48     m_byte_order = process->GetByteOrder();
49     m_addr_byte_size = process->GetAddressByteSize();
50
51     for (size_t idx = 0; idx < struct_type.GetNumFields(); idx++) {
52       std::string name;
53       uint64_t bit_offset;
54       uint32_t bitfield_bit_size;
55       bool is_bitfield;
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)
59       if (is_bitfield)
60         return;
61       auto size = field_type.GetByteSize(nullptr);
62       // no support for things larger than a uint64_t (yet)
63       if (size > 8)
64         return;
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)};
69     }
70     size_t total_size = struct_type.GetByteSize(nullptr);
71     lldb::DataBufferSP buffer_sp(new DataBufferHeap(total_size, 0));
72     Error error;
73     process->ReadMemoryFromInferior(base_addr, buffer_sp->GetBytes(),
74                                     total_size, error);
75     if (error.Fail())
76       return;
77     m_data = DataExtractor(buffer_sp, m_byte_order, m_addr_byte_size);
78   }
79
80   template <typename RetType>
81   RetType GetField(ConstString name, RetType fail_value = RetType()) {
82     auto iter = m_fields.find(name), end = m_fields.end();
83     if (iter == end)
84       return fail_value;
85     auto size = iter->second.size;
86     if (sizeof(RetType) < size)
87       return fail_value;
88     lldb::offset_t offset = iter->second.offset;
89     if (offset + size > m_data.GetByteSize())
90       return fail_value;
91     return (RetType)(m_data.GetMaxU64(&offset, size));
92   }
93
94   size_t GetOffsetOf(ConstString name, size_t fail_value = SIZE_MAX) {
95     auto iter = m_fields.find(name), end = m_fields.end();
96     if (iter == end)
97       return fail_value;
98     return iter->second.offset;
99   }
100 };
101 }
102
103 #endif // utility_ProcessStructReader_h_