NOR: add optional "flash erase_address" sector padding
[openocd.git] / src / server / gdb_server.c
index ef8d7f22900dfd45b80fd21df0354a2044b52045..4191cc2a7c664ac699fb353b4d3ac219d0b0e5c3 100644 (file)
@@ -2,7 +2,7 @@
  *   Copyright (C) 2005 by Dominic Rath                                    *
  *   Dominic.Rath@gmx.de                                                   *
  *                                                                         *
- *   Copyright (C) 2007,2008 Øyvind Harboe                                 *
+ *   Copyright (C) 2007-2009 Øyvind Harboe                                 *
  *   oyvind.harboe@zylin.com                                               *
  *                                                                         *
  *   Copyright (C) 2008 by Spencer Oliver                                  *
 #include "config.h"
 #endif
 
-#include "breakpoints.h"
-#include "target_request.h"
-#include "register.h"
+#include <target/breakpoints.h>
+#include <target/target_request.h>
+#include <target/register.h>
 #include "server.h"
-#include "flash.h"
+#include <flash/nor/core.h>
 #include "gdb_server.h"
-#include "image.h"
+#include <target/image.h>
 #include <jtag/jtag.h>
 
 
+/**
+ * @file
+ * GDB server implementation.
+ *
+ * This implements the GDB Remote Serial Protocol, over TCP connections,
+ * giving GDB access to the JTAG or other hardware debugging facilities
+ * found in most modern embedded processors.
+ */
+
+/* private connection data for GDB */
+struct gdb_connection
+{
+       char buffer[GDB_BUFFER_SIZE];
+       char *buf_p;
+       int buf_cnt;
+       int ctrl_c;
+       enum target_state frontend_state;
+       struct image *vflash_image;
+       int closed;
+       int busy;
+       int noack_mode;
+       bool sync;      /* set flag to true if you want the next stepi to return immediately.
+                      allowing GDB to pick up a fresh set of register values from the target
+                      without modifying the target state. */
+
+};
+
+
 #if 0
 #define _DEBUG_GDB_IO_
 #endif
@@ -48,7 +76,8 @@ static enum breakpoint_type gdb_breakpoint_override_type;
 
 extern int gdb_error(struct connection *connection, int retval);
 static unsigned short gdb_port = 3333;
-static const char *DIGITS = "0123456789abcdef";
+static unsigned short gdb_port_next = 0;
+static const char DIGITS[16] = "0123456789abcdef";
 
 static void gdb_log_callback(void *priv, const char *file, unsigned line,
                const char *function, const char *string);
@@ -129,30 +158,11 @@ int check_pending(struct connection *connection, int timeout_s, int *got_data)
        return ERROR_OK;
 }
 
