35e12b82b4e9614ec2f51232f635ef2d68bcf2d6
1 #ifndef _RISCV_GDBSERVER_H
2 #define _RISCV_GDBSERVER_H
9 class circular_buffer_t
12 // The buffer can store capacity-1 data elements.
13 circular_buffer_t(unsigned int capacity
) : data(new T
[capacity
]),
14 start(0), end(0), capacity(capacity
) {}
15 circular_buffer_t() : start(0), end(0), capacity(0) {}
16 ~circular_buffer_t() { delete[] data
; }
19 unsigned int start
; // Data start, inclusive.
20 unsigned int end
; // Data end, exclusive.
21 unsigned int capacity
; // Size of the buffer.
22 unsigned int size() const;
23 bool empty() const { return start
== end
; }
24 bool full() const { return ((end
+1) % capacity
) == start
; }
26 // Return size and address of the block of RAM where more data can be copied
27 // to be added to the buffer.
28 unsigned int contiguous_empty_size() const;
29 T
*contiguous_empty() { return data
+ end
; }
30 void data_added(unsigned int bytes
);
32 unsigned int contiguous_data_size() const;
33 T
*contiguous_data() { return data
+ start
; }
34 // Tell the buffer that some bytes were consumed from the start of the
36 void consume(unsigned int bytes
);
40 T
operator[](unsigned int i
) const { return data
[(start
+ i
) % capacity
]; }
42 void append(const T
*src
, unsigned int count
);
45 // Class to track software breakpoints that we set.
46 class software_breakpoint_t
53 void insert(mmu_t
* mmu
);
54 void remove(mmu_t
* mmu
);
62 operation_t(gdbserver_t
& gdbserver
) : gs(gdbserver
) {}
63 virtual ~operation_t() {}
65 // Called when the operation is first set as the current one.
66 // Return true if this operation is complete. In that case the object will
68 // Return false if more steps are required the next time the debug
69 // interrupt is clear.
70 virtual bool start() { return true; }
72 // Perform the next step of this operation (which is probably to write to
73 // Debug RAM and assert the debug interrupt).
74 // Return true if this operation is complete. In that case the object will
76 // Return false if more steps are required the next time the debug
77 // interrupt is clear.
78 virtual bool step() = 0;
86 // Create a new server, listening for connections from localhost on the given
88 gdbserver_t(uint16_t port
, sim_t
*sim
);
90 // Process all pending messages from a client.
93 void handle_packet(const std::vector
<uint8_t> &packet
);
94 void handle_interrupt();
96 void handle_breakpoint(const std::vector
<uint8_t> &packet
);
97 void handle_continue(const std::vector
<uint8_t> &packet
);
98 void handle_extended(const std::vector
<uint8_t> &packet
);
99 void handle_general_registers_read(const std::vector
<uint8_t> &packet
);
100 void continue_general_registers_read();
101 void handle_halt_reason(const std::vector
<uint8_t> &packet
);
102 void handle_kill(const std::vector
<uint8_t> &packet
);
103 void handle_memory_binary_write(const std::vector
<uint8_t> &packet
);
104 void handle_memory_read(const std::vector
<uint8_t> &packet
);
105 void handle_query(const std::vector
<uint8_t> &packet
);
106 void handle_register_read(const std::vector
<uint8_t> &packet
);
107 void continue_register_read();
108 void handle_register_write(const std::vector
<uint8_t> &packet
);
109 void handle_step(const std::vector
<uint8_t> &packet
);
111 bool connected() const { return client_fd
> 0; }
113 // TODO: Move this into its own packet sending class?
114 // Add the given message to send_buf.
115 void send(const char* msg
);
116 // Hex-encode a 64-bit value, and send it to gcc in target byte order (little
118 void send(uint64_t value
);
119 // Hex-encode a 32-bit value, and send it to gcc in target byte order (little
121 void send(uint32_t value
);
122 void send_packet(const char* data
);
123 uint8_t running_checksum
;
124 // Send "$" and clear running checksum.
126 // Send "#" and checksum.
127 void end_packet(const char* data
=NULL
);
129 // Write value to the index'th word in Debug RAM.
130 void write_debug_ram(unsigned int index
, uint32_t value
);
131 uint32_t read_debug_ram(unsigned int index
);
133 void set_interrupt(uint32_t hartid
);
139 circular_buffer_t
<uint8_t> recv_buf
;
140 circular_buffer_t
<uint8_t> send_buf
;
144 // Used to track whether we think the target is running. If we think it is
145 // but it isn't, we need to tell gdb about it.
148 std::map
<reg_t
, software_breakpoint_t
> breakpoints
;
150 // Read pending data from the client.
153 // Accept a new client if there isn't one already connected.
155 // Process all complete requests in recv_buf.
156 void process_requests();
158 operation_t
* operation
;
159 void set_operation(operation_t
* operation
);