| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include <arch/cache.h> |
| 16 |
#include <gdb.h> |
| 17 |
#include <libpayload.h> |
| 18 |
|
| 19 |
static void gdb_get_last_signal(struct gdb_message *command, |
| 20 |
int offset, struct gdb_message *reply) |
| 21 |
{ |
| 22 |
gdb_message_add_string(reply, "S"); |
| 23 |
gdb_message_encode_bytes(reply, &gdb_state.signal, 1); |
| 24 |
} |
| 25 |
|
| 26 |
static void gdb_read_general_registers(struct gdb_message *command, |
| 27 |
int offset, struct gdb_message *reply) |
| 28 |
{ |
| 29 |
gdb_arch_encode_regs(reply); |
| 30 |
} |
| 31 |
|
| 32 |
static void gdb_write_general_registers(struct gdb_message *command, |
| 33 |
int offset, struct gdb_message *reply) |
| 34 |
{ |
| 35 |
gdb_arch_decode_regs(offset, command); |
| 36 |
gdb_message_add_string(reply, "OK"); |
| 37 |
} |
| 38 |
|
| 39 |
static void gdb_read_memory(struct gdb_message *command, |
| 40 |
int offset, struct gdb_message *reply) |
| 41 |
{ |
| 42 |
int tok = gdb_message_tokenize(command, &offset); |
| 43 |
uintptr_t addr = gdb_message_decode_int(command, tok, offset - 1 - tok); |
| 44 |
size_t length = gdb_message_decode_int(command, offset, |
| 45 |
command->used - offset); |
| 46 |
|
| 47 |
gdb_message_encode_bytes(reply, (void *)addr, length); |
| 48 |
} |
| 49 |
|
| 50 |
static void gdb_write_memory(struct gdb_message *command, |
| 51 |
int offset, struct gdb_message *reply) |
| 52 |
{ |
| 53 |
int tok = gdb_message_tokenize(command, &offset); |
| 54 |
uintptr_t addr = gdb_message_decode_int(command, tok, offset - 1 - tok); |
| 55 |
tok = gdb_message_tokenize(command, &offset); |
| 56 |
size_t length = gdb_message_decode_int(command, tok, offset - 1 - tok); |
| 57 |
|
| 58 |
die_if(length * 2 != command->used - offset, "Invalid length field in " |
| 59 |
"GDB command: %.*s", command->used, command->buf); |
| 60 |
|
| 61 |
gdb_message_decode_bytes(command, offset, (void *)addr, length); |
| 62 |
cache_sync_instructions(); |
| 63 |
gdb_message_add_string(reply, "OK"); |
| 64 |
} |
| 65 |
|
| 66 |
static void gdb_continue(struct gdb_message *command, |
| 67 |
int offset, struct gdb_message *reply) |
| 68 |
{ |
| 69 |
|
| 70 |
gdb_arch_set_single_step(0); |
| 71 |
|
| 72 |
|
| 73 |
if (command->used > offset) |
| 74 |
gdb_message_add_string(reply, "E00"); |
| 75 |
else |
| 76 |
gdb_state.resumed = 1; |
| 77 |
} |
| 78 |
|
| 79 |
static void gdb_single_step(struct gdb_message *command, |
| 80 |
int offset, struct gdb_message *reply) |
| 81 |
{ |
| 82 |
if (command->used > offset || gdb_arch_set_single_step(1)) |
| 83 |
gdb_message_add_string(reply, "E00"); |
| 84 |
else |
| 85 |
gdb_state.resumed = 1; |
| 86 |
} |
| 87 |
|
| 88 |
struct gdb_command gdb_commands[] = { |
| 89 |
{ "?", &gdb_get_last_signal }, |
| 90 |
{ "g", &gdb_read_general_registers }, |
| 91 |
{ "G", &gdb_write_general_registers }, |
| 92 |
{ "m", &gdb_read_memory }, |
| 93 |
{ "M", &gdb_write_memory }, |
| 94 |
{ "c", &gdb_continue }, |
| 95 |
{ "s", &gdb_single_step } |
| 96 |
}; |
| 97 |
const int gdb_command_count = ARRAY_SIZE(gdb_commands); |