1 /*! \file */
2 #ifndef LIBGDBR_H
3 #define LIBGDBR_H
4 
5 #include <stdint.h>
6 #ifdef _MSC_VER
7 typedef unsigned int ssize_t;
8 #endif
9 
10 #include "arch.h"
11 #include "r_types_base.h"
12 #include "r_socket.h"
13 #include "r_th.h"
14 
15 #define MSG_OK 0
16 #define MSG_NOT_SUPPORTED -1
17 #define MSG_ERROR_1 -2
18 
19 #define GDB_REMOTE_TYPE_GDB 0
20 #define GDB_REMOTE_TYPE_LLDB 1
21 #define GDB_MAX_PKTSZ 4
22 
23 /*!
24  * Structure that saves a gdb message
25  */
26 typedef struct libgdbr_message_t {
27 	ssize_t len; /*! Len of the message */
28 	char *msg;      /*! Pointer to the buffer that contains the message */
29 	uint8_t chk;    /*! Cheksum of the current message read from the packet */
30 } libgdbr_message_t;
31 
32 /*!
33  * Structure that stores features supported
34  */
35 
36 typedef struct libgdbr_stub_features_t {
37 	ut32 pkt_sz; /* Max packet size */
38 	bool qXfer_btrace_read;
39 	bool qXfer_btrace_conf_read;
40 	bool qXfer_spu_read;
41 	bool qXfer_spu_write;
42 	bool qXfer_libraries_read;
43 	bool qXfer_libraries_svr4_read;
44 	bool qXfer_siginfo_read;
45 	bool qXfer_siginfo_write;
46 	bool qXfer_auxv_read;
47 	bool qXfer_exec_file_read;
48 	bool qXfer_features_read;
49 	bool qXfer_memory_map_read;
50 	bool qXfer_sdata_read;
51 	bool qXfer_threads_read;
52 	bool qXfer_traceframe_info_read;
53 	bool qXfer_uib_read;
54 	bool qXfer_fdpic_read;
55 	bool qXfer_osdata_read;
56 	bool Qbtrace_off;
57 	bool Qbtrace_bts;
58 	bool Qbtrace_pt;
59 	bool Qbtrace_conf_bts_size;
60 	bool Qbtrace_conf_pt_size;
61 	bool QNonStop;
62 	bool QCatchSyscalls;
63 	bool QPassSignals;
64 	bool QStartNoAckMode;
65 	bool QAgent;
66 	bool QAllow;
67 	bool QDisableRandomization;
68 	bool QTBuffer_size;
69 	bool QThreadEvents;
70 	bool StaticTracepoint;
71 	bool InstallInTrace;
72 	bool ConditionalBreakpoints;
73 	bool ConditionalTracepoints;
74 	bool ReverseContinue;
75 	bool ReverseStep;
76 	bool swbreak;
77 	bool hwbreak;
78 	bool fork_events;
79 	bool vfork__events;
80 	bool exec_events;
81 	bool vContSupported;
82 	bool no_resumed;
83 	bool augmented_libraries_svr4_read;
84 	bool multiprocess;
85 	bool TracepointSource;
86 	bool EnableDisableTracepoints;
87 	bool tracenz;
88 	bool BreakpointCommands;
89 	// lldb-specific features
90 	struct {
91 		bool g;
92 		bool QThreadSuffixSupported;
93 		bool QListThreadsInStopReply;
94 		bool qEcho;
95 	} lldb;
96 	// Cannot be determined with qSupported, found out on query
97 	bool qC;
98 	int extended_mode;
99 	struct {
100 		bool c, C, s, S, t, r;
101 	} vcont;
102 	bool P;
103 } libgdbr_stub_features_t;
104 
105 /*!
106  * Structure for fstat data sent by gdb remote server
107  */
108 R_PACKED(
109 typedef struct libgdbr_fstat_t {
110 	unsigned dev;
111 	unsigned ino;
112 	unsigned mode;
113 	unsigned numlinks;
114 	unsigned uid;
115 	unsigned gid;
116 	unsigned rdev;
117 	uint64_t size;
118 	uint64_t blksize;
119 	uint64_t blocks;
120 	unsigned atime;
121 	unsigned mtime;
122 	unsigned ctime;
123 }) libgdbr_fstat_t;
124 
125 /*!
126  * Stores information from the stop-reply packet (why target stopped)
127  */
128 typedef struct libgdbr_stop_reason {
129 	unsigned signum;
130 	int core;
131 	int reason;
132 	bool syscall;
133 	bool library;
134 	bool swbreak;
135 	bool hwbreak;
136 	bool create;
137 	bool vforkdone;
138 	bool is_valid;
139 	struct {
140 		bool present;
141 		ut64 addr;
142 	} watchpoint;
143 	struct {
144 		bool present;
145 		char *path;
146 	} exec;
147 	struct {
148 		bool present;
149 		int pid;
150 		int tid;
151 	} thread, fork, vfork;
152 } libgdbr_stop_reason_t;
153 
154 /*!
155  * Core "object" that saves
156  * the instance of the lib
157  */
158 typedef struct libgdbr_t {
159 	char *send_buff; // defines a buffer for reading and sending stuff
160 	ssize_t send_len;
161 	ssize_t send_max; // defines the maximal len for the given buffer
162 	char *read_buff;
163 	ssize_t read_max; // defines the maximal len for the given buffer
164 	ssize_t read_len; // len of read_buff (if read_buff not fully consumed)
165 
166 	// is already handled (i.e. already send or ...)
167 	RSocket *sock;
168 	int connected;
169 	int acks;
170 	char *data;
171 	ssize_t data_len;
172 	ssize_t data_max;
173 	gdb_reg_t *registers;
174 	int last_code;
175 	int pid; // little endian
176 	int tid; // little endian
177 	int page_size; // page size for target (useful for qemu)
178 	bool attached; // Remote server attached to process or created
179 	libgdbr_stub_features_t stub_features;
180 
181 	int remote_file_fd; // For remote file I/O
182 	int num_retries; // number of retries for packet reading
183 
184 	int remote_type;
185 	bool no_ack;
186 	bool is_server;
187 	bool server_debug;
188 	bool get_baddr;
189 	libgdbr_stop_reason_t stop_reason;
190 
191 	RThreadLock *gdbr_lock;
192 	int gdbr_lock_depth; // current depth inside the recursive lock
193 
194 	// parsed from target
195 	struct {
196 		char *regprofile;
197 		int arch, bits;
198 		bool valid;
199 	} target;
200 
201 	bool isbreaked;
202 } libgdbr_t;
203 
204 /*!
205  * \brief Function initializes the libgdbr lib
206  * \returns a failure code (currently -1) or 0 if call successfully
207  */
208 int gdbr_init(libgdbr_t *g, bool is_server);
209 
210 /*!
211  * \brief Function initializes the architecture of the gdbsession
212  * \param architecture defines the architecure used (registersize, and such)
213  * \returns false on failure
214  */
215 bool gdbr_set_architecture(libgdbr_t *g, int arch, int bits);
216 
217 /*!
218  * \brief Function get gdb registers profile based on arch and bits
219  * \param architecture and bit size.
220  * \returns a failure code
221  */
222 const char *gdbr_get_reg_profile(int arch, int bits);
223 
224 /*!
225  * \brief Function set the gdbr internal registers profile
226  * \param registers profile string which shares the same format as RReg API
227  * \returns a failure code
228  */
229 int gdbr_set_reg_profile(libgdbr_t *g, const char *str);
230 
231 /*!
232  * \brief frees all buffers and cleans the libgdbr instance stuff
233  * \returns a failure code (currently -1) or 0 if call successfully
234  */
235 int gdbr_cleanup(libgdbr_t *g);
236 
237 #endif
238