1 : /*
2 : * Copyright (c) 2010 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can be
4 : * found in the LICENSE file.
5 : */
6 :
7 :
8 : // This module provides interfaces for writing to and reading from a
9 : // GDB RSP connection. The connection uses a generic transport
10 : // object for sending packets from one endpoint to another. The session
11 : // is responsible to delievery including generation of checksums and
12 : // retransmits and handling timeouts as needed. See:
13 : // http:// ftp.gnu.org/old-gnu/Manuals/gdb-5.1.1/html_node/gdb_129.html#SEC134
14 : //
15 : // All data is read one character at a time through 'GetChar' which will
16 : // poll on DataAvail and timeout after one second, allowing the session
17 : // to handle blocking transports.
18 : //
19 : // The session object is not reentrant, and will have unpredictable
20 : // results if two threads attempt to read from the session at the same
21 : // time.
22 : //
23 : // This module may throw a std::bad_alloc if there is an error while trying
24 : // to resize the packet. In addition, the underlying ITransport is free
25 : // to throw an exception if the connection is lost, which will pass out
26 : // of any packet send or receive function.
27 : #ifndef NATIVE_CLIENT_GDB_RSP_SESSION_H_
28 : #define NATIVE_CLIENT_GDB_RSP_SESSION_H_ 1
29 :
30 : #include <sstream>
31 :
32 : #include "native_client/src/include/portability.h"
33 : #include "native_client/src/trusted/debug_stub/transport.h"
34 :
35 : namespace gdb_rsp {
36 :
37 : class Packet;
38 :
39 : // Session is not inteded to be derived from, protected members are
40 : // protected only for unit testing purposes.
41 : //
42 : // Note that the Session object is not thread-safe.
43 : class Session {
44 : public:
45 : explicit Session(port::ITransport *transport);
46 : virtual ~Session();
47 :
48 : enum {
49 : IGNORE_ACK = 1, // Do not emit or wait for '+' from RSP stream.
50 : USE_SEQ = 2, // Automatically use a sequence number
51 : DEBUG_SEND = 4, // Log all SENDs
52 : DEBUG_RECV = 8, // Log all RECVs
53 : DEBUG_MASK = (DEBUG_SEND | DEBUG_RECV)
54 : };
55 :
56 : public:
57 : virtual void SetFlags(uint32_t flags);
58 : virtual void ClearFlags(uint32_t flags);
59 : virtual uint32_t GetFlags();
60 :
61 : virtual bool SendPacketOnly(Packet *packet);
62 : virtual bool SendPacket(Packet *packet);
63 : virtual bool GetPacket(Packet *packet);
64 : // Is there any data available right now.
65 : virtual bool IsDataAvailable();
66 : virtual bool Connected();
67 : virtual void Disconnect();
68 :
69 148 : void WaitForDebugStubEvent(struct NaClApp *nap, bool ignore_gdb) {
70 148 : io_->WaitForDebugStubEvent(nap, ignore_gdb);
71 148 : }
72 :
73 : protected:
74 : virtual bool GetChar(char *ch);
75 :
76 : private:
77 : Session(const Session&);
78 : Session &operator=(const Session&);
79 :
80 : protected:
81 : port::ITransport *io_; // Transport object not owned by the Session.
82 : uint32_t flags_; // Session flags for Sequence/Ack generation.
83 : uint8_t seq_; // Next sequence number to use or -1.
84 : bool connected_; // Is the connection still valid.
85 : };
86 :
87 : } // namespace gdb_rsp
88 :
89 : #endif // NATIVE_CLIENT_GDB_RSP_SESSION_H_
90 :
|