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"
19#include "lldb/Utility/Status.h"
20
21#include "llvm/ADT/StringMap.h"
22
23#include <initializer_list>
24#include <map>
25#include <string>
26
27namespace lldb_private {
29protected:
30 struct FieldImpl {
32 size_t offset;
33 size_t size;
34 };
35
36 llvm::StringMap<FieldImpl> m_fields;
40
41public:
43 CompilerType struct_type)
44 : m_byte_order(lldb::eByteOrderInvalid), m_addr_byte_size(0) {
45 if (!process)
46 return;
47 if (base_addr == 0 || base_addr == LLDB_INVALID_ADDRESS)
48 return;
49 m_byte_order = process->GetByteOrder();
51
52 for (size_t idx = 0; idx < struct_type.GetNumFields(); idx++) {
53 std::string name;
54 uint64_t bit_offset;
55 uint32_t bitfield_bit_size;
56 bool is_bitfield;
57 CompilerType field_type = struct_type.GetFieldAtIndex(
58 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(nullptr);
63 // no support for things larger than a uint64_t (yet)
64 if (!size || *size > 8)
65 return;
66 size_t byte_index = static_cast<size_t>(bit_offset / 8);
67 m_fields.insert({name, FieldImpl{field_type, byte_index,
68 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));
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(llvm::StringRef 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}
97
98#endif // LLDB_TARGET_PROCESSSTRUCTREADER_H
static llvm::raw_ostream & error(Stream &strm)
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
std::optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
CompilerType GetFieldAtIndex(size_t idx, std::string &name, uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) const
uint32_t GetNumFields() const
A subclass of DataBuffer that stores a data buffer on the heap.
An data extractor class.
Definition: DataExtractor.h:48
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
RetType GetField(llvm::StringRef name, RetType fail_value=RetType())
ProcessStructReader(Process *process, lldb::addr_t base_addr, CompilerType struct_type)
llvm::StringMap< FieldImpl > m_fields
A plug-in interface definition class for debugging a process.
Definition: Process.h:340
size_t ReadMemoryFromInferior(lldb::addr_t vm_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
Definition: Process.cpp:2050
lldb::ByteOrder GetByteOrder() const
Definition: Process.cpp:3399
uint32_t GetAddressByteSize() const
Definition: Process.cpp:3403
An error handling class.
Definition: Status.h:44
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
uint64_t offset_t
Definition: lldb-types.h:83
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
Definition: lldb-forward.h:329
uint64_t addr_t
Definition: lldb-types.h:79