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