1 /*
2 * Copyright 2014 Google Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but without any warranty; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #include <arch/cache.h>
16 #include <gdb.h>
17 #include <libpayload.h>
18
gdb_get_last_signal(struct gdb_message * command,int offset,struct gdb_message * reply)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
gdb_read_general_registers(struct gdb_message * command,int offset,struct gdb_message * reply)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
gdb_write_general_registers(struct gdb_message * command,int offset,struct gdb_message * reply)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
gdb_read_memory(struct gdb_message * command,int offset,struct gdb_message * reply)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
gdb_write_memory(struct gdb_message * command,int offset,struct gdb_message * reply)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
gdb_continue(struct gdb_message * command,int offset,struct gdb_message * reply)66 static void gdb_continue(struct gdb_message *command,
67 int offset, struct gdb_message *reply)
68 {
69 /* Disable single step if it's still on. */
70 gdb_arch_set_single_step(0);
71
72 /* No need to support the extension that passes in new EIP/PC. */
73 if (command->used > offset)
74 gdb_message_add_string(reply, "E00");
75 else
76 gdb_state.resumed = 1;
77 }
78
gdb_single_step(struct gdb_message * command,int offset,struct gdb_message * reply)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);
98