DebugServer2
Loading...
Searching...
No Matches
SessionBase.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/GDBRemote/PacketProcessor.h"
14#include "DebugServer2/GDBRemote/ProtocolHelpers.h"
15#include "DebugServer2/GDBRemote/ProtocolInterpreter.h"
16#include "DebugServer2/GDBRemote/Types.h"
17#include "DebugServer2/Host/Channel.h"
18#include "DebugServer2/Utils/Log.h"
19
20#include <algorithm>
21#include <iomanip>
22#include <sstream>
23#include <type_traits>
24
25namespace ds2 {
26namespace GDBRemote {
27
28class SessionDelegate;
29
31private:
32 Host::Channel *_channel;
33 PacketProcessor _processor;
34 ProtocolInterpreter _interpreter;
35
36protected:
37 SessionDelegate *_delegate;
38 bool _ackmode;
39 CompatibilityMode _compatMode;
40
41public:
42 SessionBase(CompatibilityMode mode);
43 virtual ~SessionBase() = default;
44
45public:
46 CompatibilityMode mode() const { return _compatMode; }
47
48protected:
49 const char *getPacketSeparator();
50
51public:
52 inline void setDelegate(SessionDelegate *delegate) { _delegate = delegate; }
53 inline SessionDelegate *delegate() const {
54 return const_cast<SessionBase *>(this)->_delegate;
55 }
56
57public:
58 bool create(Host::Channel *channel);
59
60public:
61 bool receive(bool cooked);
62 bool parse(std::string const &data);
63
64public:
65 bool send(char const *data, bool escaped = false) {
66 return send(std::string(data), escaped);
67 }
68
69 template <typename T> bool send(T const &data, bool escaped = false) {
70 std::ostringstream ss;
71 static std::string const searchStr = "$#}*";
72 uint8_t csum;
73
74 ss << '$';
75
76 //
77 // If data contains $, #, } or * we need to escape the
78 // stream.
79 //
80 if (!escaped &&
81 std::find_first_of(data.begin(), data.end(), searchStr.begin(),
82 searchStr.end()) != data.end()) {
83 std::string encoded = Escape(data);
84 ss << encoded;
85 csum = Checksum(encoded);
86 } else {
87 for (char c : data) {
88 ss << c;
89 }
90 csum = Checksum(data);
91 }
92
93 ss << '#' << std::hex << std::setw(2) << std::setfill('0')
94 << (unsigned)csum;
95
96 std::string final_data = ss.str();
97 DS2LOG(Packet, "putpkt(\"%s\", %u)", final_data.c_str(),
98 (unsigned)final_data.length());
99
100 return _channel->send(final_data);
101 }
102
103protected:
104 bool sendACK();
105 bool sendNAK();
106 inline bool sendOK() { return send("OK"); }
107 bool sendError(ErrorCode code);
108
109public:
110 inline bool getAckMode() { return _ackmode; }
111
112protected:
113 inline void setAckMode(bool enabled) { _ackmode = enabled; }
114
115public:
116 inline ProtocolInterpreter &interpreter() const {
117 return const_cast<SessionBase *>(this)->_interpreter;
118 }
119
120protected:
121 friend class ProtocolInterpreter;
122 virtual bool onACK();
123 virtual bool onNAK();
124 virtual bool onCommandReceived(bool valid);
125 virtual void onInvalidData(std::string const &data);
126};
127} // namespace GDBRemote
128} // namespace ds2
Definition PacketProcessor.h:20
Definition ProtocolInterpreter.h:21
Definition SessionBase.h:30
Definition SessionDelegate.h:20
Definition Channel.h:18
Definition ProtocolInterpreter.h:81