DebugServer2
Loading...
Searching...
No Matches
Types.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/Base.h"
14#include "DebugServer2/Constants.h"
15#include "DebugServer2/Core/CPUTypes.h"
16#include "DebugServer2/Core/ErrorCodes.h"
17
18#include <cstdint>
19#include <cstdlib>
20#include <map>
21#include <optional>
22#include <string>
23#include <vector>
24#if !defined(OS_WIN32)
25#include <unistd.h>
26#endif
27
28namespace ds2 {
29
30//
31// Basic Types
32//
33
34#if defined(OS_WIN32)
35typedef DWORD ProcessId;
36typedef DWORD ThreadId;
37typedef PSID UserId;
38typedef PSID GroupId;
39#define PRI_PID "lu"
40#else
41typedef pid_t ProcessId;
42typedef pid_t ThreadId;
43typedef uid_t UserId;
44typedef gid_t GroupId;
45#define PRI_PID "d"
46#endif
47
48static const ProcessId kAllProcessId = static_cast<ProcessId>(-1);
49static const ProcessId kAnyProcessId = static_cast<ProcessId>(0);
50static const ThreadId kAllThreadId = static_cast<ThreadId>(-1);
51static const ThreadId kAnyThreadId = static_cast<ThreadId>(0);
52
53typedef std::vector<uint8_t> ByteVector;
54
55//
56// Process/Thread Id Tuple
57//
59 ProcessId pid;
60 ThreadId tid;
61
62 ProcessThreadId(ProcessId _pid = kAnyProcessId, ThreadId _tid = kAnyThreadId)
63 : pid(_pid), tid(_tid) {}
64
65 inline bool validPid() const {
66 return pid != kAllProcessId && pid != kAnyProcessId;
67 }
68
69 inline bool validTid() const {
70 return tid != kAllThreadId && tid != kAnyThreadId;
71 }
72
73 inline bool valid() const { return validPid() || validTid(); }
74
75 inline bool any() const { return !validPid() && !validTid(); }
76
77 inline bool all() const { return tid == kAllThreadId; }
78
79 inline void clear() {
80 pid = kAnyProcessId;
81 tid = kAnyThreadId;
82 }
83};
84
85//
86// STL Types
87//
88
89typedef std::vector<std::string> StringCollection;
90typedef std::map<std::string, std::string> EnvironmentBlock;
91
92//
93// Represents an address
94//
95
96class Address {
97private:
98 uint64_t _value;
99 bool _unset;
100
101public:
102 Address() : _value(0), _unset(true) {}
103
104 Address(uint64_t address) : _value(address), _unset(false) {}
105
106public:
107 inline operator uint64_t() const { return _value; }
108 inline Address &operator=(uint64_t address) {
109 _unset = false;
110 _value = address;
111 return *this;
112 }
113
114public:
115 inline bool valid() const { return !_unset; }
116
117public:
118 inline uint64_t value() const { return _value; }
119
120public:
121 inline void unset() {
122 _unset = true;
123 _value = 0;
124 }
125 inline void clear() { unset(); }
126};
127
128//
129// Repesents the stop information of a process.
130//
131
132struct StopInfo {
133 enum Event {
134 kEventNone,
135 kEventStop,
136 kEventExit,
137 kEventKill,
138 };
139
140 enum Reason {
141 kReasonNone,
142 kReasonWriteWatchpoint,
143 kReasonReadWatchpoint,
144 kReasonAccessWatchpoint,
145 kReasonBreakpoint,
146 kReasonTrace,
147 kReasonSignalStop, // TODO better name
148 kReasonTrap,
149 kReasonThreadSpawn,
150 kReasonThreadEntry,
151 kReasonThreadExit,
152 kReasonFork,
153 kReasonVFork,
154 kReasonVForkDone,
155#if defined(OS_WIN32)
156 kReasonMemoryError,
157 kReasonMemoryAlignment,
158 kReasonMathError,
159 kReasonInstructionError,
160 kReasonLibraryEvent,
161 kReasonDebugOutput,
162 kReasonUserException,
163#endif
164 };
165
166 Event event;
167 Reason reason;
168 // TODO: status and signal should be an union.
169 int status;
170 int signal;
171#if defined(OS_WIN32)
172 std::string debugString;
173#endif
174
175 int core;
176
177 Address watchpointAddress;
178 int watchpointIndex;
179
180 // Set from siginfo_t's si_addr for memory-fault signals (SIGSEGV/SIGBUS).
181 // The GDB-remote stop-reply description field conventionally carries this
182 // as an "address=<hex>" substring, letting a client locate the faulting
183 // expression for post-mortem analysis.
184 std::optional<Address> fault;
185
186 // Set for kReasonFork/kReasonVFork: the pid/tid of the newly forked
187 // child. We don't support debugging the child (it's detached and left
188 // to run free), but the parent's stop still needs to report who it was,
189 // per the fork-events/vfork-events GDB-remote extension.
190 ProcessThreadId child;
191
192 StopInfo() { clear(); }
193
194 inline void clear() {
195 event = kEventNone;
196 reason = kReasonNone;
197 status = 0;
198 signal = 0;
199#if defined(OS_WIN32)
200 debugString.clear();
201#endif
202 core = -1;
203 watchpointAddress = 0;
204 watchpointIndex = -1;
205 fault.reset();
206 child.clear();
207 }
208};
209
210//
211// Constant for invalid native CPU (sub)type.
212//
213static uint32_t const kInvalidCPUType = static_cast<uint32_t>(-2);
214
215//
216// Architecture Flags
217//
218enum {
219 kArchFlagWatchpointExceptionsReceivedAfter = (0 << 0),
220 kArchFlagWatchpointExceptionsReceivedBefore = (1 << 0)
221};
222
223//
224// Describes the host information.
225//
226struct HostInfo {
227 CPUType cpuType;
228 CPUSubType cpuSubType;
229 uint32_t nativeCPUType;
230 uint32_t nativeCPUSubType;
231 std::string hostName;
232 std::string osType;
233 std::string osVendor;
234 std::string osBuild;
235 std::string osKernel;
236 std::string osVersion;
237 Endian endian;
238 size_t pointerSize;
239 uint32_t archFlags;
240 uint32_t defaultPacketTimeout;
241
242 HostInfo() { clear(); }
243
244 inline void clear() {
245 cpuType = kCPUTypeAny;
246 cpuSubType = kCPUSubTypeInvalid;
247 nativeCPUType = kInvalidCPUType;
248 nativeCPUSubType = kInvalidCPUType;
249 endian = kEndianUnknown;
250 pointerSize = 0;
251 archFlags = 0;
252 defaultPacketTimeout = 0;
253 }
254};
255
256#if defined(OS_WIN32)
257namespace {
258inline BOOL AllocateAndCopySid(const PSID pSid, PSID *ppSid) noexcept {
259 if (!IsValidSid(pSid))
260 return CreateWellKnownSid(WinNullSid, nullptr, ppSid, nullptr);
261
262 UCHAR nSubAuthorityCount = *GetSidSubAuthorityCount(pSid);
263 return AllocateAndInitializeSid(GetSidIdentifierAuthority(pSid),
264 nSubAuthorityCount,
265 nSubAuthorityCount > 0 ? *GetSidSubAuthority(pSid, 0) : 0,
266 nSubAuthorityCount > 1 ? *GetSidSubAuthority(pSid, 1) : 0,
267 nSubAuthorityCount > 2 ? *GetSidSubAuthority(pSid, 2) : 0,
268 nSubAuthorityCount > 3 ? *GetSidSubAuthority(pSid, 3) : 0,
269 nSubAuthorityCount > 4 ? *GetSidSubAuthority(pSid, 4) : 0,
270 nSubAuthorityCount > 5 ? *GetSidSubAuthority(pSid, 5) : 0,
271 nSubAuthorityCount > 6 ? *GetSidSubAuthority(pSid, 6) : 0,
272 nSubAuthorityCount > 7 ? *GetSidSubAuthority(pSid, 7) : 0,
273 ppSid);
274}
275}
276#endif
277
278//
279// Describe a process
280//
282 typedef std::vector<ProcessInfo> Collection;
283
284 ProcessId pid;
285#if !defined(OS_WIN32)
286 ProcessId parentPid;
287#endif
288
289 std::string name;
290
291 UserId realUid;
292 GroupId realGid;
293#if !defined(OS_WIN32)
294 UserId effectiveUid;
295 GroupId effectiveGid;
296#endif
297
298 CPUType cpuType;
299 CPUSubType cpuSubType;
300 uint32_t nativeCPUType;
301 uint32_t nativeCPUSubType;
302 Endian endian;
303 size_t pointerSize;
304 uint32_t archFlags;
305
306 std::string osType;
307 std::string osVendor;
308
309 ProcessInfo() { clear(); }
310
311 inline void clear() {
312 pid = kAnyProcessId;
313#if !defined(OS_WIN32)
314 parentPid = kAnyProcessId;
315#endif
316
317 name.clear();
318
319#if defined(OS_WIN32)
320 realUid = nullptr;
321 realGid = nullptr;
322#else
323 realUid = 0;
324 realGid = 0;
325 effectiveUid = 0;
326 effectiveGid = 0;
327#endif
328
329 cpuType = kCPUTypeAny;
330 cpuSubType = kCPUSubTypeInvalid;
331 nativeCPUType = kInvalidCPUType;
332 nativeCPUSubType = kInvalidCPUType;
333 endian = kEndianUnknown;
334 pointerSize = 0;
335 archFlags = 0;
336
337 osType.clear();
338 osVendor.clear();
339 }
340
341#if defined(OS_WIN32)
342 ProcessInfo(const ProcessInfo &other)
343 : pid(other.pid), name(other.name), cpuType(other.cpuType),
344 cpuSubType(other.cpuSubType), nativeCPUType(other.nativeCPUType),
345 nativeCPUSubType(other.nativeCPUSubType), endian(other.endian),
346 pointerSize(other.pointerSize), archFlags(other.archFlags),
347 osType(other.osType), osVendor(other.osVendor) {
348 *this = other;
349 }
350
351
352 ProcessInfo &operator=(const ProcessInfo &rhs) {
353 pid = rhs.pid;
354 name = rhs.name;
355 AllocateAndCopySid(rhs.realUid, &realUid);
356 AllocateAndCopySid(rhs.realGid, &realGid);
357 cpuType = rhs.cpuType;
358 cpuSubType = rhs.cpuSubType;
359 nativeCPUType = rhs.nativeCPUType;
360 nativeCPUSubType = rhs.nativeCPUSubType;
361 endian = rhs.endian;
362 pointerSize = rhs.pointerSize;
363 archFlags = rhs.archFlags;
364 osType = rhs.osType;
365 osVendor = rhs.osVendor;
366 return *this;
367 }
368
369 ~ProcessInfo() {
370 FreeSid(realUid);
371 FreeSid(realGid);
372 }
373#endif
374};
375
376//
377// Describes a memory region
378//
380 typedef std::vector<MemoryRegionInfo> Collection;
381
382 Address start;
383 uint64_t length;
384 uint32_t protection;
385 std::string name;
386#if defined(OS_LINUX)
387 std::string backingFile;
388 uint64_t backingFileOffset;
389 uint64_t backingFileInode;
390#endif
391
392 MemoryRegionInfo() { clear(); }
393 MemoryRegionInfo(Address const &start_, uint64_t length_,
394 uint64_t protection_, std::string name_) {
395 clear();
396 start = start_;
397 length = length_;
398 protection = protection_;
399 name = name_;
400 }
401
402 inline void clear() {
403 start.clear();
404 length = 0;
405 protection = 0;
406 name.clear();
407#if defined(OS_LINUX)
408 backingFile.clear();
409 backingFileOffset = 0;
410 backingFileInode = 0;
411#endif
412 }
413};
414
416 std::string path;
417 bool main;
418 struct {
419 uint64_t mapAddress;
420 uint64_t baseAddress;
421 uint64_t ldAddress;
422 } svr4;
423 std::vector<uint64_t> sections;
424};
425
427 std::string path;
428 uint64_t baseAddress;
429 uint64_t size;
430};
431
433 std::string uuid;
434 std::string triple;
435 std::string file_path;
436 uint64_t file_offset;
437 uint64_t file_size;
438};
439
440} // namespace ds2
Definition Types.h:96
Definition Types.h:226
Definition Types.h:426
Definition Types.h:379
Definition Types.h:432
Definition Types.h:281
Definition Types.h:58
Definition Types.h:415
Definition Types.h:132