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 "llvm/ADT/StringMap.h"
23
24#include <initializer_list>
25#include <map>
26#include <string>
27
28namespace lldb_private {
30protected:
31 struct FieldImpl {
33 size_t offset;
34 size_t size;
35 };
36
37 llvm::StringMap<FieldImpl> m_fields;
41
42public:
44 CompilerType struct_type)
45 : m_byte_order(lldb::eByteOrderInvalid), m_addr_byte_size(0) {
46 if (!process)
47 return;
48 if (base_addr == 0 || base_addr == LLDB_INVALID_ADDRESS)
49 return;
50 m_byte_order = process->GetByteOrder();
52
53 for (size_t idx = 0; idx < struct_type.GetNumFields(); idx++) {
54 std::string name;
55 uint64_t bit_offset;
56 uint32_t bitfield_bit_size;
57 bool is_bitfield;
58 CompilerType field_type = struct_type.GetFieldAtIndex(
59 idx, name, &bit_offset, &bitfield_bit_size, &is_bitfield);
60 // no support for bitfields in here (yet)
61 if (is_bitfield)
62 return;
63 auto size_or_err = field_type.GetByteSize(nullptr);
64 if (!size_or_err) {
65 LLDB_LOG_ERROR(GetLog(LLDBLog::Target), size_or_err.takeError(), "{0}");
66 return;
67 }
68 size_t size = *size_or_err;
69
70 // no support for things larger than a uint64_t (yet)
71 if (size > 8)
72 return;
73 size_t byte_index = static_cast<size_t>(bit_offset / 8);
74 m_fields.insert(
75 {name, FieldImpl{field_type, byte_index, static_cast<size_t>(size)}});
76 }
77 auto total_size_or_err = struct_type.GetByteSize(nullptr);
78 if (!total_size_or_err) {
79 LLDB_LOG_ERROR(GetLog(LLDBLog::Target), total_size_or_err.takeError(),
80 "{0}");
81 return;
82 }
83 size_t total_size = *total_size_or_err;
84
85 lldb::WritableDataBufferSP buffer_sp(new DataBufferHeap(total_size, 0));
87 process->ReadMemoryFromInferior(base_addr, buffer_sp->GetBytes(),
88 total_size, error);
89 if (error.Fail())
90 return;
92 }
93
94 template <typename RetType>
95 RetType GetField(llvm::StringRef name, RetType fail_value = RetType()) {
96 auto iter = m_fields.find(name), end = m_fields.end();
97 if (iter == end)
98 return fail_value;
99 auto size = iter->second.size;
100 if (sizeof(RetType) < size)
101 return fail_value;
102 lldb::offset_t offset = iter->second.offset;
103 if (offset + size > m_data.GetByteSize())
104 return fail_value;
105 return (RetType)(m_data.GetMaxU64(&offset, size));
106 }
107};
108}
109
110#endif // LLDB_TARGET_PROCESSSTRUCTREADER_H
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:392
Generic representation of a type in a programming language.
CompilerType GetFieldAtIndex(size_t idx, std::string &name, uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) const
llvm::Expected< 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.
An data extractor class.
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:354
size_t ReadMemoryFromInferior(lldb::addr_t vm_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
Definition Process.cpp:2263
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:3717
uint32_t GetAddressByteSize() const
Definition Process.cpp:3721
An error handling class.
Definition Status.h:118
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
uint64_t offset_t
Definition lldb-types.h:85
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
uint64_t addr_t
Definition lldb-types.h:80