-int gdb_get_char(struct connection *connection, int* next_char)
+static int gdb_get_char_inner(struct connection *connection, int* next_char)
 {
        struct gdb_connection *gdb_con = connection->priv;
        int retval = ERROR_OK;
 
-#ifdef _DEBUG_GDB_IO_
-       char *debug_buffer;
-#endif
-
-       if (gdb_con->buf_cnt-- > 0)
-       {
-               *next_char = *(gdb_con->buf_p++);
-               if (gdb_con->buf_cnt > 0)
-                       connection->input_pending = 1;
-               else
-                       connection->input_pending = 0;
-
-#ifdef _DEBUG_GDB_IO_
-               LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
-#endif
-
-               return ERROR_OK;
-       }
-
        for (;;)
        {
                if (connection->service->type == CONNECTION_PIPE)
@@ -237,6 +247,50 @@ int gdb_get_char(struct connection *connection, int* next_char)
        return retval;
 }
 
+/**
+ * The cool thing about this fn is that it allows buf_p and buf_cnt to be
+ * held in registers in the inner loop.
+ *
+ * For small caches and embedded systems this is important!
+ */
+static inline int gdb_get_char_fast(struct connection *connection, int* next_char, char **buf_p, int *buf_cnt)
+{
+       int retval = ERROR_OK;
+
+       if ((*buf_cnt)-- > 0)
+       {
+               *next_char = **buf_p;
+               (*buf_p)++;
+               if (*buf_cnt > 0)
+                       connection->input_pending = 1;
+               else
+                       connection->input_pending = 0;
+
+#ifdef _DEBUG_GDB_IO_
+               LOG_DEBUG("returned char '%c' (0x%2.2x)", *next_char, *next_char);
+#endif
+
+               return ERROR_OK;
+       }
+
+       struct gdb_connection *gdb_con = connection->priv;
+       gdb_con->buf_p = *buf_p;
+       gdb_con->buf_cnt = *buf_cnt;
+       retval = gdb_get_char_inner(connection, next_char);
+       *buf_p = gdb_con->buf_p;
+       *buf_cnt = gdb_con->buf_cnt;
+
+       return retval;
+}
+
+
+int gdb_get_char(struct connection *connection, int* next_char)
+{
+       struct gdb_connection *gdb_con = connection->priv;
+       return gdb_get_char_fast(connection, next_char, &gdb_con->buf_p, &gdb_con->buf_cnt);
+}
+
+
 int gdb_putback_char(struct connection *connection, int last_char)
 {
        struct gdb_connection *gdb_con = connection->priv;
@@ -441,27 +495,33 @@ static __inline__ int fetch_packet(struct connection *connection, int *checksum_
        unsigned char my_checksum = 0;
        char checksum[3];
        int character;
-       int retval;
+       int retval = ERROR_OK;
 
        struct gdb_connection *gdb_con = connection->priv;
        my_checksum = 0;
        int count = 0;
        count = 0;
+
+       /* move this over into local variables to use registers and give the
+        * more freedom to optimize */
+       char *buf_p = gdb_con->buf_p;
+       int buf_cnt = gdb_con->buf_cnt;
+
        for (;;)
        {
                /* The common case is that we have an entire packet with no escape chars.
                 * We need to leave at least 2 bytes in the buffer to have
                 * gdb_get_char() update various bits and bobs correctly.
                 */
-               if ((gdb_con->buf_cnt > 2) && ((gdb_con->buf_cnt + count) < *len))
+               if ((buf_cnt > 2) && ((buf_cnt + count) < *len))
                {
                        /* The compiler will struggle a bit with constant propagation and
                         * aliasing, so we help it by showing that these values do not
                         * change inside the loop
                         */
                        int i;
-                       char *buf = gdb_con->buf_p;
-                       int run = gdb_con->buf_cnt - 2;
+                       char *buf = buf_p;
+                       int run = buf_cnt - 2;
                        i = 0;
                        int done = 0;
                        while (i < run)
@@ -493,19 +553,21 @@ static __inline__ int fetch_packet(struct connection *connection, int *checksum_
                                        buffer[count++] = character & 0xff;
                                }
                        }
-                       gdb_con->buf_p += i;
-                       gdb_con->buf_cnt -= i;
+                       buf_p += i;
+                       buf_cnt -= i;
                        if (done)
                                break;
                }
                if (count > *len)
                {
                        LOG_ERROR("packet buffer too small");
-                       return ERROR_GDB_BUFFER_TOO_SMALL;
+                       retval = ERROR_GDB_BUFFER_TOO_SMALL;
+                       break;
                }
 
-               if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
-                       return retval;
+               retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
+               if (retval != ERROR_OK)
+                       break;
 
                if (character == '#')
                        break;
@@ -515,8 +577,11 @@ static __inline__ int fetch_packet(struct connection *connection, int *checksum_
                        /* data transmitted in binary mode (X packet)
                         * uses 0x7d as escape character */
                        my_checksum += character & 0xff;
-                       if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
-                               return retval;
+
+                       retval = gdb_get_char_fast(connection, &character, &buf_p, &buf_cnt);
+                       if (retval != ERROR_OK)
+                               break;
+
                        my_checksum += character & 0xff;
                        buffer[count++] = (character ^ 0x20) & 0xff;
                }
@@ -527,6 +592,12 @@ static __inline__ int fetch_packet(struct connection *connection, int *checksum_
                }
        }
 
+       gdb_con->buf_p = buf_p;
+       gdb_con->buf_cnt = buf_cnt;
+
+       if (retval != ERROR_OK)
+               return retval;
+
        *len = count;
 
        if ((retval = gdb_get_char(connection, &character)) != ERROR_OK)
@@ -881,7 +952,7 @@ void gdb_str_to_target(struct target *target, char *tstr, struct reg *reg)
        }
 }
 
