Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/include/lldb/Target/ProcessStructReader.h
Line
Count
Source (jump to first uncovered line)
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
15
#include "lldb/Symbol/CompilerType.h"
16
#include "lldb/Target/Process.h"
17
#include "lldb/Utility/ConstString.h"
18
#include "lldb/Utility/DataBufferHeap.h"
19
#include "lldb/Utility/DataExtractor.h"
20
#include "lldb/Utility/Status.h"
21
22
#include <initializer_list>
23
#include <map>
24
#include <string>
25
26
namespace lldb_private {
27
class ProcessStructReader {
28
protected:
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
40
public:
41
  ProcessStructReader(Process *process, lldb::addr_t base_addr,
42
1.19k
                      CompilerType struct_type) {
43
1.19k
    if (!process)
44
0
      return;
45
1.19k
    if (base_addr == 0 || base_addr == LLDB_INVALID_ADDRESS)
46
0
      return;
47
1.19k
    m_byte_order = process->GetByteOrder();
48
1.19k
    m_addr_byte_size = process->GetAddressByteSize();
49
50
5.95k
    for (size_t idx = 0; idx < struct_type.GetNumFields(); 
idx++4.76k
) {
51
4.76k
      std::string name;
52
4.76k
      uint64_t bit_offset;
53
4.76k
      uint32_t bitfield_bit_size;
54
4.76k
      bool is_bitfield;
55
4.76k
      CompilerType field_type = struct_type.GetFieldAtIndex(
56
4.76k
          idx, name, &bit_offset, &bitfield_bit_size, &is_bitfield);
57
      // no support for bitfields in here (yet)
58
4.76k
      if (is_bitfield)
59
0
        return;
60
4.76k
      auto size = field_type.GetByteSize(nullptr);
61
      // no support for things larger than a uint64_t (yet)
62
4.76k
      if (!size || *size > 8)
63
0
        return;
64
4.76k
      ConstString const_name = ConstString(name.c_str());
65
4.76k
      size_t byte_index = static_cast<size_t>(bit_offset / 8);
66
4.76k
      m_fields[const_name] =
67
4.76k
          FieldImpl{field_type, byte_index, static_cast<size_t>(*size)};
68
4.76k
    }
69
1.19k
    auto total_size = struct_type.GetByteSize(nullptr);
70
1.19k
    if (!total_size)
71
0
      return;
72
1.19k
    lldb::DataBufferSP buffer_sp(new DataBufferHeap(*total_size, 0));
73
1.19k
    Status error;
74
1.19k
    process->ReadMemoryFromInferior(base_addr, buffer_sp->GetBytes(),
75
1.19k
                                    *total_size, error);
76
1.19k
    if (error.Fail())
77
0
      return;
78
1.19k
    m_data = DataExtractor(buffer_sp, m_byte_order, m_addr_byte_size);
79
1.19k
  }
80
81
  template <typename RetType>
82
4.76k
  RetType GetField(ConstString name, RetType fail_value = RetType()) {
83
4.76k
    auto iter = m_fields.find(name), end = m_fields.end();
84
4.76k
    if (iter == end)
85
0
      return fail_value;
86
4.76k
    auto size = iter->second.size;
87
4.76k
    if (sizeof(RetType) < size)
88
0
      return fail_value;
89
4.76k
    lldb::offset_t offset = iter->second.offset;
90
4.76k
    if (offset + size > m_data.GetByteSize())
91
0
      return fail_value;
92
4.76k
    return (RetType)(m_data.GetMaxU64(&offset, size));
93
4.76k
  }
94
95
0
  size_t GetOffsetOf(ConstString name, size_t fail_value = SIZE_MAX) {
96
0
    auto iter = m_fields.find(name), end = m_fields.end();
97
0
    if (iter == end)
98
0
      return fail_value;
99
0
    return iter->second.offset;
100
0
  }
101
};
102
}
103
104
#endif // LLDB_TARGET_PROCESSSTRUCTREADER_H