1 #include "debugger-protocol.h"
2 #include <mono/utils/atomic.h>
5 static int packet_id
= 0;
8 * Functions to decode protocol data
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
);
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
);
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
);
43 m_dbgprot_decode_byte (uint8_t *buf
, uint8_t **endbuf
, uint8_t *limit
)
46 g_assert (*endbuf
<= limit
);
51 m_dbgprot_decode_int (uint8_t *buf
, uint8_t **endbuf
, uint8_t *limit
)
54 g_assert (*endbuf
<= limit
);
56 return (((int)buf
[0]) << 24) | (((int)buf
[1]) << 16) | (((int)buf
[2]) << 8) | (((int)buf
[3]) << 0);
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
);
67 return ((((uint64_t)high
) << 32) | ((uint64_t)low
));
71 m_dbgprot_decode_id (uint8_t *buf
, uint8_t **endbuf
, uint8_t *limit
)
73 return m_dbgprot_decode_int (buf
, endbuf
, limit
);
77 m_dbgprot_decode_string (uint8_t *buf
, uint8_t **endbuf
, uint8_t *limit
)
79 int len
= m_dbgprot_decode_int (buf
, &buf
, limit
);
87 s
= (char *)g_malloc (len
+ 1);
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
);
109 s
= (uint8_t*)g_malloc (*len
);
112 memcpy (s
, buf
, *len
);
120 * Functions to encode protocol data
124 m_dbgprot_buffer_init (MdbgProtBuffer
*buf
, int size
)
126 buf
->buf
= (uint8_t *)g_malloc (size
);
128 buf
->end
= buf
->buf
+ size
;
132 m_dbgprot_buffer_len (MdbgProtBuffer
*buf
)
134 return buf
->p
- buf
->buf
;
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
;
146 buf
->end
= buf
->buf
+ new_size
;
151 m_dbgprot_buffer_add_byte (MdbgProtBuffer
*buf
, uint8_t val
)
153 m_dbgprot_buffer_make_room (buf
, 1);
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;
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;
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);
186 m_dbgprot_buffer_add_id (MdbgProtBuffer
*buf
, int id
)
188 m_dbgprot_buffer_add_int (buf
, (uint64_t)id
);
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
);
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
);
206 for (int i
=0; i
<len
; i
+=2) {
207 buf
->p
[i
] = data
[i
+1];
208 buf
->p
[i
+1] = data
[i
];
215 m_dbgprot_buffer_add_string (MdbgProtBuffer
*buf
, const char *str
)
220 m_dbgprot_buffer_add_int (buf
, 0);
223 m_dbgprot_buffer_add_int (buf
, len
);
224 m_dbgprot_buffer_add_data (buf
, (uint8_t*)str
, len
);
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
);
236 m_dbgprot_buffer_add_buffer (MdbgProtBuffer
*buf
, MdbgProtBuffer
*data
)
238 m_dbgprot_buffer_add_data (buf
, data
->buf
, m_dbgprot_buffer_len (data
));
242 m_dbgprot_buffer_free (MdbgProtBuffer
*buf
)
248 m_dbgprot_event_to_string (MdbgProtEventKind 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";
270 g_assert_not_reached ();