DebugServer2
Loading...
Searching...
No Matches
ProcessBase.h
1//
2// Copyright (c) 2014-present, Facebook, Inc.
3// All rights reserved.
4//
5// This source code is licensed under the University of Illinois/NCSA Open
6// Source License found in the LICENSE file in the root directory of this
7// source tree. An additional grant of patent rights can be found in the
8// PATENTS file in the same directory.
9//
10
11#pragma once
12
13#include "DebugServer2/Core/HardwareBreakpointManager.h"
14#include "DebugServer2/Core/SoftwareBreakpointManager.h"
15#include "DebugServer2/Target/ProcessDecl.h"
16#include "DebugServer2/Target/ThreadBase.h"
17
18#include <functional>
19#include <memory>
20#include <set>
21
22namespace ds2 {
23namespace Target {
24
26public:
27 enum { kFlagNewProcess = (1 << 0), kFlagAttachedProcess = (1 << 1) };
28 enum Extension : uint32_t {
29 kExtensionForkEvents = (1u << 0),
30 kExtensionVForkEvents = (1u << 1),
31 };
32 typedef std::map<ThreadId, Thread *> IdentityMap;
33
34protected:
35 bool _terminated;
36 uint32_t _flags;
37 uint32_t _enabledExtensions = 0;
38 ProcessId _pid;
39 ProcessInfo _info;
40 Address _loadBase;
41 Address _entryPoint;
42 IdentityMap _threads;
43 Thread *_currentThread;
44 mutable std::unique_ptr<SoftwareBreakpointManager> _softwareBreakpointManager;
45 mutable std::unique_ptr<HardwareBreakpointManager> _hardwareBreakpointManager;
46
47protected:
49 virtual ~ProcessBase();
50
51public:
52 inline ProcessId pid() const { return _pid; }
53
54public:
55 inline bool attached() const { return (_flags & kFlagAttachedProcess) != 0; }
56
57public:
58 // Whether the client has negotiated support for the fork-events and
59 // vfork-events GDB-remote extensions (see qSupported); stop reasons tied
60 // to these extensions are only reported when the corresponding flag here
61 // is set.
62 inline bool hasExtension(Extension extension) const {
63 return (_enabledExtensions & extension) != 0;
64 }
65 inline uint32_t enabledExtensions() const { return _enabledExtensions; }
66 inline bool forkEventsEnabled() const {
67 return hasExtension(kExtensionForkEvents);
68 }
69 inline bool vforkEventsEnabled() const {
70 return hasExtension(kExtensionVForkEvents);
71 }
72 inline void setEnabledExtensions(uint32_t extensions) {
73 _enabledExtensions = extensions;
74 }
75 inline void setForkEventsEnabled(bool fork, bool vfork) {
76 uint32_t extensions = 0;
77 if (fork) {
78 extensions |= kExtensionForkEvents;
79 }
80 if (vfork) {
81 extensions |= kExtensionVForkEvents;
82 }
83 setEnabledExtensions(extensions);
84 }
85
86public:
87 inline Address const &loadBase() const { return _loadBase; }
88 inline Address const &entryPoint() const { return _entryPoint; }
89
90public:
91 inline Thread *currentThread() const {
92 return const_cast<ProcessBase *>(this)->_currentThread;
93 }
94 Thread *thread(ThreadId tid) const;
95
96protected:
97 virtual ErrorCode initialize(ProcessId pid, uint32_t flags);
98
99public:
100 virtual ErrorCode getInfo(ProcessInfo &info);
101
102public: // ELF only
103 virtual ErrorCode getAuxiliaryVector(std::string &auxv);
104 virtual uint64_t getAuxiliaryVectorValue(uint64_t type);
105
106protected:
107 virtual void cleanup();
108
109public:
110 virtual ErrorCode detach(bool stopped) = 0;
111
112public:
113 virtual ErrorCode suspend();
114 virtual ErrorCode
115 resume(int signal = 0,
116 std::set<Thread *> const &excluded = std::set<Thread *>());
117
118public:
119 virtual ErrorCode interrupt() = 0;
120 virtual ErrorCode terminate() = 0;
121 virtual bool isAlive() const = 0;
122
123public:
124 virtual ErrorCode
125 enumerateThreads(std::function<void(Thread *)> const &cb) const;
126
127public:
128 virtual ErrorCode readString(Address const &address, std::string &str,
129 size_t length, size_t *nread = nullptr) = 0;
130 virtual ErrorCode readMemory(Address const &address, void *buffer,
131 size_t length, size_t *nread = nullptr) = 0;
132 virtual ErrorCode writeMemory(Address const &address, void const *buffer,
133 size_t length, size_t *nwritten = nullptr) = 0;
134
135public:
136 virtual ErrorCode enumerateSharedLibraries(
137 std::function<void(SharedLibraryInfo const &)> const &cb) = 0;
138 virtual ErrorCode
139 enumerateMappedFiles(std::function<void(MappedFileInfo const &)> const &cb);
140
141public:
142 ErrorCode readMemoryBuffer(Address const &address, size_t length,
143 ByteVector &buffer);
144 ErrorCode writeMemoryBuffer(Address const &address, ByteVector const &buffer,
145 size_t *nwritten = nullptr);
146 ErrorCode writeMemoryBuffer(Address const &address, ByteVector const &buffer,
147 size_t length, size_t *nwritten = nullptr);
148
149public:
150 virtual ErrorCode wait() = 0;
151
152public:
153 virtual ErrorCode allocateMemory(size_t size, uint32_t protection,
154 uint64_t *address) = 0;
155 virtual ErrorCode deallocateMemory(uint64_t address, size_t size) = 0;
156
157public:
158 virtual ErrorCode getMemoryRegionInfo(Address const &address,
159 MemoryRegionInfo &info) = 0;
160
161public:
162 virtual void getThreadIds(std::vector<ThreadId> &tids);
163
164protected:
165 virtual ErrorCode updateInfo() = 0;
166
167public:
168 virtual SoftwareBreakpointManager *softwareBreakpointManager() const final;
169 virtual HardwareBreakpointManager *hardwareBreakpointManager() const final;
170
171public:
172 virtual void prepareForDetach();
173 virtual ErrorCode beforeResume();
174 virtual ErrorCode afterResume();
175
176public:
177 virtual int getMaxBreakpoints() const { return 0; }
178 virtual int getMaxWatchpoints() const { return 0; }
179 virtual int getMaxWatchpointSize() const { return 0; }
180
181public:
182 // There shouldn't be any reason for these to be overridden.
183 virtual Architecture::GDBDescriptor const *
184 getGDBRegistersDescriptor() const final;
185 virtual Architecture::LLDBDescriptor const *
186 getLLDBRegistersDescriptor() const final;
187
188protected:
189 friend class ThreadBase;
190 virtual void insert(ThreadBase *thread);
191 virtual void remove(ThreadBase *thread);
192 virtual void removeThread(ThreadId tid);
193};
194} // namespace Target
195} // namespace ds2
Definition Types.h:96
Definition HardwareBreakpointManager.h:20
Definition SoftwareBreakpointManager.h:16
Definition ProcessBase.h:25
Definition ThreadBase.h:22
Definition RegisterLayout.h:151
Definition RegisterLayout.h:158
Definition Types.h:426
Definition Types.h:379
Definition Types.h:281
Definition Types.h:415