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