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)
44  if (!process)
45  return;
46  if (base_addr == 0 || base_addr == LLDB_INVALID_ADDRESS)
47  return;
48  m_byte_order = process->GetByteOrder();
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 || *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  auto total_size = struct_type.GetByteSize(nullptr);
71  if (!total_size)
72  return;
73  lldb::WritableDataBufferSP buffer_sp(new DataBufferHeap(*total_size, 0));
74  Status error;
75  process->ReadMemoryFromInferior(base_addr, buffer_sp->GetBytes(),
76  *total_size, error);
77  if (error.Fail())
78  return;
80  }
81 
82  template <typename RetType>
83  RetType GetField(ConstString name, RetType fail_value = RetType()) {
84  auto iter = m_fields.find(name), end = m_fields.end();
85  if (iter == end)
86  return fail_value;
87  auto size = iter->second.size;
88  if (sizeof(RetType) < size)
89  return fail_value;
90  lldb::offset_t offset = iter->second.offset;
91  if (offset + size > m_data.GetByteSize())
92  return fail_value;
93  return (RetType)(m_data.GetMaxU64(&offset, size));
94  }
95 
96  size_t GetOffsetOf(ConstString name, size_t fail_value = SIZE_MAX) {
97  auto iter = m_fields.find(name), end = m_fields.end();
98  if (iter == end)
99  return fail_value;
100  return iter->second.offset;
101  }
102 };
103 }
104 
105 #endif // LLDB_TARGET_PROCESSSTRUCTREADER_H
lldb_private::ProcessStructReader::m_byte_order
lldb::ByteOrder m_byte_order
Definition: ProcessStructReader.h:37
lldb-defines.h
lldb_private::Process
Definition: Process.h:338
lldb_private::ProcessStructReader::m_data
DataExtractor m_data
Definition: ProcessStructReader.h:36
lldb_private::CompilerType::GetByteSize
llvm::Optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
Definition: CompilerType.cpp:552
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::ProcessStructReader
Definition: ProcessStructReader.h:27
lldb_private::Process::ReadMemoryFromInferior
size_t ReadMemoryFromInferior(lldb::addr_t vm_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
Definition: Process.cpp:2037
Process.h
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::ProcessStructReader::m_fields
std::map< ConstString, FieldImpl > m_fields
Definition: ProcessStructReader.h:35
lldb_private::DataExtractor
Definition: DataExtractor.h:48
lldb_private::Process::GetAddressByteSize
uint32_t GetAddressByteSize() const
Definition: Process.cpp:3366
lldb_private::ConstString
Definition: ConstString.h:39
lldb_private::ProcessStructReader::FieldImpl::size
size_t size
Definition: ProcessStructReader.h:32
lldb_private::CompilerType::GetNumFields
uint32_t GetNumFields() const
Definition: CompilerType.cpp:604
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
CompilerType.h
lldb_private::ProcessStructReader::m_addr_byte_size
size_t m_addr_byte_size
Definition: ProcessStructReader.h:38
lldb-types.h
lldb_private::Status
Definition: Status.h:44
lldb::eByteOrderInvalid
@ eByteOrderInvalid
Definition: lldb-enumerations.h:140
uint32_t
lldb_private::ProcessStructReader::FieldImpl::offset
size_t offset
Definition: ProcessStructReader.h:31
lldb_private::DataExtractor::GetMaxU64
uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
Definition: DataExtractor.cpp:524
lldb_private::ProcessStructReader::GetOffsetOf
size_t GetOffsetOf(ConstString name, size_t fail_value=SIZE_MAX)
Definition: ProcessStructReader.h:96
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:74
DataExtractor.h
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:35
Status.h
lldb_private::ProcessStructReader::FieldImpl
Definition: ProcessStructReader.h:29
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ProcessStructReader::ProcessStructReader
ProcessStructReader(Process *process, lldb::addr_t base_addr, CompilerType struct_type)
Definition: ProcessStructReader.h:41
ConstString.h
lldb_private::ProcessStructReader::GetField
RetType GetField(ConstString name, RetType fail_value=RetType())
Definition: ProcessStructReader.h:83
lldb_private::Process::GetByteOrder
lldb::ByteOrder GetByteOrder() const
Definition: Process.cpp:3362
lldb_private::CompilerType::GetFieldAtIndex
CompilerType GetFieldAtIndex(size_t idx, std::string &name, uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) const
Definition: CompilerType.cpp:611
lldb_private::DataExtractor::GetByteSize
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
Definition: DataExtractor.h:270
lldb_private::DataBufferHeap
Definition: DataBufferHeap.h:30
lldb
Definition: SBAddress.h:15
DataBufferHeap.h
lldb::ByteOrder
ByteOrder
Byte ordering definitions.
Definition: lldb-enumerations.h:139
lldb_private::ProcessStructReader::FieldImpl::type
CompilerType type
Definition: ProcessStructReader.h:30