DebugServer2
Loading...
Searching...
No Matches
ExtensionSet.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 <cstdint>
14#include <string>
15
16namespace ds2 {
17namespace GDBProtocol {
18
20public:
22 char const *name;
23 bool supported;
24 };
25
26 enum Extension : uint32_t {
27 kQEcho = (1u << 0),
28 kQStartNoAckMode = (1u << 1),
29 kQXferFeaturesRead = (1u << 2),
30 kQXferAuxvRead = (1u << 3),
31 kQXferLibrariesSVR4Read = (1u << 4),
32 kQXferLibrariesRead = (1u << 5),
33 kQListThreadsInStopReply = (1u << 6),
34 kQPassSignals = (1u << 7),
35 kBreakpointCommands = (1u << 8),
36 kMultiprocess = (1u << 9),
37 kQDisableRandomization = (1u << 10),
38 kQNonStop = (1u << 11),
39 kQProgramSignals = (1u << 12),
40 kQXferSiginfoRead = (1u << 13),
41 kQXferSiginfoWrite = (1u << 14),
42 kForkEvents = (1u << 15),
43 kVForkEvents = (1u << 16),
44 kQXferOSDataRead = (1u << 17),
45 kQXferThreadsRead = (1u << 18),
46 };
47
48public:
49 void reset() {
50 _requested = 0;
51 _supported = 0;
52 _enabled = 0;
53 }
54
55 void enable(Extension extension) {
56 _supported |= extension;
57 _enabled = _requested & _supported;
58 }
59
60 void disable(Extension extension) {
61 _supported &= ~static_cast<uint32_t>(extension);
62 _enabled = _requested & _supported;
63 }
64
65 void negotiate(std::string const &feature) {
66 Extension extension;
67 if (!find(feature, extension))
68 return;
69
70 _requested |= extension;
71 _enabled = _requested & _supported;
72 }
73
74 bool enabled(Extension extension) const {
75 return (_enabled & extension) != 0;
76 }
77
78 bool supported(Extension extension) const {
79 return (_supported & extension) != 0;
80 }
81
82 std::string feature(Extension extension) const {
83 return feature(extension, supported(extension));
84 }
85
86 std::string feature(Extension extension, bool state) const {
87 char const *featureName = this->name(extension);
88 if (featureName == nullptr)
89 return std::string();
90
91 std::string feature(featureName);
92 feature.push_back(state ? '+' : '-');
93 return feature;
94 }
95
96 AdvertisedFeature advertise(Extension extension,
97 bool advertiseWhenUnsupported = false) const {
98 bool isSupported = supported(extension);
99 char const *featureName = name(extension);
100 if (featureName == nullptr || (!isSupported && !advertiseWhenUnsupported))
101 return {nullptr, false};
102
103 return {featureName, isSupported};
104 }
105
106 char const *name(Extension extension) const {
107 for (auto const &entry : kEntries) {
108 if (entry.extension == extension)
109 return entry.name;
110 }
111
112 return nullptr;
113 }
114
115private:
116 bool find(std::string const &feature, Extension &extension) const {
117 for (auto const &entry : kEntries) {
118 if (feature == entry.name) {
119 extension = entry.extension;
120 return true;
121 }
122 }
123
124 return false;
125 }
126
127private:
128 struct Entry {
129 Extension extension;
130 char const *name;
131 };
132
133 static constexpr Entry kEntries[] = {
134 {kQEcho, "qEcho"},
135 {kQStartNoAckMode, "QStartNoAckMode"},
136 {kQXferFeaturesRead, "qXfer:features:read"},
137 {kQXferAuxvRead, "qXfer:auxv:read"},
138 {kQXferLibrariesSVR4Read, "qXfer:libraries-svr4:read"},
139 {kQXferLibrariesRead, "qXfer:libraries:read"},
140 {kQListThreadsInStopReply, "QListThreadsInStopReply"},
141 {kQPassSignals, "QPassSignals"},
142 {kBreakpointCommands, "BreakpointCommands"},
143 {kMultiprocess, "multiprocess"},
144 {kQDisableRandomization, "QDisableRandomization"},
145 {kQNonStop, "QNonStop"},
146 {kQProgramSignals, "QProgramSignals"},
147 {kQXferSiginfoRead, "qXfer:siginfo:read"},
148 {kQXferSiginfoWrite, "qXfer:siginfo:write"},
149 {kForkEvents, "fork-events"},
150 {kVForkEvents, "vfork-events"},
151 {kQXferOSDataRead, "qXfer:osdata:read"},
152 {kQXferThreadsRead, "qXfer:threads:read"},
153 };
154
155private:
156 uint32_t _requested = 0;
157 uint32_t _supported = 0;
158 uint32_t _enabled = 0;
159};
160
161} // namespace GDBProtocol
162} // namespace ds2
Definition ExtensionSet.h:19