Revert "Fix compilation error (#20801)"
[mono-project.git] / mono / mini / debugger-protocol.c
blobb2aa17d2a3439767bae05eee00ccc81f76e05108
1 #include "debugger-protocol.h"
2 #include <mono/utils/atomic.h>
3 #include "glib.h"
5 static int packet_id = 0;
7 /*
8 * Functions to decode protocol data
9 */
10 int
11 m_dbgprot_buffer_add_command_header (MdbgProtBuffer *data, int command_set, int command, MdbgProtBuffer *out)
13 int id = mono_atomic_inc_i32 (&packet_id);
15 int len = data->p - data->buf + HEADER_LENGTH;
16 m_dbgprot_buffer_init (out, len);
17 m_dbgprot_buffer_add_int (out, len);
18 m_dbgprot_buffer_add_int (out, id);
19 m_dbgprot_buffer_add_byte (out, 0); /* flags */
20 m_dbgprot_buffer_add_byte (out, command_set);
21 m_dbgprot_buffer_add_byte (out, command);
22 m_dbgprot_buffer_add_data (out, data->buf, data->p - data->buf);
23 return id;
26 void
27 m_dbgprot_decode_command_header (MdbgProtBuffer *recvbuf, MdbgProtHeader *header)
29 header->len = m_dbgprot_decode_int (recvbuf->buf, &recvbuf->buf, recvbuf->end);
30 header->id = m_dbgprot_decode_int (recvbuf->buf, &recvbuf->buf, recvbuf->end);
31 header->flags = m_dbgprot_decode_byte (recvbuf->buf, &recvbuf->buf, recvbuf->end);
32 if (header->flags == REPLY_PACKET) {
33 header->error = m_dbgprot_decode_byte (recvbuf->buf, &recvbuf->buf, recvbuf->end);
34 header->error_2 = m_dbgprot_decode_byte (recvbuf->buf, &recvbuf->buf, recvbuf->end);
36 else {
37 header->command_set = m_dbgprot_decode_byte (recvbuf->buf, &recvbuf->buf, recvbuf->end);
38 header->command = m_dbgprot_decode_byte (recvbuf->buf, &recvbuf->buf, recvbuf->end);
42 int
43 m_dbgprot_decode_byte (uint8_t *buf, uint8_t **endbuf, uint8_t *limit)
45 *endbuf = buf + 1;
46 g_assert (*endbuf <= limit);
47 return buf [0];
50 int
51 m_dbgprot_decode_int (uint8_t *buf, uint8_t **endbuf, uint8_t *limit)
53 *endbuf = buf + 4;
54 g_assert (*endbuf <= limit);
56 return (((int)buf [0]) << 24) | (((int)buf [1]) << 16) | (((int)buf [2]) << 8) | (((int)buf [3]) << 0);
59 int64_t
60 m_dbgprot_decode_long (uint8_t *buf, uint8_t **endbuf, uint8_t *limit)
62 uint32_t high = m_dbgprot_decode_int (buf, &buf, limit);
63 uint32_t low = m_dbgprot_decode_int (buf, &buf, limit);
65 *endbuf = buf;
67 return ((((uint64_t)high) << 32) | ((uint64_t)low));
70 int
71 m_dbgprot_decode_id (uint8_t *buf, uint8_t **endbuf, uint8_t *limit)
73 return m_dbgprot_decode_int (buf, endbuf, limit);
76 char*
77 m_dbgprot_decode_string (uint8_t *buf, uint8_t **endbuf, uint8_t *limit)
79 int len = m_dbgprot_decode_int (buf, &buf, limit);
80 char *s;
82 if (len < 0) {
83 *endbuf = buf;
84 return NULL;
87 s = (char *)g_malloc (len + 1);
88 g_assert (s);
90 memcpy (s, buf, len);
91 s [len] = '\0';
92 buf += len;
93 *endbuf = buf;
95 return s;
98 uint8_t*
99 m_dbgprot_decode_byte_array (uint8_t *buf, uint8_t **endbuf, uint8_t *limit, uint32_t *len)
101 *len = m_dbgprot_decode_int (buf, &buf, limit);
102 uint8_t* s;
104 if (len < 0) {
105 *endbuf = buf;
106 return NULL;
109 s = (uint8_t*)g_malloc (*len);
110 g_assert (s);
112 memcpy (s, buf, *len);
113 buf += *len;
114 *endbuf = buf;
116 return s;
120 * Functions to encode protocol data
123 void
124 m_dbgprot_buffer_init (MdbgProtBuffer *buf, int size)
126 buf->buf = (uint8_t *)g_malloc (size);
127 buf->p = buf->buf;
128 buf->end = buf->buf + size;
132 m_dbgprot_buffer_len (MdbgProtBuffer *buf)
134 return buf->p - buf->buf;
137 void
138 m_dbgprot_buffer_make_room (MdbgProtBuffer *buf, int size)
140 if (buf->end - buf->p < size) {
141 int new_size = buf->end - buf->buf + size + 32;
142 uint8_t *p = (uint8_t *)g_realloc (buf->buf, new_size);
143 size = buf->p - buf->buf;
144 buf->buf = p;
145 buf->p = p + size;
146 buf->end = buf->buf + new_size;
150 void
151 m_dbgprot_buffer_add_byte (MdbgProtBuffer *buf, uint8_t val)
153 m_dbgprot_buffer_make_room (buf, 1);
154 buf->p [0] = val;
155 buf->p++;
158 void
159 m_dbgprot_buffer_add_short (MdbgProtBuffer *buf, uint32_t val)
161 m_dbgprot_buffer_make_room (buf, 2);
162 buf->p [0] = (val >> 8) & 0xff;
163 buf->p [1] = (val >> 0) & 0xff;
164 buf->p += 2;
167 void
168 m_dbgprot_buffer_add_int (MdbgProtBuffer *buf, uint32_t val)
170 m_dbgprot_buffer_make_room (buf, 4);
171 buf->p [0] = (val >> 24) & 0xff;
172 buf->p [1] = (val >> 16) & 0xff;
173 buf->p [2] = (val >> 8) & 0xff;
174 buf->p [3] = (val >> 0) & 0xff;
175 buf->p += 4;
178 void
179 m_dbgprot_buffer_add_long (MdbgProtBuffer *buf, uint64_t l)
181 m_dbgprot_buffer_add_int (buf, (l >> 32) & 0xffffffff);
182 m_dbgprot_buffer_add_int (buf, (l >> 0) & 0xffffffff);
185 void
186 m_dbgprot_buffer_add_id (MdbgProtBuffer *buf, int id)
188 m_dbgprot_buffer_add_int (buf, (uint64_t)id);
191 void
192 m_dbgprot_buffer_add_data (MdbgProtBuffer *buf, uint8_t *data, int len)
194 m_dbgprot_buffer_make_room (buf, len);
195 memcpy (buf->p, data, len);
196 buf->p += len;
199 void
200 m_dbgprot_buffer_add_utf16 (MdbgProtBuffer *buf, uint8_t *data, int len)
202 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
203 m_dbgprot_buffer_make_room (buf, len);
204 memcpy (buf->p, data, len);
205 #else
206 for (int i=0; i<len; i +=2) {
207 buf->p[i] = data[i+1];
208 buf->p[i+1] = data[i];
210 #endif
211 buf->p += len;
214 void
215 m_dbgprot_buffer_add_string (MdbgProtBuffer *buf, const char *str)
217 int len;
219 if (str == NULL) {
220 m_dbgprot_buffer_add_int (buf, 0);
221 } else {
222 len = strlen (str);
223 m_dbgprot_buffer_add_int (buf, len);
224 m_dbgprot_buffer_add_data (buf, (uint8_t*)str, len);
228 void
229 m_dbgprot_buffer_add_byte_array (MdbgProtBuffer *buf, uint8_t *bytes, uint32_t arr_len)
231 m_dbgprot_buffer_add_int (buf, arr_len);
232 m_dbgprot_buffer_add_data (buf, bytes, arr_len);
235 void
236 m_dbgprot_buffer_add_buffer (MdbgProtBuffer *buf, MdbgProtBuffer *data)
238 m_dbgprot_buffer_add_data (buf, data->buf, m_dbgprot_buffer_len (data));
241 void
242 m_dbgprot_buffer_free (MdbgProtBuffer *buf)
244 g_free (buf->buf);
247 const char*
248 m_dbgprot_event_to_string (MdbgProtEventKind event)
250 switch (event) {
251 case MDBGPROT_EVENT_KIND_VM_START: return "VM_START";
252 case MDBGPROT_EVENT_KIND_VM_DEATH: return "VM_DEATH";
253 case MDBGPROT_EVENT_KIND_THREAD_START: return "THREAD_START";
254 case MDBGPROT_EVENT_KIND_THREAD_DEATH: return "THREAD_DEATH";
255 case MDBGPROT_EVENT_KIND_APPDOMAIN_CREATE: return "APPDOMAIN_CREATE";
256 case MDBGPROT_EVENT_KIND_APPDOMAIN_UNLOAD: return "APPDOMAIN_UNLOAD";
257 case MDBGPROT_EVENT_KIND_METHOD_ENTRY: return "METHOD_ENTRY";
258 case MDBGPROT_EVENT_KIND_METHOD_EXIT: return "METHOD_EXIT";
259 case MDBGPROT_EVENT_KIND_ASSEMBLY_LOAD: return "ASSEMBLY_LOAD";
260 case MDBGPROT_EVENT_KIND_ASSEMBLY_UNLOAD: return "ASSEMBLY_UNLOAD";
261 case MDBGPROT_EVENT_KIND_BREAKPOINT: return "BREAKPOINT";
262 case MDBGPROT_EVENT_KIND_STEP: return "STEP";
263 case MDBGPROT_EVENT_KIND_TYPE_LOAD: return "TYPE_LOAD";
264 case MDBGPROT_EVENT_KIND_EXCEPTION: return "EXCEPTION";
265 case MDBGPROT_EVENT_KIND_KEEPALIVE: return "KEEPALIVE";
266 case MDBGPROT_EVENT_KIND_USER_BREAK: return "USER_BREAK";
267 case MDBGPROT_EVENT_KIND_USER_LOG: return "USER_LOG";
268 case MDBGPROT_EVENT_KIND_CRASH: return "CRASH";
269 default:
270 g_assert_not_reached ();
271 return "";