LLDB  mainline
ProcessStructReader.h
Go to the documentation of this file.
1 //===---------------------ProcessStructReader.h ------------------*- C++-*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_TARGET_PROCESSSTRUCTREADER_H
10 #define LLDB_TARGET_PROCESSSTRUCTREADER_H
11 
12 #include "lldb/lldb-defines.h"
13 #include "lldb/lldb-types.h"
14 
16 #include "lldb/Target/Process.h"
20 #include "lldb/Utility/Status.h"
21 
22 #include <initializer_list>
23 #include <map>
24 #include <string>
25 
26 namespace lldb_private {
28 protected:
29  struct FieldImpl {
31  size_t offset;
32  size_t size;
33  };
34 
35  std::map<ConstString, FieldImpl> m_fields;
39 
40 public:
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 || *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  auto total_size = struct_type.GetByteSize(nullptr);
70  if (!total_size)
71  return;
72  lldb::DataBufferSP buffer_sp(new DataBufferHeap(*total_size, 0));
73  Status error;
74  process->ReadMemoryFromInferior(base_addr, buffer_sp->GetBytes(),
75  *total_size, error);
76  if (error.Fail())
77  return;
78  m_data = DataExtractor(buffer_sp, m_byte_order, m_addr_byte_size);
79  }
80 
81  template <typename RetType>
82  RetType GetField(ConstString name, RetType fail_value = RetType()) {
83  auto iter = m_fields.find(name), end = m_fields.end();
84  if (iter == end)
85  return fail_value;
86  auto size = iter->second.size;
87  if (sizeof(RetType) < size)
88  return fail_value;
89  lldb::offset_t offset = iter->second.offset;
90  if (offset + size > m_data.GetByteSize())
91  return fail_value;
92  return (RetType)(m_data.GetMaxU64(&offset, size));
93  }
94 
95  size_t GetOffsetOf(ConstString name, size_t fail_value = SIZE_MAX) {
96  auto iter = m_fields.find(name), end = m_fields.end();
97  if (iter == end)
98  return fail_value;
99  return iter->second.offset;
100  }
101 };
102 }
103 
104 #endif // utility_ProcessStructReader_h_
An data extractor class.
Definition: DataExtractor.h:47
size_t ReadMemoryFromInferior(lldb::addr_t vm_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
Definition: Process.cpp:2126
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
ProcessStructReader(Process *process, lldb::addr_t base_addr, CompilerType struct_type)
size_t GetOffsetOf(ConstString name, size_t fail_value=SIZE_MAX)
std::map< ConstString, FieldImpl > m_fields
lldb::ByteOrder GetByteOrder() const
Definition: Process.cpp:3366
llvm::Optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
uint32_t GetNumFields() const
A subclass of DataBuffer that stores a data buffer on the heap.
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
uint64_t offset_t
Definition: lldb-types.h:87
uint32_t GetAddressByteSize() const
Definition: Process.cpp:3370
A plug-in interface definition class for debugging a process.
Definition: Process.h:353
uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
uint64_t addr_t
Definition: lldb-types.h:83
A uniqued constant string class.
Definition: ConstString.h:38
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
CompilerType GetFieldAtIndex(size_t idx, std::string &name, uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) const
RetType GetField(ConstString name, RetType fail_value=RetType())
An error handling class.
Definition: Status.h:44