-static int hextoint(char c)
+static int hextoint(int c)
 {
        if (c>='0'&&c<='9')
        {
@@ -1352,7 +1423,7 @@ int gdb_breakpoint_watchpoint_packet(struct connection *connection, struct targe
 {
        int type;
        enum breakpoint_type bp_type = BKPT_SOFT /* dummy init to avoid warning */;
-       enum watchpoint_rw wp_type;
+       enum watchpoint_rw wp_type = WPT_READ /* dummy init to avoid warning */;
        uint32_t address;
        uint32_t size;
        char *separator;
@@ -1372,6 +1443,12 @@ int gdb_breakpoint_watchpoint_packet(struct connection *connection, struct targe
                wp_type = WPT_READ;
        else if (type == 4) /* access watchpoint */
                wp_type = WPT_ACCESS;
+       else
+       {
+               LOG_ERROR("invalid gdb watch/breakpoint type(%d), dropping connection", type);
+               return ERROR_SERVER_REMOTE_CLOSED;
+       }
+
 
        if (gdb_breakpoint_override && ((bp_type == BKPT_SOFT)||(bp_type == BKPT_HARD)))
        {
@@ -1422,7 +1499,7 @@ int gdb_breakpoint_watchpoint_packet(struct connection *connection, struct targe
                {
                        if (packet[0] == 'Z')
                        {
-                               if ((retval = watchpoint_add(target, address, size, type-2, 0, 0xffffffffu)) != ERROR_OK)
+                               if ((retval = watchpoint_add(target, address, size, wp_type, 0, 0xffffffffu)) != ERROR_OK)
                                {
                                        if ((retval = gdb_error(connection, retval)) != ERROR_OK)
                                                return retval;
@@ -1851,9 +1928,19 @@ int gdb_v_packet(struct connection *connection, struct target *target, char *pac
                flash_set_dirty();
 
                /* perform any target specific operations before the erase */
-               target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_START);
-               result = flash_erase_address_range(gdb_service->target, addr, length);
-               target_call_event_callbacks(gdb_service->target, TARGET_EVENT_GDB_FLASH_ERASE_END);
+               target_call_event_callbacks(gdb_service->target,
+                               TARGET_EVENT_GDB_FLASH_ERASE_START);
+
+               /* vFlashErase:addr,length messages require region start and
+                * end to be "block" aligned ... if padding is ever needed,
+                * GDB will have become dangerously confused.
+                */
+               result = flash_erase_address_range(gdb_service->target,
+                               false, addr, length);
+
+               /* perform any target specific operations after the erase */
+               target_call_event_callbacks(gdb_service->target,
+                               TARGET_EVENT_GDB_FLASH_ERASE_END);
 
                /* perform erase */
                if (result != ERROR_OK)
@@ -2210,6 +2297,7 @@ static int gdb_target_start(struct target *target, uint16_t port)
        return ERROR_OK;
 }
 
+/* FIXME static */
 int gdb_target_add_one(struct target *target)
 {
        if (gdb_port == 0 && server_use_pipes == 0)
@@ -2217,6 +2305,8 @@ int gdb_target_add_one(struct target *target)
                LOG_INFO("gdb port disabled");
                return ERROR_OK;
        }
+       if (0 == gdb_port_next)
+               gdb_port_next = gdb_port;
 
        bool use_pipes = server_use_pipes;
        static bool server_started_with_pipes = false;
@@ -2229,10 +2319,12 @@ int gdb_target_add_one(struct target *target)
                use_pipes = false;
        }
 
-       int e = gdb_target_start(target, use_pipes ? 0 : gdb_port++);
+       int e = gdb_target_start(target, use_pipes ? 0 : gdb_port_next);
        if (ERROR_OK == e)
+       {
                server_started_with_pipes |= use_pipes;
-
+               gdb_port_next++;
+       }
        return e;
 }
 
@@ -2278,7 +2370,10 @@ COMMAND_HANDLER(handle_gdb_sync_command)
 /* daemon configuration command gdb_port */
 COMMAND_HANDLER(handle_gdb_port_command)
 {
-       return CALL_COMMAND_HANDLER(server_port_command, &gdb_port);
+       int retval = CALL_COMMAND_HANDLER(server_port_command, &gdb_port);
+       if (ERROR_OK == retval)
+               gdb_port_next = gdb_port;
+       return retval;
 }
 
 COMMAND_HANDLER(handle_gdb_memory_map_command)
@@ -2342,7 +2437,7 @@ COMMAND_HANDLER(handle_gdb_breakpoint_override_command)
 static const struct command_registration gdb_command_handlers[] = {
        {
                .name = "gdb_sync",
-               .handler = &handle_gdb_sync_command,
+               .handler = handle_gdb_sync_command,
                .mode = COMMAND_ANY,
                .help = "next stepi will return immediately allowing "
                        "GDB to fetch register state without affecting "
@@ -2350,39 +2445,41 @@ static const struct command_registration gdb_command_handlers[] = {
        },
        {
                .name = "gdb_port",
-               .handler = &handle_gdb_port_command,
+               .handler = handle_gdb_port_command,
                .mode = COMMAND_ANY,
-               .help = "daemon configuration command gdb_port",
-               .usage = "<port>",
+               .help = "Display or specify base port on which to listen "
+                       "for incoming GDB connections.  "
+                       "No arguments reports GDB port; zero disables.",
+               .usage = "[port_num]",
        },
        {
                .name = "gdb_memory_map",
-               .handler = &handle_gdb_memory_map_command,
+               .handler = handle_gdb_memory_map_command,
                .mode = COMMAND_CONFIG,
                .help = "enable or disable memory map",
-               .usage = "enable|disable"
+               .usage = "('enable'|'disable')"
        },
        {
                .name = "gdb_flash_program",
-               .handler = &handle_gdb_flash_program_command,
+               .handler = handle_gdb_flash_program_command,
                .mode = COMMAND_CONFIG,
                .help = "enable or disable flash program",
-               .usage = "enable|disable"
+               .usage = "('enable'|'disable')"
        },
        {
                .name = "gdb_report_data_abort",
-               .handler = &handle_gdb_report_data_abort_command,
+               .handler = handle_gdb_report_data_abort_command,
                .mode = COMMAND_CONFIG,
                .help = "enable or disable reporting data aborts",
-               .usage = "enable|disable"
+               .usage = "('enable'|'disable')"
        },
        {
                .name = "gdb_breakpoint_override",
-               .handler = &handle_gdb_breakpoint_override_command,
+               .handler = handle_gdb_breakpoint_override_command,
                .mode = COMMAND_EXEC,
-               .help = "force type of breakpoint "
-                       "used by gdb 'break' commands.",
-               .usage = "hard|soft|disable",
+               .help = "Display or specify type of breakpoint "
+                       "to be used by gdb 'break' commands.",
+               .usage = "('hard'|'soft'|'disable')"
        },
        COMMAND_REGISTRATION_DONE
 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)