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