blob: 5137dfd9f54762214d58f92689b39d591a34ac5f [file] [log] [blame]
Julius Werner50a81742014-05-15 11:57:38 -07001/*
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 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <arch/cache.h>
20#include <gdb.h>
21#include <libpayload.h>
22
23static void gdb_get_last_signal(struct gdb_message *command,
24 int offset, struct gdb_message *reply)
25{
26 gdb_message_add_string(reply, "S");
27 gdb_message_encode_bytes(reply, &gdb_state.signal, 1);
28}
29
30static void gdb_read_general_registers(struct gdb_message *command,
31 int offset, struct gdb_message *reply)
32{
33 gdb_arch_encode_regs(reply);
34}
35
36static void gdb_write_general_registers(struct gdb_message *command,
37 int offset, struct gdb_message *reply)
38{
39 gdb_arch_decode_regs(offset, command);
40 gdb_message_add_string(reply, "OK");
41}
42
43static void gdb_read_memory(struct gdb_message *command,
44 int offset, struct gdb_message *reply)
45{
46 int tok = gdb_message_tokenize(command, &offset);
47 uintptr_t addr = gdb_message_decode_int(command, tok, offset - 1 - tok);
48 size_t length = gdb_message_decode_int(command, offset,
49 command->used - offset);
50
51 gdb_message_encode_bytes(reply, (void *)addr, length);
52}
53
54static void gdb_write_memory(struct gdb_message *command,
55 int offset, struct gdb_message *reply)
56{
57 int tok = gdb_message_tokenize(command, &offset);
58 uintptr_t addr = gdb_message_decode_int(command, tok, offset - 1 - tok);
59 tok = gdb_message_tokenize(command, &offset);
60 size_t length = gdb_message_decode_int(command, tok, offset - 1 - tok);
61
62 die_if(length * 2 != command->used - offset, "Invalid length field in "
63 "GDB command: %.*s", command->used, command->buf);
64
65 gdb_message_decode_bytes(command, offset, (void *)addr, length);
66 cache_sync_instructions();
67 gdb_message_add_string(reply, "OK");
68}
69
70static void gdb_continue(struct gdb_message *command,
71 int offset, struct gdb_message *reply)
72{
73 /* Disable single step if it's still on. */
74 gdb_arch_set_single_step(0);
75
76 /* No need to support the extension that passes in new EIP/PC. */
77 if (command->used > offset)
78 gdb_message_add_string(reply, "E00");
79 else
80 gdb_state.resumed = 1;
81}
82
83static void gdb_single_step(struct gdb_message *command,
84 int offset, struct gdb_message *reply)
85{
86 if (command->used > offset || gdb_arch_set_single_step(1))
87 gdb_message_add_string(reply, "E00");
88 else
89 gdb_state.resumed = 1;
90}
91
92struct gdb_command gdb_commands[] = {
93 { "?", &gdb_get_last_signal },
94 { "g", &gdb_read_general_registers },
95 { "G", &gdb_write_general_registers },
96 { "m", &gdb_read_memory },
97 { "M", &gdb_write_memory },
98 { "c", &gdb_continue },
99 { "s", &gdb_single_step }
100};
101const int gdb_command_count = ARRAY_SIZE(gdb_commands);